Jelajahi Sumber

AIContext refactoring. AIContext is now a subclass of MethodContext

Nicolas Petton 10 tahun lalu
induk
melakukan
15fd31ba81
2 mengubah file dengan 42 tambahan dan 136 penghapusan
  1. 33 104
      js/Compiler-Interpreter.js
  2. 9 32
      st/Compiler-Interpreter.st

+ 33 - 104
js/Compiler-Interpreter.js

@@ -234,7 +234,7 @@ referencedClasses: []
 smalltalk.AIBlockClosure.klass);
 
 
-smalltalk.addClass('AIContext', smalltalk.Object, ['outerContext', 'innerContext', 'pc', 'locals', 'method', 'index', 'ast', 'interpreter'], 'Compiler-Interpreter');
+smalltalk.addClass('AIContext', smalltalk.MethodContext, ['outerContext', 'innerContext', 'pc', 'locals', 'selector', 'index', 'ast', 'interpreter'], 'Compiler-Interpreter');
 smalltalk.AIContext.comment="I am like a `MethodContext`, used by the `ASTInterpreter`.\x0aUnlike a `MethodContext`, my instances are not read-only.\x0a\x0aWhen debugging, my instances are created by copying the current `MethodContext` (thisContext)";
 smalltalk.addMethod(
 smalltalk.method({
@@ -257,29 +257,6 @@ referencedClasses: []
 }),
 smalltalk.AIContext);
 
-smalltalk.addMethod(
-smalltalk.method({
-selector: "asString",
-category: 'converting',
-fn: function (){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $2,$1;
-$2=self._isBlockContext();
-if(smalltalk.assert($2)){
-$1=_st("a block (in ".__comma(_st(self._methodContext())._asString())).__comma(")");
-} else {
-$1=_st(_st(_st(_st(self._receiver())._class())._name()).__comma(" >> ")).__comma(self._selector());
-};
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"asString",{},smalltalk.AIContext)})},
-args: [],
-source: "asString\x0a\x09^self isBlockContext\x0a\x09\x09ifTrue: [ 'a block (in ', self methodContext asString, ')' ]\x0a\x09\x09ifFalse: [ self receiver class name, ' >> ', self selector ]",
-messageSends: ["ifTrue:ifFalse:", "isBlockContext", ",", "asString", "methodContext", "name", "class", "receiver", "selector"],
-referencedClasses: []
-}),
-smalltalk.AIContext);
-
 smalltalk.addMethod(
 smalltalk.method({
 selector: "ast",
@@ -385,7 +362,7 @@ $1=self;
 _st($1)._pc_(_st(aMethodContext)._pc());
 _st($1)._index_(_st(aMethodContext)._index());
 _st($1)._receiver_(_st(aMethodContext)._receiver());
-$2=_st($1)._method_(_st(aMethodContext)._method());
+$2=_st($1)._selector_(_st(aMethodContext)._selector());
 $3=_st(aMethodContext)._outerContext();
 if(($receiver = $3) == nil || $receiver == undefined){
 $3;
@@ -405,8 +382,8 @@ return _st(self._locals())._at_put_(key,value);
 };
 return self}, function($ctx1) {$ctx1.fill(self,"initializeFromMethodContext:",{aMethodContext:aMethodContext},smalltalk.AIContext)})},
 args: ["aMethodContext"],
-source: "initializeFromMethodContext: aMethodContext\x0a\x0a\x09self \x0a\x09\x09pc: aMethodContext pc;\x0a\x09\x09index: aMethodContext index;\x0a\x09\x09receiver: aMethodContext receiver;\x0a\x09\x09method: aMethodContext method.\x0a\x09\x09\x0a\x09aMethodContext outerContext ifNotNil: [ :outer |\x0a\x09\x09\x22If the method context is nil, the block was defined in JS, so ignore it\x22\x0a\x09\x09outer methodContext ifNotNil: [\x0a\x09\x09\x09self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].\x0a\x09\x09\x09aMethodContext locals keysAndValuesDo: [ :key :value |\x0a\x09\x09\x09\x09self locals at: key put: value ] ]",
-messageSends: ["pc:", "pc", "index:", "index", "receiver:", "receiver", "method:", "method", "ifNotNil:", "outerContext", "methodContext", "outerContext:", "fromMethodContext:", "class", "keysAndValuesDo:", "locals", "at:put:"],
+source: "initializeFromMethodContext: aMethodContext\x0a\x0a\x09self \x0a\x09\x09pc: aMethodContext pc;\x0a\x09\x09index: aMethodContext index;\x0a\x09\x09receiver: aMethodContext receiver;\x0a\x09\x09selector: aMethodContext selector.\x0a\x09\x09\x0a\x09aMethodContext outerContext ifNotNil: [ :outer |\x0a\x09\x09\x22If the method context is nil, the block was defined in JS, so ignore it\x22\x0a\x09\x09outer methodContext ifNotNil: [\x0a\x09\x09\x09self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].\x0a\x09\x09\x09aMethodContext locals keysAndValuesDo: [ :key :value |\x0a\x09\x09\x09\x09self locals at: key put: value ] ]",
+messageSends: ["pc:", "pc", "index:", "index", "receiver:", "receiver", "selector:", "selector", "ifNotNil:", "outerContext", "methodContext", "outerContext:", "fromMethodContext:", "class", "keysAndValuesDo:", "locals", "at:put:"],
 referencedClasses: []
 }),
 smalltalk.AIContext);
@@ -532,24 +509,6 @@ referencedClasses: []
 }),
 smalltalk.AIContext);
 
-smalltalk.addMethod(
-smalltalk.method({
-selector: "isBlockContext",
-category: 'testing',
-fn: function (){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=_st(self._selector())._isNil();
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"isBlockContext",{},smalltalk.AIContext)})},
-args: [],
-source: "isBlockContext\x0a\x09\x22Block context have no selector.\x22\x0a\x09\x0a\x09^ self selector isNil",
-messageSends: ["isNil", "selector"],
-referencedClasses: []
-}),
-smalltalk.AIContext);
-
 smalltalk.addMethod(
 smalltalk.method({
 selector: "localAt:",
@@ -625,59 +584,18 @@ category: 'accessing',
 fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=self["@method"];
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"method",{},smalltalk.AIContext)})},
-args: [],
-source: "method\x0a\x09^ method",
-messageSends: [],
-referencedClasses: []
-}),
-smalltalk.AIContext);
-
-smalltalk.addMethod(
-smalltalk.method({
-selector: "method:",
-category: 'accessing',
-fn: function (aCompiledMethod){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-self["@method"]=aCompiledMethod;
-return self}, function($ctx1) {$ctx1.fill(self,"method:",{aCompiledMethod:aCompiledMethod},smalltalk.AIContext)})},
-args: ["aCompiledMethod"],
-source: "method: aCompiledMethod\x0a\x09method := aCompiledMethod",
-messageSends: [],
-referencedClasses: []
-}),
-smalltalk.AIContext);
-
-smalltalk.addMethod(
-smalltalk.method({
-selector: "methodContext",
-category: 'accessing',
-fn: function (){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $1,$2,$4,$3;
-$1=self._isBlockContext();
-if(! smalltalk.assert($1)){
-$2=self;
-return $2;
-};
-$4=self._outerContext();
-if(($receiver = $4) == nil || $receiver == undefined){
-$3=$4;
+var $2,$1;
+$2=self._methodContext();
+if(($receiver = $2) == nil || $receiver == undefined){
+$1=$2;
 } else {
-var outer;
-outer=$receiver;
-$3=_st(outer)._methodContext();
+$1=_st(_st(_st(self._methodContext())._receiver())._class())._lookupSelector_(_st(self._methodContext())._selector());
 };
-return $3;
-}, function($ctx1) {$ctx1.fill(self,"methodContext",{},smalltalk.AIContext)})},
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"method",{},smalltalk.AIContext)})},
 args: [],
-source: "methodContext\x0a\x09self isBlockContext ifFalse: [ ^ self ].\x0a\x09\x0a\x09^ self outerContext ifNotNil: [ :outer |\x0a\x09\x09outer methodContext ]",
-messageSends: ["ifFalse:", "isBlockContext", "ifNotNil:", "outerContext", "methodContext"],
+source: "method\x0a\x09^ self methodContext ifNotNil: [\x0a\x09\x09self methodContext receiver class lookupSelector: self methodContext selector ]",
+messageSends: ["ifNotNil:", "methodContext", "lookupSelector:", "class", "receiver", "selector"],
 referencedClasses: []
 }),
 smalltalk.AIContext);
@@ -847,18 +765,29 @@ category: 'accessing',
 fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-var $2,$1;
-$2=self._method();
-if(($receiver = $2) == nil || $receiver == undefined){
-$1=$2;
-} else {
-$1=_st(self._method())._selector();
-};
+var $1;
+$1=self["@selector"];
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"selector",{},smalltalk.AIContext)})},
 args: [],
-source: "selector\x0a\x09^ self method ifNotNil: [ \x0a\x09\x09self method selector ]",
-messageSends: ["ifNotNil:", "method", "selector"],
+source: "selector\x0a\x09^ selector",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.AIContext);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "selector:",
+category: 'accessing',
+fn: function (aString){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+self["@selector"]=aString;
+return self}, function($ctx1) {$ctx1.fill(self,"selector:",{aString:aString},smalltalk.AIContext)})},
+args: ["aString"],
+source: "selector: aString\x0a\x09selector := aString",
+messageSends: [],
 referencedClasses: []
 }),
 smalltalk.AIContext);

+ 9 - 32
st/Compiler-Interpreter.st

@@ -94,8 +94,8 @@ forContext: aContext node: aNode
 		yourself
 ! !
 
-Object subclass: #AIContext
-	instanceVariableNames: 'outerContext innerContext pc locals method index ast interpreter'
+MethodContext subclass: #AIContext
+	instanceVariableNames: 'outerContext innerContext pc locals selector index ast interpreter'
 	package: 'Compiler-Interpreter'!
 !AIContext commentStamp!
 I am like a `MethodContext`, used by the `ASTInterpreter`.
@@ -140,18 +140,8 @@ locals
 !
 
 method
-	^ method
-!
-
-method: aCompiledMethod
-	method := aCompiledMethod
-!
-
-methodContext
-	self isBlockContext ifFalse: [ ^ self ].
-	
-	^ self outerContext ifNotNil: [ :outer |
-		outer methodContext ]
+	^ self methodContext ifNotNil: [
+		self methodContext receiver class lookupSelector: self methodContext selector ]
 !
 
 outerContext
@@ -164,16 +154,11 @@ outerContext: anAIContext
 !
 
 selector
-	^ self method ifNotNil: [ 
-		self method selector ]
-! !
-
-!AIContext methodsFor: 'converting'!
+	^ selector
+!
 
-asString
-	^self isBlockContext
-		ifTrue: [ 'a block (in ', self methodContext asString, ')' ]
-		ifFalse: [ self receiver class name, ' >> ', self selector ]
+selector: aString
+	selector := aString
 ! !
 
 !AIContext methodsFor: 'factory'!
@@ -198,7 +183,7 @@ initializeFromMethodContext: aMethodContext
 		pc: aMethodContext pc;
 		index: aMethodContext index;
 		receiver: aMethodContext receiver;
-		method: aMethodContext method.
+		selector: aMethodContext selector.
 		
 	aMethodContext outerContext ifNotNil: [ :outer |
 		"If the method context is nil, the block was defined in JS, so ignore it"
@@ -281,14 +266,6 @@ setupInterpreter: anInterpreter
 	anInterpreter push: (self innerContext receiver)
 ! !
 
-!AIContext methodsFor: 'testing'!
-
-isBlockContext
-	"Block context have no selector."
-	
-	^ self selector isNil
-! !
-
 !AIContext class methodsFor: 'instance creation'!
 
 fromMethodContext: aMethodContext