fcl-js: write async function and await expression

git-svn-id: trunk@45423 -
This commit is contained in:
Mattias Gaertner 2020-05-18 15:49:31 +00:00
parent 8e8bbf4411
commit a7ecde8bb2
4 changed files with 45 additions and 3 deletions

View File

@ -48,7 +48,8 @@ type
tjsSWITCH, tjsSWITCH,
tjsTHIS, tjsTHROW, tjsTrue, tjsTRY, tjsTYPEOF, tjsTHIS, tjsTHROW, tjsTrue, tjsTRY, tjsTYPEOF,
tjsVAR, tjsVOID, tjsVAR, tjsVOID,
tjsWHILE, tjsWITH tjsWHILE, tjsWITH,
tjsAWAIT
); );
const const
@ -83,7 +84,8 @@ const
'switch', 'switch',
'this', 'throw', 'true', 'try', 'typeof', 'this', 'throw', 'true', 'try', 'typeof',
'var', 'void', 'var', 'void',
'while', 'with' 'while', 'with',
'await'
); );

View File

@ -126,6 +126,7 @@ Type
TJSFuncDef = Class(TJSObject) TJSFuncDef = Class(TJSObject)
private private
FBody: TJSFunctionBody; FBody: TJSFunctionBody;
FIsAsync: Boolean;
FIsEmpty: Boolean; FIsEmpty: Boolean;
FName: TJSString; FName: TJSString;
FParams: TStrings; FParams: TStrings;
@ -137,6 +138,7 @@ Type
Property Body : TJSFunctionBody Read FBody Write FBody; // can be nil Property Body : TJSFunctionBody Read FBody Write FBody; // can be nil
Property Name : TJSString Read FName Write FName; Property Name : TJSString Read FName Write FName;
Property IsEmpty : Boolean Read FIsEmpty Write FIsEmpty; Property IsEmpty : Boolean Read FIsEmpty Write FIsEmpty;
Property IsAsync : Boolean Read FIsAsync Write FIsAsync;
end; end;
{ TJSElement } { TJSElement }
@ -383,6 +385,13 @@ Type
Class function PrefixOperatorToken : tjsToken; override; Class function PrefixOperatorToken : tjsToken; override;
end; end;
{ TJSAwaitExpression - e.g. 'await A' }
TJSAwaitExpression = Class(TJSUnaryExpression)
Public
Class function PrefixOperatorToken : tjsToken; Override;
end;
{ TJSUnaryPrePlusPlusExpression - e.g. '++A' } { TJSUnaryPrePlusPlusExpression - e.g. '++A' }
TJSUnaryPrePlusPlusExpression = Class(TJSUnaryExpression) TJSUnaryPrePlusPlusExpression = Class(TJSUnaryExpression)
@ -1514,6 +1523,13 @@ begin
Result:=tjsTypeOf; Result:=tjsTypeOf;
end; end;
{ TJSAwaitExpression }
class function TJSAwaitExpression.PrefixOperatorToken: tjsToken;
begin
Result:=tjsAwait;
end;
{ TJSUnaryDeleteExpression } { TJSUnaryDeleteExpression }
Class function TJSUnaryDeleteExpression.PrefixOperatorToken : tjsToken; Class function TJSUnaryDeleteExpression.PrefixOperatorToken : tjsToken;
@ -1705,7 +1721,7 @@ begin
else else
begin begin
Result:=TokenInfos[t]; Result:=TokenInfos[t];
if t in [tjsTypeOf,tjsVoid,tjsDelete,tjsThrow] then if t in [tjsTypeOf,tjsVoid,tjsDelete,tjsThrow,tjsAwait] then
Result:=Result+' '; Result:=Result+' ';
end; end;
end; end;

View File

@ -927,6 +927,8 @@ Var
begin begin
LastEl:=Writer.CurElement; LastEl:=Writer.CurElement;
C:=(woCompact in Options); C:=(woCompact in Options);
if fd.IsAsync then
Write('async ');
Write('function '); Write('function ');
If (FD.Name<>'') then If (FD.Name<>'') then
Write(FD.Name); Write(FD.Name);

View File

@ -167,6 +167,7 @@ type
Procedure TestFunctionDefBody1Compact; Procedure TestFunctionDefBody1Compact;
Procedure TestFunctionDefBody2; Procedure TestFunctionDefBody2;
Procedure TestFunctionDefBody2Compact; Procedure TestFunctionDefBody2Compact;
Procedure TestFunctionDefAsync;
Procedure TestTryCatch; Procedure TestTryCatch;
Procedure TestTryCatchCompact; Procedure TestTryCatchCompact;
Procedure TestTryFinally; Procedure TestTryFinally;
@ -193,6 +194,7 @@ type
Procedure TestUnaryDelete; Procedure TestUnaryDelete;
Procedure TestUnaryVoid; Procedure TestUnaryVoid;
Procedure TestUnaryTypeOf; Procedure TestUnaryTypeOf;
Procedure TestUnaryAwait;
Procedure TestPrefixPlusPLus; Procedure TestPrefixPlusPLus;
Procedure TestPrefixMinusMinus; Procedure TestPrefixMinusMinus;
Procedure TestUnaryMinus; Procedure TestUnaryMinus;
@ -347,6 +349,11 @@ begin
TestUnary('typeof expresssion',TJSUnaryTypeOfExpression,'typeof a'); TestUnary('typeof expresssion',TJSUnaryTypeOfExpression,'typeof a');
end; end;
procedure TTestExpressionWriter.TestUnaryAwait;
begin
TestUnary('await expresssion',TJSAwaitExpression,'await a');
end;
procedure TTestExpressionWriter.TestPrefixPlusPLus; procedure TTestExpressionWriter.TestPrefixPlusPLus;
begin begin
TestUnary('prefix ++ expresssion',TJSUnaryPrePlusPlusExpression,'++a'); TestUnary('prefix ++ expresssion',TJSUnaryPrePlusPlusExpression,'++a');
@ -1902,6 +1909,21 @@ begin
AssertWrite('Function, 2 statements, compact','function a(b) {b=b*10; return b}',FD); AssertWrite('Function, 2 statements, compact','function a(b) {b=b*10; return b}',FD);
end; end;
procedure TTestStatementWriter.TestFunctionDefAsync;
Var
FD : TJSFunctionDeclarationStatement;
begin
FD:=TJSFunctionDeclarationStatement.Create(0,0);
FD.AFunction:=TJSFuncDef.Create;
FD.AFunction.IsAsync:=true;
FD.AFunction.Name:='a';
AssertWrite('Async function',
'async function a() {'+sLineBreak
+'}',FD);
end;
procedure TTestStatementWriter.TestTryCatch; procedure TTestStatementWriter.TestTryCatch;
Var Var