forked from ivere27/node-delphi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TobyPascal.pas
151 lines (124 loc) · 3.68 KB
/
TobyPascal.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
unit TobyPascal;
interface
uses
Classes, SysUtils, math, Windows;
type
TobyOnloadCB = procedure (isolate: Pointer); cdecl;
TobyOnunloadCB = procedure (isolate: Pointer; exitCode: integer); cdecl;
TobyHostcallCB = function (name, value: PAnsiChar):PAnsiChar; cdecl;
TToby = class
private
started : boolean;
H: HINST;
constructor Init;
public
onLoad : TobyOnloadCB;
onUnload : TobyOnunloadCB;
onHostCall : TobyHostcallCB;
InputPipeRead : THandle;
InputPipeWrite : THandle;
OutputPipeRead : THandle;
OutputPipeWrite : THandle;
ErrorPipeRead : THandle;
ErrorPipeWrite : THandle;
function start(processName, userScript: PAnsiChar) : boolean;
function run(source: PAnsiChar) : PAnsiChar;
procedure emit(name, value: PAnsiChar);
class function Create: TToby;
end;
implementation
var
Singleton : TToby = nil;
security : TSecurityAttributes;
tobyInit:procedure (processName, userScript: PAnsiChar;
tobyOnLoad: TobyOnloadCB;
tobyOnUnload: TobyOnunloadCB;
tobyHostCall: TobyHostcallCB); cdecl;
tobyJSCompile:function (source, dest: PAnsiChar; n: integer):integer; cdecl;
tobyJSCall:function (name, value, dest: PAnsiChar; n: integer):integer; cdecl;
tobyJSEmit: function(name, value: PAnsiChar):integer; cdecl;
procedure _OnLoad(isolate: Pointer); cdecl;
begin
//writeln(':: default tobyOnLoad called');
end;
procedure _OnUnload(isolate: Pointer; exitCode: integer); cdecl;
begin
//writeln(':: default _OnUnload called');
end;
function _OnHostCall(key,value: PAnsiChar):PAnsiChar; cdecl;
begin
//writeln(':: default tobyHostCall called. ', key, ' : ',value);
exit('');
end;
constructor TToby.Init;
var
stdout: THandle;
begin
// in case of 'git bash'(mingw) in windows
stdout := GetStdHandle(Std_Output_Handle);
Win32Check(stdout <> Invalid_Handle_Value);
if (stdout = 0) then
begin
With security do
begin
nlength := SizeOf(TSecurityAttributes) ;
binherithandle := true;
lpsecuritydescriptor := nil;
end;
// FIXME : check returns
CreatePipe(InputPipeRead, InputPipeWrite, @security, 0);
CreatePipe(OutputPipeRead,OutputPipeWrite, @security, 0);
CreatePipe(ErrorPipeRead, ErrorPipeWrite, @security, 0);
SetStdHandle(Std_Input_Handle, InputPipeRead);
SetStdHandle(Std_Output_Handle, OutputPipeWrite);
SetStdHandle(Std_Error_Handle, ErrorPipeWrite);
end;
// dynamically load the dll.
H := LoadLibrary('tobynode.dll');
if H<=0 then
begin
raise Exception.Create('tobynode.dll error!');
exit;
end;
@tobyInit := GetProcAddress(H, Pchar('tobyInit'));
@tobyJSCompile := GetProcAddress(H, Pchar('tobyJSCompile'));
@tobyJSCall := GetProcAddress(H, Pchar('tobyJSCall'));
@tobyJSEmit := GetProcAddress(H, Pchar('tobyJSEmit'));
started := false;
// set default.
onLoad := @_OnLoad;
onUnload := @_OnUnload;
onHostCall := @_OnHostCall;
inherited Create;
end;
class function TToby.Create: TToby;
begin
if Singleton = nil then
Singleton := TToby.Init;
Result := Singleton;
end;
function TToby.start(processName, userScript: PAnsiChar) : boolean;
begin
if started = false then
begin
started := true;
tobyInit(processName, userScript, onLoad, onUnload, onHostCall);
exit(true);
end;
exit(false);
end;
procedure TToby.emit(name, value: PAnsiChar);
begin
tobyJSEmit(name, value);
end;
function TToby.run(source: PAnsiChar) : PAnsiChar;
var
dest: Array[0..1024] of AnsiChar;
ret: integer;
begin
ret := tobyJSCompile(source, dest, Length(dest));
if (ret < 0) then
writeln('** Error ', ret, ' ', dest);
exit(dest);
end;
end.