Bug 1253137 - Baldr: put exports object onto .exports field of instance object (r=sunfish)
authorLuke Wagner <luke@mozilla.com>
Mon, 07 Mar 2016 16:03:04 -0600
changeset 287266 bf186476cb90f713243dd1d1d195d25de53914aa
parent 287265 b2ec1e1d2fe9b0a064fa5a65b7e11aaaa775a744
child 287267 0fc7d346c373a5c959c30b08c45383a8c3fe21b5
push id73136
push userlwagner@mozilla.com
push dateTue, 08 Mar 2016 19:48:22 +0000
treeherdermozilla-inbound@bf186476cb90 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssunfish
bugs1253137
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1253137 - Baldr: put exports object onto .exports field of instance object (r=sunfish) MozReview-Commit-ID: 1OgqSFrJsE4
js/src/asmjs/Wasm.cpp
js/src/jit-test/lib/wasm.js
js/src/jit-test/tests/wasm/binary.js
--- a/js/src/asmjs/Wasm.cpp
+++ b/js/src/asmjs/Wasm.cpp
@@ -1506,19 +1506,40 @@ ImportFunctions(JSContext* cx, HandleObj
 
         if (!imports.append(&v.toObject().as<JSFunction>()))
             return false;
     }
 
     return true;
 }
 
+static const char ExportField[] = "exports";
+
+static bool
+CreateInstance(JSContext* cx, HandleObject exportObj, MutableHandleObject instance)
+{
+    instance.set(JS_NewPlainObject(cx));
+    if (!instance)
+        return false;
+
+    JSAtom* atom = Atomize(cx, ExportField, strlen(ExportField));
+    if (!atom)
+        return false;
+
+    RootedId id(cx, AtomToId(atom));
+    RootedValue val(cx, ObjectValue(*exportObj));
+    if (!JS_DefinePropertyById(cx, instance, id, val, JSPROP_ENUMERATE))
+        return false;
+
+    return true;
+}
+
 bool
 wasm::Eval(JSContext* cx, Handle<TypedArrayObject*> code, HandleObject importObj,
-           MutableHandleObject exportObj)
+           MutableHandleObject instance)
 {
     MOZ_ASSERT(!code->isSharedMemory());
 
     if (!CheckCompilerSupport(cx))
         return false;
 
     if (!TypedArrayObject::ensureHasBuffer(cx, code))
         return false;
@@ -1548,17 +1569,21 @@ wasm::Eval(JSContext* cx, Handle<TypedAr
             ReportOutOfMemory(cx);
         return false;
     }
 
     Rooted<FunctionVector> imports(cx, FunctionVector(cx));
     if (!ImportFunctions(cx, importObj, importNames, &imports))
         return false;
 
-    return moduleObj->module().dynamicallyLink(cx, moduleObj, heap, imports, *exportMap, exportObj);
+    RootedObject exportObj(cx);
+    if (!moduleObj->module().dynamicallyLink(cx, moduleObj, heap, imports, *exportMap, &exportObj))
+        return false;
+
+    return CreateInstance(cx, exportObj, instance);
 }
 
 static bool
 InstantiateModule(JSContext* cx, unsigned argc, Value* vp)
 {
     MOZ_ASSERT(cx->runtime()->options().wasm());
     CallArgs args = CallArgsFromVp(argc, vp);
 
--- a/js/src/jit-test/lib/wasm.js
+++ b/js/src/jit-test/lib/wasm.js
@@ -1,13 +1,13 @@
 if (!wasmIsSupported())
     quit();
 
 load(libdir + "asserts.js");
 
 function wasmEvalText(str, imports) {
-    return Wasm.instantiateModule(wasmTextToBinary(str), imports);
+    return Wasm.instantiateModule(wasmTextToBinary(str), imports).exports;
 }
 
 function mismatchError(actual, expect) {
     var str = `type mismatch: expression has type ${actual} but expected ${expect}`;
     return RegExp(str);
 }
--- a/js/src/jit-test/tests/wasm/binary.js
+++ b/js/src/jit-test/tests/wasm/binary.js
@@ -41,17 +41,17 @@ function toU8(array) {
 function varU32(u32) {
     // TODO
     assertEq(u32 < 128, true);
     return [u32];
 }
 
 const U32MAX_LEB = [255, 255, 255, 255, 15];
 
-const wasmEval = Wasm.instantiateModule;
+const wasmEval = (code, imports) => Wasm.instantiateModule(code, imports).exports;
 
 assertErrorMessage(() => wasmEval(toU8([])), TypeError, magicError);
 assertErrorMessage(() => wasmEval(toU8([42])), TypeError, magicError);
 assertErrorMessage(() => wasmEval(toU8([magic0, magic1, magic2])), TypeError, magicError);
 assertErrorMessage(() => wasmEval(toU8([1,2,3,4])), TypeError, magicError);
 assertErrorMessage(() => wasmEval(toU8([magic0, magic1, magic2, magic3])), TypeError, versionError);
 assertErrorMessage(() => wasmEval(toU8([magic0, magic1, magic2, magic3, 1])), TypeError, versionError);
 assertErrorMessage(() => wasmEval(toU8([magic0, magic1, magic2, magic3, ver0])), TypeError, versionError);