pastojs: filer: fixed testsuite using shortrefglobals for all tests, added FormData js keyword, fixed shortrefglobals new/free instance fields

git-svn-id: trunk@47609 -
This commit is contained in:
Mattias Gaertner 2020-11-27 16:57:08 +00:00
parent 63a231a93d
commit e1dd6fd932
6 changed files with 126 additions and 27 deletions

View File

@ -981,7 +981,7 @@ const
'yield' 'yield'
); );
// reserved words, not usable as global identifiers, can be used as sub identifiers // reserved words, not usable as global identifiers, can be used as sub identifiers
JSReservedGlobalWords: array[0..51] of string = ( JSReservedGlobalWords: array[0..52] of string = (
// keep sorted, first uppercase, then lowercase ! // keep sorted, first uppercase, then lowercase !
'Array', 'Array',
'ArrayBuffer', 'ArrayBuffer',
@ -992,6 +992,7 @@ const
'EvalError', 'EvalError',
'Float32Array', 'Float32Array',
'Float64Array', 'Float64Array',
'FormData',
'Generator', 'Generator',
'GeneratorFunction', 'GeneratorFunction',
'Infinity', 'Infinity',
@ -9733,11 +9734,30 @@ end;
function TPasToJSConverter.CreateSubDeclJSNameExpr(El: TPasElement; function TPasToJSConverter.CreateSubDeclJSNameExpr(El: TPasElement;
JSName: string; AContext: TConvertContext; PosEl: TPasElement): TJSElement; JSName: string; AContext: TConvertContext; PosEl: TPasElement): TJSElement;
var var
C: TClass;
VarKinds: TCtxVarKinds;
ParentName: String; ParentName: String;
begin begin
if AContext.IsGlobal then C:=El.ClassType;
if C.InheritsFrom(TPasType) or (C=TPasConst) then
VarKinds:=[cvkGlobal]
else if C.InheritsFrom(TPasVariable) then
begin begin
ParentName:=GetLocalName(El.Parent,[cvkGlobal,cvkCurType],AContext); VarKinds:=[cvkCurType];
if ([vmClass, vmStatic]*TPasVariable(El).VarModifiers<>[]) then
VarKinds:=[cvkGlobal]
else if El.Parent is TPasMembersType then
VarKinds:=[cvkCurType]
else
VarKinds:=[cvkGlobal];
end
else if (El.Parent is TProcedureBody) then
VarKinds:=[]
else
VarKinds:=[cvkGlobal];
if VarKinds<>[] then
begin
ParentName:=GetLocalName(El.Parent,VarKinds,AContext);
if ParentName='' then if ParentName='' then
ParentName:='this'; ParentName:='this';
if JSName[1]='[' then if JSName[1]='[' then
@ -14826,7 +14846,7 @@ begin
ObjLit.Name:=TJSString(TransformElToJSName(El,AContext)); ObjLit.Name:=TJSString(TransformElToJSName(El,AContext));
ObjLit.Expr:=CreateVarInit(El,AContext); ObjLit.Expr:=CreateVarInit(El,AContext);
end end
else if AContext.IsGlobal then else if AContext.IsGlobal or (El.Parent is TPasMembersType) then
begin begin
// create 'this.A=initvalue' // create 'this.A=initvalue'
AssignSt:=TJSSimpleAssignStatement(CreateElement(TJSSimpleAssignStatement,El)); AssignSt:=TJSSimpleAssignStatement(CreateElement(TJSSimpleAssignStatement,El));
@ -18588,7 +18608,7 @@ begin
try try
New_FuncContext.ThisVar.Element:=El; New_FuncContext.ThisVar.Element:=El;
New_FuncContext.ThisVar.Kind:=cvkCurType; New_FuncContext.ThisVar.Kind:=cvkCurType;
New_FuncContext.IsGlobal:=true; New_FuncContext.IsGlobal:=false;
// add class members // add class members
For I:=0 to El.Members.Count-1 do For I:=0 to El.Members.Count-1 do
@ -18600,8 +18620,10 @@ begin
and (ClassVarModifiersType*TPasVariable(P).VarModifiers=[]) then and (ClassVarModifiersType*TPasVariable(P).VarModifiers=[]) then
begin begin
if Kind=mfInit then if Kind=mfInit then
begin
// mfInit: init var // mfInit: init var
NewEl:=CreateVarDecl(TPasVariable(P),New_FuncContext) // can be nil NewEl:=CreateVarDecl(TPasVariable(P),New_FuncContext); // can be nil
end
else else
begin begin
// mfFinalize: clear reference // mfFinalize: clear reference

View File

@ -393,7 +393,7 @@ type
destructor Destroy; override; destructor Destroy; override;
Function CreatePCUSupport: TPCUSupport; virtual; Function CreatePCUSupport: TPCUSupport; virtual;
function GetInitialModeSwitches: TModeSwitches; function GetInitialModeSwitches: TModeSwitches;
function IsUnitReadFromPCU: Boolean; function IsUnitReadFromPCU: Boolean; // unit was read from pcu
function GetInitialBoolSwitches: TBoolSwitches; function GetInitialBoolSwitches: TBoolSwitches;
function GetInitialConverterOptions: TPasToJsConverterOptions; function GetInitialConverterOptions: TPasToJsConverterOptions;
procedure CreateScannerAndParser(aFileResolver: TPas2jsFSResolver); procedure CreateScannerAndParser(aFileResolver: TPas2jsFSResolver);
@ -1053,7 +1053,7 @@ begin
if coEnumValuesAsNumbers in Compiler.Options then if coEnumValuesAsNumbers in Compiler.Options then
Include(Result,fppas2js.coEnumNumbers); Include(Result,fppas2js.coEnumNumbers);
if coShortRefGlobals in Compiler.Options then if (coShortRefGlobals in Compiler.Options) or IsUnitReadFromPCU then
Include(Result,fppas2js.coShortRefGlobals); Include(Result,fppas2js.coShortRefGlobals);
if coLowerCase in Compiler.Options then if coLowerCase in Compiler.Options then
@ -2219,7 +2219,6 @@ begin
end; end;
function TPas2jsCompiler.CreateOptimizer: TPas2JSAnalyzer; function TPas2jsCompiler.CreateOptimizer: TPas2JSAnalyzer;
begin begin
Result:=TPas2JSAnalyzer.Create; Result:=TPas2JSAnalyzer.Create;
end; end;
@ -2351,7 +2350,6 @@ begin
end; end;
function TPas2jsCompiler.CreateSrcMap(const aFileName: String): TPas2JSSrcMap; function TPas2jsCompiler.CreateSrcMap(const aFileName: String): TPas2JSSrcMap;
begin begin
Result:=TPas2JSSrcMap.Create(aFileName); Result:=TPas2JSSrcMap.Create(aFileName);
end; end;
@ -2788,8 +2786,6 @@ begin
FResources.DoneUnit(aFile.isMainFile); FResources.DoneUnit(aFile.isMainFile);
EmitJavaScript(aFile,aFileWriter); EmitJavaScript(aFile,aFileWriter);
if aFile.IsMainFile and (TargetPlatform=PlatformNodeJS) then if aFile.IsMainFile and (TargetPlatform=PlatformNodeJS) then
aFileWriter.WriteFile('rtl.run();'+LineEnding,aFile.UnitFilename); aFileWriter.WriteFile('rtl.run();'+LineEnding,aFile.UnitFilename);
@ -4193,19 +4189,16 @@ begin
end; end;
function TPas2jsCompiler.CreateMacroEngine: TPas2jsMacroEngine; function TPas2jsCompiler.CreateMacroEngine: TPas2jsMacroEngine;
begin begin
Result:=TPas2jsMacroEngine.Create; Result:=TPas2jsMacroEngine.Create;
end; end;
function TPas2jsCompiler.CreateLog: TPas2jsLogger; function TPas2jsCompiler.CreateLog: TPas2jsLogger;
begin begin
Result:=TPas2jsLogger.Create; Result:=TPas2jsLogger.Create;
end; end;
constructor TPas2jsCompiler.Create; constructor TPas2jsCompiler.Create;
begin begin
FOptions:=DefaultP2jsCompilerOptions; FOptions:=DefaultP2jsCompilerOptions;
FConverterGlobals:=TPasToJSConverterGlobals.Create(Self); FConverterGlobals:=TPasToJSConverterGlobals.Create(Self);

View File

@ -243,8 +243,9 @@ const
'Goto' 'Goto'
); );
PCUMinConverterOptions = [coStoreImplJS,coShortRefGlobals];
PCUDefaultConverterOptions: TPasToJsConverterOptions = PCUDefaultConverterOptions: TPasToJsConverterOptions =
[coUseStrict,coStoreImplJS,coShortRefGlobals]; PCUMinConverterOptions+[coUseStrict];
PCUConverterOptions: array[TPasToJsConverterOption] of string = ( PCUConverterOptions: array[TPasToJsConverterOption] of string = (
'LowerCase', 'LowerCase',
'SwitchStatement', 'SwitchStatement',
@ -1043,6 +1044,7 @@ type
procedure DeletePendingSpecialize(PendSpec: TPCUReaderPendingSpecialized); procedure DeletePendingSpecialize(PendSpec: TPCUReaderPendingSpecialized);
function PromiseSpecialize(SpecId: integer; const SpecName: string; RefEl, ErrorEl: TPasElement): TPCUReaderPendingSpecialized; virtual; function PromiseSpecialize(SpecId: integer; const SpecName: string; RefEl, ErrorEl: TPasElement): TPCUReaderPendingSpecialized; virtual;
procedure ResolveSpecializedElements(Complete: boolean); procedure ResolveSpecializedElements(Complete: boolean);
function IsSpecialize(ChildEl: TPasElement): boolean;
protected protected
// json // json
procedure RaiseMsg(Id: int64; const Msg: string = ''); overload; override; procedure RaiseMsg(Id: int64; const Msg: string = ''); overload; override;
@ -1300,7 +1302,7 @@ implementation
procedure RegisterPCUFormat; procedure RegisterPCUFormat;
begin begin
if PCUFormat=nil then if PCUFormat=nil then
PCUFormat:=PrecompileFormats.Add('pcu','all used pcu must match exactly',TPCUReader,TPCUWriter); PCUFormat:=PrecompileFormats.Add('pcu','all used pcu must match exactly together',TPCUReader,TPCUWriter);
end; end;
function ComparePointer(Data1, Data2: Pointer): integer; function ComparePointer(Data1, Data2: Pointer): integer;
@ -2164,7 +2166,7 @@ begin
ParserOptions:=PCUDefaultParserOptions; ParserOptions:=PCUDefaultParserOptions;
ModeSwitches:=PCUDefaultModeSwitches; ModeSwitches:=PCUDefaultModeSwitches;
BoolSwitches:=PCUDefaultBoolSwitches; BoolSwitches:=PCUDefaultBoolSwitches;
ConverterOptions:=PCUDefaultConverterOptions-[coStoreImplJS]; ConverterOptions:=PCUDefaultConverterOptions;
TargetPlatform:=PCUDefaultTargetPlatform; TargetPlatform:=PCUDefaultTargetPlatform;
TargetProcessor:=PCUDefaultTargetProcessor; TargetProcessor:=PCUDefaultTargetProcessor;
end; end;
@ -3467,7 +3469,7 @@ begin
end end
else if (El.ClassType=TPasModule) or (El is TPasUnitModule) then else if (El.ClassType=TPasModule) or (El is TPasUnitModule) then
begin begin
// indirect used unit // indirectly used unit (refs to directly used units are created in WriteSection)
if aContext.IndirectUsesArr=nil then if aContext.IndirectUsesArr=nil then
begin begin
if aContext.SectionObj=nil then if aContext.SectionObj=nil then
@ -5642,6 +5644,17 @@ begin
end; end;
end; end;
function TPCUReader.IsSpecialize(ChildEl: TPasElement): boolean;
begin
if (ChildEl is TPasGenericType)
and Resolver.IsSpecialized(TPasGenericType(ChildEl)) then
exit(true);
if (ChildEl is TPasProcedure)
and (TPas2JSProcedureScope(ChildEl.CustomData).SpecializedFromItem<>nil) then
exit(true);
Result:=false;
end;
procedure TPCUReader.RaiseMsg(Id: int64; const Msg: string); procedure TPCUReader.RaiseMsg(Id: int64; const Msg: string);
var var
E: EPas2JsReadError; E: EPas2JsReadError;
@ -6597,8 +6610,7 @@ begin
for k:=0 to Members.Count-1 do for k:=0 to Members.Count-1 do
begin begin
ChildEl:=TPasElement(Members[k]); ChildEl:=TPasElement(Members[k]);
if (ChildEl is TPasGenericType) if IsSpecialize(ChildEl) then
and Resolver.IsSpecialized(TPasGenericType(ChildEl)) then
// skip specialized type // skip specialized type
else if Index=j then else if Index=j then
break break
@ -6855,7 +6867,7 @@ begin
RaiseMsg(20180314155953,Section,'indirect unit "'+Name+'"'); RaiseMsg(20180314155953,Section,'indirect unit "'+Name+'"');
UsedScope:=Module.InterfaceSection.CustomData as TPas2JSSectionScope; UsedScope:=Module.InterfaceSection.CustomData as TPas2JSSectionScope;
if not UsedScope.Finished then if not UsedScope.Finished then
RaiseMsg(20180314155953,Section,'indirect unit "'+Name+'"'); RaiseMsg(20180314155954,Section,'indirect unit "'+Name+'"');
ReadExternalReferences(UsesObj,Module); ReadExternalReferences(UsesObj,Module);
end; end;
end; end;
@ -9828,7 +9840,7 @@ begin
'InitParserOpts': InitialFlags.ParserOptions:=ReadParserOptions(Obj,nil,aName,PCUDefaultParserOptions); 'InitParserOpts': InitialFlags.ParserOptions:=ReadParserOptions(Obj,nil,aName,PCUDefaultParserOptions);
'InitModeSwitches': InitialFlags.ModeSwitches:=ReadModeSwitches(Obj,nil,aName,PCUDefaultModeSwitches); 'InitModeSwitches': InitialFlags.ModeSwitches:=ReadModeSwitches(Obj,nil,aName,PCUDefaultModeSwitches);
'InitBoolSwitches': InitialFlags.BoolSwitches:=ReadBoolSwitches(Obj,nil,aName,PCUDefaultBoolSwitches); 'InitBoolSwitches': InitialFlags.BoolSwitches:=ReadBoolSwitches(Obj,nil,aName,PCUDefaultBoolSwitches);
'InitConverterOpts': InitialFlags.ConverterOptions:=ReadConverterOptions(Obj,nil,aName,PCUDefaultConverterOptions-[coStoreImplJS]); 'InitConverterOpts': InitialFlags.ConverterOptions:=ReadConverterOptions(Obj,nil,aName,PCUDefaultConverterOptions);
'FinalParserOpts': Parser.Options:=ReadParserOptions(Obj,nil,aName,InitialFlags.ParserOptions); 'FinalParserOpts': Parser.Options:=ReadParserOptions(Obj,nil,aName,InitialFlags.ParserOptions);
'FinalModeSwitches': Scanner.CurrentModeSwitches:=ReadModeSwitches(Obj,nil,aName,InitialFlags.ModeSwitches); 'FinalModeSwitches': Scanner.CurrentModeSwitches:=ReadModeSwitches(Obj,nil,aName,InitialFlags.ModeSwitches);
'FinalBoolSwitches': Scanner.CurrentBoolSwitches:=ReadBoolSwitches(Obj,nil,aName,InitialFlags.BoolSwitches); 'FinalBoolSwitches': Scanner.CurrentBoolSwitches:=ReadBoolSwitches(Obj,nil,aName,InitialFlags.BoolSwitches);

View File

@ -169,7 +169,7 @@ begin
PrecompileInitialFlags.ParserOptions:=MyFile.Parser.Options; PrecompileInitialFlags.ParserOptions:=MyFile.Parser.Options;
PrecompileInitialFlags.ModeSwitches:=MyFile.Scanner.CurrentModeSwitches; PrecompileInitialFlags.ModeSwitches:=MyFile.Scanner.CurrentModeSwitches;
PrecompileInitialFlags.BoolSwitches:=MyFile.Scanner.CurrentBoolSwitches; PrecompileInitialFlags.BoolSwitches:=MyFile.Scanner.CurrentBoolSwitches;
PrecompileInitialFlags.ConverterOptions:=MyFile.GetInitialConverterOptions; PrecompileInitialFlags.ConverterOptions:=MyFile.GetInitialConverterOptions+PCUMinConverterOptions;
PrecompileInitialFlags.TargetPlatform:=Compiler.TargetPlatform; PrecompileInitialFlags.TargetPlatform:=Compiler.TargetPlatform;
PrecompileInitialFlags.TargetProcessor:=Compiler.TargetProcessor; PrecompileInitialFlags.TargetProcessor:=Compiler.TargetProcessor;
end; end;
@ -315,17 +315,16 @@ begin
// create JavaScript for procs, initialization, finalization // create JavaScript for procs, initialization, finalization
MyFile.CreateConverter; MyFile.CreateConverter;
MyFile.Converter.Options:=MyFile.Converter.Options+[coStoreImplJS]; MyFile.Converter.Options:=MyFile.Converter.Options+PCUMinConverterOptions;
MyFile.Converter.OnIsElementUsed:=@OnPCUConverterIsElementUsed; MyFile.Converter.OnIsElementUsed:=@OnPCUConverterIsElementUsed;
MyFile.Converter.OnIsTypeInfoUsed:=@OnPCUConverterIsTypeInfoUsed; MyFile.Converter.OnIsTypeInfoUsed:=@OnPCUConverterIsTypeInfoUsed;
JS:=MyFile.Converter.ConvertPasElement(MyFile.PasModule,MyFile.PascalResolver); JS:=MyFile.Converter.ConvertPasElement(MyFile.PasModule,MyFile.PascalResolver);
MyFile.Converter.Options:=MyFile.Converter.Options-[coStoreImplJS];
MyFile.PCUSupport.SetInitialCompileFlags; MyFile.PCUSupport.SetInitialCompileFlags;
{$IFDEF REALLYVERBOSE} {$IFDEF REALLYVERBOSE}
writeln('TPas2jsCompilerFile.WritePCU create pcu ... ',MyFile.PCUFilename); writeln('TPas2jsCompilerFile.WritePCU create pcu ... ',MyFile.PCUFilename);
{$ENDIF} {$ENDIF}
Writer.WritePCU(MyFile.PascalResolver,MyFile.Converter, Writer.WritePCU(MyFile.PascalResolver,MyFile.Converter,
PrecompileInitialFlags,ms,AllowCompressed); PrecompileInitialFlags,ms,AllowCompressed);
{$IFDEF REALLYVERBOSE} {$IFDEF REALLYVERBOSE}
writeln('TPas2jsCompilerFile.WritePCU precompiled ',MyFile.PCUFilename); writeln('TPas2jsCompilerFile.WritePCU precompiled ',MyFile.PCUFilename);
{$ENDIF} {$ENDIF}

View File

@ -61,6 +61,7 @@ type
procedure TestOptShortRefGlobals_Unit_FromIntfImpl_ToIntfImpl; procedure TestOptShortRefGlobals_Unit_FromIntfImpl_ToIntfImpl;
procedure TestOptShortRefGlobals_Property; procedure TestOptShortRefGlobals_Property;
procedure TestOptShortRefGlobals_ExternalAbstract; procedure TestOptShortRefGlobals_ExternalAbstract;
procedure TestOptShortRefGlobals_Class;
procedure TestOptShortRefGlobals_GenericFunction; procedure TestOptShortRefGlobals_GenericFunction;
procedure TestOptShortRefGlobals_GenericMethod_Call; procedure TestOptShortRefGlobals_GenericMethod_Call;
procedure TestOptShortRefGlobals_GenericStaticMethod_Call; procedure TestOptShortRefGlobals_GenericStaticMethod_Call;
@ -551,6 +552,77 @@ begin
''])); '']));
end; end;
procedure TTestOptimizations.TestOptShortRefGlobals_Class;
begin
AddModuleWithIntfImplSrc('UnitA.pas',
LinesToStr([
'type',
' TBird = class',
' end;',
'']),
LinesToStr([
'']));
StartUnit(true,[supTObject]);
Add([
'{$optimization JSShortRefGlobals}',
'interface',
'uses unita;',
'type',
' TEagle = class(TBird)',
' Size: TBird;',
' class var Color: TBird;',
' procedure Fly;',
' class procedure Run;',
' end;',
'implementation',
'procedure TEagle.Fly;',
'begin',
' Size:=Size;',
' Self.Size:=Self.Size;',
' Color:=Color;',
' Self.Color:=Self.Color;',
'end;',
'class procedure TEagle.Run;',
'begin',
' Color:=Color;',
' Self.Color:=Self.Color;',
'end;',
'']);
ConvertUnit;
CheckSource('TestOptShortRefGlobals_Class',
LinesToStr([
'var $lt = null;',
'var $lm = pas.UnitA;',
'var $lt1 = $lm.TBird;',
'rtl.createClass(this, "TEagle", $lt1, function () {',
' $lt = this;',
' this.Color = null;',
' this.$init = function () {',
' $lt1.$init.call(this);',
' this.Size = null;',
' };',
' this.$final = function () {',
' this.Size = undefined;',
' $lt1.$final.call(this);',
' };',
' this.Fly = function () {',
' this.Size = this.Size;',
' this.Size = this.Size;',
' $lt.Color = this.Color;',
' $lt.Color = this.Color;',
' };',
' this.Run = function () {',
' $lt.Color = this.Color;',
' $lt.Color = this.Color;',
' };',
'});',
'']),
LinesToStr([
'']),
LinesToStr([
'']));
end;
procedure TTestOptimizations.TestOptShortRefGlobals_GenericFunction; procedure TTestOptimizations.TestOptShortRefGlobals_GenericFunction;
begin begin
AddModuleWithIntfImplSrc('UnitA.pas', AddModuleWithIntfImplSrc('UnitA.pas',

View File

@ -398,6 +398,7 @@ begin
aFile.Attr:=faNormal; aFile.Attr:=faNormal;
aFile.Age:=DateTimeToFileDate(CurDate); aFile.Age:=DateTimeToFileDate(CurDate);
writeln('TCustomTestCLI.OnWriteFile ',aFile.Filename,' Found=',FindFile(aFilename)<>nil,' "',LeftStr(aFile.Source,50),'" '); writeln('TCustomTestCLI.OnWriteFile ',aFile.Filename,' Found=',FindFile(aFilename)<>nil,' "',LeftStr(aFile.Source,50),'" ');
//writeln('TCustomTestCLI.OnWriteFile ',aFile.Source);
end; end;
procedure TCustomTestCLI.WriteSources; procedure TCustomTestCLI.WriteSources;