Commit 56157fe8 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Allow Uint8Array in _WASMEXP_.instantiateModule()

R=dschuff@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1767203002

Cr-Commit-Position: refs/heads/master@{#34565}
parent 835c5e6b
......@@ -37,20 +37,43 @@ struct RawBuffer {
RawBuffer GetRawBufferArgument(
ErrorThrower& thrower, const v8::FunctionCallbackInfo<v8::Value>& args) {
// TODO(titzer): allow typed array views.
if (args.Length() < 1 || !args[0]->IsArrayBuffer()) {
if (args.Length() < 1) {
thrower.Error("Argument 0 must be an array buffer");
return {nullptr, nullptr};
}
Local<ArrayBuffer> buffer = Local<ArrayBuffer>::Cast(args[0]);
ArrayBuffer::Contents contents = buffer->GetContents();
const byte* start = reinterpret_cast<const byte*>(contents.Data());
const byte* end = start + contents.ByteLength();
const byte* start = nullptr;
const byte* end = nullptr;
if (start == nullptr) {
thrower.Error("ArrayBuffer argument is empty");
if (args[0]->IsArrayBuffer()) {
// A raw array buffer was passed.
Local<ArrayBuffer> buffer = Local<ArrayBuffer>::Cast(args[0]);
ArrayBuffer::Contents contents = buffer->GetContents();
start = reinterpret_cast<const byte*>(contents.Data());
end = start + contents.ByteLength();
if (start == nullptr || end == start) {
thrower.Error("ArrayBuffer argument is empty");
}
} else if (args[0]->IsTypedArray()) {
// A TypedArray was passed.
Local<TypedArray> array = Local<TypedArray>::Cast(args[0]);
Local<ArrayBuffer> buffer = array->Buffer();
ArrayBuffer::Contents contents = buffer->GetContents();
start =
reinterpret_cast<const byte*>(contents.Data()) + array->ByteOffset();
end = start + array->ByteLength();
if (start == nullptr || end == start) {
thrower.Error("ArrayBuffer argument is empty");
}
} else {
thrower.Error("Argument 0 must be an ArrayBuffer or Uint8Array");
}
return {start, end};
}
......
......@@ -111,3 +111,30 @@ var debug = false;
var instance = _WASMEXP_.instantiateModule(buffer);
assertEquals(151587081, instance.exports.load(0));
})();
(function BasicTestWithUint8Array() {
var module = new WasmModuleBuilder();
module.addMemory(1, 2, false);
module.addFunction("foo", [kAstI32])
.addBody([kExprI8Const, 17])
.exportAs("blarg");
var buffer = module.toBuffer(debug);
var array = new Uint8Array(buffer);
var instance = _WASMEXP_.instantiateModule(array);
assertEquals(17, instance.exports.blarg());
var kPad = 5;
var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad);
var whole = new Uint8Array(buffer2);
for (var i = 0; i < whole.byteLength; i++) {
whole[i] = 0xff;
}
var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength);
for (var i = 0; i < array2.byteLength; i++) {
array2[i] = array[i];
}
var instance = _WASMEXP_.instantiateModule(array2);
assertEquals(17, instance.exports.blarg());
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment