pastojs: number literals: remove leading zeroes

git-svn-id: trunk@38900 -
This commit is contained in:
Mattias Gaertner 2018-05-04 09:07:26 +00:00
parent c618822648
commit 50564747b2
2 changed files with 48 additions and 21 deletions

View File

@ -6185,6 +6185,23 @@ end;
function TPasToJSConverter.ConvertPrimitiveExpression(El: TPrimitiveExpr;
AContext: TConvertContext): TJSElement;
function DeleteLeadingZeroes(const s: string): string;
// Note: 01 is in JS octal, and in strict mode forbidden
var
i: Integer;
begin
Result:=s;
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;
end;
Var
L : TJSLiteral;
Number : TJSNumber;
@ -6219,7 +6236,7 @@ begin
if ConversionError<>0 then
DoError(20161024191248,nInvalidNumber,sInvalidNumber,[El.Value],El);
L:=CreateLiteralNumber(El,Number);
L.Value.CustomValue:=TJSString(El.Value);
L.Value.CustomValue:=TJSString(DeleteLeadingZeroes(El.Value));
end;
'$','&','%':
begin
@ -6231,7 +6248,7 @@ begin
// number was rounded -> we lost precision
DoError(20161024230812,nInvalidNumber,sInvalidNumber,[El.Value],El);
L:=CreateLiteralNumber(El,Number);
S:=copy(El.Value,2,length(El.Value));
S:=DeleteLeadingZeroes(copy(El.Value,2,length(El.Value)));
case El.Value[1] of
'$': S:='0x'+S;
'&': if TargetProcessor=ProcessorECMAScript5 then
@ -6239,7 +6256,7 @@ begin
else
S:='0o'+S;
'%': if TargetProcessor=ProcessorECMAScript5 then
S:=''
S:='' // use decimal
else
S:='0b'+S;
end;
@ -16006,7 +16023,7 @@ begin
else if C=TPasRecordType then
Result:=CreateRecordInit(TPasRecordType(T),Expr,El,AContext)
else if Assigned(Expr) then
// if there is an expression then simply convert the it
// if there is an expression then simply convert it
Result:=ConvertElement(Expr,AContext)
else if C=TPasSetType then
// a "set" without initial value

View File

@ -1994,29 +1994,31 @@ begin
Add(' i5: nativeint = -4503599627370496;');
Add(' i6: nativeint = $fffffffffffff;');
Add(' i7: nativeint = -$10000000000000;');
Add(' i8: byte = 00;');
Add(' u8: nativeuint = $fffffffffffff;');
Add(' u9: nativeuint = $0000000000000;');
Add('begin');
ConvertProgram;
CheckSource('TestVarBaseTypes',
LinesToStr([
'this.i=0;',
'this.s="";',
'this.c="";',
'this.b=false;',
'this.d=0.0;',
'this.i2=3;',
'this.s2="foo";',
'this.c2="4";',
'this.b2=true;',
'this.d2=5.6;',
'this.i3=0x707;',
'this.i4= 4503599627370495;',
'this.i5= -4503599627370496;',
'this.i6= 0xfffffffffffff;',
'this.i7=-0x10000000000000;',
'this.u8= 0xfffffffffffff;',
'this.u9= 0x0000000000000;'
'this.i = 0;',
'this.s = "";',
'this.c = "";',
'this.b = false;',
'this.d = 0.0;',
'this.i2 = 3;',
'this.s2 = "foo";',
'this.c2 = "4";',
'this.b2 = true;',
'this.d2 = 5.6;',
'this.i3 = 0x707;',
'this.i4 = 4503599627370495;',
'this.i5 = -4503599627370496;',
'this.i6 = 0xfffffffffffff;',
'this.i7 =-0x10000000000000;',
'this.i8 = 0;',
'this.u8 = 0xfffffffffffff;',
'this.u9 = 0x0000000000000;'
]),
'');
end;
@ -4831,6 +4833,10 @@ begin
' d:=1/3;',
' d:=5.0E-324;',
' d:=1.7E308;',
' d:=001.00E00;',
' d:=002.00E001;',
' d:=-003.00E-00;',
' d:=-004.00E-001;',
' d:=10**3;',
' d:=10 mod 3;',
' d:=10 div 3;',
@ -4883,6 +4889,10 @@ begin
'$mod.d = 1 / 3;',
'$mod.d = 5.0E-324;',
'$mod.d = 1.7E308;',
'$mod.d = 1.00E0;',
'$mod.d = 2.00E1;',
'$mod.d = -3.00E-0;',
'$mod.d = -4.00E-1;',
'$mod.d = Math.pow(10, 3);',
'$mod.d = 10 % 3;',
'$mod.d = Math.floor(10 / 3);',