Browse Source

Fixes issue #396

Nicolas Petton 11 years ago
parent
commit
e35a50551e
7 changed files with 113 additions and 30 deletions
  1. 35 7
      js/Compiler-Core.deploy.js
  2. 48 10
      js/Compiler-Core.js
  3. 2 2
      js/IDE.deploy.js
  4. 3 3
      js/IDE.js
  5. 16 5
      st/Compiler-Core.st
  6. 2 2
      st/IDE.st
  7. 7 1
      st/Kernel-Methods.st

+ 35 - 7
js/Compiler-Core.deploy.js

@@ -72,6 +72,21 @@ return $1;
 messageSends: ["currentClass:", "source:", ",", "compileNode:", "parse:", "source"]}),
 messageSends: ["currentClass:", "source:", ",", "compileNode:", "parse:", "source"]}),
 smalltalk.Compiler);
 smalltalk.Compiler);
 
 
+smalltalk.addMethod(
+"_compileExpression_on_",
+smalltalk.method({
+selector: "compileExpression:on:",
+fn: function (aString,anObject){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+_st(self)._currentClass_(_st(anObject)._class());
+_st(self)._source_(_st(_st("xxxDoIt ^[").__comma(aString)).__comma("] value"));
+$1=_st(self)._compileNode_(_st(self)._parse_(_st(self)._source()));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"compileExpression:on:",{aString:aString,anObject:anObject}, smalltalk.Compiler)})},
+messageSends: ["currentClass:", "class", "source:", ",", "compileNode:", "parse:", "source"]}),
+smalltalk.Compiler);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_compileNode_",
 "_compileNode_",
 smalltalk.method({
 smalltalk.method({
@@ -133,17 +148,30 @@ smalltalk.method({
 selector: "evaluateExpression:",
 selector: "evaluateExpression:",
 fn: function (aString){
 fn: function (aString){
 var self=this;
 var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._evaluateExpression_on_(aString,_st((smalltalk.DoIt || DoIt))._new());
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"evaluateExpression:",{aString:aString}, smalltalk.Compiler)})},
+messageSends: ["evaluateExpression:on:", "new"]}),
+smalltalk.Compiler);
+
+smalltalk.addMethod(
+"_evaluateExpression_on_",
+smalltalk.method({
+selector: "evaluateExpression:on:",
+fn: function (aString,anObject){
+var self=this;
 var result,method;
 var result,method;
 return smalltalk.withContext(function($ctx1) { 
var $1;
 return smalltalk.withContext(function($ctx1) { 
var $1;
-method=_st(self)._eval_(_st(self)._compileExpression_(aString));
-_st(method)._category_("doIt");
-_st((smalltalk.DoIt || DoIt))._addCompiledMethod_(method);
-result=_st(_st((smalltalk.DoIt || DoIt))._new())._doIt();
-_st((smalltalk.DoIt || DoIt))._removeCompiledMethod_(method);
+method=_st(self)._eval_(_st(self)._compileExpression_on_(aString,anObject));
+_st(method)._category_("xxxDoIt");
+_st(_st(anObject)._class())._addCompiledMethod_(method);
+result=_st(anObject)._xxxDoIt();
+_st(_st(anObject)._class())._removeCompiledMethod_(method);
 $1=result;
 $1=result;
 return $1;
 return $1;
-}, function($ctx1) {$ctx1.fill(self,"evaluateExpression:",{aString:aString,result:result,method:method}, smalltalk.Compiler)})},
-messageSends: ["eval:", "compileExpression:", "category:", "addCompiledMethod:", "doIt", "new", "removeCompiledMethod:"]}),
+}, function($ctx1) {$ctx1.fill(self,"evaluateExpression:on:",{aString:aString,anObject:anObject,result:result,method:method}, smalltalk.Compiler)})},
+messageSends: ["eval:", "compileExpression:on:", "category:", "addCompiledMethod:", "class", "xxxDoIt", "removeCompiledMethod:"]}),
 smalltalk.Compiler);
 smalltalk.Compiler);
 
 
 smalltalk.addMethod(
 smalltalk.addMethod(

+ 48 - 10
js/Compiler-Core.js

@@ -98,6 +98,26 @@ referencedClasses: ["DoIt"]
 }),
 }),
 smalltalk.Compiler);
 smalltalk.Compiler);
 
 
+smalltalk.addMethod(
+"_compileExpression_on_",
+smalltalk.method({
+selector: "compileExpression:on:",
+category: 'compiling',
+fn: function (aString,anObject){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+_st(self)._currentClass_(_st(anObject)._class());
+_st(self)._source_(_st(_st("xxxDoIt ^[").__comma(aString)).__comma("] value"));
+$1=_st(self)._compileNode_(_st(self)._parse_(_st(self)._source()));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"compileExpression:on:",{aString:aString,anObject:anObject}, smalltalk.Compiler)})},
+args: ["aString", "anObject"],
+source: "compileExpression: aString on: anObject\x0a\x09self currentClass: anObject class.\x0a\x09self source: 'xxxDoIt ^[', aString, '] value'.\x0a\x09^self compileNode: (self parse: self source)",
+messageSends: ["currentClass:", "class", "source:", ",", "compileNode:", "parse:", "source"],
+referencedClasses: []
+}),
+smalltalk.Compiler);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_compileNode_",
 "_compileNode_",
 smalltalk.method({
 smalltalk.method({
@@ -180,23 +200,41 @@ selector: "evaluateExpression:",
 category: 'compiling',
 category: 'compiling',
 fn: function (aString){
 fn: function (aString){
 var self=this;
 var self=this;
-var result,method;
 return smalltalk.withContext(function($ctx1) { 
var $1;
 return smalltalk.withContext(function($ctx1) { 
var $1;
-method=_st(self)._eval_(_st(self)._compileExpression_(aString));
-_st(method)._category_("doIt");
-_st((smalltalk.DoIt || DoIt))._addCompiledMethod_(method);
-result=_st(_st((smalltalk.DoIt || DoIt))._new())._doIt();
-_st((smalltalk.DoIt || DoIt))._removeCompiledMethod_(method);
-$1=result;
+$1=_st(self)._evaluateExpression_on_(aString,_st((smalltalk.DoIt || DoIt))._new());
 return $1;
 return $1;
-}, function($ctx1) {$ctx1.fill(self,"evaluateExpression:",{aString:aString,result:result,method:method}, smalltalk.Compiler)})},
+}, function($ctx1) {$ctx1.fill(self,"evaluateExpression:",{aString:aString}, smalltalk.Compiler)})},
 args: ["aString"],
 args: ["aString"],
-source: "evaluateExpression: aString\x0a\x09\x22Unlike #eval: evaluate a Smalltalk expression and answer the returned object\x22\x0a\x09| result method |\x0a    method := self eval: (self compileExpression: aString).\x0a    method category: 'doIt'.\x0a\x09DoIt addCompiledMethod: method.\x0a\x09result := DoIt new doIt.\x0a\x09DoIt removeCompiledMethod: method.\x0a\x09^result",
-messageSends: ["eval:", "compileExpression:", "category:", "addCompiledMethod:", "doIt", "new", "removeCompiledMethod:"],
+source: "evaluateExpression: aString\x0a\x09\x22Unlike #eval: evaluate a Smalltalk expression and answer the returned object\x22\x0a\x09^ self evaluateExpression: aString on: DoIt new",
+messageSends: ["evaluateExpression:on:", "new"],
 referencedClasses: ["DoIt"]
 referencedClasses: ["DoIt"]
 }),
 }),
 smalltalk.Compiler);
 smalltalk.Compiler);
 
 
+smalltalk.addMethod(
+"_evaluateExpression_on_",
+smalltalk.method({
+selector: "evaluateExpression:on:",
+category: 'compiling',
+fn: function (aString,anObject){
+var self=this;
+var result,method;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+method=_st(self)._eval_(_st(self)._compileExpression_on_(aString,anObject));
+_st(method)._category_("xxxDoIt");
+_st(_st(anObject)._class())._addCompiledMethod_(method);
+result=_st(anObject)._xxxDoIt();
+_st(_st(anObject)._class())._removeCompiledMethod_(method);
+$1=result;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"evaluateExpression:on:",{aString:aString,anObject:anObject,result:result,method:method}, smalltalk.Compiler)})},
+args: ["aString", "anObject"],
+source: "evaluateExpression: aString on: anObject\x0a\x09\x22Unlike #eval: evaluate a Smalltalk expression with anObject as the receiver and answer the returned object\x22\x0a\x09| result method |\x0a    method := self eval: (self compileExpression: aString on: anObject).\x0a    method category: 'xxxDoIt'.\x0a\x09anObject class addCompiledMethod: method.\x0a\x09result := anObject xxxDoIt.\x0a\x09anObject class removeCompiledMethod: method.\x0a\x09^result",
+messageSends: ["eval:", "compileExpression:on:", "category:", "addCompiledMethod:", "class", "xxxDoIt", "removeCompiledMethod:"],
+referencedClasses: []
+}),
+smalltalk.Compiler);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_install_forClass_category_",
 "_install_forClass_category_",
 smalltalk.method({
 smalltalk.method({

+ 2 - 2
js/IDE.deploy.js

@@ -453,12 +453,12 @@ return smalltalk.withContext(function($ctx2) {
return _st(compiler)._parseExpres
 return smalltalk.withContext(function($ctx2) {
$1=_st(window)._alert_(_st(ex)._messageText());
 return smalltalk.withContext(function($ctx2) {
$1=_st(window)._alert_(_st(ex)._messageText());
 throw $early=[$1];
 throw $early=[$1];
 }, function($ctx2) {$ctx2.fillBlock({ex:ex},$ctx1)})}));
 }, function($ctx2) {$ctx2.fillBlock({ex:ex},$ctx1)})}));
-$2=_st(compiler)._evaluateExpression_(aString);
+$2=_st(compiler)._evaluateExpression_on_(aString,_st(self)._receiver());
 return $2;
 return $2;
 }
 }
 catch(e) {if(e===$early)return e[0]; throw e}
 catch(e) {if(e===$early)return e[0]; throw e}
 }, function($ctx1) {$ctx1.fill(self,"eval:",{aString:aString,compiler:compiler}, smalltalk.SourceArea)})},
 }, function($ctx1) {$ctx1.fill(self,"eval:",{aString:aString,compiler:compiler}, smalltalk.SourceArea)})},
-messageSends: ["new", "on:do:", "alert:", "messageText", "parseExpression:", "evaluateExpression:"]}),
+messageSends: ["new", "on:do:", "alert:", "messageText", "parseExpression:", "evaluateExpression:on:", "receiver"]}),
 smalltalk.SourceArea);
 smalltalk.SourceArea);
 
 
 smalltalk.addMethod(
 smalltalk.addMethod(

+ 3 - 3
js/IDE.js

@@ -589,14 +589,14 @@ return smalltalk.withContext(function($ctx2) {
return _st(compiler)._parseExpres
 return smalltalk.withContext(function($ctx2) {
$1=_st(window)._alert_(_st(ex)._messageText());
 return smalltalk.withContext(function($ctx2) {
$1=_st(window)._alert_(_st(ex)._messageText());
 throw $early=[$1];
 throw $early=[$1];
 }, function($ctx2) {$ctx2.fillBlock({ex:ex},$ctx1)})}));
 }, function($ctx2) {$ctx2.fillBlock({ex:ex},$ctx1)})}));
-$2=_st(compiler)._evaluateExpression_(aString);
+$2=_st(compiler)._evaluateExpression_on_(aString,_st(self)._receiver());
 return $2;
 return $2;
 }
 }
 catch(e) {if(e===$early)return e[0]; throw e}
 catch(e) {if(e===$early)return e[0]; throw e}
 }, function($ctx1) {$ctx1.fill(self,"eval:",{aString:aString,compiler:compiler}, smalltalk.SourceArea)})},
 }, function($ctx1) {$ctx1.fill(self,"eval:",{aString:aString,compiler:compiler}, smalltalk.SourceArea)})},
 args: ["aString"],
 args: ["aString"],
-source: "eval: aString\x0a\x09| compiler  |\x0a\x09compiler := Compiler new.\x0a\x09[compiler parseExpression: aString] on: Error do: [:ex |\x0a\x09\x09^window alert: ex messageText].\x0a\x09^compiler evaluateExpression: aString",
-messageSends: ["new", "on:do:", "alert:", "messageText", "parseExpression:", "evaluateExpression:"],
+source: "eval: aString\x0a\x09| compiler  |\x0a\x09compiler := Compiler new.\x0a\x09[ compiler parseExpression: aString ] on: Error do: [:ex |\x0a\x09\x09^window alert: ex messageText].\x0a\x09^compiler evaluateExpression: aString on: self receiver",
+messageSends: ["new", "on:do:", "alert:", "messageText", "parseExpression:", "evaluateExpression:on:", "receiver"],
 referencedClasses: ["Compiler", "Error"]
 referencedClasses: ["Compiler", "Error"]
 }),
 }),
 smalltalk.SourceArea);
 smalltalk.SourceArea);

+ 16 - 5
st/Compiler-Core.st

@@ -60,6 +60,12 @@ compileExpression: aString
 	^self compileNode: (self parse: self source)
 	^self compileNode: (self parse: self source)
 !
 !
 
 
+compileExpression: aString on: anObject
+	self currentClass: anObject class.
+	self source: 'xxxDoIt ^[', aString, '] value'.
+	^self compileNode: (self parse: self source)
+!
+
 compileNode: aNode
 compileNode: aNode
 	| generator result |
 	| generator result |
 	generator := self codeGeneratorClass new.
 	generator := self codeGeneratorClass new.
@@ -77,12 +83,17 @@ eval: aString
 
 
 evaluateExpression: aString
 evaluateExpression: aString
 	"Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
 	"Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
+	^ self evaluateExpression: aString on: DoIt new
+!
+
+evaluateExpression: aString on: anObject
+	"Unlike #eval: evaluate a Smalltalk expression with anObject as the receiver and answer the returned object"
 	| result method |
 	| result method |
-    method := self eval: (self compileExpression: aString).
-    method category: 'doIt'.
-	DoIt addCompiledMethod: method.
-	result := DoIt new doIt.
-	DoIt removeCompiledMethod: method.
+    method := self eval: (self compileExpression: aString on: anObject).
+    method category: 'xxxDoIt'.
+	anObject class addCompiledMethod: method.
+	result := anObject xxxDoIt.
+	anObject class removeCompiledMethod: method.
 	^result
 	^result
 !
 !
 
 

+ 2 - 2
st/IDE.st

@@ -242,9 +242,9 @@ doIt
 eval: aString
 eval: aString
 	| compiler  |
 	| compiler  |
 	compiler := Compiler new.
 	compiler := Compiler new.
-	[compiler parseExpression: aString] on: Error do: [:ex |
+	[ compiler parseExpression: aString ] on: Error do: [:ex |
 		^window alert: ex messageText].
 		^window alert: ex messageText].
-	^compiler evaluateExpression: aString
+	^compiler evaluateExpression: aString on: self receiver
 !
 !
 
 
 fileIn
 fileIn

+ 7 - 1
st/Kernel-Methods.st

@@ -187,7 +187,7 @@ arguments
 !
 !
 
 
 category
 category
-	^(self basicAt: 'category') ifNil: ['']
+	^(self basicAt: 'category') ifNil: [ self defaultCategory ]
 !
 !
 
 
 category: aString
 category: aString
@@ -243,6 +243,12 @@ source: aString
 	self basicAt: 'source' put: aString
 	self basicAt: 'source' put: aString
 ! !
 ! !
 
 
+!CompiledMethod methodsFor: 'defaults'!
+
+defaultCategory
+	^ 'as yet unclassified'
+! !
+
 Object subclass: #ForkPool
 Object subclass: #ForkPool
 	instanceVariableNames: 'poolSize maxPoolSize queue worker'
 	instanceVariableNames: 'poolSize maxPoolSize queue worker'
 	package: 'Kernel-Methods'!
 	package: 'Kernel-Methods'!