Mozjs185 is getting really old and unsupported. Newer versions
of the mozjs JSAPI are C++ based. Start the conversion to more recent
JSAPI's by updating pacrunner to use mozjs24 which is a common version
being used by a number of distributions. It also happens to be the first
C++ only version of JSAPI.
Signed-off-by: Jeremy Linton <jeremy.linton(a)arm.com>
---
Makefile.am | 20 +++++++++++++-------
configure.ac | 4 ++--
plugins/{mozjs.c => mozjs.cpp} | 36 +++++++++++++++++++-----------------
3 files changed, 34 insertions(+), 26 deletions(-)
rename plugins/{mozjs.c => mozjs.cpp} (82%)
diff --git a/Makefile.am b/Makefile.am
index ee84b75..59e0ef0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,9 +31,15 @@ endif
if MOZJS
js_sources = src/js_funcs.c
builtin_modules += mozjs
-builtin_sources += plugins/mozjs.c
-builtin_cflags += @MOZJS_CFLAGS@
-builtin_libadd += @MOZJS_LIBS@
+builtin_libadd += plugins/libmozjsplugin.a @MOZJS_LIBS@ -lstdc++
+# similar to v8 if mozjs isn't enabled then
+# try to avoid using g++ by building a library
+# This is a bit funny because at first sight you would think that
+# the #if MOZJS would keep the builtin_sources from affecting the automake
+# choice of linker, but it doesn't.
+noinst_LIBRARIES += plugins/libmozjsplugin.a
+plugins_libmozjsplugin_a_SOURCES = plugins/mozjs.cpp
+plugins_libmozjsplugin_a_CXXFLAGS = $(AM_CFLAGS) @MOZJS_CFLAGS@ @DBUS_CFLAGS@
@GLIB_CFLAGS@
endif
if V8
@@ -137,16 +143,16 @@ unit_test_pacrunner_LDADD += @CURL_LIBS@
endif
if MOZJS
-unit_test_pacrunner_SOURCES += plugins/mozjs.c
-
+unit_test_pacrunner_SOURCES += plugins/mozjs.cpp
+unit_test_pacrunner_CXXFLAGS = $(AM_CFLAGS) @MOZJS_CFLAGS@
unit_test_pacrunner_LDADD += @MOZJS_LIBS@ @PTHREAD_LIBS@
noinst_PROGRAMS += unit/test-mozjs
unit_test_mozjs_SOURCES = unit/test-mozjs.c src/pacrunner.h \
src/proxy.c src/manual.c src/download.c \
- src/js.c plugins/mozjs.c $(js_sources)
-
+ src/js.c plugins/mozjs.cpp $(js_sources)
+unit_test_mozjs_CXXFLAGS = $(AM_CFLAGS) @MOZJS_CFLAGS@
unit_test_mozjs_LDADD = @MOZJS_LIBS@ @GLIB_LIBS@ @PTHREAD_LIBS@
endif
diff --git a/configure.ac b/configure.ac
index 42a64a9..d2fc8f8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -76,8 +76,8 @@ AM_CONDITIONAL(DUKTAPE, test "${enable_duktape}" =
"yes")
AC_ARG_ENABLE(mozjs, AC_HELP_STRING([--enable-mozjs],
[enable Mozilla Javascript plugin support]))
if (test "${enable_mozjs}" = "yes"); then
- PKG_CHECK_MODULES(MOZJS, mozjs185, dummy=yes,
- AC_MSG_ERROR(Mozilla Javascript >= 1.8 is required))
+ PKG_CHECK_MODULES(MOZJS, mozjs-24, dummy=yes,
+ AC_MSG_ERROR(Mozilla Javascript >= 24 is required))
AC_SUBST(MOZJS_CFLAGS)
AC_SUBST(MOZJS_LIBS)
fi
diff --git a/plugins/mozjs.c b/plugins/mozjs.cpp
similarity index 82%
rename from plugins/mozjs.c
rename to plugins/mozjs.cpp
index a923203..843b2fa 100644
--- a/plugins/mozjs.c
+++ b/plugins/mozjs.cpp
@@ -34,8 +34,10 @@
#include <jsapi.h>
#pragma GCC diagnostic error "-Wredundant-decls"
+extern "C" {
#include "pacrunner.h"
#include "js.h"
+}
static pthread_mutex_t mozjs_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -43,11 +45,12 @@ struct pacrunner_mozjs {
struct pacrunner_proxy *proxy;
JSContext *jsctx;
JSObject *jsobj;
+ JSAutoCompartment *jsac;
};
-static JSBool myipaddress(JSContext *jsctx, uintN argc, jsval *vp)
+static JSBool myipaddress(JSContext *jsctx, unsigned argc, jsval *vp)
{
- struct pacrunner_mozjs *ctx = JS_GetContextPrivate(jsctx);
+ struct pacrunner_mozjs *ctx = (pacrunner_mozjs *)JS_GetContextPrivate(jsctx);
char address[NI_MAXHOST];
DBG("");
@@ -68,9 +71,9 @@ static JSBool myipaddress(JSContext *jsctx, uintN argc, jsval *vp)
return JS_TRUE;
}
-static JSBool dnsresolve(JSContext *jsctx, uintN argc, jsval *vp)
+static JSBool dnsresolve(JSContext *jsctx, unsigned argc, jsval *vp)
{
- struct pacrunner_mozjs *ctx = JS_GetContextPrivate(jsctx);
+ struct pacrunner_mozjs *ctx = (pacrunner_mozjs *)JS_GetContextPrivate(jsctx);
char address[NI_MAXHOST];
jsval *argv = JS_ARGV(jsctx, vp);
char *host = JS_EncodeString(jsctx, JS_ValueToString(jsctx, argv[0]));
@@ -97,10 +100,10 @@ static JSBool dnsresolve(JSContext *jsctx, uintN argc, jsval *vp)
static JSClass jscls = {
"global", JSCLASS_GLOBAL_FLAGS,
- JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+ JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub,
JS_StrictPropertyStub,
- JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
- NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+ JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
+ JSCLASS_NO_OPTIONAL_MEMBERS
};
static JSRuntime *jsrun;
@@ -115,7 +118,7 @@ static int create_object(struct pacrunner_proxy *proxy)
if (!script)
return 0;
- ctx = g_malloc0(sizeof(struct pacrunner_mozjs));
+ ctx = (pacrunner_mozjs *)g_malloc0(sizeof(struct pacrunner_mozjs));
ctx->proxy = proxy;
ctx->jsctx = JS_NewContext(jsrun, 8 * 1024);
@@ -126,12 +129,10 @@ static int create_object(struct pacrunner_proxy *proxy)
JS_SetContextPrivate(ctx->jsctx, ctx);
__pacrunner_proxy_set_jsctx(proxy, ctx);
-#if JS_VERSION >= 185
- ctx->jsobj = JS_NewCompartmentAndGlobalObject(ctx->jsctx, &jscls,
- NULL);
-#else
- ctx->jsobj = JS_NewObject(ctx->jsctx, &jscls, NULL, NULL);
-#endif
+ JS::CompartmentOptions compart_opts;
+ compart_opts.setVersion(JSVERSION_LATEST);
+ ctx->jsobj = JS_NewGlobalObject(ctx->jsctx, &jscls, NULL, compart_opts);
+ ctx->jsac = new JSAutoCompartment(ctx->jsctx, ctx->jsobj);
if (!JS_InitStandardClasses(ctx->jsctx, ctx->jsobj))
pacrunner_error("Failed to init JS standard classes");
@@ -152,13 +153,14 @@ static int create_object(struct pacrunner_proxy *proxy)
static int mozjs_clear_proxy(struct pacrunner_proxy *proxy)
{
- struct pacrunner_mozjs *ctx = __pacrunner_proxy_get_jsctx(proxy);
+ struct pacrunner_mozjs *ctx = (pacrunner_mozjs *)__pacrunner_proxy_get_jsctx(proxy);
DBG("proxy %p ctx %p", proxy, ctx);
if (!ctx)
return -EINVAL;
+ delete ctx->jsac;
JS_DestroyContext(ctx->jsctx);
__pacrunner_proxy_set_jsctx(proxy, NULL);
@@ -180,7 +182,7 @@ static int mozjs_set_proxy(struct pacrunner_proxy *proxy)
static char * mozjs_execute(struct pacrunner_proxy *proxy, const char *url,
const char *host)
{
- struct pacrunner_mozjs *ctx = __pacrunner_proxy_get_jsctx(proxy);
+ struct pacrunner_mozjs *ctx = (pacrunner_mozjs *)__pacrunner_proxy_get_jsctx(proxy);
JSBool result;
jsval rval, args[2];
char *answer, *g_answer;
@@ -229,7 +231,7 @@ static int mozjs_init(void)
{
DBG("");
- jsrun = JS_NewRuntime(8 * 1024 * 1024);
+ jsrun = JS_NewRuntime(8 * 1024 * 1024, JS_USE_HELPER_THREADS);
return pacrunner_js_driver_register(&mozjs_driver);
}
--
2.9.2