aboutsummaryrefslogtreecommitdiff
path: root/libraries/libproxy/libproxy-mozjs2.patch
blob: 2297f69342740811283f711e69541553c90bb607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Index: libproxy/cmake/modules/pacrunner_mozjs.cmk
===================================================================
--- libproxy/cmake/modules/pacrunner_mozjs.cmk	(revision 779)
+++ libproxy/cmake/modules/pacrunner_mozjs.cmk	(working copy)
@@ -14,6 +14,10 @@
     if(MOZJS_FOUND)
       include_directories(${MOZJS_INCLUDE_DIRS})
       link_directories(${MOZJS_LIBRARY_DIRS})
+      pkg_search_module(MOZJS2 mozilla-js>=2.0b10)
+      if(MOZJS2_FOUND)
+        add_definitions(-DHAVE_MOZJS_2)
+      endif(MOZJS2_FOUND)
     else()
       set(MOZJS_FOUND 0)
     endif()
Index: libproxy/modules/pacrunner_mozjs.cpp
===================================================================
--- libproxy/modules/pacrunner_mozjs.cpp	(revision 779)
+++ libproxy/modules/pacrunner_mozjs.cpp	(working copy)
@@ -42,12 +42,12 @@
 #define INET6_ADDRSTRLEN 46
 #endif
 
-static JSBool dnsResolve(JSContext *cx, JSObject * /*obj*/, uintN /*argc*/, jsval *argv, jsval *rval) {
+static JSBool dnsResolve_(JSContext *cx, jsval hostname, jsval *vp) {
 	// Get hostname argument
-	char *tmp = JS_strdup(cx, JS_GetStringBytes(JS_ValueToString(cx, argv[0])));
+	char *tmp = JS_EncodeString(cx, JS_ValueToString(cx, hostname));
 
 	// Set the default return value
-	*rval = JSVAL_NULL;
+	JS_SET_RVAL(cx, vp, JSVAL_NULL);
 
 	// Look it up
 	struct addrinfo *info = NULL;
@@ -66,7 +66,7 @@
 					NI_NUMERICHOST)) goto out;
 
 	// We succeeded
-	*rval = STRING_TO_JSVAL(JS_NewString(cx, tmp, strlen(tmp)));
+	JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyN(cx, tmp, strlen(tmp))));
 	tmp = NULL;
 
 	out:
@@ -75,15 +75,20 @@
 		return true;
 }
 
-static JSBool myIpAddress(JSContext *cx, JSObject *obj, uintN /*argc*/, jsval * /*argv*/, jsval *rval) {
+static JSBool dnsResolve(JSContext *cx, uintN /*argc*/, jsval *vp) {
+	jsval *argv = JS_ARGV(cx, vp);
+	return dnsResolve_(cx, argv[0], vp);
+}
+
+static JSBool myIpAddress(JSContext *cx, uintN /*argc*/, jsval *vp) {
 	char *hostname = (char *) JS_malloc(cx, 1024);
 	if (!gethostname(hostname, 1023)) {
-		JSString *myhost = JS_NewString(cx, hostname, strlen(hostname));
+		JSString *myhost = JS_NewStringCopyN(cx, hostname, strlen(hostname));
 		jsval arg = STRING_TO_JSVAL(myhost);
-		return dnsResolve(cx, obj, 1, &arg, rval);
+		return dnsResolve_(cx, 1, &arg);
 	}
 	JS_free(cx, hostname);
-	*rval = JSVAL_NULL;
+	JS_SET_RVAL(cx, vp, JSVAL_NULL);
 	return true;
 }
 
@@ -111,7 +116,11 @@
 	    //JS_SetOptions(this->jsctx, JSOPTION_VAROBJFIX);
 	    //JS_SetVersion(this->jsctx, JSVERSION_LATEST);
 	    //JS_SetErrorReporter(cx, reportError);
+		#ifdef HAVE_MOZJS_2
+		if (!(this->jsglb = JS_NewCompartmentAndGlobalObject(this->jsctx, &cls, NULL))) goto error;
+		#else
 		if (!(this->jsglb = JS_NewObject(this->jsctx, &cls, NULL, NULL))) goto error;
+		#endif
 		if (!JS_InitStandardClasses(this->jsctx, this->jsglb))            goto error;
 
 		// Define Javascript functions
@@ -147,15 +156,19 @@
 			throw bad_alloc();
 		}
 		jsval args[2] = {
-			STRING_TO_JSVAL(JS_NewString(this->jsctx, tmpurl, strlen(tmpurl))),
-			STRING_TO_JSVAL(JS_NewString(this->jsctx, tmphost, strlen(tmphost)))
+			STRING_TO_JSVAL(JS_NewStringCopyN(this->jsctx, tmpurl, strlen(tmpurl))),
+			STRING_TO_JSVAL(JS_NewStringCopyN(this->jsctx, tmphost, strlen(tmphost)))
 		};
 
 		// Find the proxy (call FindProxyForURL())
 		jsval rval;
 		JSBool result = JS_CallFunctionName(this->jsctx, this->jsglb, "FindProxyForURL", 2, args, &rval);
 		if (!result) return "";
-		string answer = string(JS_GetStringBytes(JS_ValueToString(this->jsctx, rval)));
+		
+		char * tmpanswer = JS_EncodeString(this->jsctx, JS_ValueToString(this->jsctx, rval));
+		string answer = string(tmpanswer);
+		JS_free(this->jsctx, tmpanswer);
+
 		if (answer == "undefined") return "";
 		return answer;
 	}