#include "DispatchOverrides.h" #ifdef APPLICATION_IN_DEBUG #include #include #include #include #include #include "ArgConverter.h" #include "ObjectManager.h" #include "RuntimeState.h" using namespace v8; namespace tns { DispatchOverrides::DispatchOverrides(Isolate* isolate, Local implementationObject, std::string className) : className_(std::move(className)), implementationObject_(isolate, implementationObject) { auto context = isolate->GetCurrentContext(); TryCatch tc(isolate); Local names; if (!implementationObject ->GetOwnPropertyNames(context, PropertyFilter::SKIP_SYMBOLS) .ToLocal(&names)) { return; } auto valueKey = ArgConverter::ConvertToV8String(isolate, std::string("value")); uint32_t length = names->Length(); captured_.reserve(length); for (uint32_t i = 0; i < length; i++) { Local name; if (!names->Get(context, i).ToLocal(&name) || !name->IsString()) { continue; } Captured entry; entry.name.Reset(isolate, name.As()); Local descriptor; Local value; if (implementationObject->GetOwnPropertyDescriptor(context, name.As()) .ToLocal(&descriptor) && descriptor->IsObject() && descriptor.As()->Get(context, valueKey).ToLocal(&value) && value->IsFunction()) { entry.function.Reset(isolate, value.As()); } captured_.push_back(std::move(entry)); } } void DispatchOverrides::CheckSlow(Isolate* isolate, Local context, Local instance, Local methodName, size_t index, Local method) { if (index >= byNameId_.size()) { byNameId_.resize(index + 1); } auto& entry = byNameId_[index]; if (!entry.bound) { entry.bound = true; const Captured* captured = nullptr; for (const auto& candidate : captured_) { if (candidate.name.Get(isolate)->StringEquals(methodName)) { captured = &candidate; break; } } if (captured != nullptr) { if (captured->function.IsEmpty()) { entry.quiet = true; return; } entry.expected.Reset(isolate, captured->function); if (entry.expected == method) { return; } } else if (!DefinedAfterExtend(isolate, context, instance, methodName)) { // The class does not implement the method, so the call resolved to // whatever it inherits. entry.expected.Reset(isolate, method); return; } } entry.quiet = true; Warn(className_, methodName, method); } bool DispatchOverrides::DefinedAfterExtend(Isolate* isolate, Local context, Local instance, Local name) { // HasRealNamedProperty runs neither interceptors nor getters. if (instance->HasRealNamedProperty(context, name).FromMaybe(false)) { return true; } auto prototype = instance->GetPrototype(); if (prototype->IsObject() && prototype.As()->HasRealNamedProperty(context, name).FromMaybe(false)) { return true; } return implementationObject_.Get(isolate)->HasRealNamedProperty(context, name).FromMaybe(false); } namespace { // Keyed by jclass: an instance holds its class's global ref from the class cache, // which has one per class. struct InstancePropertyWarnings { std::set> warned; }; } // namespace void DispatchOverrides::CheckInstanceProperty(Isolate* isolate, Local context, Local instance, Local methodName, int methodNameId, Local method) { if (methodNameId < 0 || !instance->HasRealNamedProperty(context, methodName).FromMaybe(false)) { return; } auto* warnings = RuntimeState::For(isolate); auto* runtime = Runtime::TryGetRuntime(isolate); if (warnings == nullptr || runtime == nullptr) { return; } jclass clazz = runtime->GetObjectManager()->GetJavaClass(instance); if (!warnings->warned.emplace(clazz, methodNameId).second) { return; } std::string className = clazz != nullptr ? ObjectManager::GetClassName(clazz) : "a Java object"; std::replace(className.begin(), className.end(), '/', '.'); Warn(className, methodName, method); } void DispatchOverrides::Warn(const std::string& className, Local methodName, Local method) { std::stringstream ss; ss << "Java called \"" << ArgConverter::ConvertToString(methodName) << "\" on " << className << " through a function other than the class's own implementation (an instance property " "or a method replaced after extend)"; int line = method->GetScriptLineNumber(); if (line != Function::kLineOffsetNotFound) { auto resourceName = method->GetScriptOrigin().ResourceName(); ss << " defined at " << (resourceName->IsString() ? ArgConverter::ConvertToString(resourceName.As()) : std::string("")) << ":" << line + 1 << ":" << method->GetScriptColumnNumber() + 1; } ss << ". This may stop working in a future version, as on iOS. Define it on the class. " "Please report your use case."; __android_log_write(ANDROID_LOG_WARN, "JS", ss.str().c_str()); } } // namespace tns #endif // APPLICATION_IN_DEBUG