pastojs: add rtl.run() on platform module

This commit is contained in:
mattias 2022-02-08 22:13:28 +01:00
parent fe39f9ca21
commit c0c6ce5422
3 changed files with 46 additions and 10 deletions

View File

@ -2103,7 +2103,7 @@ type
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;
Function AddLibraryRun(El: TPasLibrary; ModuleName: string; Src: TJSSourceElements; AContext: TConvertContext): TJSCallExpression; virtual;
Function AddRTLRun(El: TPasModule; ModuleName: string; Src: TJSSourceElements; AContext: TConvertContext): TJSCallExpression; 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;
@ -8431,11 +8431,15 @@ begin
StoreImplJSLocals(ModScope,IntfContext);
if El is TPasLibrary then
begin // library
begin
// library: rtl.run('library');
Lib:=TPasLibrary(El);
AddLibraryRun(Lib,ModuleName,OuterSrc,AContext);
AddRTLRun(Lib,ModuleName,OuterSrc,AContext);
CreateExportsSection(Lib,OuterSrc,AContext);
end;
end
else if (El is TPasProgram) and (Globals.TargetPlatform in [PlatformNodeJS,PlatformModule]) then
// program: rtl.run();
AddRTLRun(El,'',OuterSrc,AContext);
ok:=true;
finally
@ -18224,7 +18228,7 @@ begin
end;
end;
function TPasToJSConverter.AddLibraryRun(El: TPasLibrary; ModuleName: string;
function TPasToJSConverter.AddRTLRun(El: TPasModule; ModuleName: string;
Src: TJSSourceElements; AContext: TConvertContext): TJSCallExpression;
var
Call: TJSCallExpression;
@ -18236,8 +18240,9 @@ begin
AddToSourceElements(Src,Call);
Call.Expr:=CreateMemberExpression([GetBIName(pbivnRTL),'run']);
// add module name parameter
Call.AddArg(CreateLiteralString(El,ModuleName));
if ModuleName<>'' then
// add module name parameter
Call.AddArg(CreateLiteralString(El,ModuleName));
Result:=Call;
end;

View File

@ -2805,9 +2805,6 @@ begin
FResources.DoneUnit(aFile.isMainFile);
EmitJavaScript(aFile,aFileWriter);
if aFile.IsMainFile and (TargetPlatform=PlatformNodeJS) then
aFileWriter.WriteFile('rtl.run();'+LineEnding,aFile.UnitFilename);
if isSingleFile or aFile.isMainFile then
begin
if aFile.IsMainFile then

View File

@ -137,6 +137,7 @@ type
TTestCLI_UnitSearch = class(TCustomTestCLI)
protected
procedure CheckLinklibProgramSrc(Msg,Header: string);
procedure CheckFullSource(Msg, Filename, ExpectedSrc: string);
published
procedure TestUS_CreateRelativePath;
@ -146,6 +147,7 @@ type
procedure TestUS_Program_FU;
procedure TestUS_Program_FU_o;
procedure TestUS_Program_FE_o;
procedure TestUS_PlatformModule_Program;
// include files
procedure TestUS_IncludeSameDir;
@ -625,6 +627,8 @@ var
aFile: TCLIFile;
begin
aFile:=FindFile('test1.js');
if aFile=nil then
Fail(Msg+' file not found test1.js');
CheckDiff(Msg,
LinesToStr([
#$EF#$BB#$BF+Header,
@ -640,6 +644,17 @@ begin
aFile.Source);
end;
procedure TTestCLI_UnitSearch.CheckFullSource(Msg, Filename, ExpectedSrc: string
);
var
aFile: TCLIFile;
begin
aFile:=FindFile(Filename);
if aFile=nil then
Fail(Msg+' file not found "'+Filename+'"');
CheckDiff(Msg,ExpectedSrc,aFile.Source);
end;
procedure TTestCLI_UnitSearch.TestUS_CreateRelativePath;
procedure DoTest(Filename, BaseDirectory, Expected: string;
@ -748,6 +763,25 @@ begin
AssertNotNull('foo.js not found',FindFile('foo.js'));
end;
procedure TTestCLI_UnitSearch.TestUS_PlatformModule_Program;
begin
AddUnit('system.pp',[''],['']);
AddFile('test1.pas',[
'begin',
'end.']);
Compile(['-Tmodule','-va','test1.pas']);
CheckFullSource('TestUS_PlatformModule_Library','test1.js',
LinesToStr([
#$EF#$BB#$BF+'rtl.module("program",["system"],function () {',
' "use strict";',
' var $mod = this;',
' $mod.$main = function () {',
' };',
'});',
'rtl.run();',
'']));
end;
procedure TTestCLI_UnitSearch.TestUS_IncludeSameDir;
begin
AddUnit('system.pp',[''],['']);