Pārlūkot izejas kodu

pas2js: type helper for wordbool

git-svn-id: trunk@45295 -
Mattias Gaertner 5 gadi atpakaļ
vecāks
revīzija
da799883dd
2 mainītis faili ar 102 papildinājumiem un 1 dzēšanām
  1. 1 1
      packages/pastojs/src/fppas2js.pp
  2. 101 0
      packages/pastojs/tests/tcmodules.pas

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

@@ -1265,7 +1265,7 @@ const
   btAllJSChars = [btChar,btWideChar];
   btAllJSStringAndChars = btAllJSStrings+btAllJSChars;
   btAllJSFloats = [btDouble];
-  btAllJSBooleans = [btBoolean];
+  btAllJSBooleans = [btBoolean,btByteBool,btWordBool,btLongBool];
   btAllJSInteger = [btByte,btShortInt,btWord,btSmallInt,btLongWord,btLongint,
     btIntDouble,btUIntDouble,
     btCurrency  // in pas2js currency is more like an integer, instead of float

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

@@ -703,6 +703,8 @@ type
     Procedure TestTypeHelper_ExtClassMethodFail;
     Procedure TestTypeHelper_Constructor;
     Procedure TestTypeHelper_Word;
+    Procedure TestTypeHelper_Boolean;
+    Procedure TestTypeHelper_WordBool;
     Procedure TestTypeHelper_Double;
     Procedure TestTypeHelper_NativeInt;
     Procedure TestTypeHelper_StringChar;
@@ -24127,6 +24129,105 @@ begin
     '']));
 end;
 
+procedure TTestModule.TestTypeHelper_Boolean;
+begin
+  StartProgram(false);
+  Add([
+  '{$modeswitch typehelpers}',
+  'type',
+  '  Integer = longint;',
+  '  THelper = type helper for boolean',
+  '    procedure Run(e: wordbool = true);',
+  '  end;',
+  'procedure THelper.Run(e: wordbool);',
+  'begin',
+  '  Self:=e;',
+  '  Self:=not Self;',
+  '  with Self do Run;',
+  '  if Integer(Self)=0 then ;',
+  'end;',
+  'begin',
+  '  boolean(3).Run;',
+  '']);
+  ConvertProgram;
+  CheckSource('TestTypeHelper_Boolean',
+    LinesToStr([ // statements
+    'rtl.createHelper($mod, "THelper", null, function () {',
+    '  this.Run = function (e) {',
+    '    this.set(e);',
+    '    this.set(!this.get());',
+    '    var $with1 = this.get();',
+    '    $mod.THelper.Run.call(this, true);',
+    '    if ((this.get() ? 1 : 0) === 0) ;',
+    '  };',
+    '});',
+    '']),
+    LinesToStr([ // $mod.$main
+    '$mod.THelper.Run.call({',
+    '  a: 3 != 0,',
+    '  get: function () {',
+    '      return this.a;',
+    '    },',
+    '  set: function (v) {',
+    '      rtl.raiseE("EPropReadOnly");',
+    '    }',
+    '}, true);',
+    '']));
+end;
+
+procedure TTestModule.TestTypeHelper_WordBool;
+begin
+  StartProgram(false);
+  Add([
+  '{$modeswitch typehelpers}',
+  'type',
+  '  Integer = longint;',
+  '  THelper = type helper for WordBool',
+  '    procedure Run(e: wordbool = true);',
+  '  end;',
+  'procedure THelper.Run(e: wordbool);',
+  'var i: integer;',
+  'begin',
+  '  i:=Integer(Self);',
+  'end;',
+  'var w: wordbool;',
+  'begin',
+  '  w.Run;',
+  '  wordbool(3).Run;',
+  '']);
+  ConvertProgram;
+  CheckSource('TestTypeHelper_WordBool',
+    LinesToStr([ // statements
+    'rtl.createHelper($mod, "THelper", null, function () {',
+    '  this.Run = function (e) {',
+    '    var i = 0;',
+    '    i = (this.get() ? 1 : 0);',
+    '  };',
+    '});',
+    'this.w = false;',
+    '']),
+    LinesToStr([ // $mod.$main
+    '$mod.THelper.Run.call({',
+    '  p: $mod,',
+    '  get: function () {',
+    '      return this.p.w;',
+    '    },',
+    '  set: function (v) {',
+    '      this.p.w = v;',
+    '    }',
+    '}, true);',
+    '$mod.THelper.Run.call({',
+    '  a: 3 != 0,',
+    '  get: function () {',
+    '      return this.a;',
+    '    },',
+    '  set: function (v) {',
+    '      rtl.raiseE("EPropReadOnly");',
+    '    }',
+    '}, true);',
+    '']));
+end;
+
 procedure TTestModule.TestTypeHelper_Double;
 begin
   StartProgram(false);