Polish translation was updated.
[elinks.git] / src / ecmascript / spidermonkey.c
blobd2cb346e29f218081dab017ec9f66a9ca9f2541c
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 JSRuntime *jsrt;
60 static void
61 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 jsrt = JS_NewRuntime(0x400000UL);
141 /* XXX: This is a hack to avoid a crash on exit. SMJS will crash
142 * on JS_DestroyRuntime if the given runtime has never had any context
143 * created, which will be the case if one closes ELinks without having
144 * loaded any documents. */
145 JS_DestroyContext(JS_NewContext(jsrt, 0));
148 static void
149 spidermonkey_done(struct module *xxx)
151 JS_DestroyRuntime(jsrt);
152 JS_ShutDown();
156 void *
157 spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
159 JSContext *ctx;
160 JSObject *window_obj, *document_obj, *forms_obj, *history_obj, *location_obj,
161 *statusbar_obj, *menubar_obj, *navigator_obj;
163 assert(interpreter);
165 ctx = JS_NewContext(jsrt, 8192 /* Stack allocation chunk size */);
166 if (!ctx)
167 return NULL;
168 interpreter->backend_data = ctx;
169 JS_SetContextPrivate(ctx, interpreter);
170 /* TODO: Make JSOPTION_STRICT and JSOPTION_WERROR configurable. */
171 #ifndef JSOPTION_COMPILE_N_GO
172 #define JSOPTION_COMPILE_N_GO 0 /* Older SM versions don't have it. */
173 #endif
174 /* XXX: JSOPTION_COMPILE_N_GO will go (will it?) when we implement
175 * some kind of bytecode cache. (If we will ever do that.) */
176 JS_SetOptions(ctx, JSOPTION_VAROBJFIX | JSOPTION_COMPILE_N_GO);
177 JS_SetErrorReporter(ctx, error_reporter);
179 window_obj = JS_NewObject(ctx, (JSClass *) &window_class, NULL, NULL);
180 if (!window_obj) {
181 spidermonkey_put_interpreter(interpreter);
182 return NULL;
184 JS_InitStandardClasses(ctx, window_obj);
185 JS_DefineProperties(ctx, window_obj, (JSPropertySpec *) window_props);
186 JS_DefineFunctions(ctx, window_obj, (JSFunctionSpec *) window_funcs);
187 JS_SetPrivate(ctx, window_obj, interpreter->vs);
189 document_obj = JS_InitClass(ctx, window_obj, NULL,
190 (JSClass *) &document_class, NULL, 0,
191 (JSPropertySpec *) document_props,
192 (JSFunctionSpec *) document_funcs,
193 NULL, NULL);
195 forms_obj = JS_InitClass(ctx, document_obj, NULL,
196 (JSClass *) &forms_class, NULL, 0,
197 (JSPropertySpec *) forms_props,
198 (JSFunctionSpec *) forms_funcs,
199 NULL, NULL);
201 history_obj = JS_InitClass(ctx, window_obj, NULL,
202 (JSClass *) &history_class, NULL, 0,
203 (JSPropertySpec *) NULL,
204 (JSFunctionSpec *) history_funcs,
205 NULL, NULL);
207 location_obj = JS_InitClass(ctx, window_obj, NULL,
208 (JSClass *) &location_class, NULL, 0,
209 (JSPropertySpec *) location_props,
210 (JSFunctionSpec *) location_funcs,
211 NULL, NULL);
213 menubar_obj = JS_InitClass(ctx, window_obj, NULL,
214 (JSClass *) &menubar_class, NULL, 0,
215 (JSPropertySpec *) unibar_props, NULL,
216 NULL, NULL);
217 JS_SetPrivate(ctx, menubar_obj, "t");
219 statusbar_obj = JS_InitClass(ctx, window_obj, NULL,
220 (JSClass *) &statusbar_class, NULL, 0,
221 (JSPropertySpec *) unibar_props, NULL,
222 NULL, NULL);
223 JS_SetPrivate(ctx, statusbar_obj, "s");
225 navigator_obj = JS_InitClass(ctx, window_obj, NULL,
226 (JSClass *) &navigator_class, NULL, 0,
227 (JSPropertySpec *) navigator_props, NULL,
228 NULL, NULL);
230 return ctx;
233 void
234 spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter)
236 JSContext *ctx;
238 assert(interpreter);
239 ctx = interpreter->backend_data;
240 JS_DestroyContext(ctx);
241 interpreter->backend_data = NULL;
245 void
246 spidermonkey_eval(struct ecmascript_interpreter *interpreter,
247 struct string *code, struct string *ret)
249 JSContext *ctx;
250 jsval rval;
252 assert(interpreter);
253 ctx = interpreter->backend_data;
254 setup_safeguard(interpreter, ctx);
255 interpreter->ret = ret;
256 JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
257 code->source, code->length, "", 0, &rval);
261 unsigned char *
262 spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
263 struct string *code)
265 JSContext *ctx;
266 jsval rval;
268 assert(interpreter);
269 ctx = interpreter->backend_data;
270 setup_safeguard(interpreter, ctx);
271 interpreter->ret = NULL;
272 if (JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
273 code->source, code->length, "", 0, &rval)
274 == JS_FALSE) {
275 return NULL;
277 if (JSVAL_IS_VOID(rval)) {
278 /* Undefined value. */
279 return NULL;
282 return stracpy(jsval_to_string(ctx, &rval));
287 spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
288 struct string *code)
290 JSContext *ctx;
291 jsval rval;
292 int ret;
294 assert(interpreter);
295 ctx = interpreter->backend_data;
296 setup_safeguard(interpreter, ctx);
297 interpreter->ret = NULL;
298 ret = JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
299 code->source, code->length, "", 0, &rval);
300 if (ret == 2) { /* onClick="history.back()" */
301 return 0;
303 if (ret == JS_FALSE) {
304 return -1;
306 if (JSVAL_IS_VOID(rval)) {
307 /* Undefined value. */
308 return -1;
311 return jsval_to_boolean(ctx, &rval);
314 struct module spidermonkey_module = struct_module(
315 /* name: */ "SpiderMonkey",
316 /* options: */ NULL,
317 /* events: */ NULL,
318 /* submodules: */ NULL,
319 /* data: */ NULL,
320 /* init: */ spidermonkey_init,
321 /* done: */ spidermonkey_done