167b4f
From 6c9e48accddb90eef8412bef3ccc29594935d3b3 Mon Sep 17 00:00:00 2001
167b4f
From: Iain Lane <iain@orangesquash.org.uk>
167b4f
Date: Wed, 11 Mar 2020 11:54:52 +0000
167b4f
Subject: [PATCH] mozjs: Port to mozjs 68
167b4f
167b4f
There are a number of API changes that need to be adapted to, notably
167b4f
167b4f
  - JS_EncodeString is gone; need to use JS_EncodeStringToUTF8 now which
167b4f
    requires a rooted object to be passed in.
167b4f
  - JS_free is gone
167b4f
167b4f
The pkg-config file ships some flags which need to be supplied to the
167b4f
build.
167b4f
---
167b4f
 libproxy/cmake/modules/pacrunner_mozjs.cmk |  6 ++-
167b4f
 libproxy/modules/pacrunner_mozjs.cpp       | 56 ++++++++++++++--------
167b4f
 2 files changed, 41 insertions(+), 21 deletions(-)
167b4f
167b4f
diff --git a/libproxy/cmake/modules/pacrunner_mozjs.cmk b/libproxy/cmake/modules/pacrunner_mozjs.cmk
167b4f
index 871cc85..2cc3c51 100644
167b4f
--- a/libproxy/cmake/modules/pacrunner_mozjs.cmk
167b4f
+++ b/libproxy/cmake/modules/pacrunner_mozjs.cmk
167b4f
@@ -9,8 +9,12 @@ 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-60)
167b4f
+    pkg_search_module(MOZJS mozjs-68)
167b4f
     if(MOZJS_FOUND)
167b4f
+      foreach(OPT ${MOZJS_CFLAGS})
167b4f
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPT}")
167b4f
+      endforeach()
167b4f
+      message("mozjs is " ${CMAKE_CXX_FLAGS})
167b4f
       include_directories(${MOZJS_INCLUDE_DIRS})
167b4f
       link_directories(${MOZJS_LIBRARY_DIRS})
167b4f
     else()
167b4f
diff --git a/libproxy/modules/pacrunner_mozjs.cpp b/libproxy/modules/pacrunner_mozjs.cpp
167b4f
index 38e7d46..37e1b42 100644
167b4f
--- a/libproxy/modules/pacrunner_mozjs.cpp
167b4f
+++ b/libproxy/modules/pacrunner_mozjs.cpp
167b4f
@@ -37,6 +37,9 @@ using namespace libproxy;
167b4f
 #pragma GCC diagnostic error "-Winvalid-offsetof"
167b4f
 #include <js/Initialization.h>
167b4f
 #include <js/CallArgs.h>
167b4f
+#include <js/CompilationAndEvaluation.h>
167b4f
+#include <js/MemoryFunctions.h>
167b4f
+#include <js/SourceText.h>
167b4f
 
167b4f
 #include "pacutils.h"
167b4f
 
167b4f
@@ -49,19 +52,21 @@ using namespace libproxy;
167b4f
 #endif
167b4f
 
167b4f
 static void dnsResolve_(JSContext *cx, JSString *hostname, JS::CallArgs *argv) {
167b4f
+	char *tmp;
167b4f
 	// Get hostname argument
167b4f
-	char *tmp = JS_EncodeString(cx, hostname);
167b4f
+	JS::RootedString str(cx, hostname);
167b4f
+	JS::UniqueChars chars = JS_EncodeStringToUTF8(cx, str);
167b4f
+	const char *val = chars.get();
167b4f
 
167b4f
 	// Set the default return value
167b4f
 	argv->rval().setNull();
167b4f
 
167b4f
 	// Look it up
167b4f
 	struct addrinfo *info = nullptr;
167b4f
-	if (getaddrinfo(tmp, NULL, NULL, &info))
167b4f
+	if (getaddrinfo(val, NULL, NULL, &info))
167b4f
 		goto out;
167b4f
 
167b4f
 	// Allocate the IP address
167b4f
-	JS_free(cx, tmp);
167b4f
 	tmp = (char *) JS_malloc(cx, INET6_ADDRSTRLEN+1);
167b4f
 	memset(tmp, 0, INET6_ADDRSTRLEN+1);
167b4f
 
167b4f
@@ -77,7 +82,6 @@ static void dnsResolve_(JSContext *cx, JSString *hostname, JS::CallArgs *argv) {
167b4f
 
167b4f
 	out:
167b4f
 		if (info) freeaddrinfo(info);
167b4f
-		JS_free(cx, tmp);
167b4f
 }
167b4f
 
167b4f
 static bool dnsResolve(JSContext *cx, unsigned argc, JS::Value *vp) {
167b4f
@@ -121,29 +125,40 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 			if (!JS::InitSelfHostedCode(this->jsctx)) goto error;
167b4f
 
167b4f
 			JS::RootedValue  rval(this->jsctx);
167b4f
-			JS::CompartmentOptions compart_opts;
167b4f
+			JS::RealmOptions realm_opts;
167b4f
 
167b4f
 			this->jsglb = new JS::Heap<JSObject*>(JS_NewGlobalObject(
167b4f
 								  this->jsctx, &cls,
167b4f
 								  nullptr, JS::DontFireOnNewGlobalHook,
167b4f
-								  compart_opts));
167b4f
+								  realm_opts));
167b4f
 
167b4f
 			if (!(this->jsglb)) goto error;
167b4f
 			JS::RootedObject global(this->jsctx,this->jsglb->get());
167b4f
-			if (!(this->jsac = new JSAutoCompartment(this->jsctx,  global))) goto error;
167b4f
-			if (!JS_InitStandardClasses(this->jsctx, global))            goto error;
167b4f
+			if (!(this->jsar = new JSAutoRealm(this->jsctx,  global))) goto error;
167b4f
 
167b4f
 			// Define Javascript functions
167b4f
 			JS_DefineFunction(this->jsctx, global, "dnsResolve", dnsResolve, 1, 0);
167b4f
 			JS_DefineFunction(this->jsctx, global, "myIpAddress", myIpAddress, 0, 0);
167b4f
 			JS::CompileOptions options(this->jsctx);
167b4f
-			options.setUTF8(true);
167b4f
 
167b4f
-			JS::Evaluate(this->jsctx, options, JAVASCRIPT_ROUTINES,
167b4f
-				     strlen(JAVASCRIPT_ROUTINES), JS::MutableHandleValue(&rval));
167b4f
+			JS::SourceText<mozilla::Utf8Unit> routines, pac_source;
167b4f
+			if (!routines.init(this->jsctx,
167b4f
+					   JAVASCRIPT_ROUTINES,
167b4f
+					   strlen(JAVASCRIPT_ROUTINES),
167b4f
+					   JS::SourceOwnership::Borrowed))
167b4f
+				goto error;
167b4f
+
167b4f
+			if (!pac_source.init(this->jsctx,
167b4f
+					     pac.c_str(),
167b4f
+					     pac.length(),
167b4f
+					     JS::SourceOwnership::Borrowed))
167b4f
+				goto error;
167b4f
+
167b4f
+
167b4f
+			JS::Evaluate(this->jsctx, options, routines, JS::MutableHandleValue(&rval));
167b4f
 
167b4f
 			// Add PAC to the environment
167b4f
-			JS::Evaluate(this->jsctx, options, pac.c_str(), pac.length(), JS::MutableHandleValue(&rval));
167b4f
+			JS::Evaluate(this->jsctx, options, pac_source, JS::MutableHandleValue(&rval));
167b4f
 			return;
167b4f
 		}
167b4f
 		error:
167b4f
@@ -152,7 +167,7 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 	}
167b4f
 
167b4f
 	~mozjs_pacrunner() {
167b4f
-		if (this->jsac) delete this->jsac;
167b4f
+		if (this->jsar) delete this->jsar;
167b4f
 		if (this->jsglb) delete this->jsglb;
167b4f
 		if (this->jsctx) JS_DestroyContext(this->jsctx);
167b4f
 		JS_ShutDown();
167b4f
@@ -160,11 +175,9 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 
167b4f
 	string run(const url& url_) throw (bad_alloc) {
167b4f
 		// Build arguments to the FindProxyForURL() function
167b4f
-		char *tmpurl  = JS_strdup(this->jsctx, url_.to_string().c_str());
167b4f
-		char *tmphost = JS_strdup(this->jsctx, url_.get_host().c_str());
167b4f
+		const char *tmpurl  = url_.to_string().c_str();
167b4f
+		const char *tmphost = url_.get_host().c_str();
167b4f
 		if (!tmpurl || !tmphost) {
167b4f
-			if (tmpurl) JS_free(this->jsctx, tmpurl);
167b4f
-			if (tmphost) JS_free(this->jsctx, tmphost);
167b4f
 			throw bad_alloc();
167b4f
 		}
167b4f
 		JS::AutoValueArray<2> args(this->jsctx);
167b4f
@@ -176,10 +189,13 @@ 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
+		if (!rval.isString())
167b4f
+			return "";
167b4f
 
167b4f
-		char * tmpanswer = JS_EncodeString(this->jsctx, rval.toString());
167b4f
+		JS::RootedString s(this->jsctx, rval.toString());
167b4f
+		JS::UniqueChars chars = JS_EncodeStringToUTF8(this->jsctx, s);
167b4f
+		const char *tmpanswer = chars.get();
167b4f
 		string answer = string(tmpanswer);
167b4f
-		JS_free(this->jsctx, tmpanswer);
167b4f
 
167b4f
 		if (answer == "undefined") return "";
167b4f
 		return answer;
167b4f
@@ -188,7 +204,7 @@ class mozjs_pacrunner : public pacrunner {
167b4f
 private:
167b4f
 	JSContext *jsctx;
167b4f
 	JS::Heap<JSObject*> *jsglb;
167b4f
-	JSAutoCompartment *jsac;
167b4f
+	JSAutoRealm *jsar;
167b4f
 };
167b4f
 
167b4f
 PX_PACRUNNER_MODULE_EZ(mozjs, "JS_DefineFunction", "mozjs");