Bug 1247247 - Factor essential wasmEval code into public helper function. r=luke
MozReview-Commit-ID: 9l3LEYFQZEy
--- a/js/src/asmjs/Wasm.cpp
+++ b/js/src/asmjs/Wasm.cpp
@@ -1165,55 +1165,33 @@ ImportFunctions(JSContext* cx, HandleObj
if (!imports.append(&v.toObject().as<JSFunction>()))
return false;
}
return true;
}
-static bool
-WasmEval(JSContext* cx, unsigned argc, Value* vp)
+bool
+wasm::Eval(JSContext* cx, Handle<ArrayBufferObject*> code,
+ HandleObject importObj, MutableHandleObject exportObj)
{
if (!CheckCompilerSupport(cx))
return false;
- CallArgs args = CallArgsFromVp(argc, vp);
- RootedObject callee(cx, &args.callee());
-
- if (args.length() < 1 || args.length() > 2) {
- ReportUsageError(cx, callee, "Wrong number of arguments");
- return false;
- }
-
- if (!args[0].isObject() || !args[0].toObject().is<ArrayBufferObject>()) {
- ReportUsageError(cx, callee, "First argument must be an ArrayBuffer");
- return false;
- }
-
- Rooted<ArrayBufferObject*> code(cx, &args[0].toObject().as<ArrayBufferObject>());
const uint8_t* bytes = code->dataPointer();
uint32_t length = code->byteLength();
Vector<uint8_t> copy(cx);
if (code->hasInlineData()) {
if (!copy.append(bytes, length))
return false;
bytes = copy.begin();
}
- RootedObject importObj(cx);
- if (!args.get(1).isUndefined()) {
- if (!args.get(1).isObject()) {
- ReportUsageError(cx, callee, "Second argument, if present, must be an Object");
- return false;
- }
- importObj = &args[1].toObject();
- }
-
UniqueChars file;
if (!DescribeScriptedCaller(cx, &file))
return false;
ImportNameVector importNames;
UniqueExportMap exportMap;
Rooted<ArrayBufferObject*> heap(cx);
Rooted<WasmModuleObject*> moduleObj(cx);
@@ -1222,18 +1200,52 @@ WasmEval(JSContext* cx, unsigned argc, V
ReportOutOfMemory(cx);
return false;
}
Rooted<FunctionVector> imports(cx, FunctionVector(cx));
if (!ImportFunctions(cx, importObj, importNames, &imports))
return false;
+ if (!moduleObj->module().dynamicallyLink(cx, moduleObj, heap, imports, *exportMap, exportObj))
+ return false;
+
+ return true;
+}
+
+
+static bool
+WasmEval(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ RootedObject callee(cx, &args.callee());
+
+ if (args.length() < 1 || args.length() > 2) {
+ ReportUsageError(cx, callee, "Wrong number of arguments");
+ return false;
+ }
+
+ if (!args[0].isObject() || !args[0].toObject().is<ArrayBufferObject>()) {
+ ReportUsageError(cx, callee, "First argument must be an ArrayBuffer");
+ return false;
+ }
+
+ RootedObject importObj(cx);
+ if (!args.get(1).isUndefined()) {
+ if (!args.get(1).isObject()) {
+ ReportUsageError(cx, callee, "Second argument, if present, must be an Object");
+ return false;
+ }
+ importObj = &args[1].toObject();
+ }
+
+ Rooted<ArrayBufferObject*> code(cx, &args[0].toObject().as<ArrayBufferObject>());
+
RootedObject exportObj(cx);
- if (!moduleObj->module().dynamicallyLink(cx, moduleObj, heap, imports, *exportMap, &exportObj))
+ if (!Eval(cx, code, importObj, &exportObj))
return false;
args.rval().setObject(*exportObj);
return true;
}
static bool
WasmTextToBinary(JSContext* cx, unsigned argc, Value* vp)
--- a/js/src/asmjs/Wasm.h
+++ b/js/src/asmjs/Wasm.h
@@ -17,16 +17,19 @@
*/
#ifndef wasm_h
#define wasm_h
#include "gc/Rooting.h"
namespace js {
+
+class ArrayBufferObject;
+
namespace wasm {
// Add wasm testing JS functions to the given JS global object.
bool
DefineTestingFunctions(JSContext* cx, JS::HandleObject globalObj);
// Return whether WebAssembly can be compiled on this platform.
bool
@@ -39,12 +42,18 @@ static const unsigned PageSize = 64 * 10
// When signal handling is used for bounds checking, MappedSize bytes are
// reserved and the subrange [0, memory_size) is given readwrite permission.
// See also static asserts in MIRGenerator::foldableOffsetRange.
#ifdef ASMJS_MAY_USE_SIGNAL_HANDLERS_FOR_OOB
static const uint64_t Uint32Range = uint64_t(UINT32_MAX) + 1;
static const uint64_t MappedSize = 2 * Uint32Range + PageSize;
#endif
+// Compiles the given binary wasm module given the ArrayBufferObject
+// and links the module's imports with the given import object.
+bool
+Eval(JSContext* cx, JS::Handle<ArrayBufferObject*> code,
+ JS::HandleObject importObj, JS::MutableHandleObject exportObj);
+
} // namespace wasm
} // namespace js
#endif // namespace wasm_h