Skip to content
Merged
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
46 changes: 38 additions & 8 deletions NativeScript/runtime/ArgConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,38 @@ namespace tns {

class ArgConverter;

// What native callers of a JS-implemented method own of the object it answers.
enum class NativeReturnOwnership : uint8_t {
Unowned,
// A +1; the receiver stays the caller's.
Owned,
// A +1 in place of the receiver, whose reference the caller passed in.
ReplacesReceiver,
};

struct MethodCallbackWrapper {
public:
MethodCallbackWrapper(v8::Isolate* isolate,
std::shared_ptr<v8::Persistent<v8::Value>> callback,
const uint8_t initialParamIndex,
const uint8_t paramsCount,
const TypeEncoding* typeEncoding)
MethodCallbackWrapper(
v8::Isolate* isolate, std::shared_ptr<v8::Persistent<v8::Value>> callback,
const uint8_t initialParamIndex, const uint8_t paramsCount,
const TypeEncoding* typeEncoding,
NativeReturnOwnership returnOwnership = NativeReturnOwnership::Unowned,
int8_t consumedArgumentIndex = -1)
: isolateWrapper_(isolate),
callback_(callback),
initialParamIndex_(initialParamIndex),
paramsCount_(paramsCount),
typeEncoding_(typeEncoding) {}
typeEncoding_(typeEncoding),
returnOwnership_(returnOwnership),
consumedArgumentIndex_(consumedArgumentIndex) {}
IsolateWrapper isolateWrapper_;
std::shared_ptr<v8::Persistent<v8::Value>> callback_;
const uint8_t initialParamIndex_;
const uint8_t paramsCount_;
const TypeEncoding* typeEncoding_;
const NativeReturnOwnership returnOwnership_;
// Interop::ConsumedObjectArgumentIndex of the implemented method.
const int8_t consumedArgumentIndex_;
};

class ArgConverter {
Expand Down Expand Up @@ -71,9 +86,24 @@ class ArgConverter {
static const ProtocolMeta* FindProtocolMeta(Protocol* protocol);
static void MethodCallback(ffi_cif* cif, void* retValue, void** argValues,
void* userData);
// For a JS implementation of the instance method `selector`; `meta` is null
// for methods the metadata does not describe, which are classified by
// their selector's method family.
static NativeReturnOwnership ReturnOwnership(SEL selector,
const MethodMeta* meta);
// Gives the native caller of a JS implementation the reference it owns of
// the object SetValue wrote into retValue; `writtenUnowned` is what
// SetValue answered. `consumedArgument`, an argument the method took over,
// already carries that reference when it is the result.
static void RetainOwnedResult(NativeReturnOwnership ownership,
void** argValues, void* retValue,
bool writtenUnowned, id consumedArgument = nil);
// returnSize is the ABI size of the return slot (cif->rtype->size); an
// empty, null or undefined value zeroes all of it.
static void SetValue(v8::Local<v8::Context> context, void* retValue,
// empty, null or undefined value zeroes all of it. Answers whether it wrote
// an object the caller does not own: a wrapped object, or a string made
// for an id or interface slot. A plain JS object is written as a new
// DictionaryAdapter the caller owns.
static bool SetValue(v8::Local<v8::Context> context, void* retValue,
size_t returnSize, v8::Local<v8::Value> value,
const TypeEncoding* typeEncoding);
// Returns (lazily creating) the per-isolate interop.escapeException brand
Expand Down
146 changes: 84 additions & 62 deletions NativeScript/runtime/ArgConverter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,16 @@ bool IsDeallocating(id obj) {
return !((BOOL (*)(id, SEL))imp)(obj, allowsWeakReference);
}

// Clang's init method family: optional leading underscores, then "init", then
// the end of the selector's first piece or anything but a lowercase letter.
bool IsInitFamily(SEL selector) {
// Clang's method family rule: optional leading underscores, then the family
// name, then the end of the selector's first piece or anything but a
// lowercase letter.
bool InMethodFamily(SEL selector, const char* family) {
const char* name = sel_getName(selector);
while (*name == '_') {
name++;
}
return strncmp(name, "init", 4) == 0 && (name[4] < 'a' || name[4] > 'z');
}

// The native caller of an init-family method owns its result. SetValue hands
// a wrapped object or a string over at +0: for the receiver that is the
// reference the caller passed in, any other object needs its own. The
// receiver is left alone: when a super initializer substituted it, that
// initializer already released it, and nothing here can tell that case apart.
void RetainInitializerResult(Isolate* isolate, void** argValues, void* retValue,
const TypeEncoding* typeEncoding, Local<Value> value) {
BinaryTypeEncodingType type = typeEncoding->type;
if (type != BinaryTypeEncodingType::IdEncoding &&
type != BinaryTypeEncodingType::InterfaceDeclarationReference &&
type != BinaryTypeEncodingType::InstanceTypeEncoding) {
return;
}

id returned = *static_cast<id*>(retValue);
if (returned == nil || returned == *static_cast<id*>(argValues[0]) ||
!IsInitFamily(*static_cast<SEL*>(argValues[1]))) {
return;
}

// A plain JS object becomes a DictionaryAdapter SetValue already returns
// at +1.
value = tns::UnwrapProxy(value);
BaseDataWrapper* wrapper = value->IsObject() ? tns::GetValue(isolate, value) : nullptr;
bool atPlusZero =
value->IsString() || (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject);
if (atPlusZero) {
[returned retain];
}
size_t length = strlen(family);
return strncmp(name, family, length) == 0 && (name[length] < 'a' || name[length] > 'z');
}

// The borrowed wrapper WrapNativeCallbackSelf made for an object in -dealloc
Expand Down Expand Up @@ -192,7 +163,7 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
target = [allocKlass alloc];
}

ObjCMethodCall methodCall(context, meta, target, klass, args, callSuper);
ObjCMethodCall methodCall(context, meta, target, klass, args, callSuper, allocKlass != nil);
return Interop::CallFunction(methodCall);
}

Expand Down Expand Up @@ -364,6 +335,11 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}

void ArgConverter::MethodCallback(ffi_cif* cif, void* retValue, void** argValues, void* userData) {
MethodCallbackWrapper* data = static_cast<MethodCallbackWrapper*>(userData);
// Owned by this call, which gives it up on every path unless it answers it.
id consumedArgument =
data->consumedArgumentIndex_ < 0
? nil
: *static_cast<id*>(argValues[data->initialParamIndex_ + data->consumedArgumentIndex_]);

Isolate* isolate = data->isolateWrapper_.Isolate();

Expand All @@ -380,6 +356,7 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
IsolatePin pin = data->isolateWrapper_.Pin();
if (!pin || !data->isolateWrapper_.IsValid()) {
memset(retValue, 0, cif->rtype->size);
[consumedArgument release];
return;
}

Expand All @@ -391,6 +368,7 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
// what makes this second look reliable.
if (!data->isolateWrapper_.IsValid()) {
memset(retValue, 0, cif->rtype->size);
[consumedArgument release];
return;
}
Isolate::Scope isolate_scope(isolate);
Expand Down Expand Up @@ -512,25 +490,65 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
if (!success) {
memset(retValue, 0, cif->rtype->size);
} else {
ArgConverter::SetValue(context, retValue, cif->rtype->size, result, data->typeEncoding_);
if (data->initialParamIndex_ == 2) {
RetainInitializerResult(isolate, argValues, retValue, data->typeEncoding_, result);
}
bool writtenUnowned = ArgConverter::SetValue(context, retValue, cif->rtype->size, result,
data->typeEncoding_);
ArgConverter::RetainOwnedResult(data->returnOwnership_, argValues, retValue, writtenUnowned,
consumedArgument);
}
} else {
memset(retValue, 0, cif->rtype->size);
}

// A wrapper JS made for the consumed argument holds a reference of its own.
if (consumedArgument != nil && !(Interop::IsObjectEncoding(data->typeEncoding_) &&
*static_cast<id*>(retValue) == consumedArgument)) {
[consumedArgument release];
}
} // all V8 scopes destruct here

if (pendingThrow != nil) {
@throw pendingThrow;
}
}

void ArgConverter::SetValue(Local<Context> context, void* retValue, size_t returnSize,
// An owned result SetValue wrote at +0 gets a reference of its own, except a
// receiver answered in place of itself: that is the reference the caller
// passed in. The receiver is never released when another object is answered
// instead: a super initializer that substituted it already did, and nothing
// here can tell that case apart.
void ArgConverter::RetainOwnedResult(NativeReturnOwnership ownership, void** argValues,
void* retValue, bool writtenUnowned, id consumedArgument) {
if (ownership == NativeReturnOwnership::Unowned || !writtenUnowned) {
return;
}

id returned = *static_cast<id*>(retValue);
if (returned == nil || returned == consumedArgument ||
(ownership == NativeReturnOwnership::ReplacesReceiver &&
returned == *static_cast<id*>(argValues[0]))) {
return;
}

[returned retain];
}

NativeReturnOwnership ArgConverter::ReturnOwnership(SEL selector, const MethodMeta* meta) {
bool isInitializer = meta != nullptr ? meta->isInitializer() : InMethodFamily(selector, "init");
if (Interop::ConsumesReceiver(selector, isInitializer)) {
return NativeReturnOwnership::ReplacesReceiver;
}

bool owned = meta != nullptr
? meta->ownsReturnedCocoaObject()
: InMethodFamily(selector, "copy") || InMethodFamily(selector, "mutableCopy") ||
InMethodFamily(selector, "new") || Interop::ReturnsRetained(selector);
return owned ? NativeReturnOwnership::Owned : NativeReturnOwnership::Unowned;
}

bool ArgConverter::SetValue(Local<Context> context, void* retValue, size_t returnSize,
Local<Value> value, const TypeEncoding* typeEncoding) {
if (typeEncoding->type == BinaryTypeEncodingType::VoidEncoding) {
return;
return false;
}

// Runs inside an ffi closure, where a C++ throw cannot propagate; a revoked
Expand All @@ -540,7 +558,7 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
value = tns::UnwrapProxy(value);
if (value.IsEmpty() || value->IsNullOrUndefined()) {
memset(retValue, 0, std::max(returnSize, sizeof(ffi_arg)));
return;
return false;
}

Isolate* isolate = v8::Isolate::GetCurrent();
Expand All @@ -551,52 +569,52 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
if (tns::IsBool(value)) {
bool boolValue = value.As<v8::Boolean>()->Value();
*(ffi_arg*)retValue = (bool)boolValue;
return;
return false;
} else if (tns::IsNumber(value)) {
double numValue = tns::ToNumber(isolate, value);
switch (type) {
case BinaryTypeEncodingType::UShortEncoding: {
*static_cast<unsigned short*>(retValue) = (unsigned short)numValue;
return;
return false;
}
case BinaryTypeEncodingType::ShortEncoding: {
*static_cast<short*>(retValue) = (short)numValue;
return;
return false;
}
case BinaryTypeEncodingType::UIntEncoding: {
*static_cast<unsigned int*>(retValue) = (unsigned int)numValue;
return;
return false;
}
case BinaryTypeEncodingType::IntEncoding: {
*static_cast<int*>(retValue) = (int)numValue;
return;
return false;
}
case BinaryTypeEncodingType::ULongEncoding: {
*static_cast<unsigned long*>(retValue) = (unsigned long)numValue;
return;
return false;
}
case BinaryTypeEncodingType::LongEncoding: {
*static_cast<long*>(retValue) = (long)numValue;
return;
return false;
}
case BinaryTypeEncodingType::ULongLongEncoding: {
*static_cast<unsigned long long*>(retValue) = (unsigned long long)numValue;
return;
return false;
}
case BinaryTypeEncodingType::LongLongEncoding: {
*static_cast<long long*>(retValue) = (long long)numValue;
return;
return false;
}
case BinaryTypeEncodingType::FloatEncoding: {
*static_cast<float*>(retValue) = (float)numValue;
return;
return false;
}
case BinaryTypeEncodingType::DoubleEncoding: {
*static_cast<double*>(retValue) = numValue;
return;
return false;
}
default:
return;
return false;
}
} else if (value->IsString()) {
if (type == BinaryTypeEncodingType::IdEncoding ||
Expand All @@ -607,7 +625,7 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
auto ref = CFBridgingRetain(data);
*(CFTypeRef*)retValue = ref;
CFRelease(ref);
return;
return true;
}
} else if (value->IsObject()) {
if (type == BinaryTypeEncodingType::InterfaceDeclarationReference ||
Expand All @@ -619,15 +637,15 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
id data = wrapper->Data();
memset(retValue, 0, sizeof(id));
*static_cast<id __strong*>(retValue) = data;
return;
return true;
} else {
id adapter = [[DictionaryAdapter alloc]
initWithJSObject:tns::ProxyReadSource(original, value).As<Object>()
isolate:isolate];
memset(retValue, 0, sizeof(id));
*static_cast<id __strong*>(retValue) = adapter;
// CFAutorelease(adapter);
return;
return false;
}
} else if (type == BinaryTypeEncodingType::StructDeclarationReference) {
BaseDataWrapper* baseWrapper = tns::GetValue(isolate, value);
Expand All @@ -638,19 +656,20 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
const StructMeta* structMeta = static_cast<const StructMeta*>(meta);
const StructInfo& structInfo = FFICall::GetStructInfo(structMeta);
Interop::InitializeStruct(context, retValue, structInfo.Fields(), value);
return;
return false;
} else if (baseWrapper->Type() == WrapperType::Struct) {
StructWrapper* structWrapper = static_cast<StructWrapper*>(baseWrapper);
size_t size = structWrapper->StructInfo().FFIType()->size;
void* data = structWrapper->Data();
memcpy(retValue, data, size);
return;
return false;
}
}
}

// TODO: Handle other return types, i.e. assign the retValue parameter from the v8 result
tns::Assert(false, isolate);
return false;
}

void ArgConverter::ConstructObject(Local<Context> context, const FunctionCallbackInfo<Value>& info,
Expand Down Expand Up @@ -703,13 +722,16 @@ explicit BorrowedSelfScope(Isolate* isolate) : isolate(isolate) {}
auto cache = Caches::Get(isolate);
auto poInstance = ArgConverter::FindCachedInstance(isolate, cache, result);
if (poInstance != nullptr) {
// Read before the release below: when it drops a DictionaryAdapter's last
// reference, the adapter's -dealloc resets this handle.
Local<Value> cached = poInstance->Get(isolate);
// An initializer that answered with an already wrapped object (a singleton,
// a tagged pointer, a cached cluster instance) leaves us holding a +1 the
// existing entry has no use for.
if (resultIsOwned) {
if (Interop::ReleasesCachedOwnedReturn(resultIsOwned, OwnedReturnReceiver::AllocatedForCall)) {
[result release];
}
info.GetReturnValue().Set(poInstance->Get(isolate));
info.GetReturnValue().Set(cached);
} else {
ObjCDataWrapper* wrapper = new ObjCDataWrapper(result);
Local<Object> thiz = info.This();
Expand Down
Loading
Loading