nodejsapp.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. var
  49. v: JSValue;
  50. begin
  51. v:=TNJSProcess.env[EnvVar];
  52. if isString(v) then
  53. Result:=String(v)
  54. else
  55. Result:='';
  56. end;
  57. function MyGetEnvironmentVariableCount: Integer;
  58. begin
  59. Result:=length(EnvNames);
  60. end;
  61. function MyGetEnvironmentString(Index: Integer): String;
  62. begin
  63. Result:=EnvNames[Index];
  64. end;
  65. { TNodeJSApplication }
  66. procedure TNodeJSApplication.DoRun;
  67. begin
  68. // Override in descendent classes.
  69. end;
  70. function TNodeJSApplication.GetConsoleApplication: boolean;
  71. begin
  72. Result:=true;
  73. end;
  74. function TNodeJSApplication.GetLocation: String;
  75. begin
  76. Result:=''; // ToDo ExtractFilePath(GetExeName);
  77. end;
  78. procedure TNodeJSApplication.GetEnvironmentList(List: TStrings;
  79. NamesOnly: Boolean);
  80. var
  81. Names: TStringDynArray;
  82. i: Integer;
  83. begin
  84. Names:=TJSObject.getOwnPropertyNames(TNJSProcess.env);
  85. for i:=0 to length(Names)-1 do
  86. begin
  87. if NamesOnly then
  88. List.Add(Names[i])
  89. else
  90. List.Add(Names[i]+'='+String(TNJSProcess.env[Names[i]]));
  91. end;
  92. end;
  93. procedure TNodeJSApplication.ShowException(E: Exception);
  94. begin
  95. if E<>nil then
  96. writeln(E.ClassName,': ',E.Message)
  97. else if ExceptObjectJS then
  98. writeln(ExceptObjectJS);
  99. end;
  100. procedure TNodeJSApplication.HandleException(Sender: TObject);
  101. begin
  102. if ExceptObject is Exception then
  103. ShowException(ExceptObject);
  104. inherited HandleException(Sender);
  105. end;
  106. initialization
  107. IsConsole:=true;
  108. OnParamCount:=@GetParamCount;
  109. OnParamStr:=@GetParamStr;
  110. ReloadEnvironmentStrings;
  111. OnGetEnvironmentVariable:=@MyGetEnvironmentVariable;
  112. OnGetEnvironmentVariableCount:=@MyGetEnvironmentVariableCount;
  113. OnGetEnvironmentString:=@MyGetEnvironmentString;
  114. end.