Skip to content
Open
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
63 changes: 63 additions & 0 deletions NativeScript/ffi/jni/jsi/metadata/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,56 @@ JsValue MetadataNode::SuperAccessorGetterCallback(JsRuntime &rt, const JsValue &
});
}

static constexpr int kDirectSignatureUnsupported = -2;
static constexpr int kDirectSignatureMismatch = -1;
static constexpr int kDirectSignatureUnknown = 0;
static constexpr int kDirectSignatureMatch = 1;

static bool ResolveDirectParameterKinds(const std::string &signature, std::string &kinds) {
kinds.clear();
if (signature.empty() || signature[0] != '(') return false;
static const std::string stringType = "Ljava/lang/String;";
size_t index = 1;
while (index < signature.size() && signature[index] != ')') {
const char type = signature[index];
if (type == 'L') {
if (signature.compare(index, stringType.size(), stringType) != 0) return false;
kinds.push_back('T');
index += stringType.size();
} else if (type == 'Z' || type == 'B' || type == 'C' || type == 'S' || type == 'I' ||
type == 'J' || type == 'F' || type == 'D') {
kinds.push_back(type);
index++;
} else {
return false;
}
}
return index < signature.size();
}

int MetadataNode::MatchDirectSignature(MethodCallbackData *data, const JsValue *args, size_t argc) {
if (data->cachedIsDirectSignature < 0) {
data->cachedIsDirectSignature =
ResolveDirectParameterKinds(data->candidates.front().getSig(), data->directParameterKinds) ? 1 : 0;
}
if (data->cachedIsDirectSignature == 0 || data->directParameterKinds.size() != argc) {
return kDirectSignatureUnsupported;
}
bool hasUnknownArgument = false;
for (size_t i = 0; i < argc; i++) {
const JsValue &arg = args[i];
if (arg.isObject()) return kDirectSignatureUnsupported;
if (arg.isNull() || arg.isUndefined()) {
hasUnknownArgument = true;
continue;
}
const char kind = data->directParameterKinds[i];
const bool compatible = kind == 'T' ? arg.isString() : kind == 'Z' ? arg.isBool() : arg.isNumber();
if (!compatible) return kDirectSignatureMismatch;
}
return hasUnknownArgument ? kDirectSignatureUnknown : kDirectSignatureMatch;
}

JsValue MetadataNode::MethodCallback(JsRuntime &rt, const JsValue &thisVal,
const JsValue *args, size_t argc,
MethodCallbackData *initialCallbackData) {
Expand All @@ -2211,6 +2261,7 @@ JsValue MetadataNode::MethodCallback(JsRuntime &rt, const JsValue &thisVal,

// Fast path for the overwhelmingly common single-overload, non-extension
// method with no parent chain: skip the candidate-search loop entirely.
bool singleCandidate = false;
if (callbackData->parent == nullptr &&
callbackData->candidates.size() == 1 &&
!first.isExtensionFunction &&
Expand All @@ -2219,6 +2270,7 @@ JsValue MetadataNode::MethodCallback(JsRuntime &rt, const JsValue &thisVal,
selectedCallbackData = callbackData;
arityCandidates.push_back(&first);
entry = &first;
singleCandidate = true;
}

while ((callbackData != nullptr) && (entry == nullptr)) {
Expand Down Expand Up @@ -2270,6 +2322,17 @@ JsValue MetadataNode::MethodCallback(JsRuntime &rt, const JsValue &thisVal,
Runtime::GetRuntime(rt)->GetObjectManager();
}

if (singleCandidate) {
const int match = MatchDirectSignature(initialCallbackData, args, argc);
if (match != kDirectSignatureUnsupported) {
return CallbackHandlers::CallJavaMethod(rt, thisVal, *className, methodName,
match == kDirectSignatureMismatch ? nullptr : entry,
isFromInterface, first.isStatic, false, args, argc,
initialCallbackData->objectManager,
match == kDirectSignatureMatch);
}
}

size_t metadataMatches = 0;
MetadataEntry *metadataMatch = nullptr;
bool metadataSignatureIsUnambiguous = false;
Expand Down
4 changes: 4 additions & 0 deletions NativeScript/ffi/jni/jsi/metadata/MetadataNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class MetadataNode {

explicit MetadataNode(MetadataTreeNode *treeNode);

static int MatchDirectSignature(MethodCallbackData *data, const JsValue *args, size_t argc);

void SetMissingBaseMethods(
JsRuntime &rt, const std::vector<MetadataTreeNode *> &skippedBaseTypes,
const std::vector<MethodCallbackData *> &instanceMethodData,
Expand Down Expand Up @@ -305,6 +307,8 @@ class MetadataNode {
// (-1 = not yet computed, 0 = false, 1 = true).
int8_t cachedIsFromInterface = -1;
int8_t cachedIsValueOf = -1;
int8_t cachedIsDirectSignature = -1;
std::string directParameterKinds;
// Cached per-runtime ObjectManager (this data is created per runtime, so
// the pointer's lifetime matches it — no staleness across runtimes).
tns::ObjectManager *objectManager = nullptr;
Expand Down
67 changes: 67 additions & 0 deletions NativeScript/ffi/jni/napi/metadata/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,60 @@ napi_value MetadataNode::SuperAccessorGetterCallback(napi_env env, napi_callback
return nullptr;
}

static constexpr int kDirectSignatureUnsupported = -2;
static constexpr int kDirectSignatureMismatch = -1;
static constexpr int kDirectSignatureUnknown = 0;
static constexpr int kDirectSignatureMatch = 1;

static bool ResolveDirectParameterKinds(const std::string &signature, std::string &kinds) {
kinds.clear();
if (signature.empty() || signature[0] != '(') return false;
static const std::string stringType = "Ljava/lang/String;";
size_t index = 1;
while (index < signature.size() && signature[index] != ')') {
const char type = signature[index];
if (type == 'L') {
if (signature.compare(index, stringType.size(), stringType) != 0) return false;
kinds.push_back('T');
index += stringType.size();
} else if (type == 'Z' || type == 'B' || type == 'C' || type == 'S' || type == 'I' ||
type == 'J' || type == 'F' || type == 'D') {
kinds.push_back(type);
index++;
} else {
return false;
}
}
return index < signature.size();
}

int MetadataNode::MatchDirectSignature(napi_env env, MethodCallbackData *data, size_t argc,
napi_value *argv) {
if (data->cachedIsDirectSignature < 0) {
data->cachedIsDirectSignature =
ResolveDirectParameterKinds(data->candidates.front().getSig(), data->directParameterKinds) ? 1 : 0;
}
if (data->cachedIsDirectSignature == 0 || data->directParameterKinds.size() != argc) {
return kDirectSignatureUnsupported;
}
bool hasUnknownArgument = false;
for (size_t i = 0; i < argc; i++) {
napi_valuetype type;
if (napi_typeof(env, argv[i], &type) != napi_ok || type == napi_object || type == napi_function) {
return kDirectSignatureUnsupported;
}
if (type == napi_null || type == napi_undefined) {
hasUnknownArgument = true;
continue;
}
const char kind = data->directParameterKinds[i];
const bool compatible = kind == 'T' ? type == napi_string
: kind == 'Z' ? type == napi_boolean : type == napi_number;
if (!compatible) return kDirectSignatureMismatch;
}
return hasUnknownArgument ? kDirectSignatureUnknown : kDirectSignatureMatch;
}

napi_value MetadataNode::MethodCallback(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN_VARGS_FAST(8)

Expand All @@ -2619,6 +2673,7 @@ napi_value MetadataNode::MethodCallback(napi_env env, napi_callback_info info) {

// Fast path for the overwhelmingly common single-overload, non-extension
// method with no parent chain: skip the candidate-search loop entirely.
bool singleCandidate = false;
if (callbackData->parent == nullptr &&
callbackData->candidates.size() == 1 &&
!first.isExtensionFunction &&
Expand All @@ -2627,6 +2682,7 @@ napi_value MetadataNode::MethodCallback(napi_env env, napi_callback_info info) {
selectedCallbackData = callbackData;
arityCandidates.push_back(&first);
entry = &first;
singleCandidate = true;
}

while ((callbackData != nullptr) && (entry == nullptr)) {
Expand Down Expand Up @@ -2678,6 +2734,17 @@ napi_value MetadataNode::MethodCallback(napi_env env, napi_callback_info info) {
Runtime::GetRuntime(env)->GetObjectManager();
}

if (singleCandidate) {
const int match = MatchDirectSignature(env, initialCallbackData, argc, argv);
if (match != kDirectSignatureUnsupported) {
return CallbackHandlers::CallJavaMethod(env, jsThis, *className, methodName,
match == kDirectSignatureMismatch ? nullptr : entry,
isFromInterface, first.isStatic, info, argc, argv,
initialCallbackData->objectManager,
match == kDirectSignatureMatch);
}
}


size_t metadataMatches = 0;
MetadataEntry *metadataMatch = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions NativeScript/ffi/jni/napi/metadata/MetadataNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class MetadataNode {

explicit MetadataNode(MetadataTreeNode *treeNode);

static int MatchDirectSignature(napi_env env, MethodCallbackData *data, size_t argc, napi_value *argv);

void SetMissingBaseMethods(
napi_env env, const std::vector<MetadataTreeNode *> &skippedBaseTypes,
const std::vector<MethodCallbackData *> &instanceMethodData,
Expand Down Expand Up @@ -282,6 +284,8 @@ class MetadataNode {
// (-1 = not yet computed, 0 = false, 1 = true).
int8_t cachedIsFromInterface = -1;
int8_t cachedIsValueOf = -1;
int8_t cachedIsDirectSignature = -1;
std::string directParameterKinds;
// Cached per-env ObjectManager (this data is created per env, so the
// pointer's lifetime matches it — no staleness across envs).
tns::ObjectManager *objectManager = nullptr;
Expand Down
Loading