OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/api-natives.h" | 5 #include "src/api-natives.h" |
6 #include "src/api.h" | 6 #include "src/api.h" |
7 #include "src/asmjs/asm-js.h" | 7 #include "src/asmjs/asm-js.h" |
8 #include "src/asmjs/asm-typer.h" | 8 #include "src/asmjs/asm-typer.h" |
9 #include "src/asmjs/asm-wasm-builder.h" | 9 #include "src/asmjs/asm-wasm-builder.h" |
10 #include "src/assert-scope.h" | 10 #include "src/assert-scope.h" |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 i::wasm::WasmModule::Instantiate(i_isolate, thrower, i_module_obj, ffi); | 224 i::wasm::WasmModule::Instantiate(i_isolate, thrower, i_module_obj, ffi); |
225 if (instance.is_null()) { | 225 if (instance.is_null()) { |
226 if (!thrower->error()) | 226 if (!thrower->error()) |
227 thrower->RuntimeError("Could not instantiate module"); | 227 thrower->RuntimeError("Could not instantiate module"); |
228 return nothing; | 228 return nothing; |
229 } | 229 } |
230 DCHECK(!i_isolate->has_pending_exception()); | 230 DCHECK(!i_isolate->has_pending_exception()); |
231 return Utils::ToLocal(instance.ToHandleChecked()); | 231 return Utils::ToLocal(instance.ToHandleChecked()); |
232 } | 232 } |
233 | 233 |
234 void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { | 234 namespace { |
235 HandleScope scope(args.GetIsolate()); | 235 i::MaybeHandle<i::WasmModuleObject> GetFirstArgumentAsModule( |
| 236 const v8::FunctionCallbackInfo<v8::Value>& args, ErrorThrower& thrower) { |
236 v8::Isolate* isolate = args.GetIsolate(); | 237 v8::Isolate* isolate = args.GetIsolate(); |
237 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 238 i::MaybeHandle<i::WasmModuleObject> nothing; |
238 | |
239 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); | |
240 | |
241 if (args.Length() < 1) { | 239 if (args.Length() < 1) { |
242 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); | 240 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); |
243 return; | 241 return nothing; |
244 } | 242 } |
245 | 243 |
246 Local<Context> context = isolate->GetCurrentContext(); | 244 Local<Context> context = isolate->GetCurrentContext(); |
247 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | 245 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); |
248 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | 246 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), |
249 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | 247 i::Handle<i::Symbol>(i_context->wasm_module_sym()), |
250 "Argument 0 must be a WebAssembly.Module")) { | 248 "Argument 0 must be a WebAssembly.Module")) { |
251 return; | 249 return nothing; |
252 } | 250 } |
253 | 251 |
254 Local<Object> module_obj = Local<Object>::Cast(args[0]); | 252 Local<Object> module_obj = Local<Object>::Cast(args[0]); |
255 i::Handle<i::WasmModuleObject> i_module_obj = | 253 return i::Handle<i::WasmModuleObject>::cast( |
256 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); | 254 v8::Utils::OpenHandle(*module_obj)); |
| 255 } |
| 256 } // namespace |
257 | 257 |
258 i::Handle<i::JSArray> imports = i::wasm::GetImports(i_isolate, i_module_obj); | 258 void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 259 HandleScope scope(args.GetIsolate()); |
| 260 v8::Isolate* isolate = args.GetIsolate(); |
| 261 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 262 ErrorThrower thrower(i_isolate, "WebAssembly.Module.imports()"); |
259 | 263 |
260 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | 264 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
261 return_value.Set(Utils::ToLocal(imports)); | 265 |
| 266 if (!maybe_module.is_null()) { |
| 267 auto imports = |
| 268 i::wasm::GetImports(i_isolate, maybe_module.ToHandleChecked()); |
| 269 args.GetReturnValue().Set(Utils::ToLocal(imports)); |
| 270 } |
262 } | 271 } |
263 | 272 |
264 void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { | 273 void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
265 HandleScope scope(args.GetIsolate()); | 274 HandleScope scope(args.GetIsolate()); |
266 v8::Isolate* isolate = args.GetIsolate(); | 275 v8::Isolate* isolate = args.GetIsolate(); |
267 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 276 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
268 | 277 |
269 ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()"); | 278 ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()"); |
270 | 279 |
271 if (args.Length() < 1) { | 280 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
272 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); | 281 |
| 282 if (!maybe_module.is_null()) { |
| 283 auto exports = |
| 284 i::wasm::GetExports(i_isolate, maybe_module.ToHandleChecked()); |
| 285 args.GetReturnValue().Set(Utils::ToLocal(exports)); |
| 286 } |
| 287 } |
| 288 |
| 289 void WebAssemblyModuleCustomSections( |
| 290 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 291 HandleScope scope(args.GetIsolate()); |
| 292 v8::Isolate* isolate = args.GetIsolate(); |
| 293 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 294 |
| 295 ErrorThrower thrower(i_isolate, "WebAssembly.Module.customSections()"); |
| 296 |
| 297 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
| 298 |
| 299 if (args.Length() < 2) { |
| 300 thrower.TypeError("Argument 1 must be a string"); |
273 return; | 301 return; |
274 } | 302 } |
275 | 303 |
276 Local<Context> context = isolate->GetCurrentContext(); | 304 i::Handle<i::Object> name = Utils::OpenHandle(*args[1]); |
277 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | 305 if (!name->IsString()) { |
278 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | 306 thrower.TypeError("Argument 1 must be a string"); |
279 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | |
280 "Argument 0 must be a WebAssembly.Module")) { | |
281 return; | 307 return; |
282 } | 308 } |
283 | 309 |
284 Local<Object> module_obj = Local<Object>::Cast(args[0]); | 310 if (!maybe_module.is_null()) { |
285 i::Handle<i::WasmModuleObject> i_module_obj = | 311 auto custom_sections = |
286 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); | 312 i::wasm::GetCustomSections(i_isolate, maybe_module.ToHandleChecked(), |
287 | 313 i::Handle<i::String>::cast(name), &thrower); |
288 i::Handle<i::JSArray> exports = i::wasm::GetExports(i_isolate, i_module_obj); | 314 if (!thrower.error()) { |
289 | 315 args.GetReturnValue().Set(Utils::ToLocal(custom_sections)); |
290 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | 316 } |
291 return_value.Set(Utils::ToLocal(exports)); | 317 } |
292 } | 318 } |
293 | 319 |
294 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { | 320 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { |
295 HandleScope scope(args.GetIsolate()); | 321 HandleScope scope(args.GetIsolate()); |
296 v8::Isolate* isolate = args.GetIsolate(); | 322 v8::Isolate* isolate = args.GetIsolate(); |
297 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 323 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
298 | 324 |
299 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); | 325 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); |
300 | 326 |
301 if (args.Length() < 1) { | 327 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
302 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); | 328 |
303 return; | 329 if (!maybe_module.is_null()) { |
| 330 MaybeLocal<Value> instance = InstantiateModuleImpl( |
| 331 i_isolate, maybe_module.ToHandleChecked(), args, &thrower); |
| 332 |
| 333 if (instance.IsEmpty()) { |
| 334 DCHECK(thrower.error()); |
| 335 return; |
| 336 } |
| 337 args.GetReturnValue().Set(instance.ToLocalChecked()); |
304 } | 338 } |
305 | |
306 Local<Context> context = isolate->GetCurrentContext(); | |
307 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | |
308 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | |
309 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | |
310 "Argument 0 must be a WebAssembly.Module")) { | |
311 return; | |
312 } | |
313 | |
314 Local<Object> module_obj = Local<Object>::Cast(args[0]); | |
315 i::Handle<i::WasmModuleObject> i_module_obj = | |
316 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); | |
317 | |
318 MaybeLocal<Value> instance = | |
319 InstantiateModuleImpl(i_isolate, i_module_obj, args, &thrower); | |
320 if (instance.IsEmpty()) { | |
321 DCHECK(thrower.error()); | |
322 return; | |
323 } | |
324 | |
325 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | |
326 return_value.Set(instance.ToLocalChecked()); | |
327 } | 339 } |
328 | 340 |
329 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 341 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
330 v8::Isolate* isolate = args.GetIsolate(); | 342 v8::Isolate* isolate = args.GetIsolate(); |
331 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 343 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
332 | 344 |
333 HandleScope scope(isolate); | 345 HandleScope scope(isolate); |
334 ErrorThrower thrower(i_isolate, "WebAssembly.instantiate()"); | 346 ErrorThrower thrower(i_isolate, "WebAssembly.instantiate()"); |
335 | 347 |
336 Local<Context> context = isolate->GetCurrentContext(); | 348 Local<Context> context = isolate->GetCurrentContext(); |
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 Handle<JSObject> module_proto = | 869 Handle<JSObject> module_proto = |
858 factory->NewJSObject(module_constructor, TENURED); | 870 factory->NewJSObject(module_constructor, TENURED); |
859 i::Handle<i::Map> module_map = isolate->factory()->NewMap( | 871 i::Handle<i::Map> module_map = isolate->factory()->NewMap( |
860 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + | 872 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + |
861 WasmModuleObject::kFieldCount * i::kPointerSize); | 873 WasmModuleObject::kFieldCount * i::kPointerSize); |
862 JSFunction::SetInitialMap(module_constructor, module_map, module_proto); | 874 JSFunction::SetInitialMap(module_constructor, module_map, module_proto); |
863 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports, | 875 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports, |
864 1); | 876 1); |
865 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, | 877 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, |
866 1); | 878 1); |
| 879 InstallFunc(isolate, module_constructor, "customSections", |
| 880 WebAssemblyModuleCustomSections, 2); |
867 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), | 881 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), |
868 module_constructor, DONT_ENUM); | 882 module_constructor, DONT_ENUM); |
869 JSObject::AddProperty(module_proto, factory->to_string_tag_symbol(), | 883 JSObject::AddProperty(module_proto, factory->to_string_tag_symbol(), |
870 v8_str(isolate, "WebAssembly.Module"), ro_attributes); | 884 v8_str(isolate, "WebAssembly.Module"), ro_attributes); |
871 | 885 |
872 // Setup Instance | 886 // Setup Instance |
873 Handle<JSFunction> instance_constructor = | 887 Handle<JSFunction> instance_constructor = |
874 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1); | 888 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1); |
875 context->set_wasm_instance_constructor(*instance_constructor); | 889 context->set_wasm_instance_constructor(*instance_constructor); |
876 Handle<JSObject> instance_proto = | 890 Handle<JSObject> instance_proto = |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); | 965 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); |
952 return HasBrand(value, symbol); | 966 return HasBrand(value, symbol); |
953 } | 967 } |
954 | 968 |
955 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { | 969 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { |
956 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); | 970 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); |
957 return HasBrand(value, symbol); | 971 return HasBrand(value, symbol); |
958 } | 972 } |
959 } // namespace internal | 973 } // namespace internal |
960 } // namespace v8 | 974 } // namespace v8 |
OLD | NEW |