Index: src/wasm/wasm-js.cc |
diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc |
index 76abca0d44a748b52c0cdbfc105ea1b9ae096543..63a25043b46b78334320c0101d486061268b8975 100644 |
--- a/src/wasm/wasm-js.cc |
+++ b/src/wasm/wasm-js.cc |
@@ -236,16 +236,14 @@ MaybeLocal<Value> InstantiateModuleImpl( |
return Utils::ToLocal(instance.ToHandleChecked()); |
} |
-void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
- HandleScope scope(args.GetIsolate()); |
+namespace { |
+i::MaybeHandle<i::WasmModuleObject> GetFirstArgumentAsModule( |
+ const v8::FunctionCallbackInfo<v8::Value>& args, ErrorThrower& thrower) { |
v8::Isolate* isolate = args.GetIsolate(); |
- i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
- |
- ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); |
- |
+ i::MaybeHandle<i::WasmModuleObject> nothing; |
if (args.Length() < 1) { |
thrower.TypeError("Argument 0 must be a WebAssembly.Module"); |
- return; |
+ return nothing; |
} |
Local<Context> context = isolate->GetCurrentContext(); |
@@ -253,17 +251,28 @@ void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), |
i::Handle<i::Symbol>(i_context->wasm_module_sym()), |
"Argument 0 must be a WebAssembly.Module")) { |
- return; |
+ return nothing; |
} |
Local<Object> module_obj = Local<Object>::Cast(args[0]); |
- i::Handle<i::WasmModuleObject> i_module_obj = |
- i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); |
+ return i::Handle<i::WasmModuleObject>::cast( |
+ v8::Utils::OpenHandle(*module_obj)); |
+} |
+} // namespace |
+ |
+void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
+ HandleScope scope(args.GetIsolate()); |
+ v8::Isolate* isolate = args.GetIsolate(); |
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
+ ErrorThrower thrower(i_isolate, "WebAssembly.Module.imports()"); |
- i::Handle<i::JSArray> imports = i::wasm::GetImports(i_isolate, i_module_obj); |
+ auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
- v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
- return_value.Set(Utils::ToLocal(imports)); |
+ if (!maybe_module.is_null()) { |
+ auto imports = |
+ i::wasm::GetImports(i_isolate, maybe_module.ToHandleChecked()); |
+ args.GetReturnValue().Set(Utils::ToLocal(imports)); |
+ } |
} |
void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
@@ -273,27 +282,30 @@ void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()"); |
- if (args.Length() < 1) { |
- thrower.TypeError("Argument 0 must be a WebAssembly.Module"); |
- return; |
- } |
+ auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
- Local<Context> context = isolate->GetCurrentContext(); |
- i::Handle<i::Context> i_context = Utils::OpenHandle(*context); |
- if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), |
- i::Handle<i::Symbol>(i_context->wasm_module_sym()), |
- "Argument 0 must be a WebAssembly.Module")) { |
- return; |
+ if (!maybe_module.is_null()) { |
+ auto exports = |
+ i::wasm::GetExports(i_isolate, maybe_module.ToHandleChecked()); |
+ args.GetReturnValue().Set(Utils::ToLocal(exports)); |
} |
+} |
- Local<Object> module_obj = Local<Object>::Cast(args[0]); |
- i::Handle<i::WasmModuleObject> i_module_obj = |
- i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); |
+void WebAssemblyModuleCustomSections( |
+ const v8::FunctionCallbackInfo<v8::Value>& args) { |
+ HandleScope scope(args.GetIsolate()); |
+ v8::Isolate* isolate = args.GetIsolate(); |
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
- i::Handle<i::JSArray> exports = i::wasm::GetExports(i_isolate, i_module_obj); |
+ ErrorThrower thrower(i_isolate, "WebAssembly.Module.customSections()"); |
- v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
- return_value.Set(Utils::ToLocal(exports)); |
+ auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
+ |
+ if (!maybe_module.is_null()) { |
+ auto custom_sections = |
+ i::wasm::GetCustomSections(i_isolate, maybe_module.ToHandleChecked()); |
+ args.GetReturnValue().Set(Utils::ToLocal(custom_sections)); |
+ } |
} |
void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { |
@@ -303,29 +315,15 @@ void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { |
ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); |
- if (args.Length() < 1) { |
- thrower.TypeError("Argument 0 must be a WebAssembly.Module"); |
- return; |
- } |
- |
- Local<Context> context = isolate->GetCurrentContext(); |
- i::Handle<i::Context> i_context = Utils::OpenHandle(*context); |
- if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), |
- i::Handle<i::Symbol>(i_context->wasm_module_sym()), |
- "Argument 0 must be a WebAssembly.Module")) { |
- return; |
- } |
- |
- Local<Object> module_obj = Local<Object>::Cast(args[0]); |
- i::Handle<i::JSObject> i_module_obj = |
- i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*module_obj)); |
+ auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
- MaybeLocal<Value> instance = |
- InstantiateModuleImpl(i_isolate, i_module_obj, args, &thrower); |
- if (instance.IsEmpty()) return; |
+ if (!maybe_module.is_null()) { |
+ MaybeLocal<Value> instance = InstantiateModuleImpl( |
+ i_isolate, maybe_module.ToHandleChecked(), args, &thrower); |
+ if (instance.IsEmpty()) return; |
- v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
- return_value.Set(instance.ToLocalChecked()); |
+ args.GetReturnValue().Set(instance.ToLocalChecked()); |
+ } |
} |
void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
@@ -810,6 +808,8 @@ void WasmJs::Install(Isolate* isolate) { |
1); |
InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, |
1); |
+ InstallFunc(isolate, module_constructor, "customSections", |
+ WebAssemblyModuleCustomSections, 1); |
// Setup Instance |
Handle<JSFunction> instance_constructor = |