diff --git a/test-app/runtime/CMakeLists.txt b/test-app/runtime/CMakeLists.txt index d2030f74a..250131f9e 100644 --- a/test-app/runtime/CMakeLists.txt +++ b/test-app/runtime/CMakeLists.txt @@ -145,6 +145,7 @@ add_library( src/main/cpp/DirectBuffer.cpp src/main/cpp/FieldAccessor.cpp src/main/cpp/File.cpp + src/main/cpp/IsolateDisposer.cpp src/main/cpp/JEnv.cpp src/main/cpp/DesugaredInterfaceCompanionClassNameResolver.cpp src/main/cpp/JType.cpp diff --git a/test-app/runtime/src/main/cpp/ArgConverter.cpp b/test-app/runtime/src/main/cpp/ArgConverter.cpp index 236862525..75660ac78 100644 --- a/test-app/runtime/src/main/cpp/ArgConverter.cpp +++ b/test-app/runtime/src/main/cpp/ArgConverter.cpp @@ -293,4 +293,12 @@ Local ArgConverter::ConvertToV8UTF16String(Isolate* isolate, const u16st return String::NewFromTwoByte(isolate, ((const uint16_t*) utf16string.data())).ToLocalChecked(); } +void ArgConverter::onDisposeIsolate(Isolate* isolate) { + auto itFound = s_type_long_operations_cache.find(isolate); + if (itFound != s_type_long_operations_cache.end()) { + delete itFound->second; + s_type_long_operations_cache.erase(itFound); + } +} + std::map ArgConverter::s_type_long_operations_cache; \ No newline at end of file diff --git a/test-app/runtime/src/main/cpp/ArgConverter.h b/test-app/runtime/src/main/cpp/ArgConverter.h index 1d4bf43c1..39a1738eb 100644 --- a/test-app/runtime/src/main/cpp/ArgConverter.h +++ b/test-app/runtime/src/main/cpp/ArgConverter.h @@ -43,6 +43,8 @@ class ArgConverter { static v8::Local ConvertToV8UTF16String(v8::Isolate* isolate, const std::u16string& utf16string); + static void onDisposeIsolate(v8::Isolate* isolate); + private: // TODO: plamen5kov: rewrite logic for java long number operations in javascript (java long -> javascript number operations check) diff --git a/test-app/runtime/src/main/cpp/IsolateDisposer.cpp b/test-app/runtime/src/main/cpp/IsolateDisposer.cpp new file mode 100644 index 000000000..b5453289a --- /dev/null +++ b/test-app/runtime/src/main/cpp/IsolateDisposer.cpp @@ -0,0 +1,19 @@ +// +// Created by Eduardo Speroni on 3/4/22. +// + +#include "IsolateDisposer.h" +#include "ArgConverter.h" +#include "MetadataNode.h" +#include "V8GlobalHelpers.h" +#include + + +namespace tns { + void disposeIsolate(v8::Isolate *isolate) { + tns::ArgConverter::onDisposeIsolate(isolate); + tns::MetadataNode::onDisposeIsolate(isolate); + tns::V8GlobalHelpers::onDisposeIsolate(isolate); + tns::Console::onDisposeIsolate(isolate); + } +} diff --git a/test-app/runtime/src/main/cpp/IsolateDisposer.h b/test-app/runtime/src/main/cpp/IsolateDisposer.h new file mode 100644 index 000000000..7ce85997c --- /dev/null +++ b/test-app/runtime/src/main/cpp/IsolateDisposer.h @@ -0,0 +1,13 @@ +// +// Created by Eduardo Speroni on 3/4/22. +// + +#ifndef TEST_APP_ISOLATEDISPOSER_H +#define TEST_APP_ISOLATEDISPOSER_H +#include "v8.h" + +namespace tns { + void disposeIsolate(v8::Isolate* isolate); +} + +#endif //TEST_APP_ISOLATEDISPOSER_H diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 9dc418bf6..97e92b58e 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -2176,6 +2176,32 @@ std::string MetadataNode::GetJniClassName(MetadataEntry entry) { return fullClassName; } +void MetadataNode::onDisposeIsolate(Isolate* isolate) { + { + auto it = s_metadata_node_cache.find(isolate); + if (it != s_metadata_node_cache.end()) { + delete it->second; + s_metadata_node_cache.erase(it); + } + } + { + auto it = s_arrayObjectTemplates.find(isolate); + if (it != s_arrayObjectTemplates.end()) { + delete it->second; + s_arrayObjectTemplates.erase(it); + } + } + { + for (auto it = s_treeNode2NodeCache.begin(); it != s_treeNode2NodeCache.end(); it++) { + auto it2 = it->second->m_poCtorCachePerIsolate.find(isolate); + if(it2 != it->second->m_poCtorCachePerIsolate.end()) { + delete it2->second; + it->second->m_poCtorCachePerIsolate.erase(it2); + } + } + } +} + string MetadataNode::TNS_PREFIX = "com/tns/gen/"; MetadataReader MetadataNode::s_metadataReader; std::map MetadataNode::s_name2NodeCache; diff --git a/test-app/runtime/src/main/cpp/MetadataNode.h b/test-app/runtime/src/main/cpp/MetadataNode.h index e233ae533..739594bdd 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.h +++ b/test-app/runtime/src/main/cpp/MetadataNode.h @@ -58,6 +58,8 @@ class MetadataNode { static MetadataNode* GetOrCreate(const std::string& className); static std::string GetTypeMetadataName(v8::Isolate* isolate, v8::Local& value); + + static void onDisposeIsolate(v8::Isolate* isolate); private: struct MethodCallbackData; diff --git a/test-app/runtime/src/main/cpp/Runtime.cpp b/test-app/runtime/src/main/cpp/Runtime.cpp index 95271437a..037eb0b19 100644 --- a/test-app/runtime/src/main/cpp/Runtime.cpp +++ b/test-app/runtime/src/main/cpp/Runtime.cpp @@ -29,6 +29,7 @@ #include "sys/system_properties.h" #include "ManualInstrumentation.h" #include +#include "IsolateDisposer.h" #ifdef APPLICATION_IN_DEBUG #include "JsV8InspectorClient.h" @@ -809,6 +810,7 @@ bool Runtime::RunExtraCode(Isolate* isolate, Local context, const char* void Runtime::DestroyRuntime() { s_id2RuntimeCache.erase(m_id); s_isolate2RuntimesCache.erase(m_isolate); + tns::disposeIsolate(m_isolate); } Local Runtime::GetContext() { diff --git a/test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp b/test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp index 17501ae34..988ca4c09 100644 --- a/test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp +++ b/test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp @@ -141,3 +141,7 @@ bool tns::V8SetPrivateValue(Isolate* isolate, const Local& obj, const Lo return res.FromMaybe(false); } + +void tns::V8GlobalHelpers::onDisposeIsolate(Isolate* isolate) { + isolateToPersistentSmartJSONStringify.erase(isolate); +} diff --git a/test-app/runtime/src/main/cpp/V8GlobalHelpers.h b/test-app/runtime/src/main/cpp/V8GlobalHelpers.h index c13e79311..b91208304 100644 --- a/test-app/runtime/src/main/cpp/V8GlobalHelpers.h +++ b/test-app/runtime/src/main/cpp/V8GlobalHelpers.h @@ -13,6 +13,10 @@ v8::Local JsonStringifyObject(v8::Isolate* isolate, v8::Handle& obj, const v8::Local& propName, v8::Local& out); bool V8SetPrivateValue(v8::Isolate* isolate, const v8::Local& obj, const v8::Local& propName, const v8::Local& value); + +namespace V8GlobalHelpers { + void onDisposeIsolate(v8::Isolate* isolate); +} } #endif /* V8GLOBALHELPERS_H_ */ diff --git a/test-app/runtime/src/main/cpp/console/Console.cpp b/test-app/runtime/src/main/cpp/console/Console.cpp index 5922f4914..ffcd82437 100644 --- a/test-app/runtime/src/main/cpp/console/Console.cpp +++ b/test-app/runtime/src/main/cpp/console/Console.cpp @@ -554,6 +554,10 @@ void Console::timeEndCallback(const v8::FunctionCallbackInfo& info) { } } +void Console::onDisposeIsolate(v8::Isolate* isolate) { + s_isolateToConsoleTimersMap.erase(isolate); +} + const char* Console::LOG_TAG = "JS"; std::map> Console::s_isolateToConsoleTimersMap; ConsoleCallback Console::m_callback = nullptr; diff --git a/test-app/runtime/src/main/cpp/console/Console.h b/test-app/runtime/src/main/cpp/console/Console.h index 268b93eee..1da0bcb2b 100644 --- a/test-app/runtime/src/main/cpp/console/Console.h +++ b/test-app/runtime/src/main/cpp/console/Console.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace tns { @@ -27,6 +28,8 @@ class Console { static void timeCallback(const v8::FunctionCallbackInfo& info); static void timeEndCallback(const v8::FunctionCallbackInfo& info); + static void onDisposeIsolate(v8::Isolate* isolate); + private: static int m_maxLogcatObjectSize; static bool m_forceLog;