1
0

Platform-Node.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. define(["amber/boot", "require", "amber/core/Kernel-Objects"], function($boot,requirejs){"use strict";
  2. var $core=$boot.api,nil=$boot.nilAsValue,$nil=$boot.nilAsReceiver,$recv=$boot.asReceiver,$globals=$boot.globals;
  3. var $pkg = $core.addPackage("Platform-Node");
  4. $pkg.imports = ["amber/core/Platform-Services"];
  5. //>>excludeStart("imports", pragmas.excludeImports);
  6. $pkg.isReady = new Promise(function (resolve, reject) { requirejs(["amber/core/Platform-Services"], function () {resolve();}, reject); });
  7. //>>excludeEnd("imports");
  8. $pkg.transport = {"type":"amd","amdNamespace":"amber/core"};
  9. $core.addClass("NodePlatform", $globals.Object, "Platform-Node");
  10. //>>excludeStart("ide", pragmas.excludeIdeData);
  11. $globals.NodePlatform.comment="I am `Platform` service implementation for node-like environment.";
  12. //>>excludeEnd("ide");
  13. $core.addMethod(
  14. $core.method({
  15. selector: "fetch:",
  16. protocol: "public API",
  17. //>>excludeStart("ide", pragmas.excludeIdeData);
  18. args: ["aStringOrObject"],
  19. source: "fetch: aStringOrObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aStringOrObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
  20. referencedClasses: ["Promise"],
  21. //>>excludeEnd("ide");
  22. pragmas: [],
  23. messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:", "signal:"]
  24. }, function ($methodClass){ return function (aStringOrObject){
  25. var self=this,$self=this;
  26. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  27. return $core.withContext(function($ctx1) {
  28. //>>excludeEnd("ctx");
  29. return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
  30. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  31. return $core.withContext(function($ctx2) {
  32. //>>excludeEnd("ctx");
  33. return $recv(fetch)._value_(aStringOrObject);
  34. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  35. }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
  36. //>>excludeEnd("ctx");
  37. }),(function(){
  38. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  39. return $core.withContext(function($ctx2) {
  40. //>>excludeEnd("ctx");
  41. return $recv($globals.Promise)._signal_("fetch not available.");
  42. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  43. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
  44. //>>excludeEnd("ctx");
  45. }));
  46. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  47. }, function($ctx1) {$ctx1.fill(self,"fetch:",{aStringOrObject:aStringOrObject})});
  48. //>>excludeEnd("ctx");
  49. }; }),
  50. $globals.NodePlatform);
  51. $core.addMethod(
  52. $core.method({
  53. selector: "fetchUrl:options:",
  54. protocol: "public API",
  55. //>>excludeStart("ide", pragmas.excludeIdeData);
  56. args: ["aString", "anObject"],
  57. source: "fetchUrl: aString options: anObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aString value: anObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
  58. referencedClasses: ["Promise"],
  59. //>>excludeEnd("ide");
  60. pragmas: [],
  61. messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:value:", "signal:"]
  62. }, function ($methodClass){ return function (aString,anObject){
  63. var self=this,$self=this;
  64. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  65. return $core.withContext(function($ctx1) {
  66. //>>excludeEnd("ctx");
  67. return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
  68. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  69. return $core.withContext(function($ctx2) {
  70. //>>excludeEnd("ctx");
  71. return $recv(fetch)._value_value_(aString,anObject);
  72. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  73. }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
  74. //>>excludeEnd("ctx");
  75. }),(function(){
  76. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  77. return $core.withContext(function($ctx2) {
  78. //>>excludeEnd("ctx");
  79. return $recv($globals.Promise)._signal_("fetch not available.");
  80. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  81. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
  82. //>>excludeEnd("ctx");
  83. }));
  84. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  85. }, function($ctx1) {$ctx1.fill(self,"fetchUrl:options:",{aString:aString,anObject:anObject})});
  86. //>>excludeEnd("ctx");
  87. }; }),
  88. $globals.NodePlatform);
  89. $core.addMethod(
  90. $core.method({
  91. selector: "globals",
  92. protocol: "accessing",
  93. //>>excludeStart("ide", pragmas.excludeIdeData);
  94. args: [],
  95. source: "globals\x0a\x09^ global",
  96. referencedClasses: [],
  97. //>>excludeEnd("ide");
  98. pragmas: [],
  99. messageSends: []
  100. }, function ($methodClass){ return function (){
  101. var self=this,$self=this;
  102. return global;
  103. }; }),
  104. $globals.NodePlatform);
  105. $core.addMethod(
  106. $core.method({
  107. selector: "initialize",
  108. protocol: "initialization",
  109. //>>excludeStart("ide", pragmas.excludeIdeData);
  110. args: [],
  111. source: "initialize\x0a\x09process\x0a\x09\x09on: 'uncaughtException'\x0a\x09\x09do: [ :err | ErrorHandler handleError: err. process exit: 1 ];\x0a\x09\x09on: 'unhandledRejection'\x0a\x09\x09do: [ :err | ErrorHandler handleError: err. process exit: 2 ]",
  112. referencedClasses: ["ErrorHandler"],
  113. //>>excludeEnd("ide");
  114. pragmas: [],
  115. messageSends: ["on:do:", "handleError:", "exit:"]
  116. }, function ($methodClass){ return function (){
  117. var self=this,$self=this;
  118. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  119. return $core.withContext(function($ctx1) {
  120. //>>excludeEnd("ctx");
  121. var $1;
  122. $1=process;
  123. [$recv($1)._on_do_("uncaughtException",(function(err){
  124. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  125. return $core.withContext(function($ctx2) {
  126. //>>excludeEnd("ctx");
  127. [$recv($globals.ErrorHandler)._handleError_(err)
  128. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  129. ,$ctx2.sendIdx["handleError:"]=1
  130. //>>excludeEnd("ctx");
  131. ][0];
  132. return [$recv(process)._exit_((1))
  133. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  134. ,$ctx2.sendIdx["exit:"]=1
  135. //>>excludeEnd("ctx");
  136. ][0];
  137. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  138. }, function($ctx2) {$ctx2.fillBlock({err:err},$ctx1,1)});
  139. //>>excludeEnd("ctx");
  140. }))
  141. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  142. ,$ctx1.sendIdx["on:do:"]=1
  143. //>>excludeEnd("ctx");
  144. ][0];
  145. $recv($1)._on_do_("unhandledRejection",(function(err){
  146. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  147. return $core.withContext(function($ctx2) {
  148. //>>excludeEnd("ctx");
  149. $recv($globals.ErrorHandler)._handleError_(err);
  150. return $recv(process)._exit_((2));
  151. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  152. }, function($ctx2) {$ctx2.fillBlock({err:err},$ctx1,2)});
  153. //>>excludeEnd("ctx");
  154. }));
  155. return self;
  156. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  157. }, function($ctx1) {$ctx1.fill(self,"initialize",{})});
  158. //>>excludeEnd("ctx");
  159. }; }),
  160. $globals.NodePlatform);
  161. $core.addMethod(
  162. $core.method({
  163. selector: "newXhr",
  164. protocol: "public API",
  165. //>>excludeStart("ide", pragmas.excludeIdeData);
  166. args: [],
  167. source: "newXhr\x0a\x09XMLHttpRequest\x0a\x09\x09ifNotNil: [ ^ XMLHttpRequest new ]\x0a\x09\x09ifNil: [ self error: 'XMLHttpRequest not available.' ]",
  168. referencedClasses: ["XMLHttpRequest"],
  169. //>>excludeEnd("ide");
  170. pragmas: [],
  171. messageSends: ["ifNotNil:ifNil:", "new", "error:"]
  172. }, function ($methodClass){ return function (){
  173. var self=this,$self=this;
  174. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  175. return $core.withContext(function($ctx1) {
  176. //>>excludeEnd("ctx");
  177. var $1;
  178. $1=$globals.XMLHttpRequest;
  179. if($1 == null || $1.a$nil){
  180. $self._error_("XMLHttpRequest not available.");
  181. } else {
  182. return $recv($globals.XMLHttpRequest)._new();
  183. }
  184. return self;
  185. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  186. }, function($ctx1) {$ctx1.fill(self,"newXhr",{})});
  187. //>>excludeEnd("ctx");
  188. }; }),
  189. $globals.NodePlatform);
  190. $core.addMethod(
  191. $core.method({
  192. selector: "initialize",
  193. protocol: "testing",
  194. //>>excludeStart("ide", pragmas.excludeIdeData);
  195. args: [],
  196. source: "initialize\x0a\x09self isFeasible ifTrue: [ Platform registerIfNone: self new ]",
  197. referencedClasses: ["Platform"],
  198. //>>excludeEnd("ide");
  199. pragmas: [],
  200. messageSends: ["ifTrue:", "isFeasible", "registerIfNone:", "new"]
  201. }, function ($methodClass){ return function (){
  202. var self=this,$self=this;
  203. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  204. return $core.withContext(function($ctx1) {
  205. //>>excludeEnd("ctx");
  206. if($core.assert($self._isFeasible())){
  207. $recv($globals.Platform)._registerIfNone_($self._new());
  208. }
  209. return self;
  210. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  211. }, function($ctx1) {$ctx1.fill(self,"initialize",{})});
  212. //>>excludeEnd("ctx");
  213. }; }),
  214. $globals.NodePlatform.a$cls);
  215. $core.addMethod(
  216. $core.method({
  217. selector: "isFeasible",
  218. protocol: "testing",
  219. //>>excludeStart("ide", pragmas.excludeIdeData);
  220. args: [],
  221. source: "isFeasible\x0a<inlineJS: 'return typeof process !== \x22undefined\x22 && process && process.versions && process.versions.node != null'>",
  222. referencedClasses: [],
  223. //>>excludeEnd("ide");
  224. pragmas: [["inlineJS:", ["return typeof process !== \x22undefined\x22 && process && process.versions && process.versions.node != null"]]],
  225. messageSends: []
  226. }, function ($methodClass){ return function (){
  227. var self=this,$self=this;
  228. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  229. return $core.withContext(function($ctx1) {
  230. //>>excludeEnd("ctx");
  231. return typeof process !== "undefined" && process && process.versions && process.versions.node != null;
  232. return self;
  233. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  234. }, function($ctx1) {$ctx1.fill(self,"isFeasible",{})});
  235. //>>excludeEnd("ctx");
  236. }; }),
  237. $globals.NodePlatform.a$cls);
  238. });