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 16 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 16 328 */ 329 JSVM_INVALID_TYPE 330 } JSVM_Status; 331 332 /** 333 * @brief limits the range of collected properties.. 334 * 335 * @since 11 336 */ 337 typedef enum { 338 /** will include all keys of the objects's prototype chain as well. */ 339 JSVM_KEY_INCLUDE_PROTOTYPES, 340 /** limits the collected properties to the given object only. */ 341 JSVM_KEY_OWN_ONLY 342 } JSVM_KeyCollectionMode; 343 344 /** 345 * @brief Property filter bits. They can be or'ed to build a composite filter.. 346 * 347 * @since 11 348 */ 349 typedef enum { 350 /** key all properties. */ 351 JSVM_KEY_ALL_PROPERTIES = 0, 352 /** key writable. */ 353 JSVM_KEY_WRITABLE = 1, 354 /** key enumerable. */ 355 JSVM_KEY_ENUMERABLE = 1 << 1, 356 /** key configurable. */ 357 JSVM_KEY_CONFIGURABLE = 1 << 2, 358 /** key skip strings. */ 359 JSVM_KEY_SKIP_STRINGS = 1 << 3, 360 /** key skip symbols. */ 361 JSVM_KEY_SKIP_SYMBOLS = 1 << 4 362 } JSVM_KeyFilter; 363 364 /** 365 * @brief key conversion select. 366 * 367 * @since 11 368 */ 369 typedef enum { 370 /** will return numbers for integer indices. */ 371 JSVM_KEY_KEEP_NUMBERS, 372 /** will convert integer indices to strings. */ 373 JSVM_KEY_NUMBERS_TO_STRINGS 374 } JSVM_KeyConversion; 375 376 /** 377 * @brief Memory pressure level. 378 * 379 * @since 11 380 */ 381 typedef enum { 382 /** none pressure. */ 383 JSVM_MEMORY_PRESSURE_LEVEL_NONE, 384 /** moderate pressure. */ 385 JSVM_MEMORY_PRESSURE_LEVEL_MODERATE, 386 /** critical pressure. */ 387 JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL, 388 } JSVM_MemoryPressureLevel; 389 390 /** 391 * 392 * @brief Compile mode 393 * 394 * @since 12 395 */ 396 typedef enum { 397 /** default mode. */ 398 JSVM_COMPILE_MODE_DEFAULT, 399 /** consume code cache. */ 400 JSVM_COMPILE_MODE_CONSUME_CODE_CACHE, 401 /** apply eager compile. */ 402 JSVM_COMPILE_MODE_EAGER_COMPILE, 403 /** preset for compile profile. */ 404 JSVM_COMPILE_MODE_PRODUCE_COMPILE_PROFILE, 405 /** consume compile profile. */ 406 JSVM_COMPILE_MODE_CONSUME_COMPILE_PROFILE, 407 } JSVM_CompileMode; 408 409 /** 410 * @brief Compile option id 411 * 412 * @since 12 413 */ 414 typedef enum { 415 /** compile mode. */ 416 JSVM_COMPILE_MODE, 417 /** code cache content. */ 418 JSVM_COMPILE_CODE_CACHE, 419 /** script origin. */ 420 JSVM_COMPILE_SCRIPT_ORIGIN, 421 /** compile profile content. */ 422 JSVM_COMPILE_COMPILE_PROFILE, 423 /** switch for source map support. */ 424 JSVM_COMPILE_ENABLE_SOURCE_MAP, 425 } JSVM_CompileOptionId; 426 427 /** 428 * @brief Heap statisics. 429 * 430 * @since 12 431 */ 432 typedef struct { 433 /** the size of the total heap. */ 434 size_t totalHeapSize; 435 /** the executable size of the total heap. */ 436 size_t totalHeapSizeExecutable; 437 /** the physical size of the total heap. */ 438 size_t totalPhysicalSize; 439 /** the available size of the total heap. */ 440 size_t totalAvailableSize; 441 /** used size of the heap. */ 442 size_t usedHeapSize; 443 /** heap size limit. */ 444 size_t heapSizeLimit; 445 /** memory requested by the heap. */ 446 size_t mallocedMemory; 447 /** heap-requested external memory. */ 448 size_t externalMemory; 449 /** peak memory requested by the heap. */ 450 size_t peakMallocedMemory; 451 /** the number of native contexts. */ 452 size_t numberOfNativeContexts; 453 /** the number of detached contexts. */ 454 size_t numberOfDetachedContexts; 455 /** the size of the total global handles. */ 456 size_t totalGlobalHandlesSize; 457 /** the size of the used global handles. */ 458 size_t usedGlobalHandlesSize; 459 } JSVM_HeapStatistics; 460 461 /** 462 * @brief Init the JavaScript VM with init option. 463 * 464 * @since 11 465 */ 466 typedef struct { 467 /** 468 * Optional nullptr-terminated array of raw adddresses in the embedder that the 469 * VM can match against during serialization and use for deserialization. This 470 * array and its content must stay valid for the entire lifetime of the VM 471 * instance. 472 */ 473 const intptr_t* externalReferences; 474 475 /** 476 * Flags for the VM. IF removeFlags is true, recognized flags will be removed 477 * from (argc, argv). Note that these flags are specific to VM. 478 * They are mainly used for development. Do not include them in production as 479 * they might not take effect if the VM is different from the development 480 * environment. 481 */ 482 int* argc; 483 /** argv . */ 484 char** argv; 485 /** remove flags. */ 486 bool removeFlags; 487 } JSVM_InitOptions; 488 489 /** 490 * @brief Create the JavaScript VM with init option. 491 * 492 * @since 11 493 */ 494 typedef struct { 495 /** optional limits of memory use of the vm. */ 496 size_t maxOldGenerationSize; 497 /** optional limits of memory use of the vm. */ 498 size_t maxYoungGenerationSize; 499 /** optional limits of memory use of the vm. */ 500 size_t initialOldGenerationSize; 501 /** optional limits of memory use of the vm. */ 502 size_t initialYoungGenerationSize; 503 /** Optional startup snapshot data. */ 504 const char* snapshotBlobData; 505 /** Optional size of the startup snapshot data. */ 506 size_t snapshotBlobSize; 507 /** Whether the VM is used for creating snapshot. */ 508 bool isForSnapshotting; 509 } JSVM_CreateVMOptions; 510 511 /** 512 * @brief JavaScript VM info. 513 * 514 * @since 11 515 */ 516 typedef struct { 517 /** The highest API version this VM supports. */ 518 uint32_t apiVersion; 519 /** The engine name implementing the VM. */ 520 const char* engine; 521 /** The version of the VM. */ 522 const char* version; 523 /** The cached data version tag. */ 524 uint32_t cachedDataVersionTag; 525 } JSVM_VMInfo; 526 527 /** 528 * @brief Property descriptor. 529 * 530 * @since 11 531 */ 532 typedef struct { 533 /** Optional string describing the key for the property, encoded as UTF8. 534 * One of utf8name or name must be provided for the property. 535 */ 536 const char* utf8name; 537 /** Optional value that points to a JavaScript string or symbol to be used as the key for the property. */ 538 JSVM_Value name; 539 /** Set this to make the property descriptor object's value property to be 540 * a JavaScript function represented by method. 541 */ 542 JSVM_Callback method; 543 /** A function to call when a get access of the property is performed. */ 544 JSVM_Callback getter; 545 /** A function to call when a set access of the property is performed. */ 546 JSVM_Callback setter; 547 /** The value that's retrieved by a get access of the property if the property is a data property. */ 548 JSVM_Value value; 549 /** The attributes associated with the particular property. */ 550 JSVM_PropertyAttributes attributes; 551 } JSVM_PropertyDescriptor; 552 553 /** 554 * @brief JSVM-API uses both return values and JavaScript exceptions for error handling 555 * @since 11 556 */ 557 typedef struct { 558 /** UTF8-encoded string containing a VM-neutral description of the error. */ 559 const char* errorMessage; 560 /** Reserved for VM-specific error details. This is currently not implemented for any VM. */ 561 void* engineReserved; 562 /** VM-specific error code. This is currently not implemented for any VM. */ 563 uint32_t engineErrorCode; 564 /** The JSVM-API status code that originated with the last error. */ 565 JSVM_Status errorCode; 566 } JSVM_ExtendedErrorInfo; 567 568 /** 569 * @brief A 128-bit value stored as two unsigned 64-bit integers. 570 * It serves as a UUID with which JavaScript objects or externals can be "tagged" 571 * in order to ensure that they are of a certain type. 572 * 573 * @since 11 574 */ 575 typedef struct { 576 /** lower type. */ 577 uint64_t lower; 578 /** upper type. */ 579 uint64_t upper; 580 } JSVM_TypeTag; 581 582 /** 583 * @brief When the getter, setter, call, etc. behavior occurs on the object, the corresponding 584 * the corresponding callback will be triggered. 585 * 586 * @since 12 587 */ 588 typedef struct { 589 /** A callback function triggered by getting a named property of an instance object. */ 590 JSVM_Value(JSVM_CDECL* genericNamedPropertyGetterCallback)(JSVM_Env env, 591 JSVM_Value name, 592 JSVM_Value thisArg, 593 JSVM_Value namedPropertyData); 594 595 /** A callback function triggered by setting a named property of an instance object. */ 596 JSVM_Value(JSVM_CDECL* genericNamedPropertySetterCallback)(JSVM_Env env, 597 JSVM_Value name, 598 JSVM_Value property, 599 JSVM_Value thisArg, 600 JSVM_Value namedPropertyData); 601 602 /** A callback function triggered by deleting a named property of an instance object. */ 603 JSVM_Value(JSVM_CDECL* genericNamedPropertyDeleterCallback)(JSVM_Env env, 604 JSVM_Value name, 605 JSVM_Value thisArg, 606 JSVM_Value namedPropertyData); 607 608 /** A callback function triggered by getting all named properties requests on an object. */ 609 JSVM_Value(JSVM_CDECL* genericNamedPropertyEnumeratorCallback)(JSVM_Env env, 610 JSVM_Value thisArg, 611 JSVM_Value namedPropertyData); 612 613 /** A callback function triggered by getting an indexed property of an instance object. */ 614 JSVM_Value(JSVM_CDECL* genericIndexedPropertyGetterCallback)(JSVM_Env env, 615 JSVM_Value index, 616 JSVM_Value thisArg, 617 JSVM_Value indexedPropertyData); 618 619 /** A callback function triggered by setting an indexed property of an instance object. */ 620 JSVM_Value(JSVM_CDECL* genericIndexedPropertySetterCallback)(JSVM_Env env, 621 JSVM_Value index, 622 JSVM_Value property, 623 JSVM_Value thisArg, 624 JSVM_Value indexedPropertyData); 625 626 /** A callback function triggered by deleting an indexed property of an instance object. */ 627 JSVM_Value(JSVM_CDECL* genericIndexedPropertyDeleterCallback)(JSVM_Env env, 628 JSVM_Value index, 629 JSVM_Value thisArg, 630 JSVM_Value indexedPropertyData); 631 632 /** A callback function triggered by getting all indexed properties requests on an object. */ 633 JSVM_Value(JSVM_CDECL* genericIndexedPropertyEnumeratorCallback)(JSVM_Env env, 634 JSVM_Value thisArg, 635 JSVM_Value indexedPropertyData); 636 637 /** data will be utilized by the named property callbacks in this struct. */ 638 JSVM_Value namedPropertyData; 639 640 /** data will be utilized by the indexed property callbacks in this struct. */ 641 JSVM_Value indexedPropertyData; 642 } JSVM_PropertyHandlerConfigurationStruct; 643 644 /** 645 * @brief The function pointer type of the callback provided by the instance object, which triggers the 646 * corresponding callback when getter, setter, call, and other behaviors occur on the object. 647 * 648 * @since 12 649 */ 650 typedef JSVM_PropertyHandlerConfigurationStruct* JSVM_PropertyHandlerCfg; 651 652 /** 653 * 654 * @brief Source code information. 655 * 656 * @since 12 657 */ 658 typedef struct { 659 /** Sourcemap url. */ 660 const char* sourceMapUrl; 661 /** Resource name. */ 662 const char* resourceName; 663 /** Resource line offset. */ 664 size_t resourceLineOffset; 665 /** Resource column offset. */ 666 size_t resourceColumnOffset; 667 } JSVM_ScriptOrigin; 668 669 /** 670 * @brief Compile Options 671 * 672 * @since 12 673 */ 674 typedef struct { 675 /** compile option id. */ 676 JSVM_CompileOptionId id; 677 /** option content. */ 678 union { 679 /** ptr type. */ 680 void* ptr; 681 /** int type. */ 682 int num; 683 /** bool type. */ 684 bool boolean; 685 } content; 686 } JSVM_CompileOptions; 687 688 /** 689 * @brief code cache passed with JSVM_COMPILE_CODE_CACHE 690 * 691 * @since 12 692 */ 693 typedef struct { 694 /** cache pointer. */ 695 uint8_t* cache; 696 /** length. */ 697 size_t length; 698 } JSVM_CodeCache; 699 700 /** 701 * @brief WebAssembly function optimization level 702 * 703 * @since 12 704 */ 705 typedef enum { 706 /** baseline optimization level. */ 707 JSVM_WASM_OPT_BASELINE = 10, 708 /** high optimization level. */ 709 JSVM_WASM_OPT_HIGH = 20, 710 } JSVM_WasmOptLevel; 711 712 /** 713 * @brief Cache data type 714 * 715 * @since 12 716 */ 717 typedef enum { 718 /** js code cache, generated by OH_JSVM_CreateCodeCache */ 719 JSVM_CACHE_TYPE_JS, 720 /** WebAssembly cache, generated by OH_JSVM_CreateWasmCache */ 721 JSVM_CACHE_TYPE_WASM, 722 } JSVM_CacheType; 723 724 /** 725 * @brief Microtask policies of JSVM. 726 * 727 * @since 16 728 */ 729 typedef enum { 730 /** Microtasks are invoked with the OH_JSVM_PerformMicrotaskCheckpoint() method. */ 731 JSVM_MICROTASK_EXPLICIT = 0, 732 /** Microtasks are invoked when the script call depth decrements to zero. 733 * Default mode. 734 */ 735 JSVM_MICROTASK_AUTO, 736 } JSVM_MicrotaskPolicy; 737 738 /** 739 * @brief compile profile passed with JSVM_COMPILE_COMPILE_PROFILE 740 * 741 * @since 12 742 */ 743 typedef const struct { 744 /** profile pointer. */ 745 int* profile; 746 /** length. */ 747 size_t length; 748 } JSVM_CompileProfile; 749 750 /** 751 * @brief Regular expression flag bits. They can be or'ed to enable a set of flags. 752 * 753 * @since 12 754 */ 755 typedef enum { 756 /** None mode. */ 757 JSVM_REGEXP_NONE = 0, 758 /** Global mode. */ 759 JSVM_REGEXP_GLOBAL = 1 << 0, 760 /** Ignore Case mode. */ 761 JSVM_REGEXP_IGNORE_CASE = 1 << 1, 762 /** Multiline mode. */ 763 JSVM_REGEXP_MULTILINE = 1 << 2, 764 /** Sticky mode. */ 765 JSVM_REGEXP_STICKY = 1 << 3, 766 /** Unicode mode. */ 767 JSVM_REGEXP_UNICODE = 1 << 4, 768 /** dotAll mode. */ 769 JSVM_REGEXP_DOT_ALL = 1 << 5, 770 /** Linear mode. */ 771 JSVM_REGEXP_LINEAR = 1 << 6, 772 /** Has Indices mode. */ 773 JSVM_REGEXP_HAS_INDICES = 1 << 7, 774 /** Unicode Sets mode. */ 775 JSVM_REGEXP_UNICODE_SETS = 1 << 8, 776 } JSVM_RegExpFlags; 777 778 /** 779 * @brief initialization flag 780 * 781 * @since 12 782 */ 783 typedef enum { 784 /** initialize with zero. */ 785 JSVM_ZERO_INITIALIZED, 786 /** leave uninitialized. */ 787 JSVM_UNINITIALIZED, 788 } JSVM_InitializedFlag; 789 790 /** 791 * @brief The promise-reject event. 792 * 793 * @since 16 794 */ 795 typedef enum { 796 /** Promise is rejected for other reasons. */ 797 JSVM_PROMISE_REJECT_OTHER_REASONS = 0, 798 /** Promise rejected with no handler. */ 799 JSVM_PROMISE_REJECT_WITH_NO_HANDLER = 1, 800 /** Add the handler after Promise has been rejected. */ 801 JSVM_PROMISE_ADD_HANDLER_AFTER_REJECTED = 2, 802 /** After the Promise has been resolved, try to reject the Promise again. */ 803 JSVM_PROMISE_REJECT_AFTER_RESOLVED = 3, 804 /** After the Promise has been resolved, try resolving the Promise again. */ 805 JSVM_PROMISE_RESOLVE_AFTER_RESOLVED = 4, 806 } JSVM_PromiseRejectEvent; 807 808 /** 809 * @brief The level of message error. 810 * 811 * @since 16 812 */ 813 typedef enum { 814 /** Log level message. */ 815 JSVM_MESSAGE_LOG = (1 << 0), 816 /** Debug level message. */ 817 JSVM_MESSAGE_DEBUG = (1 << 1), 818 /** Info level message. */ 819 JSVM_MESSAGE_INFO = (1 << 2), 820 /** Error level message. */ 821 JSVM_MESSAGE_ERROR = (1 << 3), 822 /** Warning level message. */ 823 JSVM_MESSAGE_WARNING = (1 << 4), 824 /** All level message. */ 825 JSVM_MESSAGE_ALL = 826 JSVM_MESSAGE_LOG | JSVM_MESSAGE_DEBUG | JSVM_MESSAGE_INFO | JSVM_MESSAGE_ERROR | JSVM_MESSAGE_WARNING, 827 } JSVM_MessageErrorLevel; 828 829 /** 830 * @brief Function pointer type of OOM-Error callback. 831 * 832 * @param location The location information of the OOM error. 833 * @param detail The detail of the OOM error. 834 * @param isHeapOOM Determine whether the OOM type is Heap OOM. 835 * 836 * @since 16 837 */ 838 typedef void(JSVM_CDECL* JSVM_HandlerForOOMError)(const char* location, const char* detail, bool isHeapOOM); 839 840 /** 841 * @brief Function pointer type of Fatal-Error callback. 842 * 843 * @param location The location information of the Fatal error. 844 * @param message The message of the Fatal error. 845 * 846 * @since 16 847 */ 848 typedef void(JSVM_CDECL* JSVM_HandlerForFatalError)(const char* location, const char* message); 849 850 /** 851 * @brief Function pointer type of Promise-Reject callback. 852 * 853 * @param env The environment that the function is invoked under. 854 * @param rejectEvent The promise-reject event. 855 * @param rejectInfo An JS-object containing two properties: 'promise' and 'value'. 856 * The 'promise' represents a reference to the Promise object that was rejected. 857 * The 'value' represents the rejection reason associated with that promise. 858 * 859 * @since 16 860 */ 861 typedef void(JSVM_CDECL* JSVM_HandlerForPromiseReject)(JSVM_Env env, 862 JSVM_PromiseRejectEvent rejectEvent, 863 JSVM_Value rejectInfo); 864 /** 865 * @brief The timing of GC callback trigger. 866 * 867 * @since 16 868 */ 869 typedef enum { 870 /** Trigger GC callback before GC. */ 871 JSVM_CB_TRIGGER_BEFORE_GC, 872 /** Trigger GC callback after GC. */ 873 JSVM_CB_TRIGGER_AFTER_GC, 874 } JSVM_CBTriggerTimeForGC; 875 876 /** 877 * @brief The GC type. 878 * 879 * @since 16 880 */ 881 typedef enum { 882 /** The GC algorithm is Scavenge. */ 883 JSVM_GC_TYPE_SCAVENGE = 1 << 0, 884 /** The GC algorithm is Minor-Mark-Compact. */ 885 JSVM_GC_TYPE_MINOR_MARK_COMPACT = 1 << 1, 886 /** The GC algorithm is Mark-Sweep-Compact. */ 887 JSVM_GC_TYPE_MARK_SWEEP_COMPACT = 1 << 2, 888 /** The GC algorithm is Incremental-Marking. */ 889 JSVM_GC_TYPE_INCREMENTAL_MARKING = 1 << 3, 890 /** The GC algorithm is Weak-Callbacks. */ 891 JSVM_GC_TYPE_PROCESS_WEAK_CALLBACKS = 1 << 4, 892 /** All GC algorithm. */ 893 JSVM_GC_TYPE_ALL = JSVM_GC_TYPE_SCAVENGE | JSVM_GC_TYPE_MINOR_MARK_COMPACT | JSVM_GC_TYPE_MARK_SWEEP_COMPACT | 894 JSVM_GC_TYPE_INCREMENTAL_MARKING | JSVM_GC_TYPE_PROCESS_WEAK_CALLBACKS, 895 } JSVM_GCType; 896 897 /** 898 * @brief The GC callback flags. 899 * 900 * @since 16 901 */ 902 typedef enum { 903 /** No GC callback falgs. */ 904 JSVM_NO_GC_CALLBACK_FLAGS, 905 /** Reserved object information will be built in the garbage collection callback. */ 906 JSVM_GC_CALLBACK_CONSTRUCT_RETAINED_OBJECT_INFOS, 907 /** Enforce Garbage Collection Callback. */ 908 JSVM_GC_CALLBACK_FORCED, 909 /** Synchronous phantom callback processing. */ 910 JSVM_GC_CALLBACK_SYNCHRONOUS_PHANTOM_CALLBACK_PROCESSING, 911 /** All available garbage objects are collected during garbage collection. */ 912 JSVM_GC_CALLBACK_COLLECT_ALL_AVAILABLE_GARBAGE, 913 /** Garbage collection collects all external memory. */ 914 JSVM_GC_CALLBACK_COLLECT_ALL_EXTERNAL_MEMORY, 915 /** Schedule Garbage Collection at Idle Time. */ 916 JSVM_GC_CALLBACK_SCHEDULE_IDLE_GARBAGE_COLLECTION, 917 } JSVM_GCCallbackFlags; 918 919 /** 920 * @brief Function pointer type of GC callback. 921 * 922 * @param vm The VM instance that the JSVM-API call is invoked under. 923 * @param gcType The gc type. 924 * @param flags The GC callback flags. 925 * @param data The native pointer data. 926 * 927 * @since 16 928 */ 929 typedef void(JSVM_CDECL* JSVM_HandlerForGC)(JSVM_VM vm, JSVM_GCType gcType, JSVM_GCCallbackFlags flags, void* data); 930 /** 931 * @brief The property-handler used to define class. 932 * 933 * @since 16 934 */ 935 typedef struct { 936 /** The instance object triggers the corresponding callback function. */ 937 JSVM_PropertyHandlerCfg propertyHandlerCfg; 938 /** Calling an instance object as a function will trigger this callback. */ 939 JSVM_Callback callAsFunctionCallback; 940 } JSVM_PropertyHandler; 941 942 /** 943 * @brief DefineClass options id. 944 * 945 * @since 16 946 */ 947 typedef enum { 948 /** Defining a class in normal mode. */ 949 JSVM_DEFINE_CLASS_NORMAL, 950 /** Defining a class with count. */ 951 JSVM_DEFINE_CLASS_WITH_COUNT, 952 /** Defining a class with property handler. */ 953 JSVM_DEFINE_CLASS_WITH_PROPERTY_HANDLER, 954 } JSVM_DefineClassOptionsId; 955 956 /** 957 * @brief DefineClass options. 958 * 959 * @since 16 960 */ 961 typedef struct { 962 /** DefineClass options id. */ 963 JSVM_DefineClassOptionsId id; 964 /** option content. */ 965 union { 966 /** for option value with pointer type. */ 967 void* ptr; 968 /** for option value with integer type */ 969 int num; 970 /** for option value with bool type */ 971 bool boolean; 972 } content; 973 } JSVM_DefineClassOptions; 974 /** @} */ 975 976 /** 977 * @brief Trace category for jsvm internal trace events. 978 * 979 * @since 16 980 */ 981 typedef enum { 982 /** Tracing main interface invoking of JSVM, such as run scripts. */ 983 JSVM_TRACE_VM, 984 /** Tracing interface invoking about compilation, such as CompileCodeBackground. */ 985 JSVM_TRACE_COMPILE, 986 /** Tracing interface invoking about execution status, such as Interrupts and Microtasks. */ 987 JSVM_TRACE_EXECUTE, 988 /** Tracing external callback */ 989 JSVM_TRACE_RUNTIME, 990 /** Tracing stack trace in JSVM. */ 991 JSVM_TRACE_STACK_TRACE, 992 /** Tracing main interface invoking of WASM, such as Compile Wasm Module and Instantiate. */ 993 JSVM_TRACE_WASM, 994 /** Tracing more detailed interface invoking of WASM, such as background compilation and wrappers. */ 995 JSVM_TRACE_WASM_DETAILED 996 } JSVM_TraceCategory; 997 #endif /* ARK_RUNTIME_JSVM_JSVM_TYPE_H */ 998