Ver código fonte

pastojs: test generic external class

git-svn-id: trunk@42664 -
Mattias Gaertner 6 anos atrás
pai
commit
bbc542881f

+ 2 - 0
packages/pastojs/src/fppas2js.pp

@@ -12837,6 +12837,8 @@ begin
     Result:=ConvertProcedureType(TPasProcedureType(El),GlobalCtx)
   else if (C=TPasArrayType) then
     Result:=ConvertArrayType(TPasArrayType(El),GlobalCtx)
+  else if (C=TPasSpecializeType) then
+    // specialize type is converted at the generic type
   else
     begin
     {$IFDEF VerbosePas2JS}

+ 65 - 0
packages/pastojs/tests/tcgenerics.pas

@@ -14,9 +14,15 @@ type
 
   TTestGenerics = class(TCustomTestModule)
   Published
+    // generic record
     Procedure TestGeneric_RecordEmpty;
+
+    // generic class
     Procedure TestGeneric_ClassEmpty;
     Procedure TestGeneric_Class_EmptyMethod;
+
+    // generic external class
+    procedure TestGen_ExtClass_Array;
   end;
 
 implementation
@@ -119,6 +125,65 @@ begin
     ]));
 end;
 
+procedure TTestGenerics.TestGen_ExtClass_Array;
+begin
+  StartProgram(false);
+  Add([
+  '{$mode delphi}',
+  '{$ModeSwitch externalclass}',
+  'type',
+  '  NativeInt = longint;',
+  '  TJSGenArray<T> = Class external name ''Array''',
+  '  private',
+  '    function GetElements(Index: NativeInt): T; external name ''[]'';',
+  '    procedure SetElements(Index: NativeInt; const AValue: T); external name ''[]'';',
+  '  public',
+  '    type TSelfType = TJSGenArray<T>;',
+  '  public',
+  '    FLength : NativeInt; external name ''length'';',
+  '    constructor new; overload;',
+  '    constructor new(aLength : NativeInt); overload;',
+  '    class function _of() : TSelfType; varargs; external name ''of'';',
+  '    function fill(aValue : T) : TSelfType; overload;',
+  '    function fill(aValue : T; aStartIndex : NativeInt) : TSelfType; overload;',
+  '    function fill(aValue : T; aStartIndex,aEndIndex : NativeInt) : TSelfType; overload;',
+  '    property Length : NativeInt Read FLength Write FLength;',
+  '    property Elements[Index: NativeInt]: T read GetElements write SetElements; default;',
+  '  end;',
+  '  TJSWordArray = TJSGenArray<word>;',
+  'var',
+  '  wa: TJSWordArray;',
+  '  w: word;',
+  'begin',
+  '  wa:=TJSWordArray.new;',
+  '  wa:=TJSWordArray.new(3);',
+  '  wa:=TJSWordArray._of(4,5);',
+  '  wa:=wa.fill(7);',
+  '  wa:=wa.fill(7,8,9);',
+  '  w:=wa.length;',
+  '  wa.length:=10;',
+  '  wa[11]:=w;',
+  '  w:=wa[12];',
+  '']);
+  ConvertProgram;
+  CheckSource('TestGen_ExtClass_Array',
+    LinesToStr([ // statements
+    'this.wa = null;',
+    'this.w = 0;',
+    '']),
+    LinesToStr([ // $mod.$main
+    '$mod.wa = new Array();',
+    '$mod.wa = new Array(3);',
+    '$mod.wa = Array.of(4, 5);',
+    '$mod.wa = $mod.wa.fill(7);',
+    '$mod.wa = $mod.wa.fill(7, 8, 9);',
+    '$mod.w = $mod.wa.length;',
+    '$mod.wa.length = 10;',
+    '$mod.wa[11] = $mod.w;',
+    '$mod.w = $mod.wa[12];',
+    '']));
+end;
+
 Initialization
   RegisterTests([TTestGenerics]);
 end.