Browse Source

applySuperConstructorOn:withArguments:

Herby Vojčík 4 years ago
parent
commit
57b98cbef6
5 changed files with 120 additions and 0 deletions
  1. 1 0
      lang/API-CHANGES.txt
  2. 27 0
      lang/src/Kernel-Classes.js
  3. 7 0
      lang/src/Kernel-Classes.st
  4. 59 0
      lang/src/Kernel-Tests.js
  5. 26 0
      lang/src/Kernel-Tests.st

+ 1 - 0
lang/API-CHANGES.txt

@@ -4,6 +4,7 @@
 
 + Behavior >>
   + alternateConstructorViaSelector:
+  + applySuperConstructorOn:withArguments:
   + beJavaScriptSubclassOf:
   + javaScriptConstructor
   + javaScriptConstructor:

+ 27 - 0
lang/src/Kernel-Classes.js

@@ -208,6 +208,33 @@ return $recv($globals.BlockClosure)._javaScriptConstructorFor_initializingVia_($
 }; }),
 $globals.Behavior);
 
+$core.addMethod(
+$core.method({
+selector: "applySuperConstructorOn:withArguments:",
+protocol: "accessing",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["anObject", "anArray"],
+source: "applySuperConstructorOn: anObject withArguments: anArray\x0a\x09<inlineJS: '\x0a\x09\x09Object.getPrototypeOf($self.fn.prototype).constructor\x0a\x09\x09.apply(anObject, anArray)\x0a\x09'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [["inlineJS:", ["\x0a\x09\x09Object.getPrototypeOf($self.fn.prototype).constructor\x0a\x09\x09.apply(anObject, anArray)\x0a\x09"]]],
+messageSends: []
+}, function ($methodClass){ return function (anObject,anArray){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+
+		Object.getPrototypeOf($self.fn.prototype).constructor
+		.apply(anObject, anArray)
+	;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"applySuperConstructorOn:withArguments:",{anObject:anObject,anArray:anArray})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Behavior);
+
 $core.addMethod(
 $core.method({
 selector: "basicNew",

+ 7 - 0
lang/src/Kernel-Classes.st

@@ -44,6 +44,13 @@ allSuperclasses
 		yourself
 !
 
+applySuperConstructorOn: anObject withArguments: anArray
+	<inlineJS: '
+		Object.getPrototypeOf($self.fn.prototype).constructor
+		.apply(anObject, anArray)
+	'>
+!
+
 basicOrganization
 	^ organization
 !

+ 59 - 0
lang/src/Kernel-Tests.js

@@ -2351,6 +2351,33 @@ return self;
 }; }),
 $globals.ClassTest);
 
+$core.addMethod(
+$core.method({
+selector: "jsConstructorWithAction",
+protocol: "running",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "jsConstructorWithAction\x0a\x09<inlineJS: '\x0a\x09\x09function Foo(aFoo){this.foo = aFoo;}\x0a\x09\x09return Foo;\x0a\x09'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [["inlineJS:", ["\x0a\x09\x09function Foo(aFoo){this.foo = aFoo;}\x0a\x09\x09return Foo;\x0a\x09"]]],
+messageSends: []
+}, function ($methodClass){ return function (){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+
+		function Foo(aFoo){this.foo = aFoo;}
+		return Foo;
+	;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"jsConstructorWithAction",{})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.ClassTest);
+
 $core.addMethod(
 $core.method({
 selector: "setUp",
@@ -2470,6 +2497,38 @@ return self;
 }; }),
 $globals.ClassTest);
 
+$core.addMethod(
+$core.method({
+selector: "testApplySuperConstructor",
+protocol: "tests",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testApplySuperConstructor\x0a\x09| instance constructor |\x0a\x09theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.\x0a\x09constructor := self jsConstructorWithAction.\x0a\x09theClass beJavaScriptSubclassOf: constructor.\x0a\x09Compiler new\x0a\x09\x09install: 'bar: anObject\x0a\x09\x09\x09ObjectMock2\x0a\x09\x09\x09\x09applySuperConstructorOn: self\x0a\x09\x09\x09\x09withArguments: {anObject}'\x0a\x09\x09forClass: theClass\x0a\x09\x09protocol: 'tests'.\x0a\x09\x22testing specific to late-coupled detached root class\x22\x0a\x09instance := (theClass alternateConstructorViaSelector: #bar:) newValue: 7.\x0a\x09self assert: instance class == theClass.\x0a\x09self assert: instance isJavaScriptInstanceOf: constructor.\x0a\x09self assert: instance foo equals: 7",
+referencedClasses: ["ObjectMock", "Compiler"],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["copyClass:named:", "jsConstructorWithAction", "beJavaScriptSubclassOf:", "install:forClass:protocol:", "new", "newValue:", "alternateConstructorViaSelector:", "assert:", "==", "class", "assert:isJavaScriptInstanceOf:", "assert:equals:", "foo"]
+}, function ($methodClass){ return function (){
+var self=this,$self=this;
+var instance,constructor;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+$self.theClass=$recv($self.builder)._copyClass_named_($globals.ObjectMock,"ObjectMock2");
+constructor=$self._jsConstructorWithAction();
+$recv($self.theClass)._beJavaScriptSubclassOf_(constructor);
+$recv($recv($globals.Compiler)._new())._install_forClass_protocol_("bar: anObject\x0a\x09\x09\x09ObjectMock2\x0a\x09\x09\x09\x09applySuperConstructorOn: self\x0a\x09\x09\x09\x09withArguments: {anObject}",$self.theClass,"tests");
+instance=$recv($recv($self.theClass)._alternateConstructorViaSelector_("bar:"))._newValue_((7));
+$self._assert_($recv($recv(instance)._class()).__eq_eq($self.theClass));
+$self._assert_isJavaScriptInstanceOf_(instance,constructor);
+$self._assert_equals_($recv(instance)._foo(),(7));
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testApplySuperConstructor",{instance:instance,constructor:constructor})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.ClassTest);
+
 $core.addMethod(
 $core.method({
 selector: "testBeJavaScriptSubclassOf",

+ 26 - 0
lang/src/Kernel-Tests.st

@@ -469,6 +469,13 @@ jsConstructor
 	'>
 !
 
+jsConstructorWithAction
+	<inlineJS: '
+		function Foo(aFoo){this.foo = aFoo;}
+		return Foo;
+	'>
+!
+
 setUp
 	builder := ClassBuilder new
 !
@@ -507,6 +514,25 @@ testAlternateConstructorViaSelector
 	self assert: instance foo equals: 9
 !
 
+testApplySuperConstructor
+	| instance constructor |
+	theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
+	constructor := self jsConstructorWithAction.
+	theClass beJavaScriptSubclassOf: constructor.
+	Compiler new
+		install: 'bar: anObject
+			ObjectMock2
+				applySuperConstructorOn: self
+				withArguments: {anObject}'
+		forClass: theClass
+		protocol: 'tests'.
+	"testing specific to late-coupled detached root class"
+	instance := (theClass alternateConstructorViaSelector: #bar:) newValue: 7.
+	self assert: instance class == theClass.
+	self assert: instance isJavaScriptInstanceOf: constructor.
+	self assert: instance foo equals: 7
+!
+
 testBeJavaScriptSubclassOf
 	| instance constructor |
 	theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.