From 4c89c9d5df511852f2743d277cf39611b49ce7f2 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 10 Jun 2012 22:17:30 +0000 Subject: improve javascript support svn path=/trunk/netsurf/; revision=13962 --- javascript/global.c | 61 ++++++++++++++++++++ javascript/global.h | 28 ++++++++++ javascript/js.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++ javascript/js.h | 39 +++++++++++++ javascript/nojs.c | 51 +++++++++++++++++ 5 files changed, 336 insertions(+) create mode 100644 javascript/global.c create mode 100644 javascript/global.h create mode 100644 javascript/js.c create mode 100644 javascript/js.h create mode 100644 javascript/nojs.c (limited to 'javascript') diff --git a/javascript/global.c b/javascript/global.c new file mode 100644 index 000000000..10355a910 --- /dev/null +++ b/javascript/global.c @@ -0,0 +1,61 @@ +/* + * Copyright 2012 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +#include "mozjs/jsapi.h" + +#include "content/content.h" +#include "javascript/global.h" +#include "utils/log.h" + +static JSBool jsalert(JSContext *cx, uintN argc, jsval *vp) +{ + JSString* u16_txt; + char *txt; + + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &u16_txt)) + return JS_FALSE; + + +#ifdef SPIDERMONKEY_400 + unsigned int length; + length = JS_GetStringLength(u16_txt); + txt = alloca(sizeof(char)*(length+1)); + JS_EncodeStringToBuffer(u16_txt, txt, length); + txt[length] = '\0'; + +#else + txt = JS_GetStringBytes(u16_txt); +#endif + + warn_user(txt, NULL); + + JS_SET_RVAL(cx, vp, JSVAL_VOID); + + return JS_TRUE; +} + +static JSFunctionSpec global_functions[] = +{ + JS_FN("alert", jsalert, 1, 0), + JS_FS_END +}; + +bool js_new_globalfunc(JSContext *cx, JSObject *global) +{ + return JS_DefineFunctions(cx, global, global_functions); +} diff --git a/javascript/global.h b/javascript/global.h new file mode 100644 index 000000000..329c82c8d --- /dev/null +++ b/javascript/global.h @@ -0,0 +1,28 @@ +/* + * Copyright 2012 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +/** \file + * spidermonkey engine global functions. + */ + +#ifndef _NETSURF_JAVASCRIPT_GLOBAL_H_ +#define _NETSURF_JAVASCRIPT_GLOBAL_H_ + +bool js_new_globalfunc(JSContext *cx, JSObject *global); + +#endif diff --git a/javascript/js.c b/javascript/js.c new file mode 100644 index 000000000..81dc96be6 --- /dev/null +++ b/javascript/js.c @@ -0,0 +1,157 @@ +/* + * Copyright 2012 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +#include "mozjs/jsapi.h" + +#include "content/content.h" +#include "javascript/global.h" +#include "javascript/js.h" + +#include "utils/log.h" + +static JSRuntime *rt; /* global runtime */ + +void js_initialise(void) +{ + /* Create a JS runtime. */ + rt = JS_NewRuntime(8L * 1024L * 1024L); + LOG(("New runtime handle %p", rt)); +} + +void js_finalise(void) +{ + if (rt != NULL) { + LOG(("destroying runtime handle %p", rt)); + JS_DestroyRuntime(rt); + } + JS_ShutDown(); +} + +/* The error reporter callback. */ +static void js_reportError(JSContext *cx, const char *message, JSErrorReport *report) +{ + LOG(("%s:%u:%s\n", + report->filename ? report->filename : "", + (unsigned int) report->lineno, + message)); +} + +jscontext *js_newcontext(void) +{ + JSContext *cx; + + if (rt == NULL) { + return NULL; + } + + cx = JS_NewContext(rt, 8192); + if (cx == NULL) { + return NULL; + } + JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT ); + JS_SetVersion(cx, JSVERSION_LATEST); + JS_SetErrorReporter(cx, js_reportError); + + LOG(("New Context %p", cx)); + + return (jscontext *)cx; +} + +void js_destroycontext(jscontext *ctx) +{ + JSContext *cx = (JSContext *)ctx; + if (cx != NULL) { + LOG(("Destroying Context %p", cx)); + JS_DestroyContext(cx); + } +} + + + +/* The class of the global object. */ +static JSClass global_class = { + "global", JSCLASS_GLOBAL_FLAGS, + JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, + JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, + JSCLASS_NO_OPTIONAL_MEMBERS +}; + + +jsobject *js_newcompartment(jscontext *ctx, struct content* c) +{ + JSContext *cx = (JSContext *)ctx; + JSObject *global; + + if (cx == NULL) { + return NULL; + } +#ifdef SPIDERMONKEY_400 + global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); + if (global == NULL) { + return NULL; + } +#else + global = JS_NewObject(cx, &global_class, NULL, NULL); + if (global == NULL) { + return NULL; + } + JS_SetGlobalObject(cx, global); +#endif + + JS_SetContextPrivate(cx, c); /* private pointer to content */ + + js_new_globalfunc(cx, global); + + /* Populate the global object with the standard globals, like + Object and Array. */ + if (!JS_InitStandardClasses(cx, global)) { + return NULL; + } + + LOG(("Created new global object %p", global)); + + return (jsobject *)global; +} + +bool js_exec(jscontext *ctx, const char *txt, int txtlen) +{ + JSContext *cx = (JSContext *)ctx; + + //LOG(("%p \"%s\"",cx ,txt)); + + if (ctx == NULL) { + return false; + } + + if (txt == NULL) { + return false; + } + + if (txtlen == 0) { + return false; + } + + if (JS_EvaluateScript(cx, + JS_GetGlobalObject(cx), + txt, txtlen, + "", 0, NULL) == JS_TRUE) { + return true; + } + + return false; +} diff --git a/javascript/js.h b/javascript/js.h new file mode 100644 index 000000000..d8e241564 --- /dev/null +++ b/javascript/js.h @@ -0,0 +1,39 @@ +/* + * Copyright 2012 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +/** \file + * Interface to javascript engine functions. + */ + +#ifndef _NETSURF_JAVASCRIPT_JS_H_ +#define _NETSURF_JAVASCRIPT_JS_H_ + +typedef struct jscontext jscontext; +typedef struct jsobject jsobject; + +void js_initialise(void); +void js_finalise(void); + +jscontext *js_newcontext(void); +void js_destroycontext(jscontext *ctx); + +jsobject *js_newcompartment(jscontext *ctx, struct content* c); + +bool js_exec(jscontext *ctx, const char *txt, int txtlen); + +#endif /* _NETSURF_JAVASCRIPT_JS_H_ */ diff --git a/javascript/nojs.c b/javascript/nojs.c new file mode 100644 index 000000000..fe1cd191a --- /dev/null +++ b/javascript/nojs.c @@ -0,0 +1,51 @@ +/* + * Copyright 2012 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +/** \file + * Dummy implementation of javascript engine functions. + */ + +#include "desktop/js.h" +#include "utils/log.h" + +void js_initialise(void) +{ +} + +void js_finalise(void) +{ +} + +jscontext *js_newcontext(void) +{ + return NULL; +} + +void js_destroycontext(jscontext *ctx) +{ +} + +jsobject *js_newcompartment(jscontext *ctx, struct content* c) +{ + return NULL; +} + +bool js_exec(jscontext *ctx, char *txt, int txtlen) +{ + return true; +} -- cgit v1.2.3