瀏覽代碼

Fix export pointer types.

Vladislav Folts 10 年之前
父節點
當前提交
653c94f019

二進制
bin/compiled.zip


+ 1 - 2
src/nodejs.js

@@ -5,7 +5,6 @@ var Code = require("js/Code.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var oc = require("oc.js");
 var makeRTL = require("rtl_code.js").makeRTL;
-var Record = require("js/Record.js");
 
 var fs = require("fs");
 var path = require("path");
@@ -35,7 +34,7 @@ var ModuleGenerator = Class.extend({
             var e = exports[access];
             var code = Code.genExport(e);
             if (code){
-                var id = Record.mangleJSProperty(e.id());
+                var id = Code.exportId(e);
                 result += "exports." + id + " = " + code + ";\n";
             }
         }

+ 21 - 1
src/ob/Code.ob

@@ -100,6 +100,26 @@ PROCEDURE ModuleGenerator.prolog(): STRING;
     RETURN "var " + SELF.name + " = function (" + genCommaList(SELF.imports, TRUE) + "){" + Stream.kCR
 END;
 
+PROCEDURE exportId*(s: Symbols.Symbol): STRING;
+VAR
+    result: STRING;
+BEGIN
+    info <- s.info();
+    IF info IS TypeId.PType THEN
+        type <- info.type();
+        IF type IS Record.PPointer THEN
+            name <- Record.pointerBase(type^).cons;
+            IF LEN(name) # 0 THEN
+                result := name;
+            END;
+        END;
+    END;
+    IF LEN(result) = 0 THEN
+        result := s.id();
+    END;
+    RETURN Record.mangleJSProperty(result);
+END;
+
 PROCEDURE ModuleGenerator.epilog(exports: Symbols.Map): STRING;
 VAR
     result: STRING;
@@ -110,7 +130,7 @@ BEGIN
             IF LEN(result) # 0 THEN
                 result := result + "," + Stream.kCR;
             END;
-            result := result + CodeGenerator.kTab + s.id() + ": " + code;
+            result := result + CodeGenerator.kTab + exportId(s^) + ": " + code;
         END;
     END;
 

+ 7 - 3
src/ob/ContextExpression.ob

@@ -386,12 +386,16 @@ BEGIN
 END;
 
 PROCEDURE castCode*(type: Types.PType; cx: Context.Type): STRING;
+VAR
+    result: STRING;
 BEGIN
-    baseType <- type;
     IF type IS Record.PPointer THEN
-        baseType := Record.pointerBase(type^);
+        baseType <- Record.pointerBase(type^);
+        result := Record.constructor(cx, baseType^);
+    ELSE
+        result := Record.constructor(cx, type(Record.PType)^);
     END;
-    RETURN Record.constructor(cx, baseType(Record.PType)^);
+    RETURN result;
 END;
 
 PROCEDURE RelationOps.is(type: Types.PType; cx: Context.Type): BinaryOperatorCx;

+ 1 - 1
src/ob/Record.ob

@@ -21,7 +21,7 @@ TYPE
         notExported: ARRAY * OF STRING
     END;
 
-    NonExported* = RECORD(Type)
+    NonExported = RECORD(Type)
         PROCEDURE NonExported(cons: STRING; scope: ScopeBase.PType; base: PType);
     END;
     PNonExported* = POINTER TO NonExported;

+ 1 - 1
test/expected/export.js

@@ -14,7 +14,7 @@ function p1(){
 return {
 	ci: ci,
 	T1: T1,
-	PNotExportedRecord: NotExportedRecord,
+	NotExportedRecord: NotExportedRecord,
 	pr1: function(){return pr1;},
 	p2: function(){return p2;},
 	vi: function(){return vi;},

+ 22 - 1
test/expected/modules.js

@@ -14,6 +14,10 @@ function TPB(){
 	Base.call(this);
 }
 RTL$.extend(TPB, Base);
+function ExportPointerOnly(){
+	Base.call(this);
+}
+RTL$.extend(ExportPointerOnly, Base);
 var i = 0;
 function anonymous$1(){
 	this.i = 0;
@@ -35,6 +39,12 @@ function makeTPB(){
 	result = new TPB();
 	return result;
 }
+
+function constructor(){
+}
+
+function prototype(){
+}
 pr = new anonymous$1();
 return {
 	ci: ci,
@@ -42,12 +52,15 @@ return {
 	T: T,
 	TPA: TPA,
 	TPB: TPB,
+	ExportPointerOnly: ExportPointerOnly,
 	i: function(){return i;},
 	pr: function(){return pr;},
 	pr2: function(){return pr2;},
 	p: p,
 	makeTPA: makeTPA,
-	makeTPB: makeTPB
+	makeTPB: makeTPB,
+	constructor$: constructor,
+	prototype$: prototype
 }
 }();
 var m2 = function (m1){
@@ -68,6 +81,14 @@ function p(i/*INTEGER*/){
 
 function ref(i/*VAR INTEGER*/){
 }
+
+function castToImportedPointer(p/*PBase*/){
+	var p2 = null;
+	if (p instanceof m1.ExportPointerOnly){
+		p2 = p;
+		RTL$.assert(RTL$.typeGuard(p2, m1.ExportPointerOnly) != null);
+	}
+}
 ptr = new m1.T();
 pb = ptr;
 RTL$.typeGuard(pb, m1.T).i = 123;

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

@@ -9,6 +9,10 @@ function T(){
 RTL$.extend(T, Base);
 function TPA(){
 }
+function ExportPointerOnly(){
+	Base.call(this);
+}
+RTL$.extend(ExportPointerOnly, Base);
 var i = 0;
 function anonymous$1(){
 	this.i = 0;
@@ -35,6 +39,7 @@ exports.ci = ci;
 exports.Base = Base;
 exports.T = T;
 exports.TPA = TPA;
+exports.ExportPointerOnly = ExportPointerOnly;
 exports.i = function(){return i;};
 exports.pr = function(){return pr;};
 exports.pr2 = function(){return pr2;};

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

@@ -10,6 +10,14 @@ function p(i/*INTEGER*/){
 
 function ref(i/*VAR INTEGER*/){
 }
+
+function castToImportedPointer(p/*PBase*/){
+	var p2 = null;
+	if (p instanceof m1.ExportPointerOnly){
+		p2 = p;
+		RTL$.assert(RTL$.typeGuard(p2, m1.ExportPointerOnly) != null);
+	}
+}
 ptr = new m1.T();
 pb = ptr;
 RTL$.typeGuard(pb, m1.T).i = 123;

+ 21 - 0
test/input/modules.ob

@@ -7,6 +7,10 @@ TYPE
 	TP* = POINTER TO T;
 	TPA* = POINTER TO RECORD END;
     TPB* = POINTER TO RECORD(Base) END;
+
+    ExportPointerOnly = RECORD(Base)
+    END;
+    PExportPointerOnly* = POINTER TO ExportPointerOnly;
 VAR
     i*: INTEGER;
     pr*: POINTER TO RECORD i: INTEGER END;
@@ -29,6 +33,12 @@ BEGIN
     RETURN result
 END makeTPB;
 
+PROCEDURE constructor*();
+END constructor;
+
+PROCEDURE prototype*();
+END prototype;
+
 BEGIN
     NEW(pr);
 END m1.
@@ -38,6 +48,7 @@ IMPORT m1;
 
 TYPE
 	T = RECORD(m1.T) i2: INTEGER END;
+    PBase = POINTER TO m1.Base;
 VAR 
 	r: m1.T;
 	r2: T;
@@ -52,6 +63,16 @@ END p;
 PROCEDURE ref(VAR i: INTEGER);
 END ref;
 
+PROCEDURE castToImportedPointer(p: PBase);
+VAR
+    p2: PBase;
+BEGIN
+    IF p IS m1.PExportPointerOnly THEN
+        p2 := p;
+        ASSERT(p2(m1.PExportPointerOnly) # NIL);
+    END;
+END castToImportedPointer;
+
 BEGIN
 	NEW(ptr);
 	pb := ptr;

+ 16 - 1
test/input/nodejs/modules.ob

@@ -6,6 +6,10 @@ TYPE
 	T* = RECORD(Base) END;
 	TP* = POINTER TO T;
 	TPA* = POINTER TO RECORD END;
+
+    ExportPointerOnly = RECORD(Base)
+    END;
+    PExportPointerOnly* = POINTER TO ExportPointerOnly;
 VAR
     i*: INTEGER;
     pr*: POINTER TO RECORD i: INTEGER END;
@@ -33,7 +37,8 @@ END m1.
 
 MODULE m2;
 IMPORT m1;
-
+TYPE
+    PBase = POINTER TO m1.Base;
 VAR 
 	r: m1.T;
 	pb: POINTER TO m1.Base;
@@ -46,6 +51,16 @@ END p;
 PROCEDURE ref(VAR i: INTEGER);
 END ref;
 
+PROCEDURE castToImportedPointer(p: PBase);
+VAR
+    p2: PBase;
+BEGIN
+    IF p IS m1.PExportPointerOnly THEN
+        p2 := p;
+        ASSERT(p2(m1.PExportPointerOnly) # NIL);
+    END;
+END castToImportedPointer;
+
 BEGIN
 	NEW(ptr);
 	pb := ptr;