Spaces.js 12 KB

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