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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* Copyright (c) 2010-2013 Stefan Bolte <portix@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <JavaScriptCore/JavaScript.h>
#include <string.h>
#include <math.h>
#include "dwb.h"
#include "util.h"
#include "js.h"
void
js_make_exception(JSContextRef ctx, JSValueRef *exception, const gchar *format, ...)
{
va_list arg_list;
va_start(arg_list, format);
gchar message[STRING_LENGTH];
vsnprintf(message, sizeof(message), format, arg_list);
va_end(arg_list);
*exception = js_char_to_value(ctx, message);
}
void
js_set_property(JSContextRef ctx, JSObjectRef arg, const char *name, JSValueRef prop, JSClassAttributes attributes, JSValueRef *exc)
{
JSStringRef js_key = JSStringCreateWithUTF8CString(name);
JSObjectSetProperty(ctx, arg, js_key, prop, attributes, exc);
JSStringRelease(js_key);
}
void
js_set_object_property(JSContextRef ctx, JSObjectRef arg, const char *name, const char *value, JSValueRef *exc)
{
JSStringRef js_key = JSStringCreateWithUTF8CString(name);
JSValueRef js_value = js_char_to_value(ctx, value);
JSObjectSetProperty(ctx, arg, js_key, js_value, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, exc);
JSStringRelease(js_key);
}
gboolean
js_object_has_property(JSContextRef ctx, JSObjectRef arg, const char *name)
{
JSStringRef js_key = JSStringCreateWithUTF8CString(name);
gboolean result = JSObjectHasProperty(ctx, arg, js_key);
JSStringRelease(js_key);
return result;
}
void
js_set_object_number_property(JSContextRef ctx, JSObjectRef arg, const char *name, gdouble value, JSValueRef *exc)
{
JSStringRef js_key = JSStringCreateWithUTF8CString(name);
JSValueRef js_value = JSValueMakeNumber(ctx, value);
JSObjectSetProperty(ctx, arg, js_key, js_value, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, exc);
JSStringRelease(js_key);
}
/* js_get_object_property {{{*/
JSObjectRef
js_get_object_property(JSContextRef ctx, JSObjectRef arg, const char *name)
{
JSValueRef exc = NULL;
JSObjectRef ret;
JSStringRef buffer = JSStringCreateWithUTF8CString(name);
JSValueRef val = JSObjectGetProperty(ctx, arg, buffer, &exc);
JSStringRelease(buffer);
if (exc != NULL || !JSValueIsObject(ctx, val))
return NULL;
ret = JSValueToObject(ctx, val, &exc);
if (exc != NULL)
return NULL;
return ret;
}/*}}}*/
JSValueRef
js_char_to_value(JSContextRef ctx, const char *text)
{
JSStringRef string = JSStringCreateWithUTF8CString(text);
JSValueRef ret = JSValueMakeString(ctx, string);
JSStringRelease(string);
return ret;
}
/* js_get_string_property {{{*/
char *
js_get_string_property(JSContextRef ctx, JSObjectRef arg, const char *name)
{
JSValueRef exc = NULL;
JSStringRef buffer = JSStringCreateWithUTF8CString(name);
JSValueRef val = JSObjectGetProperty(ctx, arg, buffer, &exc);
JSStringRelease(buffer);
if (exc != NULL || !JSValueIsString(ctx, val) )
return NULL;
return js_value_to_char(ctx, val, JS_STRING_MAX, NULL);
}/*}}}*/
/* js_get_double_property {{{*/
double
js_get_double_property(JSContextRef ctx, JSObjectRef arg, const char *name)
{
double ret;
JSValueRef exc = NULL;
JSStringRef buffer = JSStringCreateWithUTF8CString(name);
JSValueRef val = JSObjectGetProperty(ctx, arg, buffer, &exc);
JSStringRelease(buffer);
if (exc != NULL || !JSValueIsNumber(ctx, val) )
return 0;
ret = JSValueToNumber(ctx, val, &exc);
if (exc != NULL)
return 0;
return ret;
}/*}}}*/
/* js_string_to_char
* Converts a JSStringRef, return a newly allocated char.
* {{{*/
char *
js_string_to_char(JSContextRef ctx, JSStringRef jsstring, size_t size)
{
size_t length;
if (size > 0)
length = MIN(JSStringGetMaximumUTF8CStringSize(jsstring), size);
else
length = JSStringGetMaximumUTF8CStringSize(jsstring);
char *ret = g_malloc(sizeof(gchar) * length);
JSStringGetUTF8CString(jsstring, ret, length);
return ret;
}/*}}}*/
JSValueRef
js_context_change(JSContextRef source_ctx, JSContextRef dest_ctx, JSValueRef val, JSValueRef *exc)
{
char *c_val = js_value_to_json(source_ctx, val, -1, exc);
if (c_val == NULL)
return JSValueMakeNull(dest_ctx);
JSStringRef json = JSStringCreateWithUTF8CString(c_val);
JSValueRef ret = JSValueMakeFromJSONString(dest_ctx, json);
g_free(c_val);
JSStringRelease(json);
if (ret == NULL)
return JSValueMakeNull(dest_ctx);
return ret;
}
/* js_create_object(WebKitWebFrame *frame, const char *)
*
* Executes a script in a function scope, should return an object with
* function-properties
* {{{*/
JSObjectRef
js_create_object(WebKitWebFrame *frame, const char *script)
{
if (script == NULL)
return NULL;
JSStringRef js_script;
JSValueRef ret, exc = NULL;
JSObjectRef return_object;
JSContextRef ctx = webkit_web_frame_get_global_context(frame);
js_script = JSStringCreateWithUTF8CString(script);
ret = JSEvaluateScript(ctx, js_script, NULL, NULL, 0, &exc);
JSStringRelease(js_script);
if (exc != NULL)
return NULL;
return_object = JSValueToObject(ctx, ret, &exc);
if (exc != NULL)
return NULL;
JSValueProtect(ctx, ret);
return return_object;
}/*}}}*/
/* js_call_as_function(WebKitWebFrame, JSObjectRef, char *string, char *json, * char **ret) {{{*/
char *
js_call_as_function(WebKitWebFrame *frame, JSObjectRef obj, const char *string, const char *json, JSType arg_type, char **char_ret)
{
char *ret = NULL;
JSValueRef js_ret, function, v = NULL;
JSObjectRef function_object;
JSStringRef js_json, js_name = NULL;
JSContextRef ctx;
if (obj == NULL)
goto error_out;
ctx = webkit_web_frame_get_global_context(frame);
js_name = JSStringCreateWithUTF8CString(string);
if (!JSObjectHasProperty(ctx, obj, js_name))
goto error_out;
function = JSObjectGetProperty(ctx, obj, js_name, NULL);
function_object = JSValueToObject(ctx, function, NULL);
if (json != NULL)
{
switch(arg_type)
{
case kJSTypeObject :
js_json = JSStringCreateWithUTF8CString(json);
v = JSValueMakeFromJSONString(ctx, js_json);
JSStringRelease(js_json);
break;
case kJSTypeString :
v = js_char_to_value(ctx, json);
break;
default :
break;
}
}
if (v)
{
JSValueRef vals[] = { v };
js_ret = JSObjectCallAsFunction(ctx, function_object, NULL, 1, vals, NULL);
}
else
js_ret = JSObjectCallAsFunction(ctx, function_object, NULL, 0, NULL, NULL);
if (char_ret != NULL)
ret = js_value_to_char(ctx, js_ret, JS_STRING_MAX, NULL);
error_out:
if (js_name)
JSStringRelease(js_name);
if (char_ret != NULL)
*char_ret = ret;
return ret;
}/*}}}*/
char *
js_value_to_string(JSContextRef ctx, JSValueRef value, size_t limit, JSValueRef *exc)
{
JSStringRef jsstring = JSValueToStringCopy(ctx, value, exc);
if (jsstring == NULL)
return NULL;
char *ret = js_string_to_char(ctx, jsstring, limit);
JSStringRelease(jsstring);
return ret;
}
/*{{{*/
char *
js_value_to_char(JSContextRef ctx, JSValueRef value, size_t limit, JSValueRef *exc)
{
if (value == NULL)
return NULL;
if (! JSValueIsString(ctx, value))
return NULL;
return js_value_to_string(ctx, value, limit, exc);
}/*}}}*/
/* print_exception {{{*/
gboolean
js_print_exception(JSContextRef ctx, JSValueRef exception, char *buffer, size_t bufferSize, int starting_line, int *linenumber)
{
if (exception == NULL)
return false;
if (!JSValueIsObject(ctx, exception))
return false;
JSObjectRef o = JSValueToObject(ctx, exception, NULL);
if (o == NULL)
return false;
gint line = (int)js_get_double_property(ctx, o, "line");
gchar *message = js_get_string_property(ctx, o, "message");
char *sourceURL = js_get_string_property(ctx, o, "sourceURL");
if (sourceURL)
fprintf(stderr, "DWB SCRIPT EXCEPTION: in file %s\n", sourceURL);
fprintf(stderr, "DWB SCRIPT EXCEPTION: in line %d: %s\n", line + starting_line, message == NULL ? "unknown" : message);
if (sourceURL != NULL && buffer != NULL)
{
strncpy(buffer, message, bufferSize-1);
buffer[bufferSize-1] = '\0';
}
if (linenumber)
*linenumber = line;
g_free(message);
g_free(sourceURL);
return true;
}
/*}}}*/
JSObjectRef
js_make_function(JSContextRef ctx, const char *script, const char *sourceurl, int linenumber)
{
JSValueRef exc;
JSObjectRef ret = NULL;
JSStringRef body = JSStringCreateWithUTF8CString(script);
JSStringRef source = NULL;
if (sourceurl)
JSStringCreateWithUTF8CString(sourceurl);
JSObjectRef function = JSObjectMakeFunction(ctx, NULL, 0, NULL, body, source, linenumber, &exc);
if (function != NULL)
ret = function;
else
js_print_exception(ctx, exc, NULL, 0, 0, NULL);
JSStringRelease(body);
if (source != NULL)
JSStringRelease(source);
return ret;
}
char *
js_value_to_json(JSContextRef ctx, JSValueRef value, size_t limit, JSValueRef *exc)
{
if (value == NULL)
return NULL;
JSStringRef js_json = JSValueCreateJSONString(ctx, value, 2, exc);
if (js_json == NULL)
return NULL;
char *json = js_string_to_char(ctx, js_json, limit);
JSStringRelease(js_json);
return json;
}
JSValueRef
js_json_to_value(JSContextRef ctx, const char *text)
{
JSStringRef json = JSStringCreateWithUTF8CString(text == NULL || *text == 0 ? "{}" : text);
JSValueRef ret = JSValueMakeFromJSONString(ctx, json);
JSStringRelease(json);
return ret;
}
JSValueRef
js_execute(JSContextRef ctx, const char *script, JSValueRef *exc)
{
JSObjectRef function = js_make_function(ctx, script, NULL, 0);
if (function != NULL)
return JSObjectCallAsFunction(ctx, function, function, 0, NULL, exc);
return NULL;
}
void
js_array_iterator_init(JSContextRef ctx, js_array_iterator *iter, JSObjectRef object)
{
g_return_if_fail(ctx != NULL && object != NULL);
iter->ctx = ctx;
iter->array = object;
JSValueProtect(ctx, iter->array);
iter->current_index = 0;
double length = js_get_double_property(ctx, object, "length");
iter->length = isnan(length) ? -1 : (int) length;
}
JSValueRef
js_array_iterator_next(js_array_iterator *iter, JSValueRef *exc)
{
g_return_val_if_fail(iter != NULL && iter->array != NULL, NULL);
if (iter->current_index < iter->length)
return JSObjectGetPropertyAtIndex(iter->ctx, iter->array, iter->current_index++, exc);
return NULL;
}
void
js_array_iterator_finish(js_array_iterator *iter)
{
g_return_if_fail(iter != NULL && iter != NULL && iter->array != NULL);
JSValueUnprotect(iter->ctx, iter->array);
}
void
js_property_iterator_init(JSContextRef ctx, js_property_iterator *iter, JSObjectRef object)
{
iter->ctx = ctx;
iter->array = JSObjectCopyPropertyNames(ctx, object);
JSPropertyNameArrayRetain(iter->array);
iter->object = object;
JSValueProtect(ctx, object);
iter->current_index = 0;
iter->length = JSPropertyNameArrayGetCount(iter->array);
}
JSValueRef
js_property_iterator_next(js_property_iterator *iter, JSStringRef *jsname_ret, char **name_ret, JSValueRef *exc)
{
g_return_val_if_fail(iter != NULL && iter->array != NULL, NULL);
if (iter->current_index < iter->length)
{
JSStringRef js_name = JSPropertyNameArrayGetNameAtIndex(iter->array, iter->current_index++);
JSValueRef ret = JSObjectGetProperty(iter->ctx, iter->object, js_name, exc);
if (name_ret)
*name_ret = js_string_to_char(iter->ctx, js_name, -1);
if (jsname_ret)
*jsname_ret = js_name;
else
JSStringRelease(js_name);
return ret;
}
if (name_ret)
*name_ret = NULL;
return NULL;
}
void
js_property_iterator_finish(js_property_iterator *iter)
{
g_return_if_fail(iter != NULL);
if (iter->array)
JSPropertyNameArrayRelease(iter->array);
if (iter->object)
JSValueUnprotect(iter->ctx, iter->object);
}
JSObjectRef
js_value_to_function(JSContextRef ctx, JSValueRef val, JSValueRef *exc)
{
JSObjectRef ret = JSValueToObject(ctx, val, exc);
if (ret != NULL && JSObjectIsFunction(ctx, ret))
return ret;
return NULL;
}
gboolean
js_check_syntax(JSContextRef ctx, const char *script, const char *filename, int lineOffset)
{
JSValueRef exc = NULL;
JSStringRef jsscript = JSStringCreateWithUTF8CString(script);
JSStringRef jssource = NULL;
if (filename != NULL)
jssource = JSStringCreateWithUTF8CString(filename);
gboolean correct = JSCheckScriptSyntax(ctx, jsscript, jssource, lineOffset, &exc);
if (!correct)
{
js_print_exception(ctx, exc, NULL, 0, 0, NULL);
}
JSStringRelease(jsscript);
if (jssource != NULL)
JSStringRelease(jssource);
return correct;
}
|