Răsfoiți Sursa

fix: non-exported fields were visible on import

Vladislav Folts 11 ani în urmă
părinte
comite
0faba7f60b
2 a modificat fișierele cu 15 adăugiri și 0 ștergeri
  1. 9 0
      src/type.js
  2. 6 0
      test/test_unit.js

+ 9 - 0
src/type.js

@@ -117,7 +117,9 @@ var Record = BasicType.extend({
         this.__cons = cons;        
         this.__scope = scope;
         this.__fields = {};
+        this.__notExported = [];
         this.__base = undefined;
+        scope.addFinalizer(this.finalize.bind(this));
     },
     initializer: function(context){
         return "new " + context.qualifyScope(this.__scope) + this.__cons + "()";
@@ -131,6 +133,8 @@ var Record = BasicType.extend({
         if (this.__base && this.__base.findSymbol(name))
             throw new Errors.Error("base record already has field: '" + name + "'");
         this.__fields[name] = type;
+        if (!field.exported())
+            this.__notExported.push(name);
     },
     ownFields: function() {return this.__fields;},
     findSymbol: function(field){
@@ -143,6 +147,11 @@ var Record = BasicType.extend({
     setBaseType: function(type) {this.__base = type;},
     description: function(){
         return this.name() || "anonymous RECORD";
+    },
+    finalize: function(){
+        for(var i = 0; i < this.__notExported.length; ++i)
+            delete this.__fields[this.__notExported[i]];
+        delete this.__notExported;
     }
 });
 

+ 6 - 0
test/test_unit.js

@@ -880,6 +880,12 @@ return {
          ["MODULE m; IMPORT t := test; BEGIN t.p(); END m.",
           "identifier 'p' is not exported by module 'test'"]
         )),
+"import record type": testWithModule(
+    "MODULE test; TYPE T* = RECORD f*: INTEGER; notExported: BOOLEAN END; END test.",
+    pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 0; END m."),
+    fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.notExported := FALSE; END m.",
+          "type 'T' has no 'notExported' field"]
+        )),
 "imported variables are read-only": testWithModule(
     "MODULE test; VAR i*: INTEGER; END test.",
     pass("MODULE m; IMPORT test; PROCEDURE p(i: INTEGER); END p; BEGIN p(test.i); END m."),