From 2a0c7445c3073484ecf0f3cd1a7772a0f1d08eec Mon Sep 17 00:00:00 2001 From: Jason Zhekov Date: Tue, 22 Dec 2015 15:27:02 +0200 Subject: [PATCH] TypeScript extends should not replace parent scope variables - close #449 --- .../ObjC/Inheritance/ObjCTypeScriptExtend.mm | 11 +++-- .../app/Inheritance/TypeScriptTests.js | 43 +++++++++++-------- .../app/Inheritance/TypeScriptTests.ts | 19 +++++--- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/NativeScript/ObjC/Inheritance/ObjCTypeScriptExtend.mm b/src/NativeScript/ObjC/Inheritance/ObjCTypeScriptExtend.mm index 62aa64282..75626baf0 100644 --- a/src/NativeScript/ObjC/Inheritance/ObjCTypeScriptExtend.mm +++ b/src/NativeScript/ObjC/Inheritance/ObjCTypeScriptExtend.mm @@ -80,10 +80,13 @@ EncodedJSValue ObjCTypeScriptExtendFunction(ExecState* execState) { JSScope* scope = callFrame->scope(callFrame->codeBlock()->scopeRegister().offset()); Identifier constructorName = Identifier::fromString(execState, name); - JSValue value = JSScope::resolve(execState, scope, constructorName); - if (value.isObject()) { - PutPropertySlot slot(value); - value.put(execState, constructorName, derivedConstructor, slot); + JSValue containingScope = JSScope::resolve(execState, scope, constructorName); + if (containingScope.isObject()) { + JSValue currentValue = containingScope.get(execState, constructorName); + if (currentValue.isCell() && currentValue.asCell() == typeScriptConstructor) { + PutPropertySlot slot(containingScope); + containingScope.put(execState, constructorName, derivedConstructor, slot); + } } // imp_implementationWithBlock calls block copy, class copy and initialize gets skipped diff --git a/tests/TestRunner/app/Inheritance/TypeScriptTests.js b/tests/TestRunner/app/Inheritance/TypeScriptTests.js index a9f219719..ea6ad0c2e 100644 --- a/tests/TestRunner/app/Inheritance/TypeScriptTests.js +++ b/tests/TestRunner/app/Inheritance/TypeScriptTests.js @@ -1,10 +1,4 @@ // TODO: Use TypeScript definitions when they get ready -var __extends = (this && this.__extends) || function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); -}; var TSObject = (function (_super) { __extends(TSObject, _super); function TSObject() { @@ -108,18 +102,6 @@ var TSObject1 = (function (_super) { TSObject1.ObjCProtocols = [TNSBaseProtocol2]; return TSObject1; })(NSObject); -var A = (function () { - function A() { - } - return A; -})(); -var B = (function (_super) { - __extends(B, _super); - function B() { - _super.apply(this, arguments); - } - return B; -})(A); var UnusedConstructor = (function (_super) { __extends(UnusedConstructor, _super); function UnusedConstructor() { @@ -213,6 +195,31 @@ describe(module.id, function () { 'baseProtocolProperty1Optional called'); }); it('PlainExtends', function () { + var A = (function () { + function A() { + } + return A; + })(); + var B = (function (_super) { + __extends(B, _super); + function B() { + _super.apply(this, arguments); + } + return B; + })(A); expect(new B() instanceof A).toBe(true); }); + it('Scope', function () { + global["Derived"] = 3; + var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + _super.apply(this, arguments); + } + return Derived; + })(NSObject); + ; + expect(global["Derived"]).toBe(3); + delete global["Derived"]; + }); }); diff --git a/tests/TestRunner/app/Inheritance/TypeScriptTests.ts b/tests/TestRunner/app/Inheritance/TypeScriptTests.ts index ba74bbbe6..d9fcf3453 100644 --- a/tests/TestRunner/app/Inheritance/TypeScriptTests.ts +++ b/tests/TestRunner/app/Inheritance/TypeScriptTests.ts @@ -151,12 +151,6 @@ class TSObject1 extends NSObject { public static ObjCProtocols = [TNSBaseProtocol2]; } -class A { -} - -class B extends A { -} - class UnusedConstructor extends NSObject { private x = 3; } @@ -286,6 +280,19 @@ describe(module.id, function () { }); it('PlainExtends', function () { + class A { + } + + class B extends A { + } + expect(new B() instanceof A).toBe(true); }); + + it('Scope', function () { + global["Derived"] = 3; + class Derived extends NSObject { }; + expect(global["Derived"]).toBe(3); + delete global["Derived"]; + }); });