|
@@ -1673,6 +1673,7 @@ type
|
|
function GetContextOfPasElement(El: TPasElement): TConvertContext;
|
|
function GetContextOfPasElement(El: TPasElement): TConvertContext;
|
|
function GetFuncContextOfPasElement(El: TPasElement): TFunctionContext;
|
|
function GetFuncContextOfPasElement(El: TPasElement): TFunctionContext;
|
|
function GetContextOfType(aType: TConvertContextClass): TConvertContext;
|
|
function GetContextOfType(aType: TConvertContextClass): TConvertContext;
|
|
|
|
+ function GetMainSectionContext: TFunctionContext;
|
|
function CurrentModeSwitches: TModeSwitches;
|
|
function CurrentModeSwitches: TModeSwitches;
|
|
function GetGlobalFunc: TFunctionContext;
|
|
function GetGlobalFunc: TFunctionContext;
|
|
procedure WriteStack;
|
|
procedure WriteStack;
|
|
@@ -1922,7 +1923,9 @@ type
|
|
Function GetTypeInfoName(El: TPasType; AContext: TConvertContext;
|
|
Function GetTypeInfoName(El: TPasType; AContext: TConvertContext;
|
|
ErrorEl: TPasElement; Full: boolean = false): String; virtual;
|
|
ErrorEl: TPasElement; Full: boolean = false): String; virtual;
|
|
Function TransformArgName(Arg: TPasArgument; AContext: TConvertContext): string; virtual;
|
|
Function TransformArgName(Arg: TPasArgument; AContext: TConvertContext): string; virtual;
|
|
- Function CreateGlobalAlias(El: TPasElement; JSPath: string; AContext: TConvertContext): string; virtual;
|
|
|
|
|
|
+ Function CreateGlobalAliasForeign(El: TPasElement; JSPath: string; AContext: TConvertContext): string; virtual; // El in other module
|
|
|
|
+ Function CreateGlobalAliasNull(El: TPasElement; Prefix: TPas2JSBuiltInName;
|
|
|
|
+ SectionContext: TSectionContext): TFCLocalIdentifier; virtual;
|
|
// utility functions for creating stuff
|
|
// utility functions for creating stuff
|
|
Function IsElementUsed(El: TPasElement): boolean; virtual;
|
|
Function IsElementUsed(El: TPasElement): boolean; virtual;
|
|
Function IsSystemUnit(aModule: TPasModule): boolean; virtual;
|
|
Function IsSystemUnit(aModule: TPasModule): boolean; virtual;
|
|
@@ -7541,6 +7544,18 @@ begin
|
|
until ctx=nil;
|
|
until ctx=nil;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+function TConvertContext.GetMainSectionContext: TFunctionContext;
|
|
|
|
+var
|
|
|
|
+ Ctx: TConvertContext;
|
|
|
|
+begin
|
|
|
|
+ Ctx:=Self;
|
|
|
|
+ repeat
|
|
|
|
+ if Ctx is TSectionContext then
|
|
|
|
+ Result:=TSectionContext(Ctx);
|
|
|
|
+ Ctx:=Ctx.Parent;
|
|
|
|
+ until Ctx=nil;
|
|
|
|
+end;
|
|
|
|
+
|
|
function TConvertContext.CurrentModeSwitches: TModeSwitches;
|
|
function TConvertContext.CurrentModeSwitches: TModeSwitches;
|
|
begin
|
|
begin
|
|
if Resolver=nil then
|
|
if Resolver=nil then
|
|
@@ -14650,6 +14665,29 @@ Var
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+ procedure InitForwards(Decls: TFPList; SectionContext: TSectionContext);
|
|
|
|
+ var
|
|
|
|
+ i: Integer;
|
|
|
|
+ P: TPasElement;
|
|
|
|
+ C: TClass;
|
|
|
|
+ begin
|
|
|
|
+ For i:=0 to Decls.Count-1 do
|
|
|
|
+ begin
|
|
|
|
+ P:=TPasElement(Decls[i]);
|
|
|
|
+ if not IsElementUsed(P) then continue;
|
|
|
|
+ C:=P.ClassType;
|
|
|
|
+ if (C=TPasClassType) and TPasClassType(P).IsForward then
|
|
|
|
+ continue;
|
|
|
|
+ if (C=TPasClassType) or (C=TPasRecordType) or (C=TPasEnumType) then
|
|
|
|
+ begin
|
|
|
|
+ // add var $lt = null;
|
|
|
|
+ CreateGlobalAliasNull(P,pbivnLocalTypeRef,SectionContext);
|
|
|
|
+ if (C=TPasClassType) or (C=TPasRecordType) then
|
|
|
|
+ InitForwards(TPasMembersType(P).Members,SectionContext);
|
|
|
|
+ end;
|
|
|
|
+ end;
|
|
|
|
+ end;
|
|
|
|
+
|
|
procedure InitSection(Section: TPasSection);
|
|
procedure InitSection(Section: TPasSection);
|
|
var
|
|
var
|
|
SectionScope: TPas2JSSectionScope;
|
|
SectionScope: TPas2JSSectionScope;
|
|
@@ -14665,6 +14703,18 @@ Var
|
|
SectionCtx:=TSectionContext(AContext);
|
|
SectionCtx:=TSectionContext(AContext);
|
|
Src:=SectionCtx.JSElement as TJSSourceElements;
|
|
Src:=SectionCtx.JSElement as TJSSourceElements;
|
|
SectionCtx.HeaderIndex:=Src.Statements.Count;
|
|
SectionCtx.HeaderIndex:=Src.Statements.Count;
|
|
|
|
+
|
|
|
|
+ // add local vars for forward declarations
|
|
|
|
+ if (coShortRefGlobals in Options)
|
|
|
|
+ and (Section.ClassType<>TImplementationSection) then
|
|
|
|
+ begin
|
|
|
|
+ InitForwards(Section.Declarations,TSectionContext(AContext));
|
|
|
|
+ if Section is TInterfaceSection then
|
|
|
|
+ begin
|
|
|
|
+ InitForwards(TPasModule(Section.Parent).ImplementationSection.Declarations,
|
|
|
|
+ TSectionContext(AContext));
|
|
|
|
+ end;
|
|
|
|
+ end;
|
|
end;
|
|
end;
|
|
|
|
|
|
var
|
|
var
|
|
@@ -14846,7 +14896,7 @@ var
|
|
P: TPasElement;
|
|
P: TPasElement;
|
|
Scope: TPas2JSClassScope;
|
|
Scope: TPas2JSClassScope;
|
|
Ancestor: TPasType;
|
|
Ancestor: TPasType;
|
|
- AncestorPath, OwnerName, DestructorName, FnName, IntfKind: String;
|
|
|
|
|
|
+ AncestorPath, OwnerName, DestructorName, FnName, IntfKind, JSName: String;
|
|
C: TClass;
|
|
C: TClass;
|
|
AssignSt: TJSSimpleAssignStatement;
|
|
AssignSt: TJSSimpleAssignStatement;
|
|
NeedInitFunction, HasConstructor, IsJSFunction, NeedClassExt,
|
|
NeedInitFunction, HasConstructor, IsJSFunction, NeedClassExt,
|
|
@@ -14926,7 +14976,8 @@ begin
|
|
Call.AddArg(CreatePrimitiveDotExpr(OwnerName,El));
|
|
Call.AddArg(CreatePrimitiveDotExpr(OwnerName,El));
|
|
|
|
|
|
// add parameter: string constant '"classname"'
|
|
// add parameter: string constant '"classname"'
|
|
- ArgEx := CreateLiteralString(El,TransformElToJSName(El,AContext));
|
|
|
|
|
|
+ JSName:=TransformElToJSName(El,AContext);
|
|
|
|
+ ArgEx:=CreateLiteralString(El,JSName);
|
|
Call.AddArg(ArgEx);
|
|
Call.AddArg(ArgEx);
|
|
|
|
|
|
if El.ObjKind=okInterface then
|
|
if El.ObjKind=okInterface then
|
|
@@ -14985,6 +15036,18 @@ begin
|
|
FuncContext.ThisVar.Element:=El;
|
|
FuncContext.ThisVar.Element:=El;
|
|
FuncContext.ThisVar.Kind:=cvkGlobal;
|
|
FuncContext.ThisVar.Kind:=cvkGlobal;
|
|
|
|
|
|
|
|
+ if coShortRefGlobals in Options then
|
|
|
|
+ begin
|
|
|
|
+ // $lt = this;
|
|
|
|
+ JSName:=AContext.GetLocalName(El,[cvkGlobal]);
|
|
|
|
+ if JSName='' then
|
|
|
|
+ RaiseNotSupported(El,AContext,20200926232402);
|
|
|
|
+ AssignSt:=TJSSimpleAssignStatement(CreateElement(TJSSimpleAssignStatement,El));
|
|
|
|
+ AssignSt.LHS:=CreatePrimitiveDotExpr(JSName,El);
|
|
|
|
+ AssignSt.Expr:=CreatePrimitiveDotExpr('this',El);
|
|
|
|
+ AddToSourceElements(Src,AssignSt);
|
|
|
|
+ end;
|
|
|
|
+
|
|
if IntfKind<>'' then
|
|
if IntfKind<>'' then
|
|
begin
|
|
begin
|
|
// add this.$kind="com";
|
|
// add this.$kind="com";
|
|
@@ -15279,7 +15342,7 @@ function TPasToJSConverter.ConvertEnumType(El: TPasEnumType;
|
|
// maxvalue: 1
|
|
// maxvalue: 1
|
|
// });
|
|
// });
|
|
// coShortRefGlobals:
|
|
// coShortRefGlobals:
|
|
-// var $lt = this.TMyEnum ...
|
|
|
|
|
|
+// $lt = this.TMyEnum ...
|
|
var
|
|
var
|
|
ObjectContect: TObjectContext;
|
|
ObjectContect: TObjectContext;
|
|
i: Integer;
|
|
i: Integer;
|
|
@@ -15295,7 +15358,7 @@ var
|
|
Src: TJSSourceElements;
|
|
Src: TJSSourceElements;
|
|
ProcScope: TPas2JSProcedureScope;
|
|
ProcScope: TPas2JSProcedureScope;
|
|
VarSt: TJSVariableStatement;
|
|
VarSt: TJSVariableStatement;
|
|
- GlobalCtx: TConvertContext;
|
|
|
|
|
|
+ SectionContext: TSectionContext;
|
|
begin
|
|
begin
|
|
Result:=nil;
|
|
Result:=nil;
|
|
for i:=0 to El.Values.Count-1 do
|
|
for i:=0 to El.Values.Count-1 do
|
|
@@ -15344,32 +15407,17 @@ begin
|
|
AssignSt.LHS:=CreateSubDeclNameExpr(El,AContext);
|
|
AssignSt.LHS:=CreateSubDeclNameExpr(El,AContext);
|
|
AssignSt.Expr:=Obj;
|
|
AssignSt.Expr:=Obj;
|
|
Result:=AssignSt;
|
|
Result:=AssignSt;
|
|
- if (coShortRefGlobals in Options) and (AContext is TFunctionContext) then
|
|
|
|
|
|
+ if coShortRefGlobals in Options then
|
|
begin
|
|
begin
|
|
- GlobalCtx:=AContext;
|
|
|
|
- while (GlobalCtx.PasElement is TPasMembersType) do
|
|
|
|
- GlobalCtx:=GlobalCtx.Parent;
|
|
|
|
- if (GlobalCtx<>AContext) and (GlobalCtx is TFunctionContext) then
|
|
|
|
- begin
|
|
|
|
- // add to GlobalCtx: var $lt = {}
|
|
|
|
- // add to local context: this.TypeName = $lt
|
|
|
|
- if not (GlobalCtx.JSElement is TJSSourceElements) then
|
|
|
|
- RaiseNotSupported(El,AContext,20200926181516,GetObjName(GlobalCtx.JSElement));
|
|
|
|
- Src:=TJSSourceElements(GlobalCtx.JSElement);
|
|
|
|
- JSName:=TFunctionContext(AContext).CreateLocalIdentifier(GetBIName(pbivnLocalTypeRef));
|
|
|
|
- AssignSt.Expr:=CreatePrimitiveDotExpr(JSName,El);
|
|
|
|
-
|
|
|
|
- VarSt:=CreateVarStatement(JSName,Obj,El);
|
|
|
|
- AddToSourceElements(Src,VarSt);
|
|
|
|
- TFunctionContext(GlobalCtx).AddLocalVar(JSName,El,cvkGlobal,false);
|
|
|
|
- end
|
|
|
|
- else
|
|
|
|
- begin
|
|
|
|
- // var $lt = this.TypeName = {}
|
|
|
|
- JSName:=TFunctionContext(AContext).CreateLocalIdentifier(GetBIName(pbivnLocalTypeRef));
|
|
|
|
- TFunctionContext(AContext).AddLocalVar(JSName,El,cvkGlobal,false);
|
|
|
|
- Result:=CreateVarStatement(JSName,AssignSt,El);
|
|
|
|
- end;
|
|
|
|
|
|
+ SectionContext:=TSectionContext(AContext.GetMainSectionContext);
|
|
|
|
+ JSName:=SectionContext.GetLocalName(El,[cvkGlobal]);
|
|
|
|
+ if JSName='' then
|
|
|
|
+ RaiseNotSupported(El,AContext,20200926232620);
|
|
|
|
+ // $lt = this.TypeName = {}
|
|
|
|
+ AssignSt:=TJSSimpleAssignStatement(CreateElement(TJSSimpleAssignStatement,El));
|
|
|
|
+ AssignSt.LHS:=CreatePrimitiveDotExpr(JSName,El);
|
|
|
|
+ AssignSt.Expr:=Result;
|
|
|
|
+ Result:=AssignSt;
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -16963,11 +17011,9 @@ var
|
|
SectionCtx: TSectionContext;
|
|
SectionCtx: TSectionContext;
|
|
begin
|
|
begin
|
|
if JS=nil then exit;
|
|
if JS=nil then exit;
|
|
- SectionCtx:=TSectionContext(aContext.GetContextOfType(TSectionContext));
|
|
|
|
|
|
+ SectionCtx:=TSectionContext(aContext.GetMainSectionContext);
|
|
if SectionCtx=nil then
|
|
if SectionCtx=nil then
|
|
RaiseNotSupported(PosEl,aContext,20200606142555);
|
|
RaiseNotSupported(PosEl,aContext,20200606142555);
|
|
- if SectionCtx.Parent is TSectionContext then
|
|
|
|
- SectionCtx:=TSectionContext(SectionCtx.Parent);
|
|
|
|
SectionCtx.AddHeaderStatement(JS);
|
|
SectionCtx.AddHeaderStatement(JS);
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -24220,7 +24266,7 @@ begin
|
|
RaiseNotSupported(El,AContext,20200609230526,GetObjPath(El));
|
|
RaiseNotSupported(El,AContext,20200609230526,GetObjPath(El));
|
|
Result:=Result+'.'+TransformElToJSName(El,AContext);
|
|
Result:=Result+'.'+TransformElToJSName(El,AContext);
|
|
if ShortRefGlobals then
|
|
if ShortRefGlobals then
|
|
- Result:=CreateGlobalAlias(El,Result,AContext);
|
|
|
|
|
|
+ Result:=CreateGlobalAliasForeign(El,Result,AContext);
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TPasToJSConverter.CreateProcedureCall(var Call: TJSCallExpression;
|
|
procedure TPasToJSConverter.CreateProcedureCall(var Call: TJSCallExpression;
|
|
@@ -25332,7 +25378,7 @@ var
|
|
DelaySrc: TJSSourceElements;
|
|
DelaySrc: TJSSourceElements;
|
|
DelayFuncContext: TFunctionContext;
|
|
DelayFuncContext: TFunctionContext;
|
|
Call: TJSCallExpression;
|
|
Call: TJSCallExpression;
|
|
- JSParentName: String;
|
|
|
|
|
|
+ JSParentName, JSName: String;
|
|
FunDecl: TJSFunctionDeclarationStatement;
|
|
FunDecl: TJSFunctionDeclarationStatement;
|
|
Src: TJSSourceElements;
|
|
Src: TJSSourceElements;
|
|
FuncContext: TFunctionContext;
|
|
FuncContext: TFunctionContext;
|
|
@@ -25345,6 +25391,7 @@ var
|
|
NewFields, Vars, Methods: TFPList;
|
|
NewFields, Vars, Methods: TFPList;
|
|
ok, IsComplex, SpecializeDelay: Boolean;
|
|
ok, IsComplex, SpecializeDelay: Boolean;
|
|
VarSt: TJSVariableStatement;
|
|
VarSt: TJSVariableStatement;
|
|
|
|
+ AssignSt: TJSSimpleAssignStatement;
|
|
begin
|
|
begin
|
|
Result:=nil;
|
|
Result:=nil;
|
|
if El.Name='' then
|
|
if El.Name='' then
|
|
@@ -25413,6 +25460,18 @@ begin
|
|
FuncContext.ThisVar.Element:=El;
|
|
FuncContext.ThisVar.Element:=El;
|
|
FuncContext.ThisVar.Kind:=cvkGlobal;
|
|
FuncContext.ThisVar.Kind:=cvkGlobal;
|
|
|
|
|
|
|
|
+ if coShortRefGlobals in Options then
|
|
|
|
+ begin
|
|
|
|
+ // $lt = this;
|
|
|
|
+ JSName:=AContext.GetLocalName(El,[cvkGlobal]);
|
|
|
|
+ if JSName='' then
|
|
|
|
+ RaiseNotSupported(El,AContext,20200926235501);
|
|
|
|
+ AssignSt:=TJSSimpleAssignStatement(CreateElement(TJSSimpleAssignStatement,El));
|
|
|
|
+ AssignSt.LHS:=CreatePrimitiveDotExpr(JSName,El);
|
|
|
|
+ AssignSt.Expr:=CreatePrimitiveDotExpr('this',El);
|
|
|
|
+ AddToSourceElements(Src,AssignSt);
|
|
|
|
+ end;
|
|
|
|
+
|
|
// init fields
|
|
// init fields
|
|
NewFields:=TFPList.Create;
|
|
NewFields:=TFPList.Create;
|
|
Vars:=TFPList.Create;
|
|
Vars:=TFPList.Create;
|
|
@@ -25725,7 +25784,7 @@ begin
|
|
Result:=GetBIName(pbivnModules)+'.'+Result;
|
|
Result:=GetBIName(pbivnModules)+'.'+Result;
|
|
|
|
|
|
if coShortRefGlobals in Options then
|
|
if coShortRefGlobals in Options then
|
|
- Result:=CreateGlobalAlias(El,Result,AContext);
|
|
|
|
|
|
+ Result:=CreateGlobalAliasForeign(El,Result,AContext);
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -25972,7 +26031,7 @@ begin
|
|
Result:=TransformToJSName(Arg,Result,true,AContext);
|
|
Result:=TransformToJSName(Arg,Result,true,AContext);
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TPasToJSConverter.CreateGlobalAlias(El: TPasElement; JSPath: string;
|
|
|
|
|
|
+function TPasToJSConverter.CreateGlobalAliasForeign(El: TPasElement; JSPath: string;
|
|
AContext: TConvertContext): string;
|
|
AContext: TConvertContext): string;
|
|
var
|
|
var
|
|
ElModule, MyModule: TPasModule;
|
|
ElModule, MyModule: TPasModule;
|
|
@@ -25998,9 +26057,7 @@ begin
|
|
else
|
|
else
|
|
begin
|
|
begin
|
|
// El is from another unit
|
|
// El is from another unit
|
|
- SectionContext:=TSectionContext(AContext.GetContextOfType(TSectionContext));
|
|
|
|
- if SectionContext.Parent is TSectionContext then
|
|
|
|
- SectionContext:=TSectionContext(SectionContext.Parent);
|
|
|
|
|
|
+ SectionContext:=TSectionContext(AContext.GetMainSectionContext);
|
|
|
|
|
|
FuncContext:=AContext.GetFunctionContext;
|
|
FuncContext:=AContext.GetFunctionContext;
|
|
if El is TPasModule then
|
|
if El is TPasModule then
|
|
@@ -26036,6 +26093,18 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+function TPasToJSConverter.CreateGlobalAliasNull(El: TPasElement;
|
|
|
|
+ Prefix: TPas2JSBuiltInName; SectionContext: TSectionContext
|
|
|
|
+ ): TFCLocalIdentifier;
|
|
|
|
+var
|
|
|
|
+ V: TJSVariableStatement;
|
|
|
|
+begin
|
|
|
|
+ // insert var $lt = null;
|
|
|
|
+ Result:=SectionContext.AddLocalVar(GetBIName(Prefix),El,cvkGlobal,true);
|
|
|
|
+ V:=CreateVarStatement(Result.Name,CreateLiteralNull(El),El);
|
|
|
|
+ AddHeaderStatement(V,El,SectionContext);
|
|
|
|
+end;
|
|
|
|
+
|
|
function TPasToJSConverter.ConvertPasElement(El: TPasElement;
|
|
function TPasToJSConverter.ConvertPasElement(El: TPasElement;
|
|
Resolver: TPas2JSResolver): TJSElement;
|
|
Resolver: TPas2JSResolver): TJSElement;
|
|
var
|
|
var
|