Blame SOURCES/libproxy-0.4.15-mozjs68.patch

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