-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
307 lines (272 loc) · 7.56 KB
/
main.cpp
File metadata and controls
307 lines (272 loc) · 7.56 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
extern "C" {
#include <stdio.h>
#include "jsapi.h"
#include "../prtypes.h"
#include "../prassert.h"
#include "../prlink.h"
extern PR_IMPORT_DATA(JSObjectOps) js_ObjectOps;
}
#include "jsIScriptable.h"
typedef nsISupports* (*FactoryFun)(JSContext*);
static JSClass global_class = {
"global", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
};
struct FactoryStruct {
PRLibrary* lib;
FactoryFun create;
FactoryStruct(PRLibrary* _lib, FactoryFun _f) : lib(_lib), create(_f) {}
};
// this is the constructor for jsIScriptable objects
static JSBool
ExtensionsConstructor(JSContext *cx,JSObject *obj,uintN argc,jsval *argv,jsval *vp)
{
obj = JSVAL_TO_OBJECT(argv[-2]); // obj is now the factory
FactoryStruct* factory = (FactoryStruct*)JS_GetPrivate(cx,obj);
jsIScriptable* jsi = (jsIScriptable*)factory->create(cx);
return (jsIScriptable::ToJS(cx,jsi,vp) == NS_OK ? JS_TRUE : JS_FALSE);
}
static JSBool
Extensions_Convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
{
*vp = OBJECT_TO_JSVAL(obj);
return JS_TRUE;
}
static void
Extensions_Finalize(JSContext *cx, JSObject *obj)
{
FactoryStruct* factory;
factory = (FactoryStruct*)JS_GetPrivate(cx,obj);
if (factory) {
PR_UnloadLibrary(factory->lib);
delete factory;
}
}
static JSBool
Extensions_lookupProperty(JSContext *cx, JSObject *obj, jsid id,
JSObject **objp, JSProperty **propp
#if defined JS_THREADSAFE && defined DEBUG
, const char *file, uintN line
#endif
)
{
return JS_TRUE;
}
static JSBool
Extensions_defineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
JSPropertyOp getter, JSPropertyOp setter,
uintN attrs, JSProperty **propp)
{
return JS_FALSE;
}
static JSBool
Extensions_getAttributes(JSContext *cx, JSObject *obj, jsid id,
JSProperty *prop, uintN *attrsp)
{
return JS_FALSE;
}
static JSBool
Extensions_setAttributes(JSContext *cx, JSObject *obj, jsid id,
JSProperty *prop, uintN *attrsp)
{
return JS_TRUE;
}
static JSBool
Extensions_deleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
return JS_FALSE;
}
static JSBool
Extensions_defaultValue(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
{
return JS_TRUE;
}
PR_STATIC_CALLBACK(JSBool)
Extensions_getProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
return JS_TRUE;
}
PR_STATIC_CALLBACK(JSBool)
Extensions_setProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
return JS_TRUE;
}
static JSBool
Extensions_newEnumerate(JSContext *cx,JSObject *obj,JSIterateOp enum_op,jsval *statep,jsid *idp)
{
return JS_TRUE;
}
static JSBool
Extensions_checkAccess(JSContext *cx, JSObject *obj, jsid id,
JSAccessMode mode, jsval *vp, uintN *attrsp)
{
return JS_TRUE;
}
JSBool ExtensionsConstructor(JSContext*,JSObject*,uintN,jsval*,jsval*);
JSObjectOps Extensions_ops = {
/* Mandatory non-null function pointer members. */
NULL, /* newObjectMap */
NULL, /* destroyObjectMap */
Extensions_lookupProperty,
Extensions_defineProperty,
Extensions_getProperty,
Extensions_setProperty,
Extensions_getAttributes,
Extensions_setAttributes,
Extensions_deleteProperty,
Extensions_defaultValue,
Extensions_newEnumerate,
Extensions_checkAccess,
/* Optionally non-null members start here. */
NULL, /* thisObject */
NULL, /* dropProperty */
ExtensionsConstructor, /* call */
ExtensionsConstructor, /* construct */
NULL, /* xdrObject */
NULL, /* hasInstance */
};
JSObjectOps *
Extensions_getObjectOps(JSContext *cx, JSClass *clazz)
{
return &Extensions_ops;
}
static JSClass Extensions_class = {
"Extensions", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, Extensions_Convert, Extensions_Finalize,
Extensions_getObjectOps,
};
// resolving Extensions.M by assigning it to an object which each time it
// is called, constructs a new instance from M's factory.
static JSBool
Factory_Resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
JSObject **objp)
{
char *str;
JSObject *fact_obj;
if (!JSVAL_IS_STRING(id))
return JS_TRUE;
str = JS_GetStringBytes(JSVAL_TO_STRING(id));
// load factory.
PRLibrary* factory = PR_LoadLibrary(str);
if (factory == NULL)
return JS_FALSE;
FactoryFun fun = (FactoryFun)PR_FindSymbol(factory, "CreateInstance");
if (fun == NULL)
return JS_FALSE;
fact_obj = JS_DefineObject(cx, obj, str, &Extensions_class, NULL, 0);
JS_SetPrivate(cx,fact_obj,new FactoryStruct(factory,fun));
*objp = obj;
return JS_TRUE;
}
static JSClass Factory_class = {
"ExtensionsFactory",
JSCLASS_NEW_RESOLVE,
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
JS_EnumerateStub,(JSResolveOp)Factory_Resolve,JS_ConvertStub,JS_FinalizeStub
};
static JSBool
Print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
uintN i, n;
JSString *str;
for (i = n = 0; i < argc; i++) {
str = JS_ValueToString(cx, argv[i]);
if (!str)
return JS_FALSE;
printf("%s%s", i ? " " : "", JS_GetStringBytes(str));
n++;
}
if (n)
putchar('\n');
return JS_TRUE;
}
static void
Process(JSContext *cx, JSObject *obj, const char *filename)
{
JSScript *jsrc = JS_CompileFile(cx,obj,filename);
jsval rval;
if (jsrc == NULL) {
#ifdef DEBUG
printf("Failed compilation of %s\n",filename);
#endif
return;
}
if (!JS_ExecuteScript(cx,obj,jsrc,&rval)) {
#ifdef DEBUG
printf("Failed execution of %s\n",filename);
#endif
return;
}
}
static JSFunctionSpec helper_functions[] = {
/* name native nargs */
{"print", Print, 0},
{0}
};
static void
my_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
{
int i, j, k, n;
fputs("js: ", stderr);
if (!report) {
fprintf(stderr, "%s\n", message);
return;
}
if (report->filename)
fprintf(stderr, "%s, ", report->filename);
if (report->lineno)
fprintf(stderr, "line %u: ", report->lineno);
fputs(message, stderr);
if (!report->linebuf) {
putc('\n', stderr);
return;
}
fprintf(stderr, ":\n%s\n", report->linebuf);
n = report->tokenptr - report->linebuf;
for (i = j = 0; i < n; i++) {
if (report->linebuf[i] == '\t') {
for (k = (j + 8) & ~7; j < k; j++)
putc('.', stderr);
continue;
}
putc('.', stderr);
j++;
}
fputs("^\n", stderr);
}
int main(int argc, char* const* argv)
{
JSRuntime *rt;
JSContext *cx;
JSObject *glob, *ext;
const char *filename;
if (argc >= 2)
filename = (const char *) argv[1];
else
filename = (const char *) "test.js";
// JS init
rt = JS_Init(8L * 1024L * 1024L);
if (!rt)
return 0;
cx = JS_NewContext(rt, 8192);
PR_ASSERT(cx);
JS_SetErrorReporter(cx, my_ErrorReporter);
glob = JS_NewObject(cx, &global_class, NULL, NULL);
PR_ASSERT(glob);
if (!JS_InitStandardClasses(cx, glob))
return -1;
if (!JS_DefineFunctions(cx, glob, helper_functions))
return -1;
ext = JS_DefineObject(cx, glob, "Extensions", &Factory_class, NULL, 0);
if (!ext)
return -1;
Extensions_ops.newObjectMap = js_ObjectOps.newObjectMap;
Extensions_ops.destroyObjectMap = js_ObjectOps.destroyObjectMap;
Process(cx, glob, filename);
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 1;
}