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

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

Issue 2626263004: [wasm] Implement WebAssembly.Module.customSections. (Closed)
Patch Set: 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 2387 matching lines...) Expand 10 before | Expand all | Expand 10 after
2398 JSArray::SetContent(array_object, storage); 2398 JSArray::SetContent(array_object, storage);
2399 array_object->set_length(Smi::FromInt(num_imports)); 2399 array_object->set_length(Smi::FromInt(num_imports));
2400 2400
2401 Handle<JSFunction> object_function = 2401 Handle<JSFunction> object_function =
2402 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); 2402 Handle<JSFunction>(isolate->native_context()->object_function(), isolate);
2403 2403
2404 // Populate the result array. 2404 // Populate the result array.
2405 for (int index = 0; index < num_imports; ++index) { 2405 for (int index = 0; index < num_imports; ++index) {
2406 WasmImport& import = module->import_table[index]; 2406 WasmImport& import = module->import_table[index];
2407 2407
2408 Handle<JSObject> entry = factory->NewJSObject(object_function, TENURED); 2408 Handle<JSObject> entry = factory->NewJSObject(object_function);
2409 2409
2410 Handle<String> import_kind; 2410 Handle<String> import_kind;
2411 switch (import.kind) { 2411 switch (import.kind) {
2412 case kExternalFunction: 2412 case kExternalFunction:
2413 import_kind = function_string; 2413 import_kind = function_string;
2414 break; 2414 break;
2415 case kExternalTable: 2415 case kExternalTable:
2416 import_kind = table_string; 2416 import_kind = table_string;
2417 break; 2417 break;
2418 case kExternalMemory: 2418 case kExternalMemory:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2469 JSArray::SetContent(array_object, storage); 2469 JSArray::SetContent(array_object, storage);
2470 array_object->set_length(Smi::FromInt(num_exports)); 2470 array_object->set_length(Smi::FromInt(num_exports));
2471 2471
2472 Handle<JSFunction> object_function = 2472 Handle<JSFunction> object_function =
2473 Handle<JSFunction>(isolate->native_context()->object_function(), isolate); 2473 Handle<JSFunction>(isolate->native_context()->object_function(), isolate);
2474 2474
2475 // Populate the result array. 2475 // Populate the result array.
2476 for (int index = 0; index < num_exports; ++index) { 2476 for (int index = 0; index < num_exports; ++index) {
2477 WasmExport& exp = module->export_table[index]; 2477 WasmExport& exp = module->export_table[index];
2478 2478
2479 Handle<JSObject> entry = factory->NewJSObject(object_function, TENURED);
2480
2481 Handle<String> export_kind; 2479 Handle<String> export_kind;
2482 switch (exp.kind) { 2480 switch (exp.kind) {
2483 case kExternalFunction: 2481 case kExternalFunction:
2484 export_kind = function_string; 2482 export_kind = function_string;
2485 break; 2483 break;
2486 case kExternalTable: 2484 case kExternalTable:
2487 export_kind = table_string; 2485 export_kind = table_string;
2488 break; 2486 break;
2489 case kExternalMemory: 2487 case kExternalMemory:
2490 export_kind = memory_string; 2488 export_kind = memory_string;
2491 break; 2489 break;
2492 case kExternalGlobal: 2490 case kExternalGlobal:
2493 export_kind = global_string; 2491 export_kind = global_string;
2494 break; 2492 break;
2495 default: 2493 default:
2496 UNREACHABLE(); 2494 UNREACHABLE();
2497 } 2495 }
2498 2496
2497 Handle<JSObject> entry = factory->NewJSObject(object_function);
2498
2499 MaybeHandle<String> export_name = 2499 MaybeHandle<String> export_name =
2500 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( 2500 WasmCompiledModule::ExtractUtf8StringFromModuleBytes(
2501 isolate, compiled_module, exp.name_offset, exp.name_length); 2501 isolate, compiled_module, exp.name_offset, exp.name_length);
2502 2502
2503 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(), 2503 JSObject::AddProperty(entry, name_string, export_name.ToHandleChecked(),
2504 NONE); 2504 NONE);
2505 JSObject::AddProperty(entry, kind_string, export_kind, NONE); 2505 JSObject::AddProperty(entry, kind_string, export_kind, NONE);
2506 2506
2507 storage->set(index, *entry); 2507 storage->set(index, *entry);
2508 } 2508 }
2509 2509
2510 return array_object; 2510 return array_object;
2511 } 2511 }
2512
2513 Handle<JSArray> wasm::GetCustomSections(
2514 Isolate* isolate, Handle<WasmModuleObject> module_object) {
2515 Handle<WasmCompiledModule> compiled_module(module_object->compiled_module(),
2516 isolate);
2517 Factory* factory = isolate->factory();
2518
2519 Handle<String> name_string = factory->InternalizeUtf8String("name");
2520 Handle<String> data_string = factory->InternalizeUtf8String("data");
2521
2522 std::vector<CustomSectionOffset> custom_sections;
2523 {
2524 DisallowHeapAllocation no_gc; // for raw access to string bytes.
2525 Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(),
2526 isolate);
2527 const byte* start =
2528 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress());
2529 const byte* end = start + module_bytes->length();
2530 custom_sections = DecodeCustomSections(start, end);
2531 }
2532
2533 // Create the result array.
2534 int num_custom_sections = static_cast<int>(custom_sections.size());
2535 Handle<JSArray> array_object = factory->NewJSArray(FAST_ELEMENTS, 0, 0);
2536 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections);
2537 JSArray::SetContent(array_object, storage);
2538 array_object->set_length(Smi::FromInt(num_custom_sections));
2539
2540 Handle<JSFunction> object_function =
2541 Handle<JSFunction>(isolate->native_context()->object_function(), isolate);
2542
2543 // Populate the result array.
2544 for (int index = 0; index < num_custom_sections; ++index) {
2545 auto section = custom_sections[index];
2546
2547 MaybeHandle<String> section_name =
2548 WasmCompiledModule::ExtractUtf8StringFromModuleBytes(
2549 isolate, compiled_module, section.name_offset, section.name_length);
2550
2551 // Make a copy of the payload data in the section.
2552 bool is_external; // Set by TryAllocateBackingStore
2553 void* memory = TryAllocateBackingStore(isolate, section.payload_length,
2554 false, is_external);
2555
2556 Handle<Object> section_data = factory->undefined_value();
2557 if (memory) {
2558 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
2559 JSArrayBuffer::Setup(buffer, isolate, is_external, memory,
2560 static_cast<int>(section.payload_length));
2561 DisallowHeapAllocation no_gc; // for raw access to string bytes.
2562 Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(),
2563 isolate);
2564 const byte* start =
2565 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress());
2566 memcpy(memory, start + section.payload_offset, section.payload_length);
2567 section_data = buffer;
2568 } else {
2569 // TODO(titzer): throw out of memory?
2570 }
2571
2572 Handle<JSObject> entry = factory->NewJSObject(object_function);
2573
2574 JSObject::AddProperty(entry, name_string, section_name.ToHandleChecked(),
2575 NONE);
2576 JSObject::AddProperty(entry, data_string, section_data, NONE);
2577
2578 storage->set(index, *entry);
2579 }
2580
2581 return array_object;
2582 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698