167b4f
From f594720280b2e40d81fa6e286a0ef8868687ef7e Mon Sep 17 00:00:00 2001
167b4f
From: Pierre Lejeune <superheron@gmail.com>
167b4f
Date: Sat, 30 Jun 2018 21:10:06 +0200
167b4f
Subject: [PATCH] Build with mozjs-52
167b4f
167b4f
Fixes #71
167b4f
---
167b4f
 libproxy/cmake/modules/pacrunner_mozjs.cmk |  2 +-
167b4f
 libproxy/modules/pacrunner_mozjs.cpp       | 19 +++++++------------
167b4f
 2 files changed, 8 insertions(+), 13 deletions(-)
167b4f
167b4f
diff --git a/libproxy/cmake/modules/pacrunner_mozjs.cmk b/libproxy/cmake/modules/pacrunner_mozjs.cmk
167b4f
index c2ae3db..20857fb 100644
167b4f
--- a/libproxy/cmake/modules/pacrunner_mozjs.cmk
167b4f
+++ b/libproxy/cmake/modules/pacrunner_mozjs.cmk
167b4f
@@ -9,7 +9,7 @@ if(WIN32)
167b4f
 elseif(NOT APPLE)
167b4f
   option(WITH_MOZJS "Search for MOZJS package" ON)
167b4f
   if (WITH_MOZJS)
167b4f
-    pkg_search_module(MOZJS mozjs-38)
167b4f
+    pkg_search_module(MOZJS mozjs-52)
167b4f
     if(MOZJS_FOUND)
167b4f
       include_directories(${MOZJS_INCLUDE_DIRS})
167b4f
       link_directories(${MOZJS_LIBRARY_DIRS})
167b4f
diff --git a/libproxy/modules/pacrunner_mozjs.cpp b/libproxy/modules/pacrunner_mozjs.cpp
167b4f
index a70b2e9..ed07c69 100644
167b4f
--- a/libproxy/modules/pacrunner_mozjs.cpp
167b4f
+++ b/libproxy/modules/pacrunner_mozjs.cpp
167b4f
@@ -35,6 +35,7 @@ using namespace libproxy;
167b4f
 #pragma GCC diagnostic ignored "-Winvalid-offsetof"
167b4f
 #include <jsapi.h>
167b4f
 #pragma GCC diagnostic error "-Winvalid-offsetof"
167b4f
+#include <js/Initialization.h>
167b4f
 #include <js/CallArgs.h>
167b4f
 
167b4f
 #include "pacutils.h"
167b4f
@@ -111,17 +112,14 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 	mozjs_pacrunner(string pac, const url& pacurl) throw (bad_alloc) : pacrunner(pac, pacurl) {
167b4f
 
167b4f
 		// Set defaults
167b4f
-		this->jsrun = nullptr;
167b4f
 		this->jsctx = nullptr;
167b4f
 		JS_Init();
167b4f
 
167b4f
-		// Initialize Javascript runtime environment
167b4f
-		if (!(this->jsrun = JS_NewRuntime(1024 * 1024)))                  goto error;
167b4f
-		if (!(this->jsctx = JS_NewContext(this->jsrun, 1024 * 1024)))     goto error;
167b4f
+		// Initialize Javascript context
167b4f
+		if (!(this->jsctx = JS_NewContext(1024 * 1024)))     goto error;
167b4f
 		{
167b4f
 			JS::RootedValue  rval(this->jsctx);
167b4f
 			JS::CompartmentOptions compart_opts;
167b4f
-			compart_opts.setVersion(JSVERSION_LATEST);
167b4f
 
167b4f
 			this->jsglb = new JS::Heap<JSObject*>(JS_NewGlobalObject(
167b4f
 								  this->jsctx, &cls,
167b4f
@@ -139,16 +137,15 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 			JS::CompileOptions options(this->jsctx);
167b4f
 			options.setUTF8(true);
167b4f
 
167b4f
-			JS::Evaluate(this->jsctx, global, options, JAVASCRIPT_ROUTINES,
167b4f
-				     strlen(JAVASCRIPT_ROUTINES), &rval);
167b4f
+			JS::Evaluate(this->jsctx, options, JAVASCRIPT_ROUTINES,
167b4f
+				     strlen(JAVASCRIPT_ROUTINES), JS::MutableHandleValue(&rval));
167b4f
 
167b4f
 			// Add PAC to the environment
167b4f
-			JS::Evaluate(this->jsctx, global, options, pac.c_str(), pac.length(), &rval);
167b4f
+			JS::Evaluate(this->jsctx, options, pac.c_str(), pac.length(), JS::MutableHandleValue(&rval));
167b4f
 			return;
167b4f
 		}
167b4f
 		error:
167b4f
 			if (this->jsctx) JS_DestroyContext(this->jsctx);
167b4f
-			if (this->jsrun) JS_DestroyRuntime(this->jsrun);
167b4f
 			throw bad_alloc();
167b4f
 	}
167b4f
 
167b4f
@@ -156,7 +153,6 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 		if (this->jsac) delete this->jsac;
167b4f
 		if (this->jsglb) delete this->jsglb;
167b4f
 		if (this->jsctx) JS_DestroyContext(this->jsctx);
167b4f
-		if (this->jsrun) JS_DestroyRuntime(this->jsrun);
167b4f
 		JS_ShutDown();
167b4f
 	}
167b4f
 
167b4f
@@ -178,7 +174,7 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 		JS::RootedObject global(this->jsctx,this->jsglb->get());
167b4f
 		bool result = JS_CallFunctionName(this->jsctx, global, "FindProxyForURL", args, &rval);
167b4f
 		if (!result) return "";
167b4f
-		
167b4f
+
167b4f
 		char * tmpanswer = JS_EncodeString(this->jsctx, rval.toString());
167b4f
 		string answer = string(tmpanswer);
167b4f
 		JS_free(this->jsctx, tmpanswer);
167b4f
@@ -188,7 +184,6 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 	}
167b4f
 
167b4f
 private:
167b4f
-	JSRuntime *jsrun;
167b4f
 	JSContext *jsctx;
167b4f
 	JS::Heap<JSObject*> *jsglb;
167b4f
 	JSAutoCompartment *jsac;
167b4f
From a9b052c6e30101fb0b702917f245a3e2a2f08366 Mon Sep 17 00:00:00 2001
167b4f
From: Laurent Bigonville <bigon@bigon.be>
167b4f
Date: Tue, 2 Oct 2018 10:22:56 +0200
167b4f
Subject: [PATCH] Add call to JS::InitSelfHostedCode()
167b4f
167b4f
This is needed otherwise mozjs crashes
167b4f
---
167b4f
 libproxy/modules/pacrunner_mozjs.cpp | 2 ++
167b4f
 1 file changed, 2 insertions(+)
167b4f
167b4f
diff --git a/libproxy/modules/pacrunner_mozjs.cpp b/libproxy/modules/pacrunner_mozjs.cpp
167b4f
index ed07c69..38e7d46 100644
167b4f
--- a/libproxy/modules/pacrunner_mozjs.cpp
167b4f
+++ b/libproxy/modules/pacrunner_mozjs.cpp
167b4f
@@ -118,6 +118,8 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 		// Initialize Javascript context
167b4f
 		if (!(this->jsctx = JS_NewContext(1024 * 1024)))     goto error;
167b4f
 		{
167b4f
+			if (!JS::InitSelfHostedCode(this->jsctx)) goto error;
167b4f
+
167b4f
 			JS::RootedValue  rval(this->jsctx);
167b4f
 			JS::CompartmentOptions compart_opts;
167b4f