From 60d40b7f50ed69cd960e31ce4f81e0ed1ceabf50 Mon Sep 17 00:00:00 2001 From: Miciah Dashiel Butler Masters Date: Sun, 18 Dec 2005 17:40:00 +0000 Subject: [PATCH] Initial skeleton for SpiderMonkey scripting backend. --- src/scripting/Makefile | 1 + src/scripting/scripting.c | 6 ++- src/scripting/smjs/Makefile | 8 ++++ src/scripting/smjs/core.c | 84 +++++++++++++++++++++++++++++++++++++ src/scripting/smjs/core.h | 18 ++++++++ src/scripting/smjs/smjs.c | 21 ++++++++++ src/scripting/smjs/smjs.h | 8 ++++ 7 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/scripting/smjs/Makefile create mode 100644 src/scripting/smjs/core.c create mode 100644 src/scripting/smjs/core.h create mode 100644 src/scripting/smjs/smjs.c create mode 100644 src/scripting/smjs/smjs.h diff --git a/src/scripting/Makefile b/src/scripting/Makefile index 1d8160028..c3b3e4636 100644 --- a/src/scripting/Makefile +++ b/src/scripting/Makefile @@ -7,6 +7,7 @@ SUBDIRS-$(CONFIG_PERL) += perl SUBDIRS-$(CONFIG_PYTHON) += python SUBDIRS-$(CONFIG_RUBY) += ruby SUBDIRS-$(CONFIG_SEE) += see +SUBDIRS-$(CONFIG_ECMASCRIPT) += smjs OBJS = scripting.o diff --git a/src/scripting/scripting.c b/src/scripting/scripting.c index b396544b8..66202cce2 100644 --- a/src/scripting/scripting.c +++ b/src/scripting/scripting.c @@ -22,11 +22,12 @@ #include "scripting/python/python.h" #include "scripting/ruby/ruby.h" #include "scripting/see/see.h" +#include "scripting/smjs/smjs.h" /* Error reporting. */ -#if defined(CONFIG_RUBY) || defined(CONFIG_SEE) +#if defined(CONFIG_RUBY) || defined(CONFIG_SEE) || defined(CONFIG_ECMASCRIPT) void report_scripting_error(struct module *module, struct session *ses, unsigned char *msg) @@ -79,6 +80,9 @@ static struct module *scripting_modules[] = { #endif #ifdef CONFIG_SEE &see_scripting_module, +#endif +#ifdef CONFIG_ECMASCRIPT + &smjs_scripting_module, #endif NULL, }; diff --git a/src/scripting/smjs/Makefile b/src/scripting/smjs/Makefile new file mode 100644 index 000000000..2c82059fe --- /dev/null +++ b/src/scripting/smjs/Makefile @@ -0,0 +1,8 @@ +top_builddir=../../.. +include $(top_builddir)/Makefile.config + +INCLUDES += $(SPIDERMONKEY_CFLAGS) + +OBJS = smjs.o core.o + +include $(top_srcdir)/Makefile.lib diff --git a/src/scripting/smjs/core.c b/src/scripting/smjs/core.c new file mode 100644 index 000000000..f76c78585 --- /dev/null +++ b/src/scripting/smjs/core.c @@ -0,0 +1,84 @@ +/* ECMAScript browser scripting module */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "elinks.h" + +#include "ecmascript/spidermonkey/util.h" +#include "main/module.h" +#include "scripting/scripting.h" +#include "scripting/smjs/core.h" +#include "scripting/smjs/smjs.h" +#include "util/string.h" + + +#define SMJS_HOOKS_FILENAME "hooks.js" + +JSContext *smjs_ctx; +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; + +void +init_smjs(struct module *module) +{ + smjs_rt = JS_NewRuntime(1L * 1024L * 1024L); + if (!smjs_rt) return; + + smjs_ctx = JS_NewContext(smjs_rt, 8192); + if (!smjs_ctx) return; + + JS_SetErrorReporter(smjs_ctx, error_reporter); +} + +void +cleanup_smjs(struct module *module) +{ + JS_DestroyContext(smjs_ctx); + JS_DestroyRuntime(smjs_rt); +} diff --git a/src/scripting/smjs/core.h b/src/scripting/smjs/core.h new file mode 100644 index 000000000..5777d7189 --- /dev/null +++ b/src/scripting/smjs/core.h @@ -0,0 +1,18 @@ +#ifndef EL__SCRIPTING_SMJS_CORE_H +#define EL__SCRIPTING_SMJS_CORE_H + +#include "ecmascript/spidermonkey/util.h" + +struct module; +struct session; +struct string; + +extern JSContext *smjs_ctx; +struct session *smjs_ses; + +void alert_smjs_error(unsigned char *msg); + +void init_smjs(struct module *module); +void cleanup_smjs(struct module *module); + +#endif diff --git a/src/scripting/smjs/smjs.c b/src/scripting/smjs/smjs.c new file mode 100644 index 000000000..95eaa4aeb --- /dev/null +++ b/src/scripting/smjs/smjs.c @@ -0,0 +1,21 @@ +/* ECMAScript browser scripting module */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "elinks.h" + +#include "main/module.h" +#include "scripting/smjs/core.h" + + +struct module smjs_scripting_module = struct_module( + /* name: */ "ECMAScript scripting engine", + /* options: */ NULL, + /* events: */ NULL, + /* submodules: */ NULL, + /* data: */ NULL, + /* init: */ init_smjs, + /* done: */ cleanup_smjs +); diff --git a/src/scripting/smjs/smjs.h b/src/scripting/smjs/smjs.h new file mode 100644 index 000000000..8c545d43c --- /dev/null +++ b/src/scripting/smjs/smjs.h @@ -0,0 +1,8 @@ +#ifndef EL__SCRIPTING_SMJS_SMJS_H +#define EL__SCRIPTING_SMJS_SMJS_H + +struct module; + +extern struct module smjs_scripting_module; + +#endif