nodejsapp.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2017 by the Free Pascal development team
  4. TNodeJSApplication class.
  5. Initial implementation by Mattias Gaertner [email protected]
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit NodeJSApp;
  13. {$mode objfpc}
  14. interface
  15. uses
  16. NodeJS, Classes, SysUtils, Types, JS, CustApp;
  17. type
  18. { TNodeJSApplication }
  19. TNodeJSApplication = class(TCustomApplication)
  20. protected
  21. procedure DoRun; override;
  22. function GetConsoleApplication: boolean; override;
  23. function GetLocation: String; override;
  24. public
  25. procedure GetEnvironmentList(List: TStrings; NamesOnly: Boolean); override;
  26. procedure ShowException(E: Exception); override;
  27. procedure HandleException(Sender: TObject); override;
  28. end;
  29. procedure ReloadEnvironmentStrings;
  30. implementation
  31. var
  32. EnvNames: TStringDynArray;
  33. procedure ReloadEnvironmentStrings;
  34. begin
  35. EnvNames:=TJSObject.getOwnPropertyNames(TNJSProcess.env);
  36. end;
  37. function GetParamCount: longint;
  38. begin
  39. // argv contains as first element nodejs, and second the src.js
  40. Result:=length(TNJSProcess.argv)-2;
  41. end;
  42. function GetParamStr(Index: longint): String;
  43. begin
  44. // ParamStr(0) is the exename = the js file, which is the second element argv[1]
  45. Result:=TNJSProcess.argv[Index+1];
  46. end;
  47. function MyGetEnvironmentVariable(Const EnvVar: String): String;
  48. begin
  49. Result:=String(TNJSProcess.env[EnvVar]);
  50. end;
  51. function MyGetEnvironmentVariableCount: Integer;
  52. begin
  53. Result:=length(EnvNames);
  54. end;
  55. function MyGetEnvironmentString(Index: Integer): String;
  56. begin
  57. Result:=EnvNames[Index];
  58. end;
  59. { TNodeJSApplication }
  60. procedure TNodeJSApplication.DoRun;
  61. begin
  62. // Override in descendent classes.
  63. end;
  64. function TNodeJSApplication.GetConsoleApplication: boolean;
  65. begin
  66. Result:=true;
  67. end;
  68. function TNodeJSApplication.GetLocation: String;
  69. begin
  70. Result:=''; // ToDo ExtractFilePath(GetExeName);
  71. end;
  72. procedure TNodeJSApplication.GetEnvironmentList(List: TStrings;
  73. NamesOnly: Boolean);
  74. var
  75. Names: TStringDynArray;
  76. i: Integer;
  77. begin
  78. Names:=TJSObject.getOwnPropertyNames(TNJSProcess.env);
  79. for i:=0 to length(Names)-1 do
  80. begin
  81. if NamesOnly then
  82. List.Add(Names[i])
  83. else
  84. List.Add(Names[i]+'='+String(TNJSProcess.env[Names[i]]));
  85. end;
  86. end;
  87. procedure TNodeJSApplication.ShowException(E: Exception);
  88. begin
  89. if E<>nil then
  90. writeln(E.ClassName,': ',E.Message)
  91. else if ExceptObjectJS then
  92. writeln(ExceptObjectJS);
  93. end;
  94. procedure TNodeJSApplication.HandleException(Sender: TObject);
  95. begin
  96. if ExceptObject is Exception then
  97. ShowException(ExceptObject);
  98. inherited HandleException(Sender);
  99. end;
  100. initialization
  101. IsConsole:=true;
  102. OnParamCount:=@GetParamCount;
  103. OnParamStr:=@GetParamStr;
  104. ReloadEnvironmentStrings;
  105. OnGetEnvironmentVariable:=@MyGetEnvironmentVariable;
  106. OnGetEnvironmentVariableCount:=@MyGetEnvironmentVariableCount;
  107. OnGetEnvironmentString:=@MyGetEnvironmentString;
  108. end.