pastojs: library: export name

This commit is contained in:
mattias 2021-10-20 14:56:05 +02:00
parent 4546673a33
commit a4a469ae37
3 changed files with 79 additions and 5 deletions

View File

@ -986,7 +986,7 @@ type
Procedure TestLibrary_ExportFunc_NameIntFail;
Procedure TestLibrary_ExportFunc_IndexStringFail;
Procedure TestLibrary_ExportVar; // ToDo
Procedure TestLibrary_ExportLocFuncFail;
Procedure TestLibrary_ExportLocalFuncFail;
Procedure TestLibrary_Initialization_Finalization;
Procedure TestLibrary_ExportFuncOverloadFail;
Procedure TestLibrary_UnitExports;
@ -18853,7 +18853,7 @@ begin
ParseLibrary;
end;
procedure TTestResolver.TestLibrary_ExportLocFuncFail;
procedure TTestResolver.TestLibrary_ExportLocalFuncFail;
begin
StartLibrary(false);
Add([

View File

@ -2098,6 +2098,7 @@ type
// section
Function CreateImplementationSection(El: TPasModule; IntfContext: TInterfaceSectionContext): TJSFunctionDeclarationStatement; virtual;
Procedure CreateInitSection(El: TPasModule; Src: TJSSourceElements; AContext: TConvertContext); virtual;
Procedure CreateExportsSection(El: TPasLibrary; Src: TJSSourceElements; AContext: TConvertContext); virtual;
Procedure AddHeaderStatement(JS: TJSElement; PosEl: TPasElement; aContext: TConvertContext); virtual;
Procedure AddImplHeaderStatement(JS: TJSElement; PosEl: TPasElement; aContext: TConvertContext); virtual;
function AddDelayedInits(El: TPasModule; Src: TJSSourceElements; AContext: TConvertContext): boolean; virtual;
@ -8312,8 +8313,8 @@ begin
if Assigned(Lib.LibrarySection) then
AddToSourceElements(Src,ConvertDeclarations(Lib.LibrarySection,IntfContext));
HasImplCode:=AddDelayedInits(Lib,Src,IntfContext);
CreateExportsSection(Lib,Src,IntfContext);
CreateInitSection(Lib,Src,IntfContext);
// ToDo: append exports
end
else
begin // unit
@ -17999,6 +18000,74 @@ begin
raise Exception.Create('TPasToJSConverter.ConvertInitializationSection: finalization section is not supported');
end;
procedure TPasToJSConverter.CreateExportsSection(El: TPasLibrary;
Src: TJSSourceElements; AContext: TConvertContext);
var
ExportSymbols: TFPList;
aResolver: TPas2JSResolver;
ExpSt: TJSExportStatement;
i: Integer;
Symb: TPasExportSymbol;
Ref: TResolvedReference;
NamePath: String;
EvalValue: TResEvalValue;
ExpNameJS: TJSExportNameElement;
Decl: TPasElement;
begin
ExportSymbols:=El.LibrarySection.ExportSymbols;
if ExportSymbols.Count=0 then exit;
aResolver:=AContext.Resolver;
ExpSt:=TJSExportStatement(CreateElement(TJSExportStatement,El));
AddToSourceElements(Src,ExpSt);
for i:=0 to ExportSymbols.Count-1 do
begin
ExpNameJS:=ExpSt.ExportNames.AddElement;
Symb:=TObject(ExportSymbols[i]) as TPasExportSymbol;
// name
if Symb.NameExpr<>nil then
begin
RaiseNotSupported(Symb.NameExpr,AContext,20211020142210);
Decl:=nil;
end
else
begin
if not (Symb.CustomData is TResolvedReference) then
RaiseNotSupported(Symb,AContext,20211020142506,GetObjName(Symb.CustomData));
Ref:=TResolvedReference(Symb.CustomData);
Decl:=Ref.Declaration;
end;
NamePath:=CreateReferencePath(Decl,AContext,rpkPathAndName,true);
ExpNameJS.Name:=NamePath;
// alias
if Symb.ExportName<>nil then
begin
EvalValue:=aResolver.Eval(Symb.ExportName,[refConst]);
if EvalValue=nil then
RaiseNotSupported(Symb.ExportName,AContext,20211020144200);
case EvalValue.Kind of
{$ifdef FPC_HAS_CPSTRING}
revkString:
ExpNameJS.Alias:=TResEvalString(EvalValue).S;
{$endif}
revkUnicodeString:
ExpNameJS.Alias:=String(TResEvalUTF16(EvalValue).S);
else
RaiseNotSupported(Symb.ExportName,AContext,20211020144404);
end;
end
else
begin
if Decl.Name='' then
RaiseNotSupported(Symb,AContext,20211020144730);
ExpNameJS.Alias:=Decl.Name;
end;
end;
end;
procedure TPasToJSConverter.AddHeaderStatement(JS: TJSElement;
PosEl: TPasElement; aContext: TConvertContext);
var

View File

@ -911,6 +911,9 @@ type
// Library
Procedure TestLibrary_Empty;
Procedure TestLibrary_ExportFunc; // ToDo
// ToDo: export let as let fail
// ToDo: Procedure TestLibrary_ExportVar;
// ToDo: Procedure TestLibrary_Export_Index_Fail;
// ToDo: test delayed specialization init
// ToDo: analyzer
end;
@ -33802,14 +33805,16 @@ begin
'begin',
'end;',
'exports',
' Run,',
' run name ''Foo'';',
' Run;',
//' run name ''Foo'';',
//' test1.run name ''Test1Run'';',
'']);
ConvertLibrary;
CheckSource('TestLibrary_ExportFunc',
LinesToStr([ // statements
'this.Run = function (w) {',
'};',
'export { this.Run as Run };',
'']),
LinesToStr([
'']));