Skip to content

Commit

Permalink
fix: Don't iterate properties in GetImplementedInterfaces
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptomato authored and edusperoni committed Jun 23, 2023
1 parent d8b8bc0 commit 9dfae65
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions test-app/runtime/src/main/cpp/CallbackHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,40 +570,25 @@ CallbackHandlers::GetImplementedInterfaces(JEnv &env, const Local<Object> &imple
vector<jstring> interfacesToImplement;
auto isolate = implementationObject->GetIsolate();
auto context = implementationObject->CreationContext();
auto propNames = implementationObject->GetOwnPropertyNames(context).ToLocalChecked();
for (int i = 0; i < propNames->Length(); i++) {
auto name = propNames->Get(context, i).ToLocalChecked().As<String>();
auto prop = implementationObject->Get(context, name).ToLocalChecked();

bool arrFound = !prop.IsEmpty() && prop->IsArray();
Local<String> interfacesName = String::NewFromUtf8Literal(isolate, "interfaces");
Local<Value> prop;
if (implementationObject->Get(context, interfacesName).ToLocal(&prop) && !prop.IsEmpty() && prop->IsArray()) {
auto interfacesArr = prop->ToObject(context).ToLocalChecked();

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

int length = interfacesArr->Get(
context,
v8::String::NewFromUtf8(isolate,
"length").ToLocalChecked()).ToLocalChecked()->ToObject(
context).ToLocalChecked()->Uint32Value(
context).ToChecked();
for (int j = 0; j < length; j++) {
auto element = interfacesArr->Get(context, j).ToLocalChecked();

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

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

node = Util::ReplaceAll(node, std::string("/"), std::string("."));

jstring value = env.NewStringUTF(node.c_str());
interfacesToImplement.push_back(value);
}
}
}
jstring value = env.NewStringUTF(node.c_str());
interfacesToImplement.push_back(value);
}
}
}
Expand Down

0 comments on commit 9dfae65

Please sign in to comment.