Bug 1321891 - Baldr: update .name of exported wasm functions (r=bbouvier)
authorLuke Wagner <luke@mozilla.com>
Fri, 23 Dec 2016 13:03:50 -0600
changeset 453578 d98d1548d5dd92e6cb5ad6e7d052e12526e59502
parent 453577 9b9dacbeb8bdf5962785f0ceecb67d5b46e0ac1d
child 453579 d01c0cd382843219b3ff12dc4a675d32b29809a7
push id39711
push userdmitchell@mozilla.com
push dateFri, 23 Dec 2016 21:59:47 +0000
reviewersbbouvier
bugs1321891
milestone53.0a1
Bug 1321891 - Baldr: update .name of exported wasm functions (r=bbouvier) MozReview-Commit-ID: 9y414nphkNK
js/src/jit-test/tests/wasm/basic.js
js/src/wasm/WasmJS.cpp
--- a/js/src/jit-test/tests/wasm/basic.js
+++ b/js/src/jit-test/tests/wasm/basic.js
@@ -10,17 +10,17 @@ var o = wasmEvalText('(module (func))').
 assertEq(Object.getOwnPropertyNames(o).length, 0);
 
 var o = wasmEvalText('(module (func) (export "a" 0))').exports;
 var names = Object.getOwnPropertyNames(o);
 assertEq(names.length, 1);
 assertEq(names[0], 'a');
 var desc = Object.getOwnPropertyDescriptor(o, 'a');
 assertEq(typeof desc.value, "function");
-assertEq(desc.value.name, "wasm-function[0]");
+assertEq(desc.value.name, "0");
 assertEq(desc.value.length, 0);
 assertEq(desc.value(), undefined);
 assertEq(desc.writable, false);
 assertEq(desc.enumerable, true);
 assertEq(desc.configurable, false);
 assertEq(desc.value(), undefined);
 
 wasmValidateText('(module (func) (func) (export "a" 0))');
@@ -28,24 +28,24 @@ wasmValidateText('(module (func) (func) 
 wasmValidateText('(module (func $a) (func $b) (export "a" $a) (export "b" $b))');
 wasmValidateText('(module (func $a) (func $b) (export "a" $a) (export "b" $b))');
 
 wasmFailValidateText('(module (func) (export "a" 1))', /exported function index out of bounds/);
 wasmFailValidateText('(module (func) (func) (export "a" 2))', /exported function index out of bounds/);
 
 var o = wasmEvalText('(module (func) (export "a" 0) (export "b" 0))').exports;
 assertEq(Object.getOwnPropertyNames(o).sort().toString(), "a,b");
-assertEq(o.a.name, "wasm-function[0]");
-assertEq(o.b.name, "wasm-function[0]");
+assertEq(o.a.name, "0");
+assertEq(o.b.name, "0");
 assertEq(o.a === o.b, true);
 
 var o = wasmEvalText('(module (func) (func) (export "a" 0) (export "b" 1))').exports;
 assertEq(Object.getOwnPropertyNames(o).sort().toString(), "a,b");
-assertEq(o.a.name, "wasm-function[0]");
-assertEq(o.b.name, "wasm-function[1]");
+assertEq(o.a.name, "0");
+assertEq(o.b.name, "1");
 assertEq(o.a === o.b, false);
 
 var o = wasmEvalText('(module (func (result i32) (i32.const 1)) (func (result i32) (i32.const 2)) (export "a" 0) (export "b" 1))').exports;
 assertEq(o.a(), 1);
 assertEq(o.b(), 2);
 var o = wasmEvalText('(module (func (result i32) (i32.const 1)) (func (result i32) (i32.const 2)) (export "a" 1) (export "b" 0))').exports;
 assertEq(o.a(), 2);
 assertEq(o.b(), 1);
--- a/js/src/wasm/WasmJS.cpp
+++ b/js/src/wasm/WasmJS.cpp
@@ -1032,32 +1032,38 @@ WasmInstanceObject::getExportedFunction(
                                         uint32_t funcIndex, MutableHandleFunction fun)
 {
     if (ExportMap::Ptr p = instanceObj->exports().lookup(funcIndex)) {
         fun.set(p->value());
         return true;
     }
 
     const Instance& instance = instanceObj->instance();
-    RootedAtom name(cx, instance.code().getFuncAtom(cx, funcIndex));
-    if (!name)
-        return false;
-
     unsigned numArgs = instance.metadata().lookupFuncExport(funcIndex).sig().args().length();
 
-    // asm.js needs to active like a normal JS function which are allowed to be
-    // used as constructors.
+    // asm.js needs to act like a normal JS function which means having the name
+    // from the original source and being callable as a constructor.
     if (instance.isAsmJS()) {
+        RootedAtom name(cx, instance.code().getFuncAtom(cx, funcIndex));
+        if (!name)
+            return false;
+
         fun.set(NewNativeConstructor(cx, WasmCall, numArgs, name, gc::AllocKind::FUNCTION_EXTENDED,
                                      SingletonObject, JSFunction::ASMJS_CTOR));
+        if (!fun)
+            return false;
     } else {
+        RootedAtom name(cx, NumberToAtom(cx, funcIndex));
+        if (!name)
+            return false;
+
         fun.set(NewNativeFunction(cx, WasmCall, numArgs, name, gc::AllocKind::FUNCTION_EXTENDED));
+        if (!fun)
+            return false;
     }
-    if (!fun)
-        return false;
 
     fun->setExtendedSlot(FunctionExtended::WASM_INSTANCE_SLOT, ObjectValue(*instanceObj));
     fun->setExtendedSlot(FunctionExtended::WASM_FUNC_INDEX_SLOT, Int32Value(funcIndex));
 
     if (!instanceObj->exports().putNew(funcIndex, fun)) {
         ReportOutOfMemory(cx);
         return false;
     }