diff --git a/js/src/jit-test/tests/parser/bug-1470992.js b/js/src/jit-test/tests/parser/bug-1470992.js new file mode 100644 index 000000000000..e03828418ed7 --- /dev/null +++ b/js/src/jit-test/tests/parser/bug-1470992.js @@ -0,0 +1,5 @@ +if (helperThreadCount() === 0) + quit(); + +offThreadCompileModule("export { x };"); +gcslice(10); diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index a478caf2bcbe..66a76ec626e0 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -6102,34 +6102,13 @@ JS_DecodeBytes(JSContext* cx, const char* src, size_t srclen, char16_t* dst, siz return true; } -static char* -EncodeLatin1(JSContext* cx, JSString* str) -{ - JSLinearString* linear = str->ensureLinear(cx); - if (!linear) - return nullptr; - - JS::AutoCheckCannotGC nogc; - if (linear->hasTwoByteChars()) - return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->twoByteRange(nogc)).c_str(); - - size_t len = str->length(); - Latin1Char* buf = cx->pod_malloc(len + 1); - if (!buf) - return nullptr; - - mozilla::PodCopy(buf, linear->latin1Chars(nogc), len); - buf[len] = '\0'; - return reinterpret_cast(buf); -} - JS_PUBLIC_API(char*) JS_EncodeString(JSContext* cx, JSString* str) { AssertHeapIsIdle(); CHECK_REQUEST(cx); - return EncodeLatin1(cx, str); + return js::EncodeLatin1(cx, str).release(); } JS_PUBLIC_API(char*) diff --git a/js/src/vm/JSAtom.cpp b/js/src/vm/JSAtom.cpp index 0677621a9817..648c07ea8141 100644 --- a/js/src/vm/JSAtom.cpp +++ b/js/src/vm/JSAtom.cpp @@ -121,7 +121,8 @@ js::AtomToPrintableString(JSContext* cx, JSAtom* atom, JSAutoByteString* bytes) JSString* str = QuoteString(cx, atom, 0); if (!str) return nullptr; - return bytes->encodeLatin1(cx, str); + bytes->initBytes(EncodeLatin1(cx, str)); + return bytes->ptr(); } #define DEFINE_PROTO_STRING(name,init,clasp) const char js_##name##_str[] = #name; diff --git a/js/src/vm/StringType.cpp b/js/src/vm/StringType.cpp index 8b8d49d4f7d4..12a4b3db33f0 100644 --- a/js/src/vm/StringType.cpp +++ b/js/src/vm/StringType.cpp @@ -2007,6 +2007,29 @@ JSString::fillWithRepresentatives(JSContext* cx, HandleArrayObject array) /*** Conversions *********************************************************************************/ +UniqueChars +js::EncodeLatin1(JSContext* cx, JSString* str) +{ + JSLinearString* linear = str->ensureLinear(cx); + if (!linear) + return nullptr; + + JS::AutoCheckCannotGC nogc; + if (linear->hasTwoByteChars()) { + Latin1CharsZ chars = JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->twoByteRange(nogc)); + return UniqueChars(chars.c_str()); + } + + size_t len = str->length(); + Latin1Char* buf = cx->pod_malloc(len + 1); + if (!buf) + return nullptr; + + mozilla::PodCopy(buf, linear->latin1Chars(nogc), len); + buf[len] = '\0'; + return UniqueChars(reinterpret_cast(buf)); +} + const char* js::ValueToPrintableLatin1(JSContext* cx, const Value& vArg, JSAutoByteString* bytes, bool asSource) diff --git a/js/src/vm/StringType.h b/js/src/vm/StringType.h index 3e39e4224dc6..8638fe7ff8b3 100644 --- a/js/src/vm/StringType.h +++ b/js/src/vm/StringType.h @@ -1589,6 +1589,12 @@ SubstringKernel(JSContext* cx, HandleString str, int32_t beginInt, int32_t lengt /*** Conversions *********************************************************************************/ +/* + * Convert a string to a printable C string. + */ +UniqueChars +EncodeLatin1(JSContext* cx, JSString* str); + /* * Convert a value to a printable C string. *