Bug 1008818: Factor AppendUseStrictSource out of AsmJS*ToString functions; r=luke

This commit is contained in:
Benjamin Bouvier 2014-05-14 15:59:12 +02:00
parent ab4522a6b1
commit 1dc203b147

View File

@ -861,6 +861,28 @@ js::IsAsmJSModule(HandleFunction fun)
return fun->isNative() && fun->maybeNative() == LinkAsmJS;
}
static bool
AppendUseStrictSource(JSContext *cx, HandleFunction fun, Handle<JSFlatString*> src, StringBuffer &out)
{
// We need to add "use strict" in the body right after the opening
// brace.
size_t bodyStart = 0, bodyEnd;
// No need to test for functions created with the Function ctor as
// these don't implicitly inherit the "use strict" context. Strict mode is
// enabled for functions created with the Function ctor only if they begin with
// 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))
return false;
return out.append(chars, bodyStart) &&
out.append("\n\"use strict\";\n") &&
out.append(chars + bodyStart, src->length() - bodyStart);
}
JSString *
js::AsmJSModuleToString(JSContext *cx, HandleFunction fun, bool addParenToLambda)
{
@ -910,26 +932,8 @@ js::AsmJSModuleToString(JSContext *cx, HandleFunction fun, bool addParenToLambda
return nullptr;
if (module.strict()) {
// We need to add "use strict" in the body right after the opening
// brace.
size_t bodyStart = 0, bodyEnd;
// No need to test for functions created with the Function ctor as
// these don't implicitly inherit the "use strict" context. Strict mode is
// enabled for functions created with the Function ctor only if they begin with
// 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 (!AppendUseStrictSource(cx, fun, src, out))
return nullptr;
if (!out.append(chars, bodyStart) ||
!out.append("\n\"use strict\";\n") ||
!out.append(chars + bodyStart, src->length() - bodyStart))
{
return nullptr;
}
} else {
if (!out.append(src->chars(), src->length()))
return nullptr;
@ -997,28 +1001,19 @@ js::AsmJSFunctionToString(JSContext *cx, HandleFunction fun)
return nullptr;
if (module.strict()) {
// Functions defined in a strict module inherit the strict context, thus we need to add
// "use strict" in the body right after the opening brace.
size_t bodyStart = 0, bodyEnd;
// AppendUseStrictSource expects its input to start right after the
// function name, so split the source chars from the src into two parts:
// the function name and the rest (arguments + body).
// asm.js functions can't be anonymous
JS_ASSERT(fun->atom());
if (!out.append(fun->atom()))
return nullptr;
// FindBody expects its input to start right after the function name, so
// split the source chars from the src into two parts: the function
// name and the rest (arguments + body).
JS_ASSERT(fun->atom()); // asm.js functions can't be anonymous
size_t nameEnd = begin + fun->atom()->length();
Rooted<JSFlatString*> src(cx, source->substring(cx, nameEnd, end));
ConstTwoByteChars chars(src->chars(), src->length());
if (!FindBody(cx, fun, chars, src->length(), &bodyStart, &bodyEnd))
if (!AppendUseStrictSource(cx, fun, src, out))
return nullptr;
if (!out.append(fun->atom()) ||
!out.append(chars, bodyStart) ||
!out.append("\n\"use strict\";\n") ||
!out.append(chars + bodyStart, src->length() - bodyStart))
{
return nullptr;
}
} else {
Rooted<JSFlatString*> src(cx, source->substring(cx, begin, end));
if (!src)