浏览代码

do not generate RTL code for nodejs modules - reuse it

Vladislav Folts 11 年之前
父节点
当前提交
8eaee83e58

+ 8 - 20
src/nodejs.js

@@ -1,14 +1,15 @@
 "use strict";
 
-var Rtl = require("rtl.js");
+var Class = require("rtl.js").Class;
 var Code = require("code.js");
 var Context = require("context.js");
 var oc = require("oc.js");
+var RTL = require("rtl_code.js").RTL;
 
 var fs = require("fs");
 var path = require("path");
 
-var ModuleGenerator = Rtl.Class.extend({
+var ModuleGenerator = Class.extend({
     init: function Nodejs$ModuleGenerator(name, imports){
         this.__name = name;
         this.__imports = imports;
@@ -37,15 +38,9 @@ var ModuleGenerator = Rtl.Class.extend({
     }
 });
 
-var RtlCodeUsingWatcher = Rtl.Code.extend({
-    init: function(){
-        Rtl.Code.prototype.init.call(this);
-        this.__used = false;
-    },
-    get: function(func){
-        this.__used = true;
-        return Rtl.Code.prototype.get.call(this, func);
-    },
+var RtlCodeUsingWatcher = Class.extend({
+    init: function(){this.__used = false;},
+    using: function(){this.__used = true;},
     used: function(){return this.__used;},
     reset: function(){this.__used = false;}
 });
@@ -57,7 +52,7 @@ function writeCompiledModule(name, code, outDir){
 
 function compile(sources, handleErrors, outDir){
     var rtlCodeWatcher = new RtlCodeUsingWatcher();
-    var rtl = new Rtl.RTL(rtlCodeWatcher);
+    var rtl = new RTL(rtlCodeWatcher.using.bind(rtlCodeWatcher));
     var moduleCode = function(name, imports){return new ModuleGenerator(name, imports);};
 
     var compiledFilesStack = [];
@@ -78,19 +73,12 @@ function compile(sources, handleErrors, outDir){
             function(e){handleErrors("File \"" + compiledFilesStack[compiledFilesStack.length - 1] + "\", " + e);},
             function(name, code){
                 if (rtlCodeWatcher.used()){
-                    code = "var " + rtl.name() + " = require(\"" + rtl.name() 
-                         + ".js\")." + rtl.name() + ";\n" + code;
+                    code = "var " + rtl.name() + " = require(\"rtl.js\");\n" + code;
                     rtlCodeWatcher.reset();
                 }
                 writeCompiledModule(name, code, outDir);
                 compiledFilesStack.pop();
             });
-
-    var rtlCode = rtl.generate();
-    if (rtlCode){
-        rtlCode += "\nexports." + rtl.name() + " = " + rtl.name() + ";";
-        writeCompiledModule(rtl.name(), rtlCode, outDir);
-    }
 }
 
 exports.compile = compile;

+ 1 - 1
src/oberon.js/JsString.js

@@ -1,4 +1,4 @@
-var RTL$ = require("RTL$.js").RTL$;
+var RTL$ = require("rtl.js");
 var JS = GLOBAL;
 var Type = RTL$.extend({
 	init: function Type(){

+ 1 - 1
src/oberon.js/Lexer.js

@@ -1,4 +1,4 @@
-var RTL$ = require("RTL$.js").RTL$;
+var RTL$ = require("rtl.js");
 var JS = GLOBAL;
 var JsString = require("JsString.js");
 var Stream = require("Stream.js");

+ 0 - 47
src/oberon.js/RTL$.js

@@ -1,47 +0,0 @@
-var RTL$ = {
-    extend: function extend(methods){
-        function Type(){
-            for(var m in methods)
-                this[m] = methods[m];
-        }
-        Type.prototype = this.prototype;
-
-        var result = methods.init;
-        result.prototype = new Type(); // inherit this.prototype
-        result.prototype.constructor = result; // to see constructor name in diagnostic
-        
-        result.extend = extend;
-        return result;
-    },
-    assert: function (condition, code){
-        if (!condition)
-            throw new Error("assertion failed"
-                          + ((code !== undefined) ? " with code " + code : ""));
-    },
-    makeArray: function (/*dimensions, initializer*/){
-        var forward = Array.prototype.slice.call(arguments);
-        var result = new Array(forward.shift());
-        var i;
-        if (forward.length == 1){
-            var init = forward[0];
-            if (typeof init == "function")
-                for(i = 0; i < result.length; ++i)
-                    result[i] = init();
-            else
-                for(i = 0; i < result.length; ++i)
-                    result[i] = init;
-        }
-        else
-            for(i = 0; i < result.length; ++i)
-                result[i] = this.makeArray.apply(this, forward);
-        return result;
-    },
-    strToArray: function (s){
-        var result = new Array(s.length);
-        for(var i = 0; i < s.length; ++i)
-            result[i] = s.charCodeAt(i);
-        return result;
-    }
-};
-
-exports.RTL$ = RTL$;

+ 1 - 1
src/oberon.js/Stream.js

@@ -1,4 +1,4 @@
-var RTL$ = require("RTL$.js").RTL$;
+var RTL$ = require("rtl.js");
 var JsString = require("JsString.js");
 var Type = RTL$.extend({
 	init: function Type(){

+ 2 - 4
src/oc.js

@@ -1,16 +1,14 @@
 "use strict";
 
+var Class = require("rtl.js").Class;
 var Code = require("code.js");
 var Context = require("context.js");
 var Errors = require("errors.js");
 var Grammar = require("grammar.js");
 var Lexer = require("oberon.js/lexer.js");
-var ImportRTL = require("rtl.js");
+var RTL = require("rtl_code.js").RTL;
 var Stream = require("oberon.js/Stream.js");
 
-var RTL = ImportRTL.RTL;
-var Class = ImportRTL.Class;
-
 var CompiledModule = Class.extend({
     init: function CompiledModule(symbol, code, exports){
         this.__symbol = symbol;

+ 2 - 2
src/parser.js

@@ -4,10 +4,10 @@ var assert = require("assert.js").ok;
 var Errors = require("errors.js");
 var Lexer = require("oberon.js/lexer.js");
 var Stream = require("oberon.js/Stream.js");
-var RTL$ = require("oberon.js/RTL$.js").RTL$;
+var Rtl = require("rtl.js");
 
 function literal(s){
-	var l = Lexer.makeLiteral(RTL$.strToArray(s));
+	var l = Lexer.makeLiteral(Rtl.strToArray(s));
 	return function(stream, context){
 		return Lexer.literal(l, stream, context);
 	};

+ 2 - 84
src/rtl.js

@@ -1,24 +1,5 @@
 "use strict";
 
-// support IE8
-if (!Array.prototype.indexOf)
-    Array.prototype.indexOf = function(x){
-        for(var i = 0; i < this.length; ++i)
-            if (this[i] === x)
-                return i;
-        return -1;
-    };
-
-// support Function.bind
-if (!Function.prototype.bind)
-    Function.prototype.bind = function(thisArg){
-        var self = this;
-        var bindArgs = Array.prototype.slice.call(arguments, 1);
-        return function(){
-            return self.apply(thisArg, bindArgs.concat(Array.prototype.slice.call(arguments)));
-        };
-    };
-
 function Class(){}
 Class.extend = function extend(methods){
         function Type(){
@@ -134,69 +115,6 @@ var impl = {
     }
 };
 
-var Code = Class.extend({
-    init: function RTL$Code(){
-        var names = [];
-        for(var f in impl)
-            names.push(f);
-        this.__functions = names;
-    },
-    functions: function(){return this.__functions;},
-    get: function(func){return impl[func];}
-});
-
-var defaultCode = new Code();
-
 exports.Class = Class;
-exports.RTL = Class.extend({
-    init: function RTL(code){
-        this.__entries = {};
-        this.__code = code || defaultCode;        
-        var names = this.__code.functions();
-        for(var i = 0; i < names.length; ++i){
-            var name = names[i];
-            this[name] = this.__makeOnDemand(name);
-            this[name + "Id"] = this.__makeIdOnDemand(name);
-        }
-    },
-    name: function(){return "RTL$";},
-    generate: function(){
-        var result = "var " + this.name() + " = {\n";
-        var firstEntry = true;
-        for (var name in this.__entries){
-            if (!firstEntry)
-                result += ",\n";
-            else
-                firstEntry = false;
-            result += "    " + name + ": " + this.__entries[name].toString();
-        }
-        if (!firstEntry)
-            result += "\n};\n";
-        else
-            result = undefined;
-        
-        return result;
-    },
-    __makeIdOnDemand: function(name){
-        return function(){
-            if (!this.__entries[name])
-                this.__entries[name] = this.__code.get(name);
-            return this.name() + "." + name;
-        };
-    },
-    __makeOnDemand: function(name){
-        return function(){
-            var result = this[name +"Id"]() + "(";
-            if (arguments.length){
-                result += arguments[0];
-                for(var a = 1; a < arguments.length; ++a)
-                    result += ", " + arguments[a];
-            }
-            result += ")";
-            return result;
-        };
-    }
-
-});
-
-exports.Code = Code;
+for(var e in impl)
+    exports[e] = impl[e];

+ 74 - 0
src/rtl_code.js

@@ -0,0 +1,74 @@
+"use strict";
+
+var Rtl = require("rtl.js");
+
+// support IE8
+if (!Array.prototype.indexOf)
+    Array.prototype.indexOf = function(x){
+        for(var i = 0; i < this.length; ++i)
+            if (this[i] === x)
+                return i;
+        return -1;
+    };
+
+// support Function.bind
+if (!Function.prototype.bind)
+    Function.prototype.bind = function(thisArg){
+        var self = this;
+        var bindArgs = Array.prototype.slice.call(arguments, 1);
+        return function(){
+            return self.apply(thisArg, bindArgs.concat(Array.prototype.slice.call(arguments)));
+        };
+    };
+
+exports.RTL = Rtl.Class.extend({
+    init: function RTL(demandedCallback){
+        this.__entries = {};
+        this.__demandedCallback = demandedCallback;
+
+        for(var name in Rtl){
+            this[name] = this.__makeOnDemand(name);
+            this[name + "Id"] = this.__makeIdOnDemand(name);
+        }
+    },
+    name: function(){return "RTL$";},
+    generate: function(){
+        var result = "var " + this.name() + " = {\n";
+        var firstEntry = true;
+        for (var name in this.__entries){
+            if (!firstEntry)
+                result += ",\n";
+            else
+                firstEntry = false;
+            result += "    " + name + ": " + this.__entries[name].toString();
+        }
+        if (!firstEntry)
+            result += "\n};\n";
+        else
+            result = undefined;
+        
+        return result;
+    },
+    __makeIdOnDemand: function(name){
+        return function(){
+            if (this.__demandedCallback)
+                this.__demandedCallback();
+            if (!this.__entries[name])
+                this.__entries[name] = Rtl[name];
+            return this.name() + "." + name;
+        };
+    },
+    __makeOnDemand: function(name){
+        return function(){
+            var result = this[name +"Id"]() + "(";
+            if (arguments.length){
+                result += arguments[0];
+                for(var a = 1; a < arguments.length; ++a)
+                    result += ", " + arguments[a];
+            }
+            result += ")";
+            return result;
+        };
+    }
+
+});

+ 0 - 45
test/expected/nodejs/modules/RTL$.js

@@ -1,45 +0,0 @@
-var RTL$ = {
-    extend: function extend(methods){
-        function Type(){
-            for(var m in methods)
-                this[m] = methods[m];
-        }
-        Type.prototype = this.prototype;
-
-        var result = methods.init;
-        result.prototype = new Type(); // inherit this.prototype
-        result.prototype.constructor = result; // to see constructor name in diagnostic
-        
-        result.extend = extend;
-        return result;
-    },
-    typeGuard: function (from, to){
-        if (!(from instanceof to))
-            throw new Error("typeguard assertion failed");
-        return from;
-    },
-    makeRef: function (obj, prop){
-        return {set: function(v){ obj[prop] = v; },
-                get: function(){ return obj[prop]; }};
-    },
-    makeArray: function (/*dimensions, initializer*/){
-        var forward = Array.prototype.slice.call(arguments);
-        var result = new Array(forward.shift());
-        var i;
-        if (forward.length == 1){
-            var init = forward[0];
-            if (typeof init == "function")
-                for(i = 0; i < result.length; ++i)
-                    result[i] = init();
-            else
-                for(i = 0; i < result.length; ++i)
-                    result[i] = init;
-        }
-        else
-            for(i = 0; i < result.length; ++i)
-                result[i] = this.makeArray.apply(this, forward);
-        return result;
-    }
-};
-
-exports.RTL$ = RTL$;

+ 1 - 1
test/expected/nodejs/modules/m1.js

@@ -1,4 +1,4 @@
-var RTL$ = require("RTL$.js").RTL$;
+var RTL$ = require("rtl.js");
 var ci = 123;
 var Base = RTL$.extend({
 	init: function Base(){

+ 1 - 1
test/expected/nodejs/modules/m2.js

@@ -1,4 +1,4 @@
-var RTL$ = require("RTL$.js").RTL$;
+var RTL$ = require("rtl.js");
 var m1 = require("m1.js");
 var r = new m1.T();
 var pb = null;

+ 1 - 1
test/expected/nodejs/modules/m3.js

@@ -1,4 +1,4 @@
-var RTL$ = require("RTL$.js").RTL$;
+var RTL$ = require("rtl.js");
 var m1 = require("m2.js");
 var m2 = require("m1.js");
 var r = new m2.T();

+ 2 - 3
test/test_unit.js

@@ -1,19 +1,18 @@
 "use strict";
 
 var assert = require("assert.js").ok;
+var Class = require("rtl.js").Class;
 var Code = require("code.js");
 var Context = require("context.js");
 var Errors = require("errors.js");
 var Grammar = require("grammar.js");
 var oc = require("oc.js");
-var ImportRTL = require("rtl.js");
+var RTL = require("rtl_code.js").RTL;
 var Scope = require("scope.js");
 var Stream = require("Stream.js");
 var Test = require("test.js");
 
 var TestError = Test.TestError;
-var RTL = ImportRTL.RTL;
-var Class = ImportRTL.Class;
 
 function parseInContext(grammar, s, context){
     var stream = Stream.make(s);