| 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/base/atomic-utils.h" | 7 #include "src/base/atomic-utils.h" |
| 8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
| 9 | 9 |
| 10 #include "src/macro-assembler.h" | 10 #include "src/macro-assembler.h" |
| (...skipping 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 os << "+" << pair.function_->func_index; | 818 os << "+" << pair.function_->func_index; |
| 819 } | 819 } |
| 820 } else { | 820 } else { |
| 821 os << "?"; | 821 os << "?"; |
| 822 } | 822 } |
| 823 return os; | 823 return os; |
| 824 } | 824 } |
| 825 | 825 |
| 826 Handle<JSFunction> wasm::WrapExportCodeAsJSFunction( | 826 Handle<JSFunction> wasm::WrapExportCodeAsJSFunction( |
| 827 Isolate* isolate, Handle<Code> export_code, Handle<String> name, int arity, | 827 Isolate* isolate, Handle<Code> export_code, Handle<String> name, int arity, |
| 828 MaybeHandle<ByteArray> maybe_signature, Handle<JSObject> module_instance) { | 828 MaybeHandle<ByteArray> maybe_signature, Handle<JSObject> instance) { |
| 829 Handle<SharedFunctionInfo> shared = | 829 Handle<SharedFunctionInfo> shared = |
| 830 isolate->factory()->NewSharedFunctionInfo(name, export_code, false); | 830 isolate->factory()->NewSharedFunctionInfo(name, export_code, false); |
| 831 shared->set_length(arity); | 831 shared->set_length(arity); |
| 832 shared->set_internal_formal_parameter_count(arity); | 832 shared->set_internal_formal_parameter_count(arity); |
| 833 Handle<JSFunction> function = isolate->factory()->NewFunction( | 833 Handle<JSFunction> function = isolate->factory()->NewFunction( |
| 834 isolate->wasm_function_map(), name, export_code); | 834 isolate->wasm_function_map(), name, export_code); |
| 835 function->set_shared(*shared); | 835 function->set_shared(*shared); |
| 836 | 836 |
| 837 function->SetInternalField(kInternalModuleInstance, *module_instance); | 837 function->SetInternalField(kInternalModuleInstance, *instance); |
| 838 // add another Internal Field as the function arity | 838 // add another Internal Field as the function arity |
| 839 function->SetInternalField(kInternalArity, Smi::FromInt(arity)); | 839 function->SetInternalField(kInternalArity, Smi::FromInt(arity)); |
| 840 // add another Internal Field as the signature of the foreign function | 840 // add another Internal Field as the signature of the foreign function |
| 841 Handle<ByteArray> signature; | 841 Handle<ByteArray> signature; |
| 842 if (maybe_signature.ToHandle(&signature)) { | 842 if (maybe_signature.ToHandle(&signature)) { |
| 843 function->SetInternalField(kInternalSignature, *signature); | 843 function->SetInternalField(kInternalSignature, *signature); |
| 844 } | 844 } |
| 845 return function; | 845 return function; |
| 846 } | 846 } |
| 847 | 847 |
| 848 Object* wasm::GetOwningWasmInstance(Code* code) { | 848 Object* wasm::GetOwningWasmInstance(Code* code) { |
| 849 DCHECK(code->kind() == Code::WASM_FUNCTION); | 849 DCHECK(code->kind() == Code::WASM_FUNCTION); |
| 850 DisallowHeapAllocation no_gc; | 850 DisallowHeapAllocation no_gc; |
| 851 FixedArray* deopt_data = code->deoptimization_data(); | 851 FixedArray* deopt_data = code->deoptimization_data(); |
| 852 DCHECK_NOT_NULL(deopt_data); | 852 DCHECK_NOT_NULL(deopt_data); |
| 853 DCHECK(deopt_data->length() == 2); | 853 DCHECK(deopt_data->length() == 2); |
| 854 Object* weak_link = deopt_data->get(0); | 854 Object* weak_link = deopt_data->get(0); |
| 855 if (!weak_link->IsWeakCell()) return nullptr; | 855 if (!weak_link->IsWeakCell()) return nullptr; |
| 856 WeakCell* cell = WeakCell::cast(weak_link); | 856 WeakCell* cell = WeakCell::cast(weak_link); |
| 857 return cell->value(); | 857 return cell->value(); |
| 858 } | 858 } |
| 859 | 859 |
| 860 int wasm::GetNumImportedFunctions(Handle<JSObject> wasm_object) { | 860 int wasm::GetNumImportedFunctions(Handle<JSObject> instance) { |
| 861 // TODO(wasm): Cache this number if it ever becomes a performance problem. | 861 // TODO(wasm): Cache this number if it ever becomes a performance problem. |
| 862 DCHECK(IsWasmObject(*wasm_object)); | 862 DCHECK(IsWasmInstance(*instance)); |
| 863 WasmCompiledModule* compiled_module = GetCompiledModule(*wasm_object); | 863 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
| 864 Handle<FixedArray> imports = | 864 Handle<FixedArray> imports = |
| 865 WasmCompiledModule::cast(compiled_module)->imports(); | 865 WasmCompiledModule::cast(compiled_module)->imports(); |
| 866 int num_imports = imports->length(); | 866 int num_imports = imports->length(); |
| 867 int num_imported_functions = 0; | 867 int num_imported_functions = 0; |
| 868 for (int i = 0; i < num_imports; ++i) { | 868 for (int i = 0; i < num_imports; ++i) { |
| 869 FixedArray* encoded_import = FixedArray::cast(imports->get(i)); | 869 FixedArray* encoded_import = FixedArray::cast(imports->get(i)); |
| 870 int kind = Smi::cast(encoded_import->get(kImportKind))->value(); | 870 int kind = Smi::cast(encoded_import->get(kImportKind))->value(); |
| 871 if (kind == kExternalFunction) ++num_imported_functions; | 871 if (kind == kExternalFunction) ++num_imported_functions; |
| 872 } | 872 } |
| 873 return num_imported_functions; | 873 return num_imported_functions; |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1418 } | 1418 } |
| 1419 module_object_->SetInternalField(0, *compiled_module_); | 1419 module_object_->SetInternalField(0, *compiled_module_); |
| 1420 instance->SetInternalField(kWasmCompiledModule, *compiled_module_); | 1420 instance->SetInternalField(kWasmCompiledModule, *compiled_module_); |
| 1421 compiled_module_->set_weak_owning_instance(link_to_owning_instance); | 1421 compiled_module_->set_weak_owning_instance(link_to_owning_instance); |
| 1422 GlobalHandles::MakeWeak(global_handle.location(), | 1422 GlobalHandles::MakeWeak(global_handle.location(), |
| 1423 global_handle.location(), &InstanceFinalizer, | 1423 global_handle.location(), &InstanceFinalizer, |
| 1424 v8::WeakCallbackType::kFinalizer); | 1424 v8::WeakCallbackType::kFinalizer); |
| 1425 } | 1425 } |
| 1426 } | 1426 } |
| 1427 | 1427 |
| 1428 DCHECK(wasm::IsWasmObject(*instance)); | 1428 DCHECK(wasm::IsWasmInstance(*instance)); |
| 1429 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), | 1429 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), |
| 1430 isolate_); | 1430 isolate_); |
| 1431 WasmJs::SetWasmMemoryInstance(isolate_, memory_object, instance); | 1431 WasmJs::SetWasmMemoryInstance(isolate_, memory_object, instance); |
| 1432 | 1432 |
| 1433 //-------------------------------------------------------------------------- | 1433 //-------------------------------------------------------------------------- |
| 1434 // Run the start function if one was specified. | 1434 // Run the start function if one was specified. |
| 1435 //-------------------------------------------------------------------------- | 1435 //-------------------------------------------------------------------------- |
| 1436 if (compiled_module_->has_startup_function()) { | 1436 if (compiled_module_->has_startup_function()) { |
| 1437 Handle<FixedArray> startup_data = compiled_module_->startup_function(); | 1437 Handle<FixedArray> startup_data = compiled_module_->startup_function(); |
| 1438 HandleScope scope(isolate_); | 1438 HandleScope scope(isolate_); |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1968 WasmCompiledModule::cast(current->ptr_to_weak_next_instance()->value()); | 1968 WasmCompiledModule::cast(current->ptr_to_weak_next_instance()->value()); |
| 1969 } | 1969 } |
| 1970 PrintF("\n"); | 1970 PrintF("\n"); |
| 1971 #endif | 1971 #endif |
| 1972 } | 1972 } |
| 1973 | 1973 |
| 1974 Handle<Object> wasm::GetWasmFunctionNameOrNull(Isolate* isolate, | 1974 Handle<Object> wasm::GetWasmFunctionNameOrNull(Isolate* isolate, |
| 1975 Handle<Object> wasm, | 1975 Handle<Object> wasm, |
| 1976 uint32_t func_index) { | 1976 uint32_t func_index) { |
| 1977 if (!wasm->IsUndefined(isolate)) { | 1977 if (!wasm->IsUndefined(isolate)) { |
| 1978 DCHECK(IsWasmObject(*wasm)); | 1978 DCHECK(IsWasmInstance(*wasm)); |
| 1979 WasmCompiledModule* compiled_module = | 1979 WasmCompiledModule* compiled_module = |
| 1980 GetCompiledModule(JSObject::cast(*wasm)); | 1980 GetCompiledModule(JSObject::cast(*wasm)); |
| 1981 Handle<ByteArray> func_names = compiled_module->function_names(); | 1981 Handle<ByteArray> func_names = compiled_module->function_names(); |
| 1982 // TODO(clemens): Extract this from the module bytes; skip whole function | 1982 // TODO(clemens): Extract this from the module bytes; skip whole function |
| 1983 // name table. | 1983 // name table. |
| 1984 Handle<Object> name; | 1984 Handle<Object> name; |
| 1985 if (GetWasmFunctionNameFromTable(func_names, func_index).ToHandle(&name)) { | 1985 if (GetWasmFunctionNameFromTable(func_names, func_index).ToHandle(&name)) { |
| 1986 return name; | 1986 return name; |
| 1987 } | 1987 } |
| 1988 } | 1988 } |
| 1989 return isolate->factory()->null_value(); | 1989 return isolate->factory()->null_value(); |
| 1990 } | 1990 } |
| 1991 | 1991 |
| 1992 Handle<String> wasm::GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, | 1992 Handle<String> wasm::GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, |
| 1993 uint32_t func_index) { | 1993 uint32_t func_index) { |
| 1994 Handle<Object> name_or_null = | 1994 Handle<Object> name_or_null = |
| 1995 GetWasmFunctionNameOrNull(isolate, wasm, func_index); | 1995 GetWasmFunctionNameOrNull(isolate, wasm, func_index); |
| 1996 if (!name_or_null->IsNull(isolate)) { | 1996 if (!name_or_null->IsNull(isolate)) { |
| 1997 return Handle<String>::cast(name_or_null); | 1997 return Handle<String>::cast(name_or_null); |
| 1998 } | 1998 } |
| 1999 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); | 1999 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); |
| 2000 } | 2000 } |
| 2001 | 2001 |
| 2002 bool wasm::IsWasmObject(Object* object) { | 2002 bool wasm::IsWasmInstance(Object* object) { |
| 2003 if (!object->IsJSObject()) return false; | 2003 if (!object->IsJSObject()) return false; |
| 2004 | 2004 |
| 2005 JSObject* obj = JSObject::cast(object); | 2005 JSObject* obj = JSObject::cast(object); |
| 2006 Isolate* isolate = obj->GetIsolate(); | 2006 Isolate* isolate = obj->GetIsolate(); |
| 2007 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount) { | 2007 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount) { |
| 2008 return false; | 2008 return false; |
| 2009 } | 2009 } |
| 2010 | 2010 |
| 2011 Object* mem = obj->GetInternalField(kWasmMemArrayBuffer); | 2011 Object* mem = obj->GetInternalField(kWasmMemArrayBuffer); |
| 2012 if (!obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() || | 2012 if (!obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() || |
| 2013 !(mem->IsUndefined(isolate) || mem->IsJSArrayBuffer()) || | 2013 !(mem->IsUndefined(isolate) || mem->IsJSArrayBuffer()) || |
| 2014 !WasmCompiledModule::IsWasmCompiledModule( | 2014 !WasmCompiledModule::IsWasmCompiledModule( |
| 2015 obj->GetInternalField(kWasmCompiledModule))) { | 2015 obj->GetInternalField(kWasmCompiledModule))) { |
| 2016 return false; | 2016 return false; |
| 2017 } | 2017 } |
| 2018 | 2018 |
| 2019 // All checks passed. | 2019 // All checks passed. |
| 2020 return true; | 2020 return true; |
| 2021 } | 2021 } |
| 2022 | 2022 |
| 2023 WasmCompiledModule* wasm::GetCompiledModule(JSObject* wasm) { | 2023 WasmCompiledModule* wasm::GetCompiledModule(JSObject* wasm) { |
| 2024 return WasmCompiledModule::cast(wasm->GetInternalField(kWasmCompiledModule)); | 2024 return WasmCompiledModule::cast(wasm->GetInternalField(kWasmCompiledModule)); |
| 2025 } | 2025 } |
| 2026 | 2026 |
| 2027 bool wasm::WasmIsAsmJs(Object* wasm, Isolate* isolate) { | 2027 bool wasm::WasmIsAsmJs(Object* wasm, Isolate* isolate) { |
| 2028 if (wasm->IsUndefined(isolate)) return false; | 2028 if (wasm->IsUndefined(isolate)) return false; |
| 2029 DCHECK(IsWasmObject(wasm)); | 2029 DCHECK(IsWasmInstance(wasm)); |
| 2030 WasmCompiledModule* compiled_module = GetCompiledModule(JSObject::cast(wasm)); | 2030 WasmCompiledModule* compiled_module = GetCompiledModule(JSObject::cast(wasm)); |
| 2031 return compiled_module->has_asm_js_script(); | 2031 return compiled_module->has_asm_js_script(); |
| 2032 } | 2032 } |
| 2033 | 2033 |
| 2034 Handle<Script> wasm::GetAsmWasmScript(Handle<JSObject> wasm) { | 2034 Handle<Script> wasm::GetAsmWasmScript(Handle<JSObject> wasm) { |
| 2035 DCHECK(IsWasmObject(*wasm)); | 2035 DCHECK(IsWasmInstance(*wasm)); |
| 2036 WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); | 2036 WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); |
| 2037 return compiled_module->asm_js_script(); | 2037 return compiled_module->asm_js_script(); |
| 2038 } | 2038 } |
| 2039 | 2039 |
| 2040 int wasm::GetAsmWasmSourcePosition(Handle<JSObject> wasm, int func_index, | 2040 int wasm::GetAsmWasmSourcePosition(Handle<JSObject> wasm, int func_index, |
| 2041 int byte_offset) { | 2041 int byte_offset) { |
| 2042 return WasmDebugInfo::GetAsmJsSourcePosition(GetDebugInfo(wasm), func_index, | 2042 return WasmDebugInfo::GetAsmJsSourcePosition(GetDebugInfo(wasm), func_index, |
| 2043 byte_offset); | 2043 byte_offset); |
| 2044 } | 2044 } |
| 2045 | 2045 |
| 2046 Handle<SeqOneByteString> wasm::GetWasmBytes(Handle<JSObject> wasm) { | 2046 Handle<SeqOneByteString> wasm::GetWasmBytes(Handle<JSObject> instance) { |
| 2047 DCHECK(IsWasmObject(*wasm)); | 2047 DCHECK(IsWasmInstance(*instance)); |
| 2048 WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); | 2048 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
| 2049 return compiled_module->module_bytes(); | 2049 return compiled_module->module_bytes(); |
| 2050 } | 2050 } |
| 2051 | 2051 |
| 2052 Handle<WasmDebugInfo> wasm::GetDebugInfo(Handle<JSObject> wasm) { | 2052 Handle<WasmDebugInfo> wasm::GetDebugInfo(Handle<JSObject> instance) { |
| 2053 Handle<Object> info(wasm->GetInternalField(kWasmDebugInfo), | 2053 Handle<Object> info(instance->GetInternalField(kWasmDebugInfo), |
| 2054 wasm->GetIsolate()); | 2054 instance->GetIsolate()); |
| 2055 if (!info->IsUndefined(wasm->GetIsolate())) | 2055 if (!info->IsUndefined(instance->GetIsolate())) |
| 2056 return Handle<WasmDebugInfo>::cast(info); | 2056 return Handle<WasmDebugInfo>::cast(info); |
| 2057 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(wasm); | 2057 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(instance); |
| 2058 wasm->SetInternalField(kWasmDebugInfo, *new_info); | 2058 instance->SetInternalField(kWasmDebugInfo, *new_info); |
| 2059 return new_info; | 2059 return new_info; |
| 2060 } | 2060 } |
| 2061 | 2061 |
| 2062 Handle<FixedArray> wasm::BuildFunctionTable(Isolate* isolate, uint32_t index, | 2062 Handle<FixedArray> wasm::BuildFunctionTable(Isolate* isolate, uint32_t index, |
| 2063 const WasmModule* module) { | 2063 const WasmModule* module) { |
| 2064 const WasmIndirectFunctionTable* table = &module->function_tables[index]; | 2064 const WasmIndirectFunctionTable* table = &module->function_tables[index]; |
| 2065 DCHECK_EQ(table->size, table->values.size()); | 2065 DCHECK_EQ(table->size, table->values.size()); |
| 2066 DCHECK_GE(table->max_size, table->size); | 2066 DCHECK_GE(table->max_size, table->size); |
| 2067 Handle<FixedArray> values = | 2067 Handle<FixedArray> values = |
| 2068 isolate->factory()->NewFixedArray(2 * table->max_size, TENURED); | 2068 isolate->factory()->NewFixedArray(2 * table->max_size, TENURED); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2088 const std::vector<Handle<Code>>* code_table) { | 2088 const std::vector<Handle<Code>>* code_table) { |
| 2089 uint32_t max_size = table->length() / 2; | 2089 uint32_t max_size = table->length() / 2; |
| 2090 for (uint32_t i = max_size; i < max_size + table_size; ++i) { | 2090 for (uint32_t i = max_size; i < max_size + table_size; ++i) { |
| 2091 int index = Smi::cast(table->get(static_cast<int>(i)))->value(); | 2091 int index = Smi::cast(table->get(static_cast<int>(i)))->value(); |
| 2092 DCHECK_GE(index, 0); | 2092 DCHECK_GE(index, 0); |
| 2093 DCHECK_LT(static_cast<size_t>(index), code_table->size()); | 2093 DCHECK_LT(static_cast<size_t>(index), code_table->size()); |
| 2094 table->set(static_cast<int>(i), *(*code_table)[index]); | 2094 table->set(static_cast<int>(i), *(*code_table)[index]); |
| 2095 } | 2095 } |
| 2096 } | 2096 } |
| 2097 | 2097 |
| 2098 int wasm::GetNumberOfFunctions(Handle<JSObject> wasm) { | 2098 int wasm::GetNumberOfFunctions(Handle<JSObject> instance) { |
| 2099 DCHECK(IsWasmObject(*wasm)); | 2099 DCHECK(IsWasmInstance(*instance)); |
| 2100 WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); | 2100 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
| 2101 ByteArray* func_names_arr = compiled_module->ptr_to_function_names(); | 2101 ByteArray* func_names_arr = compiled_module->ptr_to_function_names(); |
| 2102 // TODO(clemensh): this looks inside an array constructed elsewhere. Refactor. | 2102 // TODO(clemensh): this looks inside an array constructed elsewhere. Refactor. |
| 2103 return func_names_arr->get_int(0); | 2103 return func_names_arr->get_int(0); |
| 2104 } | 2104 } |
| 2105 | 2105 |
| 2106 Handle<JSObject> wasm::CreateCompiledModuleObject( | 2106 Handle<JSObject> wasm::CreateCompiledModuleObject( |
| 2107 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, | 2107 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, |
| 2108 ModuleOrigin origin) { | 2108 ModuleOrigin origin) { |
| 2109 Handle<JSObject> module_obj; | 2109 Handle<JSObject> module_obj; |
| 2110 if (origin == ModuleOrigin::kWasmOrigin) { | 2110 if (origin == ModuleOrigin::kWasmOrigin) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2176 DCHECK_NOT_NULL(result.val); | 2176 DCHECK_NOT_NULL(result.val); |
| 2177 delete result.val; | 2177 delete result.val; |
| 2178 return true; | 2178 return true; |
| 2179 } | 2179 } |
| 2180 return false; | 2180 return false; |
| 2181 } | 2181 } |
| 2182 | 2182 |
| 2183 MaybeHandle<JSArrayBuffer> wasm::GetInstanceMemory(Isolate* isolate, | 2183 MaybeHandle<JSArrayBuffer> wasm::GetInstanceMemory(Isolate* isolate, |
| 2184 Handle<JSObject> instance) { | 2184 Handle<JSObject> instance) { |
| 2185 Object* mem = instance->GetInternalField(kWasmMemArrayBuffer); | 2185 Object* mem = instance->GetInternalField(kWasmMemArrayBuffer); |
| 2186 DCHECK(IsWasmObject(*instance)); | 2186 DCHECK(IsWasmInstance(*instance)); |
| 2187 if (mem->IsUndefined(isolate)) return MaybeHandle<JSArrayBuffer>(); | 2187 if (mem->IsUndefined(isolate)) return MaybeHandle<JSArrayBuffer>(); |
| 2188 return Handle<JSArrayBuffer>(JSArrayBuffer::cast(mem)); | 2188 return Handle<JSArrayBuffer>(JSArrayBuffer::cast(mem)); |
| 2189 } | 2189 } |
| 2190 | 2190 |
| 2191 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) { | 2191 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) { |
| 2192 DisallowHeapAllocation no_gc; | 2192 DisallowHeapAllocation no_gc; |
| 2193 DCHECK(IsWasmObject(*instance)); | 2193 DCHECK(IsWasmInstance(*instance)); |
| 2194 instance->SetInternalField(kWasmMemArrayBuffer, buffer); | 2194 instance->SetInternalField(kWasmMemArrayBuffer, buffer); |
| 2195 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); | 2195 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
| 2196 compiled_module->set_ptr_to_heap(buffer); | 2196 compiled_module->set_ptr_to_heap(buffer); |
| 2197 } | 2197 } |
| 2198 | 2198 |
| 2199 int32_t wasm::GetInstanceMemorySize(Isolate* isolate, | 2199 int32_t wasm::GetInstanceMemorySize(Isolate* isolate, |
| 2200 Handle<JSObject> instance) { | 2200 Handle<JSObject> instance) { |
| 2201 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2201 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
| 2202 GetInstanceMemory(isolate, instance); | 2202 GetInstanceMemory(isolate, instance); |
| 2203 Handle<JSArrayBuffer> buffer; | 2203 Handle<JSArrayBuffer> buffer; |
| 2204 if (!maybe_mem_buffer.ToHandle(&buffer)) { | 2204 if (!maybe_mem_buffer.ToHandle(&buffer)) { |
| 2205 return 0; | 2205 return 0; |
| 2206 } else { | 2206 } else { |
| 2207 return buffer->byte_length()->Number() / WasmModule::kPageSize; | 2207 return buffer->byte_length()->Number() / WasmModule::kPageSize; |
| 2208 } | 2208 } |
| 2209 } | 2209 } |
| 2210 | 2210 |
| 2211 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { | 2211 uint32_t GetMaxInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { |
| 2212 uint32_t max_pages = WasmModule::kMaxMemPages; | 2212 uint32_t max_pages = WasmModule::kMaxMemPages; |
| 2213 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), | 2213 Handle<Object> memory_object(instance->GetInternalField(kWasmMemObject), |
| 2214 isolate); | 2214 isolate); |
| 2215 if (memory_object->IsUndefined(isolate)) return max_pages; | 2215 if (memory_object->IsUndefined(isolate)) return max_pages; |
| 2216 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); | 2216 return WasmJs::GetWasmMemoryMaximumSize(isolate, memory_object); |
| 2217 } | 2217 } |
| 2218 | 2218 |
| 2219 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, | 2219 int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, |
| 2220 uint32_t pages) { | 2220 uint32_t pages) { |
| 2221 if (!IsWasmObject(*instance)) return -1; | 2221 if (!IsWasmInstance(*instance)) return -1; |
| 2222 if (pages == 0) return GetInstanceMemorySize(isolate, instance); | 2222 if (pages == 0) return GetInstanceMemorySize(isolate, instance); |
| 2223 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); | 2223 uint32_t max_pages = GetMaxInstanceMemorySize(isolate, instance); |
| 2224 if (WasmModule::kMaxMemPages < max_pages) return -1; | 2224 if (WasmModule::kMaxMemPages < max_pages) return -1; |
| 2225 | 2225 |
| 2226 Address old_mem_start = nullptr; | 2226 Address old_mem_start = nullptr; |
| 2227 uint32_t old_size = 0, new_size = 0; | 2227 uint32_t old_size = 0, new_size = 0; |
| 2228 | 2228 |
| 2229 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = | 2229 MaybeHandle<JSArrayBuffer> maybe_mem_buffer = |
| 2230 GetInstanceMemory(isolate, instance); | 2230 GetInstanceMemory(isolate, instance); |
| 2231 Handle<JSArrayBuffer> old_buffer; | 2231 Handle<JSArrayBuffer> old_buffer; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2273 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()), | 2273 JSObject::cast(compiled_module->ptr_to_weak_module_object()->value()), |
| 2274 *module_obj); | 2274 *module_obj); |
| 2275 Object* prev = nullptr; | 2275 Object* prev = nullptr; |
| 2276 int found_instances = compiled_module->has_weak_owning_instance() ? 1 : 0; | 2276 int found_instances = compiled_module->has_weak_owning_instance() ? 1 : 0; |
| 2277 WasmCompiledModule* current_instance = compiled_module; | 2277 WasmCompiledModule* current_instance = compiled_module; |
| 2278 while (current_instance->has_weak_next_instance()) { | 2278 while (current_instance->has_weak_next_instance()) { |
| 2279 CHECK((prev == nullptr && !current_instance->has_weak_prev_instance()) || | 2279 CHECK((prev == nullptr && !current_instance->has_weak_prev_instance()) || |
| 2280 current_instance->ptr_to_weak_prev_instance()->value() == prev); | 2280 current_instance->ptr_to_weak_prev_instance()->value() == prev); |
| 2281 CHECK_EQ(current_instance->ptr_to_weak_module_object()->value(), | 2281 CHECK_EQ(current_instance->ptr_to_weak_module_object()->value(), |
| 2282 *module_obj); | 2282 *module_obj); |
| 2283 CHECK( | 2283 CHECK(IsWasmInstance( |
| 2284 IsWasmObject(current_instance->ptr_to_weak_owning_instance()->value())); | 2284 current_instance->ptr_to_weak_owning_instance()->value())); |
| 2285 prev = current_instance; | 2285 prev = current_instance; |
| 2286 current_instance = WasmCompiledModule::cast( | 2286 current_instance = WasmCompiledModule::cast( |
| 2287 current_instance->ptr_to_weak_next_instance()->value()); | 2287 current_instance->ptr_to_weak_next_instance()->value()); |
| 2288 ++found_instances; | 2288 ++found_instances; |
| 2289 CHECK_LE(found_instances, instance_count); | 2289 CHECK_LE(found_instances, instance_count); |
| 2290 } | 2290 } |
| 2291 CHECK_EQ(found_instances, instance_count); | 2291 CHECK_EQ(found_instances, instance_count); |
| 2292 } | 2292 } |
| 2293 | 2293 |
| 2294 void testing::ValidateModuleState(Isolate* isolate, | 2294 void testing::ValidateModuleState(Isolate* isolate, |
| 2295 Handle<JSObject> module_obj) { | 2295 Handle<JSObject> module_obj) { |
| 2296 DisallowHeapAllocation no_gc; | 2296 DisallowHeapAllocation no_gc; |
| 2297 WasmCompiledModule* compiled_module = | 2297 WasmCompiledModule* compiled_module = |
| 2298 WasmCompiledModule::cast(module_obj->GetInternalField(0)); | 2298 WasmCompiledModule::cast(module_obj->GetInternalField(0)); |
| 2299 CHECK(compiled_module->has_weak_module_object()); | 2299 CHECK(compiled_module->has_weak_module_object()); |
| 2300 CHECK_EQ(compiled_module->ptr_to_weak_module_object()->value(), *module_obj); | 2300 CHECK_EQ(compiled_module->ptr_to_weak_module_object()->value(), *module_obj); |
| 2301 CHECK(!compiled_module->has_weak_prev_instance()); | 2301 CHECK(!compiled_module->has_weak_prev_instance()); |
| 2302 CHECK(!compiled_module->has_weak_next_instance()); | 2302 CHECK(!compiled_module->has_weak_next_instance()); |
| 2303 CHECK(!compiled_module->has_weak_owning_instance()); | 2303 CHECK(!compiled_module->has_weak_owning_instance()); |
| 2304 } | 2304 } |
| 2305 | 2305 |
| 2306 void testing::ValidateOrphanedInstance(Isolate* isolate, | 2306 void testing::ValidateOrphanedInstance(Isolate* isolate, |
| 2307 Handle<JSObject> instance) { | 2307 Handle<JSObject> instance) { |
| 2308 DisallowHeapAllocation no_gc; | 2308 DisallowHeapAllocation no_gc; |
| 2309 CHECK(IsWasmObject(*instance)); | 2309 CHECK(IsWasmInstance(*instance)); |
| 2310 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); | 2310 WasmCompiledModule* compiled_module = GetCompiledModule(*instance); |
| 2311 CHECK(compiled_module->has_weak_module_object()); | 2311 CHECK(compiled_module->has_weak_module_object()); |
| 2312 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); | 2312 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); |
| 2313 } | 2313 } |
| OLD | NEW |