Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9982b5f

Browse files
authored
Use new Dart_PortEx api to ensure SendPort can be rebuild properly. (#52498)
Currently used Dart API can not be reliably used to rebuild dart SendPort object, [new API was introduced in dart sdk ](https://dart.googlesource.com/sdk/+/de4029ee1ba3bc89d839ae7d2fd0706d29abae58) and this PR moves flutter engine to use that new API. BUG: flutter/flutter#147469
1 parent e1126e5 commit 9982b5f

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

lib/ui/isolate_name_server/isolate_name_server.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,25 @@ IsolateNameServer::IsolateNameServer() {}
1010

1111
IsolateNameServer::~IsolateNameServer() = default;
1212

13-
Dart_Port IsolateNameServer::LookupIsolatePortByName(const std::string& name) {
13+
Dart_PortEx IsolateNameServer::LookupIsolatePortByName(
14+
const std::string& name) {
1415
std::scoped_lock lock(mutex_);
1516
return LookupIsolatePortByNameUnprotected(name);
1617
}
1718

18-
Dart_Port IsolateNameServer::LookupIsolatePortByNameUnprotected(
19+
Dart_PortEx IsolateNameServer::LookupIsolatePortByNameUnprotected(
1920
const std::string& name) {
2021
auto port_iterator = port_mapping_.find(name);
2122
if (port_iterator != port_mapping_.end()) {
2223
return port_iterator->second;
2324
}
24-
return ILLEGAL_PORT;
25+
return {ILLEGAL_PORT, ILLEGAL_PORT};
2526
}
2627

27-
bool IsolateNameServer::RegisterIsolatePortWithName(Dart_Port port,
28+
bool IsolateNameServer::RegisterIsolatePortWithName(Dart_PortEx port,
2829
const std::string& name) {
2930
std::scoped_lock lock(mutex_);
30-
if (LookupIsolatePortByNameUnprotected(name) != ILLEGAL_PORT) {
31+
if (LookupIsolatePortByNameUnprotected(name).port_id != ILLEGAL_PORT) {
3132
// Name is already registered.
3233
return false;
3334
}

lib/ui/isolate_name_server/isolate_name_server.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ class IsolateNameServer {
2222

2323
// Looks up the Dart_Port associated with a given name. Returns ILLEGAL_PORT
2424
// if the name does not exist.
25-
Dart_Port LookupIsolatePortByName(const std::string& name);
25+
Dart_PortEx LookupIsolatePortByName(const std::string& name);
2626

2727
// Registers a Dart_Port with a given name. Returns true if registration is
2828
// successful, false if the name entry already exists.
29-
bool RegisterIsolatePortWithName(Dart_Port port, const std::string& name);
29+
bool RegisterIsolatePortWithName(Dart_PortEx port, const std::string& name);
3030

3131
// Removes a name to Dart_Port mapping given a name. Returns true if the
3232
// mapping was successfully removed, false if the mapping does not exist.
3333
bool RemoveIsolateNameMapping(const std::string& name);
3434

3535
private:
36-
Dart_Port LookupIsolatePortByNameUnprotected(const std::string& name);
36+
Dart_PortEx LookupIsolatePortByNameUnprotected(const std::string& name);
3737

3838
mutable std::mutex mutex_;
39-
std::map<std::string, Dart_Port> port_mapping_;
39+
std::map<std::string, Dart_PortEx> port_mapping_;
4040

4141
FML_DISALLOW_COPY_AND_ASSIGN(IsolateNameServer);
4242
};

lib/ui/isolate_name_server/isolate_name_server_natives.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Dart_Handle IsolateNameServerNatives::LookupPortByName(
1919
if (!name_server) {
2020
return Dart_Null();
2121
}
22-
Dart_Port port = name_server->LookupIsolatePortByName(name);
23-
if (port == ILLEGAL_PORT) {
22+
Dart_PortEx port = name_server->LookupIsolatePortByName(name);
23+
if (port.port_id == ILLEGAL_PORT) {
2424
return Dart_Null();
2525
}
26-
return Dart_NewSendPort(port);
26+
return Dart_NewSendPortEx(port);
2727
}
2828

2929
bool IsolateNameServerNatives::RegisterPortWithName(Dart_Handle port_handle,
@@ -32,8 +32,8 @@ bool IsolateNameServerNatives::RegisterPortWithName(Dart_Handle port_handle,
3232
if (!name_server) {
3333
return false;
3434
}
35-
Dart_Port port = ILLEGAL_PORT;
36-
Dart_SendPortGetId(port_handle, &port);
35+
Dart_PortEx port;
36+
Dart_SendPortGetIdEx(port_handle, &port);
3737
if (!name_server->RegisterIsolatePortWithName(port, name)) {
3838
return false;
3939
}

runtime/dart_vm_unittests.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ TEST_F(DartVMTest, SimpleIsolateNameServer) {
2525
ASSERT_TRUE(vm);
2626
ASSERT_TRUE(vm.GetVMData());
2727
auto ns = vm->GetIsolateNameServer();
28-
ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), ILLEGAL_PORT);
28+
ASSERT_EQ(ns->LookupIsolatePortByName("foobar").port_id, ILLEGAL_PORT);
2929
ASSERT_FALSE(ns->RemoveIsolateNameMapping("foobar"));
30-
ASSERT_TRUE(ns->RegisterIsolatePortWithName(123, "foobar"));
31-
ASSERT_FALSE(ns->RegisterIsolatePortWithName(123, "foobar"));
32-
ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), 123);
30+
Dart_PortEx correct_portex = {123, 456};
31+
ASSERT_TRUE(ns->RegisterIsolatePortWithName(correct_portex, "foobar"));
32+
ASSERT_FALSE(ns->RegisterIsolatePortWithName(correct_portex, "foobar"));
33+
Dart_PortEx response = ns->LookupIsolatePortByName("foobar");
34+
ASSERT_EQ(response.port_id, correct_portex.port_id);
35+
ASSERT_EQ(response.origin_id, correct_portex.origin_id);
3336
ASSERT_TRUE(ns->RemoveIsolateNameMapping("foobar"));
3437
}
3538

0 commit comments

Comments
 (0)