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/wasm/wasm-objects.h" | 5 #include "src/wasm/wasm-objects.h" |
6 #include "src/utils.h" | 6 #include "src/utils.h" |
7 | 7 |
8 #include "src/wasm/module-decoder.h" | 8 #include "src/wasm/module-decoder.h" |
9 #include "src/wasm/wasm-module.h" | 9 #include "src/wasm/wasm-module.h" |
10 #include "src/wasm/wasm-text.h" | 10 #include "src/wasm/wasm-text.h" |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 } | 353 } |
354 | 354 |
355 void WasmCompiledModule::InitId() { | 355 void WasmCompiledModule::InitId() { |
356 #if DEBUG | 356 #if DEBUG |
357 static uint32_t instance_id_counter = 0; | 357 static uint32_t instance_id_counter = 0; |
358 set(kID_instance_id, Smi::FromInt(instance_id_counter++)); | 358 set(kID_instance_id, Smi::FromInt(instance_id_counter++)); |
359 TRACE("New compiled module id: %d\n", instance_id()); | 359 TRACE("New compiled module id: %d\n", instance_id()); |
360 #endif | 360 #endif |
361 } | 361 } |
362 | 362 |
| 363 MaybeHandle<String> WasmCompiledModule::ExtractUtf8StringFromModuleBytes( |
| 364 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, |
| 365 uint32_t offset, uint32_t size) { |
| 366 // TODO(wasm): cache strings from modules if it's a performance win. |
| 367 Handle<SeqOneByteString> module_bytes = compiled_module->module_bytes(); |
| 368 DCHECK_GE(module_bytes->length(), offset); |
| 369 DCHECK_GE(module_bytes->length() - offset, size); |
| 370 Address raw = module_bytes->GetCharsAddress() + offset; |
| 371 if (!unibrow::Utf8::Validate(reinterpret_cast<const byte*>(raw), size)) |
| 372 return {}; // UTF8 decoding error for name. |
| 373 DCHECK_GE(kMaxInt, offset); |
| 374 DCHECK_GE(kMaxInt, size); |
| 375 return isolate->factory()->NewStringFromUtf8SubString( |
| 376 module_bytes, static_cast<int>(offset), static_cast<int>(size)); |
| 377 } |
| 378 |
363 bool WasmCompiledModule::IsWasmCompiledModule(Object* obj) { | 379 bool WasmCompiledModule::IsWasmCompiledModule(Object* obj) { |
364 if (!obj->IsFixedArray()) return false; | 380 if (!obj->IsFixedArray()) return false; |
365 FixedArray* arr = FixedArray::cast(obj); | 381 FixedArray* arr = FixedArray::cast(obj); |
366 if (arr->length() != PropertyIndices::Count) return false; | 382 if (arr->length() != PropertyIndices::Count) return false; |
367 Isolate* isolate = arr->GetIsolate(); | 383 Isolate* isolate = arr->GetIsolate(); |
368 #define WCM_CHECK_SMALL_NUMBER(TYPE, NAME) \ | 384 #define WCM_CHECK_SMALL_NUMBER(TYPE, NAME) \ |
369 if (!arr->get(kID_##NAME)->IsSmi()) return false; | 385 if (!arr->get(kID_##NAME)->IsSmi()) return false; |
370 #define WCM_CHECK_OBJECT_OR_WEAK(TYPE, NAME) \ | 386 #define WCM_CHECK_OBJECT_OR_WEAK(TYPE, NAME) \ |
371 if (!arr->get(kID_##NAME)->IsUndefined(isolate) && \ | 387 if (!arr->get(kID_##NAME)->IsUndefined(isolate) && \ |
372 !arr->get(kID_##NAME)->Is##TYPE()) \ | 388 !arr->get(kID_##NAME)->Is##TYPE()) \ |
(...skipping 15 matching lines...) Expand all Loading... |
388 PrintF("->%d", current->instance_id()); | 404 PrintF("->%d", current->instance_id()); |
389 if (!current->has_weak_next_instance()) break; | 405 if (!current->has_weak_next_instance()) break; |
390 CHECK(!current->ptr_to_weak_next_instance()->cleared()); | 406 CHECK(!current->ptr_to_weak_next_instance()->cleared()); |
391 current = | 407 current = |
392 WasmCompiledModule::cast(current->ptr_to_weak_next_instance()->value()); | 408 WasmCompiledModule::cast(current->ptr_to_weak_next_instance()->value()); |
393 } | 409 } |
394 PrintF("\n"); | 410 PrintF("\n"); |
395 #endif | 411 #endif |
396 } | 412 } |
397 | 413 |
| 414 void WasmCompiledModule::RecreateModuleWrapper(Isolate* isolate, |
| 415 Handle<FixedArray> array) { |
| 416 Handle<WasmCompiledModule> compiled_module( |
| 417 reinterpret_cast<WasmCompiledModule*>(*array), isolate); |
| 418 |
| 419 WasmModule* module = nullptr; |
| 420 { |
| 421 Handle<SeqOneByteString> module_bytes = compiled_module->module_bytes(); |
| 422 // We parse the module again directly from the module bytes, so |
| 423 // the underlying storage must not be moved meanwhile. |
| 424 DisallowHeapAllocation no_allocation; |
| 425 const byte* start = |
| 426 reinterpret_cast<const byte*>(module_bytes->GetCharsAddress()); |
| 427 const byte* end = start + module_bytes->length(); |
| 428 // TODO(titzer): remember the module origin in the compiled_module |
| 429 // For now, we assume serialized modules did not originate from asm.js. |
| 430 ModuleResult result = |
| 431 DecodeWasmModule(isolate, start, end, false, kWasmOrigin); |
| 432 CHECK(result.ok()); |
| 433 CHECK_NOT_NULL(result.val); |
| 434 module = const_cast<WasmModule*>(result.val); |
| 435 } |
| 436 |
| 437 Handle<WasmModuleWrapper> module_wrapper = |
| 438 WasmModuleWrapper::New(isolate, module); |
| 439 |
| 440 compiled_module->set_module_wrapper(module_wrapper); |
| 441 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); |
| 442 } |
| 443 |
398 uint32_t WasmCompiledModule::mem_size() const { | 444 uint32_t WasmCompiledModule::mem_size() const { |
399 return has_memory() ? memory()->byte_length()->Number() : default_mem_size(); | 445 return has_memory() ? memory()->byte_length()->Number() : default_mem_size(); |
400 } | 446 } |
401 | 447 |
402 uint32_t WasmCompiledModule::default_mem_size() const { | 448 uint32_t WasmCompiledModule::default_mem_size() const { |
403 return min_mem_pages() * WasmModule::kPageSize; | 449 return min_mem_pages() * WasmModule::kPageSize; |
404 } | 450 } |
405 | 451 |
| 452 MaybeHandle<String> WasmCompiledModule::GetFunctionNameOrNull( |
| 453 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, |
| 454 uint32_t func_index) { |
| 455 DCHECK_LT(func_index, compiled_module->module()->functions.size()); |
| 456 WasmFunction& function = compiled_module->module()->functions[func_index]; |
| 457 return WasmCompiledModule::ExtractUtf8StringFromModuleBytes( |
| 458 isolate, compiled_module, function.name_offset, function.name_length); |
| 459 } |
| 460 |
| 461 Handle<String> WasmCompiledModule::GetFunctionName( |
| 462 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, |
| 463 uint32_t func_index) { |
| 464 MaybeHandle<String> name = |
| 465 GetFunctionNameOrNull(isolate, compiled_module, func_index); |
| 466 if (!name.is_null()) return name.ToHandleChecked(); |
| 467 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); |
| 468 } |
| 469 |
406 Vector<const uint8_t> WasmCompiledModule::GetRawFunctionName( | 470 Vector<const uint8_t> WasmCompiledModule::GetRawFunctionName( |
407 uint32_t func_index) { | 471 uint32_t func_index) { |
408 DCHECK_GT(module()->functions.size(), func_index); | 472 DCHECK_GT(module()->functions.size(), func_index); |
409 WasmFunction& function = module()->functions[func_index]; | 473 WasmFunction& function = module()->functions[func_index]; |
410 SeqOneByteString* bytes = ptr_to_module_bytes(); | 474 SeqOneByteString* bytes = ptr_to_module_bytes(); |
411 DCHECK_GE(bytes->length(), function.name_offset); | 475 DCHECK_GE(bytes->length(), function.name_offset); |
412 DCHECK_GE(bytes->length() - function.name_offset, function.name_length); | 476 DCHECK_GE(bytes->length() - function.name_offset, function.name_length); |
413 return Vector<const uint8_t>(bytes->GetCharsAddress() + function.name_offset, | 477 return Vector<const uint8_t>(bytes->GetCharsAddress() + function.name_offset, |
414 function.name_length); | 478 function.name_length); |
415 } | 479 } |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 !array->get(kPreviousInstanceWrapper)->IsFixedArray()) | 675 !array->get(kPreviousInstanceWrapper)->IsFixedArray()) |
612 return false; | 676 return false; |
613 return true; | 677 return true; |
614 } | 678 } |
615 | 679 |
616 void WasmInstanceWrapper::set_instance_object(Handle<JSObject> instance, | 680 void WasmInstanceWrapper::set_instance_object(Handle<JSObject> instance, |
617 Isolate* isolate) { | 681 Isolate* isolate) { |
618 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(instance); | 682 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(instance); |
619 set(kWrapperInstanceObject, *cell); | 683 set(kWrapperInstanceObject, *cell); |
620 } | 684 } |
OLD | NEW |