2
0
Kaynağa Gözat

Merge pull request #361 from mkroehnert/proxy

Add JSObjectProxy>>value
Nicolas Petton 12 yıl önce
ebeveyn
işleme
c70d83e871

+ 30 - 0
js/Kernel-Objects.deploy.js

@@ -1467,6 +1467,21 @@ return self}, function($ctx1) {$ctx1.fill(self,"at:",{aSymbol:aSymbol}, smalltal
 }),
 smalltalk.JSObjectProxy);
 
+smalltalk.addMethod(
+"_at_ifAbsent_",
+smalltalk.method({
+selector: "at:ifAbsent:",
+fn: function (aSymbol,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+    	var obj = self['@jsObject'],
+        	symbol = aSymbol._asString();
+		return symbol in obj ? obj[symbol] : aBlock();
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{aSymbol:aSymbol,aBlock:aBlock}, smalltalk.JSObjectProxy)})}
+}),
+smalltalk.JSObjectProxy);
+
 smalltalk.addMethod(
 "_at_put_",
 smalltalk.method({
@@ -1597,6 +1612,21 @@ return $1;
 }),
 smalltalk.JSObjectProxy);
 
+smalltalk.addMethod(
+"_value",
+smalltalk.method({
+selector: "value",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._at_ifAbsent_("value",(function(){
+return smalltalk.withContext(function($ctx2) {
return smalltalk.Object.fn.prototype._value.apply(_st(self), []);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"value",{}, smalltalk.JSObjectProxy)})}
+}),
+smalltalk.JSObjectProxy);
+
 
 smalltalk.addMethod(
 "_on_",

+ 40 - 0
js/Kernel-Objects.js

@@ -2056,6 +2056,26 @@ referencedClasses: []
 }),
 smalltalk.JSObjectProxy);
 
+smalltalk.addMethod(
+"_at_ifAbsent_",
+smalltalk.method({
+selector: "at:ifAbsent:",
+category: 'accessing',
+fn: function (aSymbol,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+    	var obj = self['@jsObject'],
+        	symbol = aSymbol._asString();
+		return symbol in obj ? obj[symbol] : aBlock();
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{aSymbol:aSymbol,aBlock:aBlock}, smalltalk.JSObjectProxy)})},
+args: ["aSymbol", "aBlock"],
+source: "at: aSymbol ifAbsent: aBlock\x0a\x09\x22return the aSymbol property or evaluate aBlock if the property is not defined on the object\x22\x0a\x09<\x0a    \x09var obj = self['@jsObject'],\x0a        \x09symbol = aSymbol._asString();\x0a\x09\x09return symbol in obj ? obj[symbol] : aBlock();\x0a\x09>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxy);
+
 smalltalk.addMethod(
 "_at_put_",
 smalltalk.method({
@@ -2231,6 +2251,26 @@ referencedClasses: []
 }),
 smalltalk.JSObjectProxy);
 
+smalltalk.addMethod(
+"_value",
+smalltalk.method({
+selector: "value",
+category: 'accessing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._at_ifAbsent_("value",(function(){
+return smalltalk.withContext(function($ctx2) {
return smalltalk.Object.fn.prototype._value.apply(_st(self), []);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"value",{}, smalltalk.JSObjectProxy)})},
+args: [],
+source: "value\x0a\x09\x22if attribute 'value' exists on the JS object return it,\x0a    otherwise return the result of Object>>value.\x22\x0a\x09^ self at: 'value' ifAbsent: [super value]",
+messageSends: ["at:ifAbsent:", "value"],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxy);
+
 
 smalltalk.addMethod(
 "_on_",

+ 42 - 3
js/Kernel-Tests.deploy.js

@@ -1831,10 +1831,34 @@ smalltalk.addMethod(
 "_jsObject",
 smalltalk.method({
 selector: "jsObject",
-fn: function () {
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined};
+return self}, function($ctx1) {$ctx1.fill(self,"jsObject",{}, smalltalk.JSObjectProxyTest)})}
+}),
+smalltalk.JSObjectProxyTest);
+
+smalltalk.addMethod(
+"_testAtIfAbsent",
+smalltalk.method({
+selector: "testAtIfAbsent",
+fn: function (){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null};
-return self}, function($ctx1) {$ctx1.fill(self,"jsObject",{}, smalltalk.JSObjectProxyTest)});}
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_("Property does not exist",_st(testObject)._at_ifAbsent_("abc",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+_st(self)._assert_equals_(nil,_st(testObject)._at_ifAbsent_("e",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+_st(self)._assert_equals_((1),_st(testObject)._at_ifAbsent_("a",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+_st(self)._assert_equals_(nil,_st(testObject)._at_ifAbsent_("f",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfAbsent",{testObject:testObject}, smalltalk.JSObjectProxyTest)})}
 }),
 smalltalk.JSObjectProxyTest);
 
@@ -1917,6 +1941,21 @@ return self}, function($ctx1) {$ctx1.fill(self,"testPropertyThatReturnsUndefined
 }),
 smalltalk.JSObjectProxyTest);
 
+smalltalk.addMethod(
+"_testValue",
+smalltalk.method({
+selector: "testValue",
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_("[object Object]",_st(_st(testObject)._value())._printString());
+_st(testObject)._at_put_("value","aValue");
+_st(self)._assert_equals_("aValue",_st(testObject)._value());
+return self}, function($ctx1) {$ctx1.fill(self,"testValue",{testObject:testObject}, smalltalk.JSObjectProxyTest)})}
+}),
+smalltalk.JSObjectProxyTest);
+
 smalltalk.addMethod(
 "_testYourself",
 smalltalk.method({

+ 53 - 4
js/Kernel-Tests.js

@@ -2347,17 +2347,46 @@ smalltalk.addMethod(
 smalltalk.method({
 selector: "jsObject",
 category: 'accessing',
-fn: function () {
+fn: function (){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null};
-return self}, function($ctx1) {$ctx1.fill(self,"jsObject",{}, smalltalk.JSObjectProxyTest)});},
+return smalltalk.withContext(function($ctx1) { 
return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined};
+return self}, function($ctx1) {$ctx1.fill(self,"jsObject",{}, smalltalk.JSObjectProxyTest)})},
 args: [],
-source: "jsObject\x0a\x09<return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null}>",
+source: "jsObject\x0a\x09<return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>",
 messageSends: [],
 referencedClasses: []
 }),
 smalltalk.JSObjectProxyTest);
 
+smalltalk.addMethod(
+"_testAtIfAbsent",
+smalltalk.method({
+selector: "testAtIfAbsent",
+category: 'tests',
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_("Property does not exist",_st(testObject)._at_ifAbsent_("abc",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+_st(self)._assert_equals_(nil,_st(testObject)._at_ifAbsent_("e",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+_st(self)._assert_equals_((1),_st(testObject)._at_ifAbsent_("a",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+_st(self)._assert_equals_(nil,_st(testObject)._at_ifAbsent_("f",(function(){
+return smalltalk.withContext(function($ctx2) {
return "Property does not exist";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfAbsent",{testObject:testObject}, smalltalk.JSObjectProxyTest)})},
+args: [],
+source: "testAtIfAbsent\x0a\x09| testObject |\x0a    testObject := self jsObject.\x0a\x09self assert: 'Property does not exist' equals: (testObject at: 'abc' ifAbsent: ['Property does not exist']).\x0a\x09self assert: nil equals: (testObject at: 'e' ifAbsent: ['Property does not exist']).\x0a    self assert: 1 equals: (testObject at: 'a' ifAbsent: ['Property does not exist']).\x0a    self assert: nil equals: (testObject at: 'f' ifAbsent: ['Property does not exist']).",
+messageSends: ["jsObject", "assert:equals:", "at:ifAbsent:"],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxyTest);
+
 smalltalk.addMethod(
 "_testDNU",
 smalltalk.method({
@@ -2467,6 +2496,26 @@ referencedClasses: ["MessageNotUnderstood"]
 }),
 smalltalk.JSObjectProxyTest);
 
+smalltalk.addMethod(
+"_testValue",
+smalltalk.method({
+selector: "testValue",
+category: 'tests',
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_("[object Object]",_st(_st(testObject)._value())._printString());
+_st(testObject)._at_put_("value","aValue");
+_st(self)._assert_equals_("aValue",_st(testObject)._value());
+return self}, function($ctx1) {$ctx1.fill(self,"testValue",{testObject:testObject}, smalltalk.JSObjectProxyTest)})},
+args: [],
+source: "testValue\x0a\x09| testObject |\x0a    testObject := self jsObject.\x0a\x09self assert: '[object Object]' equals: testObject value printString.\x0a    testObject at: 'value' put: 'aValue'.\x0a\x09self assert: 'aValue' equals: testObject value",
+messageSends: ["jsObject", "assert:equals:", "printString", "value", "at:put:"],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxyTest);
+
 smalltalk.addMethod(
 "_testYourself",
 smalltalk.method({

+ 15 - 0
st/Kernel-Objects.st

@@ -672,6 +672,15 @@ at: aSymbol
 	<return self['@jsObject'][aSymbol._asString()]>
 !
 
+at: aSymbol ifAbsent: aBlock
+	"return the aSymbol property or evaluate aBlock if the property is not defined on the object"
+	<
+    	var obj = self['@jsObject'],
+        	symbol = aSymbol._asString();
+		return symbol in obj ? obj[symbol] : aBlock();
+	>
+!
+
 at: aSymbol put: anObject
 	<self['@jsObject'][aSymbol._asString()] = anObject>
 !
@@ -682,6 +691,12 @@ jsObject
 
 jsObject: aJSObject
 	jsObject := aJSObject
+!
+
+value
+	"if attribute 'value' exists on the JS object return it,
+    otherwise return the result of Object>>value."
+	^ self at: 'value' ifAbsent: [super value]
 ! !
 
 !JSObjectProxy methodsFor: 'enumerating'!

+ 18 - 1
st/Kernel-Tests.st

@@ -931,11 +931,20 @@ TestCase subclass: #JSObjectProxyTest
 !JSObjectProxyTest methodsFor: 'accessing'!
 
 jsObject
-	<return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null}>
+	<return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>
 ! !
 
 !JSObjectProxyTest methodsFor: 'tests'!
 
+testAtIfAbsent
+	| testObject |
+    testObject := self jsObject.
+	self assert: 'Property does not exist' equals: (testObject at: 'abc' ifAbsent: ['Property does not exist']).
+	self assert: nil equals: (testObject at: 'e' ifAbsent: ['Property does not exist']).
+    self assert: 1 equals: (testObject at: 'a' ifAbsent: ['Property does not exist']).
+    self assert: nil equals: (testObject at: 'f' ifAbsent: ['Property does not exist']).
+!
+
 testDNU
 	self should: [self jsObject foo] raise: MessageNotUnderstood
 !
@@ -973,6 +982,14 @@ testPropertyThatReturnsUndefined
     self assert: object e isNil
 !
 
+testValue
+	| testObject |
+    testObject := self jsObject.
+	self assert: '[object Object]' equals: testObject value printString.
+    testObject at: 'value' put: 'aValue'.
+	self assert: 'aValue' equals: testObject value
+!
+
 testYourself
 	| object |
 	object := self jsObject