1031: JS_SetErrorReporter only once per JSRuntime.
[elinks.git] / src / ecmascript / spidermonkey.c
blobd5d10304c79a4d80cc46f568e2e2536d007fa2c1
1 /* The SpiderMonkey ECMAScript backend. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "elinks.h"
13 #include "ecmascript/spidermonkey/util.h"
15 #include "bfu/dialog.h"
16 #include "cache/cache.h"
17 #include "cookies/cookies.h"
18 #include "dialogs/menu.h"
19 #include "dialogs/status.h"
20 #include "document/html/frames.h"
21 #include "document/document.h"
22 #include "document/forms.h"
23 #include "document/view.h"
24 #include "ecmascript/ecmascript.h"
25 #include "ecmascript/spidermonkey.h"
26 #include "ecmascript/spidermonkey/document.h"
27 #include "ecmascript/spidermonkey/form.h"
28 #include "ecmascript/spidermonkey/location.h"
29 #include "ecmascript/spidermonkey/navigator.h"
30 #include "ecmascript/spidermonkey/unibar.h"
31 #include "ecmascript/spidermonkey/window.h"
32 #include "intl/gettext/libintl.h"
33 #include "main/select.h"
34 #include "osdep/newwin.h"
35 #include "osdep/sysname.h"
36 #include "protocol/http/http.h"
37 #include "protocol/uri.h"
38 #include "session/history.h"
39 #include "session/location.h"
40 #include "session/session.h"
41 #include "session/task.h"
42 #include "terminal/tab.h"
43 #include "terminal/terminal.h"
44 #include "util/conv.h"
45 #include "util/string.h"
46 #include "viewer/text/draw.h"
47 #include "viewer/text/form.h"
48 #include "viewer/text/link.h"
49 #include "viewer/text/vs.h"
53 /*** Global methods */
56 /* TODO? Are there any which need to be implemented? */
58 static int js_module_init_ok;
60 void
61 spidermonkey_error_reporter(JSContext *ctx, const char *message, JSErrorReport *report)
63 struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
64 struct terminal *term;
65 unsigned char *strict, *exception, *warning, *error;
66 struct string msg;
68 assert(interpreter && interpreter->vs && interpreter->vs->doc_view
69 && interpreter->vs->doc_view->session
70 && interpreter->vs->doc_view->session->tab);
71 if_assert_failed goto reported;
73 term = interpreter->vs->doc_view->session->tab->term;
75 #ifdef CONFIG_LEDS
76 set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
77 #endif
79 if (!get_opt_bool("ecmascript.error_reporting")
80 || !init_string(&msg))
81 goto reported;
83 strict = JSREPORT_IS_STRICT(report->flags) ? " strict" : "";
84 exception = JSREPORT_IS_EXCEPTION(report->flags) ? " exception" : "";
85 warning = JSREPORT_IS_WARNING(report->flags) ? " warning" : "";
86 error = !report->flags ? " error" : "";
88 add_format_to_string(&msg, _("A script embedded in the current "
89 "document raised the following%s%s%s%s", term),
90 strict, exception, warning, error);
92 add_to_string(&msg, ":\n\n");
93 add_to_string(&msg, message);
95 if (report->linebuf && report->tokenptr) {
96 int pos = report->tokenptr - report->linebuf;
98 add_format_to_string(&msg, "\n\n%s\n.%*s^%*s.",
99 report->linebuf,
100 pos - 2, " ",
101 strlen(report->linebuf) - pos - 1, " ");
104 info_box(term, MSGBOX_FREE_TEXT, N_("JavaScript Error"), ALIGN_CENTER,
105 msg.source);
107 reported:
108 /* Im clu'les. --pasky */
109 JS_ClearPendingException(ctx);
112 static JSBool
113 safeguard(JSContext *ctx, JSScript *script)
115 struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
116 int max_exec_time = get_opt_int("ecmascript.max_exec_time");
118 if (time(NULL) - interpreter->exec_start > max_exec_time) {
119 struct terminal *term = interpreter->vs->doc_view->session->tab->term;
121 /* A killer script! Alert! */
122 ecmascript_timeout_dialog(term, max_exec_time);
123 return JS_FALSE;
125 return JS_TRUE;
128 static void
129 setup_safeguard(struct ecmascript_interpreter *interpreter,
130 JSContext *ctx)
132 interpreter->exec_start = time(NULL);
133 JS_SetBranchCallback(ctx, safeguard);
137 static void
138 spidermonkey_init(struct module *xxx)
140 js_module_init_ok = spidermonkey_runtime_addref();
143 static void
144 spidermonkey_done(struct module *xxx)
146 if (js_module_init_ok)
147 spidermonkey_runtime_release();
151 void *
152 spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
154 JSContext *ctx;
155 JSObject *window_obj, *document_obj, *forms_obj, *history_obj, *location_obj,
156 *statusbar_obj, *menubar_obj, *navigator_obj;
158 assert(interpreter);
159 if (!js_module_init_ok) return NULL;
161 ctx = JS_NewContext(spidermonkey_runtime,
162 8192 /* Stack allocation chunk size */);
163 if (!ctx)
164 return NULL;
165 interpreter->backend_data = ctx;
166 JS_SetContextPrivate(ctx, interpreter);
167 /* TODO: Make JSOPTION_STRICT and JSOPTION_WERROR configurable. */
168 #ifndef JSOPTION_COMPILE_N_GO
169 #define JSOPTION_COMPILE_N_GO 0 /* Older SM versions don't have it. */
170 #endif
171 /* XXX: JSOPTION_COMPILE_N_GO will go (will it?) when we implement
172 * some kind of bytecode cache. (If we will ever do that.) */
173 JS_SetOptions(ctx, JSOPTION_VAROBJFIX | JSOPTION_COMPILE_N_GO);
175 window_obj = JS_NewObject(ctx, (JSClass *) &window_class, NULL, NULL);
176 if (!window_obj) {
177 spidermonkey_put_interpreter(interpreter);
178 return NULL;
180 JS_InitStandardClasses(ctx, window_obj);
181 JS_DefineProperties(ctx, window_obj, (JSPropertySpec *) window_props);
182 spidermonkey_DefineFunctions(ctx, window_obj, window_funcs);
183 JS_SetPrivate(ctx, window_obj, interpreter->vs); /* to @window_class */
185 document_obj = spidermonkey_InitClass(ctx, window_obj, NULL,
186 (JSClass *) &document_class, NULL, 0,
187 (JSPropertySpec *) document_props,
188 document_funcs,
189 NULL, NULL);
191 forms_obj = spidermonkey_InitClass(ctx, document_obj, NULL,
192 (JSClass *) &forms_class, NULL, 0,
193 (JSPropertySpec *) forms_props,
194 forms_funcs,
195 NULL, NULL);
197 history_obj = spidermonkey_InitClass(ctx, window_obj, NULL,
198 (JSClass *) &history_class, NULL, 0,
199 (JSPropertySpec *) NULL,
200 history_funcs,
201 NULL, NULL);
203 location_obj = spidermonkey_InitClass(ctx, window_obj, NULL,
204 (JSClass *) &location_class, NULL, 0,
205 (JSPropertySpec *) location_props,
206 location_funcs,
207 NULL, NULL);
209 menubar_obj = JS_InitClass(ctx, window_obj, NULL,
210 (JSClass *) &menubar_class, NULL, 0,
211 (JSPropertySpec *) unibar_props, NULL,
212 NULL, NULL);
213 JS_SetPrivate(ctx, menubar_obj, "t"); /* to @menubar_class */
215 statusbar_obj = JS_InitClass(ctx, window_obj, NULL,
216 (JSClass *) &statusbar_class, NULL, 0,
217 (JSPropertySpec *) unibar_props, NULL,
218 NULL, NULL);
219 JS_SetPrivate(ctx, statusbar_obj, "s"); /* to @statusbar_class */
221 navigator_obj = JS_InitClass(ctx, window_obj, NULL,
222 (JSClass *) &navigator_class, NULL, 0,
223 (JSPropertySpec *) navigator_props, NULL,
224 NULL, NULL);
226 return ctx;
229 void
230 spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter)
232 JSContext *ctx;
234 assert(interpreter);
235 if (!js_module_init_ok) return;
236 ctx = interpreter->backend_data;
237 JS_DestroyContext(ctx);
238 interpreter->backend_data = NULL;
242 void
243 spidermonkey_eval(struct ecmascript_interpreter *interpreter,
244 struct string *code, struct string *ret)
246 JSContext *ctx;
247 jsval rval;
249 assert(interpreter);
250 if (!js_module_init_ok) return;
251 ctx = interpreter->backend_data;
252 setup_safeguard(interpreter, ctx);
253 interpreter->ret = ret;
254 JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
255 code->source, code->length, "", 0, &rval);
259 unsigned char *
260 spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
261 struct string *code)
263 JSContext *ctx;
264 jsval rval;
266 assert(interpreter);
267 if (!js_module_init_ok) return NULL;
268 ctx = interpreter->backend_data;
269 setup_safeguard(interpreter, ctx);
270 interpreter->ret = NULL;
271 if (JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
272 code->source, code->length, "", 0, &rval)
273 == JS_FALSE) {
274 return NULL;
276 if (JSVAL_IS_VOID(rval)) {
277 /* Undefined value. */
278 return NULL;
281 return stracpy(jsval_to_string(ctx, &rval));
286 spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
287 struct string *code)
289 JSContext *ctx;
290 JSFunction *fun;
291 jsval rval;
292 int ret;
294 assert(interpreter);
295 if (!js_module_init_ok) return 0;
296 ctx = interpreter->backend_data;
297 setup_safeguard(interpreter, ctx);
298 interpreter->ret = NULL;
299 fun = JS_CompileFunction(ctx, NULL, "", 0, NULL, code->source,
300 code->length, "", 0);
301 if (!fun)
302 return -1;
304 ret = JS_CallFunction(ctx, NULL, fun, 0, NULL, &rval);
305 if (ret == 2) { /* onClick="history.back()" */
306 return 0;
308 if (ret == JS_FALSE) {
309 return -1;
311 if (JSVAL_IS_VOID(rval)) {
312 /* Undefined value. */
313 return -1;
316 return jsval_to_boolean(ctx, &rval);
319 struct module spidermonkey_module = struct_module(
320 /* name: */ N_("SpiderMonkey"),
321 /* options: */ NULL,
322 /* events: */ NULL,
323 /* submodules: */ NULL,
324 /* data: */ NULL,
325 /* init: */ spidermonkey_init,
326 /* done: */ spidermonkey_done