diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc index 80a94f7ac2..d286a54ef6 100644 --- a/src/wasm/wasm-js.cc +++ b/src/wasm/wasm-js.cc @@ -1731,33 +1731,18 @@ void WebAssemblyMemoryType(const v8::FunctionCallbackInfo& args) { auto maybe_memory = GetFirstArgumentAsMemory(args, &thrower); if (thrower.error()) return; i::Handle memory = maybe_memory.ToHandleChecked(); - v8::Local ret = v8::Object::New(isolate); i::Handle buffer(memory->array_buffer(), i_isolate); - size_t curr_size = buffer->byte_length() / i::wasm::kWasmPageSize; DCHECK_LE(curr_size, std::numeric_limits::max()); - if (!ret->CreateDataProperty(isolate->GetCurrentContext(), - v8_str(isolate, "minimum"), - v8::Integer::NewFromUnsigned( - isolate, static_cast(curr_size))) - .IsJust()) { - return; - } - + uint32_t min_size = static_cast(curr_size); + base::Optional max_size; if (memory->has_maximum_pages()) { - uint64_t max_size = memory->maximum_pages(); - DCHECK_LE(max_size, std::numeric_limits::max()); - if (!ret->CreateDataProperty(isolate->GetCurrentContext(), - v8_str(isolate, "maximum"), - v8::Integer::NewFromUnsigned( - isolate, static_cast(max_size))) - .IsJust()) { - return; - } + uint64_t max_size64 = memory->maximum_pages(); + DCHECK_LE(max_size64, std::numeric_limits::max()); + max_size.emplace(static_cast(max_size64)); } - - v8::ReturnValue return_value = args.GetReturnValue(); - return_value.Set(ret); + auto type = i::wasm::GetTypeForMemory(i_isolate, min_size, max_size); + args.GetReturnValue().Set(Utils::ToLocal(type)); } void WebAssemblyGlobalGetValueCommon( diff --git a/src/wasm/wasm-module.cc b/src/wasm/wasm-module.cc index 0d7f3a68eb..3bf2198e9b 100644 --- a/src/wasm/wasm-module.cc +++ b/src/wasm/wasm-module.cc @@ -198,6 +198,24 @@ Handle GetTypeForGlobal(Isolate* isolate, bool is_mutable, return object; } +Handle GetTypeForMemory(Isolate* isolate, uint32_t min_size, + base::Optional max_size) { + Factory* factory = isolate->factory(); + + Handle object_function = isolate->object_function(); + Handle object = factory->NewJSObject(object_function); + Handle minimum_string = factory->InternalizeUtf8String("minimum"); + Handle maximum_string = factory->InternalizeUtf8String("maximum"); + JSObject::AddProperty(isolate, object, minimum_string, + factory->NewNumberFromUint(min_size), NONE); + if (max_size.has_value()) { + JSObject::AddProperty(isolate, object, maximum_string, + factory->NewNumberFromUint(max_size.value()), NONE); + } + + return object; +} + Handle GetTypeForTable(Isolate* isolate, ValueType type, uint32_t min_size, base::Optional max_size) { @@ -283,6 +301,15 @@ Handle GetImports(Isolate* isolate, import_kind = table_string; break; case kExternalMemory: + if (enabled_features.type_reflection) { + DCHECK_EQ(0, import.index); // Only one memory supported. + base::Optional maximum_size; + if (module->has_maximum_pages) { + maximum_size.emplace(module->maximum_pages); + } + type_value = + GetTypeForMemory(isolate, module->initial_pages, maximum_size); + } import_kind = memory_string; break; case kExternalGlobal: @@ -374,6 +401,15 @@ Handle GetExports(Isolate* isolate, export_kind = table_string; break; case kExternalMemory: + if (enabled_features.type_reflection) { + DCHECK_EQ(0, exp.index); // Only one memory supported. + base::Optional maximum_size; + if (module->has_maximum_pages) { + maximum_size.emplace(module->maximum_pages); + } + type_value = + GetTypeForMemory(isolate, module->initial_pages, maximum_size); + } export_kind = memory_string; break; case kExternalGlobal: diff --git a/src/wasm/wasm-module.h b/src/wasm/wasm-module.h index 264540e7d1..69c57725de 100644 --- a/src/wasm/wasm-module.h +++ b/src/wasm/wasm-module.h @@ -305,6 +305,8 @@ V8_EXPORT_PRIVATE bool IsWasmCodegenAllowed(Isolate* isolate, Handle GetTypeForFunction(Isolate* isolate, FunctionSig* sig); Handle GetTypeForGlobal(Isolate* isolate, bool is_mutable, ValueType type); +Handle GetTypeForMemory(Isolate* isolate, uint32_t min_size, + base::Optional max_size); Handle GetTypeForTable(Isolate* isolate, ValueType type, uint32_t min_size, base::Optional max_size); diff --git a/test/mjsunit/wasm/type-reflection.js b/test/mjsunit/wasm/type-reflection.js index 310f499264..a51f114b25 100644 --- a/test/mjsunit/wasm/type-reflection.js +++ b/test/mjsunit/wasm/type-reflection.js @@ -57,6 +57,52 @@ load('test/mjsunit/wasm/wasm-module-builder.js'); assertEquals(2, Object.getOwnPropertyNames(type).length); })(); +(function TestMemoryExports() { + let builder = new WasmModuleBuilder(); + builder.addMemory(1).exportMemoryAs("a") + let module = new WebAssembly.Module(builder.toBuffer()); + let exports = WebAssembly.Module.exports(module); + + assertEquals("a", exports[0].name); + assertTrue("type" in exports[0]); + assertEquals(1, exports[0].type.minimum); + assertFalse("maximum" in exports[0].type); + + builder = new WasmModuleBuilder(); + builder.addMemory(2, 16).exportMemoryAs("b") + module = new WebAssembly.Module(builder.toBuffer()); + exports = WebAssembly.Module.exports(module); + + assertEquals("b", exports[0].name); + assertTrue("type" in exports[0]); + assertEquals(2, exports[0].type.minimum); + assertEquals(16, exports[0].type.maximum); +})(); + +(function TestMemoryImports() { + let builder = new WasmModuleBuilder(); + builder.addImportedMemory("m", "a", 1); + let module = new WebAssembly.Module(builder.toBuffer()); + let imports = WebAssembly.Module.imports(module); + + assertEquals("a", imports[0].name); + assertEquals("m", imports[0].module); + assertTrue("type" in imports[0]); + assertEquals(1, imports[0].type.minimum); + assertFalse("maximum" in imports[0].type); + + builder = new WasmModuleBuilder(); + builder.addImportedMemory("m", "b", 2, 16); + module = new WebAssembly.Module(builder.toBuffer()); + imports = WebAssembly.Module.imports(module); + + assertEquals("b", imports[0].name); + assertEquals("m", imports[0].module); + assertTrue("type" in imports[0]); + assertEquals(2, imports[0].type.minimum); + assertEquals(16, imports[0].type.maximum); +})(); + (function TestTableType() { let table = new WebAssembly.Table({initial: 1, element: "anyfunc"}); let type = WebAssembly.Table.type(table);