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

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

Issue 2626263004: [wasm] Implement WebAssembly.Module.customSections. (Closed)
Patch Set: Fix semantics Created 3 years, 11 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 <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 2566 matching lines...) Expand 10 before | Expand all | Expand 10 after
2577 JSArray::SetContent(array_object, storage); 2577 JSArray::SetContent(array_object, storage);
2578 array_object->set_length(Smi::FromInt(num_imports)); 2578 array_object->set_length(Smi::FromInt(num_imports));
2579 2579
2580 Handle<JSFunction> object_function = 2580 Handle<JSFunction> object_function =
2581 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); 2581 Handle<JSFunction>(isolate->native_context()->object_function(), isolate);
2582 2582
2583 // Populate the result array. 2583 // Populate the result array.
2584 for (int index = 0; index < num_imports; ++index) { 2584 for (int index = 0; index < num_imports; ++index) {
2585 WasmImport& import = module->import_table[index]; 2585 WasmImport& import = module->import_table[index];
2586 2586
2587 Handle<JSObject> entry = factory->NewJSObject(object_function, TENURED); 2587 Handle<JSObject> entry = factory->NewJSObject(object_function);
2588 2588
2589 Handle<String> import_kind; 2589 Handle<String> import_kind;
2590 switch (import.kind) { 2590 switch (import.kind) {
2591 case kExternalFunction: 2591 case kExternalFunction:
2592 import_kind = function_string; 2592 import_kind = function_string;
2593 break; 2593 break;
2594 case kExternalTable: 2594 case kExternalTable:
2595 import_kind = table_string; 2595 import_kind = table_string;
2596 break; 2596 break;
2597 case kExternalMemory: 2597 case kExternalMemory:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2648 JSArray::SetContent(array_object, storage); 2648 JSArray::SetContent(array_object, storage);
2649 array_object->set_length(Smi::FromInt(num_exports)); 2649 array_object->set_length(Smi::FromInt(num_exports));
2650 2650
2651 Handle<JSFunction> object_function = 2651 Handle<JSFunction> object_function =
2652 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); 2652 Handle<JSFunction>(isolate->native_context()->object_function(), isolate);
2653 2653
2654 // Populate the result array. 2654 // Populate the result array.
2655 for (int index = 0; index < num_exports; ++index) { 2655 for (int index = 0; index < num_exports; ++index) {
2656 WasmExport& exp = module->export_table[index]; 2656 WasmExport& exp = module->export_table[index];
2657 2657
2658 Handle<JSObject> entry = factory->NewJSObject(object_function, TENURED);
2659
2660 Handle<String> export_kind; 2658 Handle<String> export_kind;
2661 switch (exp.kind) { 2659 switch (exp.kind) {
2662 case kExternalFunction: 2660 case kExternalFunction:
2663 export_kind = function_string; 2661 export_kind = function_string;
2664 break; 2662 break;
2665 case kExternalTable: 2663 case kExternalTable:
2666 export_kind = table_string; 2664 export_kind = table_string;
2667 break; 2665 break;
2668 case kExternalMemory: 2666 case kExternalMemory:
2669 export_kind = memory_string; 2667 export_kind = memory_string;
2670 break; 2668 break;
2671 case kExternalGlobal: 2669 case kExternalGlobal:
2672 export_kind = global_string; 2670 export_kind = global_string;
2673 break; 2671 break;
2674 default: 2672 default:
2675 UNREACHABLE(); 2673 UNREACHABLE();
2676 } 2674 }
2677 2675
2676 Handle<JSObject> entry = factory->NewJSObject(object_function);
2677
2678 MaybeHandle<String> export_name = 2678 MaybeHandle<String> export_name =
2679 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( 2679 WasmCompiledModule::ExtractUtf8StringFromModuleBytes(
2680 isolate, compiled_module, exp.name_offset, exp.name_length); 2680 isolate, compiled_module, exp.name_offset, exp.name_length);
2681 2681
2682 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), 2682 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(),
2683 NONE); 2683 NONE);
2684 JSObject::AddProperty(entry, kind_string, export_kind, NONE); 2684 JSObject::AddProperty(entry, kind_string, export_kind, NONE);
2685 2685
2686 storage->set(index, *entry); 2686 storage->set(index, *entry);
2687 } 2687 }
2688 2688
2689 return array_object; 2689 return array_object;
2690 } 2690 }
2691
2692 Handle<JSArray> wasm::GetCustomSections(Isolate* isolate,
2693 Handle<WasmModuleObject> module_object,
2694 Handle<String> name) {
2695 Handle<WasmCompiledModule> compiled_module(module_object->compiled_module(),
2696 isolate);
2697 Factory* factory = isolate->factory();
2698
2699 std::vector<CustomSectionOffset> custom_sections;
2700 {
2701 DisallowHeapAllocation no_gc; // for raw access to string bytes.
2702 Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(),
2703 isolate);
2704 const byte* start =
2705 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress());
2706 const byte* end = start + module_bytes->length();
2707 custom_sections = DecodeCustomSections(start, end);
2708 }
2709
2710 std::vector<Handle<Object>> matching_sections;
2711
2712 // Gather matching sections.
2713 for (auto section : custom_sections) {
2714 MaybeHandle<String> section_name =
2715 WasmCompiledModule::ExtractUtf8StringFromModuleBytes(
2716 isolate, compiled_module, section.name_offset, section.name_length);
2717
2718 if (!name->Equals(*section_name.ToHandleChecked())) continue;
2719
2720 // Make a copy of the payload data in the section.
2721 bool is_external; // Set by TryAllocateBackingStore
2722 void* memory = TryAllocateBackingStore(isolate, section.payload_length,
2723 false, is_external);
2724
2725 Handle<Object> section_data = factory->undefined_value();
2726 if (memory) {
2727 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
2728 JSArrayBuffer::Setup(buffer, isolate, is_external, memory,
2729 static_cast<int>(section.payload_length));
2730 DisallowHeapAllocation no_gc; // for raw access to string bytes.
2731 Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(),
2732 isolate);
2733 const byte* start =
2734 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress());
2735 memcpy(memory, start + section.payload_offset, section.payload_length);
2736 section_data = buffer;
2737 } else {
2738 // TODO(titzer): throw out of memory?
rossberg 2017/01/24 11:34:42 Yes please.
titzer 2017/01/24 12:38:39 Done.
2739 }
2740
2741 matching_sections.push_back(section_data);
2742 }
2743
2744 int num_custom_sections = static_cast<int>(matching_sections.size());
2745 Handle<JSArray> array_object = factory->NewJSArray(FAST_ELEMENTS, 0, 0);
2746 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections);
2747 JSArray::SetContent(array_object, storage);
2748 array_object->set_length(Smi::FromInt(num_custom_sections));
2749
2750 for (int i = 0; i < num_custom_sections; i++) {
2751 storage->set(i, *matching_sections[i]);
2752 }
2753
2754 return array_object;
2755 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698