JS backend: added wraping to object the local vars which captured in closure.
Moved local functions and function literals to class/namespace definition. (cherry picked from commit 36c954b)
This commit is contained in:
@@ -14,10 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
var foo = Kotlin.definePackage({box:function(){
|
||||
return !false;
|
||||
var foo = Kotlin.definePackage({initialize: function () {
|
||||
}, box: function () {
|
||||
return !false;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function test() {
|
||||
return foo.box()
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
{
|
||||
var items = function () {
|
||||
var A = Kotlin.createClass({initialize: function () {
|
||||
var A = Kotlin.createClass(null, {initialize: function () {
|
||||
this.$order = '';
|
||||
{
|
||||
this.set_order(this.get_order() + 'A');
|
||||
|
||||
@@ -97,10 +97,11 @@ var Kotlin = {};
|
||||
function subclass() {
|
||||
}
|
||||
|
||||
function create() {
|
||||
var parent = null, properties = Kotlin.argumentsToArrayLike(arguments);
|
||||
if (typeof (properties[0]) == "function") {
|
||||
parent = properties.shift();
|
||||
function create(parent, properties, staticProperties) {
|
||||
var traits = null;
|
||||
if (parent instanceof Array) {
|
||||
traits = parent;
|
||||
parent = parent[0];
|
||||
}
|
||||
|
||||
function klass() {
|
||||
@@ -111,13 +112,19 @@ var Kotlin = {};
|
||||
}
|
||||
|
||||
klass.addMethods = addMethods;
|
||||
klass.superclass = parent;
|
||||
klass.superclass = parent || null;
|
||||
klass.subclasses = [];
|
||||
|
||||
if (parent) {
|
||||
subclass.prototype = parent.prototype;
|
||||
klass.prototype = new subclass();
|
||||
parent.subclasses.push(klass);
|
||||
if (typeof (parent) == "function") {
|
||||
subclass.prototype = parent.prototype;
|
||||
klass.prototype = new subclass();
|
||||
parent.subclasses.push(klass);
|
||||
}
|
||||
else {
|
||||
// trait
|
||||
klass.addMethods(parent);
|
||||
}
|
||||
}
|
||||
|
||||
klass.addMethods({get_class: function () {
|
||||
@@ -131,8 +138,13 @@ var Kotlin = {};
|
||||
}});
|
||||
}
|
||||
|
||||
for (var i = 0, length = properties.length; i < length; i++) {
|
||||
klass.addMethods(properties[i]);
|
||||
if (traits !== null) {
|
||||
for (var i = 1, n = traits.length; i < n; i++) {
|
||||
klass.addMethods(traits[i]);
|
||||
}
|
||||
}
|
||||
if (properties !== null && properties !== undefined) {
|
||||
klass.addMethods(properties);
|
||||
}
|
||||
|
||||
if (!klass.prototype.initialize) {
|
||||
@@ -140,6 +152,9 @@ var Kotlin = {};
|
||||
}
|
||||
|
||||
klass.prototype.constructor = klass;
|
||||
if (staticProperties !== null && staticProperties !== undefined) {
|
||||
copyProperties(klass, staticProperties);
|
||||
}
|
||||
return klass;
|
||||
}
|
||||
|
||||
@@ -151,7 +166,13 @@ var Kotlin = {};
|
||||
return create;
|
||||
})();
|
||||
|
||||
Kotlin.$createClass = Kotlin.createClass;
|
||||
Kotlin.$createClass = function (parent, properties) {
|
||||
if (parent !== null && typeof (parent) != "function") {
|
||||
properties = parent;
|
||||
parent = null;
|
||||
}
|
||||
return Kotlin.createClass(parent, properties, null);
|
||||
};
|
||||
|
||||
Kotlin.createObjectWithPrototype = function (prototype) {
|
||||
function C() {}
|
||||
|
||||
@@ -66,13 +66,13 @@ var Kotlin = Object.create(null);
|
||||
return proto;
|
||||
}
|
||||
|
||||
Kotlin.createTrait = function (bases, properties) {
|
||||
return createClass(bases, null, properties, false);
|
||||
Kotlin.createTrait = function (bases, properties, staticProperties) {
|
||||
return createClass(bases, null, properties, staticProperties, false);
|
||||
};
|
||||
|
||||
Kotlin.createClass = function (bases, initializer, properties) {
|
||||
Kotlin.createClass = function (bases, initializer, properties, staticProperties) {
|
||||
// proto must be created for class even if it is not needed (requires for is operator)
|
||||
return createClass(bases, initializer === null ? function () {} : initializer, properties, true);
|
||||
return createClass(bases, initializer === null ? function () {} : initializer, properties, staticProperties, true);
|
||||
};
|
||||
|
||||
function computeProto2(bases, properties) {
|
||||
@@ -94,7 +94,7 @@ var Kotlin = Object.create(null);
|
||||
return o;
|
||||
};
|
||||
|
||||
function createClass(bases, initializer, properties, isClass) {
|
||||
function createClass(bases, initializer, properties, staticProperties, isClass) {
|
||||
var proto;
|
||||
var baseInitializer;
|
||||
if (bases === null) {
|
||||
@@ -125,6 +125,10 @@ var Kotlin = Object.create(null);
|
||||
Object.freeze(initializer);
|
||||
}
|
||||
|
||||
if (staticProperties !== null && staticProperties !== undefined) {
|
||||
Object.defineProperties(constructor, staticProperties);
|
||||
}
|
||||
|
||||
Object.freeze(constructor);
|
||||
return constructor;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user