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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } | 185 } |
186 i::MaybeHandle<i::JSObject> module_obj = | 186 i::MaybeHandle<i::JSObject> module_obj = |
187 CreateModuleObject(isolate, args[0], &thrower); | 187 CreateModuleObject(isolate, args[0], &thrower); |
188 if (module_obj.is_null()) return; | 188 if (module_obj.is_null()) return; |
189 | 189 |
190 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | 190 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
191 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked())); | 191 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked())); |
192 } | 192 } |
193 | 193 |
194 MaybeLocal<Value> InstantiateModuleImpl( | 194 MaybeLocal<Value> InstantiateModuleImpl( |
195 i::Isolate* i_isolate, i::Handle<i::JSObject> i_module_obj, | 195 i::Isolate* i_isolate, i::Handle<i::WasmModuleObject> i_module_obj, |
196 const v8::FunctionCallbackInfo<v8::Value>& args, ErrorThrower* thrower) { | 196 const v8::FunctionCallbackInfo<v8::Value>& args, ErrorThrower* thrower) { |
197 // It so happens that in both the WebAssembly.instantiate, as well as | 197 // It so happens that in both the WebAssembly.instantiate, as well as |
198 // WebAssembly.Instance ctor, the positions of the ffi object and memory | 198 // WebAssembly.Instance ctor, the positions of the ffi object and memory |
199 // are the same. If that changes later, we refactor the consts into | 199 // are the same. If that changes later, we refactor the consts into |
200 // parameters. | 200 // parameters. |
201 static const int kFfiOffset = 1; | 201 static const int kFfiOffset = 1; |
202 static const int kMemOffset = 2; | 202 static const int kMemOffset = 2; |
203 | 203 |
204 MaybeLocal<Value> nothing; | 204 MaybeLocal<Value> nothing; |
205 i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null(); | 205 i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null(); |
206 if (args.Length() > kFfiOffset && !args[kFfiOffset]->IsUndefined()) { | 206 if (args.Length() > kFfiOffset && !args[kFfiOffset]->IsUndefined()) { |
207 if (!args[kFfiOffset]->IsObject()) { | 207 if (!args[kFfiOffset]->IsObject()) { |
208 thrower->TypeError("Argument %d must be an object", kFfiOffset); | 208 thrower->TypeError("Argument %d must be an object", kFfiOffset); |
209 return nothing; | 209 return nothing; |
210 } | 210 } |
211 Local<Object> obj = Local<Object>::Cast(args[kFfiOffset]); | 211 Local<Object> obj = Local<Object>::Cast(args[kFfiOffset]); |
212 ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); | 212 ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); |
213 } | 213 } |
| 214 if (i_module_obj->compiled_module()->module()->num_imported_functions > 0 && |
| 215 ffi.is_null()) { |
| 216 thrower->TypeError("Argument %d must be present and must be an object", |
| 217 kFfiOffset); |
| 218 return nothing; |
| 219 } |
214 | 220 |
215 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); | 221 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); |
216 if (args.Length() > kMemOffset && !args[kMemOffset]->IsUndefined()) { | 222 if (args.Length() > kMemOffset && !args[kMemOffset]->IsUndefined()) { |
217 if (!args[kMemOffset]->IsObject()) { | 223 if (!args[kMemOffset]->IsObject()) { |
218 thrower->TypeError("Argument %d must be a WebAssembly.Memory", | 224 thrower->TypeError("Argument %d must be a WebAssembly.Memory", |
219 kMemOffset); | 225 kMemOffset); |
220 return nothing; | 226 return nothing; |
221 } | 227 } |
222 Local<Object> obj = Local<Object>::Cast(args[kMemOffset]); | 228 Local<Object> obj = Local<Object>::Cast(args[kMemOffset]); |
223 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); | 229 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 320 |
315 Local<Context> context = isolate->GetCurrentContext(); | 321 Local<Context> context = isolate->GetCurrentContext(); |
316 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | 322 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); |
317 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), | 323 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), |
318 i::Handle<i::Symbol>(i_context->wasm_module_sym()), | 324 i::Handle<i::Symbol>(i_context->wasm_module_sym()), |
319 "Argument 0 must be a WebAssembly.Module")) { | 325 "Argument 0 must be a WebAssembly.Module")) { |
320 return; | 326 return; |
321 } | 327 } |
322 | 328 |
323 Local<Object> module_obj = Local<Object>::Cast(args[0]); | 329 Local<Object> module_obj = Local<Object>::Cast(args[0]); |
324 i::Handle<i::JSObject> i_module_obj = | 330 i::Handle<i::WasmModuleObject> i_module_obj = |
325 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*module_obj)); | 331 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); |
326 | 332 |
327 MaybeLocal<Value> instance = | 333 MaybeLocal<Value> instance = |
328 InstantiateModuleImpl(i_isolate, i_module_obj, args, &thrower); | 334 InstantiateModuleImpl(i_isolate, i_module_obj, args, &thrower); |
329 if (instance.IsEmpty()) return; | 335 if (instance.IsEmpty()) return; |
330 | 336 |
331 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | 337 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
332 return_value.Set(instance.ToLocalChecked()); | 338 return_value.Set(instance.ToLocalChecked()); |
333 } | 339 } |
334 | 340 |
335 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 341 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
336 v8::Isolate* isolate = args.GetIsolate(); | 342 v8::Isolate* isolate = args.GetIsolate(); |
337 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 343 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
338 | 344 |
339 HandleScope scope(isolate); | 345 HandleScope scope(isolate); |
340 ErrorThrower thrower(i_isolate, "WebAssembly.compile()"); | 346 ErrorThrower thrower(i_isolate, "WebAssembly.compile()"); |
341 | 347 |
342 if (args.Length() < 1) { | 348 if (args.Length() < 1) { |
343 thrower.TypeError("Argument 0 must be a buffer source"); | 349 thrower.TypeError("Argument 0 must be a buffer source"); |
344 return; | 350 return; |
345 } | 351 } |
346 i::MaybeHandle<i::JSObject> module_obj = | 352 i::MaybeHandle<i::WasmModuleObject> module_obj = |
347 CreateModuleObject(isolate, args[0], &thrower); | 353 CreateModuleObject(isolate, args[0], &thrower); |
348 | 354 |
349 Local<Context> context = isolate->GetCurrentContext(); | 355 Local<Context> context = isolate->GetCurrentContext(); |
350 v8::Local<v8::Promise::Resolver> resolver; | 356 v8::Local<v8::Promise::Resolver> resolver; |
351 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) return; | 357 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) return; |
352 if (module_obj.is_null()) { | 358 if (module_obj.is_null()) { |
353 DCHECK(thrower.error()); | 359 DCHECK(thrower.error()); |
354 resolver->Reject(context, Utils::ToLocal(thrower.Reify())); | 360 resolver->Reject(context, Utils::ToLocal(thrower.Reify())); |
355 } else { | 361 } else { |
356 MaybeLocal<Value> instance = InstantiateModuleImpl( | 362 MaybeLocal<Value> instance = InstantiateModuleImpl( |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); | 897 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); |
892 return HasBrand(value, symbol); | 898 return HasBrand(value, symbol); |
893 } | 899 } |
894 | 900 |
895 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { | 901 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { |
896 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); | 902 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); |
897 return HasBrand(value, symbol); | 903 return HasBrand(value, symbol); |
898 } | 904 } |
899 } // namespace internal | 905 } // namespace internal |
900 } // namespace v8 | 906 } // namespace v8 |
OLD | NEW |