From e53270ad3fbc325ad1f7a06a1e78dd5b6b3acc0d Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Sun, 10 Mar 2013 04:02:15 +0400 Subject: [PATCH] Fixed ECMAScript3 implementation of Kotlin.$new. Added bind polyfill. --- .../testFiles/kotlin_lib_ecma3.js | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/js/js.translator/testFiles/kotlin_lib_ecma3.js b/js/js.translator/testFiles/kotlin_lib_ecma3.js index ded8f10e831..46ba6fd3b44 100644 --- a/js/js.translator/testFiles/kotlin_lib_ecma3.js +++ b/js/js.translator/testFiles/kotlin_lib_ecma3.js @@ -12,6 +12,31 @@ var Kotlin = {}; var emptyFunction = function () { }; + if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 internal IsCallable function + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function () { + }, + fBound = function () { + return fToBind.apply(this instanceof fNOP && oThis + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + fNOP.prototype = this.prototype; + fBound.prototype = new fNOP(); + + return fBound; + }; + } + Kotlin.keys = Object.keys || function (o) { var result = []; var i = 0; @@ -129,8 +154,14 @@ var Kotlin = {}; Kotlin.$createClass = Kotlin.createClass; + Kotlin.createObjectWithPrototype = function (prototype) { + function C() {} + C.prototype = prototype; + return new C(); + }; + Kotlin.$new = function (f) { - var o = {'__proto__': f.prototype}; + var o = Kotlin.createObjectWithPrototype(f.prototype); return function () { f.apply(o, arguments); return o;