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 #ifndef ARK_RUNTIME_JSVM_JSVM_TYPE_H 17 #define ARK_RUNTIME_JSVM_JSVM_TYPE_H 18 19 /** 20 * @addtogroup JSVM 21 * @{ 22 * 23 * @brief Provides the standard JavaScript engine capabilities. 24 * 25 * Provides API to Provide independent, standard, and complete JavaScript engine capabilities for developers, 26 * including managing the engine lifecycle, compiling and running JS code, implementing JS/C++ cross language calls, 27 * and taking snapshots. 28 * 29 * @since 11 30 */ 31 32 /** 33 * @file jsvm_types.h 34 * 35 * @brief Provides the JSVM API type define. 36 * 37 * Provides API to Provide independent, standard, and complete JavaScript engine capabilities for developers, 38 * including managing the engine lifecycle, compiling and running JS code, implementing JS/C++ cross language calls, 39 * and taking snapshots. 40 * @library libjsvm.so 41 * @syscap SystemCapability.ArkCompiler.JSVM 42 * @since 11 43 */ 44 45 #include <stdbool.h> // NOLINT(modernize-deprecated-headers) 46 #include <stddef.h> // NOLINT(modernize-deprecated-headers) 47 #include <stdint.h> // NOLINT(modernize-deprecated-headers) 48 49 #if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900) 50 typedef uint16_t char16_t; 51 #endif 52 53 #ifndef JSVM_CDECL 54 #ifdef _WIN32 55 #define JSVM_CDECL __cdecl 56 #else 57 #define JSVM_CDECL 58 #endif 59 #endif 60 61 /** 62 * @brief To represent a JavaScript VM instance. 63 * 64 * @since 11 65 */ 66 typedef struct JSVM_VM__* JSVM_VM; 67 68 /** 69 * @brief To represent a JavaScript VM scope. 70 * 71 * @since 11 72 */ 73 typedef struct JSVM_VMScope__* JSVM_VMScope; 74 75 /** 76 * @brief To represent a JavaScript VM environment scope. 77 * 78 * @since 11 79 */ 80 typedef struct JSVM_EnvScope__* JSVM_EnvScope; 81 82 /** 83 * @brief To represent a JavaScript code. 84 * 85 * @since 11 86 */ 87 typedef struct JSVM_Script__* JSVM_Script; 88 89 /** 90 * @brief To represent a JavaScript VM instance. 91 * 92 * @since 11 93 */ 94 typedef struct JSVM_Env__* JSVM_Env; 95 96 /** 97 * @brief To represent a JavaScript profiler. 98 * 99 * @since 12 100 */ 101 typedef struct JSVM_CpuProfiler__* JSVM_CpuProfiler; 102 103 /** 104 * @brief To represent a JavaScript VM environment. 105 * 106 * @since 11 107 */ 108 typedef struct JSVM_Value__* JSVM_Value; 109 110 /** 111 * @brief To represent a JavaScript Data type. 112 * 113 * @since 18 114 */ 115 typedef struct JSVM_Data__* JSVM_Data; 116 117 /** 118 * @brief To represent a JavaScript value references. 119 * 120 * @since 11 121 */ 122 typedef struct JSVM_Ref__* JSVM_Ref; 123 124 /** 125 * @brief To represent a JavaScript VM handle scope. 126 * 127 * @since 11 128 */ 129 typedef struct JSVM_HandleScope__* JSVM_HandleScope; 130 131 /** 132 * @brief To represent a JavaScript VM escapable handle scope. 133 * 134 * @since 11 135 */ 136 typedef struct JSVM_EscapableHandleScope__* JSVM_EscapableHandleScope; 137 138 /** 139 * @brief To represent a JavaScript VM callback additional information. 140 * 141 * @since 11 142 */ 143 typedef struct JSVM_CallbackInfo__* JSVM_CallbackInfo; 144 145 /** 146 * @brief To represent a JavaScript VM value deferred. 147 * 148 * @since 11 149 */ 150 typedef struct JSVM_Deferred__* JSVM_Deferred; 151 152 /** 153 * @brief Callback function pointer and data for user-provided native function which are to exposed to js via JSVM-API. 154 * 155 * @since 11 156 */ 157 typedef struct { 158 JSVM_Value(JSVM_CDECL* callback)(JSVM_Env env, JSVM_CallbackInfo info); 159 void* data; 160 } JSVM_CallbackStruct; 161 162 /** 163 * @brief Function pointer type for user-provided native function which are to exposed to js via JSVM-API. 164 * 165 * @since 11 166 */ 167 typedef JSVM_CallbackStruct* JSVM_Callback; 168 169 /** 170 * @brief Function pointer type for add-on provided function that allow the user to be notified. 171 * 172 * @since 11 173 */ 174 typedef void(JSVM_CDECL* JSVM_Finalize)(JSVM_Env env, void* finalizeData, void* finalizeHint); 175 176 /** 177 * @brief Function pointer type for callback of ASCII output stream. 178 * 179 * @since 12 180 */ 181 typedef bool(JSVM_CDECL* JSVM_OutputStream)(const char* data, int size, void* streamData); 182 183 /** 184 * @brief JSVM_PropertyAttributes are flag used to control the behavior of properties set on a js object. 185 * 186 * @since 11 187 */ 188 typedef enum { 189 /** No explicit attributes are set on the property. */ 190 JSVM_DEFAULT = 0, 191 /** The property is writable. */ 192 JSVM_WRITABLE = 1 << 0, 193 /** The property is enumeable. */ 194 JSVM_ENUMERABLE = 1 << 1, 195 /** The property is configurable. */ 196 JSVM_CONFIGURABLE = 1 << 2, 197 /** Used to mark the receiver of a native method need not be checked. 198 * If JSVM_NO_RECEIVER_CHECK is not set, the method only accept instance of the defined class as receiver, 199 * Otherwise Exception "Type Error: Illegal Ivocation" will be throw into JSVM. 200 */ 201 JSVM_NO_RECEIVER_CHECK = 1 << 3, 202 /** Used with OH_JSVM_DefineClass to distinguish static properties from instance properties. */ 203 JSVM_STATIC = 1 << 10, 204 /** Default for class methods. */ 205 JSVM_DEFAULT_METHOD = JSVM_WRITABLE | JSVM_CONFIGURABLE, 206 /** Class method with no receiver check*/ 207 JSVM_METHOD_NO_RECEIVER_CHECK = JSVM_DEFAULT_METHOD | JSVM_NO_RECEIVER_CHECK, 208 /** Default for object properties, like in JS obj[prop]. */ 209 JSVM_DEFAULT_JSPROPERTY = JSVM_WRITABLE | JSVM_ENUMERABLE | JSVM_CONFIGURABLE, 210 /** Object properties with no receiver check*/ 211 JSVM_JSPROPERTY_NO_RECEIVER_CHECK = JSVM_DEFAULT_JSPROPERTY | JSVM_NO_RECEIVER_CHECK, 212 } JSVM_PropertyAttributes; 213 214 /** 215 * @brief Describes the type of a JSVM_Value. 216 * 217 * @since 11 218 */ 219 typedef enum { 220 /** undefined type. */ 221 JSVM_UNDEFINED, 222 /** null type. */ 223 JSVM_NULL, 224 /** boolean type. */ 225 JSVM_BOOLEAN, 226 /** number type. */ 227 JSVM_NUMBER, 228 /** string type. */ 229 JSVM_STRING, 230 /** symbol type. */ 231 JSVM_SYMBOL, 232 /** object type. */ 233 JSVM_OBJECT, 234 /** function type. */ 235 JSVM_FUNCTION, 236 /** external type. */ 237 JSVM_EXTERNAL, 238 /** bigint type. */ 239 JSVM_BIGINT, 240 } JSVM_ValueType; 241 242 /** 243 * @brief Describes the type of a typedarray. 244 * 245 * @since 11 246 */ 247 typedef enum { 248 /** int8 type. */ 249 JSVM_INT8_ARRAY, 250 /** uint8 type. */ 251 JSVM_UINT8_ARRAY, 252 /** uint8 clamped type. */ 253 JSVM_UINT8_CLAMPED_ARRAY, 254 /** int16 type. */ 255 JSVM_INT16_ARRAY, 256 /** uint16 type. */ 257 JSVM_UINT16_ARRAY, 258 /** int32 type. */ 259 JSVM_INT32_ARRAY, 260 /** uint32 type. */ 261 JSVM_UINT32_ARRAY, 262 /** float32 type. */ 263 JSVM_FLOAT32_ARRAY, 264 /** float64 type. */ 265 JSVM_FLOAT64_ARRAY, 266 /** bigint64 type. */ 267 JSVM_BIGINT64_ARRAY, 268 /** biguint64 type. */ 269 JSVM_BIGUINT64_ARRAY, 270 } JSVM_TypedarrayType; 271 272 /** 273 * @brief Integral status code indicating the success or failure of a JSVM-API call. 274 * 275 * @since 11 276 */ 277 typedef enum { 278 /** success status. */ 279 JSVM_OK, 280 /** invalidarg status. */ 281 JSVM_INVALID_ARG, 282 /** object expected status. */ 283 JSVM_OBJECT_EXPECTED, 284 /** string expected status. */ 285 JSVM_STRING_EXPECTED, 286 /** name expected status. */ 287 JSVM_NAME_EXPECTED, 288 /** function expected status. */ 289 JSVM_FUNCTION_EXPECTED, 290 /** number expected status. */ 291 JSVM_NUMBER_EXPECTED, 292 /** boolean expected status. */ 293 JSVM_BOOLEAN_EXPECTED, 294 /** array expected status. */ 295 JSVM_ARRAY_EXPECTED, 296 /** generic failure status. */ 297 JSVM_GENERIC_FAILURE, 298 /** pending exception status. */ 299 JSVM_PENDING_EXCEPTION, 300 /** cancelled status. */ 301 JSVM_CANCELLED, 302 /** escape called twice status. */ 303 JSVM_ESCAPE_CALLED_TWICE, 304 /** handle scope mismatch status. */ 305 JSVM_HANDLE_SCOPE_MISMATCH, 306 /** callback scope mismatch status. */ 307 JSVM_CALLBACK_SCOPE_MISMATCH, 308 /** queue full status. */ 309 JSVM_QUEUE_FULL, 310 /** closing status. */ 311 JSVM_CLOSING, 312 /** bigint expected status. */ 313 JSVM_BIGINT_EXPECTED, 314 /** date expected status. */ 315 JSVM_DATE_EXPECTED, 316 /** arraybuffer expected status. */ 317 JSVM_ARRAYBUFFER_EXPECTED, 318 /** detachable arraybuffer expected status. */ 319 JSVM_DETACHABLE_ARRAYBUFFER_EXPECTED, 320 /** would deadlock status. */ 321 JSVM_WOULD_DEADLOCK, 322 /** no external buffers allowed status. */ 323 JSVM_NO_EXTERNAL_BUFFERS_ALLOWED, 324 /** cannot run +js status. */ 325 JSVM_CANNOT_RUN_JS, 326 /** invalid input type status. 327 * @since 18 328 */ 329 JSVM_INVALID_TYPE, 330 /** jit mode expected status. 331 * @since 18 332 */ 333 JSVM_JIT_MODE_EXPECTED, 334 } JSVM_Status; 335 336 /** 337 * @brief limits the range of collected properties.. 338 * 339 * @since 11 340 */ 341 typedef enum { 342 /** will include all keys of the objects's prototype chain as well. */ 343 JSVM_KEY_INCLUDE_PROTOTYPES, 344 /** limits the collected properties to the given object only. */ 345 JSVM_KEY_OWN_ONLY 346 } JSVM_KeyCollectionMode; 347 348 /** 349 * @brief Property filter bits. They can be or'ed to build a composite filter.. 350 * 351 * @since 11 352 */ 353 typedef enum { 354 /** key all properties. */ 355 JSVM_KEY_ALL_PROPERTIES = 0, 356 /** key writable. */ 357 JSVM_KEY_WRITABLE = 1, 358 /** key enumerable. */ 359 JSVM_KEY_ENUMERABLE = 1 << 1, 360 /** key configurable. */ 361 JSVM_KEY_CONFIGURABLE = 1 << 2, 362 /** key skip strings. */ 363 JSVM_KEY_SKIP_STRINGS = 1 << 3, 364 /** key skip symbols. */ 365 JSVM_KEY_SKIP_SYMBOLS = 1 << 4 366 } JSVM_KeyFilter; 367 368 /** 369 * @brief key conversion select. 370 * 371 * @since 11 372 */ 373 typedef enum { 374 /** will return numbers for integer indices. */ 375 JSVM_KEY_KEEP_NUMBERS, 376 /** will convert integer indices to strings. */ 377 JSVM_KEY_NUMBERS_TO_STRINGS 378 } JSVM_KeyConversion; 379 380 /** 381 * @brief Memory pressure level. 382 * 383 * @since 11 384 */ 385 typedef enum { 386 /** none pressure. */ 387 JSVM_MEMORY_PRESSURE_LEVEL_NONE, 388 /** moderate pressure. */ 389 JSVM_MEMORY_PRESSURE_LEVEL_MODERATE, 390 /** critical pressure. */ 391 JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL, 392 } JSVM_MemoryPressureLevel; 393 394 /** 395 * 396 * @brief Compile mode 397 * 398 * @since 12 399 */ 400 typedef enum { 401 /** default mode. */ 402 JSVM_COMPILE_MODE_DEFAULT, 403 /** consume code cache. */ 404 JSVM_COMPILE_MODE_CONSUME_CODE_CACHE, 405 /** apply eager compile. */ 406 JSVM_COMPILE_MODE_EAGER_COMPILE, 407 /** preset for compile profile. */ 408 JSVM_COMPILE_MODE_PRODUCE_COMPILE_PROFILE, 409 /** consume compile profile. */ 410 JSVM_COMPILE_MODE_CONSUME_COMPILE_PROFILE, 411 } JSVM_CompileMode; 412 413 /** 414 * @brief Compile option id 415 * 416 * @since 12 417 */ 418 typedef enum { 419 /** compile mode. */ 420 JSVM_COMPILE_MODE, 421 /** code cache content. */ 422 JSVM_COMPILE_CODE_CACHE, 423 /** script origin. */ 424 JSVM_COMPILE_SCRIPT_ORIGIN, 425 /** compile profile content. */ 426 JSVM_COMPILE_COMPILE_PROFILE, 427 /** switch for source map support. */ 428 JSVM_COMPILE_ENABLE_SOURCE_MAP, 429 } JSVM_CompileOptionId; 430 431 /** 432 * @brief Heap statistics. 433 * 434 * @since 12 435 */ 436 typedef struct { 437 /** the size of the total heap. */ 438 size_t totalHeapSize; 439 /** the executable size of the total heap. */ 440 size_t totalHeapSizeExecutable; 441 /** the physical size of the total heap. */ 442 size_t totalPhysicalSize; 443 /** the available size of the total heap. */ 444 size_t totalAvailableSize; 445 /** used size of the heap. */ 446 size_t usedHeapSize; 447 /** heap size limit. */ 448 size_t heapSizeLimit; 449 /** memory requested by the heap. */ 450 size_t mallocedMemory; 451 /** heap-requested external memory. */ 452 size_t externalMemory; 453 /** peak memory requested by the heap. */ 454 size_t peakMallocedMemory; 455 /** the number of native contexts. */ 456 size_t numberOfNativeContexts; 457 /** the number of detached contexts. */ 458 size_t numberOfDetachedContexts; 459 /** the size of the total global handles. */ 460 size_t totalGlobalHandlesSize; 461 /** the size of the used global handles. */ 462 size_t usedGlobalHandlesSize; 463 } JSVM_HeapStatistics; 464 465 /** 466 * @brief Init the JavaScript VM with init option. 467 * 468 * @since 11 469 */ 470 typedef struct { 471 /** 472 * Optional nullptr-terminated array of raw adddresses in the embedder that the 473 * VM can match against during serialization and use for deserialization. This 474 * array and its content must stay valid for the entire lifetime of the VM 475 * instance. 476 */ 477 const intptr_t* externalReferences; 478 479 /** 480 * Flags for the VM. IF removeFlags is true, recognized flags will be removed 481 * from (argc, argv). Note that these flags are specific to VM. 482 * They are mainly used for development. Do not include them in production as 483 * they might not take effect if the VM is different from the development 484 * environment. 485 */ 486 int* argc; 487 /** argv . */ 488 char** argv; 489 /** remove flags. */ 490 bool removeFlags; 491 } JSVM_InitOptions; 492 493 /** 494 * @brief Create the JavaScript VM with init option. 495 * 496 * @since 11 497 */ 498 typedef struct { 499 /** optional limits of memory use of the vm. */ 500 size_t maxOldGenerationSize; 501 /** optional limits of memory use of the vm. */ 502 size_t maxYoungGenerationSize; 503 /** optional limits of memory use of the vm. */ 504 size_t initialOldGenerationSize; 505 /** optional limits of memory use of the vm. */ 506 size_t initialYoungGenerationSize; 507 /** Optional startup snapshot data. */ 508 const char* snapshotBlobData; 509 /** Optional size of the startup snapshot data. */ 510 size_t snapshotBlobSize; 511 /** Whether the VM is used for creating snapshot. */ 512 bool isForSnapshotting; 513 } JSVM_CreateVMOptions; 514 515 /** 516 * @brief JavaScript VM info. 517 * 518 * @since 11 519 */ 520 typedef struct { 521 /** The highest API version this VM supports. */ 522 uint32_t apiVersion; 523 /** The engine name implementing the VM. */ 524 const char* engine; 525 /** The version of the VM. */ 526 const char* version; 527 /** The cached data version tag. */ 528 uint32_t cachedDataVersionTag; 529 } JSVM_VMInfo; 530 531 /** 532 * @brief Property descriptor. 533 * 534 * @since 11 535 */ 536 typedef struct { 537 /** Optional string describing the key for the property, encoded as UTF8. 538 * One of utf8name or name must be provided for the property. 539 */ 540 const char* utf8name; 541 /** Optional value that points to a JavaScript string or symbol to be used as the key for the property. */ 542 JSVM_Value name; 543 /** Set this to make the property descriptor object's value property to be 544 * a JavaScript function represented by method. 545 */ 546 JSVM_Callback method; 547 /** A function to call when a get access of the property is performed. */ 548 JSVM_Callback getter; 549 /** A function to call when a set access of the property is performed. */ 550 JSVM_Callback setter; 551 /** The value that's retrieved by a get access of the property if the property is a data property. */ 552 JSVM_Value value; 553 /** The attributes associated with the particular property. */ 554 JSVM_PropertyAttributes attributes; 555 } JSVM_PropertyDescriptor; 556 557 /** 558 * @brief JSVM-API uses both return values and JavaScript exceptions for error handling 559 * @since 11 560 */ 561 typedef struct { 562 /** UTF8-encoded string containing a VM-neutral description of the error. */ 563 const char* errorMessage; 564 /** Reserved for VM-specific error details. This is currently not implemented for any VM. */ 565 void* engineReserved; 566 /** VM-specific error code. This is currently not implemented for any VM. */ 567 uint32_t engineErrorCode; 568 /** The JSVM-API status code that originated with the last error. */ 569 JSVM_Status errorCode; 570 } JSVM_ExtendedErrorInfo; 571 572 /** 573 * @brief A 128-bit value stored as two unsigned 64-bit integers. 574 * It serves as a UUID with which JavaScript objects or externals can be "tagged" 575 * in order to ensure that they are of a certain type. 576 * 577 * @since 11 578 */ 579 typedef struct { 580 /** lower type. */ 581 uint64_t lower; 582 /** upper type. */ 583 uint64_t upper; 584 } JSVM_TypeTag; 585 586 /** 587 * @brief When the getter, setter, call, etc. behavior occurs on the object, the corresponding 588 * the corresponding callback will be triggered. 589 * 590 * @since 12 591 */ 592 typedef struct { 593 /** A callback function triggered by getting a named property of an instance object. */ 594 JSVM_Value(JSVM_CDECL* genericNamedPropertyGetterCallback)(JSVM_Env env, 595 JSVM_Value name, 596 JSVM_Value thisArg, 597 JSVM_Value namedPropertyData); 598 599 /** A callback function triggered by setting a named property of an instance object. */ 600 JSVM_Value(JSVM_CDECL* genericNamedPropertySetterCallback)(JSVM_Env env, 601 JSVM_Value name, 602 JSVM_Value property, 603 JSVM_Value thisArg, 604 JSVM_Value namedPropertyData); 605 606 /** A callback function triggered by deleting a named property of an instance object. */ 607 JSVM_Value(JSVM_CDECL* genericNamedPropertyDeleterCallback)(JSVM_Env env, 608 JSVM_Value name, 609 JSVM_Value thisArg, 610 JSVM_Value namedPropertyData); 611 612 /** A callback function triggered by getting all named properties requests on an object. */ 613 JSVM_Value(JSVM_CDECL* genericNamedPropertyEnumeratorCallback)(JSVM_Env env, 614 JSVM_Value thisArg, 615 JSVM_Value namedPropertyData); 616 617 /** A callback function triggered by getting an indexed property of an instance object. */ 618 JSVM_Value(JSVM_CDECL* genericIndexedPropertyGetterCallback)(JSVM_Env env, 619 JSVM_Value index, 620 JSVM_Value thisArg, 621 JSVM_Value indexedPropertyData); 622 623 /** A callback function triggered by setting an indexed property of an instance object. */ 624 JSVM_Value(JSVM_CDECL* genericIndexedPropertySetterCallback)(JSVM_Env env, 625 JSVM_Value index, 626 JSVM_Value property, 627 JSVM_Value thisArg, 628 JSVM_Value indexedPropertyData); 629 630 /** A callback function triggered by deleting an indexed property of an instance object. */ 631 JSVM_Value(JSVM_CDECL* genericIndexedPropertyDeleterCallback)(JSVM_Env env, 632 JSVM_Value index, 633 JSVM_Value thisArg, 634 JSVM_Value indexedPropertyData); 635 636 /** A callback function triggered by getting all indexed properties requests on an object. */ 637 JSVM_Value(JSVM_CDECL* genericIndexedPropertyEnumeratorCallback)(JSVM_Env env, 638 JSVM_Value thisArg, 639 JSVM_Value indexedPropertyData); 640 641 /** data will be utilized by the named property callbacks in this struct. */ 642 JSVM_Value namedPropertyData; 643 644 /** data will be utilized by the indexed property callbacks in this struct. */ 645 JSVM_Value indexedPropertyData; 646 } JSVM_PropertyHandlerConfigurationStruct; 647 648 /** 649 * @brief The function pointer type of the callback provided by the instance object, which triggers the 650 * corresponding callback when getter, setter, call, and other behaviors occur on the object. 651 * 652 * @since 12 653 */ 654 typedef JSVM_PropertyHandlerConfigurationStruct* JSVM_PropertyHandlerCfg; 655 656 /** 657 * 658 * @brief Source code information. 659 * 660 * @since 12 661 */ 662 typedef struct { 663 /** Sourcemap url. */ 664 const char* sourceMapUrl; 665 /** Resource name. */ 666 const char* resourceName; 667 /** Resource line offset. */ 668 size_t resourceLineOffset; 669 /** Resource column offset. */ 670 size_t resourceColumnOffset; 671 } JSVM_ScriptOrigin; 672 673 /** 674 * @brief Compile Options 675 * 676 * @since 12 677 */ 678 typedef struct { 679 /** compile option id. */ 680 JSVM_CompileOptionId id; 681 /** option content. */ 682 union { 683 /** ptr type. */ 684 void* ptr; 685 /** int type. */ 686 int num; 687 /** bool type. */ 688 bool boolean; 689 } content; 690 } JSVM_CompileOptions; 691 692 /** 693 * @brief code cache passed with JSVM_COMPILE_CODE_CACHE 694 * 695 * @since 12 696 */ 697 typedef struct { 698 /** cache pointer. */ 699 uint8_t* cache; 700 /** length. */ 701 size_t length; 702 } JSVM_CodeCache; 703 704 /** 705 * @brief WebAssembly function optimization level 706 * 707 * @since 12 708 */ 709 typedef enum { 710 /** baseline optimization level. */ 711 JSVM_WASM_OPT_BASELINE = 10, 712 /** high optimization level. */ 713 JSVM_WASM_OPT_HIGH = 20, 714 } JSVM_WasmOptLevel; 715 716 /** 717 * @brief Cache data type 718 * 719 * @since 12 720 */ 721 typedef enum { 722 /** js code cache, generated by OH_JSVM_CreateCodeCache */ 723 JSVM_CACHE_TYPE_JS, 724 /** WebAssembly cache, generated by OH_JSVM_CreateWasmCache */ 725 JSVM_CACHE_TYPE_WASM, 726 } JSVM_CacheType; 727 728 /** 729 * @brief Microtask policies of JSVM. 730 * 731 * @since 18 732 */ 733 typedef enum { 734 /** Microtasks are invoked with the OH_JSVM_PerformMicrotaskCheckpoint() method. */ 735 JSVM_MICROTASK_EXPLICIT = 0, 736 /** Microtasks are invoked when the script call depth decrements to zero. 737 * Default mode. 738 */ 739 JSVM_MICROTASK_AUTO, 740 } JSVM_MicrotaskPolicy; 741 742 /** 743 * @brief compile profile passed with JSVM_COMPILE_COMPILE_PROFILE 744 * 745 * @since 12 746 */ 747 typedef const struct { 748 /** profile pointer. */ 749 int* profile; 750 /** length. */ 751 size_t length; 752 } JSVM_CompileProfile; 753 754 /** 755 * @brief Regular expression flag bits. They can be or'ed to enable a set of flags. 756 * 757 * @since 12 758 */ 759 typedef enum { 760 /** None mode. */ 761 JSVM_REGEXP_NONE = 0, 762 /** Global mode. */ 763 JSVM_REGEXP_GLOBAL = 1 << 0, 764 /** Ignore Case mode. */ 765 JSVM_REGEXP_IGNORE_CASE = 1 << 1, 766 /** Multiline mode. */ 767 JSVM_REGEXP_MULTILINE = 1 << 2, 768 /** Sticky mode. */ 769 JSVM_REGEXP_STICKY = 1 << 3, 770 /** Unicode mode. */ 771 JSVM_REGEXP_UNICODE = 1 << 4, 772 /** dotAll mode. */ 773 JSVM_REGEXP_DOT_ALL = 1 << 5, 774 /** Linear mode. */ 775 JSVM_REGEXP_LINEAR = 1 << 6, 776 /** Has Indices mode. */ 777 JSVM_REGEXP_HAS_INDICES = 1 << 7, 778 /** Unicode Sets mode. */ 779 JSVM_REGEXP_UNICODE_SETS = 1 << 8, 780 } JSVM_RegExpFlags; 781 782 /** 783 * @brief initialization flag 784 * 785 * @since 12 786 */ 787 typedef enum { 788 /** initialize with zero. */ 789 JSVM_ZERO_INITIALIZED, 790 /** leave uninitialized. */ 791 JSVM_UNINITIALIZED, 792 } JSVM_InitializedFlag; 793 794 /** 795 * @brief The promise-reject event. 796 * 797 * @since 18 798 */ 799 typedef enum { 800 /** Promise is rejected for other reasons. */ 801 JSVM_PROMISE_REJECT_OTHER_REASONS = 0, 802 /** Promise rejected with no handler. */ 803 JSVM_PROMISE_REJECT_WITH_NO_HANDLER = 1, 804 /** Add the handler after Promise has been rejected. */ 805 JSVM_PROMISE_ADD_HANDLER_AFTER_REJECTED = 2, 806 /** After the Promise has been resolved, try to reject the Promise again. */ 807 JSVM_PROMISE_REJECT_AFTER_RESOLVED = 3, 808 /** After the Promise has been resolved, try resolving the Promise again. */ 809 JSVM_PROMISE_RESOLVE_AFTER_RESOLVED = 4, 810 } JSVM_PromiseRejectEvent; 811 812 /** 813 * @brief The level of message error. 814 * 815 * @since 18 816 */ 817 typedef enum { 818 /** Log level message. */ 819 JSVM_MESSAGE_LOG = (1 << 0), 820 /** Debug level message. */ 821 JSVM_MESSAGE_DEBUG = (1 << 1), 822 /** Info level message. */ 823 JSVM_MESSAGE_INFO = (1 << 2), 824 /** Error level message. */ 825 JSVM_MESSAGE_ERROR = (1 << 3), 826 /** Warning level message. */ 827 JSVM_MESSAGE_WARNING = (1 << 4), 828 /** All level message. */ 829 JSVM_MESSAGE_ALL = 830 JSVM_MESSAGE_LOG | JSVM_MESSAGE_DEBUG | JSVM_MESSAGE_INFO | JSVM_MESSAGE_ERROR | JSVM_MESSAGE_WARNING, 831 } JSVM_MessageErrorLevel; 832 833 /** 834 * @brief Function pointer type of OOM-Error callback. 835 * 836 * @param location The location information of the OOM error. 837 * @param detail The detail of the OOM error. 838 * @param isHeapOOM Determine whether the OOM type is Heap OOM. 839 * 840 * @since 18 841 */ 842 typedef void(JSVM_CDECL* JSVM_HandlerForOOMError)(const char* location, const char* detail, bool isHeapOOM); 843 844 /** 845 * @brief Function pointer type of Fatal-Error callback. 846 * 847 * @param location The location information of the Fatal error. 848 * @param message The message of the Fatal error. 849 * 850 * @since 18 851 */ 852 typedef void(JSVM_CDECL* JSVM_HandlerForFatalError)(const char* location, const char* message); 853 854 /** 855 * @brief Function pointer type of Promise-Reject callback. 856 * 857 * @param env The environment that the function is invoked under. 858 * @param rejectEvent The promise-reject event. 859 * @param rejectInfo An JS-object containing two properties: 'promise' and 'value'. 860 * The 'promise' represents a reference to the Promise object that was rejected. 861 * The 'value' represents the rejection reason associated with that promise. 862 * 863 * @since 18 864 */ 865 typedef void(JSVM_CDECL* JSVM_HandlerForPromiseReject)(JSVM_Env env, 866 JSVM_PromiseRejectEvent rejectEvent, 867 JSVM_Value rejectInfo); 868 /** 869 * @brief The timing of GC callback trigger. 870 * 871 * @since 18 872 */ 873 typedef enum { 874 /** Trigger GC callback before GC. */ 875 JSVM_CB_TRIGGER_BEFORE_GC, 876 /** Trigger GC callback after GC. */ 877 JSVM_CB_TRIGGER_AFTER_GC, 878 } JSVM_CBTriggerTimeForGC; 879 880 /** 881 * @brief The GC type. 882 * 883 * @since 18 884 */ 885 typedef enum { 886 /** The GC algorithm is Scavenge. */ 887 JSVM_GC_TYPE_SCAVENGE = 1 << 0, 888 /** The GC algorithm is Minor-Mark-Compact. */ 889 JSVM_GC_TYPE_MINOR_MARK_COMPACT = 1 << 1, 890 /** The GC algorithm is Mark-Sweep-Compact. */ 891 JSVM_GC_TYPE_MARK_SWEEP_COMPACT = 1 << 2, 892 /** The GC algorithm is Incremental-Marking. */ 893 JSVM_GC_TYPE_INCREMENTAL_MARKING = 1 << 3, 894 /** The GC algorithm is Weak-Callbacks. */ 895 JSVM_GC_TYPE_PROCESS_WEAK_CALLBACKS = 1 << 4, 896 /** All GC algorithm. */ 897 JSVM_GC_TYPE_ALL = JSVM_GC_TYPE_SCAVENGE | JSVM_GC_TYPE_MINOR_MARK_COMPACT | JSVM_GC_TYPE_MARK_SWEEP_COMPACT | 898 JSVM_GC_TYPE_INCREMENTAL_MARKING | JSVM_GC_TYPE_PROCESS_WEAK_CALLBACKS, 899 } JSVM_GCType; 900 901 /** 902 * @brief The GC callback flags. 903 * 904 * @since 18 905 */ 906 typedef enum { 907 /** No GC callback falgs. */ 908 JSVM_NO_GC_CALLBACK_FLAGS, 909 /** Reserved object information will be built in the garbage collection callback. */ 910 JSVM_GC_CALLBACK_CONSTRUCT_RETAINED_OBJECT_INFOS, 911 /** Enforce Garbage Collection Callback. */ 912 JSVM_GC_CALLBACK_FORCED, 913 /** Synchronous phantom callback processing. */ 914 JSVM_GC_CALLBACK_SYNCHRONOUS_PHANTOM_CALLBACK_PROCESSING, 915 /** All available garbage objects are collected during garbage collection. */ 916 JSVM_GC_CALLBACK_COLLECT_ALL_AVAILABLE_GARBAGE, 917 /** Garbage collection collects all external memory. */ 918 JSVM_GC_CALLBACK_COLLECT_ALL_EXTERNAL_MEMORY, 919 /** Schedule Garbage Collection at Idle Time. */ 920 JSVM_GC_CALLBACK_SCHEDULE_IDLE_GARBAGE_COLLECTION, 921 } JSVM_GCCallbackFlags; 922 923 /** 924 * @brief Function pointer type of GC callback. 925 * 926 * @param vm The VM instance that the JSVM-API call is invoked under. 927 * @param gcType The gc type. 928 * @param flags The GC callback flags. 929 * @param data The native pointer data. 930 * 931 * @since 18 932 */ 933 typedef void(JSVM_CDECL* JSVM_HandlerForGC)(JSVM_VM vm, JSVM_GCType gcType, JSVM_GCCallbackFlags flags, void* data); 934 /** 935 * @brief The property-handler used to define class. 936 * 937 * @since 18 938 */ 939 typedef struct { 940 /** The instance object triggers the corresponding callback function. */ 941 JSVM_PropertyHandlerCfg propertyHandlerCfg; 942 /** Calling an instance object as a function will trigger this callback. */ 943 JSVM_Callback callAsFunctionCallback; 944 } JSVM_PropertyHandler; 945 946 /** 947 * @brief DefineClass options id. 948 * 949 * @since 18 950 */ 951 typedef enum { 952 /** Defining a class in normal mode. */ 953 JSVM_DEFINE_CLASS_NORMAL, 954 /** Defining a class with count. */ 955 JSVM_DEFINE_CLASS_WITH_COUNT, 956 /** Defining a class with property handler. */ 957 JSVM_DEFINE_CLASS_WITH_PROPERTY_HANDLER, 958 } JSVM_DefineClassOptionsId; 959 960 /** 961 * @brief DefineClass options. 962 * 963 * @since 18 964 */ 965 typedef struct { 966 /** DefineClass options id. */ 967 JSVM_DefineClassOptionsId id; 968 /** option content. */ 969 union { 970 /** for option value with pointer type. */ 971 void* ptr; 972 /** for option value with integer type */ 973 int num; 974 /** for option value with bool type */ 975 bool boolean; 976 } content; 977 } JSVM_DefineClassOptions; 978 /** @} */ 979 980 /** 981 * @brief Trace category for jsvm internal trace events. 982 * 983 * @since 18 984 */ 985 typedef enum { 986 /** Tracing main interface invoking of JSVM, such as run scripts. */ 987 JSVM_TRACE_VM, 988 /** Tracing interface invoking about compilation, such as CompileCodeBackground. */ 989 JSVM_TRACE_COMPILE, 990 /** Tracing interface invoking about execution status, such as Interrupts and Microtasks. */ 991 JSVM_TRACE_EXECUTE, 992 /** Tracing external callback */ 993 JSVM_TRACE_RUNTIME, 994 /** Tracing stack trace in JSVM. */ 995 JSVM_TRACE_STACK_TRACE, 996 /** Tracing main interface invoking of WASM, such as Compile Wasm Module and Instantiate. */ 997 JSVM_TRACE_WASM, 998 /** Tracing more detailed interface invoking of WASM, such as background compilation and wrappers. */ 999 JSVM_TRACE_WASM_DETAILED 1000 } JSVM_TraceCategory; 1001 1002 /** 1003 * @brief Debug options. 1004 * 1005 * @since 20 1006 */ 1007 typedef enum { 1008 /** Scope check. */ 1009 JSVM_SCOPE_CHECK, 1010 } JSVM_DebugOption; 1011 #endif /* ARK_RUNTIME_JSVM_JSVM_TYPE_H */ 1012