Archive for the ‘JSX’ Category

  1. Hello, JSX World!

    Posted on 3月 20th, 2013 by cx20

    JSX

    JSX は DeNA により開発された静的型付けを特徴としたスクリプト言語である。JavaScript にコンパイルすることが可能となっている。

    ソースコード(JSX)

    class _Main {
        static function main(args : string[]) :void {
            log "Hello, JSX World!";
        }
    }

    上記コードを JavaScript にコンパイルした場合、以下のコードが生成される。

    ソースコード(JavaScript)

    #!/usr/local/Cellar/node/0.6.6/bin/node
    // generatedy by JSX compiler 0.9.2 (2013-01-30 11:14:28 +0900; 1a8b01b71633517541daa97862da204a2005c8aa)
    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) {
    	console.log("Hello, JSX World!");
    };
     
    var _Main$main$AS = _Main.main$AS;
     
    var $__jsx_classMap = {
    	"hello.jsx": {
    		_Main: _Main,
    		_Main$: _Main$
    	}
    };
     
     
    /**
     * launches _Main.main(:string[]):void invoked by jsx --run|--executable
     */
    JSX.runMain = function (sourceFile, args) {
    	var module = JSX.require(sourceFile);
     
    	if (! module._Main) {
    		throw new Error("entry point _Main not found in " + sourceFile);
    	}
    	if (! module._Main.main$AS) {
    		throw new Error("entry point _Main.main(:string[]):void not found in " + sourceFile);
    	}
    	module._Main.main$AS(args);
    };
     
    /**
     * launches _Test#test*():void invoked by jsx --test
     */
    JSX.runTests = function (sourceFile, tests) {
    	var module = JSX.require(sourceFile);
    	var testClass = module._Test$;
     
    	if (!testClass) return; // skip if there's no test class
     
    	if(tests.length === 0) {
    		var p = testClass.prototype;
    		for (var m in p) {
    			if (p[m] instanceof Function
    				&& /^test.*[$]$/.test(m)) {
    				tests.push(m);
    			}
    		}
    	}
    	else { // set as process arguments
    		tests = tests.map(function (name) {
    			return name + "$"; // mangle for function test*():void
    		});
    	}
     
    	var testCase = new testClass();
     
    	if (testCase.beforeClass$AS != null)
    		testCase.beforeClass$AS(tests);
     
    	for (var i = 0; i < tests.length; ++i) {
    		(function (method) {
    			if (method in testCase) {
    				testCase.run$SF$V$(method, function() { testCase[method](); });
    			}
    			else {
    				throw new ReferenceError("No such test method: " + method);
    			}
    		}(tests[i]));
    	}
     
    	if (testCase.afterClass$ != null)
    		testCase.afterClass$();
    };
    JSX.runMain("hello.jsx", process.argv.slice(2))
    })();

    実行方法(JSX による実行例)

    C:¥> jsx --run hello.jsx

    実行方法(Node.js による実行例)

    $ jsx --executable node --output hello.jsx.js hello.jsx
    $ node hello.jsx.js

    実行結果

    Hello, JSX World!