Browse Source

NodePlatform

Herbert Vojčík 9 years ago
parent
commit
3ac32d23c1
3 changed files with 146 additions and 1 deletions
  1. 1 1
      external/amber-dev/lib/amberc.js
  2. 116 0
      src/Platform-Node.js
  3. 29 0
      src/Platform-Node.st

+ 1 - 1
external/amber-dev/lib/amberc.js

@@ -74,7 +74,7 @@ function AmberCompiler(amber_dir) {
 	// Important: in next list, boot MUST be first
 	this.kernel_libraries = ['boot', 'Kernel-Objects', 'Kernel-Classes', 'Kernel-Methods',
 							'Kernel-Collections', 'Kernel-Infrastructure', 'Kernel-Exceptions', 'Kernel-Announcements',
-							'Platform-Services'];
+							'Platform-Services', 'Platform-Node'];
 	this.compiler_libraries = this.kernel_libraries.concat(['parser', 'Platform-ImportExport', 'Compiler-Exceptions',
 							'Compiler-Core', 'Compiler-AST', 'Compiler-Exceptions', 'Compiler-IR', 'Compiler-Inlining', 'Compiler-Semantic']);
 }

+ 116 - 0
src/Platform-Node.js

@@ -0,0 +1,116 @@
+define("amber_core/Platform-Node", ["amber/boot", "amber_core/Kernel-Objects"], function($boot){"use strict";
+var $core=$boot.api,nil=$boot.nil,$recv=$boot.asReceiver,$globals=$boot.globals;
+$core.addPackage('Platform-Node');
+$core.packages["Platform-Node"].innerEval = function (expr) { return eval(expr); };
+$core.packages["Platform-Node"].transport = {"type":"amd","amdNamespace":"amber_core"};
+
+$core.addClass('NodePlatform', $globals.Object, [], 'Platform-Node');
+//>>excludeStart("ide", pragmas.excludeIdeData);
+$globals.NodePlatform.comment="I am `Platform` service implementation for node-like environment.";
+//>>excludeEnd("ide");
+$core.addMethod(
+$core.method({
+selector: "globals",
+protocol: 'accessing',
+fn: function (){
+var self=this;
+var $1;
+$1=global;
+return $1;
+
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "globals\x0a\x09^ global",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: []
+}),
+$globals.NodePlatform);
+
+$core.addMethod(
+$core.method({
+selector: "newXhr",
+protocol: 'accessing',
+fn: function (){
+var self=this;
+function $XMLHttpRequest(){return $globals.XMLHttpRequest||(typeof XMLHttpRequest=="undefined"?nil:XMLHttpRequest)}
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$receiver;
+if(($receiver = $XMLHttpRequest()) == null || $receiver.isNil){
+self._error_("XMLHttpRequest not available.");
+} else {
+$1=$recv($XMLHttpRequest())._new();
+return $1;
+};
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"newXhr",{},$globals.NodePlatform)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "newXhr\x0a\x09XMLHttpRequest\x0a\x09\x09ifNotNil: [ ^ XMLHttpRequest new ]\x0a\x09\x09ifNil: [ self error: 'XMLHttpRequest not available.' ]",
+referencedClasses: ["XMLHttpRequest"],
+//>>excludeEnd("ide");
+messageSends: ["ifNotNil:ifNil:", "new", "error:"]
+}),
+$globals.NodePlatform);
+
+
+$core.addMethod(
+$core.method({
+selector: "initialize",
+protocol: 'testing',
+fn: function (){
+var self=this;
+function $Platform(){return $globals.Platform||(typeof Platform=="undefined"?nil:Platform)}
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+$1=self._isFeasible();
+if($core.assert($1)){
+$recv($Platform())._registerIfNone_(self._new());
+};
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"initialize",{},$globals.NodePlatform.klass)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "initialize\x0a\x09self isFeasible ifTrue: [ Platform registerIfNone: self new ]",
+referencedClasses: ["Platform"],
+//>>excludeEnd("ide");
+messageSends: ["ifTrue:", "isFeasible", "registerIfNone:", "new"]
+}),
+$globals.NodePlatform.klass);
+
+$core.addMethod(
+$core.method({
+selector: "isFeasible",
+protocol: 'testing',
+fn: function (){
+var self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return typeof global !== "undefined";
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"isFeasible",{},$globals.NodePlatform.klass)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "isFeasible\x0a<return typeof global !== \x22undefined\x22>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: []
+}),
+$globals.NodePlatform.klass);
+
+});

+ 29 - 0
src/Platform-Node.st

@@ -0,0 +1,29 @@
+Smalltalk createPackage: 'Platform-Node'!
+Object subclass: #NodePlatform
+	instanceVariableNames: ''
+	package: 'Platform-Node'!
+!NodePlatform commentStamp!
+I am `Platform` service implementation for node-like environment.!
+
+!NodePlatform methodsFor: 'accessing'!
+
+globals
+	^ global
+!
+
+newXhr
+	XMLHttpRequest
+		ifNotNil: [ ^ XMLHttpRequest new ]
+		ifNil: [ self error: 'XMLHttpRequest not available.' ]
+! !
+
+!NodePlatform class methodsFor: 'testing'!
+
+initialize
+	self isFeasible ifTrue: [ Platform registerIfNone: self new ]
+!
+
+isFeasible
+<return typeof global !!== "undefined">
+! !
+