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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/assembler-inl.h" | 7 #include "src/assembler-inl.h" |
8 #include "src/base/adapters.h" | 8 #include "src/base/adapters.h" |
9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
10 #include "src/code-stubs.h" | 10 #include "src/code-stubs.h" |
(...skipping 2575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2586 JSArray::SetContent(array_object, storage); | 2586 JSArray::SetContent(array_object, storage); |
2587 array_object->set_length(Smi::FromInt(num_imports)); | 2587 array_object->set_length(Smi::FromInt(num_imports)); |
2588 | 2588 |
2589 Handle<JSFunction> object_function = | 2589 Handle<JSFunction> object_function = |
2590 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); | 2590 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); |
2591 | 2591 |
2592 // Populate the result array. | 2592 // Populate the result array. |
2593 for (int index = 0; index < num_imports; ++index) { | 2593 for (int index = 0; index < num_imports; ++index) { |
2594 WasmImport& import = module->import_table[index]; | 2594 WasmImport& import = module->import_table[index]; |
2595 | 2595 |
2596 Handle<JSObject> entry = factory->NewJSObject(object_function, TENURED); | 2596 Handle<JSObject> entry = factory->NewJSObject(object_function); |
2597 | 2597 |
2598 Handle<String> import_kind; | 2598 Handle<String> import_kind; |
2599 switch (import.kind) { | 2599 switch (import.kind) { |
2600 case kExternalFunction: | 2600 case kExternalFunction: |
2601 import_kind = function_string; | 2601 import_kind = function_string; |
2602 break; | 2602 break; |
2603 case kExternalTable: | 2603 case kExternalTable: |
2604 import_kind = table_string; | 2604 import_kind = table_string; |
2605 break; | 2605 break; |
2606 case kExternalMemory: | 2606 case kExternalMemory: |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2657 JSArray::SetContent(array_object, storage); | 2657 JSArray::SetContent(array_object, storage); |
2658 array_object->set_length(Smi::FromInt(num_exports)); | 2658 array_object->set_length(Smi::FromInt(num_exports)); |
2659 | 2659 |
2660 Handle<JSFunction> object_function = | 2660 Handle<JSFunction> object_function = |
2661 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); | 2661 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); |
2662 | 2662 |
2663 // Populate the result array. | 2663 // Populate the result array. |
2664 for (int index = 0; index < num_exports; ++index) { | 2664 for (int index = 0; index < num_exports; ++index) { |
2665 WasmExport& exp = module->export_table[index]; | 2665 WasmExport& exp = module->export_table[index]; |
2666 | 2666 |
2667 Handle<JSObject> entry = factory->NewJSObject(object_function, TENURED); | |
2668 | |
2669 Handle<String> export_kind; | 2667 Handle<String> export_kind; |
2670 switch (exp.kind) { | 2668 switch (exp.kind) { |
2671 case kExternalFunction: | 2669 case kExternalFunction: |
2672 export_kind = function_string; | 2670 export_kind = function_string; |
2673 break; | 2671 break; |
2674 case kExternalTable: | 2672 case kExternalTable: |
2675 export_kind = table_string; | 2673 export_kind = table_string; |
2676 break; | 2674 break; |
2677 case kExternalMemory: | 2675 case kExternalMemory: |
2678 export_kind = memory_string; | 2676 export_kind = memory_string; |
2679 break; | 2677 break; |
2680 case kExternalGlobal: | 2678 case kExternalGlobal: |
2681 export_kind = global_string; | 2679 export_kind = global_string; |
2682 break; | 2680 break; |
2683 default: | 2681 default: |
2684 UNREACHABLE(); | 2682 UNREACHABLE(); |
2685 } | 2683 } |
2686 | 2684 |
| 2685 Handle<JSObject> entry = factory->NewJSObject(object_function); |
| 2686 |
2687 MaybeHandle<String> export_name = | 2687 MaybeHandle<String> export_name = |
2688 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( | 2688 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( |
2689 isolate, compiled_module, exp.name_offset, exp.name_length); | 2689 isolate, compiled_module, exp.name_offset, exp.name_length); |
2690 | 2690 |
2691 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), | 2691 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), |
2692 NONE); | 2692 NONE); |
2693 JSObject::AddProperty(entry, kind_string, export_kind, NONE); | 2693 JSObject::AddProperty(entry, kind_string, export_kind, NONE); |
2694 | 2694 |
2695 storage->set(index, *entry); | 2695 storage->set(index, *entry); |
2696 } | 2696 } |
2697 | 2697 |
2698 return array_object; | 2698 return array_object; |
2699 } | 2699 } |
| 2700 |
| 2701 Handle<JSArray> wasm::GetCustomSections(Isolate* isolate, |
| 2702 Handle<WasmModuleObject> module_object, |
| 2703 Handle<String> name, |
| 2704 ErrorThrower* thrower) { |
| 2705 Handle<WasmCompiledModule> compiled_module(module_object->compiled_module(), |
| 2706 isolate); |
| 2707 Factory* factory = isolate->factory(); |
| 2708 |
| 2709 std::vector<CustomSectionOffset> custom_sections; |
| 2710 { |
| 2711 DisallowHeapAllocation no_gc; // for raw access to string bytes. |
| 2712 Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(), |
| 2713 isolate); |
| 2714 const byte* start = |
| 2715 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress()); |
| 2716 const byte* end = start + module_bytes->length(); |
| 2717 custom_sections = DecodeCustomSections(start, end); |
| 2718 } |
| 2719 |
| 2720 std::vector<Handle<Object>> matching_sections; |
| 2721 |
| 2722 // Gather matching sections. |
| 2723 for (auto section : custom_sections) { |
| 2724 MaybeHandle<String> section_name = |
| 2725 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( |
| 2726 isolate, compiled_module, section.name_offset, section.name_length); |
| 2727 |
| 2728 if (!name->Equals(*section_name.ToHandleChecked())) continue; |
| 2729 |
| 2730 // Make a copy of the payload data in the section. |
| 2731 bool is_external; // Set by TryAllocateBackingStore |
| 2732 void* memory = TryAllocateBackingStore(isolate, section.payload_length, |
| 2733 false, is_external); |
| 2734 |
| 2735 Handle<Object> section_data = factory->undefined_value(); |
| 2736 if (memory) { |
| 2737 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
| 2738 JSArrayBuffer::Setup(buffer, isolate, is_external, memory, |
| 2739 static_cast<int>(section.payload_length)); |
| 2740 DisallowHeapAllocation no_gc; // for raw access to string bytes. |
| 2741 Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(), |
| 2742 isolate); |
| 2743 const byte* start = |
| 2744 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress()); |
| 2745 memcpy(memory, start + section.payload_offset, section.payload_length); |
| 2746 section_data = buffer; |
| 2747 } else { |
| 2748 thrower->RangeError("out of memory allocating custom section data"); |
| 2749 return Handle<JSArray>(); |
| 2750 } |
| 2751 |
| 2752 matching_sections.push_back(section_data); |
| 2753 } |
| 2754 |
| 2755 int num_custom_sections = static_cast<int>(matching_sections.size()); |
| 2756 Handle<JSArray> array_object = factory->NewJSArray(FAST_ELEMENTS, 0, 0); |
| 2757 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections); |
| 2758 JSArray::SetContent(array_object, storage); |
| 2759 array_object->set_length(Smi::FromInt(num_custom_sections)); |
| 2760 |
| 2761 for (int i = 0; i < num_custom_sections; i++) { |
| 2762 storage->set(i, *matching_sections[i]); |
| 2763 } |
| 2764 |
| 2765 return array_object; |
| 2766 } |
OLD | NEW |