JSXでnode.jsを使ったhttpサーバを書いてみたよ
JSXでnode.jsを使ったhttpサーバを書いてみたのです。
と言ってもまあ、大したことをしているわけでもない上にまともに出来てないのですけど。
環境:Ubuntu Server 12.04 LTS 32bit / node.js 0.8.8 / JSX 0.0.1 commit 43b29a509f6be2ea9aa1b2314e0b910cbfbc0e83
node.jsx
native class global { static function require(filename: string): Object; }
http.jsx
import 'js.jsx'; native class http { function createServer(): Server; function createServer(requestListener: function(: ServerRequest, : ServerResponse): void): Server; } native class Server { function listen(port: int): void; } native class ServerRequest { var method: string; var url: string; function setEncoding(encoding: string): void; } native class ServerResponse { function writeHead(statusCode: int): void; function writeHead(statusCode: int, reasonPhrase: Map.<string>): void; //function writeHead(statusCode: int, reasonPhrase: Map.<variant>): void; function end(): void; function end(data: string): void; function end(data: string, encoding: string): void; }
app.jsx
import 'node.jsx'; import 'http.jsx'; final class _Main { static function main(args: string[]): void { var _http: http = global.require('http') as http; _http.createServer((req: ServerRequest, res: ServerResponse): void -> { res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' }); res.end('Hello, World!'); }).listen(3000); } }
Makefile
.PHONY: all all: $(HOME)/Repos/git/JSX/bin/jsx app.jsx --release --executable node > app.js
これらをコンパイルすると……
app.js
var JSX = {}; (function () { /** * copies the implementations from source interface to target */ function $__jsx_merge_interface(target, source) { for (var k in source.prototype) if (source.prototype.hasOwnProperty(k)) target.prototype[k] = source.prototype[k]; } /** * defers the initialization of the property */ function $__jsx_lazy_init(obj, prop, func) { function reset(obj, prop, value) { delete obj[prop]; obj[prop] = value; return value; } Object.defineProperty(obj, prop, { get: function () { return reset(obj, prop, func()); }, set: function (v) { reset(obj, prop, v); }, enumerable: true, configurable: true }); } /** * sideeffect().a /= b */ function $__jsx_div_assign(obj, prop, divisor) { return obj[prop] = (obj[prop] / divisor) | 0; } /* * global functions called by JSX * (enamed so that they do not conflict with local variable names) */ var $__jsx_parseInt = parseInt; var $__jsx_parseFloat = parseFloat; var $__jsx_isNaN = isNaN; var $__jsx_isFinite = isFinite; var $__jsx_encodeURIComponent = encodeURIComponent; var $__jsx_decodeURIComponent = decodeURIComponent; var $__jsx_encodeURI = encodeURI; var $__jsx_decodeURI = decodeURI; var $__jsx_ObjectToString = Object.prototype.toString; var $__jsx_ObjectHasOwnProperty = Object.prototype.hasOwnProperty; /* * profiler object, initialized afterwards */ function $__jsx_profiler() { } /* * public interface to JSX code */ JSX.require = function (path) { var m = $__jsx_classMap[path]; return m !== undefined ? m : null; }; JSX.profilerIsRunning = function () { return $__jsx_profiler.getResults != null; }; JSX.getProfileResults = function () { return ($__jsx_profiler.getResults || function () { return {}; })(); }; JSX.postProfileResults = function (url) { if ($__jsx_profiler.postResults == null) throw new Error("profiler has not been turned on"); return $__jsx_profiler.postResults(url); }; JSX.resetProfileResults = function () { if ($__jsx_profiler.resetResults == null) throw new Error("profiler has not been turned on"); return $__jsx_profiler.resetResults(); }; /** * class _Main extends Object * @constructor */ function _Main() { } _Main.prototype = new Object; /** * @constructor */ function _Main$() { }; _Main$.prototype = new _Main; /** * @param {Array.<undefined|!string>} args */ _Main.main$AS = function (args) { /** @type {http} */ var _http; _http = (function (o) { return o instanceof http ? o : null; })(global.require('http')); _http.createServer((function (req, res) { res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' }); res.end('Hello, World!'); })).listen(3000); }; var _Main$main$AS = _Main.main$AS; /** * class js extends Object * @constructor */ function js() { } js.prototype = new Object; /** * @constructor */ function js$() { }; js$.prototype = new js; js.global = (function () { return this; })(); var $__jsx_classMap = { "app.jsx": { _Main: _Main, _Main$: _Main$ }, "system:lib/js/js.jsx": { js: js, js$: js$ } }; })();
な感じに。あとは一番うしろに
JSX.require('app.jsx')._Main.main$AS([]);
を付ければOK……じゃなかった。
- _http = (function (o) { return o instanceof http ? o : null; })(global.require('http')); + _http = require('http');
という風に直に書き換えるというアレなことをすると動くみたい。
$ node app.js
requireどうやって書くんだろう。てかこんなことしてる場合じゃない!