pastojs: convert str(float:w:p) to rtl.floatToStr(d,w,p)

git-svn-id: trunk@37301 -
This commit is contained in:
Mattias Gaertner 2017-09-22 15:04:52 +00:00
parent 58aaf2a545
commit f1055a32b9
2 changed files with 21 additions and 15 deletions

View File

@ -406,6 +406,7 @@ type
pbifnGetObject,
pbifnIs,
pbifnIsExt,
pbifnFloatToStr,
pbifnFreeLocalVar,
pbifnFreeVar,
pbifnProcType_Create,
@ -506,6 +507,7 @@ const
'getObject', // rtl.getObject
'is', // rtl.is
'isExt', // rtl.isExt
'floatToStr', // rtl.floatToStr
'freeLoc', // rtl.freeLoc
'free', // rtl.free
'createCallback', // rtl.createCallback
@ -7010,7 +7012,7 @@ end;
function TPasToJSConverter.ConvertBuiltIn_StrProc(El: TParamsExpr;
AContext: TConvertContext): TJSElement;
// convert 'str(value,aString)' to 'aString = <string>'
// for the conversion see ConvertBuiltInStrFunc
// for the conversion see ConvertBuiltInStrParam
var
AssignContext: TAssignContext;
StrVar: TPasExpr;
@ -7086,6 +7088,7 @@ var
Call: TJSCallExpression;
PlusEl: TJSAdditiveExpressionPlus;
Bracket: TJSBracketMemberExpression;
procedure PrependStrLit;
begin
PlusEl:=TJSAdditiveExpressionPlus(CreateElement(TJSAdditiveExpressionPlus,El));
@ -7109,18 +7112,17 @@ begin
end
else if ResolvedEl.BaseType in btAllJSFloats then
begin
NeedStrLit:=true;
Add:=ConvertElement(El,AContext);
// convert to rtl.floatToStr(El,width,precision)
Call:=CreateCallExpression(El);
Call.Expr:=CreateMemberExpression([FBuiltInNames[pbivnRTL],FBuiltInNames[pbifnFloatToStr]]);
Call.AddArg(ConvertElement(El,AContext));
if El.format1<>nil then
Call.AddArg(ConvertElement(El.format1,AContext));
if El.format2<>nil then
begin
// precision -> rtl El.toFixed(precision);
NeedStrLit:=false;
Call:=CreateCallExpression(El);
Call.Expr:=CreateDotExpression(El,Add,CreatePrimitiveDotExpr('toFixed',El));
Call.AddArg(ConvertElement(El.format2,AContext));
Add:=Call;
Call:=nil;
end;
Result:=Call;
Call:=nil;
exit;
end
else if IsStrFunc and (ResolvedEl.BaseType in btAllJSStringAndChars) then
Add:=ConvertElement(El,AContext)

View File

@ -4253,6 +4253,8 @@ begin
Add(' str(d,s);');
Add(' str(i:3,s);');
Add(' str(d:3:2,s);');
Add(' Str(12.456:12:1,s);');
Add(' Str(12.456:12,s);');
Add(' s:=str(b);');
Add(' s:=str(i);');
Add(' s:=str(d);');
@ -4275,15 +4277,17 @@ begin
LinesToStr([ // this.$main
'$mod.s = ""+$mod.b;',
'$mod.s = ""+$mod.i;',
'$mod.s = ""+$mod.d;',
'$mod.s = rtl.floatToStr($mod.d);',
'$mod.s = rtl.spaceLeft(""+$mod.i,3);',
'$mod.s = rtl.spaceLeft($mod.d.toFixed(2),3);',
'$mod.s = rtl.floatToStr($mod.d,3,2);',
'$mod.s = rtl.floatToStr(12.456,12,1);',
'$mod.s = rtl.floatToStr(12.456,12);',
'$mod.s = ""+$mod.b;',
'$mod.s = ""+$mod.i;',
'$mod.s = ""+$mod.d;',
'$mod.s = rtl.floatToStr($mod.d);',
'$mod.s = (""+$mod.i)+$mod.i;',
'$mod.s = rtl.spaceLeft(""+$mod.i,3);',
'$mod.s = rtl.spaceLeft($mod.d.toFixed(2),3);',
'$mod.s = rtl.floatToStr($mod.d,3,2);',
'$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
'$mod.s = ("" + $mod.i) + rtl.spaceLeft("" + $mod.i, 5);',
'$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',