@@ -19,8 +19,6 @@ static bool mp_reader(cmp_ctx_t *ctx, void *data, size_t limit);
1919jsi::Value readObject (cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length);
2020jsi::Value readArray (cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length);
2121
22-
23-
2422void writeValue (cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Value &value)
2523{
2624 if (value.isBool ())
@@ -37,14 +35,14 @@ void writeValue(cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Value &value)
3735
3836 if (value.isString ())
3937 {
40- auto v = value.getString (rt).utf8 (rt);
38+ std::string const & v = value.getString (rt).utf8 (rt);
4139 cmp_write_str (ctx, v.c_str (), v.size ());
4240 return ;
4341 }
4442
4543 if (value.isObject ())
4644 {
47- auto object = std::move ( value.getObject (rt) );
45+ const auto & object = value.getObject (rt);
4846 if (object.isArray (rt))
4947 {
5048 writeArray (ctx, rt, object);
@@ -63,23 +61,32 @@ void writeValue(cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Value &value)
6361
6462void writeArray (cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Object &object)
6563{
66- cmp_write_array (ctx, object.getPropertyNames (rt).size (rt));
67- auto array = object.asArray (rt);
68- for (size_t i = 0 ; i < array.size (rt); i++)
64+ const auto &array = object.getArray (rt);
65+ const size_t length = array.length (rt);
66+ cmp_write_array (ctx, length);
67+
68+ for (size_t i = 0 ; i < length; i++)
6969 {
70- writeValue (ctx, rt, std::move (array.getValueAtIndex (rt, i)));
70+ const jsi::Value &value = array.getValueAtIndex (rt, i);
71+ writeValue (ctx, rt, value);
7172 }
7273}
7374
7475void writeObject (cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Object &object)
7576{
76- cmp_write_map (ctx, object.getPropertyNames (rt).size (rt));
77- auto names = object.getPropertyNames (rt);
77+ const auto &names = object.getPropertyNames (rt);
78+ cmp_write_map (ctx, names.size (rt));
79+
7880 for (size_t i = 0 ; i < names.size (rt); i++)
7981 {
80- auto name = names.getValueAtIndex (rt, i).asString (rt);
81- cmp_write_str (ctx, name.utf8 (rt).c_str (), name.utf8 (rt).size ());
82- writeValue (ctx, rt, (object.getProperty (rt, name)));
82+ const auto &name = names.getValueAtIndex (rt, i).asString (rt);
83+ const auto &value = object.getProperty (rt, name);
84+
85+ const auto &name_str = name.utf8 (rt);
86+ const auto name_len = name_str.size ();
87+
88+ cmp_write_str (ctx, name_str.c_str (), name_len);
89+ writeValue (ctx, rt, std::move (value));
8390 }
8491}
8592
@@ -94,7 +101,9 @@ struct MessagePackWriter
94101
95102 size_t write (void *data, size_t count)
96103 {
97- this ->data .insert (this ->data .end (), (uint8_t *)data, (uint8_t *)data + count);
104+ size_t start = this ->data .size ();
105+ this ->data .resize (start + count);
106+ std::memcpy (this ->data .data () + start, data, count);
98107 return count;
99108 }
100109};
@@ -107,16 +116,14 @@ static size_t mp_writer(cmp_ctx_t *ctx, const void *data, size_t count)
107116
108117std::vector<uint8_t > write (jsi::Runtime &rt, const jsi::Value &value)
109118{
110- std::vector<uint8_t > data;
111- MessagePackWriter writer;
112- std::unique_ptr<cmp_ctx_s> ctx (new cmp_ctx_s);
119+ MessagePackWriter writer;
120+ std::unique_ptr<cmp_ctx_s> ctx (new cmp_ctx_s);
113121 cmp_init (ctx.get (), &writer, mp_reader, NULL , mp_writer);
114122 writeValue (ctx.get (), rt, value);
115123 return std::move (writer.data );
116124}
117125
118-
119- jsi::Value readValue (cmp_ctx_t * ctx, jsi::Runtime &rt)
126+ jsi::Value readValue (cmp_ctx_t *ctx, jsi::Runtime &rt)
120127{
121128 cmp_object_t obj;
122129 if (!cmp_read_object (ctx, &obj))
@@ -170,29 +177,29 @@ jsi::Value readValue(cmp_ctx_t * ctx, jsi::Runtime &rt)
170177 }
171178
172179 case CMP_TYPE_POSITIVE_FIXNUM :
173- return jsi::Value ((double ) obj.as .u8 );
180+ return jsi::Value ((double )obj.as .u8 );
174181 case CMP_TYPE_NEGATIVE_FIXNUM :
175- return jsi::Value ((double ) obj.as .s8 );
182+ return jsi::Value ((double )obj.as .s8 );
176183 case CMP_TYPE_FLOAT :
177- return jsi::Value ((double ) obj.as .flt );
184+ return jsi::Value ((double )obj.as .flt );
178185 case CMP_TYPE_DOUBLE :
179- return jsi::Value ((double ) obj.as .dbl );
186+ return jsi::Value ((double )obj.as .dbl );
180187 case CMP_TYPE_UINT8 :
181- return jsi::Value ((double ) obj.as .u8 );
188+ return jsi::Value ((double )obj.as .u8 );
182189 case CMP_TYPE_UINT16 :
183- return jsi::Value ((double ) obj.as .u16 );
190+ return jsi::Value ((double )obj.as .u16 );
184191 case CMP_TYPE_UINT32 :
185- return jsi::Value ((double ) obj.as .u32 );
192+ return jsi::Value ((double )obj.as .u32 );
186193 case CMP_TYPE_UINT64 :
187- return jsi::Value ((double ) obj.as .u64 );
194+ return jsi::Value ((double )obj.as .u64 );
188195 case CMP_TYPE_SINT8 :
189- return jsi::Value ((double ) obj.as .s8 );
196+ return jsi::Value ((double )obj.as .s8 );
190197 case CMP_TYPE_SINT16 :
191- return jsi::Value ((double ) obj.as .s16 );
198+ return jsi::Value ((double )obj.as .s16 );
192199 case CMP_TYPE_SINT32 :
193- return jsi::Value ((double ) obj.as .s32 );
200+ return jsi::Value ((double )obj.as .s32 );
194201 case CMP_TYPE_SINT64 :
195- return jsi::Value ((double ) obj.as .s64 );
202+ return jsi::Value ((double )obj.as .s64 );
196203
197204 default :
198205 return jsi::Value (jsi::Value::null ());
@@ -206,16 +213,25 @@ jsi::Value readObject(cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length)
206213 {
207214 auto key = readValue (ctx, rt).toString (rt);
208215 auto value = readValue (ctx, rt);
209- if (value.isString ()) {
210- result.setProperty (rt, key, value.getString (rt));
211- } else if (value.isNumber ()) {
212- result.setProperty (rt, key, value.getNumber ());
213- } else if (value.isBool ()) {
214- result.setProperty (rt, key, value.getBool ());
215- } else if (value.isNull () || value.isUndefined ()) {
216- result.setProperty (rt, key, value.null ());
217- } else if (value.isObject ()) {
218- result.setProperty (rt, key, value.getObject (rt));
216+ if (value.isString ())
217+ {
218+ result.setProperty (rt, key, value.getString (rt));
219+ }
220+ else if (value.isNumber ())
221+ {
222+ result.setProperty (rt, key, value.getNumber ());
223+ }
224+ else if (value.isBool ())
225+ {
226+ result.setProperty (rt, key, value.getBool ());
227+ }
228+ else if (value.isNull () || value.isUndefined ())
229+ {
230+ result.setProperty (rt, key, value.null ());
231+ }
232+ else if (value.isObject ())
233+ {
234+ result.setProperty (rt, key, value.getObject (rt));
219235 }
220236 }
221237 return result;
@@ -232,55 +248,55 @@ jsi::Value readArray(cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length)
232248 return result;
233249}
234250
235-
236- class MessagePackReader {
251+ class MessagePackReader
252+ {
237253public:
238- MessagePackReader (const char * data, size_t length) {
239- this ->data = new char [length];
240- std::memcpy (this ->data , data, length);
241- this ->size = length;
242- index = 0 ;
243- }
254+ MessagePackReader (const char *data, size_t length)
255+ {
256+ this ->data = new char [length];
257+ std::memcpy (this ->data , data, length);
258+ this ->size = length;
259+ index = 0 ;
260+ }
244261
245- ~MessagePackReader () {
246- delete[] data;
247- }
262+ ~MessagePackReader ()
263+ {
264+ delete[] data;
265+ }
248266
249- size_t read (void *data, size_t limit)
267+ size_t read (void *data, size_t limit)
268+ {
269+ if (this ->index + limit > this ->size )
250270 {
251- if (this ->index + limit > this ->size )
252- {
253- return 0 ;
254- }
271+ return 0 ;
272+ }
255273
256- memcpy (data, this ->data + this ->index , limit);
274+ memcpy (data, this ->data + this ->index , limit);
257275
258- this ->index += limit;
259- return limit;
260- }
276+ this ->index += limit;
277+ return limit;
278+ }
261279
262- jsi::Value read (jsi::Runtime &rt)
263- {
264- cmp_ctx_t ctx;
265- cmp_init (&ctx, this , _mp_reader, NULL , _mp_writer);
266- return readValue (&ctx, rt);
267- }
280+ jsi::Value read (jsi::Runtime &rt)
281+ {
282+ cmp_ctx_t ctx;
283+ cmp_init (&ctx, this , _mp_reader, NULL , _mp_writer);
284+ return readValue (&ctx, rt);
285+ }
268286
269287private:
270- char * data;
271- size_t index;
272- size_t size;
288+ char * data;
289+ size_t index;
290+ size_t size;
273291};
274292
275-
276293static bool _mp_reader (cmp_ctx_t *ctx, void *data, size_t limit)
277294{
278- auto *mp = (MessagePackReader *)ctx->buf ;
279- return mp->read (data, limit);
295+ auto *mp = (MessagePackReader *)ctx->buf ;
296+ return mp->read (data, limit);
280297}
281298
282299static size_t _mp_writer (cmp_ctx_t *ctx, const void *data, size_t count)
283300{
284- return 0 ;
301+ return 0 ;
285302}
286-
0 commit comments