pas2js: ArrayOfChar:=String, pass string to ArrayOfChar

git-svn-id: trunk@45321 -
This commit is contained in:
Mattias Gaertner 2020-05-09 20:14:21 +00:00
parent a316229ef6
commit 042ad497d3
2 changed files with 57 additions and 12 deletions

View File

@ -1890,6 +1890,7 @@ type
AssignContext: TAssignContext; PosEl: TPasElement): TJSCallExpression; virtual; AssignContext: TAssignContext; PosEl: TPasElement): TJSCallExpression; virtual;
Function AppendPropertyReadArgs(Call: TJSCallExpression; Prop: TPasProperty; Function AppendPropertyReadArgs(Call: TJSCallExpression; Prop: TPasProperty;
aContext: TConvertContext; PosEl: TPasElement): TJSCallExpression; virtual; aContext: TConvertContext; PosEl: TPasElement): TJSCallExpression; virtual;
Function CreateDotSplit(El: TPasElement; Expr: TJSElement): TJSElement; virtual;
Function CreatePrecompiledJS(El: TJSElement): string; virtual; Function CreatePrecompiledJS(El: TJSElement): string; virtual;
Function CreateRaisePropReadOnly(PosEl: TPasElement): TJSElement; virtual; Function CreateRaisePropReadOnly(PosEl: TPasElement): TJSElement; virtual;
Procedure AddRTLVersionCheck(FuncContext: TFunctionContext; PosEl: TPasElement); Procedure AddRTLVersionCheck(FuncContext: TFunctionContext; PosEl: TPasElement);
@ -17427,6 +17428,21 @@ begin
Result:=Call; Result:=Call;
end; end;
function TPasToJSConverter.CreateDotSplit(El: TPasElement; Expr: TJSElement
): TJSElement;
var
DotExpr: TJSDotMemberExpression;
Call: TJSCallExpression;
begin
Call:=CreateCallExpression(El);
DotExpr:=TJSDotMemberExpression(CreateElement(TJSDotMemberExpression,El));
Call.Expr:=DotExpr;
DotExpr.MExpr:=Expr;
DotExpr.Name:='split';
Call.AddArg(CreateLiteralJSString(El,''));
Result:=Call;
end;
function TPasToJSConverter.CreatePrecompiledJS(El: TJSElement): string; function TPasToJSConverter.CreatePrecompiledJS(El: TJSElement): string;
var var
aWriter: TBufferWriter; aWriter: TBufferWriter;
@ -19812,6 +19828,17 @@ begin
// e.g. double := currency -> double := currency/10000 // e.g. double := currency -> double := currency/10000
AssignContext.RightSide:=CreateDivideNumber(El,AssignContext.RightSide,10000); AssignContext.RightSide:=CreateDivideNumber(El,AssignContext.RightSide,10000);
end end
else if AssignContext.RightResolved.BaseType in btAllStringAndChars then
begin
if AssignContext.LeftResolved.BaseType=btContext then
begin
if AssignContext.LeftResolved.LoTypeEl is TPasArrayType then
begin
// AnArray:=aString -> AnArray:=aString.split("")
AssignContext.RightSide:=CreateDotSplit(El.right,AssignContext.RightSide);
end;
end;
end
else if AssignContext.RightResolved.BaseType=btContext then else if AssignContext.RightResolved.BaseType=btContext then
begin begin
RightTypeEl:=AssignContext.RightResolved.LoTypeEl; RightTypeEl:=AssignContext.RightResolved.LoTypeEl;
@ -22689,14 +22716,21 @@ begin
if ArgTypeIsArray then if ArgTypeIsArray then
begin begin
// array as argument
if ExprResolved.BaseType=btNil then if ExprResolved.BaseType=btNil then
begin begin
// nil to array -> pass [] // nil to array -> pass []
Result:=TJSArrayLiteral(CreateElement(TJSArrayLiteral,El)); Result:=TJSArrayLiteral(CreateElement(TJSArrayLiteral,El));
exit; exit;
end; end
// array as argument else if ExprResolved.BaseType in btAllStringAndChars then
Result:=CreateArrayInit(TPasArrayType(ArgTypeEl),El,El,AContext); begin
// pass string to an array
Result:=ConvertExpression(El,AContext);
Result:=CreateDotSplit(El,Result);
end
else
Result:=CreateArrayInit(TPasArrayType(ArgTypeEl),El,El,AContext);
end; end;
if Result=nil then if Result=nil then

View File

@ -9510,8 +9510,6 @@ end;
procedure TTestModule.TestArray_ArrayOfCharAssignString; procedure TTestModule.TestArray_ArrayOfCharAssignString;
begin begin
exit; // todo
StartProgram(false); StartProgram(false);
Add([ Add([
'type TArr = array of char;', 'type TArr = array of char;',
@ -9522,21 +9520,34 @@ begin
'procedure Run(const a: array of char);', 'procedure Run(const a: array of char);',
'begin', 'begin',
' Run(c);', ' Run(c);',
//' Run(s);', ' Run(s);',
'end;', 'end;',
'begin', 'begin',
//' a:=c;', ' a:=c;',
//' a:=s;', ' a:=s;',
//' a:=#13;', ' a:=#13;',
//' a:=''Foo'';', ' a:=''Foo'';',
//' Run(c);', ' Run(c);',
//' Run(s);', ' Run(s);',
'']); '']);
ConvertProgram; ConvertProgram;
CheckSource('TestArray_ArrayOfCharAssignString', CheckSource('TestArray_ArrayOfCharAssignString',
LinesToStr([ // statements LinesToStr([ // statements
'this.c = "";',
'this.s = "";',
'this.a = [];',
'this.Run = function (a) {',
' $mod.Run($mod.c.split(""));',
' $mod.Run($mod.s.split(""));',
'};',
'']), '']),
LinesToStr([ LinesToStr([
'$mod.a = $mod.c.split("");',
'$mod.a = $mod.s.split("");',
'$mod.a = "\r".split("");',
'$mod.a = "Foo".split("");',
'$mod.Run($mod.c.split(""));',
'$mod.Run($mod.s.split(""));',
''])); '']));
end; end;