diff options
author | Jeremy Linton <jeremy.linton@arm.com> | 2017-04-26 14:56:55 -0500 |
---|---|---|
committer | Jeremy Linton <jeremy.linton@arm.com> | 2017-04-26 14:56:55 -0500 |
commit | e6c993a1baa04b0bd2c4194f6e3b6fa69d901656 (patch) | |
tree | 11320a8e4387aefeeaafe73429823e87fd7b3fd6 | |
parent | ac8a3f8bf8395b2e814a888bee15e43c1b2801ed (diff) | |
download | libproxy-git-e6c993a1baa04b0bd2c4194f6e3b6fa69d901656.tar.gz |
Update build to mozjs38
This pull libproxy forward a little more to mozjs38, which
is where gnome is currently at. This means most distro's will
already be shipping this version of mozjs.
Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
-rw-r--r-- | libproxy/cmake/modules/pacrunner_mozjs.cmk | 2 | ||||
-rw-r--r-- | libproxy/modules/pacrunner_mozjs.cpp | 91 |
2 files changed, 54 insertions, 39 deletions
diff --git a/libproxy/cmake/modules/pacrunner_mozjs.cmk b/libproxy/cmake/modules/pacrunner_mozjs.cmk index 9fb36d6..c2ae3db 100644 --- a/libproxy/cmake/modules/pacrunner_mozjs.cmk +++ b/libproxy/cmake/modules/pacrunner_mozjs.cmk @@ -9,7 +9,7 @@ if(WIN32) elseif(NOT APPLE) option(WITH_MOZJS "Search for MOZJS package" ON) if (WITH_MOZJS) - pkg_search_module(MOZJS mozjs-24) + pkg_search_module(MOZJS mozjs-38) if(MOZJS_FOUND) include_directories(${MOZJS_INCLUDE_DIRS}) link_directories(${MOZJS_LIBRARY_DIRS}) diff --git a/libproxy/modules/pacrunner_mozjs.cpp b/libproxy/modules/pacrunner_mozjs.cpp index 196b5dd..a70b2e9 100644 --- a/libproxy/modules/pacrunner_mozjs.cpp +++ b/libproxy/modules/pacrunner_mozjs.cpp @@ -32,7 +32,11 @@ using namespace libproxy; #define XP_WIN #endif #endif +#pragma GCC diagnostic ignored "-Winvalid-offsetof" #include <jsapi.h> +#pragma GCC diagnostic error "-Winvalid-offsetof" +#include <js/CallArgs.h> + #include "pacutils.h" #ifndef INET_ADDRSTRLEN @@ -43,15 +47,15 @@ using namespace libproxy; #define INET6_ADDRSTRLEN 46 #endif -static JSBool dnsResolve_(JSContext *cx, jsval hostname, jsval *vp) { +static void dnsResolve_(JSContext *cx, JSString *hostname, JS::CallArgs *argv) { // Get hostname argument - char *tmp = JS_EncodeString(cx, JS_ValueToString(cx, hostname)); + char *tmp = JS_EncodeString(cx, hostname); // Set the default return value - JS_SET_RVAL(cx, vp, JSVAL_NULL); + argv->rval().setNull(); // Look it up - struct addrinfo *info = NULL; + struct addrinfo *info = nullptr; if (getaddrinfo(tmp, NULL, NULL, &info)) goto out; @@ -67,29 +71,32 @@ static JSBool dnsResolve_(JSContext *cx, jsval hostname, jsval *vp) { NI_NUMERICHOST)) goto out; // We succeeded - JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyN(cx, tmp, strlen(tmp)))); - tmp = NULL; + argv->rval().setString(JS_NewStringCopyZ(cx, tmp)); + tmp = nullptr; out: if (info) freeaddrinfo(info); JS_free(cx, tmp); - return true; } -static JSBool dnsResolve(JSContext *cx, unsigned argc, jsval *vp) { - jsval *argv = JS_ARGV(cx, vp); - return dnsResolve_(cx, argv[0], vp); +static bool dnsResolve(JSContext *cx, unsigned argc, JS::Value *vp) { + JS::CallArgs argv=JS::CallArgsFromVp(argc,vp); + dnsResolve_(cx, argv[0].toString(), &argv); + return true; } -static JSBool myIpAddress(JSContext *cx, unsigned argc, jsval *vp) { +static bool myIpAddress(JSContext *cx, unsigned argc, JS::Value *vp) { + JS::CallArgs argv=JS::CallArgsFromVp(argc,vp); char *hostname = (char *) JS_malloc(cx, 1024); + if (!gethostname(hostname, 1023)) { JSString *myhost = JS_NewStringCopyN(cx, hostname, strlen(hostname)); - jsval arg = STRING_TO_JSVAL(myhost); - return dnsResolve_(cx, arg, vp); + dnsResolve_(cx, myhost, &argv); + } else { + argv.rval().setNull(); } + JS_free(cx, hostname); - JS_SET_RVAL(cx, vp, JSVAL_NULL); return true; } @@ -97,39 +104,46 @@ static JSBool myIpAddress(JSContext *cx, unsigned argc, jsval *vp) { // This MUST be a static global static JSClass cls = { "global", JSCLASS_GLOBAL_FLAGS, - JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, - JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL, }; class mozjs_pacrunner : public pacrunner { public: mozjs_pacrunner(string pac, const url& pacurl) throw (bad_alloc) : pacrunner(pac, pacurl) { - jsval rval; // Set defaults - this->jsrun = NULL; - this->jsctx = NULL; + this->jsrun = nullptr; + this->jsctx = nullptr; + JS_Init(); // Initialize Javascript runtime environment - if (!(this->jsrun = JS_NewRuntime(1024 * 1024,JS_USE_HELPER_THREADS))) goto error; + if (!(this->jsrun = JS_NewRuntime(1024 * 1024))) goto error; if (!(this->jsctx = JS_NewContext(this->jsrun, 1024 * 1024))) goto error; { + JS::RootedValue rval(this->jsctx); JS::CompartmentOptions compart_opts; compart_opts.setVersion(JSVERSION_LATEST); - if (!(this->jsglb = JS_NewGlobalObject (this->jsctx, &cls, NULL, compart_opts))) goto error; - if (!(this->jsac = new JSAutoCompartment(this->jsctx, this->jsglb))) goto error; - if (!JS_InitStandardClasses(this->jsctx, this->jsglb)) goto error; + this->jsglb = new JS::Heap<JSObject*>(JS_NewGlobalObject( + this->jsctx, &cls, + nullptr, JS::DontFireOnNewGlobalHook, + compart_opts)); + + if (!(this->jsglb)) goto error; + JS::RootedObject global(this->jsctx,this->jsglb->get()); + if (!(this->jsac = new JSAutoCompartment(this->jsctx, global))) goto error; + if (!JS_InitStandardClasses(this->jsctx, global)) goto error; // Define Javascript functions - JS_DefineFunction(this->jsctx, this->jsglb, "dnsResolve", dnsResolve, 1, 0); - JS_DefineFunction(this->jsctx, this->jsglb, "myIpAddress", myIpAddress, 0, 0); - JS_EvaluateScript(this->jsctx, this->jsglb, JAVASCRIPT_ROUTINES, - strlen(JAVASCRIPT_ROUTINES), "pacutils.js", 0, &rval); + JS_DefineFunction(this->jsctx, global, "dnsResolve", dnsResolve, 1, 0); + JS_DefineFunction(this->jsctx, global, "myIpAddress", myIpAddress, 0, 0); + JS::CompileOptions options(this->jsctx); + options.setUTF8(true); + + JS::Evaluate(this->jsctx, global, options, JAVASCRIPT_ROUTINES, + strlen(JAVASCRIPT_ROUTINES), &rval); // Add PAC to the environment - JS_EvaluateScript(this->jsctx, this->jsglb, pac.c_str(), - strlen(pac.c_str()), pacurl.to_string().c_str(), 0, &rval); + JS::Evaluate(this->jsctx, global, options, pac.c_str(), pac.length(), &rval); return; } error: @@ -140,9 +154,10 @@ public: ~mozjs_pacrunner() { if (this->jsac) delete this->jsac; + if (this->jsglb) delete this->jsglb; if (this->jsctx) JS_DestroyContext(this->jsctx); if (this->jsrun) JS_DestroyRuntime(this->jsrun); - // JS_ShutDown()? + JS_ShutDown(); } string run(const url& url_) throw (bad_alloc) { @@ -154,17 +169,17 @@ public: if (tmphost) JS_free(this->jsctx, tmphost); throw bad_alloc(); } - jsval args[2] = { - STRING_TO_JSVAL(JS_NewStringCopyN(this->jsctx, tmpurl, strlen(tmpurl))), - STRING_TO_JSVAL(JS_NewStringCopyN(this->jsctx, tmphost, strlen(tmphost))) - }; + JS::AutoValueArray<2> args(this->jsctx); + args[0].setString(JS_NewStringCopyZ(this->jsctx, tmpurl)); + args[1].setString(JS_NewStringCopyZ(this->jsctx, tmphost)); // Find the proxy (call FindProxyForURL()) - jsval rval; - JSBool result = JS_CallFunctionName(this->jsctx, this->jsglb, "FindProxyForURL", 2, args, &rval); + JS::RootedValue rval(this->jsctx); + JS::RootedObject global(this->jsctx,this->jsglb->get()); + bool result = JS_CallFunctionName(this->jsctx, global, "FindProxyForURL", args, &rval); if (!result) return ""; - char * tmpanswer = JS_EncodeString(this->jsctx, JS_ValueToString(this->jsctx, rval)); + char * tmpanswer = JS_EncodeString(this->jsctx, rval.toString()); string answer = string(tmpanswer); JS_free(this->jsctx, tmpanswer); @@ -175,7 +190,7 @@ public: private: JSRuntime *jsrun; JSContext *jsctx; - JSObject *jsglb; + JS::Heap<JSObject*> *jsglb; JSAutoCompartment *jsac; }; |