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

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: test 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
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-wasm-builder.h" 7 #include "src/asmjs/asm-wasm-builder.h"
8 #include "src/asmjs/typing-asm.h" 8 #include "src/asmjs/typing-asm.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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 if (args.Length() < 1) { 292 if (args.Length() < 1) {
293 thrower.Error("Argument 0 must be a buffer source"); 293 thrower.Error("Argument 0 must be a buffer source");
294 return; 294 return;
295 } 295 }
296 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); 296 RawBuffer buffer = GetRawBufferSource(args[0], &thrower);
297 if (buffer.start == nullptr) return; 297 if (buffer.start == nullptr) return;
298 298
299 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); 299 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower);
300 } 300 }
301 301
302
303 static i::MaybeHandle<i::JSObject> CreateModuleObject( 302 static i::MaybeHandle<i::JSObject> CreateModuleObject(
304 v8::Isolate* isolate, const v8::Local<v8::Value> source, 303 v8::Isolate* isolate, const v8::Local<v8::Value> source,
305 ErrorThrower* thrower) { 304 ErrorThrower* thrower) {
306 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 305 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
306 i::MaybeHandle<i::JSObject> nothing;
307 307
308 RawBuffer buffer = GetRawBufferSource(source, thrower); 308 RawBuffer buffer = GetRawBufferSource(source, thrower);
309 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); 309 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>();
310 310
311 // TODO(rossberg): Once we can, do compilation here.
312 DCHECK(source->IsArrayBuffer() || source->IsTypedArray()); 311 DCHECK(source->IsArrayBuffer() || source->IsTypedArray());
312 i::Zone zone(i_isolate->allocator());
313 i::wasm::ModuleResult result = i::wasm::DecodeWasmModule(
314 i_isolate, &zone, buffer.start, buffer.end, false, i::wasm::kWasmOrigin);
315 std::unique_ptr<const i::wasm::WasmModule> decoded_module(result.val);
316 if (result.failed()) {
317 thrower->Failed("", result);
318 return nothing;
319 }
320 i::MaybeHandle<i::FixedArray> compiled_module =
321 decoded_module->CompileFunctions(i_isolate);
322 if (compiled_module.is_null()) return nothing;
rossberg 2016/07/04 10:04:00 It seems like CompileFunctions eagerly raises exce
Mircea Trofin 2016/07/14 16:02:58 Done, + test
313 Local<Context> context = isolate->GetCurrentContext(); 323 Local<Context> context = isolate->GetCurrentContext();
314 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 324 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
315 i::Handle<i::JSFunction> module_cons(i_context->wasm_module_constructor()); 325 i::Handle<i::JSFunction> module_cons(i_context->wasm_module_constructor());
326 i::Handle<i::Map> map = i_isolate->factory()->NewMap(
rossberg 2016/07/04 10:04:00 It isn't desirable to create a new map creation fo
Mircea Trofin 2016/07/14 16:02:58 Done.
327 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + i::kPointerSize);
328 module_cons->set_prototype_or_initial_map(*map);
329 map->SetConstructor(*module_cons);
316 i::Handle<i::JSObject> module_obj = 330 i::Handle<i::JSObject> module_obj =
317 i_isolate->factory()->NewJSObject(module_cons); 331 i_isolate->factory()->NewJSObject(module_cons);
332 module_obj->SetInternalField(0, *compiled_module.ToHandleChecked());
318 i::Handle<i::Object> module_ref = Utils::OpenHandle(*source); 333 i::Handle<i::Object> module_ref = Utils::OpenHandle(*source);
319 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym()); 334 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym());
320 i::Object::SetProperty(module_obj, module_sym, module_ref, i::STRICT).Check(); 335 i::Object::SetProperty(module_obj, module_sym, module_ref, i::STRICT).Check();
321 336
322 return module_obj; 337 return module_obj;
323 } 338 }
324 339
325 void WebAssemblyCompile(const v8::FunctionCallbackInfo<v8::Value>& args) { 340 void WebAssemblyCompile(const v8::FunctionCallbackInfo<v8::Value>& args) {
326 v8::Isolate* isolate = args.GetIsolate(); 341 v8::Isolate* isolate = args.GetIsolate();
327 HandleScope scope(isolate); 342 HandleScope scope(isolate);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 CreateModuleObject(isolate, args[0], &thrower); 374 CreateModuleObject(isolate, args[0], &thrower);
360 if (module_obj.is_null()) return; 375 if (module_obj.is_null()) return;
361 376
362 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 377 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
363 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked())); 378 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked()));
364 } 379 }
365 380
366 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { 381 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
367 HandleScope scope(args.GetIsolate()); 382 HandleScope scope(args.GetIsolate());
368 v8::Isolate* isolate = args.GetIsolate(); 383 v8::Isolate* isolate = args.GetIsolate();
369 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate), 384 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
370 "WebAssembly.Instance()");
371 385
372 if (args.Length() < 1) { 386 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
387
388 if (args.Length() < 1 || !args[0]->IsObject()) {
rossberg 2016/07/04 10:04:00 This check is not sufficient (nor necessary). You
Mircea Trofin 2016/07/05 06:02:42 Done.
rossberg 2016/07/14 13:30:28 Hm, I still don't see the code that addresses this
Mircea Trofin 2016/07/14 16:02:58 Done (hopefully this time I won't delete my own pa
373 thrower.Error("Argument 0 must be a WebAssembly.Module"); 389 thrower.Error("Argument 0 must be a WebAssembly.Module");
374 return; 390 return;
375 } 391 }
376 Local<Context> context = isolate->GetCurrentContext(); 392 Local<Object> obj = Local<Object>::Cast(args[0]);
377 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
378 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym());
379 i::MaybeHandle<i::Object> source =
380 i::Object::GetProperty(Utils::OpenHandle(*args[0]), module_sym);
381 if (source.is_null()) return;
382 393
383 RawBuffer buffer = 394 i::Handle<i::JSObject> module_obj =
384 GetRawBufferSource(Utils::ToLocal(source.ToHandleChecked()), &thrower); 395 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj));
385 if (buffer.start == nullptr) return; 396 if (module_obj->GetInternalFieldCount() < 1 ||
397 !module_obj->GetInternalField(0)->IsFixedArray()) {
398 thrower.Error("Argument 0 is an invalid WebAssembly.Module");
399 return;
400 }
386 401
387 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); 402 i::Handle<i::FixedArray> compiled_code = i::Handle<i::FixedArray>(
403 i::FixedArray::cast(module_obj->GetInternalField(0)));
404
405 i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null();
406 if (args.Length() > 1 && args[1]->IsObject()) {
407 Local<Object> obj = Local<Object>::Cast(args[1]);
408 ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj));
409 }
410
411 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
412 if (args.Length() > 2 && args[2]->IsArrayBuffer()) {
413 Local<Object> obj = Local<Object>::Cast(args[2]);
414 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
415 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj));
416 }
417 i::MaybeHandle<i::JSObject> instance =
418 i::wasm::WasmModule::Instantiate(i_isolate, compiled_code, ffi, memory);
419 if (instance.is_null()) {
420 thrower.Error("Could not instantiate module");
421 return;
422 }
423 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
424 return_value.Set(Utils::ToLocal(instance.ToHandleChecked()));
388 } 425 }
389 } // namespace 426 } // namespace
390 427
391 // TODO(titzer): we use the API to create the function template because the 428 // TODO(titzer): we use the API to create the function template because the
392 // internal guts are too ugly to replicate here. 429 // internal guts are too ugly to replicate here.
393 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate, 430 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate,
394 FunctionCallback func) { 431 FunctionCallback func) {
395 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate); 432 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate);
396 Local<FunctionTemplate> local = FunctionTemplate::New(isolate, func); 433 Local<FunctionTemplate> local = FunctionTemplate::New(isolate, func);
397 return v8::Utils::OpenHandle(*local); 434 return v8::Utils::OpenHandle(*local);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 int unused_property_fields = in_object_properties - pre_allocated; 542 int unused_property_fields = in_object_properties - pre_allocated;
506 Handle<Map> map = Map::CopyInitialMap( 543 Handle<Map> map = Map::CopyInitialMap(
507 prev_map, instance_size, in_object_properties, unused_property_fields); 544 prev_map, instance_size, in_object_properties, unused_property_fields);
508 545
509 context->set_wasm_function_map(*map); 546 context->set_wasm_function_map(*map);
510 } 547 }
511 } 548 }
512 549
513 } // namespace internal 550 } // namespace internal
514 } // namespace v8 551 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/wasm/instantiate-module-basic.js » ('j') | test/mjsunit/wasm/instantiate-module-basic.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698