Browse Source

pastojs: fixed removing leading zeroes from hex numbers

git-svn-id: trunk@39010 -
Mattias Gaertner 7 years ago
parent
commit
0fcec04382
2 changed files with 29 additions and 10 deletions
  1. 26 9
      packages/pastojs/src/fppas2js.pp
  2. 3 1
      packages/pastojs/tests/tcmodules.pas

+ 26 - 9
packages/pastojs/src/fppas2js.pp

@@ -6277,19 +6277,35 @@ function TPasToJSConverter.ConvertPrimitiveExpression(El: TPrimitiveExpr;
 
 
   function DeleteLeadingZeroes(const s: string): string;
   function DeleteLeadingZeroes(const s: string): string;
   // Note: 01 is in JS octal, and in strict mode forbidden
   // Note: 01 is in JS octal, and in strict mode forbidden
+  // $00ff00 -> $ff00
+  // 00E001 -> 0E1
+  // 0.001 -> 0.001
+  // 0.00E1 -> 0.00E1
   var
   var
     i: Integer;
     i: Integer;
   begin
   begin
     Result:=s;
     Result:=s;
     i:=1;
     i:=1;
-    while i<length(Result) do
-      begin
-      if (Result[i]='0') and (Result[i+1] in ['0'..'9'])
-          and ((i=1) or not (Result[i-1] in ['.','0'..'9'])) then
-        Delete(Result,i,1)
-      else
-        inc(i);
-      end;
+    if Result[1]='$' then
+      // hexadecimal -> can not be a float, 'E' is a hexdigit
+      while i<length(Result) do
+        begin
+        if (Result[i]='0') and (Result[i+1] in ['0'..'9','A'..'F','a'..'f'])
+            and ((i=1) or not (Result[i-1] in ['0'..'9','A'..'F','a'..'f'])) then
+          Delete(Result,i,1)
+        else
+          inc(i);
+        end
+    else
+      // decimal, can be a float, 'E' is a start of a new number
+      while i<length(Result) do
+        begin
+        if (Result[i]='0') and (Result[i+1] in ['0'..'9'])
+            and ((i=1) or not (Result[i-1] in ['.','0'..'9'])) then
+          Delete(Result,i,1)
+        else
+          inc(i);
+        end;
   end;
   end;
 
 
 Var
 Var
@@ -6338,7 +6354,8 @@ begin
             // number was rounded -> we lost precision
             // number was rounded -> we lost precision
             DoError(20161024230812,nInvalidNumber,sInvalidNumber,[El.Value],El);
             DoError(20161024230812,nInvalidNumber,sInvalidNumber,[El.Value],El);
           L:=CreateLiteralNumber(El,Number);
           L:=CreateLiteralNumber(El,Number);
-          S:=DeleteLeadingZeroes(copy(El.Value,2,length(El.Value)));
+          S:=DeleteLeadingZeroes(El.Value);
+          S:=copy(S,2,length(S));
           case El.Value[1] of
           case El.Value[1] of
           '$': S:='0x'+S;
           '$': S:='0x'+S;
           '&': if TargetProcessor=ProcessorECMAScript5 then
           '&': if TargetProcessor=ProcessorECMAScript5 then

+ 3 - 1
packages/pastojs/tests/tcmodules.pas

@@ -2154,6 +2154,7 @@ begin
   Add('  i8: byte = 00;');
   Add('  i8: byte = 00;');
   Add('  u8: nativeuint =  $fffffffffffff;');
   Add('  u8: nativeuint =  $fffffffffffff;');
   Add('  u9: nativeuint =  $0000000000000;');
   Add('  u9: nativeuint =  $0000000000000;');
+  Add('  u10: nativeuint = $00ff00;');
   Add('begin');
   Add('begin');
   ConvertProgram;
   ConvertProgram;
   CheckSource('TestVarBaseTypes',
   CheckSource('TestVarBaseTypes',
@@ -2175,7 +2176,8 @@ begin
     'this.i7 =-0x10000000000000;',
     'this.i7 =-0x10000000000000;',
     'this.i8 = 0;',
     'this.i8 = 0;',
     'this.u8 = 0xfffffffffffff;',
     'this.u8 = 0xfffffffffffff;',
-    'this.u9 = 0x0;'
+    'this.u9 = 0x0;',
+    'this.u10 = 0xff00;'
     ]),
     ]),
     '');
     '');
 end;
 end;