Skip to content

Commit 9dfae65

Browse files
ptomatoedusperoni
authored andcommitted
fix: Don't iterate properties in GetImplementedInterfaces
In CallbackHandlers::GetImplementedInterfaces, we were iterating over all properties of the implementation object (including `super`, which was problematic for the same reasons as given in the previous commit) but discarding all of them except `interfaces`. Instead, we can just get the `interfaces` property if it exists, and use it directly. A few tweaks in looping over the JS array, as well: we can use String::NewFromUtf8Literal() to get the name of the `length` property, we can skip converting the value to an Object before converting it to a Number (this is equivalent to doing `+arr.length` instead of `+Object(arr.length)`), and we don't have to explicitly check length > 0 because the loop won't execute if it's 0.
1 parent d8b8bc0 commit 9dfae65

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

test-app/runtime/src/main/cpp/CallbackHandlers.cpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -570,40 +570,25 @@ CallbackHandlers::GetImplementedInterfaces(JEnv &env, const Local<Object> &imple
570570
vector<jstring> interfacesToImplement;
571571
auto isolate = implementationObject->GetIsolate();
572572
auto context = implementationObject->CreationContext();
573-
auto propNames = implementationObject->GetOwnPropertyNames(context).ToLocalChecked();
574-
for (int i = 0; i < propNames->Length(); i++) {
575-
auto name = propNames->Get(context, i).ToLocalChecked().As<String>();
576-
auto prop = implementationObject->Get(context, name).ToLocalChecked();
577-
578-
bool arrFound = !prop.IsEmpty() && prop->IsArray();
573+
Local<String> interfacesName = String::NewFromUtf8Literal(isolate, "interfaces");
574+
Local<Value> prop;
575+
if (implementationObject->Get(context, interfacesName).ToLocal(&prop) && !prop.IsEmpty() && prop->IsArray()) {
576+
auto interfacesArr = prop->ToObject(context).ToLocalChecked();
579577

580-
if (arrFound) {
581-
v8::String::Utf8Value propName(isolate, name);
582-
std::string arrNameC = std::string(*propName);
583-
if (arrNameC == "interfaces") {
584-
auto interfacesArr = prop->ToObject(context).ToLocalChecked();
578+
Local<String> lengthName = String::NewFromUtf8Literal(isolate, "length");
579+
unsigned length = interfacesArr->Get(context,lengthName).ToLocalChecked()
580+
->Uint32Value(context).ToChecked();
585581

586-
int length = interfacesArr->Get(
587-
context,
588-
v8::String::NewFromUtf8(isolate,
589-
"length").ToLocalChecked()).ToLocalChecked()->ToObject(
590-
context).ToLocalChecked()->Uint32Value(
591-
context).ToChecked();
582+
for (int j = 0; j < length; j++) {
583+
auto element = interfacesArr->Get(context, j).ToLocalChecked();
592584

593-
if (length > 0) {
594-
for (int j = 0; j < length; j++) {
595-
auto element = interfacesArr->Get(context, j).ToLocalChecked();
585+
if (element->IsFunction()) {
586+
auto node = MetadataNode::GetTypeMetadataName(isolate, element);
596587

597-
if (element->IsFunction()) {
598-
auto node = MetadataNode::GetTypeMetadataName(isolate, element);
588+
node = Util::ReplaceAll(node, std::string("/"), std::string("."));
599589

600-
node = Util::ReplaceAll(node, std::string("/"), std::string("."));
601-
602-
jstring value = env.NewStringUTF(node.c_str());
603-
interfacesToImplement.push_back(value);
604-
}
605-
}
606-
}
590+
jstring value = env.NewStringUTF(node.c_str());
591+
interfacesToImplement.push_back(value);
607592
}
608593
}
609594
}

0 commit comments

Comments
 (0)