2005-12-18 17:40:00 +00:00
|
|
|
/* ECMAScript browser scripting module */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2005-12-18 18:02:32 +00:00
|
|
|
#include "config/home.h"
|
2005-12-18 17:40:00 +00:00
|
|
|
#include "ecmascript/spidermonkey/util.h"
|
|
|
|
#include "main/module.h"
|
|
|
|
#include "scripting/scripting.h"
|
|
|
|
#include "scripting/smjs/core.h"
|
2005-12-18 17:47:54 +00:00
|
|
|
#include "scripting/smjs/elinks_object.h"
|
2005-12-24 03:39:24 +00:00
|
|
|
#include "scripting/smjs/global_object.h"
|
2005-12-18 17:40:00 +00:00
|
|
|
#include "scripting/smjs/smjs.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define SMJS_HOOKS_FILENAME "hooks.js"
|
|
|
|
|
|
|
|
JSContext *smjs_ctx;
|
2005-12-18 17:47:54 +00:00
|
|
|
JSObject *smjs_elinks_object;
|
2005-12-18 17:40:00 +00:00
|
|
|
struct session *smjs_ses;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
alert_smjs_error(unsigned char *msg)
|
|
|
|
{
|
|
|
|
report_scripting_error(&smjs_scripting_module,
|
|
|
|
smjs_ses, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
error_reporter(JSContext *ctx, const char *message, JSErrorReport *report)
|
|
|
|
{
|
|
|
|
unsigned char *strict, *exception, *warning, *error;
|
|
|
|
struct string msg;
|
|
|
|
|
|
|
|
if (!init_string(&msg)) goto reported;
|
|
|
|
|
|
|
|
strict = JSREPORT_IS_STRICT(report->flags) ? " strict" : "";
|
|
|
|
exception = JSREPORT_IS_EXCEPTION(report->flags) ? " exception" : "";
|
|
|
|
warning = JSREPORT_IS_WARNING(report->flags) ? " warning" : "";
|
|
|
|
error = !report->flags ? " error" : "";
|
|
|
|
|
|
|
|
add_format_to_string(&msg, "A client script raised the following%s%s%s%s",
|
|
|
|
strict, exception, warning, error);
|
|
|
|
|
|
|
|
add_to_string(&msg, ":\n\n");
|
|
|
|
add_to_string(&msg, message);
|
|
|
|
|
|
|
|
if (report->linebuf && report->tokenptr) {
|
|
|
|
int pos = report->tokenptr - report->linebuf;
|
|
|
|
|
|
|
|
add_format_to_string(&msg, "\n\n%s\n.%*s^%*s.",
|
|
|
|
report->linebuf,
|
|
|
|
pos - 2, " ",
|
|
|
|
strlen(report->linebuf) - pos - 1, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
alert_smjs_error(msg.source);
|
|
|
|
done_string(&msg);
|
|
|
|
|
|
|
|
reported:
|
|
|
|
JS_ClearPendingException(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSRuntime *smjs_rt;
|
|
|
|
|
2005-12-29 13:26:38 +00:00
|
|
|
static void
|
2005-12-29 13:27:12 +00:00
|
|
|
smjs_do_file(unsigned char *path)
|
2005-12-18 18:02:32 +00:00
|
|
|
{
|
|
|
|
jsval rval;
|
|
|
|
struct string script;
|
2005-12-29 13:27:12 +00:00
|
|
|
|
|
|
|
if (!init_string(&script)) return;
|
|
|
|
|
|
|
|
if (add_file_to_string(&script, path)
|
|
|
|
&& JS_FALSE == JS_EvaluateScript(smjs_ctx,
|
|
|
|
JS_GetGlobalObject(smjs_ctx),
|
|
|
|
script.source, script.length, path, 1, &rval))
|
|
|
|
alert_smjs_error("error loading default script file");
|
|
|
|
|
|
|
|
done_string(&script);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
smjs_load_hooks(void)
|
|
|
|
{
|
2005-12-18 18:02:32 +00:00
|
|
|
unsigned char *path;
|
|
|
|
|
2005-12-19 21:24:27 +00:00
|
|
|
assert(smjs_ctx);
|
|
|
|
|
2005-12-18 18:02:32 +00:00
|
|
|
if (elinks_home) {
|
|
|
|
path = straconcat(elinks_home, SMJS_HOOKS_FILENAME, NULL);
|
|
|
|
} else {
|
|
|
|
path = stracpy(CONFDIR "/" SMJS_HOOKS_FILENAME);
|
|
|
|
}
|
|
|
|
|
2005-12-29 13:27:12 +00:00
|
|
|
smjs_do_file(path);
|
2005-12-19 19:08:34 +00:00
|
|
|
mem_free(path);
|
2005-12-18 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
2005-12-18 17:40:00 +00:00
|
|
|
void
|
|
|
|
init_smjs(struct module *module)
|
|
|
|
{
|
|
|
|
smjs_rt = JS_NewRuntime(1L * 1024L * 1024L);
|
|
|
|
if (!smjs_rt) return;
|
|
|
|
|
|
|
|
smjs_ctx = JS_NewContext(smjs_rt, 8192);
|
2005-12-19 21:26:05 +00:00
|
|
|
if (!smjs_ctx) {
|
|
|
|
JS_DestroyRuntime(smjs_rt);
|
|
|
|
smjs_rt = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2005-12-18 17:40:00 +00:00
|
|
|
|
|
|
|
JS_SetErrorReporter(smjs_ctx, error_reporter);
|
2005-12-18 17:40:13 +00:00
|
|
|
|
2005-12-24 03:40:59 +00:00
|
|
|
smjs_init_global_object();
|
2005-12-18 17:47:54 +00:00
|
|
|
|
2005-12-24 03:47:34 +00:00
|
|
|
smjs_init_elinks_object();
|
2005-12-18 18:02:32 +00:00
|
|
|
|
|
|
|
smjs_load_hooks();
|
2005-12-18 17:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup_smjs(struct module *module)
|
|
|
|
{
|
2005-12-19 21:24:54 +00:00
|
|
|
if (!smjs_ctx) return;
|
|
|
|
|
2005-12-18 17:40:00 +00:00
|
|
|
JS_DestroyContext(smjs_ctx);
|
|
|
|
JS_DestroyRuntime(smjs_rt);
|
|
|
|
}
|