Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(822)

Side by Side Diff: src/wasm/wasm-js.cc

Issue 2121593002: [wasm] Compile and Instantiation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: module_sym Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/asmjs/asm-js.cc ('k') | src/wasm/wasm-module.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-typer.h" 7 #include "src/asmjs/asm-typer.h"
8 #include "src/asmjs/asm-wasm-builder.h" 8 #include "src/asmjs/asm-wasm-builder.h"
9 #include "src/assert-scope.h" 9 #include "src/assert-scope.h"
10 #include "src/ast/ast.h" 10 #include "src/ast/ast.h"
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 188 }
189 189
190 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); 190 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
191 if (args.Length() > 2 && args[2]->IsArrayBuffer()) { 191 if (args.Length() > 2 && args[2]->IsArrayBuffer()) {
192 Local<Object> obj = Local<Object>::Cast(args[2]); 192 Local<Object> obj = Local<Object>::Cast(args[2]);
193 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); 193 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
194 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); 194 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj));
195 } 195 }
196 196
197 i::MaybeHandle<i::FixedArray> compiled_module = 197 i::MaybeHandle<i::FixedArray> compiled_module =
198 result.val->CompileFunctions(isolate); 198 result.val->CompileFunctions(isolate, thrower);
199 if (!compiled_module.is_null()) { 199 if (!thrower->error()) {
200 DCHECK(!compiled_module.is_null());
200 object = i::wasm::WasmModule::Instantiate( 201 object = i::wasm::WasmModule::Instantiate(
201 isolate, compiled_module.ToHandleChecked(), ffi, memory); 202 isolate, compiled_module.ToHandleChecked(), ffi, memory);
202 if (!object.is_null()) { 203 if (!object.is_null()) {
203 args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked())); 204 args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked()));
204 } 205 }
205 } 206 }
206 } 207 }
207 208
208 if (result.val) delete result.val; 209 if (result.val) delete result.val;
209 return object; 210 return object;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 if (args.Length() < 1) { 290 if (args.Length() < 1) {
290 thrower.Error("Argument 0 must be a buffer source"); 291 thrower.Error("Argument 0 must be a buffer source");
291 return; 292 return;
292 } 293 }
293 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); 294 RawBuffer buffer = GetRawBufferSource(args[0], &thrower);
294 if (buffer.start == nullptr) return; 295 if (buffer.start == nullptr) return;
295 296
296 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); 297 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower);
297 } 298 }
298 299
299
300 static i::MaybeHandle<i::JSObject> CreateModuleObject( 300 static i::MaybeHandle<i::JSObject> CreateModuleObject(
301 v8::Isolate* isolate, const v8::Local<v8::Value> source, 301 v8::Isolate* isolate, const v8::Local<v8::Value> source,
302 ErrorThrower* thrower) { 302 ErrorThrower* thrower) {
303 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 303 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
304 i::MaybeHandle<i::JSObject> nothing;
304 305
305 RawBuffer buffer = GetRawBufferSource(source, thrower); 306 RawBuffer buffer = GetRawBufferSource(source, thrower);
306 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); 307 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>();
307 308
308 // TODO(rossberg): Once we can, do compilation here.
309 DCHECK(source->IsArrayBuffer() || source->IsTypedArray()); 309 DCHECK(source->IsArrayBuffer() || source->IsTypedArray());
310 i::Zone zone(i_isolate->allocator());
311 i::wasm::ModuleResult result = i::wasm::DecodeWasmModule(
312 i_isolate, &zone, buffer.start, buffer.end, false, i::wasm::kWasmOrigin);
313 std::unique_ptr<const i::wasm::WasmModule> decoded_module(result.val);
314 if (result.failed()) {
315 thrower->Failed("", result);
316 return nothing;
317 }
318 i::MaybeHandle<i::FixedArray> compiled_module =
319 decoded_module->CompileFunctions(i_isolate, thrower);
320 if (compiled_module.is_null()) return nothing;
310 Local<Context> context = isolate->GetCurrentContext(); 321 Local<Context> context = isolate->GetCurrentContext();
311 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 322 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
312 i::Handle<i::JSFunction> module_cons(i_context->wasm_module_constructor()); 323 i::Handle<i::JSFunction> module_cons(i_context->wasm_module_constructor());
313 i::Handle<i::JSObject> module_obj = 324 i::Handle<i::JSObject> module_obj =
314 i_isolate->factory()->NewJSObject(module_cons); 325 i_isolate->factory()->NewJSObject(module_cons);
326 module_obj->SetInternalField(0, *compiled_module.ToHandleChecked());
315 i::Handle<i::Object> module_ref = Utils::OpenHandle(*source); 327 i::Handle<i::Object> module_ref = Utils::OpenHandle(*source);
316 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym()); 328 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym());
317 i::Object::SetProperty(module_obj, module_sym, module_ref, i::STRICT).Check(); 329 i::Object::SetProperty(module_obj, module_sym, module_ref, i::STRICT).Check();
318 330
319 return module_obj; 331 return module_obj;
320 } 332 }
321 333
322 void WebAssemblyCompile(const v8::FunctionCallbackInfo<v8::Value>& args) { 334 void WebAssemblyCompile(const v8::FunctionCallbackInfo<v8::Value>& args) {
323 v8::Isolate* isolate = args.GetIsolate(); 335 v8::Isolate* isolate = args.GetIsolate();
324 HandleScope scope(isolate); 336 HandleScope scope(isolate);
325 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate), 337 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
326 "WebAssembly.compile()"); 338 "WebAssembly.compile()");
327 339
328 if (args.Length() < 1) { 340 if (args.Length() < 1) {
329 thrower.Error("Argument 0 must be a buffer source"); 341 thrower.Error("Argument 0 must be a buffer source");
330 return; 342 return;
331 } 343 }
332 i::MaybeHandle<i::JSObject> module_obj = 344 i::MaybeHandle<i::JSObject> module_obj =
333 CreateModuleObject(isolate, args[0], &thrower); 345 CreateModuleObject(isolate, args[0], &thrower);
334 if (module_obj.is_null()) return;
335 346
336 Local<Context> context = isolate->GetCurrentContext(); 347 Local<Context> context = isolate->GetCurrentContext();
337 v8::Local<v8::Promise::Resolver> resolver; 348 v8::Local<v8::Promise::Resolver> resolver;
338 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) return; 349 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) return;
339 resolver->Resolve(context, Utils::ToLocal(module_obj.ToHandleChecked())); 350 if (thrower.error()) {
340 351 resolver->Reject(context, Utils::ToLocal(thrower.Reify()));
352 } else {
353 resolver->Resolve(context, Utils::ToLocal(module_obj.ToHandleChecked()));
354 }
341 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 355 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
342 return_value.Set(resolver->GetPromise()); 356 return_value.Set(resolver->GetPromise());
343 } 357 }
344 358
345 void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) { 359 void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
346 v8::Isolate* isolate = args.GetIsolate(); 360 v8::Isolate* isolate = args.GetIsolate();
347 HandleScope scope(isolate); 361 HandleScope scope(isolate);
348 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate), 362 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
349 "WebAssembly.Module()"); 363 "WebAssembly.Module()");
350 364
351 if (args.Length() < 1) { 365 if (args.Length() < 1) {
352 thrower.Error("Argument 0 must be a buffer source"); 366 thrower.Error("Argument 0 must be a buffer source");
353 return; 367 return;
354 } 368 }
355 i::MaybeHandle<i::JSObject> module_obj = 369 i::MaybeHandle<i::JSObject> module_obj =
356 CreateModuleObject(isolate, args[0], &thrower); 370 CreateModuleObject(isolate, args[0], &thrower);
357 if (module_obj.is_null()) return; 371 if (module_obj.is_null()) return;
358 372
359 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 373 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
360 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked())); 374 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked()));
361 } 375 }
362 376
363 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { 377 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
364 HandleScope scope(args.GetIsolate()); 378 HandleScope scope(args.GetIsolate());
365 v8::Isolate* isolate = args.GetIsolate(); 379 v8::Isolate* isolate = args.GetIsolate();
366 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate), 380 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
367 "WebAssembly.Instance()"); 381
382 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
368 383
369 if (args.Length() < 1) { 384 if (args.Length() < 1) {
370 thrower.Error("Argument 0 must be a WebAssembly.Module"); 385 thrower.Error(
386 "Argument 0 must be provided, and must be a WebAssembly.Module object");
371 return; 387 return;
372 } 388 }
389
373 Local<Context> context = isolate->GetCurrentContext(); 390 Local<Context> context = isolate->GetCurrentContext();
374 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 391 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
375 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym()); 392 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym());
376 i::MaybeHandle<i::Object> source = 393 i::MaybeHandle<i::Object> source =
377 i::Object::GetProperty(Utils::OpenHandle(*args[0]), module_sym); 394 i::Object::GetProperty(Utils::OpenHandle(*args[0]), module_sym);
378 if (source.is_null()) return; 395 if (source.is_null() || source.ToHandleChecked()->IsUndefined(i_isolate)) {
396 thrower.Error("Argument 0 must be a WebAssembly.Module");
397 return;
398 }
379 399
380 RawBuffer buffer = 400 Local<Object> obj = Local<Object>::Cast(args[0]);
381 GetRawBufferSource(Utils::ToLocal(source.ToHandleChecked()), &thrower);
382 if (buffer.start == nullptr) return;
383 401
384 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); 402 i::Handle<i::JSObject> module_obj =
403 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj));
404 if (module_obj->GetInternalFieldCount() < 1 ||
405 !module_obj->GetInternalField(0)->IsFixedArray()) {
406 thrower.Error("Argument 0 is an invalid WebAssembly.Module");
407 return;
408 }
409
410 i::Handle<i::FixedArray> compiled_code = i::Handle<i::FixedArray>(
411 i::FixedArray::cast(module_obj->GetInternalField(0)));
412
413 i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null();
414 if (args.Length() > 1 && args[1]->IsObject()) {
415 Local<Object> obj = Local<Object>::Cast(args[1]);
416 ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj));
417 }
418
419 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
420 if (args.Length() > 2 && args[2]->IsArrayBuffer()) {
421 Local<Object> obj = Local<Object>::Cast(args[2]);
422 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
423 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj));
424 }
425 i::MaybeHandle<i::JSObject> instance =
426 i::wasm::WasmModule::Instantiate(i_isolate, compiled_code, ffi, memory);
427 if (instance.is_null()) {
428 thrower.Error("Could not instantiate module");
429 return;
430 }
431 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
432 return_value.Set(Utils::ToLocal(instance.ToHandleChecked()));
385 } 433 }
386 } // namespace 434 } // namespace
387 435
388 // TODO(titzer): we use the API to create the function template because the 436 // TODO(titzer): we use the API to create the function template because the
389 // internal guts are too ugly to replicate here. 437 // internal guts are too ugly to replicate here.
390 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate, 438 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate,
391 FunctionCallback func) { 439 FunctionCallback func) {
392 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate); 440 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate);
393 Local<FunctionTemplate> local = FunctionTemplate::New(isolate, func); 441 Local<FunctionTemplate> local = FunctionTemplate::New(isolate, func);
394 return v8::Utils::OpenHandle(*local); 442 return v8::Utils::OpenHandle(*local);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED); 519 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED);
472 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM); 520 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM);
473 JSObject::AddProperty(global, name, wasm_object, attributes); 521 JSObject::AddProperty(global, name, wasm_object, attributes);
474 522
475 // Install static methods on WebAssembly object. 523 // Install static methods on WebAssembly object.
476 InstallFunc(isolate, wasm_object, "compile", WebAssemblyCompile); 524 InstallFunc(isolate, wasm_object, "compile", WebAssemblyCompile);
477 Handle<JSFunction> module_constructor = 525 Handle<JSFunction> module_constructor =
478 InstallFunc(isolate, wasm_object, "Module", WebAssemblyModule); 526 InstallFunc(isolate, wasm_object, "Module", WebAssemblyModule);
479 Handle<JSFunction> instance_constructor = 527 Handle<JSFunction> instance_constructor =
480 InstallFunc(isolate, wasm_object, "Instance", WebAssemblyInstance); 528 InstallFunc(isolate, wasm_object, "Instance", WebAssemblyInstance);
529 i::Handle<i::Map> map = isolate->factory()->NewMap(
530 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + i::kPointerSize);
531 module_constructor->set_prototype_or_initial_map(*map);
532 map->SetConstructor(*module_constructor);
533
481 context->set_wasm_module_constructor(*module_constructor); 534 context->set_wasm_module_constructor(*module_constructor);
482 context->set_wasm_instance_constructor(*instance_constructor); 535 context->set_wasm_instance_constructor(*instance_constructor);
483 } 536 }
484 537
485 void WasmJs::InstallWasmFunctionMap(Isolate* isolate, Handle<Context> context) { 538 void WasmJs::InstallWasmFunctionMap(Isolate* isolate, Handle<Context> context) {
486 if (!context->get(Context::WASM_FUNCTION_MAP_INDEX)->IsMap()) { 539 if (!context->get(Context::WASM_FUNCTION_MAP_INDEX)->IsMap()) {
487 // TODO(titzer): Move this to bootstrapper.cc?? 540 // TODO(titzer): Move this to bootstrapper.cc??
488 // TODO(titzer): Also make one for strict mode functions? 541 // TODO(titzer): Also make one for strict mode functions?
489 Handle<Map> prev_map = Handle<Map>(context->sloppy_function_map(), isolate); 542 Handle<Map> prev_map = Handle<Map>(context->sloppy_function_map(), isolate);
490 543
(...skipping 11 matching lines...) Expand all
502 int unused_property_fields = in_object_properties - pre_allocated; 555 int unused_property_fields = in_object_properties - pre_allocated;
503 Handle<Map> map = Map::CopyInitialMap( 556 Handle<Map> map = Map::CopyInitialMap(
504 prev_map, instance_size, in_object_properties, unused_property_fields); 557 prev_map, instance_size, in_object_properties, unused_property_fields);
505 558
506 context->set_wasm_function_map(*map); 559 context->set_wasm_function_map(*map);
507 } 560 }
508 } 561 }
509 562
510 } // namespace internal 563 } // namespace internal
511 } // namespace v8 564 } // namespace v8
OLDNEW
« no previous file with comments | « src/asmjs/asm-js.cc ('k') | src/wasm/wasm-module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698