Spaces.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. define("amber_core/Spaces", ["amber_vm/smalltalk", "amber_vm/nil", "amber_vm/_st", "amber_vm/globals", "amber_core/Kernel-Objects", "amber_core/Kernel-Exceptions", "amber_core/SUnit"], function(smalltalk,nil,_st, globals){
  2. smalltalk.addPackage('Spaces');
  3. smalltalk.packages["Spaces"].transport = {"type":"amd","amdNamespace":"amber_core"};
  4. smalltalk.addClass('ObjectSpace', globals.Object, ['frame'], 'Spaces');
  5. globals.ObjectSpace.comment="I am a connection to another Smalltalk environment.\x0aThe implementation creates an iframe on the same location as the window, and connect to the Amber environment.\x0a\x0a\x0a\x0a## Usage example:\x0a\x0a\x09| space |\x0a\x09\x0a\x09space := ObjectSpace new.\x0a\x09space do: [ smalltalk ] \x22Answers aSmalltalk\x22\x0a\x09(space do: [ smalltalk ]) == smalltalk \x22Answers false\x22\x0a\x09\x0a\x09space release \x22Remove the object space environment\x22";
  6. smalltalk.addMethod(
  7. smalltalk.method({
  8. selector: "connectTo:",
  9. protocol: 'initialization',
  10. fn: function (aFrame){
  11. var self=this;
  12. return smalltalk.withContext(function($ctx1) {
  13. self._release();
  14. self["@frame"]=aFrame;
  15. return self}, function($ctx1) {$ctx1.fill(self,"connectTo:",{aFrame:aFrame},globals.ObjectSpace)})},
  16. args: ["aFrame"],
  17. source: "connectTo: aFrame\x0a\x09self release.\x0a\x09frame := aFrame",
  18. messageSends: ["release"],
  19. referencedClasses: []
  20. }),
  21. globals.ObjectSpace);
  22. smalltalk.addMethod(
  23. smalltalk.method({
  24. selector: "create",
  25. protocol: 'initialization',
  26. fn: function (){
  27. var self=this;
  28. return smalltalk.withContext(function($ctx1) {
  29. var $1;
  30. $1="body"._asJQuery();
  31. $ctx1.sendIdx["asJQuery"]=1;
  32. _st($1)._append_("<iframe style=\x22display: none;\x22></iframe>");
  33. self["@frame"]=_st(_st("iframe"._asJQuery())._get())._last();
  34. _st(_st(self["@frame"])._contentWindow())._location_(_st(window)._location());
  35. return self}, function($ctx1) {$ctx1.fill(self,"create",{},globals.ObjectSpace)})},
  36. args: [],
  37. source: "create\x0a\x09'body' asJQuery append: '<iframe style=\x22display: none;\x22></iframe>'.\x0a\x09frame := 'iframe' asJQuery get last.\x0a\x09frame contentWindow location: window location",
  38. messageSends: ["append:", "asJQuery", "last", "get", "location:", "contentWindow", "location"],
  39. referencedClasses: []
  40. }),
  41. globals.ObjectSpace);
  42. smalltalk.addMethod(
  43. smalltalk.method({
  44. selector: "destroy",
  45. protocol: 'releasing',
  46. fn: function (){
  47. var self=this;
  48. return smalltalk.withContext(function($ctx1) {
  49. var $1,$receiver;
  50. $1=self["@frame"];
  51. if(($receiver = $1) == null || $receiver.isNil){
  52. return self;
  53. } else {
  54. $1;
  55. };
  56. _st(_st(self["@frame"])._asJQuery())._remove();
  57. self._release();
  58. return self}, function($ctx1) {$ctx1.fill(self,"destroy",{},globals.ObjectSpace)})},
  59. args: [],
  60. source: "destroy\x0a\x09frame ifNil: [ ^ self ].\x0a\x09frame asJQuery remove.\x0a\x0a\x09self release",
  61. messageSends: ["ifNil:", "remove", "asJQuery", "release"],
  62. referencedClasses: []
  63. }),
  64. globals.ObjectSpace);
  65. smalltalk.addMethod(
  66. smalltalk.method({
  67. selector: "do:",
  68. protocol: 'evaluating',
  69. fn: function (aBlock){
  70. var self=this;
  71. function $ObjectSpaceConnectionError(){return globals.ObjectSpaceConnectionError||(typeof ObjectSpaceConnectionError=="undefined"?nil:ObjectSpaceConnectionError)}
  72. return smalltalk.withContext(function($ctx1) {
  73. var $1,$2,$4,$5,$3;
  74. $1=self._isConnected();
  75. if(! smalltalk.assert($1)){
  76. $2=_st($ObjectSpaceConnectionError())._signal();
  77. return $2;
  78. };
  79. $4=_st(self["@frame"])._contentWindow();
  80. $5=_st("(".__comma(_st(aBlock)._compiledSource())).__comma(")()");
  81. $ctx1.sendIdx[","]=1;
  82. $3=_st($4)._eval_($5);
  83. return $3;
  84. }, function($ctx1) {$ctx1.fill(self,"do:",{aBlock:aBlock},globals.ObjectSpace)})},
  85. args: ["aBlock"],
  86. source: "do: aBlock\x0a\x09self isConnected ifFalse: [ ^ ObjectSpaceConnectionError signal ].\x0a\x09^ frame contentWindow eval: '(', aBlock compiledSource, ')()'",
  87. messageSends: ["ifFalse:", "isConnected", "signal", "eval:", "contentWindow", ",", "compiledSource"],
  88. referencedClasses: ["ObjectSpaceConnectionError"]
  89. }),
  90. globals.ObjectSpace);
  91. smalltalk.addMethod(
  92. smalltalk.method({
  93. selector: "frame",
  94. protocol: 'accessing',
  95. fn: function (){
  96. var self=this;
  97. var $1;
  98. $1=self["@frame"];
  99. return $1;
  100. },
  101. args: [],
  102. source: "frame\x0a\x09^ frame",
  103. messageSends: [],
  104. referencedClasses: []
  105. }),
  106. globals.ObjectSpace);
  107. smalltalk.addMethod(
  108. smalltalk.method({
  109. selector: "initialize",
  110. protocol: 'initialization',
  111. fn: function (){
  112. var self=this;
  113. return smalltalk.withContext(function($ctx1) {
  114. ($ctx1.supercall = true, globals.ObjectSpace.superclass.fn.prototype._initialize.apply(_st(self), []));
  115. $ctx1.supercall = false;
  116. self._create();
  117. return self}, function($ctx1) {$ctx1.fill(self,"initialize",{},globals.ObjectSpace)})},
  118. args: [],
  119. source: "initialize\x0a\x09super initialize.\x0a\x09self create",
  120. messageSends: ["initialize", "create"],
  121. referencedClasses: []
  122. }),
  123. globals.ObjectSpace);
  124. smalltalk.addMethod(
  125. smalltalk.method({
  126. selector: "isConnected",
  127. protocol: 'initialization',
  128. fn: function (){
  129. var self=this;
  130. return smalltalk.withContext(function($ctx1) {
  131. var $1;
  132. $1=_st(self._frame())._notNil();
  133. return $1;
  134. }, function($ctx1) {$ctx1.fill(self,"isConnected",{},globals.ObjectSpace)})},
  135. args: [],
  136. source: "isConnected\x0a\x09^ self frame notNil",
  137. messageSends: ["notNil", "frame"],
  138. referencedClasses: []
  139. }),
  140. globals.ObjectSpace);
  141. smalltalk.addMethod(
  142. smalltalk.method({
  143. selector: "release",
  144. protocol: 'releasing',
  145. fn: function (){
  146. var self=this;
  147. self["@frame"]=nil;
  148. return self},
  149. args: [],
  150. source: "release\x0a\x09frame := nil",
  151. messageSends: [],
  152. referencedClasses: []
  153. }),
  154. globals.ObjectSpace);
  155. smalltalk.addMethod(
  156. smalltalk.method({
  157. selector: "whenReadyDo:",
  158. protocol: 'events',
  159. fn: function (aBlock){
  160. var self=this;
  161. return smalltalk.withContext(function($ctx1) {
  162. _st(_st(self["@frame"])._asJQuery())._bind_do_("load",aBlock);
  163. return self}, function($ctx1) {$ctx1.fill(self,"whenReadyDo:",{aBlock:aBlock},globals.ObjectSpace)})},
  164. args: ["aBlock"],
  165. source: "whenReadyDo: aBlock\x0a\x09frame asJQuery\x0a\x09\x09bind: 'load'\x0a\x09\x09do: aBlock",
  166. messageSends: ["bind:do:", "asJQuery"],
  167. referencedClasses: []
  168. }),
  169. globals.ObjectSpace);
  170. smalltalk.addMethod(
  171. smalltalk.method({
  172. selector: "on:",
  173. protocol: 'instance creation',
  174. fn: function (aFrame){
  175. var self=this;
  176. return smalltalk.withContext(function($ctx1) {
  177. var $2,$3,$1;
  178. $2=self._basicNew();
  179. _st($2)._connectTo_(aFrame);
  180. $3=_st($2)._yourself();
  181. $1=$3;
  182. return $1;
  183. }, function($ctx1) {$ctx1.fill(self,"on:",{aFrame:aFrame},globals.ObjectSpace.klass)})},
  184. args: ["aFrame"],
  185. source: "on: aFrame\x0a\x09^ self basicNew\x0a\x09\x09connectTo: aFrame;\x0a\x09\x09yourself",
  186. messageSends: ["connectTo:", "basicNew", "yourself"],
  187. referencedClasses: []
  188. }),
  189. globals.ObjectSpace.klass);
  190. smalltalk.addClass('ObjectSpaceConnectionError', globals.Error, [], 'Spaces');
  191. smalltalk.addMethod(
  192. smalltalk.method({
  193. selector: "messageText",
  194. protocol: 'accessing',
  195. fn: function (){
  196. var self=this;
  197. return "The ObjectSpace is not connected";
  198. },
  199. args: [],
  200. source: "messageText\x0a\x09^ 'The ObjectSpace is not connected'",
  201. messageSends: [],
  202. referencedClasses: []
  203. }),
  204. globals.ObjectSpaceConnectionError);
  205. smalltalk.addClass('ObjectSpaceTest', globals.TestCase, ['space'], 'Spaces');
  206. smalltalk.addMethod(
  207. smalltalk.method({
  208. selector: "setUp",
  209. protocol: 'initialization',
  210. fn: function (){
  211. var self=this;
  212. function $ObjectSpace(){return globals.ObjectSpace||(typeof ObjectSpace=="undefined"?nil:ObjectSpace)}
  213. return smalltalk.withContext(function($ctx1) {
  214. self["@space"]=_st($ObjectSpace())._new();
  215. return self}, function($ctx1) {$ctx1.fill(self,"setUp",{},globals.ObjectSpaceTest)})},
  216. args: [],
  217. source: "setUp\x0a\x09space := ObjectSpace new",
  218. messageSends: ["new"],
  219. referencedClasses: ["ObjectSpace"]
  220. }),
  221. globals.ObjectSpaceTest);
  222. smalltalk.addMethod(
  223. smalltalk.method({
  224. selector: "tearDown",
  225. protocol: 'initialization',
  226. fn: function (){
  227. var self=this;
  228. return smalltalk.withContext(function($ctx1) {
  229. _st(self["@space"])._destroy();
  230. return self}, function($ctx1) {$ctx1.fill(self,"tearDown",{},globals.ObjectSpaceTest)})},
  231. args: [],
  232. source: "tearDown\x0a\x09space destroy",
  233. messageSends: ["destroy"],
  234. referencedClasses: []
  235. }),
  236. globals.ObjectSpaceTest);
  237. smalltalk.addMethod(
  238. smalltalk.method({
  239. selector: "testConnection",
  240. protocol: 'tests',
  241. fn: function (){
  242. var self=this;
  243. function $ObjectSpaceConnectionError(){return globals.ObjectSpaceConnectionError||(typeof ObjectSpaceConnectionError=="undefined"?nil:ObjectSpaceConnectionError)}
  244. return smalltalk.withContext(function($ctx1) {
  245. _st(self["@space"])._destroy();
  246. self._deny_(_st(self["@space"])._isConnected());
  247. self._should_raise_((function(){
  248. return smalltalk.withContext(function($ctx2) {
  249. return _st(self["@space"])._do_((function(){
  250. }));
  251. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$ObjectSpaceConnectionError());
  252. return self}, function($ctx1) {$ctx1.fill(self,"testConnection",{},globals.ObjectSpaceTest)})},
  253. args: [],
  254. source: "testConnection\x0a\x09space destroy.\x0a\x09self deny: space isConnected.\x0a\x09self should: [ space do: [] ] raise: ObjectSpaceConnectionError",
  255. messageSends: ["destroy", "deny:", "isConnected", "should:raise:", "do:"],
  256. referencedClasses: ["ObjectSpaceConnectionError"]
  257. }),
  258. globals.ObjectSpaceTest);
  259. smalltalk.addMethod(
  260. smalltalk.method({
  261. selector: "testCreate",
  262. protocol: 'tests',
  263. fn: function (){
  264. var self=this;
  265. return smalltalk.withContext(function($ctx1) {
  266. self._assert_(_st(_st(self["@space"])._frame())._notNil());
  267. $ctx1.sendIdx["assert:"]=1;
  268. self._assert_(_st(self["@space"])._isConnected());
  269. return self}, function($ctx1) {$ctx1.fill(self,"testCreate",{},globals.ObjectSpaceTest)})},
  270. args: [],
  271. source: "testCreate\x0a\x0a\x09self assert: space frame notNil.\x0a\x09self assert: space isConnected",
  272. messageSends: ["assert:", "notNil", "frame", "isConnected"],
  273. referencedClasses: []
  274. }),
  275. globals.ObjectSpaceTest);
  276. smalltalk.addMethod(
  277. smalltalk.method({
  278. selector: "testEvaluation",
  279. protocol: 'tests',
  280. fn: function (){
  281. var self=this;
  282. var result;
  283. function $Smalltalk(){return globals.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  284. return smalltalk.withContext(function($ctx1) {
  285. var $2,$1;
  286. _st(self["@space"])._whenReadyDo_((function(){
  287. return smalltalk.withContext(function($ctx2) {
  288. result=_st(self["@space"])._do_((function(){
  289. return smalltalk;
  290. }));
  291. result;
  292. $2=_st(result)._class();
  293. $ctx2.sendIdx["class"]=1;
  294. $1=_st($2)._name();
  295. self._assert_equals_($1,"Smalltalk");
  296. self._deny_(_st(_st(result)._class()).__eq($Smalltalk()));
  297. $ctx2.sendIdx["deny:"]=1;
  298. return self._deny_(_st(result).__eq_eq(smalltalk));
  299. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}));
  300. return self}, function($ctx1) {$ctx1.fill(self,"testEvaluation",{result:result},globals.ObjectSpaceTest)})},
  301. args: [],
  302. source: "testEvaluation\x0a\x09| result |\x0a\x0a\x09space whenReadyDo: [\x0a\x09\x09result := space do: [ smalltalk ].\x0a\x0a\x09\x09self assert: result class name equals: 'Smalltalk'.\x0a\x09\x09self deny: result class = Smalltalk.\x0a\x09\x09self deny: result == smalltalk ]",
  303. messageSends: ["whenReadyDo:", "do:", "assert:equals:", "name", "class", "deny:", "=", "=="],
  304. referencedClasses: ["Smalltalk"]
  305. }),
  306. globals.ObjectSpaceTest);
  307. smalltalk.addMethod(
  308. smalltalk.method({
  309. selector: "testRelease",
  310. protocol: 'tests',
  311. fn: function (){
  312. var self=this;
  313. return smalltalk.withContext(function($ctx1) {
  314. var $2,$1;
  315. $2=_st(self["@space"])._frame();
  316. $ctx1.sendIdx["frame"]=1;
  317. $1=_st($2)._isNil();
  318. $ctx1.sendIdx["isNil"]=1;
  319. self._deny_($1);
  320. _st(self["@space"])._release();
  321. self._assert_(_st(_st(self["@space"])._frame())._isNil());
  322. return self}, function($ctx1) {$ctx1.fill(self,"testRelease",{},globals.ObjectSpaceTest)})},
  323. args: [],
  324. source: "testRelease\x0a\x0a\x09self deny: space frame isNil.\x0a\x0a\x09space release.\x0a\x09\x0a\x09self assert: space frame isNil",
  325. messageSends: ["deny:", "isNil", "frame", "release", "assert:"],
  326. referencedClasses: []
  327. }),
  328. globals.ObjectSpaceTest);
  329. });