1 /*
2 * Copyright (c) 2020 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 #include "jsi.h"
16
17 #include <cstdarg>
18 #include <cstring>
19 #if (OHOS_ACELITE_PRODUCT_WATCH == 1)
20 #include "securec.h"
21 #endif // OHOS_ACELITE_PRODUCT_WATCH
22 #include "ace_log.h"
23 #include "ace_mem_base.h"
24 #include "internal/jsi_internal.h"
25
26 namespace OHOS {
27 namespace ACELite {
GetGlobalObject()28 JSIValue JSI::GetGlobalObject()
29 {
30 #if (ENABLE_JERRY == 1)
31 return AS_JSI_VALUE(jerry_get_global_object());
32 #else
33 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetGlobalObject has not been implemented in this js engine!");
34 return CreateUndefined();
35 #endif
36 }
37
CreateObject()38 JSIValue JSI::CreateObject()
39 {
40 #if (ENABLE_JERRY == 1)
41 return AS_JSI_VALUE(jerry_create_object());
42 #else
43 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateObject has not been implemented in this js engine!");
44 return CreateUndefined();
45 #endif
46 }
47
SetProperty(JSIValue object,JSIValue key,JSIValue value)48 void JSI::SetProperty(JSIValue object, JSIValue key, JSIValue value)
49 {
50 if (!ValueIsObject(object)) {
51 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetProperty failed!");
52 return;
53 }
54 #if (ENABLE_JERRY == 1)
55 jerry_value_t jObject = AS_JERRY_VALUE(object);
56 jerry_value_t jKey = AS_JERRY_VALUE(key);
57 jerry_value_t jVal = AS_JERRY_VALUE(value);
58
59 jerry_release_value(jerry_set_property(jObject, jKey, jVal));
60 #else
61 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetProperty has not been implemented in this js engine!");
62 #endif
63 }
64
SetNamedProperty(JSIValue object,const char * const propName,JSIValue value)65 void JSI::SetNamedProperty(JSIValue object, const char * const propName, JSIValue value)
66 {
67 if (!ValueIsObject(object) || (propName == nullptr)) {
68 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetNamedProperty failed!");
69 return;
70 }
71 #if (ENABLE_JERRY == 1)
72 jerry_value_t jKey = jerry_create_string((const jerry_char_t *)propName);
73 SetProperty(object, AS_JSI_VALUE(jKey), value);
74 jerry_release_value(jKey);
75 #else
76 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetNamedProperty has not been implemented in this js engine!");
77 #endif
78 }
79
SetNumberProperty(JSIValue object,const char * const propName,double value)80 void JSI::SetNumberProperty(JSIValue object, const char * const propName, double value)
81 {
82 if (!ValueIsObject(object)) {
83 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetNumberProperty failed!");
84 return;
85 }
86 JSIValue numValue = CreateNumber(value);
87 SetNamedProperty(object, propName, numValue);
88 ReleaseValue(numValue);
89 }
90
SetBooleanProperty(JSIValue object,const char * const propName,bool value)91 void JSI::SetBooleanProperty(JSIValue object, const char * const propName, bool value)
92 {
93 if (!ValueIsObject(object)) {
94 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetBooleanProperty failed!");
95 return;
96 }
97 JSIValue boolValue = CreateBoolean(value);
98 SetNamedProperty(object, propName, boolValue);
99 ReleaseValue(boolValue);
100 }
101
SetStringProperty(JSIValue object,const char * const propName,const char * value)102 void JSI::SetStringProperty(JSIValue object, const char * const propName, const char *value)
103 {
104 if (!ValueIsObject(object)) {
105 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetStringProperty failed!");
106 return;
107 }
108 JSIValue strValue = CreateString(value);
109 SetNamedProperty(object, propName, strValue);
110 ReleaseValue(strValue);
111 }
112
SetStringPropertyWithBufferSize(JSIValue object,const char * const propName,const char * value,size_t size)113 void JSI::SetStringPropertyWithBufferSize(JSIValue object, const char * const propName, const char *value, size_t size)
114 {
115 if (!ValueIsObject(object)) {
116 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetStringPropertyWithBufferSize failed!");
117 return;
118 }
119 JSIValue strValue = CreateStringWithBufferSize(value, size);
120 SetNamedProperty(object, propName, strValue);
121 ReleaseValue(strValue);
122 }
123
124 #if (ENABLE_JERRY == 1)
125 /**
126 * @brief: CreateJerryFuncHelper is used to create jerry function
127 * along with JSI::CreateFunction, for internal use only.
128 */
CreateJerryFuncHelper(const jerry_value_t func,const jerry_value_t thisVal,const jerry_value_t * args,const jerry_length_t argsNum)129 static jerry_value_t CreateJerryFuncHelper(const jerry_value_t func,
130 const jerry_value_t thisVal,
131 const jerry_value_t *args,
132 const jerry_length_t argsNum)
133 {
134 void *nativePointer = nullptr;
135 bool exist = jerry_get_object_native_pointer(func, &nativePointer, nullptr);
136 if (!exist || (nativePointer == nullptr) || (argsNum >= UINT8_MAX)) {
137 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateJerryFuncHelper get function pointer failed!");
138 return jerry_create_undefined();
139 }
140
141 JSIFunctionHandler handler = reinterpret_cast<JSIFunctionHandler>(nativePointer);
142 JSIValue jsiThis = AS_JSI_VALUE(thisVal);
143 uint8_t jsiArgsNum = (uint8_t)argsNum;
144 if ((args == nullptr) || (jsiArgsNum == 0)) {
145 return AS_JERRY_VALUE(handler(jsiThis, nullptr, 0));
146 }
147 JSIValue *jsiArgs = static_cast<JSIValue *>(ace_malloc(sizeof(JSIValue) * jsiArgsNum));
148 if (jsiArgs == nullptr) {
149 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateJerryFuncHelper allocate memory failed!");
150 return jerry_create_undefined();
151 }
152
153 for (uint8_t index = 0; index < jsiArgsNum; index++) {
154 jsiArgs[index] = AS_JSI_VALUE(args[index]);
155 }
156 JSIValue res = handler(jsiThis, jsiArgs, jsiArgsNum);
157 ace_free(jsiArgs);
158 jsiArgs = nullptr;
159 return AS_JERRY_VALUE(res);
160 }
161 #endif
162
CreateFunction(JSIFunctionHandler handler)163 JSIValue JSI::CreateFunction(JSIFunctionHandler handler)
164 {
165 if (handler == nullptr) {
166 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateFunction failed!");
167 return CreateUndefined();
168 }
169 #if (ENABLE_JERRY == 1)
170 jerry_value_t jFunc = jerry_create_external_function(CreateJerryFuncHelper);
171 void *nativePointer = reinterpret_cast<void *>(handler);
172 jerry_set_object_native_pointer(jFunc, nativePointer, nullptr);
173 return AS_JSI_VALUE(jFunc);
174 #else
175 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateFunction has not been implemented in this js engine!");
176 return CreateUndefined();
177 #endif
178 }
179
CreateString(const char * const str)180 JSIValue JSI::CreateString(const char * const str)
181 {
182 if (str == nullptr) {
183 return CreateUndefined();
184 }
185 #if (ENABLE_JERRY == 1)
186 return AS_JSI_VALUE(jerry_create_string(reinterpret_cast<const jerry_char_t *>(str)));
187 #else
188 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateString has not been implemented in this js engine!");
189 return CreateUndefined();
190 #endif
191 }
192
CreateStringWithBufferSize(const char * const str,size_t size)193 JSIValue JSI::CreateStringWithBufferSize(const char * const str, size_t size)
194 {
195 if (str == nullptr) {
196 return CreateUndefined();
197 }
198 #if (ENABLE_JERRY == 1)
199 return AS_JSI_VALUE(jerry_create_string_sz(reinterpret_cast<const jerry_char_t *>(str), size));
200 #else
201 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateStringWithBufferSize has not been implemented in this js engine!");
202 return CreateUndefined();
203 #endif
204 }
205
CreateUndefined()206 JSIValue JSI::CreateUndefined()
207 {
208 #if (ENABLE_JERRY == 1)
209 return AS_JSI_VALUE(jerry_create_undefined());
210 #else
211 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateUndefined has not been implemented in this js engine!");
212 return nullptr;
213 #endif
214 }
215
ValueIsFunction(JSIValue value)216 bool JSI::ValueIsFunction(JSIValue value)
217 {
218 #if (ENABLE_JERRY == 1)
219 return jerry_value_is_function(AS_JERRY_VALUE(value));
220 #else
221 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsFunction has not been implemented in this js engine!");
222 return false;
223 #endif
224 }
225
ValueIsUndefined(JSIValue value)226 bool JSI::ValueIsUndefined(JSIValue value)
227 {
228 #if (ENABLE_JERRY == 1)
229 return jerry_value_is_undefined(AS_JERRY_VALUE(value));
230 #else
231 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsUndefined has not been implemented in this js engine!");
232 return false;
233 #endif
234 }
235
ValueIsNumber(JSIValue value)236 bool JSI::ValueIsNumber(JSIValue value)
237 {
238 #if (ENABLE_JERRY == 1)
239 return jerry_value_is_number(AS_JERRY_VALUE(value));
240 #else
241 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsNumber has not been implemented in this js engine!");
242 return false;
243 #endif
244 }
245
ValueIsString(JSIValue value)246 bool JSI::ValueIsString(JSIValue value)
247 {
248 #if (ENABLE_JERRY == 1)
249 return jerry_value_is_string(AS_JERRY_VALUE(value));
250 #else
251 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsString has not been implemented in this js engine!");
252 return false;
253 #endif
254 }
255
ValueIsBoolean(JSIValue value)256 bool JSI::ValueIsBoolean(JSIValue value)
257 {
258 #if (ENABLE_JERRY == 1)
259 return jerry_value_is_boolean(AS_JERRY_VALUE(value));
260 #else
261 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsBoolean has not been implemented in this js engine!");
262 return false;
263 #endif
264 }
265
ValueIsNull(JSIValue value)266 bool JSI::ValueIsNull(JSIValue value)
267 {
268 #if (ENABLE_JERRY == 1)
269 return jerry_value_is_null(AS_JERRY_VALUE(value));
270 #else
271 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsNull has not been implemented in this js engine!");
272 return false;
273 #endif
274 }
275
ValueIsObject(JSIValue value)276 bool JSI::ValueIsObject(JSIValue value)
277 {
278 #if (ENABLE_JERRY == 1)
279 return jerry_value_is_object(AS_JERRY_VALUE(value));
280 #else
281 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsObject has not been implemented in this js engine!");
282 return false;
283 #endif
284 }
285
ValueIsError(JSIValue value)286 bool JSI::ValueIsError(JSIValue value)
287 {
288 #if (ENABLE_JERRY == 1)
289 return jerry_value_is_error(AS_JERRY_VALUE(value));
290 #else
291 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsError has not been implemented in this js engine!");
292 return false;
293 #endif
294 }
295
JsonStringify(JSIValue value)296 char *JSI::JsonStringify(JSIValue value)
297 {
298 if (!ValueIsObject(value)) {
299 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:JsonStringify failed!");
300 return nullptr;
301 }
302 #if (ENABLE_JERRY == 1)
303 jerry_value_t stringified = jerry_json_stringify(AS_JERRY_VALUE(value));
304 char *res = ValueToString(AS_JSI_VALUE(stringified));
305 jerry_release_value(stringified);
306 return res;
307 #else
308 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:JsonStringify has not been implemented in this js engine!");
309 return nullptr;
310 #endif
311 }
312
JsonParse(const char * const str)313 JSIValue JSI::JsonParse(const char * const str)
314 {
315 if (str == nullptr) {
316 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:JsonParse failed!");
317 return CreateUndefined();
318 }
319 #if (ENABLE_JERRY == 1)
320 jerry_value_t jVal = jerry_json_parse(reinterpret_cast<const jerry_char_t *>(str), strlen(str));
321 return AS_JSI_VALUE(jVal);
322 #else
323 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:JsonParse has not been implemented in this js engine!");
324 return CreateUndefined();
325 #endif
326 }
327
GetProperty(JSIValue object,JSIValue key)328 JSIValue JSI::GetProperty(JSIValue object, JSIValue key)
329 {
330 if (!ValueIsObject(object)) {
331 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetProperty failed!");
332 return CreateUndefined();
333 }
334 #if (ENABLE_JERRY == 1)
335 jerry_value_t jObject = AS_JERRY_VALUE(object);
336 jerry_value_t jKey = AS_JERRY_VALUE(key);
337 return AS_JSI_VALUE(jerry_get_property(jObject, jKey));
338 #else
339 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetProperty has not been implemented in this js engine!");
340 return CreateUndefined();
341 #endif
342 }
343
GetNamedProperty(JSIValue object,const char * const propName)344 JSIValue JSI::GetNamedProperty(JSIValue object, const char * const propName)
345 {
346 if (!ValueIsObject(object) || (propName == nullptr)) {
347 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetNamedProperty failed!");
348 return CreateUndefined();
349 }
350 #if (ENABLE_JERRY == 1)
351 jerry_value_t jKey = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propName));
352 JSIValue result = GetProperty(object, AS_JSI_VALUE(jKey));
353 jerry_release_value(jKey);
354 return result;
355 #else
356 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetNamedProperty has not been implemented in this js engine!");
357 return CreateUndefined();
358 #endif
359 }
360
GetNumberProperty(JSIValue object,const char * const propName)361 double JSI::GetNumberProperty(JSIValue object, const char * const propName)
362 {
363 if (!ValueIsObject(object)) {
364 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetNumberProperty failed!");
365 return 0.0;
366 }
367 JSIValue propValue = GetNamedProperty(object, propName);
368 double res = ValueToNumber(propValue);
369 ReleaseValue(propValue);
370 return res;
371 }
372
GetBooleanProperty(JSIValue object,const char * const propName)373 bool JSI::GetBooleanProperty(JSIValue object, const char * const propName)
374 {
375 if (!ValueIsObject(object)) {
376 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetBooleanProperty failed!");
377 return false;
378 }
379 JSIValue propValue = GetNamedProperty(object, propName);
380 bool res = ValueToBoolean(propValue);
381 ReleaseValue(propValue);
382 return res;
383 }
384
GetStringProperty(JSIValue object,const char * const propName)385 char *JSI::GetStringProperty(JSIValue object, const char * const propName)
386 {
387 if (!ValueIsObject(object)) {
388 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetStringProperty failed!");
389 return nullptr;
390 }
391 JSIValue propValue = GetNamedProperty(object, propName);
392 char *res = ValueToString(propValue);
393 ReleaseValue(propValue);
394 return res;
395 }
396
GetStringPropertyWithBufferSize(JSIValue object,const char * const propName,size_t & size)397 char *JSI::GetStringPropertyWithBufferSize(JSIValue object, const char * const propName, size_t &size)
398 {
399 if (!ValueIsObject(object)) {
400 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetStringPropertyWithBufferSize failed!");
401 return nullptr;
402 }
403 JSIValue propValue = GetNamedProperty(object, propName);
404 char *res = ValueToStringWithBufferSize(propValue, size);
405 ReleaseValue(propValue);
406 return res;
407 }
408
ReleaseValue(JSIValue value)409 void JSI::ReleaseValue(JSIValue value)
410 {
411 #if (ENABLE_JERRY == 1)
412 if (value != ARGS_END) {
413 jerry_release_value(AS_JERRY_VALUE(value));
414 }
415 #else
416 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ReleaseValue has not been implemented in this js engine!");
417 #endif
418 }
419
ReleaseString(char * & str)420 void JSI::ReleaseString(char *&str)
421 {
422 if (str != nullptr) {
423 ace_free(str);
424 str = nullptr;
425 }
426 }
427
CallFunction(JSIValue funcObj,JSIValue thisVal,const JSIValue * argv,uint8_t argc)428 void JSI::CallFunction(JSIValue funcObj, JSIValue thisVal, const JSIValue *argv, uint8_t argc)
429 {
430 if (!ValueIsFunction(funcObj)) {
431 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CallFunction failed!");
432 return;
433 }
434 #if (ENABLE_JERRY == 1)
435 jerry_value_t jFunc = AS_JERRY_VALUE(funcObj);
436 jerry_value_t jThis = AS_JERRY_VALUE(thisVal);
437 if ((argv == nullptr) || argc == 0) {
438 jerry_value_t ret = jerry_call_function(jFunc, jThis, nullptr, 0);
439 if (jerry_value_is_error(ret)) {
440 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CallFunction error returned!");
441 }
442 jerry_release_value(ret);
443 return;
444 }
445 jerry_value_t *jArgv = new jerry_value_t[argc];
446 if (jArgv == nullptr) {
447 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CallFunction allocate memory failed!");
448 return;
449 }
450
451 for (uint8_t index = 0; index < argc; index++) {
452 jArgv[index] = AS_JERRY_VALUE(argv[index]);
453 }
454 jerry_value_t ret = jerry_call_function(jFunc, jThis, jArgv, argc);
455 if (jerry_value_is_error(ret)) {
456 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CallFunction error returned!");
457 }
458 jerry_release_value(ret);
459 delete[] jArgv;
460 jArgv = nullptr;
461 #else
462 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CallFunction has not been implemented in this js engine!");
463 #endif
464 }
465
CreateNumber(double value)466 JSIValue JSI::CreateNumber(double value)
467 {
468 #if (ENABLE_JERRY == 1)
469 return AS_JSI_VALUE(jerry_create_number(value));
470 #else
471 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateNumber has not been implemented in this js engine!");
472 return CreateUndefined();
473 #endif
474 }
475
CreateNumberNaN()476 JSIValue JSI::CreateNumberNaN()
477 {
478 #if (ENABLE_JERRY == 1)
479 return AS_JSI_VALUE(jerry_create_number_nan());
480 #else
481 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateNumberNan has not been implemented in this js engine!");
482 return CreateUndefined();
483 #endif
484 }
485
486 #if (ENABLE_JERRY == 1)
GetJerryErrorType(JsiErrorType jsiType)487 static jerry_error_t GetJerryErrorType(JsiErrorType jsiType)
488 {
489 jerry_error_t jerryType;
490 switch (jsiType) {
491 case JsiErrorType::JSI_ERROR_COMMON:
492 jerryType = JERRY_ERROR_COMMON;
493 break;
494 case JsiErrorType::JSI_ERROR_EVAL:
495 jerryType = JERRY_ERROR_EVAL;
496 break;
497 case JsiErrorType::JSI_ERROR_RANGE:
498 jerryType = JERRY_ERROR_RANGE;
499 break;
500 case JsiErrorType::JSI_ERROR_REFERENCE:
501 jerryType = JERRY_ERROR_REFERENCE;
502 break;
503 case JsiErrorType::JSI_ERROR_SYNTAX:
504 jerryType = JERRY_ERROR_SYNTAX;
505 break;
506 case JsiErrorType::JSI_ERROR_TYPE:
507 jerryType = JERRY_ERROR_TYPE;
508 break;
509 case JsiErrorType::JSI_ERROR_URI:
510 jerryType = JERRY_ERROR_URI;
511 break;
512 default:
513 jerryType = JERRY_ERROR_COMMON;
514 break;
515 }
516 return jerryType;
517 }
518
GetJsiErrorType(jerry_error_t jerryType)519 static JsiErrorType GetJsiErrorType(jerry_error_t jerryType)
520 {
521 JsiErrorType jsiType;
522 switch (jerryType) {
523 case JERRY_ERROR_COMMON:
524 jsiType = JsiErrorType::JSI_ERROR_COMMON;
525 break;
526 case JERRY_ERROR_EVAL:
527 jsiType = JsiErrorType::JSI_ERROR_EVAL;
528 break;
529 case JERRY_ERROR_RANGE:
530 jsiType = JsiErrorType::JSI_ERROR_RANGE;
531 break;
532 case JERRY_ERROR_REFERENCE:
533 jsiType = JsiErrorType::JSI_ERROR_REFERENCE;
534 break;
535 case JERRY_ERROR_SYNTAX:
536 jsiType = JsiErrorType::JSI_ERROR_SYNTAX;
537 break;
538 case JERRY_ERROR_TYPE:
539 jsiType = JsiErrorType::JSI_ERROR_TYPE;
540 break;
541 case JERRY_ERROR_URI:
542 jsiType = JsiErrorType::JSI_ERROR_URI;
543 break;
544 default:
545 jsiType = JsiErrorType::JSI_ERROR_INVALID;
546 break;
547 }
548 return jsiType;
549 }
550 #endif
551
CreateError(JsiErrorType type,const char * const errorMsg)552 JSIValue JSI::CreateError(JsiErrorType type, const char * const errorMsg)
553 {
554 if ((type <= JsiErrorType::JSI_ERROR_INVALID) ||
555 (type >= JsiErrorType::JSI_ERROR_MAX) || (errorMsg == nullptr)) {
556 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateError parameters invalid!");
557 return CreateUndefined();
558 }
559 #if (ENABLE_JERRY == 1)
560 jerry_error_t jerryType = GetJerryErrorType(type);
561 jerry_value_t jError = jerry_create_error(jerryType, reinterpret_cast<const jerry_char_t *>(errorMsg));
562 return AS_JSI_VALUE(jError);
563 #else
564 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateError has not been implemented in this js engine!");
565 return CreateUndefined();
566 #endif
567 }
568
GetErrorType(JSIValue errorValue)569 JsiErrorType JSI::GetErrorType(JSIValue errorValue)
570 {
571 if (!ValueIsError(errorValue)) {
572 return JsiErrorType::JSI_ERROR_INVALID;
573 }
574 #if (ENABLE_JERRY == 1)
575 jerry_value_t jError = AS_JERRY_VALUE(errorValue);
576 jerry_error_t jerryType = jerry_get_error_type(jError);
577 return GetJsiErrorType(jerryType);
578 #else
579 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetErrorType has not been implemented in this js engine!");
580 return JsiErrorType::JSI_ERROR_INVALID;
581 #endif
582 }
583
CreateBoolean(bool value)584 JSIValue JSI::CreateBoolean(bool value)
585 {
586 #if (ENABLE_JERRY == 1)
587 return AS_JSI_VALUE(jerry_create_boolean(value));
588 #else
589 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateBoolean has not been implemented in this js engine!");
590 return CreateUndefined();
591 #endif
592 }
593
CreateNull()594 JSIValue JSI::CreateNull()
595 {
596 #if (ENABLE_JERRY == 1)
597 return AS_JSI_VALUE(jerry_create_null());
598 #else
599 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateNull has not been implemented in this js engine!");
600 return CreateUndefined();
601 #endif
602 }
603
604 #if (JS_FWK_SYMBOL == 1)
CreateSymbol(JSIValue description)605 JSIValue JSI::CreateSymbol(JSIValue description)
606 {
607 #if (ENABLE_JERRY == 1)
608 jerry_value_t jDsc = AS_JERRY_VALUE(description);
609 return AS_JSI_VALUE(jerry_create_symbol(jDsc));
610 #else
611 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateSymbol has not been implemented in this js engine!");
612 return CreateUndefined();
613 #endif
614 }
615
ValueIsSymbol(JSIValue value)616 bool JSI::ValueIsSymbol(JSIValue value)
617 {
618 #if (ENABLE_JERRY == 1)
619 return jerry_value_is_symbol(AS_JERRY_VALUE(value));
620 #else
621 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsSymbol has not been implemented in this js engine!");
622 return false;
623 #endif
624 }
625 #endif // JS_FWK_SYMBOL
626
CreateArray(uint32_t length)627 JSIValue JSI::CreateArray(uint32_t length)
628 {
629 #if (ENABLE_JERRY == 1)
630 return AS_JSI_VALUE(jerry_create_array(length));
631 #else
632 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateArray has not been implemented in this js engine!");
633 return CreateUndefined();
634 #endif
635 }
636
SetPropertyByIndex(JSIValue object,uint32_t index,JSIValue value)637 bool JSI::SetPropertyByIndex(JSIValue object, uint32_t index, JSIValue value)
638 {
639 #if (ENABLE_JERRY == 1)
640 jerry_value_t jObject = AS_JERRY_VALUE(object);
641 jerry_value_t jVal = AS_JERRY_VALUE(value);
642
643 jerry_value_t res = jerry_set_property_by_index(jObject, index, jVal);
644 if (jerry_value_is_error(res)) {
645 jerry_release_value(res);
646 return false;
647 }
648 jerry_release_value(res);
649 return true;
650 #else
651 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetPropertyByIndex has not been implemented in this js engine!");
652 return false;
653 #endif
654 }
655
ValueToString(JSIValue value)656 char *JSI::ValueToString(JSIValue value)
657 {
658 #if (ENABLE_JERRY == 1)
659 char *result = nullptr;
660 jerry_value_t jVal = AS_JERRY_VALUE(value);
661 if (!jerry_value_is_string(jVal)) {
662 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToString params invalid!");
663 return nullptr;
664 }
665
666 jerry_size_t size = jerry_get_string_size(jVal);
667 if ((size == 0) || (size == UINT32_MAX)) {
668 // Output empty char instead of nullptr, thus caller can free safely
669 result = static_cast<char *>(ace_malloc(sizeof(char)));
670 if (result == nullptr) {
671 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToString malloc memory for empty char failed!");
672 return nullptr;
673 }
674 result[0] = '\0';
675 return result;
676 } else {
677 jerry_char_t *buffer = static_cast<jerry_char_t *>(ace_malloc(sizeof(jerry_char_t) * (size + 1)));
678 if (buffer == nullptr) {
679 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToString malloc memory failed!");
680 return nullptr;
681 }
682 jerry_size_t length = jerry_string_to_char_buffer(jVal, buffer, size);
683 if ((length == 0) || (length > size)) {
684 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToString jerry string to char buffer failed");
685 ace_free(buffer);
686 buffer = nullptr;
687 return nullptr;
688 }
689 buffer[length] = '\0';
690 result = reinterpret_cast<char *>(buffer);
691 return result;
692 }
693 #else
694 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToString has not been implemented in this js engine!");
695 return nullptr;
696 #endif
697 }
698
ValueToStringWithBufferSize(JSIValue value,size_t & size)699 char *JSI::ValueToStringWithBufferSize(JSIValue value, size_t &size)
700 {
701 #if (ENABLE_JERRY == 1)
702 auto jVal = AS_JERRY_VALUE(value);
703 if (!jerry_value_is_string(jVal)) {
704 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToStringWithBufferSize params invalid!");
705 size = 0;
706 return nullptr;
707 }
708
709 size = jerry_get_string_size(jVal);
710 if ((size == 0) || (size == UINT32_MAX)) {
711 // Output nullptr
712 size = 0;
713 return nullptr;
714 } else {
715 auto *buffer = static_cast<jerry_char_t *>(ace_malloc(sizeof(jerry_char_t) * size));
716 if (buffer == nullptr) {
717 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToStringWithBufferSize malloc memory failed!");
718 size = 0;
719 return nullptr;
720 }
721 jerry_size_t length = jerry_string_to_char_buffer(jVal, buffer, size);
722 if ((length == 0) || (length > size)) {
723 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToStringWithBufferSize jerry string to char buffer failed");
724 ace_free(buffer);
725 size = 0;
726 return nullptr;
727 }
728 char *result = reinterpret_cast<char *>(buffer);
729 return result;
730 }
731 #else
732 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToStringWithBufferSize has not been implemented in this js engine!");
733 size = 0;
734 return nullptr;
735 #endif
736 }
737
ValueToNumber(JSIValue value)738 double JSI::ValueToNumber(JSIValue value)
739 {
740 #if (ENABLE_JERRY == 1)
741 jerry_value_t jVal = AS_JERRY_VALUE(value);
742 if (!jerry_value_is_number(jVal)) {
743 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToNumber params invalid!");
744 return 0.0;
745 }
746 return jerry_get_number_value(jVal);
747 #else
748 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToNumber has not been implemented in this js engine!");
749 return 0.0;
750 #endif
751 }
752
ValueToBoolean(JSIValue value)753 bool JSI::ValueToBoolean(JSIValue value)
754 {
755 #if (ENABLE_JERRY == 1)
756 jerry_value_t jVal = AS_JERRY_VALUE(value);
757 if (!jerry_value_is_boolean(jVal)) {
758 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToBoolean params invalid!");
759 return false;
760 }
761 return jerry_value_to_boolean(jVal);
762 #else
763 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueToBoolean has not been implemented in this js engine!");
764 return false;
765 #endif
766 }
767
GetArrayLength(JSIValue value)768 uint32_t JSI::GetArrayLength(JSIValue value)
769 {
770 #if (ENABLE_JERRY == 1)
771 jerry_value_t jVal = AS_JERRY_VALUE(value);
772 if (!jerry_value_is_array(jVal)) {
773 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetArrayLength params invalid!");
774 return 0;
775 }
776 return jerry_get_array_length(jVal);
777 #else
778 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetArrayLength has not been implemented in this js engine!");
779 return 0;
780 #endif
781 }
782
GetPropertyByIndex(JSIValue object,uint32_t index)783 JSIValue JSI::GetPropertyByIndex(JSIValue object, uint32_t index)
784 {
785 #if (ENABLE_JERRY == 1)
786 jerry_value_t jVal = AS_JERRY_VALUE(object);
787 return AS_JSI_VALUE(jerry_get_property_by_index(jVal, index));
788 #else
789 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetPropertyByIndex has not been implemented in this js engine!");
790 return CreateUndefined();
791 #endif
792 }
793
GetObjectKeys(JSIValue object)794 JSIValue JSI::GetObjectKeys(JSIValue object)
795 {
796 if (!ValueIsObject(object)) {
797 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetObjectKeys failed!");
798 return CreateUndefined();
799 }
800 #if (ENABLE_JERRY == 1)
801 jerry_value_t jVal = AS_JERRY_VALUE(object);
802 return AS_JSI_VALUE(jerry_get_object_keys(jVal));
803 #else
804 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetObjectKeys has not been implemented in this js engine!");
805 return CreateUndefined();
806 #endif
807 }
808
GetJSHeapStatus(JSHeapStatus & heapStatus)809 bool JSI::GetJSHeapStatus(JSHeapStatus &heapStatus)
810 {
811 #if (ENABLE_JERRY == 1)
812 jerry_heap_stats_t stats = {0};
813 if (!jerry_get_memory_stats(&stats)) {
814 return false;
815 }
816 heapStatus.totalBytes = stats.size;
817 heapStatus.allocBytes = stats.allocated_bytes;
818 heapStatus.peakAllocBytes = stats.peak_allocated_bytes;
819 return true;
820 #else
821 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetJSHeapStatus has not been implemented in this js engine!");
822 return false;
823 #endif
824 }
825
ValueIsArray(JSIValue value)826 bool JSI::ValueIsArray(JSIValue value)
827 {
828 #if (ENABLE_JERRY == 1)
829 return jerry_value_is_array(AS_JERRY_VALUE(value));
830 #else
831 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsArray has not been implemented in this js engine!");
832 return false;
833 #endif
834 }
835
AcquireValue(JSIValue value)836 JSIValue JSI::AcquireValue(JSIValue value)
837 {
838 #if (ENABLE_JERRY == 1)
839 jerry_value_t jVal = AS_JERRY_VALUE(value);
840 return AS_JSI_VALUE(jerry_acquire_value(jVal));
841 #else
842 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:AcquireValue has not been implemented in this js engine!");
843 return CreateUndefined();
844 #endif
845 }
846
SetModuleAPI(JSIValue exports,const char * const name,JSIFunctionHandler handler)847 void JSI::SetModuleAPI(JSIValue exports, const char * const name, JSIFunctionHandler handler)
848 {
849 if (!ValueIsObject(exports) || (handler == nullptr)) {
850 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetModuleAPI failed!");
851 return;
852 }
853 JSIValue jsFunc = CreateFunction(handler);
854 SetNamedProperty(exports, name, jsFunc);
855 ReleaseValue(jsFunc);
856 }
857
SetOnDestroy(JSIValue object,NativeCallback callback)858 void JSI::SetOnDestroy(JSIValue object, NativeCallback callback)
859 {
860 if (!ValueIsObject(object) || (callback == nullptr)) {
861 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetOnDestroy failed!");
862 return;
863 }
864 #if (ENABLE_JERRY == 1)
865 void *nativePtr = reinterpret_cast<void *>(callback);
866 jerry_set_object_native_pointer(AS_JERRY_VALUE(object), nativePtr, nullptr);
867 #else
868 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetOnDestroy has not been implemented in this js engine!");
869 #endif
870 }
871
SetOnDestroy(JSIValue object,JsiCallback callback)872 void JSI::SetOnDestroy(JSIValue object, JsiCallback callback)
873 {
874 SetNamedPointer(object, ON_DESTROY_HANDLER, callback);
875 }
876
SetOnTerminate(JSIValue object,NativeCallback callback)877 void JSI::SetOnTerminate(JSIValue object, NativeCallback callback)
878 {
879 if (!ValueIsObject(object) || (callback == nullptr)) {
880 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetOnTerminate failed!");
881 return;
882 }
883 JSIValue funObj = JSI::CreateObject();
884 JSI::SetOnDestroy(funObj, callback);
885 JSI::SetNamedProperty(object, ON_TERMINATE, funObj);
886 JSI::ReleaseValue(funObj);
887 }
888
SetOnTerminate(JSIValue object,JsiCallback callback)889 void JSI::SetOnTerminate(JSIValue object, JsiCallback callback)
890 {
891 SetNamedPointer(object, ON_TERMINATE_HANDLER, callback);
892 }
893
SetNamedPointer(JSIValue object,const char * const name,JsiCallback callback)894 void JSI::SetNamedPointer(JSIValue object, const char * const name, JsiCallback callback)
895 {
896 if (!ValueIsObject(object) || (callback == nullptr)) {
897 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetNamedPointer failed!");
898 return;
899 }
900 #if (ENABLE_JERRY == 1)
901 JSIValue funObj = JSI::CreateObject();
902 void *nativePtr = reinterpret_cast<void *>(callback);
903 jerry_set_object_native_pointer(AS_JERRY_VALUE(funObj), nativePtr, nullptr);
904 SetNamedProperty(object, name, funObj);
905 ReleaseValue(funObj);
906 #else
907 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:SetNamedPointer has not been implemented in this js engine!");
908 #endif
909 }
910
911 #if (JS_FWK_TYPEDARRAY == 1)
912 #if (ENABLE_JERRY == 1)
913 // Helper function for JSI TypedArray type convert
GetJsiArrayType(jerry_typedarray_type_t jerryType)914 TypedArrayType GetJsiArrayType(jerry_typedarray_type_t jerryType)
915 {
916 TypedArrayType jsiType;
917 switch (jerryType) {
918 case JERRY_TYPEDARRAY_INT8:
919 jsiType = TypedArrayType::JSI_INT8_ARRAY;
920 break;
921 case JERRY_TYPEDARRAY_UINT8:
922 jsiType = TypedArrayType::JSI_UINT8_ARRAY;
923 break;
924 case JERRY_TYPEDARRAY_UINT8CLAMPED:
925 jsiType = TypedArrayType::JSI_UINT8CLAMPED_ARRAY;
926 break;
927 case JERRY_TYPEDARRAY_INT16:
928 jsiType = TypedArrayType::JSI_INT16_ARRAY;
929 break;
930 case JERRY_TYPEDARRAY_UINT16:
931 jsiType = TypedArrayType::JSI_UINT16_ARRAY;
932 break;
933 case JERRY_TYPEDARRAY_INT32:
934 jsiType = TypedArrayType::JSI_INT32_ARRAY;
935 break;
936 case JERRY_TYPEDARRAY_UINT32:
937 jsiType = TypedArrayType::JSI_UINT32_ARRAY;
938 break;
939 case JERRY_TYPEDARRAY_FLOAT32:
940 jsiType = TypedArrayType::JSI_FLOAT32_ARRAY;
941 break;
942 case JERRY_TYPEDARRAY_FLOAT64:
943 jsiType = TypedArrayType::JSI_FLOAT64_ARRAY;
944 break;
945 default:
946 jsiType = TypedArrayType::JSI_INVALID_ARRAY;
947 break;
948 }
949 return jsiType;
950 }
951 #endif
952
GetTypedArrayInfo(JSIValue typedArray,TypedArrayType & type,size_t & length,JSIValue & arrayBuffer,size_t & byteOffset)953 uint8_t *JSI::GetTypedArrayInfo(JSIValue typedArray,
954 TypedArrayType &type,
955 size_t &length,
956 JSIValue &arrayBuffer,
957 size_t &byteOffset)
958 {
959 if (!ValueIsTypedArray(typedArray)) {
960 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetTypedArrayInfo failed!");
961 return nullptr;
962 }
963 #if (ENABLE_JERRY == 1)
964 jerry_value_t jVal = AS_JERRY_VALUE(typedArray);
965 jerry_typedarray_type_t jType = jerry_get_typedarray_type(jVal);
966 type = GetJsiArrayType(jType);
967
968 jerry_length_t jLength = jerry_get_typedarray_length(jVal);
969 jerry_length_t jByteOffset;
970 jerry_length_t jByteLength;
971 jerry_value_t jArrayBuffer = jerry_get_typedarray_buffer(jVal, &jByteOffset, &jByteLength);
972
973 length = (size_t)jLength;
974 arrayBuffer = AS_JSI_VALUE(jArrayBuffer);
975 byteOffset = (size_t)jByteOffset;
976 return jerry_get_arraybuffer_pointer(jArrayBuffer);
977 #else
978 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetTypedArrayInfo has not been implemented in this js engine!");
979 return nullptr;
980 #endif
981 }
982
983 #if (ENABLE_JERRY == 1)
984 // Helper function for jerry TypedArray type convert
GetJerryArrayType(TypedArrayType jsiType)985 jerry_typedarray_type_t GetJerryArrayType(TypedArrayType jsiType)
986 {
987 jerry_typedarray_type_t jerryType;
988 switch (jsiType) {
989 case TypedArrayType::JSI_INT8_ARRAY:
990 jerryType = JERRY_TYPEDARRAY_INT8;
991 break;
992 case TypedArrayType::JSI_UINT8_ARRAY:
993 jerryType = JERRY_TYPEDARRAY_UINT8;
994 break;
995 case TypedArrayType::JSI_UINT8CLAMPED_ARRAY:
996 jerryType = JERRY_TYPEDARRAY_UINT8CLAMPED;
997 break;
998 case TypedArrayType::JSI_INT16_ARRAY:
999 jerryType = JERRY_TYPEDARRAY_INT16;
1000 break;
1001 case TypedArrayType::JSI_UINT16_ARRAY:
1002 jerryType = JERRY_TYPEDARRAY_UINT16;
1003 break;
1004 case TypedArrayType::JSI_INT32_ARRAY:
1005 jerryType = JERRY_TYPEDARRAY_INT32;
1006 break;
1007 case TypedArrayType::JSI_UINT32_ARRAY:
1008 jerryType = JERRY_TYPEDARRAY_UINT32;
1009 break;
1010 case TypedArrayType::JSI_FLOAT32_ARRAY:
1011 jerryType = JERRY_TYPEDARRAY_FLOAT32;
1012 break;
1013 case TypedArrayType::JSI_FLOAT64_ARRAY:
1014 jerryType = JERRY_TYPEDARRAY_FLOAT64;
1015 break;
1016 default:
1017 jerryType = JERRY_TYPEDARRAY_INVALID;
1018 break;
1019 }
1020 return jerryType;
1021 }
1022 #endif
1023
CreateTypedArray(TypedArrayType type,size_t length,JSIValue arrayBuffer,size_t byteOffset)1024 JSIValue JSI::CreateTypedArray(TypedArrayType type, size_t length, JSIValue arrayBuffer, size_t byteOffset)
1025 {
1026 #if (ENABLE_JERRY == 1)
1027 jerry_typedarray_type_t jType = GetJerryArrayType(type);
1028 jerry_value_t jArrayBuffer = AS_JERRY_VALUE(arrayBuffer);
1029 jerry_value_t jVal = jerry_create_typedarray_for_arraybuffer_sz(jType, jArrayBuffer, (jerry_length_t)byteOffset,
1030 (jerry_length_t)length);
1031 return AS_JSI_VALUE(jVal);
1032 #else
1033 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateTypedArray has not been implemented in this js engine!");
1034 return CreateUndefined();
1035 #endif
1036 }
1037
GetArrayBufferInfo(JSIValue arrayBuffer,size_t & byteLength)1038 uint8_t *JSI::GetArrayBufferInfo(JSIValue arrayBuffer, size_t &byteLength)
1039 {
1040 if (!ValueIsArrayBuffer(arrayBuffer)) {
1041 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetArrayBufferInfo failed!");
1042 return nullptr;
1043 }
1044 #if (ENABLE_JERRY == 1)
1045 jerry_value_t jVal = AS_JERRY_VALUE(arrayBuffer);
1046 jerry_length_t len = jerry_get_arraybuffer_byte_length(jVal);
1047
1048 byteLength = (size_t)len;
1049 return jerry_get_arraybuffer_pointer(jVal);
1050 #else
1051 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:GetArrayBufferInfo has not been implemented in this js engine!");
1052 return nullptr;
1053 #endif
1054 }
1055
CreateArrayBuffer(size_t byteLength,uint8_t * & buffPtr)1056 JSIValue JSI::CreateArrayBuffer(size_t byteLength, uint8_t *&buffPtr)
1057 {
1058 if (byteLength == 0) {
1059 return CreateUndefined();
1060 }
1061 #if (ENABLE_JERRY == 1)
1062 jerry_value_t jVal = jerry_create_arraybuffer((jerry_length_t)byteLength);
1063 buffPtr = jerry_get_arraybuffer_pointer(jVal);
1064 return AS_JSI_VALUE(jVal);
1065 #else
1066 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:CreateArrayBuffer has not been implemented in this js engine!");
1067 return CreateUndefined();
1068 #endif
1069 }
1070
ValueIsArrayBuffer(JSIValue value)1071 bool JSI::ValueIsArrayBuffer(JSIValue value)
1072 {
1073 #if (ENABLE_JERRY == 1)
1074 return jerry_value_is_arraybuffer(AS_JERRY_VALUE(value));
1075 #else
1076 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsArrayBuffer has not been implemented in this js engine!");
1077 return false;
1078 #endif
1079 }
1080
ValueIsTypedArray(JSIValue value)1081 bool JSI::ValueIsTypedArray(JSIValue value)
1082 {
1083 #if (ENABLE_JERRY == 1)
1084 return jerry_value_is_typedarray(AS_JERRY_VALUE(value));
1085 #else
1086 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:ValueIsTypedArray has not been implemented in this js engine!");
1087 return false;
1088 #endif
1089 }
1090 #endif // JS_FWK_TYPEDARRAY
1091
DefineProperty(JSIValue object,JSIValue propName,JSPropertyDescriptor descriptor)1092 bool JSI::DefineProperty(JSIValue object, JSIValue propName, JSPropertyDescriptor descriptor)
1093 {
1094 if (!ValueIsObject(object)) {
1095 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:DefineProperty failed!");
1096 return false;
1097 }
1098 #if (ENABLE_JERRY == 1)
1099 jerry_property_descriptor_t jerryDesc;
1100 jerry_init_property_descriptor_fields(&jerryDesc);
1101
1102 jerryDesc.is_set_defined = false;
1103 if (descriptor.setter != nullptr) {
1104 jerryDesc.is_set_defined = true;
1105 jerryDesc.setter = AS_JERRY_VALUE(CreateFunction(descriptor.setter));
1106 }
1107 jerryDesc.is_get_defined = false;
1108 if (descriptor.getter != nullptr) {
1109 jerryDesc.is_get_defined = true;
1110 jerryDesc.getter = AS_JERRY_VALUE(CreateFunction(descriptor.getter));
1111 }
1112 jerry_value_t retValue = jerry_define_own_property(AS_JERRY_VALUE(object), AS_JERRY_VALUE(propName), &jerryDesc);
1113 jerry_free_property_descriptor_fields(&jerryDesc);
1114
1115 bool res = true;
1116 if (jerry_value_is_error(retValue)) {
1117 res = false;
1118 }
1119 jerry_release_value(retValue);
1120 return res;
1121 #else
1122 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:DefineProperty has not been implemented in this js engine!");
1123 return false;
1124 #endif
1125 }
1126
DefineNamedProperty(JSIValue object,const char * const propNameStr,JSPropertyDescriptor descriptor)1127 bool JSI::DefineNamedProperty(JSIValue object, const char * const propNameStr, JSPropertyDescriptor descriptor)
1128 {
1129 if (!ValueIsObject(object) || (propNameStr == nullptr)) {
1130 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:DefineNamedProperty failed!");
1131 return false;
1132 }
1133 #if (ENABLE_JERRY == 1)
1134 jerry_value_t propName = jerry_create_string(reinterpret_cast<const jerry_char_t *>(propNameStr));
1135 bool res = DefineProperty(object, AS_JSI_VALUE(propName), descriptor);
1136 jerry_release_value(propName);
1137 return res;
1138 #else
1139 HILOG_ERROR(HILOG_MODULE_ACE, "JSI:DefineNamedProperty has not been implemented in this js engine!");
1140 return false;
1141 #endif
1142 }
1143
DefineNamedProperty(JSIValue object,const char * const propNameStr,JSIFunctionHandler setter,JSIFunctionHandler getter)1144 bool JSI::DefineNamedProperty(JSIValue object,
1145 const char * const propNameStr,
1146 JSIFunctionHandler setter,
1147 JSIFunctionHandler getter)
1148 {
1149 JSPropertyDescriptor desc;
1150 desc.setter = setter;
1151 desc.getter = getter;
1152 return JSI::DefineNamedProperty(object, propNameStr, desc);
1153 }
1154
FailCallback(const JSIValue thisVal,const JSIValue args,int32_t errCode,const char * const errDesc)1155 void JSI::FailCallback(const JSIValue thisVal, const JSIValue args, int32_t errCode, const char * const errDesc)
1156 {
1157 if (ValueIsUndefined(args)) {
1158 return;
1159 }
1160 JSIValue fail = GetNamedProperty(args, CB_FAIL);
1161 JSIValue complete = GetNamedProperty(args, CB_COMPLETE);
1162 JSIValue errInfo = CreateString(errDesc);
1163 JSIValue retCode = CreateNumber(errCode);
1164 JSIValue argv[ARGC_TWO] = {errInfo, retCode};
1165
1166 CallFunction(fail, thisVal, argv, ARGC_TWO);
1167 CallFunction(complete, thisVal, nullptr, 0);
1168 ReleaseValueList(errInfo, retCode, fail, complete);
1169 }
1170
SuccessCallback(const JSIValue thisVal,const JSIValue args,const JSIValue * argv,uint8_t argc)1171 void JSI::SuccessCallback(const JSIValue thisVal, const JSIValue args, const JSIValue *argv, uint8_t argc)
1172 {
1173 if (ValueIsUndefined(args)) {
1174 return;
1175 }
1176 JSIValue success = GetNamedProperty(args, CB_SUCCESS);
1177 JSIValue complete = GetNamedProperty(args, CB_COMPLETE);
1178
1179 CallFunction(success, thisVal, argv, argc);
1180 CallFunction(complete, thisVal, nullptr, 0);
1181 ReleaseValueList(success, complete);
1182 }
1183 } // namespace ACELite
1184 } // namespace OHOS
1185