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/macro-assembler.h" | 5 #include "src/macro-assembler.h" |
6 #include "src/objects.h" | 6 #include "src/objects.h" |
| 7 #include "src/property-descriptor.h" |
7 #include "src/v8.h" | 8 #include "src/v8.h" |
8 | 9 |
9 #include "src/simulator.h" | 10 #include "src/simulator.h" |
10 | 11 |
11 #include "src/wasm/ast-decoder.h" | 12 #include "src/wasm/ast-decoder.h" |
12 #include "src/wasm/module-decoder.h" | 13 #include "src/wasm/module-decoder.h" |
13 #include "src/wasm/wasm-module.h" | 14 #include "src/wasm/wasm-module.h" |
14 #include "src/wasm/wasm-result.h" | 15 #include "src/wasm/wasm-result.h" |
15 | 16 |
16 #include "src/compiler/wasm-compiler.h" | 17 #include "src/compiler/wasm-compiler.h" |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 // * compiles wasm code to machine code | 403 // * compiles wasm code to machine code |
403 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, | 404 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, |
404 Handle<JSObject> ffi, | 405 Handle<JSObject> ffi, |
405 Handle<JSArrayBuffer> memory) { | 406 Handle<JSArrayBuffer> memory) { |
406 HistogramTimerScope wasm_instantiate_module_time_scope( | 407 HistogramTimerScope wasm_instantiate_module_time_scope( |
407 isolate->counters()->wasm_instantiate_module_time()); | 408 isolate->counters()->wasm_instantiate_module_time()); |
408 this->shared_isolate = isolate; // TODO(titzer): have a real shared isolate. | 409 this->shared_isolate = isolate; // TODO(titzer): have a real shared isolate. |
409 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); | 410 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); |
410 Factory* factory = isolate->factory(); | 411 Factory* factory = isolate->factory(); |
411 | 412 |
| 413 PropertyDescriptor desc; |
| 414 desc.set_writable(false); |
| 415 |
412 //------------------------------------------------------------------------- | 416 //------------------------------------------------------------------------- |
413 // Allocate the instance and its JS counterpart. | 417 // Allocate the instance and its JS counterpart. |
414 //------------------------------------------------------------------------- | 418 //------------------------------------------------------------------------- |
415 Handle<Map> map = factory->NewMap( | 419 Handle<Map> map = factory->NewMap( |
416 JS_OBJECT_TYPE, | 420 JS_OBJECT_TYPE, |
417 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); | 421 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); |
418 WasmModuleInstance instance(this); | 422 WasmModuleInstance instance(this); |
419 instance.context = isolate->native_context(); | 423 instance.context = isolate->native_context(); |
420 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); | 424 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); |
421 Handle<FixedArray> code_table = | 425 Handle<FixedArray> code_table = |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 } | 559 } |
556 } | 560 } |
557 if (!code.is_null()) { | 561 if (!code.is_null()) { |
558 // Install the code into the linker table. | 562 // Install the code into the linker table. |
559 linker.Finish(i, code); | 563 linker.Finish(i, code); |
560 code_table->set(i, *code); | 564 code_table->set(i, *code); |
561 } | 565 } |
562 if (func.exported) { | 566 if (func.exported) { |
563 // Exported functions are installed as read-only properties on the | 567 // Exported functions are installed as read-only properties on the |
564 // module. | 568 // module. |
565 JSObject::AddProperty(instance.js_object, name, function, READ_ONLY); | 569 desc.set_value(function); |
| 570 Maybe<bool> status = JSReceiver::DefineOwnProperty( |
| 571 isolate, instance.js_object, name, &desc, Object::THROW_ON_ERROR); |
| 572 if (!status.IsJust()) |
| 573 thrower.Error("export of %.*s failed.", str.length(), str.start()); |
566 } | 574 } |
567 } | 575 } |
568 | 576 |
569 // Second pass: patch all direct call sites. | 577 // Second pass: patch all direct call sites. |
570 linker.Link(instance.function_table, this->function_table); | 578 linker.Link(instance.function_table, this->function_table); |
571 instance.js_object->SetInternalField(kWasmModuleFunctionTable, | 579 instance.js_object->SetInternalField(kWasmModuleFunctionTable, |
572 Smi::FromInt(0)); | 580 Smi::FromInt(0)); |
573 | 581 |
574 //------------------------------------------------------------------------- | 582 //------------------------------------------------------------------------- |
575 // Create and populate the exports object. | 583 // Create and populate the exports object. |
(...skipping 10 matching lines...) Expand all Loading... |
586 | 594 |
587 // Compile wrappers and add them to the exports object. | 595 // Compile wrappers and add them to the exports object. |
588 for (const WasmExport& exp : export_table) { | 596 for (const WasmExport& exp : export_table) { |
589 if (thrower.error()) break; | 597 if (thrower.error()) break; |
590 WasmName str = GetName(exp.name_offset, exp.name_length); | 598 WasmName str = GetName(exp.name_offset, exp.name_length); |
591 Handle<String> name = factory->InternalizeUtf8String(str); | 599 Handle<String> name = factory->InternalizeUtf8String(str); |
592 Handle<Code> code = linker.GetFunctionCode(exp.func_index); | 600 Handle<Code> code = linker.GetFunctionCode(exp.func_index); |
593 Handle<JSFunction> function = compiler::CompileJSToWasmWrapper( | 601 Handle<JSFunction> function = compiler::CompileJSToWasmWrapper( |
594 isolate, &module_env, name, code, instance.js_object, | 602 isolate, &module_env, name, code, instance.js_object, |
595 exp.func_index); | 603 exp.func_index); |
596 JSObject::AddProperty(exports_object, name, function, READ_ONLY); | 604 desc.set_value(function); |
| 605 Maybe<bool> status = JSReceiver::DefineOwnProperty( |
| 606 isolate, exports_object, name, &desc, Object::THROW_ON_ERROR); |
| 607 if (!status.IsJust()) |
| 608 thrower.Error("export of %.*s failed.", str.length(), str.start()); |
597 } | 609 } |
598 | 610 |
599 if (mem_export) { | 611 if (mem_export) { |
600 // Export the memory as a named property. | 612 // Export the memory as a named property. |
601 Handle<String> name = factory->InternalizeUtf8String("memory"); | 613 Handle<String> name = factory->InternalizeUtf8String("memory"); |
602 JSObject::AddProperty(exports_object, name, instance.mem_buffer, | 614 JSObject::AddProperty(exports_object, name, instance.mem_buffer, |
603 READ_ONLY); | 615 READ_ONLY); |
604 } | 616 } |
605 } | 617 } |
606 } | 618 } |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 } | 763 } |
752 if (result->IsHeapNumber()) { | 764 if (result->IsHeapNumber()) { |
753 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); | 765 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
754 } | 766 } |
755 thrower.Error("WASM.compileRun() failed: Return value should be number"); | 767 thrower.Error("WASM.compileRun() failed: Return value should be number"); |
756 return -1; | 768 return -1; |
757 } | 769 } |
758 } // namespace wasm | 770 } // namespace wasm |
759 } // namespace internal | 771 } // namespace internal |
760 } // namespace v8 | 772 } // namespace v8 |
OLD | NEW |