@@ -7,149 +7,166 @@ using namespace facebook;
77using namespace facebook ::jsi;
88using 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+
1020Value 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
2636std::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
4353Value 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
105115enum class ReadState { needSize, needContent };
106116ReadState g_state = ReadState::needSize;
107117msgpack::unpacker unp;
108118
109119ShareValues 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
138148void 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}
0 commit comments