-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPJS_Context.c
More file actions
287 lines (223 loc) · 6.75 KB
/
PJS_Context.c
File metadata and controls
287 lines (223 loc) · 6.75 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "JavaScript_Env.h"
#include "PJS_Types.h"
#include "PJS_Common.h"
#include "PJS_Function.h"
#include "PJS_Context.h"
#include "PJS_Runtime.h"
#include "PJS_Class.h"
#include "PJS_PerlArray.h"
#include "PJS_PerlHash.h"
#include "PJS_PerlSub.h"
#define NEED_mro_get_linear_isa /* or NEED_mro_get_linear_isa_GLOBAL */
#include "mro_compat.h"
/* Global class, does nothing */
static JSClass global_class = {
"global", 0,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
PJS_Function * PJS_GetFunctionByName(PJS_Context *cx, const char *name) {
PJS_Function *func = NULL;
SV **sv;
IV tmp;
sv = hv_fetch(cx->function_by_name, name, strlen(name), 0);
if (sv == NULL) {
return NULL;
}
tmp = SvIV((SV *) SvRV(*sv));
func = INT2PTR(PJS_Function *, tmp);
return func;
}
PJS_Class *
PJS_GetClassByName(PJS_Context *cx, const char *name) {
PJS_Class *cls = NULL;
SV **sv;
IV tmp;
sv = hv_fetch(cx->class_by_name, name, strlen(name), 0);
if (sv == NULL) {
return NULL;
}
tmp = SvIV((SV *) SvRV(*sv));
cls = INT2PTR(PJS_Class *, tmp);
return cls;
}
PJS_Class *
PJS_GetClassByPackage(PJS_Context *cx, const char *pkg) {
PJS_Class *cls = NULL;
SV **sv;
IV tmp;
sv = hv_fetch(cx->class_by_package, pkg, strlen(pkg), 0);
if (sv == NULL) {
HV *stash;
AV *isa;
int i;
const char *tmp_pkg;
STRLEN l;
/* Non found, try walking the ISA */
stash = gv_stashpv(pkg, 0);
if (stash == NULL) {
return NULL;
}
isa = mro_get_linear_isa(stash);
if (isa == NULL) {
return NULL;
}
for (i = 0; sv == NULL && i < av_len(isa); i++) {
sv = av_fetch(isa, i, 0);
if (sv != NULL) {
tmp_pkg = SvPV(*sv, l);
sv = hv_fetch(cx->class_by_package, tmp_pkg, l, 0);
}
}
/* Still null means we didn't find anything in ISA */
if (sv == NULL) {
return NULL;
}
}
tmp = SvIV((SV *) SvRV(*sv));
cls = INT2PTR(PJS_Class *, tmp);
return cls;
}
/*
Create PJS_Context structure
*/
PJS_Context * PJS_CreateContext(PJS_Runtime *rt) {
PJS_Context *pcx;
JSObject *obj;
Newz(1, pcx, 1, PJS_Context);
if (pcx == NULL) {
croak("Failed to allocate memory for PJS_Context");
}
/*
The 'stack size' param here isn't actually the stack size, it's
the "chunk size of the stack pool--an obscure memory management
tuning knob"
http://groups.google.com/group/mozilla.dev.tech.js-engine/browse_thread/thread/be9f404b623acf39
*/
pcx->cx = JS_NewContext(rt->rt, 8192);
if(pcx->cx == NULL) {
Safefree(pcx);
croak("Failed to create JSContext");
}
JS_SetOptions(pcx->cx, JSOPTION_DONT_REPORT_UNCAUGHT);
obj = JS_NewObject(pcx->cx, &global_class, NULL, NULL);
if (JS_InitStandardClasses(pcx->cx, obj) == JS_FALSE) {
PJS_DestroyContext(pcx);
croak("Standard classes not loaded properly.");
}
pcx->function_by_name = newHV();
pcx->class_by_name = newHV();
pcx->class_by_package = newHV();
if (PJS_InitPerlArrayClass(pcx, obj) == JS_FALSE) {
PJS_DestroyContext(pcx);
croak("Perl classes not loaded properly.");
}
if (PJS_InitPerlHashClass(pcx, obj) == JS_FALSE) {
PJS_DestroyContext(pcx);
croak("Perl classes not loaded properly.");
}
if (PJS_InitPerlSubClass(pcx, obj) == JS_FALSE) {
PJS_DestroyContext(pcx);
croak("Perl class 'PerlSub' not loaded properly.");
}
pcx->rt = rt;
/* Add context to context list */
pcx->next = rt->list;
rt->list = pcx;
JS_SetContextPrivate(pcx->cx, (void *) pcx);
return pcx;
}
/*
Free memory occupied by PJS_Context structure
*/
void PJS_DestroyContext(PJS_Context *pcx) {
if (pcx == NULL) {
return;
}
if (pcx->function_by_name) {
SvREFCNT_dec(pcx->function_by_name);
pcx->function_by_name = NULL;
}
if (pcx->class_by_name) {
SvREFCNT_dec(pcx->class_by_name);
pcx->class_by_name = NULL;
}
if (pcx->class_by_package) {
SvREFCNT_dec(pcx->class_by_package);
pcx->class_by_package = NULL;
}
/* Destory context */
if (pcx->cx) {
JS_DestroyContext(pcx->cx);
pcx->cx = NULL;
}
Safefree(pcx);
}
PJS_Function *
PJS_DefineFunction(PJS_Context *inContext, const char *functionName, SV *perlCallback) {
PJS_Function *function;
JSContext *js_context = inContext->cx;
SV *sv;
if (PJS_GetFunctionByName(inContext, functionName) != NULL) {
warn("Function named '%s' is already defined in the context", functionName);
return NULL;
}
if ((function = PJS_CreateFunction(functionName, perlCallback)) == NULL) {
return NULL;
}
/* Add the function to the javascript context */
if (JS_DefineFunction(js_context, JS_GetGlobalObject(js_context), functionName, PJS_invoke_perl_function, 0, 0) == JS_FALSE) {
warn("Failed to define function");
PJS_DestroyFunction(function);
return NULL;
}
sv = newSV(0);
sv_setref_pv(sv, "JavaScript::PerlFunction", (void*) function);
if (functionName != NULL) {
SvREFCNT_inc(sv);
hv_store(inContext->function_by_name, functionName, strlen(functionName), sv, 0);
}
return function;
}
void PJS_set_thread_stack_limit(JSContext *cx, SV *size){
JS_SetThreadStackLimit(cx, (jsuword*) SvUV(size) );
}
/* Called by context when a branch occurs */
JSBool PJS_branch_handler(JSContext *cx, JSScript *script) {
dSP;
PJS_Context *pcx;
SV *rv;
I32 rc = 0;
JSBool status = JS_TRUE;
pcx = PJS_GET_CONTEXT(cx);
if (pcx != NULL && pcx->branch_handler) {
ENTER ;
SAVETMPS ;
PUSHMARK(SP);
rc = perl_call_sv(SvRV(pcx->branch_handler), G_SCALAR | G_EVAL);
SPAGAIN;
rv = POPs;
if (!SvTRUE(rv)) {
status = JS_FALSE;
}
if (SvTRUE(ERRSV)) {
sv_setsv(ERRSV, &PL_sv_undef);
status = JS_FALSE;
}
PUTBACK;
FREETMPS;
LEAVE;
}
return status;
}
/*JSContext *
PJS_GetJSContext(PJS_Context *fromContext) {
if (fromContext != NULL) {
return fromContext->cx;
}
return NULL;
}*/