2
0
Эх сурвалжийг харах

import special JS identifiers ('constructor' nad 'prototype')

Vladislav Folts 10 жил өмнө
parent
commit
995d792b70

BIN
bin/compiled.zip


+ 1 - 1
src/context.js

@@ -261,7 +261,7 @@ exports.QualifiedIdentificator = ChainedContext.extend({
         return undefined;
     },
     endParse: function(){
-        var code = this.__code + this.__id;
+        var code = this.__code ? this.__code + Type.mangleJSProperty(this.__id) : this.__id;
         this.parent().handleQIdent({
             module: this.__module,
             id: this.__id,

+ 1 - 1
src/eberon/EberonConstructor.ob

@@ -59,7 +59,7 @@ PROCEDURE makeCallGenerator(
 BEGIN
     Procedure.initStdCall(call);
     call.recordType := type; 
-    cons <- EberonRecord.actualConstructor(type^);
+    cons <- EberonRecord.constructor(type^);
     IF cons # NIL THEN
         call.args := cons.args();
     END;

+ 2 - 2
src/eberon/EberonRecord.ob

@@ -479,11 +479,11 @@ BEGIN
     RETURN result;
 END;
 
-PROCEDURE actualConstructor*(r: Record): Types.PDefinedProcedure;
+PROCEDURE constructor*(r: Record): Types.PDefinedProcedure;
 BEGIN
     result <- r.customConstructor;
     IF (result = NIL) & (r.base # NIL) THEN
-        result := actualConstructor(r.base(PRecord)^);
+        result := constructor(r.base(PRecord)^);
     END;
     RETURN result;
 END;

+ 2 - 2
src/eberon/eberon_context.js

@@ -428,7 +428,7 @@ var RecordDecl = Context.RecordDecl.extend({
         return EberonRecord.makeRecordField(field, type, this.__type);
     },
     _generateBaseConstructorCallCode: function(){
-        var actualConstructor = EberonRecord.actualConstructor(this.type());
+        var actualConstructor = EberonRecord.constructor$(this.type());
         if (!actualConstructor || !actualConstructor.args().length)
             return Context.RecordDecl.prototype._generateBaseConstructorCallCode.call(this);
         
@@ -627,7 +627,7 @@ var ProcOrMethodDecl = Context.ProcDecl.extend({
                 this.__boundType.defineConstructor(this.__methodType.procType());
 
                 var base = Type.recordBase(this.__boundType);
-                var baseConstructor = base && EberonRecord.actualConstructor(base);
+                var baseConstructor = base && EberonRecord.constructor$(base);
                 if (!this.__baseConstructorWasCalled && baseConstructor && baseConstructor.args().length)
                     throw new Errors.Error("base record constructor has parameters but was not called (use '| SUPER' to pass parameters to base constructor)");
                 if (this.__baseConstructorWasCalled && (!baseConstructor || !baseConstructor.args().length))

+ 2 - 3
src/nodejs.js

@@ -5,6 +5,7 @@ var Code = require("js/Code.js");
 var Context = require("context.js");
 var oc = require("oc.js");
 var RTL = require("rtl_code.js").RTL;
+var Type = require("js/Types.js");
 
 var fs = require("fs");
 var path = require("path");
@@ -34,9 +35,7 @@ var ModuleGenerator = Class.extend({
             var e = exports[access];
             var code = Code.genExport(e);
             if (code){
-                var id = e.id();
-                if (id == "constructor" || id == "prototype")
-                    id += "$";
+                var id = Type.mangleJSProperty(e.id());
                 result += "exports." + id + " = " + code + ";\n";
             }
         }

+ 10 - 3
src/ob/Types.ob

@@ -822,15 +822,22 @@ BEGIN
     RETURN result;
 END;
 
+PROCEDURE mangleJSProperty*(id: STRING): STRING;
+BEGIN
+    result <- id;
+    IF (id = "constructor") OR (id = "prototype") THEN
+        result := result + "$";
+    END;
+    RETURN result;
+END;
+
 PROCEDURE mangleField*(id: STRING; type: PType): STRING;
 BEGIN
     result <- id;
     IF isScalar(type^) 
         OR ((type IS PArray)
             & isScalar(arrayBaseElementsType(type^)^)) THEN
-        IF (id = "constructor") OR (id = "prototype") THEN
-            result := result + "$";
-        END;
+        result := mangleJSProperty(result);
     ELSE
         result := "$" + result;
     END;

+ 8 - 0
test/expected/nodejs/modules/m1.js

@@ -24,6 +24,12 @@ function makeTPA(){
 	result = new TPA();
 	return result;
 }
+
+function constructor(){
+}
+
+function prototype(){
+}
 pr = new anonymous$1();
 exports.ci = ci;
 exports.Base = Base;
@@ -34,3 +40,5 @@ exports.pr = function(){return pr;};
 exports.pr2 = function(){return pr2;};
 exports.p = p;
 exports.makeTPA = makeTPA;
+exports.constructor$ = constructor;
+exports.prototype$ = prototype;

+ 2 - 0
test/expected/nodejs/modules/m2.js

@@ -18,3 +18,5 @@ m1.p();
 p(m1.i());
 p(m1.ci);
 ref(RTL$.makeRef(m1.pr2(), "i"));
+m1.constructor$();
+m1.prototype$();

+ 9 - 0
test/input/nodejs/modules.ob

@@ -21,6 +21,12 @@ BEGIN
     RETURN result
 END makeTPA;
 
+PROCEDURE constructor*();
+END constructor;
+
+PROCEDURE prototype*();
+END prototype;
+
 BEGIN
     NEW(pr);
 END m1.
@@ -51,6 +57,9 @@ BEGIN
     p(m1.i);
     p(m1.ci);
     ref(m1.pr2.i);
+
+    m1.constructor();
+    m1.prototype();
 END m2.
 
 MODULE m3;