Chromium Code Reviews| Index: src/wasm/wasm-js.cc |
| diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc |
| index ae4f972a984e75c3f76324ecc6664491295f410a..466d6e93e9798d79e36ea99c48dd564547ab2cde 100644 |
| --- a/src/wasm/wasm-js.cc |
| +++ b/src/wasm/wasm-js.cc |
| @@ -27,11 +27,15 @@ using v8::internal::wasm::ErrorThrower; |
| namespace v8 { |
| -static const int kWasmMemoryBufferFieldIndex = 0; |
| -static const int kWasmMemoryMaximumFieldIndex = 1; |
| static const int kWasmTableArrayFieldIndex = 0; |
| static const int kWasmTableMaximumFieldIndex = 1; |
| +enum WasmMemoryObjectData { |
| + kWasmMemoryBuffer, |
| + kWasmMemoryMaximum, |
| + kWasmMemoryInstanceObject |
| +}; |
| + |
| namespace { |
| i::Handle<i::String> v8_str(i::Isolate* isolate, const char* str) { |
| return isolate->factory()->NewStringFromAsciiChecked(str); |
| @@ -575,11 +579,42 @@ void WebAssemblyMemoryGrow(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| "Receiver is not a WebAssembly.Memory")) { |
| return; |
| } |
| + if (args.Length() < 1 || !args[0]->IsUint32()) { |
| + v8::Local<v8::Value> e = v8::Exception::TypeError( |
| + v8_str(isolate, "Argument 0 must be numeric value of pages")); |
| + isolate->ThrowException(e); |
| + return; |
| + } |
| - // TODO(rossberg): grow memory. |
| - v8::Local<v8::Value> e = |
| - v8::Exception::TypeError(v8_str(isolate, "Memory#grow unimplemented")); |
| - isolate->ThrowException(e); |
| + uint32_t delta = args[0]->Uint32Value(context).FromJust(); |
|
bradnelson
2016/10/17 22:23:26
Still not groking from before.
If you use ToUint32
gdeepti
2016/10/18 02:34:17
ToUint32(&delta) method can be used only on intern
bradnelson
2016/10/18 04:29:38
Ah ok.
|
| + i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| + i::Handle<i::JSObject> receiver = |
| + i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This())); |
| + i::Handle<i::Object> instance_object(receiver->GetInternalField(2), |
|
bradnelson
2016/10/17 22:13:24
where's this 2 come from?
gdeepti
2016/10/18 02:34:17
From the internal field of the memory object, repl
|
| + i_isolate); |
| + i::Handle<i::JSObject> instance( |
| + i::Handle<i::JSObject>::cast(instance_object)); |
| + |
| + // TODO(gdeepti) Implement growing memory when shared by different |
| + // instances. |
| + uint32_t ret = internal::wasm::GrowInstanceMemory(i_isolate, instance, delta); |
| + if (ret == -1) { |
|
bradnelson
2016/10/17 22:13:25
unsigned, compared to -1 ?
gdeepti
2016/10/18 02:34:17
Oops, fixed. For the tests I currently have, the c
|
| + v8::Local<v8::Value> e = v8::Exception::Error( |
| + v8_str(isolate, "Unable to grow instance memory.")); |
| + isolate->ThrowException(e); |
| + return; |
| + } |
| + i::MaybeHandle<i::JSArrayBuffer> buffer = |
| + internal::wasm::GetInstanceMemory(i_isolate, instance); |
| + if (buffer.is_null()) { |
| + v8::Local<v8::Value> e = v8::Exception::Error( |
| + v8_str(isolate, "WebAssembly.Memory buffer object not set.")); |
| + isolate->ThrowException(e); |
| + return; |
| + } |
| + receiver->SetInternalField(kWasmMemoryBuffer, *buffer.ToHandleChecked()); |
| + v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
| + return_value.Set(ret); |
| } |
| void WebAssemblyMemoryGetBuffer( |
| @@ -595,8 +630,8 @@ void WebAssemblyMemoryGetBuffer( |
| i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| i::Handle<i::JSObject> receiver = |
| i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This())); |
| - i::Handle<i::Object> buffer( |
| - receiver->GetInternalField(kWasmMemoryBufferFieldIndex), i_isolate); |
| + i::Handle<i::Object> buffer(receiver->GetInternalField(kWasmMemoryBuffer), |
| + i_isolate); |
| DCHECK(buffer->IsJSArrayBuffer()); |
| v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
| return_value.Set(Utils::ToLocal(buffer)); |
| @@ -610,9 +645,9 @@ i::Handle<i::JSObject> i::WasmJs::CreateWasmMemoryObject( |
| i_isolate->native_context()->wasm_memory_constructor()); |
| i::Handle<i::JSObject> memory_obj = |
| i_isolate->factory()->NewJSObject(memory_ctor); |
| - memory_obj->SetInternalField(kWasmMemoryBufferFieldIndex, *buffer); |
| + memory_obj->SetInternalField(kWasmMemoryBuffer, *buffer); |
| memory_obj->SetInternalField( |
| - kWasmMemoryMaximumFieldIndex, |
| + kWasmMemoryMaximum, |
| has_maximum |
| ? static_cast<i::Object*>(i::Smi::FromInt(maximum)) |
| : static_cast<i::Object*>(i_isolate->heap()->undefined_value())); |
| @@ -743,7 +778,7 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate, |
| Handle<JSObject> memory_proto = |
| factory->NewJSObject(memory_constructor, TENURED); |
| map = isolate->factory()->NewMap( |
| - i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + 2 * i::kPointerSize); |
| + i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + 3 * i::kPointerSize); |
|
bradnelson
2016/10/17 22:13:24
Add a size enum item above so this can have a name
gdeepti
2016/10/18 02:34:17
Done.
|
| JSFunction::SetInitialMap(memory_constructor, map, memory_proto); |
| JSObject::AddProperty(memory_proto, isolate->factory()->constructor_string(), |
| memory_constructor, DONT_ENUM); |
| @@ -851,8 +886,7 @@ Handle<JSArrayBuffer> WasmJs::GetWasmMemoryArrayBuffer(Isolate* isolate, |
| Handle<Object> value) { |
| DCHECK(IsWasmMemoryObject(isolate, value)); |
| Handle<Object> buf( |
| - JSObject::cast(*value)->GetInternalField(kWasmMemoryBufferFieldIndex), |
| - isolate); |
| + JSObject::cast(*value)->GetInternalField(kWasmMemoryBuffer), isolate); |
| return Handle<JSArrayBuffer>::cast(buf); |
| } |
| } // namespace internal |