| 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 i_isolate, thrower, i_module_obj, ffi, memory); | 229 i_isolate, thrower, i_module_obj, ffi, memory); |
| 230 if (instance.is_null()) { | 230 if (instance.is_null()) { |
| 231 if (!thrower->error()) | 231 if (!thrower->error()) |
| 232 thrower->RuntimeError("Could not instantiate module"); | 232 thrower->RuntimeError("Could not instantiate module"); |
| 233 return nothing; | 233 return nothing; |
| 234 } | 234 } |
| 235 DCHECK(!i_isolate->has_pending_exception()); | 235 DCHECK(!i_isolate->has_pending_exception()); |
| 236 return Utils::ToLocal(instance.ToHandleChecked()); | 236 return Utils::ToLocal(instance.ToHandleChecked()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { | 239 namespace { |
| 240 HandleScope scope(args.GetIsolate()); | 240 i::MaybeHandle<i::WasmModuleObject> GetFirstArgumentAsModule( |
| 241 const v8::FunctionCallbackInfo<v8::Value>& args, ErrorThrower& thrower) { |
| 241 v8::Isolate* isolate = args.GetIsolate(); | 242 v8::Isolate* isolate = args.GetIsolate(); |
| 242 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 243 i::MaybeHandle<i::WasmModuleObject> nothing; |
| 243 | |
| 244 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); | |
| 245 | |
| 246 if (args.Length() < 1) { | 244 if (args.Length() < 1) { |
| 247 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); | 245 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); |
| 248 return; | 246 return nothing; |
| 249 } | 247 } |
| 250 | 248 |
| 251 Local<Context> context = isolate->GetCurrentContext(); | 249 Local<Context> context = isolate->GetCurrentContext(); |
| 252 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | 250 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); |
| 253 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | 251 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), |
| 254 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | 252 i::Handle<i::Symbol>(i_context->wasm_module_sym()), |
| 255 "Argument 0 must be a WebAssembly.Module")) { | 253 "Argument 0 must be a WebAssembly.Module")) { |
| 256 return; | 254 return nothing; |
| 257 } | 255 } |
| 258 | 256 |
| 259 Local<Object> module_obj = Local<Object>::Cast(args[0]); | 257 Local<Object> module_obj = Local<Object>::Cast(args[0]); |
| 260 i::Handle<i::WasmModuleObject> i_module_obj = | 258 return i::Handle<i::WasmModuleObject>::cast( |
| 261 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); | 259 v8::Utils::OpenHandle(*module_obj)); |
| 260 } |
| 261 } // namespace |
| 262 | 262 |
| 263 i::Handle<i::JSArray> imports = i::wasm::GetImports(i_isolate, i_module_obj); | 263 void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 264 HandleScope scope(args.GetIsolate()); |
| 265 v8::Isolate* isolate = args.GetIsolate(); |
| 266 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 267 ErrorThrower thrower(i_isolate, "WebAssembly.Module.imports()"); |
| 264 | 268 |
| 265 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | 269 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
| 266 return_value.Set(Utils::ToLocal(imports)); | 270 |
| 271 if (!maybe_module.is_null()) { |
| 272 auto imports = |
| 273 i::wasm::GetImports(i_isolate, maybe_module.ToHandleChecked()); |
| 274 args.GetReturnValue().Set(Utils::ToLocal(imports)); |
| 275 } |
| 267 } | 276 } |
| 268 | 277 |
| 269 void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { | 278 void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 270 HandleScope scope(args.GetIsolate()); | 279 HandleScope scope(args.GetIsolate()); |
| 271 v8::Isolate* isolate = args.GetIsolate(); | 280 v8::Isolate* isolate = args.GetIsolate(); |
| 272 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 281 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 273 | 282 |
| 274 ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()"); | 283 ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()"); |
| 275 | 284 |
| 276 if (args.Length() < 1) { | 285 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
| 277 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); | 286 |
| 278 return; | 287 if (!maybe_module.is_null()) { |
| 288 auto exports = |
| 289 i::wasm::GetExports(i_isolate, maybe_module.ToHandleChecked()); |
| 290 args.GetReturnValue().Set(Utils::ToLocal(exports)); |
| 279 } | 291 } |
| 292 } |
| 280 | 293 |
| 281 Local<Context> context = isolate->GetCurrentContext(); | 294 void WebAssemblyModuleCustomSections( |
| 282 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | 295 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 283 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | 296 HandleScope scope(args.GetIsolate()); |
| 284 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | 297 v8::Isolate* isolate = args.GetIsolate(); |
| 285 "Argument 0 must be a WebAssembly.Module")) { | 298 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 286 return; | 299 |
| 300 ErrorThrower thrower(i_isolate, "WebAssembly.Module.customSections()"); |
| 301 |
| 302 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
| 303 |
| 304 if (!maybe_module.is_null()) { |
| 305 auto custom_sections = |
| 306 i::wasm::GetCustomSections(i_isolate, maybe_module.ToHandleChecked()); |
| 307 args.GetReturnValue().Set(Utils::ToLocal(custom_sections)); |
| 287 } | 308 } |
| 288 | |
| 289 Local<Object> module_obj = Local<Object>::Cast(args[0]); | |
| 290 i::Handle<i::WasmModuleObject> i_module_obj = | |
| 291 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); | |
| 292 | |
| 293 i::Handle<i::JSArray> exports = i::wasm::GetExports(i_isolate, i_module_obj); | |
| 294 | |
| 295 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | |
| 296 return_value.Set(Utils::ToLocal(exports)); | |
| 297 } | 309 } |
| 298 | 310 |
| 299 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { | 311 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 300 HandleScope scope(args.GetIsolate()); | 312 HandleScope scope(args.GetIsolate()); |
| 301 v8::Isolate* isolate = args.GetIsolate(); | 313 v8::Isolate* isolate = args.GetIsolate(); |
| 302 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 314 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 303 | 315 |
| 304 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); | 316 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); |
| 305 | 317 |
| 306 if (args.Length() < 1) { | 318 auto maybe_module = GetFirstArgumentAsModule(args, thrower); |
| 307 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); | 319 |
| 308 return; | 320 if (!maybe_module.is_null()) { |
| 321 MaybeLocal<Value> instance = InstantiateModuleImpl( |
| 322 i_isolate, maybe_module.ToHandleChecked(), args, &thrower); |
| 323 if (instance.IsEmpty()) return; |
| 324 |
| 325 args.GetReturnValue().Set(instance.ToLocalChecked()); |
| 309 } | 326 } |
| 310 | |
| 311 Local<Context> context = isolate->GetCurrentContext(); | |
| 312 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | |
| 313 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | |
| 314 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | |
| 315 "Argument 0 must be a WebAssembly.Module")) { | |
| 316 return; | |
| 317 } | |
| 318 | |
| 319 Local<Object> module_obj = Local<Object>::Cast(args[0]); | |
| 320 i::Handle<i::JSObject> i_module_obj = | |
| 321 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*module_obj)); | |
| 322 | |
| 323 MaybeLocal<Value> instance = | |
| 324 InstantiateModuleImpl(i_isolate, i_module_obj, args, &thrower); | |
| 325 if (instance.IsEmpty()) return; | |
| 326 | |
| 327 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | |
| 328 return_value.Set(instance.ToLocalChecked()); | |
| 329 } | 327 } |
| 330 | 328 |
| 331 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 329 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 332 v8::Isolate* isolate = args.GetIsolate(); | 330 v8::Isolate* isolate = args.GetIsolate(); |
| 333 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 331 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 334 | 332 |
| 335 HandleScope scope(isolate); | 333 HandleScope scope(isolate); |
| 336 ErrorThrower thrower(i_isolate, "WebAssembly.compile()"); | 334 ErrorThrower thrower(i_isolate, "WebAssembly.compile()"); |
| 337 | 335 |
| 338 if (args.Length() < 1) { | 336 if (args.Length() < 1) { |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 i::Handle<i::Map> module_map = isolate->factory()->NewMap( | 801 i::Handle<i::Map> module_map = isolate->factory()->NewMap( |
| 804 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + | 802 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + |
| 805 WasmModuleObject::kFieldCount * i::kPointerSize); | 803 WasmModuleObject::kFieldCount * i::kPointerSize); |
| 806 JSFunction::SetInitialMap(module_constructor, module_map, module_proto); | 804 JSFunction::SetInitialMap(module_constructor, module_map, module_proto); |
| 807 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), | 805 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), |
| 808 module_constructor, DONT_ENUM); | 806 module_constructor, DONT_ENUM); |
| 809 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports, | 807 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports, |
| 810 1); | 808 1); |
| 811 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, | 809 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, |
| 812 1); | 810 1); |
| 811 InstallFunc(isolate, module_constructor, "customSections", |
| 812 WebAssemblyModuleCustomSections, 1); |
| 813 | 813 |
| 814 // Setup Instance | 814 // Setup Instance |
| 815 Handle<JSFunction> instance_constructor = | 815 Handle<JSFunction> instance_constructor = |
| 816 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1); | 816 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1); |
| 817 context->set_wasm_instance_constructor(*instance_constructor); | 817 context->set_wasm_instance_constructor(*instance_constructor); |
| 818 Handle<JSObject> instance_proto = | 818 Handle<JSObject> instance_proto = |
| 819 factory->NewJSObject(instance_constructor, TENURED); | 819 factory->NewJSObject(instance_constructor, TENURED); |
| 820 i::Handle<i::Map> instance_map = isolate->factory()->NewMap( | 820 i::Handle<i::Map> instance_map = isolate->factory()->NewMap( |
| 821 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + | 821 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + |
| 822 WasmInstanceObject::kFieldCount * i::kPointerSize); | 822 WasmInstanceObject::kFieldCount * i::kPointerSize); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); | 887 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); |
| 888 return HasBrand(value, symbol); | 888 return HasBrand(value, symbol); |
| 889 } | 889 } |
| 890 | 890 |
| 891 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { | 891 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { |
| 892 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); | 892 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); |
| 893 return HasBrand(value, symbol); | 893 return HasBrand(value, symbol); |
| 894 } | 894 } |
| 895 } // namespace internal | 895 } // namespace internal |
| 896 } // namespace v8 | 896 } // namespace v8 |
| OLD | NEW |