1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_message_parcel.h"
17 #include <cstring>
18 #include <unistd.h>
19 #include "hilog/log.h"
20 #include "log_tags.h"
21 #include "napi_ashmem.h"
22 #include "napi_remote_object.h"
23 #include "string_ex.h"
24
25 namespace OHOS {
26 using namespace OHOS::HiviewDFX;
27 constexpr size_t MAX_CAPACITY_TO_WRITE = 200 * 1024;
28 constexpr size_t BYTE_SIZE_8 = 1;
29 constexpr size_t BYTE_SIZE_32 = 4;
30 constexpr size_t BYTE_SIZE_64 = 8;
31
32 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_messageParcel" };
33 #ifndef TITLE
34 #define TITLE __PRETTY_FUNCTION__
35 #endif
36 #define DBINDER_LOGE(fmt, args...) \
37 (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
38 #define DBINDER_LOGI(fmt, args...) \
39 (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
40
41 #define CHECK_WRITE_CAPACITY(env, lenToWrite, napiParcel) \
42 do { \
43 size_t cap = napiParcel->maxCapacityToWrite_ - napiParcel->nativeParcel_->GetWritePosition(); \
44 if (cap < lenToWrite) { \
45 DBINDER_LOGI("No enough capacity to write"); \
46 napi_throw_range_error(env, nullptr, "No enough capacity to write"); \
47 } \
48 } while (0)
49
50 #define REWIND_IF_WRITE_CHECK_FAIL(env, lenToWrite, pos, napiParcel) \
51 do { \
52 size_t cap = napiParcel->maxCapacityToWrite_ - napiParcel->nativeParcel_->GetWritePosition(); \
53 if (cap < lenToWrite) { \
54 DBINDER_LOGI("No enough capacity to write"); \
55 napiParcel->nativeParcel_->RewindWrite(pos); \
56 napi_throw_range_error(env, nullptr, "No enough capacity to write"); \
57 } \
58 } while (0)
59
60 #define CHECK_READ_LENGTH(env, arrayLength, typeSize, napiParcel) \
61 do { \
62 size_t remainSize = napiParcel->nativeParcel_->GetDataSize() - napiParcel->nativeParcel_->GetReadPosition(); \
63 if ((arrayLength < 0) || (arrayLength > remainSize) || ((arrayLength * typeSize) > remainSize)) { \
64 DBINDER_LOGI("No enough data to read"); \
65 napi_throw_range_error(env, nullptr, "No enough data to read"); \
66 } \
67 } while (0)
68
NAPI_MessageParcel(napi_env env,napi_value thisVar,MessageParcel * parcel)69 NAPI_MessageParcel::NAPI_MessageParcel(napi_env env, napi_value thisVar, MessageParcel *parcel)
70 {
71 env_ = env;
72 maxCapacityToWrite_ = MAX_CAPACITY_TO_WRITE;
73 // do NOT reference js parcel here
74 if (parcel == nullptr) {
75 nativeParcel_ = std::shared_ptr<MessageParcel>(new MessageParcel());
76 owner = true;
77 } else {
78 nativeParcel_ = std::shared_ptr<MessageParcel>(parcel, release);
79 owner = false;
80 }
81 }
82
~NAPI_MessageParcel()83 NAPI_MessageParcel::~NAPI_MessageParcel()
84 {
85 DBINDER_LOGI("NAPI_MessageParcel::Destructor");
86 nativeParcel_ = nullptr;
87 env_ = nullptr;
88 }
89
release(MessageParcel * parcel)90 void NAPI_MessageParcel::release(MessageParcel *parcel)
91 {
92 DBINDER_LOGI("message parcel is created by others, do nothing");
93 }
94
GetMessageParcel()95 std::shared_ptr<MessageParcel> NAPI_MessageParcel::GetMessageParcel()
96 {
97 return nativeParcel_;
98 }
99
JS_writeByte(napi_env env,napi_callback_info info)100 napi_value NAPI_MessageParcel::JS_writeByte(napi_env env, napi_callback_info info)
101 {
102 size_t argc = 1;
103 napi_value argv[1] = {0};
104 napi_value thisVar = nullptr;
105 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
106 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
107
108 napi_valuetype valueType = napi_null;
109 napi_typeof(env, argv[0], &valueType);
110 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
111
112 int32_t value = 0;
113 napi_get_value_int32(env, argv[0], &value);
114
115 NAPI_MessageParcel *napiParcel = nullptr;
116 napi_unwrap(env, thisVar, (void **)&napiParcel);
117 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
118 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32, napiParcel);
119 bool result = napiParcel->nativeParcel_->WriteInt8(static_cast<int8_t>(value));
120 napi_value napiValue = nullptr;
121 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
122 return napiValue;
123 }
124
JS_writeShort(napi_env env,napi_callback_info info)125 napi_value NAPI_MessageParcel::JS_writeShort(napi_env env, napi_callback_info info)
126 {
127 size_t argc = 1;
128 napi_value argv[1] = {0};
129 napi_value thisVar = nullptr;
130 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
131 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
132
133 napi_valuetype valueType = napi_null;
134 napi_typeof(env, argv[0], &valueType);
135 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
136
137 int32_t value = 0;
138 napi_get_value_int32(env, argv[0], &value);
139
140 NAPI_MessageParcel *napiParcel = nullptr;
141 napi_unwrap(env, thisVar, (void **)&napiParcel);
142 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
143 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32, napiParcel);
144 bool result = napiParcel->nativeParcel_->WriteInt16(static_cast<int16_t>(value));
145 napi_value napiValue = nullptr;
146 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
147 return napiValue;
148 }
149
JS_writeInt(napi_env env,napi_callback_info info)150 napi_value NAPI_MessageParcel::JS_writeInt(napi_env env, napi_callback_info info)
151 {
152 size_t argc = 1;
153 napi_value argv[1] = {0};
154 napi_value thisVar = nullptr;
155 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
156 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
157
158 napi_valuetype valueType = napi_null;
159 napi_typeof(env, argv[0], &valueType);
160 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
161
162 int32_t value = 0;
163 napi_get_value_int32(env, argv[0], &value);
164
165 NAPI_MessageParcel *napiParcel = nullptr;
166 napi_unwrap(env, thisVar, (void **)&napiParcel);
167 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
168 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32, napiParcel);
169 bool result = napiParcel->nativeParcel_->WriteInt32(value);
170 napi_value napiValue = nullptr;
171 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
172 return napiValue;
173 }
174
JS_writeLong(napi_env env,napi_callback_info info)175 napi_value NAPI_MessageParcel::JS_writeLong(napi_env env, napi_callback_info info)
176 {
177 size_t argc = 1;
178 napi_value argv[1] = {0};
179 napi_value thisVar = nullptr;
180 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
181 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
182
183 napi_valuetype valueType = napi_null;
184 napi_typeof(env, argv[0], &valueType);
185 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
186
187 int64_t value = 0;
188 napi_get_value_int64(env, argv[0], &value);
189
190 NAPI_MessageParcel *napiParcel = nullptr;
191 napi_unwrap(env, thisVar, (void **)&napiParcel);
192 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
193 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_64, napiParcel);
194 bool result = napiParcel->nativeParcel_->WriteInt64(value);
195 napi_value napiValue = nullptr;
196 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
197 return napiValue;
198 }
199
JS_writeFloat(napi_env env,napi_callback_info info)200 napi_value NAPI_MessageParcel::JS_writeFloat(napi_env env, napi_callback_info info)
201 {
202 size_t argc = 1;
203 napi_value argv[1] = {0};
204 napi_value thisVar = nullptr;
205 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
206 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
207
208 napi_valuetype valueType = napi_null;
209 napi_typeof(env, argv[0], &valueType);
210 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
211
212 double value = 0;
213 napi_get_value_double(env, argv[0], &value);
214
215 NAPI_MessageParcel *napiParcel = nullptr;
216 napi_unwrap(env, thisVar, (void **)&napiParcel);
217 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
218 CHECK_WRITE_CAPACITY(env, sizeof(double), napiParcel);
219 bool result = napiParcel->nativeParcel_->WriteDouble(value);
220 napi_value napiValue = nullptr;
221 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
222 return napiValue;
223 }
224
JS_writeDouble(napi_env env,napi_callback_info info)225 napi_value NAPI_MessageParcel::JS_writeDouble(napi_env env, napi_callback_info info)
226 {
227 size_t argc = 1;
228 napi_value argv[1] = {0};
229 napi_value thisVar = nullptr;
230 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
231 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
232
233 napi_valuetype valueType = napi_null;
234 napi_typeof(env, argv[0], &valueType);
235 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
236
237 double value = 0;
238 napi_get_value_double(env, argv[0], &value);
239
240 NAPI_MessageParcel *napiParcel = nullptr;
241 napi_unwrap(env, thisVar, (void **)&napiParcel);
242 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
243 CHECK_WRITE_CAPACITY(env, sizeof(double), napiParcel);
244 bool result = napiParcel->nativeParcel_->WriteDouble(value);
245 napi_value napiValue = nullptr;
246 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
247 return napiValue;
248 }
249
JS_writeBoolean(napi_env env,napi_callback_info info)250 napi_value NAPI_MessageParcel::JS_writeBoolean(napi_env env, napi_callback_info info)
251 {
252 size_t argc = 1;
253 napi_value argv[1] = {0};
254 napi_value thisVar = nullptr;
255 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
256 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
257
258 napi_valuetype valueType = napi_null;
259 napi_typeof(env, argv[0], &valueType);
260 NAPI_ASSERT(env, valueType == napi_boolean, "type mismatch for parameter 1");
261
262 bool value = 0;
263 napi_get_value_bool(env, argv[0], &value);
264
265 NAPI_MessageParcel *napiParcel = nullptr;
266 napi_unwrap(env, thisVar, (void **)&napiParcel);
267 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
268 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32, napiParcel);
269 bool result = napiParcel->nativeParcel_->WriteInt8(static_cast<int8_t>(value));
270 napi_value napiValue = nullptr;
271 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
272 return napiValue;
273 }
274
JS_writeChar(napi_env env,napi_callback_info info)275 napi_value NAPI_MessageParcel::JS_writeChar(napi_env env, napi_callback_info info)
276 {
277 size_t argc = 1;
278 napi_value argv[1] = {0};
279 napi_value thisVar = nullptr;
280 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
281 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
282
283 napi_valuetype valueType = napi_null;
284 napi_typeof(env, argv[0], &valueType);
285 NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
286
287 size_t bufferSize = 0;
288 size_t strLength = 0;
289 napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize);
290 DBINDER_LOGI("messageparcel writeChar bufferSize = %{public}d", (int)bufferSize);
291 char buffer[bufferSize + 1];
292 napi_get_value_string_utf8(env, argv[0], buffer, bufferSize + 1, &strLength);
293
294 NAPI_MessageParcel *napiParcel = nullptr;
295 napi_unwrap(env, thisVar, (void **)&napiParcel);
296 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
297 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32, napiParcel);
298 std::string parcelString = buffer;
299 std::u16string tmp = to_utf16(parcelString);
300 auto value = reinterpret_cast<uint16_t *>(tmp.data());
301 bool result = napiParcel->nativeParcel_->WriteUint16(*value);
302
303 napi_value napiValue = nullptr;
304 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
305 return napiValue;
306 }
307
JS_writeByteArray(napi_env env,napi_callback_info info)308 napi_value NAPI_MessageParcel::JS_writeByteArray(napi_env env, napi_callback_info info)
309 {
310 size_t argc = 1;
311 napi_value argv[1] = {0};
312 napi_value thisVar = nullptr;
313 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
314 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
315
316 bool isTypedArray = false;
317 napi_is_typedarray(env, argv[0], &isTypedArray);
318 NAPI_ASSERT(env, isTypedArray == true, "type mismatch for parameter 1");
319
320 napi_typedarray_type typedarrayType = napi_uint8_array;
321 size_t typedarrayLength = 0;
322 void *typedarrayBufferPtr = nullptr;
323 napi_value tmpArrayBuffer = nullptr;
324 size_t byteOffset = 0;
325 napi_get_typedarray_info(env, argv[0], &typedarrayType, &typedarrayLength, &typedarrayBufferPtr,
326 &tmpArrayBuffer, &byteOffset);
327
328 NAPI_ASSERT(env, typedarrayType == napi_int8_array, "array type mismatch for parameter 1");
329 DBINDER_LOGI("messageparcel WriteBuffer typedarrayLength = %{public}d", (int)(typedarrayLength));
330
331 NAPI_MessageParcel *napiParcel = nullptr;
332 napi_unwrap(env, thisVar, (void **)&napiParcel);
333 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
334 size_t len = (typedarrayLength / BYTE_SIZE_32) + ((typedarrayLength % BYTE_SIZE_32) == 0 ? 0 : 1);
335 DBINDER_LOGI("messageparcel WriteBuffer len = %{public}d", (int)(len));
336 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * (len + 1), napiParcel);
337 napiParcel->nativeParcel_->WriteUint32(typedarrayLength);
338 bool result = napiParcel->nativeParcel_->WriteBuffer(typedarrayBufferPtr, typedarrayLength);
339 napi_value napiValue = nullptr;
340 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
341 return napiValue;
342 }
343
JS_writeShortArray(napi_env env,napi_callback_info info)344 napi_value NAPI_MessageParcel::JS_writeShortArray(napi_env env, napi_callback_info info)
345 {
346 size_t argc = 1;
347 napi_value argv[1] = { 0 };
348 napi_value thisVar = nullptr;
349 void *data = nullptr;
350 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
351 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
352
353 bool isArray = false;
354 napi_is_array(env, argv[0], &isArray);
355 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
356
357 uint32_t arrayLength = 0;
358 napi_get_array_length(env, argv[0], &arrayLength);
359
360 NAPI_MessageParcel *napiParcel = nullptr;
361 napi_unwrap(env, thisVar, (void **)&napiParcel);
362 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
363
364 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * (arrayLength + 1), napiParcel);
365 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
366 napiParcel->nativeParcel_->WriteUint32(arrayLength);
367 bool result = false;
368 for (size_t i = 0; i < arrayLength; i++) {
369 bool hasElement = false;
370 napi_has_element(env, argv[0], i, &hasElement);
371 NAPI_ASSERT(env, hasElement == true, "parameter check error");
372
373 napi_value element = nullptr;
374 napi_get_element(env, argv[0], i, &element);
375
376 int32_t value = 0;
377 napi_get_value_int32(env, element, &value);
378 result = napiParcel->nativeParcel_->WriteInt16(static_cast<int16_t>(value));
379 if (!result) {
380 napiParcel->nativeParcel_->RewindWrite(pos);
381 break;
382 }
383 }
384
385 napi_value napiValue = nullptr;
386 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
387 return napiValue;
388 }
389
JS_writeIntArray(napi_env env,napi_callback_info info)390 napi_value NAPI_MessageParcel::JS_writeIntArray(napi_env env, napi_callback_info info)
391 {
392 size_t argc = 1;
393 napi_value argv[1] = { 0 };
394 napi_value thisVar = nullptr;
395 void *data = nullptr;
396 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
397 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
398
399 bool isArray = false;
400 napi_is_array(env, argv[0], &isArray);
401 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
402
403 uint32_t arrayLength = 0;
404 napi_get_array_length(env, argv[0], &arrayLength);
405
406 NAPI_MessageParcel *napiParcel = nullptr;
407 napi_unwrap(env, thisVar, (void **)&napiParcel);
408 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
409
410 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * (arrayLength + 1), napiParcel);
411 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
412 napiParcel->nativeParcel_->WriteUint32(arrayLength);
413 bool result = false;
414 for (size_t i = 0; i < arrayLength; i++) {
415 bool hasElement = false;
416 napi_has_element(env, argv[0], i, &hasElement);
417 NAPI_ASSERT(env, hasElement == true, "parameter check error");
418
419 napi_value element = nullptr;
420 napi_get_element(env, argv[0], i, &element);
421
422 napi_valuetype valueType;
423 napi_typeof(env, element, &valueType);
424 NAPI_ASSERT(env, valueType == napi_number, "type mismatch element");
425
426 int32_t value = 0;
427 napi_get_value_int32(env, element, &value);
428 result = napiParcel->nativeParcel_->WriteInt32(value);
429 if (!result) {
430 napiParcel->nativeParcel_->RewindWrite(pos);
431 break;
432 }
433 }
434
435 napi_value napiValue = nullptr;
436 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
437 return napiValue;
438 }
439
JS_writeLongArray(napi_env env,napi_callback_info info)440 napi_value NAPI_MessageParcel::JS_writeLongArray(napi_env env, napi_callback_info info)
441 {
442 size_t argc = 1;
443 napi_value argv[1] = { 0 };
444 napi_value thisVar = nullptr;
445 void *data = nullptr;
446 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
447 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
448
449 bool isArray = false;
450 napi_is_array(env, argv[0], &isArray);
451 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
452
453 uint32_t arrayLength = 0;
454 napi_get_array_length(env, argv[0], &arrayLength);
455 DBINDER_LOGI("messageparcel WriteBuffer typedarrayLength = %{public}d", (int)(arrayLength));
456
457 NAPI_MessageParcel *napiParcel = nullptr;
458 napi_unwrap(env, thisVar, (void **)&napiParcel);
459 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
460
461 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 + BYTE_SIZE_64 * arrayLength, napiParcel);
462 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
463 napiParcel->nativeParcel_->WriteUint32(arrayLength);
464 bool result = false;
465 for (size_t i = 0; i < arrayLength; i++) {
466 bool hasElement = false;
467 napi_has_element(env, argv[0], i, &hasElement);
468 NAPI_ASSERT(env, hasElement == true, "parameter check error");
469
470 napi_value element = nullptr;
471 napi_get_element(env, argv[0], i, &element);
472
473 int64_t value = 0;
474 napi_get_value_int64(env, element, &value);
475
476 result = napiParcel->nativeParcel_->WriteInt64(value);
477 if (!result) {
478 napiParcel->nativeParcel_->RewindWrite(pos);
479 break;
480 }
481 }
482
483 napi_value napiValue = nullptr;
484 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
485 return napiValue;
486 }
487
JS_writeFloatArray(napi_env env,napi_callback_info info)488 napi_value NAPI_MessageParcel::JS_writeFloatArray(napi_env env, napi_callback_info info)
489 {
490 size_t argc = 1;
491 napi_value argv[1] = { 0 };
492 napi_value thisVar = nullptr;
493 void *data = nullptr;
494 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
495 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
496
497 bool isArray = false;
498 napi_is_array(env, argv[0], &isArray);
499 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
500
501 uint32_t arrayLength = 0;
502 napi_get_array_length(env, argv[0], &arrayLength);
503
504 NAPI_MessageParcel *napiParcel = nullptr;
505 napi_unwrap(env, thisVar, (void **)&napiParcel);
506 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
507
508 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 + sizeof(double) * arrayLength, napiParcel);
509 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
510 napiParcel->nativeParcel_->WriteUint32(arrayLength);
511 bool result = false;
512 for (size_t i = 0; i < arrayLength; i++) {
513 bool hasElement = false;
514 napi_has_element(env, argv[0], i, &hasElement);
515 NAPI_ASSERT(env, hasElement == true, "parameter check error");
516
517 napi_value element = nullptr;
518 napi_get_element(env, argv[0], i, &element);
519
520 double value = 0;
521 napi_get_value_double(env, element, &value);
522
523 result = napiParcel->nativeParcel_->WriteDouble(value);
524 if (!result) {
525 napiParcel->nativeParcel_->RewindWrite(pos);
526 break;
527 }
528 }
529
530 napi_value napiValue = nullptr;
531 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
532 return napiValue;
533 }
534
JS_writeDoubleArray(napi_env env,napi_callback_info info)535 napi_value NAPI_MessageParcel::JS_writeDoubleArray(napi_env env, napi_callback_info info)
536 {
537 size_t argc = 1;
538 napi_value argv[1] = { 0 };
539 napi_value thisVar = nullptr;
540 void *data = nullptr;
541 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
542 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
543
544 bool isArray = false;
545 napi_is_array(env, argv[0], &isArray);
546 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
547
548 uint32_t arrayLength = 0;
549 napi_get_array_length(env, argv[0], &arrayLength);
550
551 NAPI_MessageParcel *napiParcel = nullptr;
552 napi_unwrap(env, thisVar, (void **)&napiParcel);
553 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
554
555 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 + sizeof(double) * arrayLength, napiParcel);
556 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
557 napiParcel->nativeParcel_->WriteUint32(arrayLength);
558 bool result = false;
559 for (size_t i = 0; i < arrayLength; i++) {
560 bool hasElement = false;
561 napi_has_element(env, argv[0], i, &hasElement);
562 NAPI_ASSERT(env, hasElement == true, "parameter check error");
563
564 napi_value element = nullptr;
565 napi_get_element(env, argv[0], i, &element);
566
567 double value = 0;
568 napi_get_value_double(env, element, &value);
569
570 result = napiParcel->nativeParcel_->WriteDouble(value);
571 if (!result) {
572 napiParcel->nativeParcel_->RewindWrite(pos);
573 break;
574 }
575 }
576
577 napi_value napiValue = nullptr;
578 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
579 return napiValue;
580 }
581
JS_writeBooleanArray(napi_env env,napi_callback_info info)582 napi_value NAPI_MessageParcel::JS_writeBooleanArray(napi_env env, napi_callback_info info)
583 {
584 size_t argc = 1;
585 napi_value argv[1] = { 0 };
586 napi_value thisVar = nullptr;
587 void *data = nullptr;
588 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
589 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
590
591 bool isArray = false;
592 napi_is_array(env, argv[0], &isArray);
593 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
594
595 uint32_t arrayLength = 0;
596 napi_get_array_length(env, argv[0], &arrayLength);
597
598 NAPI_MessageParcel *napiParcel = nullptr;
599 napi_unwrap(env, thisVar, (void **)&napiParcel);
600 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
601
602 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * (arrayLength + 1), napiParcel);
603 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
604 napiParcel->nativeParcel_->WriteUint32(arrayLength);
605 bool result = false;
606 for (size_t i = 0; i < arrayLength; i++) {
607 bool hasElement = false;
608 napi_has_element(env, argv[0], i, &hasElement);
609 NAPI_ASSERT(env, hasElement == true, "parameter check error");
610
611 napi_value element = nullptr;
612 napi_get_element(env, argv[0], i, &element);
613
614 bool value = 0;
615 napi_get_value_bool(env, element, &value);
616
617 result = napiParcel->nativeParcel_->WriteInt8(static_cast<int8_t>(value));
618 if (!result) {
619 napiParcel->nativeParcel_->RewindWrite(pos);
620 break;
621 }
622 }
623
624 napi_value napiValue = nullptr;
625 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
626 return napiValue;
627 }
628
JS_writeCharArray(napi_env env,napi_callback_info info)629 napi_value NAPI_MessageParcel::JS_writeCharArray(napi_env env, napi_callback_info info)
630 {
631 size_t argc = 1;
632 napi_value argv[1] = { 0 };
633 napi_value thisVar = nullptr;
634 void *data = nullptr;
635 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
636 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
637
638 bool isArray = false;
639 napi_is_array(env, argv[0], &isArray);
640 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
641
642 uint32_t arrayLength = 0;
643 napi_get_array_length(env, argv[0], &arrayLength);
644
645 NAPI_MessageParcel *napiParcel = nullptr;
646 napi_unwrap(env, thisVar, (void **)&napiParcel);
647 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
648
649 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * (arrayLength + 1), napiParcel);
650 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
651 napiParcel->nativeParcel_->WriteUint32(arrayLength);
652 bool result = false;
653 for (size_t i = 0; i < arrayLength; i++) {
654 bool hasElement = false;
655 napi_has_element(env, argv[0], i, &hasElement);
656 NAPI_ASSERT(env, hasElement == true, "parameter check error");
657
658 napi_value element = nullptr;
659 napi_get_element(env, argv[0], i, &element);
660 size_t bufferSize = 0;
661 size_t strLength = 0;
662 napi_get_value_string_utf8(env, element, nullptr, 0, &bufferSize);
663 DBINDER_LOGI("messageparcel writeChar bufferSize = %{public}d", (int)bufferSize);
664 char buffer[bufferSize + 1];
665 napi_get_value_string_utf8(env, element, buffer, bufferSize + 1, &strLength);
666 DBINDER_LOGI("messageparcel writeChar strLength = %{public}d", (int)strLength);
667
668 std::string parcelString = buffer;
669 std::u16string tmp = to_utf16(parcelString);
670 auto value = reinterpret_cast<uint16_t *>(tmp.data());
671 result = napiParcel->nativeParcel_->WriteUint16(*value);
672 if (!result) {
673 napiParcel->nativeParcel_->RewindWrite(pos);
674 break;
675 }
676 }
677
678 napi_value napiValue = nullptr;
679 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
680 return napiValue;
681 }
682
JS_writeString(napi_env env,napi_callback_info info)683 napi_value NAPI_MessageParcel::JS_writeString(napi_env env, napi_callback_info info)
684 {
685 size_t argc = 1;
686 napi_value argv[1] = {0};
687 napi_value thisVar = nullptr;
688 void *data = nullptr;
689 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
690 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
691
692 NAPI_MessageParcel *napiParcel = nullptr;
693 napi_unwrap(env, thisVar, (void **)&napiParcel);
694 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
695
696 napi_valuetype valueType = napi_null;
697 napi_typeof(env, argv[0], &valueType);
698 NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter 1");
699
700 size_t bufferSize = 0;
701 size_t maxLen = 40960;
702 napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize);
703 NAPI_ASSERT(env, bufferSize < maxLen, "string length too large");
704
705 char stringValue[bufferSize + 1];
706 size_t jsStringLength = 0;
707 napi_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength);
708 NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong");
709
710 CHECK_WRITE_CAPACITY(env, BYTE_SIZE_32 * bufferSize, napiParcel);
711 std::string parcelString = stringValue;
712 bool result = napiParcel->nativeParcel_->WriteString16(to_utf16(parcelString));
713
714 napi_value napiValue = nullptr;
715 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
716 return napiValue;
717 }
718
JS_writeStringArray(napi_env env,napi_callback_info info)719 napi_value NAPI_MessageParcel::JS_writeStringArray(napi_env env, napi_callback_info info)
720 {
721 size_t argc = 1;
722 napi_value argv[1] = { 0 };
723 napi_value thisVar = nullptr;
724 void *data = nullptr;
725 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
726 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
727
728 bool isArray = false;
729 napi_is_array(env, argv[0], &isArray);
730 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
731
732 uint32_t arrayLength = 0;
733 napi_get_array_length(env, argv[0], &arrayLength);
734
735 NAPI_MessageParcel *napiParcel = nullptr;
736 napi_unwrap(env, thisVar, (void **)&napiParcel);
737 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
738
739 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
740 napiParcel->nativeParcel_->WriteUint32(arrayLength);
741 bool result = false;
742 for (size_t i = 0; i < arrayLength; i++) {
743 bool hasElement = false;
744 size_t maxSize = 40960;
745 napi_has_element(env, argv[0], i, &hasElement);
746 NAPI_ASSERT(env, hasElement == true, "parameter check error");
747
748 napi_value element = nullptr;
749 napi_get_element(env, argv[0], i, &element);
750 napi_valuetype valuetype;
751 napi_typeof(env, element, &valuetype);
752 NAPI_ASSERT(env, valuetype == napi_string, "Parameter type error");
753
754 size_t bufferSize = 0;
755 napi_get_value_string_utf8(env, element, nullptr, 0, &bufferSize);
756 NAPI_ASSERT(env, bufferSize < maxSize, "string length too large");
757
758 char stringValue[bufferSize + 1];
759 size_t jsStringLength = 0;
760 napi_get_value_string_utf8(env, element, stringValue, bufferSize + 1, &jsStringLength);
761 NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong");
762
763 REWIND_IF_WRITE_CHECK_FAIL(env, BYTE_SIZE_32 * bufferSize, pos, napiParcel);
764 std::string parcelString = stringValue;
765 result = napiParcel->nativeParcel_->WriteString16(to_utf16(parcelString));
766 if (!result) {
767 napiParcel->nativeParcel_->RewindWrite(pos);
768 break;
769 }
770 }
771
772 napi_value napiValue = nullptr;
773 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
774 return napiValue;
775 }
776
JS_writeSequenceable(napi_env env,napi_callback_info info)777 napi_value NAPI_MessageParcel::JS_writeSequenceable(napi_env env, napi_callback_info info)
778 {
779 napi_value result = nullptr;
780 napi_get_boolean(env, false, &result);
781
782 size_t argc = 1;
783 napi_value argv[1] = { 0 };
784 napi_value thisVar = nullptr;
785 void *data = nullptr;
786 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
787 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
788
789 NAPI_MessageParcel *napiParcel = nullptr;
790 napi_unwrap(env, thisVar, (void **)&napiParcel);
791 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
792
793 napi_valuetype valueType = napi_null;
794 napi_typeof(env, argv[0], &valueType);
795 if (valueType == napi_null || valueType == napi_undefined) {
796 napiParcel->nativeParcel_->WriteInt32(0);
797 return result;
798 }
799 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
800 napiParcel->nativeParcel_->WriteInt32(1);
801 napi_value propKey = nullptr;
802 const char *propKeyStr = "marshalling";
803 napi_create_string_utf8(env, propKeyStr, strlen(propKeyStr), &propKey);
804 napi_value prop = nullptr;
805 napi_get_property(env, argv[0], propKey, &prop);
806
807 napi_value funcArg[1] = { thisVar };
808 napi_value callResult = nullptr;
809 napi_call_function(env, argv[0], prop, 1, funcArg, &callResult);
810 if (callResult != nullptr) {
811 return callResult;
812 }
813 DBINDER_LOGE("call mashalling failed");
814 napiParcel->nativeParcel_->RewindWrite(pos);
815 return result;
816 }
817
JS_writeSequenceableArray(napi_env env,napi_callback_info info)818 napi_value NAPI_MessageParcel::JS_writeSequenceableArray(napi_env env, napi_callback_info info)
819 {
820 napi_value retValue = nullptr;
821 napi_get_boolean(env, false, &retValue);
822
823 size_t argc = 1;
824 napi_value argv[1] = { 0 };
825 napi_value thisVar = nullptr;
826 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
827 NAPI_ASSERT_BASE(env, argc == 1, "requires 1 parameter", retValue);
828
829 bool isArray = false;
830 napi_is_array(env, argv[0], &isArray);
831 NAPI_ASSERT_BASE(env, isArray == true, "type mismatch for parameter 1", retValue);
832 uint32_t arrayLength = 0;
833 napi_get_array_length(env, argv[0], &arrayLength);
834
835 NAPI_MessageParcel *napiParcel = nullptr;
836 napi_unwrap(env, thisVar, (void **)&napiParcel);
837 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", retValue);
838
839 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
840 bool result = napiParcel->nativeParcel_->WriteUint32(arrayLength);
841 for (size_t i = 0; i < arrayLength; i++) {
842 bool hasElement = false;
843 napi_has_element(env, argv[0], i, &hasElement);
844 NAPI_ASSERT_BASE(env, hasElement == true, "parameter check error", retValue);
845
846 napi_value element = nullptr;
847 napi_get_element(env, argv[0], i, &element);
848 napi_valuetype valueType = napi_null;
849 napi_typeof(env, element, &valueType);
850 if (valueType == napi_null || valueType == napi_undefined) {
851 napiParcel->nativeParcel_->WriteInt32(0);
852 continue;
853 } else {
854 napiParcel->nativeParcel_->WriteInt32(1);
855 }
856 napi_value propKey = nullptr;
857 const char *propKeyStr = "marshalling";
858 napi_create_string_utf8(env, propKeyStr, strlen(propKeyStr), &propKey);
859 napi_value prop = nullptr;
860 napi_get_property(env, element, propKey, &prop);
861
862 napi_value funcArg[1] = { thisVar };
863 napi_value callResult = nullptr;
864 napi_call_function(env, element, prop, 1, funcArg, &callResult);
865 if (callResult == nullptr) {
866 DBINDER_LOGE("call mashalling failed, element index: %{public}zu", i);
867 napiParcel->nativeParcel_->RewindWrite(pos);
868 return retValue;
869 }
870 }
871
872 napi_get_boolean(env, result, &retValue);
873 return retValue;
874 }
875
JS_writeRemoteObjectArray(napi_env env,napi_callback_info info)876 napi_value NAPI_MessageParcel::JS_writeRemoteObjectArray(napi_env env, napi_callback_info info)
877 {
878 napi_value retValue = nullptr;
879 napi_get_boolean(env, false, &retValue);
880
881 size_t argc = 1;
882 napi_value argv[1] = { 0 };
883 napi_value thisVar = nullptr;
884 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
885 NAPI_ASSERT_BASE(env, argc == 1, "requires 1 parameter", retValue);
886
887 NAPI_MessageParcel *napiParcel = nullptr;
888 napi_unwrap(env, thisVar, (void **)&napiParcel);
889 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", retValue);
890 napi_valuetype valueType = napi_null;
891 napi_typeof(env, argv[0], &valueType);
892 if (valueType == napi_null || valueType == napi_undefined) {
893 napiParcel->nativeParcel_->WriteInt32(-1);
894 return retValue;
895 }
896
897 bool isArray = false;
898 napi_is_array(env, argv[0], &isArray);
899 NAPI_ASSERT_BASE(env, isArray == true, "type mismatch for parameter 1", retValue);
900
901 uint32_t arrayLength = 0;
902 napi_get_array_length(env, argv[0], &arrayLength);
903 size_t pos = napiParcel->nativeParcel_->GetWritePosition();
904 bool result = napiParcel->nativeParcel_->WriteInt32(arrayLength);
905 for (size_t i = 0; i < arrayLength; i++) {
906 bool hasElement = false;
907 napi_has_element(env, argv[0], i, &hasElement);
908 NAPI_ASSERT_BASE(env, hasElement == true, "parameter check error", retValue);
909 napi_value element = nullptr;
910 napi_get_element(env, argv[0], i, &element);
911 sptr<IRemoteObject> remoteObject = NAPI_ohos_rpc_getNativeRemoteObject(env, element);
912 NAPI_ASSERT_BASE(env, remoteObject != nullptr, "parameter check error", retValue);
913 result = napiParcel->nativeParcel_->WriteRemoteObject(remoteObject);
914 if (!result) {
915 napiParcel->nativeParcel_->RewindWrite(pos);
916 return retValue;
917 }
918 }
919 napi_get_boolean(env, result, &retValue);
920 return retValue;
921 }
922
JS_readByte(napi_env env,napi_callback_info info)923 napi_value NAPI_MessageParcel::JS_readByte(napi_env env, napi_callback_info info)
924 {
925 size_t argc = 0;
926 napi_value thisVar = nullptr;
927 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
928 NAPI_MessageParcel *napiParcel = nullptr;
929 napi_unwrap(env, thisVar, (void **)&napiParcel);
930 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
931
932 int8_t value = napiParcel->nativeParcel_->ReadInt8();
933 napi_value napiValue = nullptr;
934 NAPI_CALL(env, napi_create_int32(env, value, &napiValue));
935 return napiValue;
936 }
937
JS_readShort(napi_env env,napi_callback_info info)938 napi_value NAPI_MessageParcel::JS_readShort(napi_env env, napi_callback_info info)
939 {
940 size_t argc = 0;
941 napi_value thisVar = nullptr;
942 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
943 NAPI_MessageParcel *napiParcel = nullptr;
944 napi_unwrap(env, thisVar, (void **)&napiParcel);
945 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
946
947 int16_t value = napiParcel->nativeParcel_->ReadInt16();
948 napi_value napiValue = nullptr;
949 NAPI_CALL(env, napi_create_int32(env, value, &napiValue));
950 return napiValue;
951 }
952
JS_readInt(napi_env env,napi_callback_info info)953 napi_value NAPI_MessageParcel::JS_readInt(napi_env env, napi_callback_info info)
954 {
955 size_t argc = 0;
956 napi_value thisVar = nullptr;
957 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
958 NAPI_MessageParcel *napiParcel = nullptr;
959 napi_unwrap(env, thisVar, (void **)&napiParcel);
960 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
961
962 int32_t value = napiParcel->nativeParcel_->ReadInt32();
963 napi_value napiValue = nullptr;
964 NAPI_CALL(env, napi_create_int32(env, value, &napiValue));
965 return napiValue;
966 }
967
JS_readLong(napi_env env,napi_callback_info info)968 napi_value NAPI_MessageParcel::JS_readLong(napi_env env, napi_callback_info info)
969 {
970 size_t argc = 0;
971 napi_value thisVar = nullptr;
972 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
973 NAPI_MessageParcel *napiParcel = nullptr;
974 napi_unwrap(env, thisVar, (void **)&napiParcel);
975 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
976
977 int64_t value = napiParcel->nativeParcel_->ReadInt64();
978 napi_value napiValue = nullptr;
979 NAPI_CALL(env, napi_create_int64(env, value, &napiValue));
980 return napiValue;
981 }
982
JS_readFloat(napi_env env,napi_callback_info info)983 napi_value NAPI_MessageParcel::JS_readFloat(napi_env env, napi_callback_info info)
984 {
985 size_t argc = 0;
986 napi_value thisVar = nullptr;
987 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
988 NAPI_MessageParcel *napiParcel = nullptr;
989 napi_unwrap(env, thisVar, (void **)&napiParcel);
990 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
991
992 double value = napiParcel->nativeParcel_->ReadDouble();
993 napi_value napiValue = nullptr;
994 NAPI_CALL(env, napi_create_double(env, value, &napiValue));
995 return napiValue;
996 }
997
JS_readDouble(napi_env env,napi_callback_info info)998 napi_value NAPI_MessageParcel::JS_readDouble(napi_env env, napi_callback_info info)
999 {
1000 size_t argc = 0;
1001 napi_value thisVar = nullptr;
1002 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1003 NAPI_MessageParcel *napiParcel = nullptr;
1004 napi_unwrap(env, thisVar, (void **)&napiParcel);
1005 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1006
1007 double value = napiParcel->nativeParcel_->ReadDouble();
1008 napi_value napiValue = nullptr;
1009 NAPI_CALL(env, napi_create_double(env, value, &napiValue));
1010 return napiValue;
1011 }
1012
JS_readBoolean(napi_env env,napi_callback_info info)1013 napi_value NAPI_MessageParcel::JS_readBoolean(napi_env env, napi_callback_info info)
1014 {
1015 size_t argc = 0;
1016 napi_value thisVar = nullptr;
1017 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1018 NAPI_MessageParcel *napiParcel = nullptr;
1019 napi_unwrap(env, thisVar, (void **)&napiParcel);
1020 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1021
1022 int8_t value = napiParcel->nativeParcel_->ReadInt8();
1023 napi_value napiValue = nullptr;
1024 NAPI_CALL(env, napi_get_boolean(env, value, &napiValue));
1025 return napiValue;
1026 }
1027
JS_readChar(napi_env env,napi_callback_info info)1028 napi_value NAPI_MessageParcel::JS_readChar(napi_env env, napi_callback_info info)
1029 {
1030 size_t argc = 0;
1031 napi_value thisVar = nullptr;
1032 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1033 NAPI_MessageParcel *napiParcel = nullptr;
1034 napi_unwrap(env, thisVar, (void **)&napiParcel);
1035 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1036
1037 uint16_t value = napiParcel->nativeParcel_->ReadUint16();
1038 char ch[2] = { 0 };
1039 if (sprintf_s(ch, sizeof(ch) / sizeof(ch[0]), "%c", value) < 0) {
1040 return nullptr;
1041 }
1042 napi_value result = nullptr;
1043 napi_create_string_utf8(env, ch, 1, &result);
1044 return result;
1045 }
1046
JS_readString(napi_env env,napi_callback_info info)1047 napi_value NAPI_MessageParcel::JS_readString(napi_env env, napi_callback_info info)
1048 {
1049 size_t argc = 0;
1050 napi_value thisVar = nullptr;
1051 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1052 NAPI_MessageParcel *napiParcel = nullptr;
1053 napi_unwrap(env, thisVar, (void **)&napiParcel);
1054 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1055
1056 std::u16string parcelString = napiParcel->nativeParcel_->ReadString16();
1057 std::string outString = Str16ToStr8(parcelString.c_str());
1058 napi_value napiValue = nullptr;
1059 napi_create_string_utf8(env, outString.c_str(), outString.length(), &napiValue);
1060 return napiValue;
1061 }
1062
JS_getSize(napi_env env,napi_callback_info info)1063 napi_value NAPI_MessageParcel::JS_getSize(napi_env env, napi_callback_info info)
1064 {
1065 size_t argc = 0;
1066 napi_value thisVar = nullptr;
1067 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1068
1069 NAPI_MessageParcel *napiParcel = nullptr;
1070 napi_unwrap(env, thisVar, (void **)&napiParcel);
1071 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1072
1073 size_t value = napiParcel->nativeParcel_->GetDataSize();
1074 napi_value napiValue = nullptr;
1075 NAPI_CALL(env, napi_create_uint32(env, static_cast<uint32_t>(value), &napiValue));
1076 return napiValue;
1077 }
1078
JS_getCapacity(napi_env env,napi_callback_info info)1079 napi_value NAPI_MessageParcel::JS_getCapacity(napi_env env, napi_callback_info info)
1080 {
1081 size_t argc = 0;
1082 napi_value thisVar = nullptr;
1083 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1084
1085 NAPI_MessageParcel *napiParcel = nullptr;
1086 napi_unwrap(env, thisVar, (void **)&napiParcel);
1087 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1088
1089 size_t value = napiParcel->nativeParcel_->GetDataCapacity();
1090 napi_value napiValue = nullptr;
1091 NAPI_CALL(env, napi_create_uint32(env, static_cast<uint32_t>(value), &napiValue));
1092 return napiValue;
1093 }
1094
JS_setSize(napi_env env,napi_callback_info info)1095 napi_value NAPI_MessageParcel::JS_setSize(napi_env env, napi_callback_info info)
1096 {
1097 size_t argc = 1;
1098 napi_value argv[1] = {0};
1099 napi_value thisVar = nullptr;
1100 void *data = nullptr;
1101 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1102 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1103
1104 napi_valuetype valueType = napi_null;
1105 napi_typeof(env, argv[0], &valueType);
1106 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
1107
1108 uint32_t value = 0;
1109 napi_get_value_uint32(env, argv[0], &value);
1110
1111 NAPI_MessageParcel *napiParcel = nullptr;
1112 napi_unwrap(env, thisVar, (void **)&napiParcel);
1113 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1114
1115 bool result = napiParcel->nativeParcel_->SetDataSize(static_cast<size_t>(value));
1116 napi_value napiValue = nullptr;
1117 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
1118 return napiValue;
1119 }
1120
JS_setCapacity(napi_env env,napi_callback_info info)1121 napi_value NAPI_MessageParcel::JS_setCapacity(napi_env env, napi_callback_info info)
1122 {
1123 size_t argc = 1;
1124 napi_value argv[1] = {0};
1125 napi_value thisVar = nullptr;
1126 void *data = nullptr;
1127 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1128 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1129
1130 napi_valuetype valueType = napi_null;
1131 napi_typeof(env, argv[0], &valueType);
1132 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
1133
1134 uint32_t value = 0;
1135 napi_get_value_uint32(env, argv[0], &value);
1136
1137 NAPI_MessageParcel *napiParcel = nullptr;
1138 napi_unwrap(env, thisVar, (void **)&napiParcel);
1139 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1140
1141 bool result = napiParcel->nativeParcel_->SetDataCapacity(static_cast<size_t>(value));
1142 if (result) {
1143 napiParcel->maxCapacityToWrite_ = value;
1144 }
1145 napi_value napiValue = nullptr;
1146 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
1147 return napiValue;
1148 }
1149
JS_getWritableBytes(napi_env env,napi_callback_info info)1150 napi_value NAPI_MessageParcel::JS_getWritableBytes(napi_env env, napi_callback_info info)
1151 {
1152 size_t argc = 0;
1153 napi_value thisVar = nullptr;
1154 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1155
1156 NAPI_MessageParcel *napiParcel = nullptr;
1157 napi_unwrap(env, thisVar, (void **)&napiParcel);
1158 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1159
1160 size_t value = napiParcel->nativeParcel_->GetWritableBytes();
1161 napi_value napiValue = nullptr;
1162 NAPI_CALL(env, napi_create_uint32(env, static_cast<uint32_t>(value), &napiValue));
1163 return napiValue;
1164 }
1165
JS_getReadableBytes(napi_env env,napi_callback_info info)1166 napi_value NAPI_MessageParcel::JS_getReadableBytes(napi_env env, napi_callback_info info)
1167 {
1168 size_t argc = 0;
1169 napi_value thisVar = nullptr;
1170 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1171
1172 NAPI_MessageParcel *napiParcel = nullptr;
1173 napi_unwrap(env, thisVar, (void **)&napiParcel);
1174 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1175
1176 size_t value = napiParcel->nativeParcel_->GetReadableBytes();
1177 napi_value napiValue = nullptr;
1178 NAPI_CALL(env, napi_create_uint32(env, static_cast<uint32_t>(value), &napiValue));
1179 return napiValue;
1180 }
1181
JS_getReadPosition(napi_env env,napi_callback_info info)1182 napi_value NAPI_MessageParcel::JS_getReadPosition(napi_env env, napi_callback_info info)
1183 {
1184 size_t argc = 0;
1185 napi_value thisVar = nullptr;
1186 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1187
1188 NAPI_MessageParcel *napiParcel = nullptr;
1189 napi_unwrap(env, thisVar, (void **)&napiParcel);
1190 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1191
1192 size_t value = napiParcel->nativeParcel_->GetReadPosition();
1193 napi_value napiValue = nullptr;
1194 NAPI_CALL(env, napi_create_uint32(env, value, &napiValue));
1195 return napiValue;
1196 }
1197
JS_rewindRead(napi_env env,napi_callback_info info)1198 napi_value NAPI_MessageParcel::JS_rewindRead(napi_env env, napi_callback_info info)
1199 {
1200 size_t argc = 1;
1201 napi_value argv[1] = {0};
1202 napi_value thisVar = nullptr;
1203 void *data = nullptr;
1204 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1205 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1206
1207 napi_valuetype valueType = napi_null;
1208 napi_typeof(env, argv[0], &valueType);
1209 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
1210
1211 uint32_t pos = 0;
1212 napi_get_value_uint32(env, argv[0], &pos);
1213
1214 NAPI_MessageParcel *napiParcel = nullptr;
1215 napi_unwrap(env, thisVar, (void **)&napiParcel);
1216 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1217
1218 bool result = napiParcel->nativeParcel_->RewindRead(static_cast<size_t>(pos));
1219 napi_value napiValue = nullptr;
1220 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
1221 return napiValue;
1222 }
1223
JS_getWritePosition(napi_env env,napi_callback_info info)1224 napi_value NAPI_MessageParcel::JS_getWritePosition(napi_env env, napi_callback_info info)
1225 {
1226 size_t argc = 0;
1227 napi_value thisVar = nullptr;
1228 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1229
1230 NAPI_MessageParcel *napiParcel = nullptr;
1231 napi_unwrap(env, thisVar, (void **)&napiParcel);
1232 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1233
1234 size_t value = napiParcel->nativeParcel_->GetWritePosition();
1235 napi_value napiValue = nullptr;
1236 NAPI_CALL(env, napi_create_uint32(env, value, &napiValue));
1237 return napiValue;
1238 }
1239
JS_rewindWrite(napi_env env,napi_callback_info info)1240 napi_value NAPI_MessageParcel::JS_rewindWrite(napi_env env, napi_callback_info info)
1241 {
1242 size_t argc = 1;
1243 napi_value argv[1] = {0};
1244 napi_value thisVar = nullptr;
1245 void *data = nullptr;
1246 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1247 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1248
1249 napi_valuetype valueType = napi_null;
1250 napi_typeof(env, argv[0], &valueType);
1251 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
1252
1253 uint32_t pos = 0;
1254 napi_get_value_uint32(env, argv[0], &pos);
1255
1256 NAPI_MessageParcel *napiParcel = nullptr;
1257 napi_unwrap(env, thisVar, (void **)&napiParcel);
1258 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1259
1260 bool result = napiParcel->nativeParcel_->RewindWrite(static_cast<size_t>(pos));
1261 napi_value napiValue = nullptr;
1262 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
1263 return napiValue;
1264 }
1265
JS_writeNoException(napi_env env,napi_callback_info info)1266 napi_value NAPI_MessageParcel::JS_writeNoException(napi_env env, napi_callback_info info)
1267 {
1268 napi_value thisVar = nullptr;
1269 napi_get_cb_info(env, info, 0, nullptr, &thisVar, nullptr);
1270 NAPI_MessageParcel *napiParcel = nullptr;
1271 napi_unwrap(env, thisVar, (void **)&napiParcel);
1272 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1273 napiParcel->nativeParcel_->WriteInt32(0);
1274 napi_value result = nullptr;
1275 napi_get_undefined(env, &result);
1276 return result;
1277 }
1278
JS_readException(napi_env env,napi_callback_info info)1279 napi_value NAPI_MessageParcel::JS_readException(napi_env env, napi_callback_info info)
1280 {
1281 napi_value result = nullptr;
1282 napi_get_undefined(env, &result);
1283 napi_value thisVar = nullptr;
1284 napi_get_cb_info(env, info, 0, nullptr, &thisVar, nullptr);
1285 NAPI_MessageParcel *napiParcel = nullptr;
1286 napi_unwrap(env, thisVar, (void **)&napiParcel);
1287 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1288
1289 int32_t code = napiParcel->nativeParcel_->ReadInt32();
1290 if (code == 0) {
1291 return result;
1292 }
1293 std::u16string str = napiParcel->nativeParcel_->ReadString16();
1294 napi_throw_error(env, nullptr, Str16ToStr8(str).c_str());
1295 return result;
1296 }
1297
JS_readByteArray(napi_env env,napi_callback_info info)1298 napi_value NAPI_MessageParcel::JS_readByteArray(napi_env env, napi_callback_info info)
1299 {
1300 size_t argc = 0;
1301 napi_value thisVar = nullptr;
1302 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1303
1304 NAPI_MessageParcel *napiParcel = nullptr;
1305 napi_unwrap(env, thisVar, (void **)&napiParcel);
1306 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1307
1308 uint32_t maxBytesLen = 40960;
1309 uint32_t arrayBufferLength = napiParcel->nativeParcel_->ReadUint32();
1310 NAPI_ASSERT(env, arrayBufferLength < maxBytesLen, "byte array length too large");
1311 size_t len = (arrayBufferLength / BYTE_SIZE_32) + ((arrayBufferLength % BYTE_SIZE_32) == 0 ? 0 : 1);
1312 DBINDER_LOGI("messageparcel WriteBuffer typedarrayLength = %{public}d", (int)(len));
1313
1314 if (argc > 0) {
1315 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1316 CHECK_READ_LENGTH(env, len, BYTE_SIZE_32, napiParcel);
1317 napi_value argv[1] = {0};
1318 void *data = nullptr;
1319 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1320
1321 bool isTypedArray = false;
1322 napi_is_typedarray(env, argv[0], &isTypedArray);
1323 NAPI_ASSERT(env, isTypedArray == true, "type mismatch for parameter 1");
1324
1325 napi_typedarray_type arrayType;
1326 size_t arrayLength = 0;
1327 void *arrayBufferPtr = nullptr;
1328 napi_value tmpArrayBuffer = nullptr;
1329 size_t byteOffset = 0;
1330 napi_get_typedarray_info(env, argv[0], &arrayType, &arrayLength, &arrayBufferPtr,
1331 &tmpArrayBuffer, &byteOffset);
1332 NAPI_ASSERT(env, arrayType == napi_int8_array, "array type mismatch for parameter 1");
1333 NAPI_ASSERT(env, arrayLength == arrayBufferLength, "array size mismatch for length");
1334
1335 const uint8_t *arrayAddr = napiParcel->nativeParcel_->ReadUnpadBuffer(arrayBufferLength);
1336 NAPI_ASSERT(env, arrayAddr != nullptr, "buffer is nullptr");
1337 errno_t status = memcpy_s(arrayBufferPtr, arrayBufferLength, arrayAddr, arrayBufferLength);
1338 NAPI_ASSERT(env, status == EOK, "memcpy_s is failed");
1339
1340 napi_value napiValue = nullptr;
1341 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1342 return napiValue;
1343 }
1344
1345 CHECK_READ_LENGTH(env, len, BYTE_SIZE_32, napiParcel);
1346 napi_value arrayBuffer = nullptr;
1347 void *arrayBufferPtr = nullptr;
1348 napi_create_arraybuffer(env, arrayBufferLength, &arrayBufferPtr, &arrayBuffer);
1349 napi_value typedarray = nullptr;
1350 napi_create_typedarray(env, napi_int8_array, arrayBufferLength, arrayBuffer, 0, &typedarray);
1351 if (arrayBufferLength == 0) {
1352 return typedarray;
1353 }
1354
1355 const uint8_t *arrayAddr = napiParcel->nativeParcel_->ReadUnpadBuffer(arrayBufferLength);
1356 NAPI_ASSERT(env, arrayAddr != nullptr, "buffer is nullptr");
1357 errno_t status = memcpy_s(arrayBufferPtr, arrayBufferLength, arrayAddr, arrayBufferLength);
1358 NAPI_ASSERT(env, status == EOK, "memcpy_s is failed");
1359 return typedarray;
1360 }
1361
JS_readShortArray(napi_env env,napi_callback_info info)1362 napi_value NAPI_MessageParcel::JS_readShortArray(napi_env env, napi_callback_info info)
1363 {
1364 size_t argc = 0;
1365 napi_value thisVar = nullptr;
1366 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1367
1368 NAPI_MessageParcel *napiParcel = nullptr;
1369 napi_unwrap(env, thisVar, (void **)&napiParcel);
1370 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1371
1372 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1373 if (argc > 0) {
1374 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1375 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1376 napi_value argv[1] = {0};
1377 void *data = nullptr;
1378 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1379
1380 bool isArray = false;
1381 napi_is_array(env, argv[0], &isArray);
1382 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1383
1384 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1385 int16_t val = napiParcel->nativeParcel_->ReadInt16();
1386 napi_value num = nullptr;
1387 napi_create_int32(env, val, &num);
1388 napi_set_element(env, argv[0], i, num);
1389 }
1390 napi_value napiValue = nullptr;
1391 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1392 return napiValue;
1393 }
1394
1395 if (arrayLength <= 0) {
1396 napi_value result = nullptr;
1397 napi_create_array(env, &result);
1398 return result;
1399 }
1400 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1401 napi_value result = nullptr;
1402 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1403
1404 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1405 int16_t val = napiParcel->nativeParcel_->ReadInt16();
1406 napi_value num = nullptr;
1407 napi_create_int32(env, val, &num);
1408 napi_set_element(env, result, i, num);
1409 }
1410 return result;
1411 }
1412
JS_readIntArray(napi_env env,napi_callback_info info)1413 napi_value NAPI_MessageParcel::JS_readIntArray(napi_env env, napi_callback_info info)
1414 {
1415 size_t argc = 0;
1416 napi_value thisVar = nullptr;
1417 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1418
1419 NAPI_MessageParcel *napiParcel = nullptr;
1420 napi_unwrap(env, thisVar, (void **)&napiParcel);
1421 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1422
1423 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1424 if (argc > 0) {
1425 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1426 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1427 napi_value argv[1] = {0};
1428 void *data = nullptr;
1429 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1430
1431 bool isArray = false;
1432 napi_is_array(env, argv[0], &isArray);
1433 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1434
1435 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1436 int32_t val = napiParcel->nativeParcel_->ReadInt32();
1437 napi_value num = nullptr;
1438 napi_create_int32(env, val, &num);
1439 napi_set_element(env, argv[0], i, num);
1440 }
1441 napi_value napiValue = nullptr;
1442 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1443 return napiValue;
1444 }
1445
1446 if (arrayLength <= 0) {
1447 napi_value result = nullptr;
1448 napi_create_array(env, &result);
1449 return result;
1450 }
1451 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1452 napi_value result = nullptr;
1453 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1454
1455 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1456 int32_t val = napiParcel->nativeParcel_->ReadInt32();
1457 napi_value num = nullptr;
1458 napi_create_int32(env, val, &num);
1459 napi_set_element(env, result, i, num);
1460 }
1461 return result;
1462 }
1463
JS_readLongArray(napi_env env,napi_callback_info info)1464 napi_value NAPI_MessageParcel::JS_readLongArray(napi_env env, napi_callback_info info)
1465 {
1466 size_t argc = 0;
1467 napi_value thisVar = nullptr;
1468 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1469
1470 NAPI_MessageParcel *napiParcel = nullptr;
1471 napi_unwrap(env, thisVar, (void **)&napiParcel);
1472 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1473
1474 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1475 if (argc > 0) {
1476 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1477 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_64, napiParcel);
1478 napi_value argv[1] = {0};
1479 void *data = nullptr;
1480 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1481
1482 bool isArray = false;
1483 napi_is_array(env, argv[0], &isArray);
1484 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1485
1486 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1487 int64_t val = napiParcel->nativeParcel_->ReadInt64();
1488 napi_value num = nullptr;
1489 napi_create_int64(env, val, &num);
1490 napi_set_element(env, argv[0], i, num);
1491 }
1492 napi_value napiValue = nullptr;
1493 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1494 return napiValue;
1495 }
1496
1497 if (arrayLength <= 0) {
1498 napi_value result = nullptr;
1499 napi_create_array(env, &result);
1500 return result;
1501 }
1502 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_64, napiParcel);
1503 napi_value result = nullptr;
1504 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1505
1506 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1507 int64_t val = napiParcel->nativeParcel_->ReadInt64();
1508 napi_value num = nullptr;
1509 napi_create_int64(env, val, &num);
1510 napi_set_element(env, result, i, num);
1511 }
1512 return result;
1513 }
1514
JS_readFloatArray(napi_env env,napi_callback_info info)1515 napi_value NAPI_MessageParcel::JS_readFloatArray(napi_env env, napi_callback_info info)
1516 {
1517 size_t argc = 0;
1518 napi_value thisVar = nullptr;
1519 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1520
1521 NAPI_MessageParcel *napiParcel = nullptr;
1522 napi_unwrap(env, thisVar, (void **)&napiParcel);
1523 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1524
1525 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1526 if (argc > 0) {
1527 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1528 CHECK_READ_LENGTH(env, (size_t)arrayLength, sizeof(double), napiParcel);
1529 napi_value argv[1] = {0};
1530 void *data = nullptr;
1531 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1532
1533 bool isArray = false;
1534 napi_is_array(env, argv[0], &isArray);
1535 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1536
1537 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1538 double val = napiParcel->nativeParcel_->ReadDouble();
1539 napi_value num = nullptr;
1540 napi_create_double(env, val, &num);
1541 napi_set_element(env, argv[0], i, num);
1542 }
1543 napi_value napiValue = nullptr;
1544 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1545 return napiValue;
1546 }
1547
1548 if (arrayLength <= 0) {
1549 napi_value result = nullptr;
1550 napi_create_array(env, &result);
1551 return result;
1552 }
1553 CHECK_READ_LENGTH(env, (size_t)arrayLength, sizeof(double), napiParcel);
1554 napi_value result = nullptr;
1555 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1556
1557 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1558 double val = napiParcel->nativeParcel_->ReadDouble();
1559 napi_value num = nullptr;
1560 napi_create_double(env, val, &num);
1561 napi_set_element(env, result, i, num);
1562 }
1563 return result;
1564 }
1565
JS_readDoubleArray(napi_env env,napi_callback_info info)1566 napi_value NAPI_MessageParcel::JS_readDoubleArray(napi_env env, napi_callback_info info)
1567 {
1568 size_t argc = 0;
1569 napi_value thisVar = nullptr;
1570 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1571
1572 NAPI_MessageParcel *napiParcel = nullptr;
1573 napi_unwrap(env, thisVar, (void **)&napiParcel);
1574 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1575
1576 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1577 if (argc > 0) {
1578 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1579 CHECK_READ_LENGTH(env, (size_t)arrayLength, sizeof(double), napiParcel);
1580 napi_value argv[1] = {0};
1581 void *data = nullptr;
1582 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1583
1584 bool isArray = false;
1585 napi_is_array(env, argv[0], &isArray);
1586 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1587
1588 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1589 double val = napiParcel->nativeParcel_->ReadDouble();
1590 napi_value num = nullptr;
1591 napi_create_double(env, val, &num);
1592 napi_set_element(env, argv[0], i, num);
1593 }
1594 napi_value napiValue = nullptr;
1595 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1596 return napiValue;
1597 }
1598
1599 if (arrayLength <= 0) {
1600 napi_value result = nullptr;
1601 napi_create_array(env, &result);
1602 return result;
1603 }
1604 CHECK_READ_LENGTH(env, (size_t)arrayLength, sizeof(double), napiParcel);
1605 napi_value result = nullptr;
1606 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1607
1608 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1609 double val = napiParcel->nativeParcel_->ReadDouble();
1610 napi_value num = nullptr;
1611 napi_create_double(env, val, &num);
1612 napi_set_element(env, result, i, num);
1613 }
1614 return result;
1615 }
1616
JS_readBooleanArray(napi_env env,napi_callback_info info)1617 napi_value NAPI_MessageParcel::JS_readBooleanArray(napi_env env, napi_callback_info info)
1618 {
1619 size_t argc = 0;
1620 napi_value thisVar = nullptr;
1621 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1622
1623 NAPI_MessageParcel *napiParcel = nullptr;
1624 napi_unwrap(env, thisVar, (void **)&napiParcel);
1625 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1626
1627 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1628 if (argc > 0) {
1629 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1630 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1631 napi_value argv[1] = {0};
1632 void *data = nullptr;
1633 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1634
1635 bool isArray = false;
1636 napi_is_array(env, argv[0], &isArray);
1637 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1638
1639 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1640 int8_t val = napiParcel->nativeParcel_->ReadInt8();
1641 napi_value boolean = nullptr;
1642 napi_get_boolean(env, val, &boolean);
1643 napi_set_element(env, argv[0], i, boolean);
1644 }
1645 napi_value napiValue = nullptr;
1646 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1647 return napiValue;
1648 }
1649
1650 if (arrayLength <= 0) {
1651 napi_value result = nullptr;
1652 napi_create_array(env, &result);
1653 return result;
1654 }
1655
1656 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1657 napi_value result = nullptr;
1658 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1659
1660 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1661 int8_t val = napiParcel->nativeParcel_->ReadInt8();
1662 napi_value boolean = nullptr;
1663 napi_get_boolean(env, val, &boolean);
1664 napi_set_element(env, result, i, boolean);
1665 }
1666 return result;
1667 }
1668
JS_readCharArray(napi_env env,napi_callback_info info)1669 napi_value NAPI_MessageParcel::JS_readCharArray(napi_env env, napi_callback_info info)
1670 {
1671 size_t argc = 0;
1672 napi_value thisVar = nullptr;
1673 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1674
1675 NAPI_MessageParcel *napiParcel = nullptr;
1676 napi_unwrap(env, thisVar, (void **)&napiParcel);
1677 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1678
1679 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1680 if (argc > 0) {
1681 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1682 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1683 napi_value argv[1] = {0};
1684 void *data = nullptr;
1685 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1686
1687 bool isArray = false;
1688 napi_is_array(env, argv[0], &isArray);
1689 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1690
1691 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1692 uint16_t val = napiParcel->nativeParcel_->ReadUint16();
1693 char ch[2] = { 0 };
1694 if (sprintf_s(ch, sizeof(ch) / sizeof(ch[0]), "%c", val) < 0) {
1695 return nullptr;
1696 }
1697 napi_value num = nullptr;
1698 napi_create_string_utf8(env, ch, 1, &num);
1699 napi_set_element(env, argv[0], i, num);
1700 }
1701 napi_value napiValue = nullptr;
1702 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1703 return napiValue;
1704 }
1705
1706 if (arrayLength <= 0) {
1707 napi_value result = nullptr;
1708 napi_create_array(env, &result);
1709 return result;
1710 }
1711 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1712 napi_value result = nullptr;
1713 napi_create_array_with_length(env, (size_t)arrayLength, &result);
1714
1715 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1716 uint16_t val = napiParcel->nativeParcel_->ReadUint16();
1717 char ch[2] = { 0 };
1718 if (sprintf_s(ch, sizeof(ch) / sizeof(ch[0]), "%c", val) < 0) {
1719 return nullptr;
1720 }
1721 napi_value num = nullptr;
1722 napi_create_string_utf8(env, ch, 1, &num);
1723 napi_set_element(env, result, i, num);
1724 }
1725 return result;
1726 }
1727
JS_readStringArray(napi_env env,napi_callback_info info)1728 napi_value NAPI_MessageParcel::JS_readStringArray(napi_env env, napi_callback_info info)
1729 {
1730 size_t argc = 0;
1731 napi_value thisVar = nullptr;
1732 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1733
1734 NAPI_MessageParcel *napiParcel = nullptr;
1735 napi_unwrap(env, thisVar, (void **)&napiParcel);
1736 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1737
1738 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1739 if (argc > 0) {
1740 NAPI_ASSERT(env, argc == 1, "type mismatch for parameter 1");
1741 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1742 napi_value argv[1] = {0};
1743 void *data = nullptr;
1744 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1745
1746 bool isArray = false;
1747 napi_is_array(env, argv[0], &isArray);
1748 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1749
1750 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1751 if (napiParcel->nativeParcel_->GetReadableBytes() <= 0) {
1752 break;
1753 }
1754 std::u16string parcelString = napiParcel->nativeParcel_->ReadString16();
1755 std::string outString = Str16ToStr8(parcelString.c_str());
1756 napi_value val = nullptr;
1757 napi_create_string_utf8(env, outString.c_str(), outString.length(), &val);
1758 napi_set_element(env, argv[0], i, val);
1759 }
1760 napi_value napiValue = nullptr;
1761 NAPI_CALL(env, napi_get_boolean(env, true, &napiValue));
1762 return napiValue;
1763 }
1764
1765 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_32, napiParcel);
1766 napi_value result = nullptr;
1767 napi_create_array(env, &result);
1768 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1769 if (napiParcel->nativeParcel_->GetReadableBytes() <= 0) {
1770 break;
1771 }
1772 std::u16string parcelString = napiParcel->nativeParcel_->ReadString16();
1773 std::string outString = Str16ToStr8(parcelString.c_str());
1774 napi_value val = nullptr;
1775 napi_create_string_utf8(env, outString.c_str(), outString.length(), &val);
1776 napi_set_element(env, result, i, val);
1777 }
1778 return result;
1779 }
1780
JS_readSequenceableArray(napi_env env,napi_callback_info info)1781 napi_value NAPI_MessageParcel::JS_readSequenceableArray(napi_env env, napi_callback_info info)
1782 {
1783 size_t argc = 1;
1784 napi_value thisVar = nullptr;
1785 napi_value argv[1] = { 0 };
1786 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1787 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1788
1789 NAPI_MessageParcel *napiParcel = nullptr;
1790 napi_unwrap(env, thisVar, (void **)&napiParcel);
1791 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1792
1793 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1794 // checking here is not accurate, but we can defend some extreme attacking case.
1795 CHECK_READ_LENGTH(env, (size_t)arrayLength, BYTE_SIZE_8, napiParcel);
1796
1797 bool isArray = false;
1798 napi_is_array(env, argv[0], &isArray);
1799 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1800 uint32_t length = 0;
1801 napi_get_array_length(env, argv[0], &length);
1802 if (static_cast<int32_t>(length) != arrayLength) {
1803 napi_value result = nullptr;
1804 napi_get_undefined(env, &result);
1805 napi_throw_error(env, nullptr, "Bad length while reading Sequenceable array");
1806 return result;
1807 }
1808
1809 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1810 int32_t len = napiParcel->nativeParcel_->ReadInt32();
1811 if (len > 0) {
1812 bool hasElement = false;
1813 napi_has_element(env, argv[0], i, &hasElement);
1814 NAPI_ASSERT(env, hasElement == true, "parameter check error");
1815 napi_value element = nullptr;
1816 napi_get_element(env, argv[0], i, &element);
1817
1818 napi_value propKey = nullptr;
1819 const char *propKeyStr = "unmarshalling";
1820 napi_create_string_utf8(env, propKeyStr, strlen(propKeyStr), &propKey);
1821 napi_value prop = nullptr;
1822 napi_get_property(env, element, propKey, &prop);
1823
1824 napi_value funcArg[1] = { thisVar };
1825 napi_value callResult = nullptr;
1826 napi_call_function(env, element, prop, 1, funcArg, &callResult);
1827 if (callResult == nullptr) {
1828 DBINDER_LOGE("call unmarshalling failed, element index: %{public}d", i);
1829 break;
1830 }
1831 }
1832 }
1833 napi_value result = nullptr;
1834 napi_get_undefined(env, &result);
1835 return result;
1836 }
1837
JS_readRemoteObjectArray(napi_env env,napi_callback_info info)1838 napi_value NAPI_MessageParcel::JS_readRemoteObjectArray(napi_env env, napi_callback_info info)
1839 {
1840 napi_value result = nullptr;
1841 napi_get_undefined(env, &result);
1842
1843 size_t argc = 0;
1844 napi_value argv[1] = { 0 };
1845 napi_value thisVar = nullptr;
1846 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1847
1848 NAPI_MessageParcel *napiParcel = nullptr;
1849 napi_unwrap(env, thisVar, (void **)&napiParcel);
1850 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1851
1852 int32_t arrayLength = napiParcel->nativeParcel_->ReadInt32();
1853 if (argc > 0) { // uses passed in array
1854 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1855 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1856 bool isArray = false;
1857 napi_is_array(env, argv[0], &isArray);
1858 NAPI_ASSERT(env, isArray == true, "type mismatch for parameter 1");
1859 uint32_t length = 0;
1860 napi_get_array_length(env, argv[0], &length);
1861 if (static_cast<int32_t>(length) != arrayLength) {
1862 return result;
1863 }
1864 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1865 sptr<IRemoteObject> value = napiParcel->nativeParcel_->ReadRemoteObject();
1866 napi_value napiValue = NAPI_ohos_rpc_CreateJsRemoteObject(env, value);
1867 napi_set_element(env, argv[0], i, napiValue);
1868 }
1869 return result;
1870 }
1871
1872 if (arrayLength <= 0) {
1873 napi_get_null(env, &result);
1874 return result;
1875 }
1876 napi_create_array(env, &result);
1877 for (uint32_t i = 0; i < (uint32_t)arrayLength; i++) {
1878 sptr<IRemoteObject> value = napiParcel->nativeParcel_->ReadRemoteObject();
1879 napi_value napiValue = NAPI_ohos_rpc_CreateJsRemoteObject(env, value);
1880 napi_set_element(env, result, i, napiValue);
1881 }
1882 return result;
1883 }
1884
JS_readSequenceable(napi_env env,napi_callback_info info)1885 napi_value NAPI_MessageParcel::JS_readSequenceable(napi_env env, napi_callback_info info)
1886 {
1887 size_t argc = 1;
1888 napi_value argv[1] = {0};
1889 napi_value thisVar = nullptr;
1890 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1891 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1892
1893 NAPI_MessageParcel *napiParcel = nullptr;
1894 napi_unwrap(env, thisVar, (void **)&napiParcel);
1895 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1896
1897 int32_t len = napiParcel->nativeParcel_->ReadInt32();
1898 if (len > 0) {
1899 napi_value propKey = nullptr;
1900 const char *propKeyStr = "unmarshalling";
1901 napi_create_string_utf8(env, propKeyStr, strlen(propKeyStr), &propKey);
1902 napi_value prop = nullptr;
1903 napi_get_property(env, argv[0], propKey, &prop);
1904
1905 napi_value funcArg[1] = {thisVar};
1906 napi_value callResult = nullptr;
1907 napi_call_function(env, argv[0], prop, 1, funcArg, &callResult);
1908 if (callResult != nullptr) {
1909 return callResult;
1910 }
1911 DBINDER_LOGI("call unmarshalling failed");
1912 }
1913
1914 napi_value napiValue = nullptr;
1915 NAPI_CALL(env, napi_get_boolean(env, false, &napiValue));
1916 return napiValue;
1917 }
1918
JS_create(napi_env env,napi_callback_info info)1919 napi_value NAPI_MessageParcel::JS_create(napi_env env, napi_callback_info info)
1920 {
1921 // new native parcel object
1922 napi_value global = nullptr;
1923 napi_status status = napi_get_global(env, &global);
1924 NAPI_ASSERT(env, status == napi_ok, "get napi global failed");
1925 napi_value constructor = nullptr;
1926 status = napi_get_named_property(env, global, "IPCParcelConstructor_", &constructor);
1927 NAPI_ASSERT(env, status == napi_ok, "get message parcel constructor failed");
1928 napi_value jsMessageParcel;
1929 status = napi_new_instance(env, constructor, 0, nullptr, &jsMessageParcel);
1930 NAPI_ASSERT(env, status == napi_ok, "failed to construct js MessageParcel");
1931 return jsMessageParcel;
1932 }
1933
JS_reclaim(napi_env env,napi_callback_info info)1934 napi_value NAPI_MessageParcel::JS_reclaim(napi_env env, napi_callback_info info)
1935 {
1936 size_t argc = 0;
1937 napi_value thisVar = nullptr;
1938 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1939
1940 NAPI_MessageParcel *napiParcel = nullptr;
1941 napi_remove_wrap(env, thisVar, (void **)&napiParcel);
1942 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1943 delete napiParcel;
1944
1945 napi_value result = nullptr;
1946 napi_get_undefined(env, &result);
1947 return result;
1948 }
1949
JS_writeRemoteObject(napi_env env,napi_callback_info info)1950 napi_value NAPI_MessageParcel::JS_writeRemoteObject(napi_env env, napi_callback_info info)
1951 {
1952 size_t argc = 1;
1953 napi_value argv[1] = { 0 };
1954 napi_value thisVar = nullptr;
1955 void *data = nullptr;
1956 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1957 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
1958 napi_valuetype valueType = napi_null;
1959 napi_typeof(env, argv[0], &valueType);
1960 napi_value napiValue = nullptr;
1961 if (valueType != napi_object) {
1962 napi_get_boolean(env, false, &napiValue);
1963 return napiValue;
1964 }
1965 sptr<IRemoteObject> remoteObject = NAPI_ohos_rpc_getNativeRemoteObject(env, argv[0]);
1966 if (remoteObject == nullptr) {
1967 napi_get_boolean(env, false, &napiValue);
1968 return napiValue;
1969 }
1970 NAPI_MessageParcel *napiParcel = nullptr;
1971 napi_unwrap(env, thisVar, (void **)&napiParcel);
1972 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
1973 bool result = napiParcel->nativeParcel_->WriteRemoteObject(remoteObject);
1974 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
1975 return napiValue;
1976 }
1977
JS_readRemoteObject(napi_env env,napi_callback_info info)1978 napi_value NAPI_MessageParcel::JS_readRemoteObject(napi_env env, napi_callback_info info)
1979 {
1980 size_t argc = 0;
1981 napi_value thisVar = nullptr;
1982 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
1983
1984 NAPI_MessageParcel *napiParcel = nullptr;
1985 napi_unwrap(env, thisVar, (void **)&napiParcel);
1986 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
1987
1988 sptr<IRemoteObject> value = napiParcel->nativeParcel_->ReadRemoteObject();
1989 napi_value napiValue = NAPI_ohos_rpc_CreateJsRemoteObject(env, value);
1990 return napiValue;
1991 }
1992
JS_writeInterfaceToken(napi_env env,napi_callback_info info)1993 napi_value NAPI_MessageParcel::JS_writeInterfaceToken(napi_env env, napi_callback_info info)
1994 {
1995 size_t argc = 1;
1996 napi_value argv[1] = { 0 };
1997 napi_value thisVar = nullptr;
1998 void *data = nullptr;
1999 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
2000 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
2001
2002 napi_valuetype valueType = napi_null;
2003 napi_typeof(env, argv[0], &valueType);
2004 NAPI_ASSERT(env, valueType == napi_string, "type mismatch for parameter");
2005
2006 size_t bufferSize = 0;
2007 size_t maxSize = 40960;
2008 napi_get_value_string_utf8(env, argv[0], nullptr, 0, &bufferSize);
2009 NAPI_ASSERT(env, bufferSize < maxSize, "string length too large");
2010
2011 char stringValue[bufferSize + 1];
2012 size_t jsStringLength = 0;
2013 napi_get_value_string_utf8(env, argv[0], stringValue, bufferSize + 1, &jsStringLength);
2014 NAPI_ASSERT(env, jsStringLength == bufferSize, "string length wrong");
2015
2016 NAPI_MessageParcel *napiParcel = nullptr;
2017 napi_unwrap(env, thisVar, (void **)&napiParcel);
2018 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
2019
2020 std::string parcelString = stringValue;
2021 bool result = napiParcel->nativeParcel_->WriteInterfaceToken(to_utf16(parcelString));
2022
2023 napi_value napiValue = nullptr;
2024 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
2025 return napiValue;
2026 }
2027
JS_readInterfaceToken(napi_env env,napi_callback_info info)2028 napi_value NAPI_MessageParcel::JS_readInterfaceToken(napi_env env, napi_callback_info info)
2029 {
2030 size_t argc = 0;
2031 napi_value thisVar = nullptr;
2032 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
2033
2034 NAPI_MessageParcel *napiParcel = nullptr;
2035 napi_unwrap(env, thisVar, (void **)&napiParcel);
2036 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2037
2038 std::u16string parcelString = napiParcel->nativeParcel_->ReadInterfaceToken();
2039 std::string outString = Str16ToStr8(parcelString.c_str());
2040 napi_value napiValue = nullptr;
2041 napi_create_string_utf8(env, outString.c_str(), outString.length(), &napiValue);
2042 return napiValue;
2043 }
2044
JS_CloseFileDescriptor(napi_env env,napi_callback_info info)2045 napi_value NAPI_MessageParcel::JS_CloseFileDescriptor(napi_env env, napi_callback_info info)
2046 {
2047 size_t argc = 1;
2048 napi_value argv[1] = { 0 };
2049 napi_value thisVar = nullptr;
2050 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2051 NAPI_ASSERT(env, argc == 1, "requires 1 parameters");
2052 napi_valuetype valueType = napi_null;
2053 napi_typeof(env, argv[0], &valueType);
2054 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
2055 int32_t fd = -1;
2056 napi_get_value_int32(env, argv[0], &fd);
2057 close(fd);
2058 napi_value result = nullptr;
2059 napi_get_undefined(env, &result);
2060 return result;
2061 }
2062
JS_DupFileDescriptor(napi_env env,napi_callback_info info)2063 napi_value NAPI_MessageParcel::JS_DupFileDescriptor(napi_env env, napi_callback_info info)
2064 {
2065 size_t argc = 1;
2066 napi_value argv[1] = { 0 };
2067 napi_value thisVar = nullptr;
2068 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2069 NAPI_ASSERT(env, argc == 1, "requires 1 parameters");
2070 napi_valuetype valueType = napi_null;
2071 napi_typeof(env, argv[0], &valueType);
2072 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
2073 int32_t fd = -1;
2074 napi_get_value_int32(env, argv[0], &fd);
2075 int32_t dupResult = dup(fd);
2076 napi_value napiValue;
2077 napi_create_int32(env, dupResult, &napiValue);
2078 return napiValue;
2079 }
2080
JS_ContainFileDescriptors(napi_env env,napi_callback_info info)2081 napi_value NAPI_MessageParcel::JS_ContainFileDescriptors(napi_env env, napi_callback_info info)
2082 {
2083 size_t argc = 0;
2084 napi_value thisVar = nullptr;
2085 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
2086 NAPI_MessageParcel *napiParcel = nullptr;
2087 napi_unwrap(env, thisVar, (void **)&napiParcel);
2088 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2089 bool result = napiParcel->nativeParcel_->ContainFileDescriptors();
2090 napi_value napiValue = nullptr;
2091 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
2092 return napiValue;
2093 }
2094
JS_WriteFileDescriptor(napi_env env,napi_callback_info info)2095 napi_value NAPI_MessageParcel::JS_WriteFileDescriptor(napi_env env, napi_callback_info info)
2096 {
2097 size_t argc = 1;
2098 napi_value argv[1] = { 0 };
2099 napi_value thisVar = nullptr;
2100 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2101 NAPI_ASSERT(env, argc == 1, "requires 1 parameters");
2102 napi_valuetype valueType = napi_null;
2103 napi_typeof(env, argv[0], &valueType);
2104 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
2105 int32_t fd = -1;
2106 napi_get_value_int32(env, argv[0], &fd);
2107 NAPI_MessageParcel *napiParcel = nullptr;
2108 napi_unwrap(env, thisVar, (void **)&napiParcel);
2109 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2110 bool result = napiParcel->nativeParcel_->WriteFileDescriptor(fd);
2111 napi_value napiValue = nullptr;
2112 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
2113 return napiValue;
2114 }
2115
JS_ReadFileDescriptor(napi_env env,napi_callback_info info)2116 napi_value NAPI_MessageParcel::JS_ReadFileDescriptor(napi_env env, napi_callback_info info)
2117 {
2118 size_t argc = 0;
2119 napi_value thisVar = nullptr;
2120 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
2121 NAPI_MessageParcel *napiParcel = nullptr;
2122 napi_unwrap(env, thisVar, (void **)&napiParcel);
2123 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2124 int32_t result = napiParcel->nativeParcel_->ReadFileDescriptor();
2125 napi_value napiValue;
2126 napi_create_int32(env, result, &napiValue);
2127 return napiValue;
2128 }
2129
JS_WriteAshmem(napi_env env,napi_callback_info info)2130 napi_value NAPI_MessageParcel::JS_WriteAshmem(napi_env env, napi_callback_info info)
2131 {
2132 size_t argc = 1;
2133 napi_value argv[1] = { 0 };
2134 napi_value thisVar = nullptr;
2135 void *data = nullptr;
2136 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
2137 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
2138 // check type is Ashmem
2139 napi_value global = nullptr;
2140 napi_status status = napi_get_global(env, &global);
2141 NAPI_ASSERT(env, status == napi_ok, "get napi global failed");
2142 napi_value constructor = nullptr;
2143 status = napi_get_named_property(env, global, "AshmemConstructor_", &constructor);
2144 NAPI_ASSERT(env, status == napi_ok, "get Ashmem constructor failed");
2145 bool isAshmem = false;
2146 napi_instanceof(env, argv[0], constructor, &isAshmem);
2147 NAPI_ASSERT(env, isAshmem == true, "parameter is not instanceof Ashmem");
2148 NAPIAshmem *napiAshmem = nullptr;
2149 napi_unwrap(env, argv[0], (void **)&napiAshmem);
2150 NAPI_ASSERT(env, napiAshmem != nullptr, "napiAshmem is null");
2151 sptr<Ashmem> nativeAshmem = napiAshmem->GetAshmem();
2152 NAPI_MessageParcel *napiParcel = nullptr;
2153 napi_unwrap(env, thisVar, (void **)&napiParcel);
2154 NAPI_ASSERT(env, napiParcel != nullptr, "napiParcel is null");
2155 bool result = napiParcel->nativeParcel_->WriteAshmem(nativeAshmem);
2156 napi_value napiValue = nullptr;
2157 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
2158 return napiValue;
2159 }
2160
JS_ReadAshmem(napi_env env,napi_callback_info info)2161 napi_value NAPI_MessageParcel::JS_ReadAshmem(napi_env env, napi_callback_info info)
2162 {
2163 size_t argc = 0;
2164 napi_value thisVar = nullptr;
2165 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
2166
2167 NAPI_MessageParcel *napiParcel = nullptr;
2168 napi_unwrap(env, thisVar, (void **)&napiParcel);
2169 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2170 sptr<Ashmem> nativeAshmem = napiParcel->nativeParcel_->ReadAshmem();
2171 napi_value global = nullptr;
2172 napi_status status = napi_get_global(env, &global);
2173 NAPI_ASSERT(env, status == napi_ok, "get napi global failed");
2174 napi_value constructor = nullptr;
2175 status = napi_get_named_property(env, global, "AshmemConstructor_", &constructor);
2176 NAPI_ASSERT(env, status == napi_ok, "get Ashmem constructor failed");
2177 napi_value jsAshmem;
2178 status = napi_new_instance(env, constructor, 0, nullptr, &jsAshmem);
2179 NAPI_ASSERT(env, status == napi_ok, "failed to construct js Ashmem");
2180 NAPIAshmem *napiAshmem = nullptr;
2181 napi_unwrap(env, jsAshmem, (void **)&napiAshmem);
2182 NAPI_ASSERT(env, napiAshmem != nullptr, "napiAshmem is null");
2183 napiAshmem->SetAshmem(nativeAshmem);
2184 return jsAshmem;
2185 }
JS_WriteRawData(napi_env env,napi_callback_info info)2186 napi_value NAPI_MessageParcel::JS_WriteRawData(napi_env env, napi_callback_info info)
2187 {
2188 size_t argc = 2;
2189 napi_value argv[2] = {0};
2190 napi_value thisVar = nullptr;
2191 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2192 NAPI_ASSERT(env, argc == 2, "requires 2 parameter");
2193 bool isTypedArray = false;
2194 napi_is_typedarray(env, argv[0], &isTypedArray);
2195 NAPI_ASSERT(env, isTypedArray == true, "type mismatch for parameter 1");
2196 napi_typedarray_type typedarrayType = napi_uint8_array;
2197 size_t typedarrayLength = 0;
2198 void *typedarrayBufferPtr = nullptr;
2199 napi_value tmpArrayBuffer = nullptr;
2200 size_t byteOffset = 0;
2201 napi_get_typedarray_info(env, argv[0], &typedarrayType, &typedarrayLength, &typedarrayBufferPtr,
2202 &tmpArrayBuffer, &byteOffset);
2203 NAPI_ASSERT(env, typedarrayType == napi_int8_array, "array type mismatch for parameter 1");
2204 // get Array size
2205 napi_valuetype valueType = napi_null;
2206 napi_typeof(env, argv[1], &valueType);
2207 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 2");
2208 int32_t size = 0;
2209 napi_get_value_int32(env, argv[1], &size);
2210 NAPI_MessageParcel *napiParcel = nullptr;
2211 napi_unwrap(env, thisVar, (void **)&napiParcel);
2212 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2213 bool result = napiParcel->nativeParcel_->WriteRawData(typedarrayBufferPtr, size);
2214 napi_value napiValue = nullptr;
2215 NAPI_CALL(env, napi_get_boolean(env, result, &napiValue));
2216 return napiValue;
2217 }
2218
JS_ReadRawData(napi_env env,napi_callback_info info)2219 napi_value NAPI_MessageParcel::JS_ReadRawData(napi_env env, napi_callback_info info)
2220 {
2221 size_t argc = 1;
2222 napi_value argv[1] = { 0 };
2223 napi_value thisVar = nullptr;
2224 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2225 NAPI_ASSERT(env, argc == 1, "requires 1 parameters");
2226 napi_valuetype valueType = napi_null;
2227 napi_typeof(env, argv[0], &valueType);
2228 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
2229 int32_t size = 0;
2230 napi_get_value_int32(env, argv[0], &size);
2231 NAPI_MessageParcel *napiParcel = nullptr;
2232 napi_unwrap(env, thisVar, (void **)&napiParcel);
2233 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2234 const void *rawData = napiParcel->nativeParcel_->ReadRawData(size);
2235 NAPI_ASSERT_BASE(env, rawData != nullptr, "rawData is null", 0);
2236 // [c++] rawData -> byteBuffer()[js]
2237 napi_value arrayBuffer = nullptr;
2238 void *arrayBufferPtr = nullptr;
2239 napi_create_arraybuffer(env, (int32_t)size, &arrayBufferPtr, &arrayBuffer);
2240 napi_value typedarray = nullptr;
2241 napi_create_typedarray(env, napi_int8_array, (int32_t)size, arrayBuffer, 0, &typedarray);
2242 bool isTypedArray = false;
2243 napi_is_typedarray(env, typedarray, &isTypedArray);
2244 NAPI_ASSERT(env, isTypedArray == true, "create TypedArray failed");
2245 if (size == 0) {
2246 return typedarray;
2247 }
2248 errno_t status = memcpy_s(arrayBufferPtr, size, rawData, size);
2249 NAPI_ASSERT(env, status == EOK, "memcpy_s is failed");
2250 return typedarray;
2251 }
2252
JS_GetRawDataCapacity(napi_env env,napi_callback_info info)2253 napi_value NAPI_MessageParcel::JS_GetRawDataCapacity(napi_env env, napi_callback_info info)
2254 {
2255 size_t argc = 0;
2256 napi_value thisVar = nullptr;
2257 napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr);
2258 NAPI_MessageParcel *napiParcel = nullptr;
2259 napi_unwrap(env, thisVar, (void **)&napiParcel);
2260 NAPI_ASSERT_BASE(env, napiParcel != nullptr, "napiParcel is null", 0);
2261 uint32_t result = napiParcel->nativeParcel_->GetRawDataCapacity();
2262 napi_value napiValue;
2263 napi_create_uint32(env, result, &napiValue);
2264 return napiValue;
2265 }
2266
Export(napi_env env,napi_value exports)2267 napi_value NAPI_MessageParcel::Export(napi_env env, napi_value exports)
2268 {
2269 const std::string className = "MessageParcel";
2270 napi_property_descriptor properties[] = {
2271 DECLARE_NAPI_STATIC_FUNCTION("create", NAPI_MessageParcel::JS_create),
2272 DECLARE_NAPI_FUNCTION("reclaim", NAPI_MessageParcel::JS_reclaim),
2273 DECLARE_NAPI_FUNCTION("writeRemoteObject", NAPI_MessageParcel::JS_writeRemoteObject),
2274 DECLARE_NAPI_FUNCTION("readRemoteObject", NAPI_MessageParcel::JS_readRemoteObject),
2275 DECLARE_NAPI_FUNCTION("writeInterfaceToken", NAPI_MessageParcel::JS_writeInterfaceToken),
2276 DECLARE_NAPI_FUNCTION("readInterfaceToken", NAPI_MessageParcel::JS_readInterfaceToken),
2277 DECLARE_NAPI_FUNCTION("getSize", NAPI_MessageParcel::JS_getSize),
2278 DECLARE_NAPI_FUNCTION("getCapacity", NAPI_MessageParcel::JS_getCapacity),
2279 DECLARE_NAPI_FUNCTION("setSize", NAPI_MessageParcel::JS_setSize),
2280 DECLARE_NAPI_FUNCTION("setCapacity", NAPI_MessageParcel::JS_setCapacity),
2281 DECLARE_NAPI_FUNCTION("getWritableBytes", NAPI_MessageParcel::JS_getWritableBytes),
2282 DECLARE_NAPI_FUNCTION("getReadableBytes", NAPI_MessageParcel::JS_getReadableBytes),
2283 DECLARE_NAPI_FUNCTION("getReadPosition", NAPI_MessageParcel::JS_getReadPosition),
2284 DECLARE_NAPI_FUNCTION("getWritePosition", NAPI_MessageParcel::JS_getWritePosition),
2285 DECLARE_NAPI_FUNCTION("rewindRead", NAPI_MessageParcel::JS_rewindRead),
2286 DECLARE_NAPI_FUNCTION("rewindWrite", NAPI_MessageParcel::JS_rewindWrite),
2287 DECLARE_NAPI_FUNCTION("writeNoException", NAPI_MessageParcel::JS_writeNoException),
2288 DECLARE_NAPI_FUNCTION("readException", NAPI_MessageParcel::JS_readException),
2289 DECLARE_NAPI_FUNCTION("writeByte", NAPI_MessageParcel::JS_writeByte),
2290 DECLARE_NAPI_FUNCTION("writeShort", NAPI_MessageParcel::JS_writeShort),
2291 DECLARE_NAPI_FUNCTION("writeInt", NAPI_MessageParcel::JS_writeInt),
2292 DECLARE_NAPI_FUNCTION("writeLong", NAPI_MessageParcel::JS_writeLong),
2293 DECLARE_NAPI_FUNCTION("writeFloat", NAPI_MessageParcel::JS_writeFloat),
2294 DECLARE_NAPI_FUNCTION("writeDouble", NAPI_MessageParcel::JS_writeDouble),
2295 DECLARE_NAPI_FUNCTION("writeBoolean", NAPI_MessageParcel::JS_writeBoolean),
2296 DECLARE_NAPI_FUNCTION("writeChar", NAPI_MessageParcel::JS_writeChar),
2297 DECLARE_NAPI_FUNCTION("writeString", NAPI_MessageParcel::JS_writeString),
2298 DECLARE_NAPI_FUNCTION("writeSequenceable", NAPI_MessageParcel::JS_writeSequenceable),
2299 DECLARE_NAPI_FUNCTION("writeByteArray", NAPI_MessageParcel::JS_writeByteArray),
2300 DECLARE_NAPI_FUNCTION("writeShortArray", NAPI_MessageParcel::JS_writeShortArray),
2301 DECLARE_NAPI_FUNCTION("writeIntArray", NAPI_MessageParcel::JS_writeIntArray),
2302 DECLARE_NAPI_FUNCTION("writeLongArray", NAPI_MessageParcel::JS_writeLongArray),
2303 DECLARE_NAPI_FUNCTION("writeFloatArray", NAPI_MessageParcel::JS_writeFloatArray),
2304 DECLARE_NAPI_FUNCTION("writeDoubleArray", NAPI_MessageParcel::JS_writeDoubleArray),
2305 DECLARE_NAPI_FUNCTION("writeBooleanArray", NAPI_MessageParcel::JS_writeBooleanArray),
2306 DECLARE_NAPI_FUNCTION("writeCharArray", NAPI_MessageParcel::JS_writeCharArray),
2307 DECLARE_NAPI_FUNCTION("writeStringArray", NAPI_MessageParcel::JS_writeStringArray),
2308 DECLARE_NAPI_FUNCTION("writeSequenceableArray", NAPI_MessageParcel::JS_writeSequenceableArray),
2309 DECLARE_NAPI_FUNCTION("writeRemoteObjectArray", NAPI_MessageParcel::JS_writeRemoteObjectArray),
2310 DECLARE_NAPI_FUNCTION("readByte", NAPI_MessageParcel::JS_readByte),
2311 DECLARE_NAPI_FUNCTION("readShort", NAPI_MessageParcel::JS_readShort),
2312 DECLARE_NAPI_FUNCTION("readInt", NAPI_MessageParcel::JS_readInt),
2313 DECLARE_NAPI_FUNCTION("readLong", NAPI_MessageParcel::JS_readLong),
2314 DECLARE_NAPI_FUNCTION("readFloat", NAPI_MessageParcel::JS_readFloat),
2315 DECLARE_NAPI_FUNCTION("readDouble", NAPI_MessageParcel::JS_readDouble),
2316 DECLARE_NAPI_FUNCTION("readBoolean", NAPI_MessageParcel::JS_readBoolean),
2317 DECLARE_NAPI_FUNCTION("readChar", NAPI_MessageParcel::JS_readChar),
2318 DECLARE_NAPI_FUNCTION("readString", NAPI_MessageParcel::JS_readString),
2319 DECLARE_NAPI_FUNCTION("readSequenceable", NAPI_MessageParcel::JS_readSequenceable),
2320 DECLARE_NAPI_FUNCTION("readByteArray", NAPI_MessageParcel::JS_readByteArray),
2321 DECLARE_NAPI_FUNCTION("readShortArray", NAPI_MessageParcel::JS_readShortArray),
2322 DECLARE_NAPI_FUNCTION("readIntArray", NAPI_MessageParcel::JS_readIntArray),
2323 DECLARE_NAPI_FUNCTION("readLongArray", NAPI_MessageParcel::JS_readLongArray),
2324 DECLARE_NAPI_FUNCTION("readFloatArray", NAPI_MessageParcel::JS_readFloatArray),
2325 DECLARE_NAPI_FUNCTION("readDoubleArray", NAPI_MessageParcel::JS_readDoubleArray),
2326 DECLARE_NAPI_FUNCTION("readBooleanArray", NAPI_MessageParcel::JS_readBooleanArray),
2327 DECLARE_NAPI_FUNCTION("readCharArray", NAPI_MessageParcel::JS_readCharArray),
2328 DECLARE_NAPI_FUNCTION("readStringArray", NAPI_MessageParcel::JS_readStringArray),
2329 DECLARE_NAPI_FUNCTION("readSequenceableArray", NAPI_MessageParcel::JS_readSequenceableArray),
2330 DECLARE_NAPI_FUNCTION("readRemoteObjectArray", NAPI_MessageParcel::JS_readRemoteObjectArray),
2331 DECLARE_NAPI_STATIC_FUNCTION("closeFileDescriptor", NAPI_MessageParcel::JS_CloseFileDescriptor),
2332 DECLARE_NAPI_STATIC_FUNCTION("dupFileDescriptor", NAPI_MessageParcel::JS_DupFileDescriptor),
2333 DECLARE_NAPI_FUNCTION("writeFileDescriptor", NAPI_MessageParcel::JS_WriteFileDescriptor),
2334 DECLARE_NAPI_FUNCTION("readFileDescriptor", NAPI_MessageParcel::JS_ReadFileDescriptor),
2335 DECLARE_NAPI_FUNCTION("containFileDescriptors", NAPI_MessageParcel::JS_ContainFileDescriptors),
2336 DECLARE_NAPI_FUNCTION("writeAshmem", NAPI_MessageParcel::JS_WriteAshmem),
2337 DECLARE_NAPI_FUNCTION("readAshmem", NAPI_MessageParcel::JS_ReadAshmem),
2338 DECLARE_NAPI_FUNCTION("getRawDataCapacity", NAPI_MessageParcel::JS_GetRawDataCapacity),
2339 DECLARE_NAPI_FUNCTION("writeRawData", NAPI_MessageParcel::JS_WriteRawData),
2340 DECLARE_NAPI_FUNCTION("readRawData", NAPI_MessageParcel::JS_ReadRawData),
2341 };
2342 napi_value constructor = nullptr;
2343 napi_define_class(env, className.c_str(), className.length(), JS_constructor, nullptr,
2344 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
2345 NAPI_ASSERT(env, constructor != nullptr, "define js class MessageParcel failed");
2346 napi_status status = napi_set_named_property(env, exports, "MessageParcel", constructor);
2347 NAPI_ASSERT(env, status == napi_ok, "set property MessageParcel failed");
2348 napi_value global = nullptr;
2349 status = napi_get_global(env, &global);
2350 NAPI_ASSERT(env, status == napi_ok, "get napi global failed");
2351 status = napi_set_named_property(env, global, "IPCParcelConstructor_", constructor);
2352 NAPI_ASSERT(env, status == napi_ok, "set message parcel constructor failed");
2353 return exports;
2354 }
2355
JS_constructor(napi_env env,napi_callback_info info)2356 napi_value NAPI_MessageParcel::JS_constructor(napi_env env, napi_callback_info info)
2357 {
2358 napi_value thisVar = nullptr;
2359 size_t argc = 1;
2360 napi_value argv[1] = { 0 };
2361 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2362 NAPI_ASSERT(env, status == napi_ok, "napi get callback info failed");
2363 MessageParcel *parcel = nullptr;
2364 if (argv[0] != nullptr) {
2365 int64_t tmp = 0;
2366 napi_get_value_int64(env, argv[0], &tmp);
2367 parcel = reinterpret_cast<MessageParcel *>(tmp);
2368 NAPI_ASSERT(env, parcel != nullptr, "parcel is null");
2369 }
2370 // new native parcel object
2371 auto messageParcel = new NAPI_MessageParcel(env, thisVar, parcel);
2372 // connect native object to js thisVar
2373 status = napi_wrap(
2374 env, thisVar, messageParcel,
2375 [](napi_env env, void *data, void *hint) {},
2376 nullptr, nullptr);
2377 NAPI_ASSERT(env, status == napi_ok, "napi wrap message parcel failed");
2378 return thisVar;
2379 }
2380 } // namespace OHOS
2381