Browse Source

Wrap Javascript objects before showing

Nicolas reminded me that JS objects can't respond to __asString(), and
so must be wrapped. This is done via _st(), which will wrap the object
in a JSObjectProxy. Initial tests seem to indicate that this is enough -
I was able to print the Window object and some other JS objects.
Kenneth Pullen 11 years ago
parent
commit
bd01a09519

+ 4 - 0
js/Kernel-Tests.deploy.js

@@ -1891,6 +1891,10 @@ _st(self)._shouldnt_raise_((function(){
 return smalltalk.withContext(function($ctx2) {
 return smalltalk.withContext(function($ctx2) {
 return _st($Transcript())._show_("Hello console!");
 return _st($Transcript())._show_("Hello console!");
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),$Error());
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),$Error());
+_st(self)._shouldnt_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return _st($Transcript())._show_(window);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),$Error());
 _st($Transcript())._register_(originalTranscript);
 _st($Transcript())._register_(originalTranscript);
 return self}, function($ctx1) {$ctx1.fill(self,"testShow",{originalTranscript:originalTranscript},smalltalk.ConsoleTranscriptTest)})},
 return self}, function($ctx1) {$ctx1.fill(self,"testShow",{originalTranscript:originalTranscript},smalltalk.ConsoleTranscriptTest)})},
 messageSends: ["current", "register:", "new", "shouldnt:raise:", "show:"]}),
 messageSends: ["current", "register:", "new", "shouldnt:raise:", "show:"]}),

+ 5 - 2
js/Kernel-Tests.js

@@ -2345,7 +2345,6 @@ smalltalk.StringTest.klass);
 
 
 
 
 smalltalk.addClass('ConsoleTranscriptTest', smalltalk.TestCase, [], 'Kernel-Tests');
 smalltalk.addClass('ConsoleTranscriptTest', smalltalk.TestCase, [], 'Kernel-Tests');
-smalltalk.ConsoleTranscriptTest.comment="I will try to ensure that you can print to a browser's console."
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "testShow",
 selector: "testShow",
@@ -2363,10 +2362,14 @@ _st(self)._shouldnt_raise_((function(){
 return smalltalk.withContext(function($ctx2) {
 return smalltalk.withContext(function($ctx2) {
 return _st($Transcript())._show_("Hello console!");
 return _st($Transcript())._show_("Hello console!");
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),$Error());
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),$Error());
+_st(self)._shouldnt_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return _st($Transcript())._show_(window);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}),$Error());
 _st($Transcript())._register_(originalTranscript);
 _st($Transcript())._register_(originalTranscript);
 return self}, function($ctx1) {$ctx1.fill(self,"testShow",{originalTranscript:originalTranscript},smalltalk.ConsoleTranscriptTest)})},
 return self}, function($ctx1) {$ctx1.fill(self,"testShow",{originalTranscript:originalTranscript},smalltalk.ConsoleTranscriptTest)})},
 args: [],
 args: [],
-source: "testShow\x0a| originalTranscript |\x0aoriginalTranscript := Transcript current.\x0aTranscript register: ConsoleTranscript new.\x0aself shouldnt: [ Transcript show: 'Hello console!' ] raise: Error.\x0aTranscript register: originalTranscript.",
+source: "testShow\x0a| originalTranscript |\x0aoriginalTranscript := Transcript current.\x0aTranscript register: ConsoleTranscript new.\x0a\x0aself shouldnt: [ Transcript show: 'Hello console!' ] raise: Error.\x0aself shouldnt: [ Transcript show: window ] raise: Error.\x0a\x0aTranscript register: originalTranscript.",
 messageSends: ["current", "register:", "new", "shouldnt:raise:", "show:"],
 messageSends: ["current", "register:", "new", "shouldnt:raise:", "show:"],
 referencedClasses: ["Transcript", "ConsoleTranscript", "Error"]
 referencedClasses: ["Transcript", "ConsoleTranscript", "Error"]
 }),
 }),

+ 1 - 1
js/Kernel-Transcript.deploy.js

@@ -36,7 +36,7 @@ selector: "show:",
 fn: function (anObject){
 fn: function (anObject){
 var self=this;
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 return smalltalk.withContext(function($ctx1) { 
-console.log(String(anObject._asString()));
+console.log(String(_st(anObject)._asString()));
 return self}, function($ctx1) {$ctx1.fill(self,"show:",{anObject:anObject},smalltalk.ConsoleTranscript)})},
 return self}, function($ctx1) {$ctx1.fill(self,"show:",{anObject:anObject},smalltalk.ConsoleTranscript)})},
 messageSends: []}),
 messageSends: []}),
 smalltalk.ConsoleTranscript);
 smalltalk.ConsoleTranscript);

+ 2 - 2
js/Kernel-Transcript.js

@@ -53,10 +53,10 @@ category: 'printing',
 fn: function (anObject){
 fn: function (anObject){
 var self=this;
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 return smalltalk.withContext(function($ctx1) { 
-console.log(String(anObject._asString()));
+console.log(String(_st(anObject)._asString()));
 return self}, function($ctx1) {$ctx1.fill(self,"show:",{anObject:anObject},smalltalk.ConsoleTranscript)})},
 return self}, function($ctx1) {$ctx1.fill(self,"show:",{anObject:anObject},smalltalk.ConsoleTranscript)})},
 args: ["anObject"],
 args: ["anObject"],
-source: "show: anObject\x0a\x09<console.log(String(anObject._asString()))>",
+source: "show: anObject\x0a\x22Smalltalk objects should have no trouble displaying themselves on the Transcript; Javascript objects don't know how, so must be wrapped in a JSObectProxy.\x22\x0a<console.log(String(_st(anObject)._asString()))>",
 messageSends: [],
 messageSends: [],
 referencedClasses: []
 referencedClasses: []
 }),
 }),

+ 3 - 2
st/Kernel-Tests.st

@@ -893,8 +893,6 @@ collectionClass
 TestCase subclass: #ConsoleTranscriptTest
 TestCase subclass: #ConsoleTranscriptTest
 	instanceVariableNames: ''
 	instanceVariableNames: ''
 	package: 'Kernel-Tests'!
 	package: 'Kernel-Tests'!
-!ConsoleTranscriptTest commentStamp!
-I will try to ensure that you can print to a browser's console.!
 
 
 !ConsoleTranscriptTest methodsFor: 'tests'!
 !ConsoleTranscriptTest methodsFor: 'tests'!
 
 
@@ -902,7 +900,10 @@ testShow
 | originalTranscript |
 | originalTranscript |
 originalTranscript := Transcript current.
 originalTranscript := Transcript current.
 Transcript register: ConsoleTranscript new.
 Transcript register: ConsoleTranscript new.
+
 self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
 self shouldnt: [ Transcript show: 'Hello console!!' ] raise: Error.
+self shouldnt: [ Transcript show: window ] raise: Error.
+
 Transcript register: originalTranscript.
 Transcript register: originalTranscript.
 ! !
 ! !
 
 

+ 2 - 1
st/Kernel-Transcript.st

@@ -23,7 +23,8 @@ cr
 !
 !
 
 
 show: anObject
 show: anObject
-	<console.log(String(anObject._asString()))>
+"Smalltalk objects should have no trouble displaying themselves on the Transcript; Javascript objects don't know how, so must be wrapped in a JSObectProxy."
+<console.log(String(_st(anObject)._asString()))>
 ! !
 ! !
 
 
 !ConsoleTranscript class methodsFor: 'initialization'!
 !ConsoleTranscript class methodsFor: 'initialization'!