-
Notifications
You must be signed in to change notification settings - Fork 1
/
language_js.c
319 lines (282 loc) · 8.95 KB
/
language_js.c
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
308
309
310
311
312
313
314
315
316
317
318
#include <stdbool.h>
#include <string.h>
#ifdef _MSC_VER
# define XP_WIN
#else
# define XP_UNIX
#endif
#include <jsapi.h>
#include <ffi.h>
#include "language.h"
#include "util.h"
#include "dict.h"
#include "function.h"
typedef struct _js_internal {
language_t*li;
JSRuntime *rt;
JSContext *cx;
JSObject *global;
char*buffer;
char noerrors;
dict_t* jsfunction_to_function;
} js_internal_t;
static JSClass global_class = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS };
static void error_callback(JSContext *cx, const char *message, JSErrorReport *report) {
js_internal_t*js = JS_GetContextPrivate(cx);
if(js->noerrors)
return;
language_error(js->li, "line %u: %s\n", (unsigned int) report->lineno, message);
}
static bool initialize_js(language_t*li, size_t mem_size)
{
if(li->internal)
return true; //already initialized
li->internal = calloc(1, sizeof(js_internal_t));
js_internal_t*js = (js_internal_t*)li->internal;
js->li = li;
log_dbg("[js] initializing: allocating runtime with %dMB of memory", mem_size / 1048576);
js->rt = JS_NewRuntime(mem_size);
if (js->rt == NULL)
return false;
js->cx = JS_NewContext(js->rt, 8192);
if (js->cx == NULL)
return false;
JS_SetContextPrivate(js->cx, js);
JS_SetOptions(js->cx, JSOPTION_VAROBJFIX | JSOPTION_JIT);
JS_SetVersion(js->cx, JSVERSION_LATEST);
JS_SetErrorReporter(js->cx, error_callback);
js->global = JS_NewCompartmentAndGlobalObject(js->cx, &global_class, NULL);
if (js->global == NULL)
return false;
/* Populate the global object with the standard globals, like Object and Array. */
if (!JS_InitStandardClasses(js->cx, js->global))
return false;
js->buffer = malloc(65536);
js->jsfunction_to_function = dict_new(&ptr_type);
return true;
}
static value_t* jsval_to_value(const js_internal_t*js, jsval v)
{
// Also see JS_ConvertArguments()
int32 d;
if(JSVAL_IS_NULL(v)) {
return value_new_void();
} else if(JSVAL_IS_VOID(v)) {
return value_new_void();
} else if(JSVAL_IS_INT(v)) {
return value_new_int32(JSVAL_TO_INT(v));
} else if(JSVAL_IS_NUMBER(v)) {
return value_new_float32(JSVAL_TO_DOUBLE(v));
} else if(JSVAL_IS_STRING(v)) {
JSString*s = JSVAL_TO_STRING(v);
char*cstr = JS_EncodeString(js->cx, s);
return value_new_string(cstr);
} else if(JSVAL_IS_BOOLEAN(v)) {
return value_new_boolean(JSVAL_TO_BOOLEAN(v));
} else if(JSVAL_IS_OBJECT(v)) {
JSObject * obj = JSVAL_TO_OBJECT(v);
jsuint length;
bool ret = JS_GetArrayLength(js->cx, obj, &length);
if(!ret) {
language_error(js->li, "Can't determine array length\n");
return NULL;
}
value_t*a = array_new();
int i;
for(i=0;i<length;i++) {
jsval entry;
ret = JS_GetElement(js->cx, obj, i, &entry);
if(!ret) {
language_error(js->li, "Can't determine array length\n");
return NULL;
}
array_append(a, jsval_to_value(js, entry));
}
return a;
} else {
/* TODO: arrays */
language_error(js->li, "Can't convert javascript type to a value.\n");
return NULL;
}
}
static value_t* js_argv_to_args(language_t*li, JSContext *cx, uintN argc, jsval *argv)
{
js_internal_t*js = (js_internal_t*)li->internal;
int i;
value_t*args = array_new();
for(i=0;i<argc;i++) {
array_append(args, jsval_to_value(js, argv[i]));
}
return args;
}
static jsval value_to_jsval(JSContext*cx, value_t*value)
{
switch(value->type) {
case TYPE_VOID:
return OBJECT_TO_JSVAL(NULL);
break;
case TYPE_FLOAT32:
return DOUBLE_TO_JSVAL(value->f32);
break;
case TYPE_INT32:
return INT_TO_JSVAL(value->i32);
break;
case TYPE_BOOLEAN:
return BOOLEAN_TO_JSVAL(value->b);
break;
case TYPE_STRING: {
JSString *s = JS_InternString(cx, value->str);
return STRING_TO_JSVAL(s);
}
break;
case TYPE_ARRAY: {
JSObject *array = JS_NewArrayObject(cx, 0, NULL);
if (array == NULL)
return OBJECT_TO_JSVAL(NULL);
int i;
for(i=0;i<value->length;i++) {
jsval entry = value_to_jsval(cx, value->data[i]);
JS_SetElement(cx, array, i, &entry);
}
return OBJECT_TO_JSVAL(array);
}
break;
default: {
return OBJECT_TO_JSVAL(NULL);
}
}
}
static JSBool js_function_proxy(JSContext *cx, uintN argc, jsval *vp)
{
js_internal_t*js = JS_GetContextPrivate(cx);
jsval* argv = JS_ARGV(cx, vp);
JSFunction*func = JS_ValueToFunction(cx, argv[-2]);
if(!func) {
language_error(js->li, "Internal error: Couldn't determine function pointer for %p (%d arguments)", argv[-2], argc);
return JS_FALSE;
}
function_t*f = dict_lookup(js->jsfunction_to_function, func);
if(!f) {
language_error(js->li, "Internal error: Javascript tried to call native function %p (%d args), which we've never seen before.", func, argc);
return JS_FALSE;
}
value_t* args = js_argv_to_args(js->li, cx, argc, argv);
value_t* value = f->call(f, args);
value_destroy(args);
if(value == NULL) {
language_error(js->li, "Failed calling function %p", func);
return JS_FALSE;
}
JS_SET_RVAL(cx, vp, value_to_jsval(cx, value));
value_destroy(value);
log_dbg("[js] callback successful");
return JS_TRUE;
}
static void define_function_js(language_t*li, const char*name, function_t*f)
{
js_internal_t*js = (js_internal_t*)li->internal;
JSFunction*func = JS_DefineFunction(js->cx, js->global,
name,
js_function_proxy,
/*FIXME function_count_args(f),*/0,
0
);
dict_put(js->jsfunction_to_function, func, f);
}
void define_constant_js(language_t*li, const char*name, value_t* value)
{
js_internal_t*js = (js_internal_t*)li->internal;
jsval v = value_to_jsval(js->cx, value);
#ifdef DEBUG
printf("[js] define constant %s=",name);
value_dump(value);
printf("\n",name);
#endif
if(!JS_SetProperty(js->cx, js->global, name, &v)) {
language_error(li, "Couldn't define constant %s", name);
}
}
static bool compile_script_js(language_t*li, const char*script)
{
js_internal_t*js = (js_internal_t*)li->internal;
jsval rval;
JSBool ok;
log_dbg("[js] compiling script %p %p", script, js);
ok = JS_EvaluateScript(js->cx, js->global, script, strlen(script), "__main__", 1, &rval);
if(!ok) {
language_error(li, "Couldn't compile javascript program\n");
}
return ok;
}
static bool is_function_js(language_t*li, const char*name)
{
js_internal_t*js = (js_internal_t*)li->internal;
log_dbg("[js] is_function %s", name);
jsval rval;
JSBool ok;
js->noerrors = 1;
ok = JS_EvaluateScript(js->cx, js->global, name, strlen(name), "__main__", 1, &rval);
js->noerrors = 0;
if(!ok) {
return false;
}
if(JSVAL_IS_OBJECT(rval))
return true;
return false;
}
static value_t* call_function_js(language_t*li, const char*name, value_t* _args)
{
js_internal_t*js = (js_internal_t*)li->internal;
log_dbg("[js] calling function %s", name);
assert(_args->type == TYPE_ARRAY);
JSBool ok;
jsval* args = malloc(sizeof(jsval)*_args->length);
int i;
for(i=0;i<_args->length;i++) {
args[i] = value_to_jsval(js->cx, _args->data[i]);
}
jsval rval;
ok = JS_CallFunctionName(js->cx, js->global, name, _args->length, args, &rval);
if(!ok) {
language_error(js->li, "execution of function %s failed\n", name);
return NULL;
}
value_t*val = jsval_to_value(js, rval);
#ifdef DEBUG
printf("[js] return value: ");
value_dump(val);
printf("\n");
fflush(stdout);
#endif
return val;
}
void destroy_js(language_t* li)
{
if(li->internal) {
js_internal_t*js = (js_internal_t*)li->internal;
JS_DestroyContext(js->cx);
JS_DestroyRuntime(js->rt);
JS_ShutDown();
free(js->buffer);
free(js);
}
free(li);
}
language_t* javascript_interpreter_new()
{
language_t * li = calloc(1, sizeof(language_t));
li->name = "js";
li->initialize = initialize_js;
li->compile_script = compile_script_js;
li->is_function = is_function_js;
li->call_function = call_function_js;
li->define_function = define_function_js;
li->define_constant = define_constant_js;
li->destroy = destroy_js;
return li;
}