@@ -19,8 +19,6 @@ static bool mp_reader(cmp_ctx_t *ctx, void *data, size_t limit);
19
19
jsi::Value readObject (cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length);
20
20
jsi::Value readArray (cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length);
21
21
22
-
23
-
24
22
void writeValue (cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Value &value)
25
23
{
26
24
if (value.isBool ())
@@ -37,14 +35,14 @@ void writeValue(cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Value &value)
37
35
38
36
if (value.isString ())
39
37
{
40
- auto v = value.getString (rt).utf8 (rt);
38
+ std::string const & v = value.getString (rt).utf8 (rt);
41
39
cmp_write_str (ctx, v.c_str (), v.size ());
42
40
return ;
43
41
}
44
42
45
43
if (value.isObject ())
46
44
{
47
- auto object = std::move ( value.getObject (rt) );
45
+ const auto & object = value.getObject (rt);
48
46
if (object.isArray (rt))
49
47
{
50
48
writeArray (ctx, rt, object);
@@ -63,23 +61,32 @@ void writeValue(cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Value &value)
63
61
64
62
void writeArray (cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Object &object)
65
63
{
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++)
69
69
{
70
- writeValue (ctx, rt, std::move (array.getValueAtIndex (rt, i)));
70
+ const jsi::Value &value = array.getValueAtIndex (rt, i);
71
+ writeValue (ctx, rt, value);
71
72
}
72
73
}
73
74
74
75
void writeObject (cmp_ctx_t *ctx, jsi::Runtime &rt, const jsi::Object &object)
75
76
{
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
+
78
80
for (size_t i = 0 ; i < names.size (rt); i++)
79
81
{
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));
83
90
}
84
91
}
85
92
@@ -94,7 +101,9 @@ struct MessagePackWriter
94
101
95
102
size_t write (void *data, size_t count)
96
103
{
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);
98
107
return count;
99
108
}
100
109
};
@@ -107,16 +116,14 @@ static size_t mp_writer(cmp_ctx_t *ctx, const void *data, size_t count)
107
116
108
117
std::vector<uint8_t > write (jsi::Runtime &rt, const jsi::Value &value)
109
118
{
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);
113
121
cmp_init (ctx.get (), &writer, mp_reader, NULL , mp_writer);
114
122
writeValue (ctx.get (), rt, value);
115
123
return std::move (writer.data );
116
124
}
117
125
118
-
119
- jsi::Value readValue (cmp_ctx_t * ctx, jsi::Runtime &rt)
126
+ jsi::Value readValue (cmp_ctx_t *ctx, jsi::Runtime &rt)
120
127
{
121
128
cmp_object_t obj;
122
129
if (!cmp_read_object (ctx, &obj))
@@ -170,29 +177,29 @@ jsi::Value readValue(cmp_ctx_t * ctx, jsi::Runtime &rt)
170
177
}
171
178
172
179
case CMP_TYPE_POSITIVE_FIXNUM:
173
- return jsi::Value ((double ) obj.as .u8 );
180
+ return jsi::Value ((double )obj.as .u8 );
174
181
case CMP_TYPE_NEGATIVE_FIXNUM:
175
- return jsi::Value ((double ) obj.as .s8 );
182
+ return jsi::Value ((double )obj.as .s8 );
176
183
case CMP_TYPE_FLOAT:
177
- return jsi::Value ((double ) obj.as .flt );
184
+ return jsi::Value ((double )obj.as .flt );
178
185
case CMP_TYPE_DOUBLE:
179
- return jsi::Value ((double ) obj.as .dbl );
186
+ return jsi::Value ((double )obj.as .dbl );
180
187
case CMP_TYPE_UINT8:
181
- return jsi::Value ((double ) obj.as .u8 );
188
+ return jsi::Value ((double )obj.as .u8 );
182
189
case CMP_TYPE_UINT16:
183
- return jsi::Value ((double ) obj.as .u16 );
190
+ return jsi::Value ((double )obj.as .u16 );
184
191
case CMP_TYPE_UINT32:
185
- return jsi::Value ((double ) obj.as .u32 );
192
+ return jsi::Value ((double )obj.as .u32 );
186
193
case CMP_TYPE_UINT64:
187
- return jsi::Value ((double ) obj.as .u64 );
194
+ return jsi::Value ((double )obj.as .u64 );
188
195
case CMP_TYPE_SINT8:
189
- return jsi::Value ((double ) obj.as .s8 );
196
+ return jsi::Value ((double )obj.as .s8 );
190
197
case CMP_TYPE_SINT16:
191
- return jsi::Value ((double ) obj.as .s16 );
198
+ return jsi::Value ((double )obj.as .s16 );
192
199
case CMP_TYPE_SINT32:
193
- return jsi::Value ((double ) obj.as .s32 );
200
+ return jsi::Value ((double )obj.as .s32 );
194
201
case CMP_TYPE_SINT64:
195
- return jsi::Value ((double ) obj.as .s64 );
202
+ return jsi::Value ((double )obj.as .s64 );
196
203
197
204
default :
198
205
return jsi::Value (jsi::Value::null ());
@@ -206,16 +213,25 @@ jsi::Value readObject(cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length)
206
213
{
207
214
auto key = readValue (ctx, rt).toString (rt);
208
215
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));
219
235
}
220
236
}
221
237
return result;
@@ -232,55 +248,55 @@ jsi::Value readArray(cmp_ctx_t *ctx, jsi::Runtime &rt, uint32_t length)
232
248
return result;
233
249
}
234
250
235
-
236
- class MessagePackReader {
251
+ class MessagePackReader
252
+ {
237
253
public:
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
+ }
244
261
245
- ~MessagePackReader () {
246
- delete[] data;
247
- }
262
+ ~MessagePackReader ()
263
+ {
264
+ delete[] data;
265
+ }
248
266
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 )
250
270
{
251
- if (this ->index + limit > this ->size )
252
- {
253
- return 0 ;
254
- }
271
+ return 0 ;
272
+ }
255
273
256
- memcpy (data, this ->data + this ->index , limit);
274
+ memcpy (data, this ->data + this ->index , limit);
257
275
258
- this ->index += limit;
259
- return limit;
260
- }
276
+ this ->index += limit;
277
+ return limit;
278
+ }
261
279
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
+ }
268
286
269
287
private:
270
- char * data;
271
- size_t index;
272
- size_t size;
288
+ char * data;
289
+ size_t index;
290
+ size_t size;
273
291
};
274
292
275
-
276
293
static bool _mp_reader (cmp_ctx_t *ctx, void *data, size_t limit)
277
294
{
278
- auto *mp = (MessagePackReader *)ctx->buf ;
279
- return mp->read (data, limit);
295
+ auto *mp = (MessagePackReader *)ctx->buf ;
296
+ return mp->read (data, limit);
280
297
}
281
298
282
299
static size_t _mp_writer (cmp_ctx_t *ctx, const void *data, size_t count)
283
300
{
284
- return 0 ;
301
+ return 0 ;
285
302
}
286
-
0 commit comments