libproxy-0.4.17-mozjs68.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. From 6c9e48accddb90eef8412bef3ccc29594935d3b3 Mon Sep 17 00:00:00 2001
  2. From: Iain Lane <iain@orangesquash.org.uk>
  3. Date: Wed, 11 Mar 2020 11:54:52 +0000
  4. Subject: [PATCH] mozjs: Port to mozjs 68
  5. There are a number of API changes that need to be adapted to, notably
  6. - JS_EncodeString is gone; need to use JS_EncodeStringToUTF8 now which
  7. requires a rooted object to be passed in.
  8. - JS_free is gone
  9. The pkg-config file ships some flags which need to be supplied to the
  10. build.
  11. ---
  12. libproxy/cmake/modules/pacrunner_mozjs.cmk | 6 ++-
  13. libproxy/modules/pacrunner_mozjs.cpp | 56 ++++++++++++++--------
  14. 2 files changed, 41 insertions(+), 21 deletions(-)
  15. diff --git a/libproxy/cmake/modules/pacrunner_mozjs.cmk b/libproxy/cmake/modules/pacrunner_mozjs.cmk
  16. index 871cc85..2cc3c51 100644
  17. --- a/libproxy/cmake/modules/pacrunner_mozjs.cmk
  18. +++ b/libproxy/cmake/modules/pacrunner_mozjs.cmk
  19. @@ -9,8 +9,12 @@ if(WIN32)
  20. elseif(NOT APPLE)
  21. option(WITH_MOZJS "Search for MOZJS package" ON)
  22. if (WITH_MOZJS)
  23. - pkg_search_module(MOZJS mozjs-60)
  24. + pkg_search_module(MOZJS mozjs-68)
  25. if(MOZJS_FOUND)
  26. + foreach(OPT ${MOZJS_CFLAGS})
  27. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPT}")
  28. + endforeach()
  29. + message("mozjs is " ${CMAKE_CXX_FLAGS})
  30. include_directories(${MOZJS_INCLUDE_DIRS})
  31. link_directories(${MOZJS_LIBRARY_DIRS})
  32. else()
  33. diff --git a/libproxy/modules/pacrunner_mozjs.cpp b/libproxy/modules/pacrunner_mozjs.cpp
  34. index 38e7d46..37e1b42 100644
  35. --- a/libproxy/modules/pacrunner_mozjs.cpp
  36. +++ b/libproxy/modules/pacrunner_mozjs.cpp
  37. @@ -37,6 +37,9 @@ using namespace libproxy;
  38. #pragma GCC diagnostic error "-Winvalid-offsetof"
  39. #include <js/Initialization.h>
  40. #include <js/CallArgs.h>
  41. +#include <js/CompilationAndEvaluation.h>
  42. +#include <js/MemoryFunctions.h>
  43. +#include <js/SourceText.h>
  44. #include "pacutils.h"
  45. @@ -49,19 +52,21 @@ using namespace libproxy;
  46. #endif
  47. static void dnsResolve_(JSContext *cx, JSString *hostname, JS::CallArgs *argv) {
  48. + char *tmp;
  49. // Get hostname argument
  50. - char *tmp = JS_EncodeString(cx, hostname);
  51. + JS::RootedString str(cx, hostname);
  52. + JS::UniqueChars chars = JS_EncodeStringToUTF8(cx, str);
  53. + const char *val = chars.get();
  54. // Set the default return value
  55. argv->rval().setNull();
  56. // Look it up
  57. struct addrinfo *info = nullptr;
  58. - if (getaddrinfo(tmp, NULL, NULL, &info))
  59. + if (getaddrinfo(val, NULL, NULL, &info))
  60. goto out;
  61. // Allocate the IP address
  62. - JS_free(cx, tmp);
  63. tmp = (char *) JS_malloc(cx, INET6_ADDRSTRLEN+1);
  64. memset(tmp, 0, INET6_ADDRSTRLEN+1);
  65. @@ -77,7 +82,6 @@ static void dnsResolve_(JSContext *cx, JSString *hostname, JS::CallArgs *argv) {
  66. out:
  67. if (info) freeaddrinfo(info);
  68. - JS_free(cx, tmp);
  69. }
  70. static bool dnsResolve(JSContext *cx, unsigned argc, JS::Value *vp) {
  71. @@ -121,29 +125,40 @@ class mozjs_pacrunner : public pacrunner {
  72. if (!JS::InitSelfHostedCode(this->jsctx)) goto error;
  73. JS::RootedValue rval(this->jsctx);
  74. - JS::CompartmentOptions compart_opts;
  75. + JS::RealmOptions realm_opts;
  76. this->jsglb = new JS::Heap<JSObject*>(JS_NewGlobalObject(
  77. this->jsctx, &cls,
  78. nullptr, JS::DontFireOnNewGlobalHook,
  79. - compart_opts));
  80. + realm_opts));
  81. if (!(this->jsglb)) goto error;
  82. JS::RootedObject global(this->jsctx,this->jsglb->get());
  83. - if (!(this->jsac = new JSAutoCompartment(this->jsctx, global))) goto error;
  84. - if (!JS_InitStandardClasses(this->jsctx, global)) goto error;
  85. + if (!(this->jsar = new JSAutoRealm(this->jsctx, global))) goto error;
  86. // Define Javascript functions
  87. JS_DefineFunction(this->jsctx, global, "dnsResolve", dnsResolve, 1, 0);
  88. JS_DefineFunction(this->jsctx, global, "myIpAddress", myIpAddress, 0, 0);
  89. JS::CompileOptions options(this->jsctx);
  90. - options.setUTF8(true);
  91. - JS::Evaluate(this->jsctx, options, JAVASCRIPT_ROUTINES,
  92. - strlen(JAVASCRIPT_ROUTINES), JS::MutableHandleValue(&rval));
  93. + JS::SourceText<mozilla::Utf8Unit> routines, pac_source;
  94. + if (!routines.init(this->jsctx,
  95. + JAVASCRIPT_ROUTINES,
  96. + strlen(JAVASCRIPT_ROUTINES),
  97. + JS::SourceOwnership::Borrowed))
  98. + goto error;
  99. +
  100. + if (!pac_source.init(this->jsctx,
  101. + pac.c_str(),
  102. + pac.length(),
  103. + JS::SourceOwnership::Borrowed))
  104. + goto error;
  105. +
  106. +
  107. + JS::Evaluate(this->jsctx, options, routines, JS::MutableHandleValue(&rval));
  108. // Add PAC to the environment
  109. - JS::Evaluate(this->jsctx, options, pac.c_str(), pac.length(), JS::MutableHandleValue(&rval));
  110. + JS::Evaluate(this->jsctx, options, pac_source, JS::MutableHandleValue(&rval));
  111. return;
  112. }
  113. error:
  114. @@ -152,7 +167,7 @@ class mozjs_pacrunner : public pacrunner {
  115. }
  116. ~mozjs_pacrunner() {
  117. - if (this->jsac) delete this->jsac;
  118. + if (this->jsar) delete this->jsar;
  119. if (this->jsglb) delete this->jsglb;
  120. if (this->jsctx) JS_DestroyContext(this->jsctx);
  121. JS_ShutDown();
  122. @@ -160,11 +175,9 @@ class mozjs_pacrunner : public pacrunner {
  123. string run(const url& url_) throw (bad_alloc) {
  124. // Build arguments to the FindProxyForURL() function
  125. - char *tmpurl = JS_strdup(this->jsctx, url_.to_string().c_str());
  126. - char *tmphost = JS_strdup(this->jsctx, url_.get_host().c_str());
  127. + const char *tmpurl = url_.to_string().c_str();
  128. + const char *tmphost = url_.get_host().c_str();
  129. if (!tmpurl || !tmphost) {
  130. - if (tmpurl) JS_free(this->jsctx, tmpurl);
  131. - if (tmphost) JS_free(this->jsctx, tmphost);
  132. throw bad_alloc();
  133. }
  134. JS::AutoValueArray<2> args(this->jsctx);
  135. @@ -176,10 +189,13 @@ class mozjs_pacrunner : public pacrunner {
  136. JS::RootedObject global(this->jsctx,this->jsglb->get());
  137. bool result = JS_CallFunctionName(this->jsctx, global, "FindProxyForURL", args, &rval);
  138. if (!result) return "";
  139. + if (!rval.isString())
  140. + return "";
  141. - char * tmpanswer = JS_EncodeString(this->jsctx, rval.toString());
  142. + JS::RootedString s(this->jsctx, rval.toString());
  143. + JS::UniqueChars chars = JS_EncodeStringToUTF8(this->jsctx, s);
  144. + const char *tmpanswer = chars.get();
  145. string answer = string(tmpanswer);
  146. - JS_free(this->jsctx, tmpanswer);
  147. if (answer == "undefined") return "";
  148. return answer;
  149. @@ -188,7 +204,7 @@ class mozjs_pacrunner : public pacrunner {
  150. private:
  151. JSContext *jsctx;
  152. JS::Heap<JSObject*> *jsglb;
  153. - JSAutoCompartment *jsac;
  154. + JSAutoRealm *jsar;
  155. };
  156. PX_PACRUNNER_MODULE_EZ(mozjs, "JS_DefineFunction", "mozjs");