Skip to content

Commit

Permalink
[wasm] allow WebAssembly memory to instantiated as a SharedArrayBuffer
Browse files Browse the repository at this point in the history
By recognizing a new boolean field in the options object passed to
WebAssembly.Memory: isShared.
  • Loading branch information
bpowers committed Jan 13, 2017
1 parent ad889a6 commit bda41b7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/wasm/wasm-js.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
return_value.Set(resolver->GetPromise());
}

static
bool GetIntegerProperty(v8::Isolate* isolate, ErrorThrower* thrower,
Local<Context> context, Local<v8::Object> object,
Local<String> property, int* result, int lower_bound,
Expand Down Expand Up @@ -386,6 +387,22 @@ bool GetIntegerProperty(v8::Isolate* isolate, ErrorThrower* thrower,
return false;
}

static
bool GetBooleanProperty(v8::Isolate* isolate, ErrorThrower* thrower,
Local<Context> context, Local<v8::Object> object,
Local<String> property, bool* result, bool default_=false) {
v8::MaybeLocal<v8::Value> maybe = object->Get(context, property);
v8::Local<v8::Value> value;
if (maybe.ToLocal(&value)) {
bool boolean;
if (!value->BooleanValue(context).To(&boolean)) return false;
*result = boolean;
return true;
}
*result = default_;
return true;
}

const int max_table_size = 1 << 26;

void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) {
Expand Down Expand Up @@ -479,11 +496,17 @@ void WebAssemblyMemory(const v8::FunctionCallbackInfo<v8::Value>& args) {
return;
}
}
bool is_shared = false;
if (!GetBooleanProperty(isolate, &thrower, context, descriptor,
v8_str(isolate, "isShared"), &is_shared)) {
return;
}
auto shared = is_shared ? i::SharedFlag::kShared : i::SharedFlag::kNotShared;
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
size_t size = static_cast<size_t>(i::wasm::WasmModule::kPageSize) *
static_cast<size_t>(initial);
i::Handle<i::JSArrayBuffer> buffer =
i::wasm::NewArrayBuffer(i_isolate, size, i::FLAG_wasm_guard_pages);
i::wasm::NewArrayBuffer(i_isolate, size, i::FLAG_wasm_guard_pages, shared);
if (buffer.is_null()) {
thrower.RangeError("could not allocate memory");
return;
Expand Down
5 changes: 3 additions & 2 deletions src/wasm/wasm-module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,8 @@ Handle<Script> CreateWasmScript(Isolate* isolate,
} // namespace

Handle<JSArrayBuffer> wasm::NewArrayBuffer(Isolate* isolate, size_t size,
bool enable_guard_regions) {
bool enable_guard_regions,
SharedFlag shared) {
if (size > (kV8MaxWasmMemoryPages * WasmModule::kPageSize)) {
// TODO(titzer): lift restriction on maximum memory allocated here.
return Handle<JSArrayBuffer>::null();
Expand All @@ -810,7 +811,7 @@ Handle<JSArrayBuffer> wasm::NewArrayBuffer(Isolate* isolate, size_t size,

Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
JSArrayBuffer::Setup(buffer, isolate, is_external, memory,
static_cast<int>(size));
static_cast<int>(size), shared);
buffer->set_is_neuterable(false);
buffer->set_has_guard_region(enable_guard_regions);

Expand Down
3 changes: 2 additions & 1 deletion src/wasm/wasm-module.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ int32_t GrowInstanceMemory(Isolate* isolate,
Handle<WasmInstanceObject> instance, uint32_t pages);

Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size,
bool enable_guard_regions);
bool enable_guard_regions,
SharedFlag shared = SharedFlag::kNotShared);

int32_t GrowWebAssemblyMemory(Isolate* isolate, Handle<Object> receiver,
uint32_t pages);
Expand Down

0 comments on commit bda41b7

Please sign in to comment.