Browse Source

pastojs: library: create and call main

mattias 3 years ago
parent
commit
0aecf4d89f
3 changed files with 30 additions and 10 deletions
  1. 1 0
      packages/pastojs/src/fppas2js.pp
  2. 27 8
      packages/pastojs/tests/tcmodules.pas
  3. 2 2
      utils/pas2js/dist/rtl.js

+ 1 - 0
packages/pastojs/src/fppas2js.pp

@@ -17991,6 +17991,7 @@ begin
   RootContext:=AContext.GetRootContext as TRootContext;
   // add initialization section
   if Assigned(El.InitializationSection)
+      or (El is TPasLibrary) // the begin..end is optional in a library, but the js it always needed
       or (length(RootContext.GlobalClassMethods)>0) then
     AddToSourceElements(Src,ConvertInitializationSection(El,AContext));
   // finalization: not supported

+ 27 - 8
packages/pastojs/tests/tcmodules.pas

@@ -1982,8 +1982,17 @@ var
   InitName: String;
   LastNode: TJSElement;
   Arg: TJSArrayLiteralElement;
+  IsProg, IsLib: Boolean;
 begin
   if SkipTests then exit;
+
+  IsProg:=false;
+  IsLib:=false;
+  if Module is TPasProgram then
+    IsProg:=true
+  else if Module is TPasLibrary then
+    IsLib:=true;
+
   try
     FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
   except
@@ -2018,9 +2027,9 @@ begin
   AssertNotNull('module name param',Arg.Expr);
   ModuleNameExpr:=Arg.Expr as TJSLiteral;
   AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
-  if Module is TPasProgram then
+  if IsProg then
     AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
-  else if Module is TPasLibrary then
+  else if IsLib then
     AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
   else
     AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
@@ -2038,13 +2047,15 @@ begin
   CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
 
   // search for $mod.$init or $mod.$main - the last statement
-  if (Module is TPasProgram) or (Module is TPasLibrary) then
+  if IsProg or IsLib then
     begin
     InitName:='$main';
     AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
     end
   else
     InitName:='$init';
+  InitAssign:=nil;
+  InitFunction:=nil;
   FJSInitBody:=nil;
   if JSModuleSrc.Statements.Count>0 then
     begin
@@ -2057,7 +2068,7 @@ begin
         InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
         FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
         end
-      else if (Module is TPasProgram) or (Module is TPasLibrary) then
+      else if IsProg or IsLib then
         CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
       end;
     end;
@@ -2125,6 +2136,7 @@ procedure TCustomTestModule.CheckSource(Msg, Statements: String;
   InitStatements: string; ImplStatements: string);
 var
   ActualSrc, ExpectedSrc, InitName: String;
+  IsProg, IsLib: Boolean;
 begin
   ActualSrc:=JSToStr(JSModuleSrc);
   if coUseStrict in Converter.Options then
@@ -2142,9 +2154,15 @@ begin
       +'};'+LineEnding;
 
   // program main or unit initialization
-  if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
+  IsProg:=false;
+  IsLib:=false;
+  if Module is TPasProgram then
+    IsProg:=true
+  else if Module is TPasLibrary then
+    IsLib:=true;
+  if IsProg or IsLib or (Trim(InitStatements)<>'') then
     begin
-    if (Module is TPasProgram) or (Module is TPasLibrary) then
+    if IsProg or IsLib then
       InitName:='$main'
     else
       InitName:='$init';
@@ -2156,6 +2174,7 @@ begin
 
   //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
   //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
+  //writeln('TCustomTestModule.CheckSource ',ActualSrc);
   CheckDiff(Msg,ExpectedSrc,ActualSrc);
 end;
 
@@ -33777,8 +33796,6 @@ end;
 
 procedure TTestModule.TestLibrary_ExportFunc;
 begin
-  exit;
-
   StartLibrary(false);
   Add([
   'procedure Run(w: word);',
@@ -33791,6 +33808,8 @@ begin
   ConvertLibrary;
   CheckSource('TestLibrary_ExportFunc',
     LinesToStr([ // statements
+    'this.Run = function (w) {',
+    '};',
     '']),
     LinesToStr([
     '']));

+ 2 - 2
utils/pas2js/dist/rtl.js

@@ -136,9 +136,9 @@ var rtl = {
       if (!module) rtl.error('rtl.run module "'+module_name+'" missing');
       rtl.loadintf(module);
       rtl.loadimpl(module);
-      if (module_name=='program'){
+      if ((module_name=='program') || (module_name=='library')){
         if (rtl.debug_load_units) rtl.debug('running $main');
-        var r = pas.program.$main();
+        var r = pas[module_name].$main();
         if (rtl.isNumber(r)) rtl.exitcode = r;
       }
     } catch(re) {