Skip to content

Commit 8833a8a

Browse files
authored
different jsi handling (keybase#25244)
1 parent 95099b1 commit 8833a8a

23 files changed

Lines changed: 791 additions & 707 deletions

File tree

‎rnmodules/react-native-kb/android/cpp-adapter.cpp‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ static jstring string2jstring(JNIEnv *env, const string &str) {
8989
}
9090

9191
void install(facebook::jsi::Runtime &jsiRuntime) {
92-
auto rpcOnGo = Function::createFromHostFunction(
93-
jsiRuntime, PropNameID::forAscii(jsiRuntime, "rpcOnGo"), 1,
94-
[](Runtime &runtime, const Value &thisValue, const Value *arguments,
95-
size_t count) -> Value {
92+
auto rpcOnGoWrap = [](Runtime &runtime, const Value &thisValue, const Value *arguments, size_t count) -> Value {
9693
return RpcOnGo(
9794
runtime, thisValue, arguments, count, [](void *ptr, size_t size) {
9895
JNIEnv *jniEnv = GetJniEnv();
@@ -105,8 +102,10 @@ void install(facebook::jsi::Runtime &jsiRuntime) {
105102
params[0].l = jba;
106103
jniEnv->CallVoidMethodA(java_object, rpcOnGo, params);
107104
});
108-
});
109-
jsiRuntime.global().setProperty(jsiRuntime, "rpcOnGo", move(rpcOnGo));
105+
};
106+
jsiRuntime.global().setProperty(jsiRuntime, "rpcOnGo",
107+
Function::createFromHostFunction(
108+
jsiRuntime, PropNameID::forAscii(jsiRuntime, "rpcOnGo"), 1, move(rpcOnGoWrap)));
110109
}
111110

112111
extern "C" JNIEXPORT void JNICALL

‎rnmodules/react-native-kb/android/src/legacy/KbModule.java‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,5 @@ public void engineReset() {
120120
public void engineStart() {
121121
this.impl.engineStart();
122122
}
123-
124-
@ReactMethod(isBlockingSynchronousMethod = true)
125-
public boolean installJSI() {
126-
return this.impl.installJSI();
127-
}
128-
129123
}
130124

‎rnmodules/react-native-kb/android/src/main/java/com/reactnativekb/KbModuleImpl.java‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ private String readGuiConfig() {
140140
}
141141

142142
public Map<String, Object> getConstants() {
143+
try {
144+
jsiInstalled = true;
145+
this.nativeInstallJSI(this.reactContext.getJavaScriptContextHolder().get());
146+
} catch (Exception exception) {
147+
NativeLogger.error("Exception in installJSI", exception);
148+
}
149+
143150
String versionCode = String.valueOf(getBuildConfigValue("VERSION_CODE"));
144151
String versionName = String.valueOf(getBuildConfigValue("VERSION_NAME"));
145152
boolean isDeviceSecure = false;
@@ -592,7 +599,10 @@ public void engineStart() {
592599
NativeLogger.info("KeybaseEngine started");
593600
try {
594601
started = true;
595-
602+
if (executor == null) {
603+
executor = Executors.newSingleThreadExecutor();
604+
executor.execute(new ReadFromKBLib(this.reactContext));
605+
}
596606
} catch (Exception e) {
597607
NativeLogger.error("Exception in engineStart", e);
598608
}
@@ -674,21 +684,6 @@ public void destroy() {
674684
}
675685
}
676686

677-
public boolean installJSI() {
678-
jsiInstalled = true;
679-
try {
680-
this.nativeInstallJSI(this.reactContext.getJavaScriptContextHolder().get());
681-
if (executor == null) {
682-
executor = Executors.newSingleThreadExecutor();
683-
executor.execute(new ReadFromKBLib(this.reactContext));
684-
}
685-
return true;
686-
} catch (Exception exception) {
687-
NativeLogger.error("Exception in installJSI", exception);
688-
return false;
689-
}
690-
}
691-
692687
public void rpcOnGo(byte[] arr) {
693688
try {
694689
writeArr(arr);

‎rnmodules/react-native-kb/android/src/turbo/KbModule.java‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ public void engineReset() {
139139
public void engineStart() {
140140
this.impl.engineStart();
141141
}
142-
143-
@Override
144-
@ReactMethod(isBlockingSynchronousMethod = true)
145-
public boolean installJSI() {
146-
return this.impl.installJSI();
147-
}
148142
}
149143

150144

‎rnmodules/react-native-kb/cpp/rpc.cpp‎

Lines changed: 139 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -7,149 +7,166 @@ using namespace facebook;
77
using namespace facebook::jsi;
88
using namespace std;
99

10+
std::atomic<bool> isTowndown{false};
11+
12+
void Teardown() {
13+
isTowndown.store(true);
14+
}
15+
16+
void Tearup() {
17+
isTowndown.store(false);
18+
}
19+
1020
Value RpcOnGo(Runtime &runtime, const Value &thisValue, const Value *arguments,
1121
size_t count, void (*callback)(void *ptr, size_t size)) {
12-
try {
13-
auto obj = arguments[0].asObject(runtime);
14-
auto buffer = obj.getArrayBuffer(runtime);
15-
auto ptr = buffer.data(runtime);
16-
auto size = buffer.size(runtime);
17-
callback(ptr, size);
18-
return Value(true);
19-
} catch (const std::exception &e) {
20-
throw new std::runtime_error("Error in RpcOnGo: " + string(e.what()));
21-
} catch (...) {
22-
throw new std::runtime_error("Unknown error in RpcOnGo");
23-
}
22+
try {
23+
auto obj = arguments[0].asObject(runtime);
24+
auto buffer = obj.getArrayBuffer(runtime);
25+
auto ptr = buffer.data(runtime);
26+
auto size = buffer.size(runtime);
27+
callback(ptr, size);
28+
return Value(true);
29+
} catch (const std::exception &e) {
30+
throw new std::runtime_error("Error in RpcOnGo: " + string(e.what()));
31+
} catch (...) {
32+
throw new std::runtime_error("Unknown error in RpcOnGo");
33+
}
2434
}
2535

2636
std::string mpToString(msgpack::object &o) {
27-
switch (o.type) {
28-
case msgpack::type::STR:
29-
return o.as<std::string>();
30-
case msgpack::type::POSITIVE_INTEGER:
31-
return std::to_string(o.as<unsigned int>());
32-
case msgpack::type::NEGATIVE_INTEGER:
33-
return std::to_string(o.as<int>());
34-
case msgpack::type::FLOAT32:
35-
return std::to_string(o.as<double>());
36-
case msgpack::type::FLOAT64:
37-
return std::to_string(o.as<double>());
38-
default:
39-
throw new std::runtime_error("Invalid map key");
40-
}
37+
switch (o.type) {
38+
case msgpack::type::STR:
39+
return o.as<std::string>();
40+
case msgpack::type::POSITIVE_INTEGER:
41+
return std::to_string(o.as<unsigned int>());
42+
case msgpack::type::NEGATIVE_INTEGER:
43+
return std::to_string(o.as<int>());
44+
case msgpack::type::FLOAT32:
45+
return std::to_string(o.as<double>());
46+
case msgpack::type::FLOAT64:
47+
return std::to_string(o.as<double>());
48+
default:
49+
throw new std::runtime_error("Invalid map key");
50+
}
4151
}
4252

4353
Value convertMPToJSI(Runtime &runtime, msgpack::object &o) {
44-
switch (o.type) {
45-
case msgpack::type::STR:
46-
return jsi::String::createFromUtf8(runtime, o.as<std::string>());
47-
case msgpack::type::POSITIVE_INTEGER:
48-
return jsi::Value(o.as<double>());
49-
case msgpack::type::NEGATIVE_INTEGER:
50-
return jsi::Value(o.as<int>());
51-
case msgpack::type::FLOAT32:
52-
return jsi::Value(o.as<double>());
53-
case msgpack::type::FLOAT64:
54-
return jsi::Value(o.as<double>());
55-
case msgpack::type::BOOLEAN:
56-
return jsi::Value(o.as<bool>());
57-
case msgpack::type::NIL:
58-
return jsi::Value::null();
59-
case msgpack::type::EXT:
60-
return jsi::Value::undefined();
61-
case msgpack::type::MAP: {
62-
jsi::Object obj = jsi::Object(runtime);
63-
auto *p = o.via.map.ptr;
64-
auto *const pend = o.via.map.ptr + o.via.map.size;
65-
for (; p < pend; ++p) {
66-
auto key = mpToString(p->key);
67-
auto val = convertMPToJSI(runtime, p->val);
68-
obj.setProperty(runtime, key.c_str(), val);
69-
}
70-
return obj;
71-
}
72-
case msgpack::type::BIN: {
73-
auto ptr = o.via.bin.ptr;
74-
int size = o.via.bin.size;
75-
// make ArrayBuffer and copy in data
76-
Function arrayBufferCtor =
77-
runtime.global().getPropertyAsFunction(runtime, "ArrayBuffer");
78-
Value ab = arrayBufferCtor.callAsConstructor(runtime, size);
79-
Object abo = ab.getObject(runtime);
80-
ArrayBuffer abbuf = abo.getArrayBuffer(runtime);
81-
std::copy(ptr, ptr + size, abbuf.data(runtime));
82-
83-
// Wrap in Buffer like framed-msg-pack
84-
Object bufObj = runtime.global().getPropertyAsObject(runtime, "Buffer");
85-
Function bufFrom = bufObj.getPropertyAsFunction(runtime, "from");
86-
Value buf = bufFrom.callWithThis(
87-
runtime, bufObj,
88-
std::move(ab)); // Buffer shares the memory and just wraps
89-
return buf;
90-
}
91-
case msgpack::type::ARRAY: {
92-
auto size = o.via.array.size;
93-
jsi::Array arr(runtime, size);
94-
for (int i = 0; i < size; ++i) {
95-
arr.setValueAtIndex(runtime, i,
96-
convertMPToJSI(runtime, o.via.array.ptr[i]));
54+
switch (o.type) {
55+
case msgpack::type::STR:
56+
return jsi::String::createFromUtf8(runtime, o.as<std::string>());
57+
case msgpack::type::POSITIVE_INTEGER:
58+
return jsi::Value(o.as<double>());
59+
case msgpack::type::NEGATIVE_INTEGER:
60+
return jsi::Value(o.as<int>());
61+
case msgpack::type::FLOAT32:
62+
return jsi::Value(o.as<double>());
63+
case msgpack::type::FLOAT64:
64+
return jsi::Value(o.as<double>());
65+
case msgpack::type::BOOLEAN:
66+
return jsi::Value(o.as<bool>());
67+
case msgpack::type::NIL:
68+
return jsi::Value::null();
69+
case msgpack::type::EXT:
70+
return jsi::Value::undefined();
71+
case msgpack::type::MAP: {
72+
jsi::Object obj = jsi::Object(runtime);
73+
auto *p = o.via.map.ptr;
74+
auto *const pend = o.via.map.ptr + o.via.map.size;
75+
for (; p < pend; ++p) {
76+
auto key = mpToString(p->key);
77+
auto val = convertMPToJSI(runtime, p->val);
78+
obj.setProperty(runtime, key.c_str(), val);
79+
}
80+
return obj;
81+
}
82+
case msgpack::type::BIN: {
83+
auto ptr = o.via.bin.ptr;
84+
int size = o.via.bin.size;
85+
// make ArrayBuffer and copy in data
86+
Function arrayBufferCtor =
87+
runtime.global().getPropertyAsFunction(runtime, "ArrayBuffer");
88+
Value ab = arrayBufferCtor.callAsConstructor(runtime, size);
89+
Object abo = ab.getObject(runtime);
90+
ArrayBuffer abbuf = abo.getArrayBuffer(runtime);
91+
std::copy(ptr, ptr + size, abbuf.data(runtime));
92+
93+
// Wrap in Buffer like framed-msg-pack
94+
Object bufObj = runtime.global().getPropertyAsObject(runtime, "Buffer");
95+
Function bufFrom = bufObj.getPropertyAsFunction(runtime, "from");
96+
Value buf = bufFrom.callWithThis(
97+
runtime, bufObj,
98+
std::move(ab)); // Buffer shares the memory and just wraps
99+
return buf;
100+
}
101+
case msgpack::type::ARRAY: {
102+
auto size = o.via.array.size;
103+
jsi::Array arr(runtime, size);
104+
for (int i = 0; i < size; ++i) {
105+
arr.setValueAtIndex(runtime, i,
106+
convertMPToJSI(runtime, o.via.array.ptr[i]));
107+
}
108+
return arr;
109+
}
110+
default:
111+
return jsi::Value::undefined();
97112
}
98-
return arr;
99-
}
100-
default:
101-
return jsi::Value::undefined();
102-
}
103113
}
104114

105115
enum class ReadState { needSize, needContent };
106116
ReadState g_state = ReadState::needSize;
107117
msgpack::unpacker unp;
108118

109119
ShareValues PrepRpcOnJS(Runtime &runtime, uint8_t *data, int size) {
110-
try {
111-
auto values = make_shared<std::vector<msgpack::object_handle>>();
112-
if (size > 0) {
113-
unp.reserve_buffer(size);
114-
std::copy(data, data + size, unp.buffer());
115-
unp.buffer_consumed(size);
116-
while (true) {
117-
msgpack::object_handle result;
118-
if (unp.next(result)) {
119-
if (g_state == ReadState::needSize) {
120-
g_state = ReadState::needContent;
121-
} else {
122-
values->push_back(move(result));
123-
g_state = ReadState::needSize;
124-
}
125-
} else {
126-
break;
120+
try {
121+
auto values = make_shared<std::vector<msgpack::object_handle>>();
122+
if (size > 0) {
123+
unp.reserve_buffer(size);
124+
std::copy(data, data + size, unp.buffer());
125+
unp.buffer_consumed(size);
126+
while (true) {
127+
msgpack::object_handle result;
128+
if (unp.next(result)) {
129+
if (g_state == ReadState::needSize) {
130+
g_state = ReadState::needContent;
131+
} else {
132+
values->push_back(move(result));
133+
g_state = ReadState::needSize;
134+
}
135+
} else {
136+
break;
137+
}
138+
}
127139
}
128-
}
140+
return values;
141+
} catch (const std::exception &e) {
142+
throw new std::runtime_error("Error in PrepRpcOnJS: " + string(e.what()));
143+
} catch (...) {
144+
throw new std::runtime_error("Unknown error in PrepRpcOnJS");
129145
}
130-
return values;
131-
} catch (const std::exception &e) {
132-
throw new std::runtime_error("Error in PrepRpcOnJS: " + string(e.what()));
133-
} catch (...) {
134-
throw new std::runtime_error("Unknown error in PrepRpcOnJS");
135-
}
136146
}
137147

138148
void RpcOnJS(Runtime &runtime, ShareValues values,
139149
void (*err_callback)(const std::string &err)) {
140-
try {
141-
Function rpcOnJs =
142-
runtime.global().getPropertyAsFunction(runtime, "rpcOnJs");
143-
for (auto &result : *values) {
144-
msgpack::object obj(result.get());
145-
Value value = convertMPToJSI(runtime, obj);
146-
rpcOnJs.call(runtime, move(value), 1);
150+
try {
151+
if (isTowndown.load()) {
152+
return;
153+
}
154+
155+
for (auto &result : *values) {
156+
msgpack::object obj(result.get());
157+
Value value = convertMPToJSI(runtime, obj);
158+
if (isTowndown.load()) {
159+
return;
160+
}
161+
Function rpcOnJs =
162+
runtime.global().getPropertyAsFunction(runtime, "rpcOnJs");
163+
rpcOnJs.call(runtime, move(value), 1);
164+
}
165+
} catch (const std::exception &e) {
166+
err_callback(e.what());
167+
throw new std::runtime_error("Error in RpcOnJS: " + string(e.what()));
168+
} catch (...) {
169+
err_callback("unknown error");
170+
throw new std::runtime_error("Unknown error in RpcOnJS");
147171
}
148-
} catch (const std::exception &e) {
149-
err_callback(e.what());
150-
throw new std::runtime_error("Error in RpcOnJS: " + string(e.what()));
151-
} catch (...) {
152-
err_callback("unknown error");
153-
throw new std::runtime_error("Unknown error in RpcOnJS");
154-
}
155172
}

‎rnmodules/react-native-kb/cpp/rpc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ facebook::jsi::Value RpcOnGo(facebook::jsi::Runtime &runtime,
1414
typedef std::shared_ptr<std::vector<msgpack::object_handle>> ShareValues;
1515
ShareValues PrepRpcOnJS(facebook::jsi::Runtime &runtime, uint8_t *data, int size);
1616
void RpcOnJS(facebook::jsi::Runtime &runtime, ShareValues values, void (*err_callback)(const std::string & err));
17+
void Teardown();
18+
void Tearup();

0 commit comments

Comments
 (0)