| 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 | 6 |
| 7 #include "src/wasm/module-decoder.h" | 7 #include "src/wasm/module-decoder.h" |
| 8 #include "src/wasm/wasm-module.h" | 8 #include "src/wasm/wasm-module.h" |
| 9 | 9 |
| 10 #define TRACE(...) \ | 10 #define TRACE(...) \ |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 static uint32_t SafeUint32(Object* value) { | 45 static uint32_t SafeUint32(Object* value) { |
| 46 if (value->IsSmi()) { | 46 if (value->IsSmi()) { |
| 47 int32_t val = Smi::cast(value)->value(); | 47 int32_t val = Smi::cast(value)->value(); |
| 48 CHECK_GE(val, 0); | 48 CHECK_GE(val, 0); |
| 49 return static_cast<uint32_t>(val); | 49 return static_cast<uint32_t>(val); |
| 50 } | 50 } |
| 51 DCHECK(value->IsHeapNumber()); | 51 DCHECK(value->IsHeapNumber()); |
| 52 HeapNumber* num = HeapNumber::cast(value); | 52 HeapNumber* num = HeapNumber::cast(value); |
| 53 CHECK_GE(num->value(), 0.0); | 53 CHECK_GE(num->value(), 0.0); |
| 54 CHECK_LE(num->value(), static_cast<double>(kMaxUInt32)); | 54 CHECK_LE(num->value(), kMaxUInt32); |
| 55 return static_cast<uint32_t>(num->value()); | 55 return static_cast<uint32_t>(num->value()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 static int32_t SafeInt32(Object* value) { | 58 static int32_t SafeInt32(Object* value) { |
| 59 if (value->IsSmi()) { | 59 if (value->IsSmi()) { |
| 60 return Smi::cast(value)->value(); | 60 return Smi::cast(value)->value(); |
| 61 } | 61 } |
| 62 DCHECK(value->IsHeapNumber()); | 62 DCHECK(value->IsHeapNumber()); |
| 63 HeapNumber* num = HeapNumber::cast(value); | 63 HeapNumber* num = HeapNumber::cast(value); |
| 64 CHECK_GE(num->value(), static_cast<double>(Smi::kMinValue)); | 64 CHECK_GE(num->value(), Smi::kMinValue); |
| 65 CHECK_LE(num->value(), static_cast<double>(Smi::kMaxValue)); | 65 CHECK_LE(num->value(), Smi::kMaxValue); |
| 66 return static_cast<int32_t>(num->value()); | 66 return static_cast<int32_t>(num->value()); |
| 67 } | 67 } |
| 68 | 68 |
| 69 Handle<WasmModuleObject> WasmModuleObject::New( | 69 Handle<WasmModuleObject> WasmModuleObject::New( |
| 70 Isolate* isolate, Handle<WasmCompiledModule> compiled_module) { | 70 Isolate* isolate, Handle<WasmCompiledModule> compiled_module) { |
| 71 ModuleOrigin origin = compiled_module->module()->origin; | 71 ModuleOrigin origin = compiled_module->module()->origin; |
| 72 | 72 |
| 73 Handle<JSObject> module_object; | 73 Handle<JSObject> module_object; |
| 74 if (origin == ModuleOrigin::kWasmOrigin) { | 74 if (origin == ModuleOrigin::kWasmOrigin) { |
| 75 Handle<JSFunction> module_cons( | 75 Handle<JSFunction> module_cons( |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 387 |
| 388 uint32_t WasmCompiledModule::default_mem_size() const { | 388 uint32_t WasmCompiledModule::default_mem_size() const { |
| 389 return min_mem_pages() * WasmModule::kPageSize; | 389 return min_mem_pages() * WasmModule::kPageSize; |
| 390 } | 390 } |
| 391 | 391 |
| 392 Vector<const uint8_t> WasmCompiledModule::GetRawFunctionName( | 392 Vector<const uint8_t> WasmCompiledModule::GetRawFunctionName( |
| 393 uint32_t func_index) { | 393 uint32_t func_index) { |
| 394 DCHECK_GT(module()->functions.size(), func_index); | 394 DCHECK_GT(module()->functions.size(), func_index); |
| 395 WasmFunction& function = module()->functions[func_index]; | 395 WasmFunction& function = module()->functions[func_index]; |
| 396 SeqOneByteString* bytes = ptr_to_module_bytes(); | 396 SeqOneByteString* bytes = ptr_to_module_bytes(); |
| 397 DCHECK_GE(static_cast<size_t>(bytes->length()), function.name_offset); | 397 DCHECK_GE(bytes->length(), function.name_offset); |
| 398 DCHECK_GE(static_cast<size_t>(bytes->length() - function.name_offset), | 398 DCHECK_GE(bytes->length() - function.name_offset, function.name_length); |
| 399 function.name_length); | |
| 400 return Vector<const uint8_t>(bytes->GetCharsAddress() + function.name_offset, | 399 return Vector<const uint8_t>(bytes->GetCharsAddress() + function.name_offset, |
| 401 function.name_length); | 400 function.name_length); |
| 402 } | 401 } |
| 403 | 402 |
| 404 int WasmCompiledModule::GetFunctionOffset(uint32_t func_index) const { | 403 int WasmCompiledModule::GetFunctionOffset(uint32_t func_index) const { |
| 405 std::vector<WasmFunction>& functions = module()->functions; | 404 std::vector<WasmFunction>& functions = module()->functions; |
| 406 if (static_cast<uint32_t>(func_index) >= functions.size()) return -1; | 405 if (static_cast<uint32_t>(func_index) >= functions.size()) return -1; |
| 407 DCHECK_GE(static_cast<uint32_t>(kMaxInt), | 406 DCHECK_GE(kMaxInt, functions[func_index].code_start_offset); |
| 408 functions[func_index].code_start_offset); | |
| 409 return static_cast<int>(functions[func_index].code_start_offset); | 407 return static_cast<int>(functions[func_index].code_start_offset); |
| 410 } | 408 } |
| 411 | 409 |
| 412 int WasmCompiledModule::GetContainingFunction(uint32_t byte_offset) const { | 410 int WasmCompiledModule::GetContainingFunction(uint32_t byte_offset) const { |
| 413 std::vector<WasmFunction>& functions = module()->functions; | 411 std::vector<WasmFunction>& functions = module()->functions; |
| 414 | 412 |
| 415 // Binary search for a function containing the given position. | 413 // Binary search for a function containing the given position. |
| 416 int left = 0; // inclusive | 414 int left = 0; // inclusive |
| 417 int right = static_cast<int>(functions.size()); // exclusive | 415 int right = static_cast<int>(functions.size()); // exclusive |
| 418 if (right == 0) return false; | 416 if (right == 0) return false; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 461 |
| 464 AsmJsOffsetsResult asm_offsets; | 462 AsmJsOffsetsResult asm_offsets; |
| 465 { | 463 { |
| 466 DisallowHeapAllocation no_gc; | 464 DisallowHeapAllocation no_gc; |
| 467 const byte* bytes_start = offset_table->GetDataStartAddress(); | 465 const byte* bytes_start = offset_table->GetDataStartAddress(); |
| 468 const byte* bytes_end = bytes_start + offset_table->length() - 1; | 466 const byte* bytes_end = bytes_start + offset_table->length() - 1; |
| 469 asm_offsets = wasm::DecodeAsmJsOffsets(bytes_start, bytes_end); | 467 asm_offsets = wasm::DecodeAsmJsOffsets(bytes_start, bytes_end); |
| 470 } | 468 } |
| 471 // Wasm bytes must be valid and must contain asm.js offset table. | 469 // Wasm bytes must be valid and must contain asm.js offset table. |
| 472 DCHECK(asm_offsets.ok()); | 470 DCHECK(asm_offsets.ok()); |
| 473 DCHECK_GE(static_cast<size_t>(kMaxInt), asm_offsets.val.size()); | 471 DCHECK_GE(kMaxInt, asm_offsets.val.size()); |
| 474 int num_functions = static_cast<int>(asm_offsets.val.size()); | 472 int num_functions = static_cast<int>(asm_offsets.val.size()); |
| 475 int num_imported_functions = | 473 int num_imported_functions = |
| 476 static_cast<int>(compiled_module->module()->num_imported_functions); | 474 static_cast<int>(compiled_module->module()->num_imported_functions); |
| 477 DCHECK_EQ(compiled_module->module()->functions.size(), | 475 DCHECK_EQ(compiled_module->module()->functions.size(), |
| 478 static_cast<size_t>(num_functions) + num_imported_functions); | 476 static_cast<size_t>(num_functions) + num_imported_functions); |
| 479 // One byte to encode that this is a decoded table. | 477 // One byte to encode that this is a decoded table. |
| 480 int total_size = 1; | 478 int total_size = 1; |
| 481 for (int func = 0; func < num_functions; ++func) { | 479 for (int func = 0; func < num_functions; ++func) { |
| 482 size_t new_size = asm_offsets.val[func].size() * 2 * kIntSize; | 480 size_t new_size = asm_offsets.val[func].size() * 2 * kIntSize; |
| 483 DCHECK_LE(new_size, static_cast<size_t>(kMaxInt) - total_size); | 481 DCHECK_LE(new_size, static_cast<size_t>(kMaxInt) - total_size); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 !array->get(kPreviousInstanceWrapper)->IsFixedArray()) | 562 !array->get(kPreviousInstanceWrapper)->IsFixedArray()) |
| 565 return false; | 563 return false; |
| 566 return true; | 564 return true; |
| 567 } | 565 } |
| 568 | 566 |
| 569 void WasmInstanceWrapper::set_instance_object(Handle<JSObject> instance, | 567 void WasmInstanceWrapper::set_instance_object(Handle<JSObject> instance, |
| 570 Isolate* isolate) { | 568 Isolate* isolate) { |
| 571 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(instance); | 569 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(instance); |
| 572 set(kWrapperInstanceObject, *cell); | 570 set(kWrapperInstanceObject, *cell); |
| 573 } | 571 } |
| OLD | NEW |