Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions NativeScript/runtime/ClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -638,6 +639,9 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) {
Local<Value> exposedMethods, Local<Value> exposedProtocols,
Local<Object> 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<std::string> nativeCalled;
std::vector<const ProtocolMeta*> protocols;
if (!exposedProtocols.IsEmpty() && exposedProtocols->IsArray()) {
Local<v8::Array> protocolsArray = exposedProtocols.As<v8::Array>();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -828,26 +834,34 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) {
.ToLocal(&setter);
tns::Assert(success, isolate);

Local<Value> method;
if ((!getter.IsEmpty() || !setter.IsEmpty()) &&
(getter->IsFunction() || setter->IsFunction())) {
std::vector<const PropertyMeta*> 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<const PropertyMeta*> propertyMetas;
VisitProperties(methodName, extendedClassMeta, propertyMetas, protocols);
ExposeProperties(isolate, extendedClass, propertyMetas, getter, setter);
continue;
}
} else {
success = propertyDescriptor.As<Object>()
->Get(context, tns::ToV8String(isolate, "value"))
.ToLocal(&method);
tns::Assert(success, isolate);
}

Local<Value> method;
success = propertyDescriptor.As<Object>()
->Get(context, tns::ToV8String(isolate, "value"))
.ToLocal(&method);
tns::Assert(success, isolate);

if (method.IsEmpty() || !method->IsFunction()) {
continue;
}

std::vector<const MethodMeta*> 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];
Expand All @@ -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,
Expand Down
24 changes: 21 additions & 3 deletions NativeScript/runtime/MetadataBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ class MetadataBuilder {
v8::Local<v8::Name> property,
v8::Local<v8::Object> receiver,
v8::Local<v8::Value> 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<v8::Context> context,
v8::Local<v8::Object> prototype,
const std::vector<std::string>& names,
bool iterator);
// The method behind an accessor pair GuardNativeCalledMethods installed,
// given its getter; empty for any other value.
static v8::Local<v8::Value> GuardedNativeCalledMethod(
v8::Local<v8::Context> context, v8::Local<v8::Value> getter);

private:
// The binding of an object whose static type adds protocols to `meta`: a
Expand Down Expand Up @@ -111,11 +125,13 @@ class MetadataBuilder {
static void RegisterAllocMethod(
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
const InterfaceMeta* interfaceMeta);
// `guarded`, when given, records the JS name of each method installed.
static void RegisterInstanceMethods(
v8::Local<v8::Context> context,
v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
const BaseClassMeta* meta, KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names);
robin_hood::unordered_map<std::string, uint8_t>& names,
std::vector<std::string>* guarded = nullptr);
// `setters`, when given, records each accessor pair installed.
static void RegisterInstanceProperties(
v8::Local<v8::Context> context,
Expand All @@ -130,13 +146,15 @@ class MetadataBuilder {
const BaseClassMeta* meta, std::string className,
KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names,
NativeSetters* setters = nullptr);
NativeSetters* setters = nullptr,
std::vector<std::string>* guarded = nullptr);
static void RegisterAdditionalProtocols(
v8::Local<v8::Context> context,
v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
KnownUnknownClassPair pair,
const std::vector<std::string>& additionalProtocols,
robin_hood::unordered_map<std::string, uint8_t>& names);
robin_hood::unordered_map<std::string, uint8_t>& names,
std::vector<std::string>* guarded = nullptr);
static void RegisterStaticMethods(
v8::Local<v8::Context> context, v8::Local<v8::Function> ctorFunc,
const BaseClassMeta* meta, KnownUnknownClassPair pair,
Expand Down
Loading
Loading