Bug 878399: Implement toSource / toString for asm.js modules lambda; r=luke

This commit is contained in:
Benjamin Bouvier 2014-04-02 19:51:53 +02:00
parent 4e4ef12e5e
commit 348bab473e
3 changed files with 95 additions and 3 deletions

View File

@ -23,6 +23,28 @@ var funcBody = 'function f0() {\n\
assertEq(f0.toString(), funcBody);
assertEq(f0.toSource(), funcBody);
var f0 = function() {
"use asm";
function g() {}
return g;
}
funcBody1 = funcBody.replace('function f0','function ');
assertEq(f0.toString(), funcBody1);
assertEq(f0.toSource(), '(' + funcBody1 + ')');
var g = function g0() {
"use asm";
function g() {}
return g;
}
funcBody2 = funcBody.replace('function f0', 'function g0');
assertEq(g.toString(), funcBody2);
assertEq(g.toSource(), '(' + funcBody2 + ')');
f0 = new Function(bodyOnly);
assertEq(f0.toString(), "function anonymous() {\n" + bodyOnly + "\n}");
assertEq(f0.toSource(), "(function anonymous() {\n" + bodyOnly + "\n})");
@ -60,6 +82,28 @@ var funcBody = 'function f1(glob) {\n\
assertEq(f1.toString(), funcBody);
assertEq(f1.toSource(), funcBody);
f1 = function(glob) {
"use asm";
function g() {}
return g;
}
funcBody1 = funcBody.replace('function f1', 'function ');
assertEq(f1.toString(), funcBody1);
assertEq(f1.toSource(), '(' + funcBody1 + ')');
var g = function g0(glob) {
"use asm";
function g() {}
return g;
}
funcBody2 = funcBody.replace('function f1', 'function g0');
assertEq(g.toString(), funcBody2);
assertEq(g.toSource(), '(' + funcBody2 + ')');
f1 = new Function('glob', bodyOnly);
assertEq(f1.toString(), "function anonymous(glob) {\n" + bodyOnly + "\n}");
assertEq(f1.toSource(), "(function anonymous(glob) {\n" + bodyOnly + "\n})");
@ -98,6 +142,28 @@ var funcBody = 'function f2(glob, ffi) {\n\
assertEq(f2.toString(), funcBody);
assertEq(f2.toSource(), funcBody);
f2 = function (glob, ffi) {
"use asm";
function g() {}
return g;
}
funcBody1 = funcBody.replace('function f2', 'function ');
assertEq(f2.toString(), funcBody1);
assertEq(f2.toSource(), '(' + funcBody1 + ')');
var g = function g0(glob, ffi) {
"use asm";
function g() {}
return g;
}
var funcBody2 = funcBody.replace('function f2', 'function g0');
assertEq(g.toString(), funcBody2);
assertEq(g.toSource(), '(' + funcBody2 + ')');
f2 = new Function('glob', 'ffi', bodyOnly);
assertEq(f2.toString(), "function anonymous(glob, ffi) {\n" + bodyOnly + "\n}");
assertEq(f2.toSource(), "(function anonymous(glob, ffi) {\n" + bodyOnly + "\n})");
@ -136,6 +202,28 @@ var funcBody = 'function f3(glob, ffi, heap) {\n\
assertEq(f3.toString(), funcBody);
assertEq(f3.toSource(), funcBody);
f3 = function (glob, ffi, heap) {
"use asm";
function g() {}
return g;
}
funcBody1 = funcBody.replace('function f3', 'function ');
assertEq(f3.toString(), funcBody1);
assertEq(f3.toSource(), '(' + funcBody1 + ')');
var g = function g0(glob, ffi, heap) {
"use asm";
function g() {}
return g;
}
funcBody2 = funcBody.replace('function f3', 'function g0');
assertEq(g.toString(), funcBody2);
assertEq(g.toSource(), '(' + funcBody2 + ')');
f3 = new Function('glob', 'ffi', 'heap', bodyOnly);
assertEq(f3.toString(), "function anonymous(glob, ffi, heap) {\n" + bodyOnly + "\n}");
assertEq(f3.toSource(), "(function anonymous(glob, ffi, heap) {\n" + bodyOnly + "\n})");

View File

@ -680,8 +680,11 @@ JSFunction *
js::NewAsmJSModuleFunction(ExclusiveContext *cx, JSFunction *origFun, HandleObject moduleObj)
{
RootedPropertyName name(cx, origFun->name());
JSFunction::Flags flags = origFun->isLambda() ? JSFunction::NATIVE_LAMBDA_FUN
: JSFunction::NATIVE_FUN;
JSFunction *moduleFun = NewFunction(cx, NullPtr(), LinkAsmJS, origFun->nargs(),
JSFunction::NATIVE_FUN, NullPtr(), name,
flags, NullPtr(), name,
JSFunction::ExtendedFinalizeKind, TenuredObject);
if (!moduleFun)
return nullptr;
@ -743,7 +746,7 @@ js::AsmJSModuleToString(JSContext *cx, HandleFunction fun, bool addParenToLambda
// Whether the function has been created with a Function ctor
bool funCtor = begin == 0 && end == source->length() && source->argumentsNotIncluded();
if (funCtor && addParenToLambda && !out.append("("))
if (addParenToLambda && fun->isLambda() && !out.append("("))
return nullptr;
if (!out.append("function "))
@ -784,7 +787,7 @@ js::AsmJSModuleToString(JSContext *cx, HandleFunction fun, bool addParenToLambda
if (funCtor && !out.append("\n}"))
return nullptr;
if (funCtor && addParenToLambda && !out.append(")"))
if (addParenToLambda && fun->isLambda() && !out.append(")"))
return nullptr;
return out.finishString();

View File

@ -52,6 +52,7 @@ class JSFunction : public JSObject
/* Derived Flags values for convenience: */
NATIVE_FUN = 0,
NATIVE_LAMBDA_FUN = NATIVE_FUN | LAMBDA,
INTERPRETED_LAMBDA = INTERPRETED | LAMBDA,
INTERPRETED_LAMBDA_ARROW = INTERPRETED | LAMBDA | ARROW
};