Bug 1276029 - Baldr: change wasm::Compile to take ShareableBytes (r=bbouvier)
authorLuke Wagner <luke@mozilla.com>
Fri, 15 Jul 2016 12:26:40 -0500
changeset 330221 c40efa3603e05cb5a781c4ef74726363e5c0c1e2
parent 330220 7792f6453b843b82835a104cc58674f2de01e947
child 330222 606e6f6aebe26585dd35c400ce72c3032b5e3378
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbbouvier
bugs1276029
milestone50.0a1
Bug 1276029 - Baldr: change wasm::Compile to take ShareableBytes (r=bbouvier) MozReview-Commit-ID: FvYx5JTfCXw
js/src/asmjs/WasmCode.h
js/src/asmjs/WasmCompile.cpp
js/src/asmjs/WasmCompile.h
js/src/asmjs/WasmJS.cpp
--- a/js/src/asmjs/WasmCode.h
+++ b/js/src/asmjs/WasmCode.h
@@ -98,19 +98,16 @@ class CodeSegment
     }
 };
 
 // ShareableBytes is a ref-counted vector of bytes which are incrementally built
 // during compilation and then immutably shared.
 
 struct ShareableBytes : ShareableBase<ShareableBytes>
 {
-    ShareableBytes() = default;
-    explicit ShareableBytes(Bytes&& bytes) : bytes(Move(bytes)) {}
-
     // Vector is 'final', so instead make Vector a member and add boilerplate.
     Bytes bytes;
     size_t sizeOfExcludingThis(MallocSizeOf m) const { return bytes.sizeOfExcludingThis(m); }
     const uint8_t* begin() const { return bytes.begin(); }
     const uint8_t* end() const { return bytes.end(); }
     bool append(const uint8_t *p, uint32_t ct) { return bytes.append(p, ct); }
 };
 
--- a/js/src/asmjs/WasmCompile.cpp
+++ b/js/src/asmjs/WasmCompile.cpp
@@ -1393,17 +1393,17 @@ CompileArgs::init(ExclusiveContext* cx)
         ReportOutOfMemory(cx);
         return false;
     }
 
     return true;
 }
 
 SharedModule
-wasm::Compile(Bytes&& bytecode, CompileArgs&& args, UniqueChars* error)
+wasm::Compile(const ShareableBytes& bytecode, CompileArgs&& args, UniqueChars* error)
 {
     bool newFormat = args.assumptions.newFormat;
 
     auto init = js::MakeUnique<ModuleGeneratorData>(args.assumptions.usesSignal);
     if (!init)
         return nullptr;
 
     Decoder d(bytecode.begin(), bytecode.end(), error);
@@ -1451,14 +1451,10 @@ wasm::Compile(Bytes&& bytecode, CompileA
     if (!DecodeNameSection(d, mg))
         return nullptr;
 
     if (!DecodeUnknownSections(d))
         return nullptr;
 
     MOZ_ASSERT(!*error, "unreported error in decoding");
 
-    SharedBytes sharedBytes = js_new<ShareableBytes>(Move(bytecode));
-    if (!sharedBytes)
-        return nullptr;
-
-    return mg.finish(Move(imports), *sharedBytes);
+    return mg.finish(Move(imports), bytecode);
 }
--- a/js/src/asmjs/WasmCompile.h
+++ b/js/src/asmjs/WasmCompile.h
@@ -34,14 +34,14 @@ struct CompileArgs
     UniqueChars filename;
     bool alwaysBaseline;
 
     CompileArgs() : alwaysBaseline(false) {}
     bool init(ExclusiveContext* cx);
 };
 
 SharedModule
-Compile(Bytes&& code, CompileArgs&& args, UniqueChars* error);
+Compile(const ShareableBytes& bytecode, CompileArgs&& args, UniqueChars* error);
 
 }  // namespace wasm
 }  // namespace js
 
 #endif // namespace wasm_compile_h
--- a/js/src/asmjs/WasmJS.cpp
+++ b/js/src/asmjs/WasmJS.cpp
@@ -127,35 +127,38 @@ GetImports(JSContext* cx, HandleObject i
 
 bool
 wasm::Eval(JSContext* cx, Handle<TypedArrayObject*> code, HandleObject importObj,
            MutableHandleWasmInstanceObject instanceObj)
 {
     if (!CheckCompilerSupport(cx))
         return false;
 
-    Bytes bytecode;
-    if (!bytecode.append((uint8_t*)code->viewDataEither().unwrap(), code->byteLength())) {
+    MutableBytes bytecode = cx->new_<ShareableBytes>();
+    if (!bytecode)
+        return false;
+
+    if (!bytecode->append((uint8_t*)code->viewDataEither().unwrap(), code->byteLength())) {
         ReportOutOfMemory(cx);
         return false;
     }
 
     CompileArgs compileArgs;
     if (!compileArgs.init(cx))
         return false;
 
     JS::AutoFilename af;
     if (DescribeScriptedCaller(cx, &af)) {
         compileArgs.filename = DuplicateString(cx, af.get());
         if (!compileArgs.filename)
             return false;
     }
 
     UniqueChars error;
-    SharedModule module = Compile(Move(bytecode), Move(compileArgs), &error);
+    SharedModule module = Compile(*bytecode, Move(compileArgs), &error);
     if (!module) {
         if (error)
             JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_WASM_FAIL, error.get());
         else
             ReportOutOfMemory(cx);
         return false;
     }
 
@@ -306,24 +309,27 @@ WasmModuleObject::construct(JSContext* c
     if (!callArgs.requireAtLeast(cx, "WebAssembly.Module", 1))
         return false;
 
     if (!callArgs.get(0).isObject()) {
         JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_WASM_BAD_BUF_ARG);
         return false;
     }
 
-    Bytes bytecode;
+    MutableBytes bytecode = cx->new_<ShareableBytes>();
+    if (!bytecode)
+        return false;
+
     if (callArgs[0].toObject().is<TypedArrayObject>()) {
         TypedArrayObject& view = callArgs[0].toObject().as<TypedArrayObject>();
-        if (!bytecode.append((uint8_t*)view.viewDataEither().unwrap(), view.byteLength()))
+        if (!bytecode->append((uint8_t*)view.viewDataEither().unwrap(), view.byteLength()))
             return false;
     } else if (callArgs[0].toObject().is<ArrayBufferObject>()) {
         ArrayBufferObject& buffer = callArgs[0].toObject().as<ArrayBufferObject>();
-        if (!bytecode.append(buffer.dataPointer(), buffer.byteLength()))
+        if (!bytecode->append(buffer.dataPointer(), buffer.byteLength()))
             return false;
     } else {
         JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_WASM_BAD_BUF_ARG);
         return false;
     }
 
     CompileArgs compileArgs;
     if (!compileArgs.init(cx))
@@ -337,17 +343,17 @@ WasmModuleObject::construct(JSContext* c
         if (!compileArgs.filename)
             return false;
     }
 
     if (!CheckCompilerSupport(cx))
         return false;
 
     UniqueChars error;
-    SharedModule module = Compile(Move(bytecode), Move(compileArgs), &error);
+    SharedModule module = Compile(*bytecode, Move(compileArgs), &error);
     if (!module) {
         if (error)
             JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_WASM_FAIL, error.get());
         else
             ReportOutOfMemory(cx);
         return false;
     }