Skip to content
Closed
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
23 changes: 12 additions & 11 deletions NativeScript/runtime/ArgConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Common.h"
#include "DataWrapper.h"
#include "IsolateWrapper.h"
#include "WrapperLifetimeCanary.h"
#include "libffi.h"

namespace tns {
Expand Down Expand Up @@ -64,14 +65,14 @@ class ArgConverter {
v8::Local<v8::Object> receiver,
const std::vector<std::string>& additionalProtocols =
std::vector<std::string>());
// `intercepted`: the object carries the canary handlers
// (WrapperLifetimeCanary::Intercepts).
// `kind`: the canary handlers the object carries
// (WrapperLifetimeCanary::KindOf).
static std::shared_ptr<v8::Persistent<v8::Value>> CreateEmptyObject(
v8::Local<v8::Context> context, bool intercepted);
// The constructor NewEmptyInstance takes: EmptyObjCtorFunc, or with
// `intercepted` its counterpart carrying the canary handlers.
v8::Local<v8::Context> context, CanaryKind kind);
// The constructor NewEmptyInstance takes: EmptyObjCtorFunc for kNone, or its
// counterpart carrying `kind`'s canary handlers.
static v8::Persistent<v8::Function>* EmptyInstanceFunction(
v8::Isolate* isolate, bool intercepted);
v8::Isolate* isolate, CanaryKind kind);
// CreateEmptyObject's object without the ObjectManager registration, for an
// object whose collection has nothing to dispose.
static v8::Local<v8::Object> NewEmptyInstance(
Expand Down Expand Up @@ -138,17 +139,17 @@ class ArgConverter {
// caller returns its defined default. Resets `tc` in both cases.
static id HandleBoundaryException(v8::Local<v8::Context> context,
v8::TryCatch& tc);
// `thisIntercepts`: the constructor's instance template carries the canary
// handlers (WrapperLifetimeCanary::ConfigureInstanceTemplate). Without them,
// a canary gets a new object, returned in place of `this`.
// `thisKind`: the canary handlers the constructor's instance template
// carries (WrapperLifetimeCanary::ConfigureInstanceTemplate). A canary whose
// kind differs gets a new object, returned in place of `this`.
static void ConstructObject(v8::Local<v8::Context> context,
const v8::FunctionCallbackInfo<v8::Value>& info,
Class klass, const InterfaceMeta* interfaceMeta,
bool thisIntercepts);
CanaryKind thisKind);

private:
static v8::Local<v8::Function> CreateEmptyInstanceFunction(
v8::Local<v8::Context> context, bool intercepted);
v8::Local<v8::Context> context, CanaryKind kind);
static std::shared_ptr<v8::Persistent<v8::Value>> CreateEmptyInstance(
v8::Local<v8::Context> context, v8::Persistent<v8::Function>* ctorFunc);
static void FindMethodOverloads(Class klass, std::string methodName,
Expand Down
85 changes: 46 additions & 39 deletions NativeScript/runtime/ArgConverter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ bool IsKVOSubclassOf(Class klass, Class base) {
strncmp(class_getName(klass), prefix, sizeof(prefix) - 1) == 0;
}

// Canary mode: constructors of wrappers that carry the canary handlers.
// Canary mode: constructors of wrappers that carry the canary handlers, per
// CanaryKind (kNone has none).
struct CanaryWrapperConstructors {
static constexpr size_t kKinds = 3;
// The empty instance's, for canaries.
std::unique_ptr<Persistent<v8::Function>> emptyInstance;
// Shaped like a native class constructor's instances, which lack the
// handlers under scope "extended".
std::unique_ptr<Persistent<v8::Function>> constructedInstance;
std::unique_ptr<Persistent<v8::Function>> emptyInstance[kKinds];
// Shaped like a native class constructor's instances, whose template is
// configured for one kind only (WrapperLifetimeCanary::NativeInstancesKind).
std::unique_ptr<Persistent<v8::Function>> constructedInstance[kKinds];
};

} // namespace
Expand Down Expand Up @@ -98,33 +100,37 @@ bool IsKVOSubclassOf(Class klass, Class base) {
Isolate* isolate = v8::Isolate::GetCurrent();
auto cache = Caches::Get(isolate);
cache->EmptyObjCtorFunc = std::make_unique<Persistent<v8::Function>>(
isolate, ArgConverter::CreateEmptyInstanceFunction(context, false));
isolate, ArgConverter::CreateEmptyInstanceFunction(context, CanaryKind::kNone));
if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) {
return;
}
CanaryWrapperConstructors* canary = Caches::StateFor<CanaryWrapperConstructors>(isolate);
if (canary == nullptr) {
return;
}
canary->emptyInstance = std::make_unique<Persistent<v8::Function>>(
isolate, ArgConverter::CreateEmptyInstanceFunction(context, true));
Local<FunctionTemplate> constructedTemplate = FunctionTemplate::New(isolate);
constructedTemplate->InstanceTemplate()->SetInternalFieldCount(1);
WrapperLifetimeCanary::ConfigureInstanceTemplate(constructedTemplate->InstanceTemplate());
Local<v8::Function> constructedFunc;
tns::Assert(constructedTemplate->GetFunction(context).ToLocal(&constructedFunc), isolate);
canary->constructedInstance =
std::make_unique<Persistent<v8::Function>>(isolate, constructedFunc);
for (CanaryKind kind : {CanaryKind::kPlain, CanaryKind::kExtended}) {
size_t index = static_cast<size_t>(kind);
canary->emptyInstance[index] = std::make_unique<Persistent<v8::Function>>(
isolate, ArgConverter::CreateEmptyInstanceFunction(context, kind));
Local<FunctionTemplate> constructedTemplate = FunctionTemplate::New(isolate);
constructedTemplate->InstanceTemplate()->SetInternalFieldCount(1);
WrapperLifetimeCanary::ConfigureInstanceTemplate(constructedTemplate->InstanceTemplate(), kind);
Local<v8::Function> constructedFunc;
tns::Assert(constructedTemplate->GetFunction(context).ToLocal(&constructedFunc), isolate);
canary->constructedInstance[index] =
std::make_unique<Persistent<v8::Function>>(isolate, constructedFunc);
}
}

Persistent<v8::Function>* ArgConverter::EmptyInstanceFunction(Isolate* isolate, bool intercepted) {
if (intercepted) {
Persistent<v8::Function>* ArgConverter::EmptyInstanceFunction(Isolate* isolate, CanaryKind kind) {
if (kind != CanaryKind::kNone) {
// Teardown-safe: a -dealloc receiver can be built after InvalidateIsolate,
// while deferred releases drain.
CanaryWrapperConstructors* canary =
Caches::ExistingStateFor<CanaryWrapperConstructors>(isolate);
tns::Assert(canary != nullptr && canary->emptyInstance != nullptr, isolate);
return canary->emptyInstance.get();
size_t index = static_cast<size_t>(kind);
tns::Assert(canary != nullptr && canary->emptyInstance[index] != nullptr, isolate);
return canary->emptyInstance[index].get();
}
return Caches::Get(isolate)->EmptyObjCtorFunc.get();
}
Expand Down Expand Up @@ -741,7 +747,7 @@ bool IsKVOSubclassOf(Class klass, Class base) {

void ArgConverter::ConstructObject(Local<Context> context, const FunctionCallbackInfo<Value>& info,
Class klass, const InterfaceMeta* interfaceMeta,
bool thisIntercepts) {
CanaryKind thisKind) {
Isolate* isolate = v8::Isolate::GetCurrent();
tns::Assert(klass != nullptr, isolate);
WrapperLifetime::DrainAtCallEntry(isolate);
Expand Down Expand Up @@ -805,20 +811,23 @@ bool IsKVOSubclassOf(Class klass, Class base) {
ObjCDataWrapper* wrapper = new ObjCDataWrapper(result);
Local<Object> thiz = info.This();
Local<Context> context = cache->GetContext();
bool intercepted = thisIntercepts;
if (!intercepted && WrapperLifetimeCanary::Intercepts(result)) {
// A canary needs the handlers `this` was built without: returning
// another object makes it the result of `new`, and `this` after super().
CanaryKind kind = thisKind;
CanaryKind resultKind = WrapperLifetimeCanary::KindOf(result);
if (resultKind != CanaryKind::kNone && resultKind != kind) {
// A canary needs handlers `this` was built without: returning another
// object makes it the result of `new`, and `this` after super().
CanaryWrapperConstructors* canary = Caches::StateFor<CanaryWrapperConstructors>(isolate);
tns::Assert(canary != nullptr && canary->constructedInstance != nullptr, isolate);
Local<Object> replacement = NewEmptyInstance(context, canary->constructedInstance.get());
size_t index = static_cast<size_t>(resultKind);
tns::Assert(canary != nullptr && canary->constructedInstance[index] != nullptr, isolate);
Local<Object> replacement =
NewEmptyInstance(context, canary->constructedInstance[index].get());
bool success;
if (!replacement->SetPrototype(context, thiz->GetPrototype()).To(&success) || !success) {
tns::Assert(false, isolate);
}
thiz = replacement;
info.GetReturnValue().Set(thiz);
intercepted = true;
kind = resultKind;
}
tns::SetValue(isolate, thiz, wrapper);
std::shared_ptr<Persistent<Value>> poThiz = ObjectManager::Register(context, thiz);
Expand All @@ -837,7 +846,7 @@ bool IsKVOSubclassOf(Class klass, Class base) {
wrapper->SetBornOnRuntimeThread();
ObjectManager::ProtectIfNativelyRetained(isolate, result, wrapper);
}
WrapperLifetime::Adopt(isolate, wrapper, poThiz, result, intercepted);
WrapperLifetime::Adopt(isolate, wrapper, poThiz, result, kind);
ExternalMemory::ChargeEstimatedSize(isolate, thiz);
}
}
Expand Down Expand Up @@ -1058,7 +1067,7 @@ bool IsKVOSubclassOf(Class klass, Class base) {
ObjCAllocDataWrapper* allocDataWrapper = static_cast<ObjCAllocDataWrapper*>(wrapper);
Class klass = allocDataWrapper->Klass();

std::shared_ptr<Persistent<Value>> poValue = CreateEmptyObject(context, false);
std::shared_ptr<Persistent<Value>> poValue = CreateEmptyObject(context, CanaryKind::kNone);
receiver = poValue->Get(isolate).As<Object>();

const Meta* meta = FindMeta(klass);
Expand Down Expand Up @@ -1108,8 +1117,8 @@ bool IsKVOSubclassOf(Class klass, Class base) {
receiver = it->second->Get(isolate).As<Object>();
WrapperLifetime::EscapeValue(isolate, receiver);
} else {
bool intercepted = WrapperLifetimeCanary::Intercepts(target);
std::shared_ptr<Persistent<Value>> poValue = CreateEmptyObject(context, intercepted);
CanaryKind kind = WrapperLifetimeCanary::KindOf(target);
std::shared_ptr<Persistent<Value>> poValue = CreateEmptyObject(context, kind);
receiver = poValue->Get(isolate).As<Object>();
tns::SetValue(isolate, receiver, wrapper);
cache->Instances.emplace(target, poValue);
Expand All @@ -1124,7 +1133,7 @@ bool IsKVOSubclassOf(Class klass, Class base) {
}
if (wrapper->Type() == WrapperType::ObjCObject) {
WrapperLifetime::Adopt(isolate, static_cast<ObjCDataWrapper*>(wrapper), poValue, target,
intercepted);
kind);
}
}
} else {
Expand Down Expand Up @@ -1333,7 +1342,7 @@ bool IsKVOSubclassOf(Class klass, Class base) {
wrapper->MarkBorrowed();
// Its writes reach the object's JS state through the canary handlers.
Local<Object> receiver = NewEmptyInstance(
context, EmptyInstanceFunction(isolate, WrapperLifetimeCanary::Intercepts(self)));
context, EmptyInstanceFunction(isolate, WrapperLifetimeCanary::KindOf(self)));
CreateJsWrapper(context, wrapper, receiver);
WrapperLifetimeCanary::Hydrate(isolate, receiver);
*borrowed = wrapper;
Expand Down Expand Up @@ -1364,9 +1373,9 @@ bool IsKVOSubclassOf(Class klass, Class base) {
}

std::shared_ptr<Persistent<Value>> ArgConverter::CreateEmptyObject(Local<Context> context,
bool intercepted) {
CanaryKind kind) {
Isolate* isolate = v8::Isolate::GetCurrent();
Persistent<v8::Function>* ctorFunc = EmptyInstanceFunction(isolate, intercepted);
Persistent<v8::Function>* ctorFunc = EmptyInstanceFunction(isolate, kind);
tns::Assert(ctorFunc != nullptr, isolate);
return ArgConverter::CreateEmptyInstance(context, ctorFunc);
}
Expand All @@ -1390,17 +1399,15 @@ bool IsKVOSubclassOf(Class klass, Class base) {
}

Local<v8::Function> ArgConverter::CreateEmptyInstanceFunction(Local<Context> context,
bool intercepted) {
CanaryKind kind) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<FunctionTemplate> emptyInstanceCtorFuncTemplate = FunctionTemplate::New(isolate, nullptr);
Local<ObjectTemplate> instanceTemplate = emptyInstanceCtorFuncTemplate->InstanceTemplate();
instanceTemplate->SetInternalFieldCount(2);

instanceTemplate->SetHandler(IndexedPropertyHandlerConfiguration(IndexedPropertyGetterCallback,
IndexedPropertySetterCallback));
if (intercepted) {
WrapperLifetimeCanary::ConfigureInstanceTemplate(instanceTemplate);
}
WrapperLifetimeCanary::ConfigureInstanceTemplate(instanceTemplate, kind);

Local<v8::Function> emptyInstanceCtorFunc;
if (!emptyInstanceCtorFuncTemplate->GetFunction(context).ToLocal(&emptyInstanceCtorFunc)) {
Expand Down
4 changes: 2 additions & 2 deletions NativeScript/runtime/ClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) {
FunctionTemplate::New(isolate, ExtendedClassConstructorCallback, ext);
extendedClassCtorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(1);
WrapperLifetimeCanary::ConfigureInstanceTemplate(
extendedClassCtorFuncTemplate->InstanceTemplate());
extendedClassCtorFuncTemplate->InstanceTemplate(), CanaryKind::kExtended);

Local<v8::Function> extendClassCtorFunc;
if (!extendedClassCtorFuncTemplate->GetFunction(context).ToLocal(&extendClassCtorFunc)) {
Expand Down Expand Up @@ -180,7 +180,7 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) {
info.Data().As<External>()->Value(v8::kExternalPointerTypeTagDefault));
Class klass = item->data_;

ArgConverter::ConstructObject(context, info, klass, nullptr, true);
ArgConverter::ConstructObject(context, info, klass, nullptr, CanaryKind::kExtended);
} catch (NativeScriptException& ex) {
ex.ReThrowToV8(isolate);
}
Expand Down
7 changes: 3 additions & 4 deletions NativeScript/runtime/MetadataBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,8 @@ throw NativeScriptException(

ctorFuncTemplate = FunctionTemplate::New(isolate, ClassConstructorCallback, ext);
ctorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(1);
if (WrapperLifetimeCanary::NativeInstancesIntercept()) {
WrapperLifetimeCanary::ConfigureInstanceTemplate(ctorFuncTemplate->InstanceTemplate());
}
WrapperLifetimeCanary::ConfigureInstanceTemplate(ctorFuncTemplate->InstanceTemplate(),
WrapperLifetimeCanary::NativeInstancesKind());
ctorFuncTemplate->SetClassName(tns::ToV8String(isolate, meta->name()));

Local<v8::Function> baseCtorFunc;
Expand Down Expand Up @@ -1410,7 +1409,7 @@ void CollectInstanceMemberNames(const BaseClassMeta* meta, Class klass, bool own

const InterfaceMeta* interfaceMeta = static_cast<const InterfaceMeta*>(item->meta_);
ArgConverter::ConstructObject(context, info, klass, interfaceMeta,
WrapperLifetimeCanary::NativeInstancesIntercept());
WrapperLifetimeCanary::NativeInstancesKind());
} catch (NativeScriptException& ex) {
ex.ReThrowToV8(isolate);
}
Expand Down
7 changes: 4 additions & 3 deletions NativeScript/runtime/WrapperLifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "DataWrapper.h"
#include "Helpers.h"
#include "ObjectManager.h"
#include "WrapperLifetimeCanary.h"

namespace tns {

Expand Down Expand Up @@ -76,11 +77,11 @@ class WrapperLifetime {

// A newly wrapped ObjC object: `handle` is its Instances entry, registered by
// ObjectManager::Register and holding the entry's +1 on `target`.
// `intercepted`: the wrapper carries the canary handlers
// (WrapperLifetimeCanary::Intercepts).
// `kind`: the canary handlers the wrapper carries
// (WrapperLifetimeCanary::KindOf).
static void Adopt(v8::Isolate* isolate, ObjCDataWrapper* wrapper,
const std::shared_ptr<v8::Persistent<v8::Value>>& handle,
id target, bool intercepted);
id target, CanaryKind kind);

// The object crossed the JS/native boundary: native code returned it to JS,
// or received it as a receiver, argument, return value of a JS-implemented
Expand Down
9 changes: 6 additions & 3 deletions NativeScript/runtime/WrapperLifetime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void ReleaseAll(Isolate* isolate, RootedState* rooted) {

void WrapperLifetime::Adopt(Isolate* isolate, ObjCDataWrapper* wrapper,
const std::shared_ptr<Persistent<Value>>& handle, id target,
bool intercepted) {
CanaryKind kind) {
if (GetWrapperLifetimeMode() == WrapperLifetimeMode::kResurrecting || target == nil) {
return;
}
Expand All @@ -372,8 +372,11 @@ void ReleaseAll(Isolate* isolate, RootedState* rooted) {
wrapper->SetLifetimeState(state);
if (GetWrapperLifetimeMode() == WrapperLifetimeMode::kCanary) {
state->lifetimeCanary_ = IsInScope(target);
// A canary without the handlers would lose its JS properties when it dies.
tns::Assert(!state->lifetimeCanary_ || intercepted, isolate);
#ifdef DEBUG
// A canary without its kind's handlers would lose its JS properties when it
// dies.
tns::Assert(!state->lifetimeCanary_ || kind == WrapperLifetimeCanary::KindOf(target), isolate);
#endif
if (state->lifetimeCanary_) {
state->canaryRecord_ = WrapperLifetimeCanary::Find(isolate, target);
if (state->canaryRecord_ != nullptr) {
Expand Down
Loading
Loading