Bug 1027528 part 5 - Make FunctionToString handle Latin1 strings. r=luke

This commit is contained in:
Jan de Mooij 2014-06-20 14:44:33 +02:00
parent f698872409
commit 43da44b220
3 changed files with 22 additions and 16 deletions

View File

@ -892,13 +892,12 @@ AppendUseStrictSource(JSContext *cx, HandleFunction fun, Handle<JSFlatString*> s
// the "use strict" directive, but these functions won't validate as asm.js
// modules.
ConstTwoByteChars chars(src->chars(), src->length());
if (!FindBody(cx, fun, chars, src->length(), &bodyStart, &bodyEnd))
if (!FindBody(cx, fun, src, &bodyStart, &bodyEnd))
return false;
return out.append(chars, bodyStart) &&
return out.appendSubstring(src, 0, bodyStart) &&
out.append("\n\"use strict\";\n") &&
out.append(chars + bodyStart, src->length() - bodyStart);
out.appendSubstring(src, bodyStart, src->length() - bodyStart);
}
JSString *

View File

@ -55,6 +55,7 @@ using namespace js::frontend;
using mozilla::ArrayLength;
using mozilla::PodCopy;
using mozilla::Range;
using mozilla::RangedPtr;
static bool
fun_getProperty(JSContext *cx, HandleObject obj_, HandleId id, MutableHandleValue vp)
@ -747,8 +748,8 @@ const Class* const js::FunctionClassPtr = &JSFunction::class_;
/* Find the body of a function (not including braces). */
bool
js::FindBody(JSContext *cx, HandleFunction fun, ConstTwoByteChars chars, size_t length,
size_t *bodyStart, size_t *bodyEnd)
js::FindBody(JSContext *cx, HandleFunction fun, HandleLinearString src, size_t *bodyStart,
size_t *bodyEnd)
{
// We don't need principals, since those are only used for error reporting.
CompileOptions options(cx);
@ -759,7 +760,13 @@ js::FindBody(JSContext *cx, HandleFunction fun, ConstTwoByteChars chars, size_t
options.setVersion(fun->nonLazyScript()->getVersion());
AutoKeepAtoms keepAtoms(cx->perThreadData);
TokenStream ts(cx, options, chars.get(), length, nullptr);
AutoStableStringChars stableChars(cx, src);
if (!stableChars.initTwoByte(cx))
return false;
const Range<const jschar> srcChars = stableChars.twoByteRange();
TokenStream ts(cx, options, srcChars.start().get(), srcChars.length(), nullptr);
int nest = 0;
bool onward = true;
// Skip arguments list.
@ -794,7 +801,7 @@ js::FindBody(JSContext *cx, HandleFunction fun, ConstTwoByteChars chars, size_t
*bodyStart = ts.currentToken().pos.begin;
if (braced)
*bodyStart += 1;
ConstTwoByteChars end(chars.get() + length, chars.get(), length);
RangedPtr<const jschar> end = srcChars.end();
if (end[-1] == '}') {
end--;
} else {
@ -802,7 +809,7 @@ js::FindBody(JSContext *cx, HandleFunction fun, ConstTwoByteChars chars, size_t
for (; unicode::IsSpaceOrBOM2(end[-1]); end--)
;
}
*bodyEnd = end - chars;
*bodyEnd = end - srcChars.start();
JS_ASSERT(*bodyStart <= *bodyEnd);
return true;
}
@ -862,7 +869,6 @@ js::FunctionToString(JSContext *cx, HandleFunction fun, bool bodyOnly, bool lamb
if (!src)
return nullptr;
ConstTwoByteChars chars(src->chars(), src->length());
bool exprBody = fun->isExprClosure();
// The source data for functions created by calling the Function
@ -878,7 +884,8 @@ js::FunctionToString(JSContext *cx, HandleFunction fun, bool bodyOnly, bool lamb
// expression closures.
JS_ASSERT_IF(funCon, !fun->isArrow());
JS_ASSERT_IF(funCon, !exprBody);
JS_ASSERT_IF(!funCon && !fun->isArrow(), src->length() > 0 && chars[0] == '(');
JS_ASSERT_IF(!funCon && !fun->isArrow(),
src->length() > 0 && src->latin1OrTwoByteChar(0) == '(');
// If a function inherits strict mode by having scopes above it that
// have "use strict", we insert "use strict" into the body of the
@ -919,7 +926,7 @@ js::FunctionToString(JSContext *cx, HandleFunction fun, bool bodyOnly, bool lamb
// already have a body.
if (!funCon) {
JS_ASSERT(!buildBody);
if (!FindBody(cx, fun, chars, src->length(), &bodyStart, &bodyEnd))
if (!FindBody(cx, fun, src, &bodyStart, &bodyEnd))
return nullptr;
} else {
bodyEnd = src->length();
@ -927,7 +934,7 @@ js::FunctionToString(JSContext *cx, HandleFunction fun, bool bodyOnly, bool lamb
if (addUseStrict) {
// Output source up to beginning of body.
if (!out.append(chars, bodyStart))
if (!out.appendSubstring(src, 0, bodyStart))
return nullptr;
if (exprBody) {
// We can't insert a statement into a function with an
@ -944,7 +951,7 @@ js::FunctionToString(JSContext *cx, HandleFunction fun, bool bodyOnly, bool lamb
// Output just the body (for bodyOnly) or the body and possibly
// closing braces (for addUseStrict).
size_t dependentEnd = bodyOnly ? bodyEnd : src->length();
if (!out.append(chars + bodyStart, dependentEnd - bodyStart))
if (!out.appendSubstring(src, bodyStart, dependentEnd - bodyStart))
return nullptr;
} else {
if (!out.append(src))

View File

@ -567,8 +567,8 @@ CloneFunctionObject(JSContext *cx, HandleFunction fun, HandleObject parent,
extern bool
FindBody(JSContext *cx, HandleFunction fun, ConstTwoByteChars chars, size_t length,
size_t *bodyStart, size_t *bodyEnd);
FindBody(JSContext *cx, HandleFunction fun, HandleLinearString src, size_t *bodyStart,
size_t *bodyEnd);
} // namespace js