diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index 153794a1e..92fcd674a 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -9,6 +9,7 @@ #include "FastEnumerationAdapter.h" #include "Helpers.h" #include "Interop.h" +#include "MetadataBuilder.h" #include "NativeScriptException.h" #include "ObjectManager.h" #include "Runtime.h" @@ -638,6 +639,9 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { Local exposedMethods, Local exposedProtocols, Local implementationObject) { Isolate* isolate = v8::Isolate::GetCurrent(); + // The methods native code calls: exposed ones, and those implementing a + // native class's or protocol's method. + std::vector nativeCalled; std::vector protocols; if (!exposedProtocols.IsEmpty() && exposedProtocols->IsArray()) { Local protocolsArray = exposedProtocols.As(); @@ -711,6 +715,7 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { std::string methodNameStr = tns::ToString(isolate, methodName); SEL selector = sel_registerName(methodNameStr.c_str()); + nativeCalled.push_back(methodNameStr); // TODO: Investigate and maybe find a way to free this // Currently this is added to the meta cache by the Interop::CreateMethod @@ -755,6 +760,7 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { const Meta* m = ArgConverter::FindMeta(extendedClass); if (m == nullptr) { + MetadataBuilder::GuardNativeCalledMethods(context, implementationObject, nativeCalled, false); return; } @@ -828,26 +834,34 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { .ToLocal(&setter); tns::Assert(success, isolate); + Local method; if ((!getter.IsEmpty() || !setter.IsEmpty()) && (getter->IsFunction() || setter->IsFunction())) { - std::vector propertyMetas; - VisitProperties(methodName, extendedClassMeta, propertyMetas, protocols); - ExposeProperties(isolate, extendedClass, propertyMetas, getter, setter); - continue; + // A method an earlier pass over this prototype turned into an accessor + // pair: +initialize also runs for a subclass that does not implement it. + method = MetadataBuilder::GuardedNativeCalledMethod(context, getter); + if (method.IsEmpty()) { + std::vector propertyMetas; + VisitProperties(methodName, extendedClassMeta, propertyMetas, protocols); + ExposeProperties(isolate, extendedClass, propertyMetas, getter, setter); + continue; + } + } else { + success = propertyDescriptor.As() + ->Get(context, tns::ToV8String(isolate, "value")) + .ToLocal(&method); + tns::Assert(success, isolate); } - Local method; - success = propertyDescriptor.As() - ->Get(context, tns::ToV8String(isolate, "value")) - .ToLocal(&method); - tns::Assert(success, isolate); - if (method.IsEmpty() || !method->IsFunction()) { continue; } std::vector methodMetas; VisitMethods(extendedClass, methodName, extendedClassMeta, methodMetas, protocols); + if (!methodMetas.empty()) { + nativeCalled.push_back(methodName); + } for (int j = 0; j < methodMetas.size(); j++) { const MethodMeta* methodMeta = methodMetas[j]; @@ -868,6 +882,10 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { typeInfo.c_str()); } } + + MetadataBuilder::GuardNativeCalledMethods( + context, implementationObject, nativeCalled, + ownsIterator && !symbolIterator.IsEmpty() && symbolIterator->IsFunction()); } void ClassBuilder::VisitProperties(std::string propertyName, const BaseClassMeta* meta, diff --git a/NativeScript/runtime/MetadataBuilder.h b/NativeScript/runtime/MetadataBuilder.h index 0b525fd54..81547fecf 100644 --- a/NativeScript/runtime/MetadataBuilder.h +++ b/NativeScript/runtime/MetadataBuilder.h @@ -44,6 +44,20 @@ class MetadataBuilder { v8::Local property, v8::Local receiver, v8::Local value); + // Where the native member guard is on (GuardsNativeMembers): turns each + // of `names`, and Symbol.iterator with `iterator`, an own method of + // `prototype`, a prototype level of a JS-extended class, that native code + // calls through the function the class captured, into an accessor pair + // whose getter returns that same function, so reads and `super` loads + // resolve as before. Assigning the name on a native object throws. + static void GuardNativeCalledMethods(v8::Local context, + v8::Local prototype, + const std::vector& names, + bool iterator); + // The method behind an accessor pair GuardNativeCalledMethods installed, + // given its getter; empty for any other value. + static v8::Local GuardedNativeCalledMethod( + v8::Local context, v8::Local getter); private: // The binding of an object whose static type adds protocols to `meta`: a @@ -111,11 +125,13 @@ class MetadataBuilder { static void RegisterAllocMethod( v8::Isolate* isolate, v8::Local ctorFuncTemplate, const InterfaceMeta* interfaceMeta); + // `guarded`, when given, records the JS name of each method installed. static void RegisterInstanceMethods( v8::Local context, v8::Local ctorFuncTemplate, const BaseClassMeta* meta, KnownUnknownClassPair pair, - robin_hood::unordered_map& names); + robin_hood::unordered_map& names, + std::vector* guarded = nullptr); // `setters`, when given, records each accessor pair installed. static void RegisterInstanceProperties( v8::Local context, @@ -130,13 +146,15 @@ class MetadataBuilder { const BaseClassMeta* meta, std::string className, KnownUnknownClassPair pair, robin_hood::unordered_map& names, - NativeSetters* setters = nullptr); + NativeSetters* setters = nullptr, + std::vector* guarded = nullptr); static void RegisterAdditionalProtocols( v8::Local context, v8::Local ctorFuncTemplate, KnownUnknownClassPair pair, const std::vector& additionalProtocols, - robin_hood::unordered_map& names); + robin_hood::unordered_map& names, + std::vector* guarded = nullptr); static void RegisterStaticMethods( v8::Local context, v8::Local ctorFunc, const BaseClassMeta* meta, KnownUnknownClassPair pair, diff --git a/NativeScript/runtime/MetadataBuilder.mm b/NativeScript/runtime/MetadataBuilder.mm index fbcd6f46e..d3cbb655a 100644 --- a/NativeScript/runtime/MetadataBuilder.mm +++ b/NativeScript/runtime/MetadataBuilder.mm @@ -205,6 +205,175 @@ void ForgetNativeSetter(Isolate* isolate, Local prototype, Local getterFactory; + // Marks the getters MetadataBuilder::GuardNativeCalledMethods installs. + Global nativeCalledGetter; +}; + +NativeMemberGuardState* GuardState(Isolate* isolate, Local context) { + NativeMemberGuardState* state = Caches::StateFor(isolate); + if (state == nullptr || !state->getterFactory.IsEmpty()) { + return state; + } + Local