Browse Source

Fixes issue #690.
Adds accessors to the constructor of classes.

Nicolas Petton 10 years ago
parent
commit
a98076e134
3 changed files with 61 additions and 0 deletions
  1. 32 0
      js/Kernel-Classes.js
  2. 13 0
      st/Kernel-Classes.st
  3. 16 0
      support/boot.js

+ 32 - 0
js/Kernel-Classes.js

@@ -502,6 +502,38 @@ referencedClasses: []
 }),
 smalltalk.Behavior);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "javascriptConstructor",
+category: 'accessing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return self.fn;
+return self}, function($ctx1) {$ctx1.fill(self,"javascriptConstructor",{},smalltalk.Behavior)})},
+args: [],
+source: "javascriptConstructor\x0a\x09\x22Answer the JS constructor used to instantiate. See boot.js\x22\x0a\x09\x0a\x09<return self.fn>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Behavior);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "javascriptConstructor:",
+category: 'accessing',
+fn: function (aJavaScriptFunction){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+smalltalk.setClassConstructor(self, aJavaScriptFunction);;
+return self}, function($ctx1) {$ctx1.fill(self,"javascriptConstructor:",{aJavaScriptFunction:aJavaScriptFunction},smalltalk.Behavior)})},
+args: ["aJavaScriptFunction"],
+source: "javascriptConstructor: aJavaScriptFunction\x0a\x09\x22Set the JS constructor used to instantiate.\x0a\x09See the JS counter-part in boot.js `smalltalk.setClassConstructor'\x22\x0a\x09\x0a\x09<smalltalk.setClassConstructor(self, aJavaScriptFunction);>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Behavior);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "lookupSelector:",

+ 13 - 0
st/Kernel-Classes.st

@@ -85,6 +85,19 @@ instanceVariableNames
 	<return self.iVarNames>
 !
 
+javascriptConstructor
+	"Answer the JS constructor used to instantiate. See boot.js"
+	
+	<return self.fn>
+!
+
+javascriptConstructor: aJavaScriptFunction
+	"Set the JS constructor used to instantiate.
+	See the JS counter-part in boot.js `smalltalk.setClassConstructor'"
+	
+	<smalltalk.setClassConstructor(self, aJavaScriptFunction);>
+!
+
 lookupSelector: selector
 	"Look up the given selector in my methodDictionary.
 	Return the corresponding method if found.

+ 16 - 0
support/boot.js

@@ -490,6 +490,22 @@ function ClassesBrik(brikz, st) {
 		}
 	};
 
+	/* Manually set the constructor of an existing Smalltalk klass, making it a wrapped class. */
+
+	st.setClassConstructor = function(klass, constructor) {
+		wrappedClasses.addElement(klass);
+		klass.wrapped = true;
+		klass.fn = constructor;
+
+		// The fn property changed. We need to add back the klass property to the prototype
+		Object.defineProperty(klass.fn.prototype, "klass", {
+			value: klass,
+			enumerable: false, configurable: true, writable: true
+		});
+
+		st.initClass(klass);
+	};
+
 	/* Create an alias for an existing class */
 
 	st.alias = function(klass, alias) {