瀏覽代碼

pas2js: started delayed specialize

git-svn-id: trunk@45640 -
Mattias Gaertner 5 年之前
父節點
當前提交
f26afe4a6a
共有 3 個文件被更改,包括 68 次插入14 次删除
  1. 51 3
      packages/pastojs/tests/tcgenerics.pas
  2. 1 0
      packages/pastojs/tests/tcoptimizations.pas
  3. 16 11
      utils/pas2js/dist/rtl.js

+ 51 - 3
packages/pastojs/tests/tcgenerics.pas

@@ -17,7 +17,7 @@ type
     // generic record
     Procedure TestGen_RecordEmpty;
     Procedure TestGen_Record_ClassProc;
-    //Procedure TestGen_Record_ReferGenClass_DelphiFail;
+    Procedure TestGen_Record_DelayProgram; // ToDo
 
     // generic class
     Procedure TestGen_ClassEmpty;
@@ -29,7 +29,7 @@ type
     Procedure TestGen_Class_TypeOverload; // ToDo TBird, TBird<T>, TBird<S,T>
     Procedure TestGen_Class_ClassProperty;
     Procedure TestGen_Class_ClassProc;
-    //Procedure TestGen_Class_ReferGenClass_DelphiFail;
+    //Procedure TestGen_Record_ReferGenClass_DelphiFail; TBird<T> = class x:TBird; end;
     Procedure TestGen_Class_ClassConstructor;
     Procedure TestGen_Class_TypeCastSpecializesWarn;
     Procedure TestGen_Class_TypeCastSpecializesJSValueNoWarn;
@@ -41,7 +41,7 @@ type
     procedure TestGen_ExtClass_Array;
     procedure TestGen_ExtClass_GenJSValueAssign;
     procedure TestGen_ExtClass_AliasMemberType;
-    Procedure TestGen_ExtClass_RTTI;
+    Procedure TestGen_ExtClass_RTTI; // ToDo: use "TGJSSET<JSValue>"
 
     // class interfaces
     procedure TestGen_ClassInterface_Corba;
@@ -154,6 +154,54 @@ begin
     '']));
 end;
 
+procedure TTestGenerics.TestGen_Record_DelayProgram;
+begin
+  exit;
+
+  StartProgram(false);
+  Add([
+  '{$modeswitch AdvancedRecords}',
+  'type',
+  '  generic TAnt<T> = record',
+  '    class var x: T;',
+  '  end;',
+  '  TBird = record',
+  '    b: word;',
+  '  end;',
+  'var f: specialize TAnt<TBird>;',
+  'begin',
+  '  f.x.b:=f.x.b+10;',
+  '']);
+  ConvertProgram;
+  CheckSource('TestGen_Record_DelayProgram',
+    LinesToStr([ // statements
+    'rtl.recNewS($mod, "TAnt$G1", function () {',
+    '  this.x = $mod.TBird.$new();',
+    '  this.$eq = function (b) {',
+    '    return true;',
+    '  };',
+    '  this.$assign = function (s) {',
+    '    return this;',
+    '  };',
+    '}, true);',
+    'rtl.recNewT($mod, "TBird", function () {',
+    '  this.b = 0;',
+    '  this.$eq = function (b) {',
+    '    return this.b === b.b;',
+    '  };',
+    '  this.$assign = function (s) {',
+    '    this.b = s.b;',
+    '    return this;',
+    '  };',
+    '});',
+    '$mod.TAnt$G1();',
+    'this.f = $mod.TAnt$G1.$new();',
+    '']),
+    LinesToStr([ // $mod.$main
+    '$mod.f.x.b = $mod.f.x.b + 10;',
+    '']));
+end;
+
 procedure TTestGenerics.TestGen_ClassEmpty;
 begin
   StartProgram(false);

+ 1 - 0
packages/pastojs/tests/tcoptimizations.pas

@@ -59,6 +59,7 @@ type
     // unit optimization: aliasglobals
     procedure TestOptAliasGlobals_Program;
     procedure TestOptAliasGlobals_Unit; // ToDo
+    // ToDo: external var, const, class
     // ToDo: RTTI
     // ToDo: typeinfo(var), typeinfo(type)
     // ToDo: resourcestring

+ 16 - 11
utils/pas2js/dist/rtl.js

@@ -432,29 +432,34 @@ var rtl = {
     // create new record type
     var t = {};
     if (parent) parent[name] = t;
-    function hide(prop){
-      Object.defineProperty(t,prop,{enumerable:false});
-    }
+    var h = rtl.hideProp;
     if (full){
       rtl.initStruct(t,parent,name);
       t.$record = t;
-      hide('$record');
-      hide('$name');
-      hide('$parent');
-      hide('$module');
+      h(t,'$record');
+      h(t,'$name');
+      h(t,'$parent');
+      h(t,'$module');
     }
     initfn.call(t);
     if (!t.$new){
       t.$new = function(){ return Object.create(t); };
     }
     t.$clone = function(r){ return t.$new().$assign(r); };
-    hide('$new');
-    hide('$clone');
-    hide('$eq');
-    hide('$assign');
+    h(t,'$new');
+    h(t,'$clone');
+    h(t,'$eq');
+    h(t,'$assign');
     return t;
   },
 
+  recNewS: function(parent,name,initfn,full){
+    // register specialized record type
+    parent[name] = function(){
+      rtl.recNewT(parent,name,initfn,full);
+    }
+  },
+
   is: function(instance,type){
     return type.isPrototypeOf(instance) || (instance===type);
   },