Procházet zdrojové kódy

pastojs: fixed initializing static array of record

git-svn-id: trunk@40127 -
Mattias Gaertner před 6 roky
rodič
revize
057534ef47

+ 7 - 1
packages/pastojs/src/fppas2js.pp

@@ -14075,7 +14075,13 @@ begin
         end;
 
       // add parameter defaultvalue
-      DefaultValue:=CreateValInit(ElTypeResolved.LoTypeEl,nil,El,AContext);
+      if ElTypeResolved.LoTypeEl is TPasRecordType then
+        begin
+        // array of record -> push the type reference
+        DefaultValue:=CreateReferencePathExpr(ElTypeResolved.LoTypeEl,AContext);
+        end
+      else
+        DefaultValue:=CreateValInit(ElTypeResolved.LoTypeEl,nil,El,AContext);
       Call.AddArg(DefaultValue);
 
       // add parameters dim1,dim2,...

+ 39 - 0
packages/pastojs/tests/tcmodules.pas

@@ -389,6 +389,7 @@ type
     Procedure TestArray_StaticChar;
     Procedure TestArray_StaticMultiDim;
     Procedure TestArrayOfRecord;
+    Procedure TestArray_StaticRecord;
     Procedure TestArrayOfSet;
     // call(set)  literal and clone var
     // call([set])   literal and clone var
@@ -7322,6 +7323,44 @@ begin
     '']));
 end;
 
+procedure TTestModule.TestArray_StaticRecord;
+begin
+  StartProgram(false);
+  Add([
+  'type',
+  '  TRec = record',
+  '    Int: longint;',
+  '  end;',
+  '  TArrayRec = array[1..2] of TRec;',
+  'var',
+  '  Arr: TArrayRec;',
+  'begin',
+  '  arr[1].int:=length(arr)+low(arr)+high(arr);',
+  '']);
+  ConvertProgram;
+  CheckSource('TestArray_StaticRecord',
+    LinesToStr([ // statements
+    'this.TRec = function (s) {',
+    '  if (s) {',
+    '    this.Int = s.Int;',
+    '  } else {',
+    '    this.Int = 0;',
+    '  };',
+    '  this.$equal = function (b) {',
+    '    return this.Int === b.Int;',
+    '  };',
+    '};',
+    'this.TArrayRec$clone = function (a) {',
+    '  var r = [];',
+    '  for (var i = 0; i < 2; i++) r.push(new $mod.TRec(a[i]));',
+    '  return r;',
+    '};',
+    'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
+    '']),
+    LinesToStr([ // $mod.$main
+    '$mod.Arr[0].Int = (2 + 1) + 2;']));
+end;
+
 procedure TTestModule.TestArrayOfSet;
 begin
   StartProgram(false);