├── .clang-format ├── .gitattributes ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── NOTICE ├── README.md ├── include └── js.h ├── package.json ├── src └── js.cc └── test ├── CMakeLists.txt ├── add-finalizer.c ├── add-teardown-callback-deferred-remove.c ├── add-teardown-callback-deferred.c ├── add-teardown-callback-multiple.c ├── add-teardown-callback-remove.c ├── add-teardown-callback.c ├── add-type-tag-delegate.c ├── add-type-tag.c ├── adjust-external-memory.c ├── atomics-wait-timeout-notify.c ├── atomics-wait-timeout.c ├── call-function-ignore-return.c ├── call-function-in-context.c ├── call-function-microtask-nested.c ├── call-function-microtask.c ├── call-function-throw.c ├── call-function.c ├── call-threadsafe-function-after-release.c ├── call-threadsafe-function-with-data.c ├── coerce-number-to-string.c ├── coerce-object-to-string.c ├── coerce-string-to-number.c ├── coerce-symbol-to-string.c ├── create-arraybuffer-too-large.c ├── create-arraybuffer-with-backing-store.c ├── create-arraybuffer.c ├── create-date-nan.c ├── create-date-overflow.c ├── create-date.c ├── create-delegate-delete-throw.c ├── create-delegate-get-throw.c ├── create-delegate-get-without-has.c ├── create-delegate-has-throw.c ├── create-delegate-keys-throw.c ├── create-delegate-set-throw.c ├── create-delegate.c ├── create-env-multiple.c ├── create-env.c ├── create-external-arraybuffer-with-finalizer-detach.c ├── create-external-arraybuffer-with-finalizer.c ├── create-external-arraybuffer.c ├── create-external-string-latin1-with-finalizer.c ├── create-external-string-latin1.c ├── create-external-string-utf16le-with-finalizer.c ├── create-external-string-utf16le.c ├── create-external-string-utf8-with-finalizer.c ├── create-external-string-utf8.c ├── create-external-with-finalizer.c ├── create-external.c ├── create-function-args-extra.c ├── create-function-args-fewer.c ├── create-function-throw-indirect.c ├── create-function-throw.c ├── create-function-with-finalizer.c ├── create-function-with-source.c ├── create-function.c ├── create-module-import-missing.c ├── create-promise-reject.c ├── create-promise-resolve.c ├── create-reference-array.c ├── create-reference-bigint.c ├── create-reference-boolean.c ├── create-reference-function.c ├── create-reference-null.c ├── create-reference-number.c ├── create-reference-object-multiple.c ├── create-reference-object-weak.c ├── create-reference-object.c ├── create-reference-string.c ├── create-reference-symbol.c ├── create-reference-undefined.c ├── create-sharedarraybuffer-with-backing-store.c ├── create-sharedarraybuffer.c ├── create-string-utf8-with-null.c ├── create-string-utf8.c ├── create-threadsafe-function-with-callback.c ├── create-threadsafe-function-with-context.c ├── create-threadsafe-function-with-finalizer.c ├── create-threadsafe-function.c ├── create-typed-function-no-jit.c ├── create-typed-function-with-arraybuffer.c ├── create-typed-function-with-bigint64.c ├── create-typed-function-with-biguint64.c ├── create-typed-function-with-dataview.c ├── create-typed-function-with-finalizer.c ├── create-typed-function-with-int32.c ├── create-typed-function-with-int64.c ├── create-typed-function-with-int8array.c ├── create-typed-function-with-number.c ├── create-typed-function-with-pointer.c ├── create-typed-function-with-receiver.c ├── create-typed-function-with-string-latin1.c ├── create-typed-function-with-string-utf16le.c ├── create-typed-function-with-uint32.c ├── create-typed-function-with-uint64.c ├── create-typed-function-with-uint8array.c ├── create-typed-function.c ├── create-uint8array.c ├── create-unsafe-arraybuffer.c ├── define-class-with-method.c ├── define-class-with-static-method.c ├── define-class-with-static-value.c ├── define-class-with-symbol-property.c ├── define-class-with-value.c ├── define-class.c ├── dynamic-import-without-handler.c ├── dynamic-import.c ├── fatal-exception.c ├── fixtures ├── Makefile ├── atomics-wait-timeout-notify.js ├── atomics-wait-timeout-notify.js.h ├── atomics-wait-timeout.js ├── atomics-wait-timeout.js.h ├── many-large-allocs.js ├── many-large-allocs.js.h ├── many-small-allocs.js ├── many-small-allocs.js.h ├── promise-rejection-unhandled-deferred.js ├── promise-rejection-unhandled-deferred.js.h ├── promise-rejection-unhandled.js ├── promise-rejection-unhandled.js.h ├── promise-rejection.js ├── promise-rejection.js.h ├── wasm-async-log.js ├── wasm-async-log.js.h ├── wasm-async.js ├── wasm-async.js.h ├── wasm-sync.js ├── wasm-sync.js.h └── wasm.wat ├── get-arraybuffer-info.c ├── get-dataview-info.c ├── get-platform-identifier.c ├── get-platform-limits.c ├── get-platform-version.c ├── get-property-missing.c ├── get-typedarray-info-uint16array-large.c ├── get-typedarray-info-uint16array.c ├── get-typedarray-info-uint8array-large.c ├── get-typedarray-info-uint8array-with-offset.c ├── get-typedarray-info-uint8array.c ├── get-value-string-utf8-length.c ├── get-value-string-utf8-no-null.c ├── get-value-string-utf8.c ├── helpers.h ├── import-meta-throw.c ├── import-meta.c ├── inspector-pause.c ├── inspector.c ├── many-large-allocs.c ├── many-small-allocs.c ├── promise-rejection-unhandled-reentrant-deferred.c ├── promise-rejection-unhandled-reentrant.c ├── promise-rejection-unhandled.c ├── promise-rejection.c ├── run-module-async.c ├── run-module-cyclic-import.c ├── run-module-double-nested-import.c ├── run-module-in-context.c ├── run-module-nested-import.c ├── run-module-throw.c ├── run-module.c ├── run-script-ignore-return.c ├── run-script-in-context.c ├── run-script-throw.c ├── run-script.c ├── set-named-property.c ├── terminate-execution.c ├── threads-platform-loop.c ├── threads.c ├── throw-error-formatted.c ├── throw-error.c ├── typeof-bigint.c ├── typeof-boolean.c ├── typeof-external.c ├── typeof-function.c ├── typeof-null.c ├── typeof-number.c ├── typeof-object.c ├── typeof-string.c ├── typeof-symbol.c ├── typeof-undefined.c ├── wasm-async-io-multiple.c ├── wasm-async-io.c ├── wasm-async.c ├── wasm.c ├── wrap-remove-with-finalizer.c ├── wrap-with-finalizer.c ├── wrap-with-reference.c └── wrap.c /.clang-format: -------------------------------------------------------------------------------- 1 | AlignAfterOpenBracket: BlockIndent 2 | AlignConsecutiveMacros: Consecutive 3 | AlignEscapedNewlines: DontAlign 4 | AllowShortIfStatementsOnASingleLine: AllIfsAndElse 5 | AlwaysBreakAfterReturnType: All 6 | BinPackArguments: false 7 | BinPackParameters: false 8 | ContinuationIndentWidth: 2 9 | ColumnLimit: 0 10 | SpaceAfterCStyleCast: true 11 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | prebuilds/ 3 | node_modules/ 4 | package-lock.json 5 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Holepunch Inc 2 | 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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "libjs", 4 | "repository": { 5 | "type": "git", 6 | "url": "git+https://github.com/holepunchto/libjs.git" 7 | }, 8 | "author": "Holepunch", 9 | "license": "Apache-2.0", 10 | "bugs": { 11 | "url": "https://github.com/holepunchto/libjs/issues" 12 | }, 13 | "homepage": "https://github.com/holepunchto/libjs#readme", 14 | "devDependencies": { 15 | "cmake-fetch": "^1.0.0", 16 | "cmake-gn": "^1.0.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/add-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool finalize_called = false; 9 | 10 | static void 11 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 12 | finalize_called = true; 13 | 14 | assert((intptr_t) data == 42); 15 | } 16 | 17 | int 18 | main() { 19 | int e; 20 | 21 | uv_loop_t *loop = uv_default_loop(); 22 | 23 | js_platform_options_t options = { 24 | .expose_garbage_collection = true, 25 | .trace_garbage_collection = true, 26 | }; 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, &options, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_value_t *script; 41 | e = js_create_string_utf8(env, (utf8_t *) "({ hello: 'world' })", -1, &script); 42 | assert(e == 0); 43 | 44 | js_value_t *object; 45 | e = js_run_script(env, NULL, 0, 0, script, &object); 46 | assert(e == 0); 47 | 48 | e = js_add_finalizer(env, object, (void *) 42, on_finalize, NULL, NULL); 49 | assert(e == 0); 50 | 51 | e = js_close_handle_scope(env, scope); 52 | assert(e == 0); 53 | 54 | e = js_request_garbage_collection(env); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | 66 | assert(finalize_called); 67 | } 68 | -------------------------------------------------------------------------------- /test/add-teardown-callback-deferred-remove.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static uv_timer_t timer; 9 | 10 | static bool teardown_called = false; 11 | 12 | static void 13 | on_teardown(js_deferred_teardown_t *handle, void *data) { 14 | int e; 15 | 16 | teardown_called = true; 17 | } 18 | 19 | int 20 | main() { 21 | int e; 22 | 23 | uv_loop_t *loop = uv_default_loop(); 24 | 25 | js_platform_t *platform; 26 | e = js_create_platform(loop, NULL, &platform); 27 | assert(e == 0); 28 | 29 | js_env_t *env; 30 | e = js_create_env(loop, platform, NULL, &env); 31 | assert(e == 0); 32 | 33 | js_deferred_teardown_t *handle; 34 | e = js_add_deferred_teardown_callback(env, on_teardown, (void *) env, &handle); 35 | assert(e == 0); 36 | 37 | e = js_finish_deferred_teardown_callback(handle); 38 | assert(e == 0); 39 | 40 | e = js_destroy_env(env); 41 | assert(e == 0); 42 | 43 | assert(teardown_called == false); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/add-teardown-callback-deferred.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static uv_timer_t timer; 9 | 10 | static bool teardown_called = false; 11 | 12 | static void 13 | on_timer(uv_timer_t *timer) { 14 | int e; 15 | 16 | js_deferred_teardown_t *handle = (js_deferred_teardown_t *) timer->data; 17 | 18 | e = js_finish_deferred_teardown_callback(handle); 19 | assert(e == 0); 20 | } 21 | 22 | static void 23 | on_teardown(js_deferred_teardown_t *handle, void *data) { 24 | int e; 25 | 26 | teardown_called = true; 27 | 28 | uv_loop_t *loop = uv_default_loop(); 29 | 30 | e = uv_timer_init(loop, &timer); 31 | assert(e == 0); 32 | 33 | timer.data = (void *) handle; 34 | 35 | e = uv_timer_start(&timer, on_timer, 100, 0); 36 | assert(e == 0); 37 | } 38 | 39 | int 40 | main() { 41 | int e; 42 | 43 | uv_loop_t *loop = uv_default_loop(); 44 | 45 | js_platform_t *platform; 46 | e = js_create_platform(loop, NULL, &platform); 47 | assert(e == 0); 48 | 49 | js_env_t *env; 50 | e = js_create_env(loop, platform, NULL, &env); 51 | assert(e == 0); 52 | 53 | e = js_add_deferred_teardown_callback(env, on_teardown, (void *) env, NULL); 54 | assert(e == 0); 55 | 56 | e = js_destroy_env(env); 57 | assert(e == 0); 58 | 59 | assert(teardown_called); 60 | 61 | e = js_destroy_platform(platform); 62 | assert(e == 0); 63 | 64 | e = uv_run(loop, UV_RUN_DEFAULT); 65 | assert(e == 0); 66 | } 67 | -------------------------------------------------------------------------------- /test/add-teardown-callback-multiple.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static int teardown_called = 0; 9 | 10 | static void 11 | on_teardown(void *data) { 12 | int e; 13 | 14 | teardown_called++; 15 | 16 | assert((intptr_t) data == teardown_called); 17 | } 18 | 19 | int 20 | main() { 21 | int e; 22 | 23 | uv_loop_t *loop = uv_default_loop(); 24 | 25 | js_platform_t *platform; 26 | e = js_create_platform(loop, NULL, &platform); 27 | assert(e == 0); 28 | 29 | js_env_t *env; 30 | e = js_create_env(loop, platform, NULL, &env); 31 | assert(e == 0); 32 | 33 | e = js_add_teardown_callback(env, on_teardown, (void *) 3); 34 | assert(e == 0); 35 | 36 | e = js_add_teardown_callback(env, on_teardown, (void *) 2); 37 | assert(e == 0); 38 | 39 | e = js_add_teardown_callback(env, on_teardown, (void *) 1); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | assert(teardown_called == 3); 46 | 47 | e = js_destroy_platform(platform); 48 | assert(e == 0); 49 | 50 | e = uv_run(loop, UV_RUN_DEFAULT); 51 | assert(e == 0); 52 | } 53 | -------------------------------------------------------------------------------- /test/add-teardown-callback-remove.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool teardown_called = false; 9 | 10 | static void 11 | on_teardown(void *data) { 12 | int e; 13 | 14 | teardown_called = true; 15 | } 16 | 17 | int 18 | main() { 19 | int e; 20 | 21 | uv_loop_t *loop = uv_default_loop(); 22 | 23 | js_platform_t *platform; 24 | e = js_create_platform(loop, NULL, &platform); 25 | assert(e == 0); 26 | 27 | js_env_t *env; 28 | e = js_create_env(loop, platform, NULL, &env); 29 | assert(e == 0); 30 | 31 | e = js_add_teardown_callback(env, on_teardown, (void *) 1); 32 | assert(e == 0); 33 | 34 | e = js_remove_teardown_callback(env, on_teardown, (void *) 1); 35 | assert(e == 0); 36 | 37 | e = js_destroy_env(env); 38 | assert(e == 0); 39 | 40 | assert(teardown_called == false); 41 | 42 | e = js_destroy_platform(platform); 43 | assert(e == 0); 44 | 45 | e = uv_run(loop, UV_RUN_DEFAULT); 46 | assert(e == 0); 47 | } 48 | -------------------------------------------------------------------------------- /test/add-teardown-callback.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool teardown_called = false; 9 | 10 | static void 11 | on_teardown(void *data) { 12 | int e; 13 | 14 | teardown_called = true; 15 | } 16 | 17 | int 18 | main() { 19 | int e; 20 | 21 | uv_loop_t *loop = uv_default_loop(); 22 | 23 | js_platform_t *platform; 24 | e = js_create_platform(loop, NULL, &platform); 25 | assert(e == 0); 26 | 27 | js_env_t *env; 28 | e = js_create_env(loop, platform, NULL, &env); 29 | assert(e == 0); 30 | 31 | e = js_add_teardown_callback(env, on_teardown, NULL); 32 | assert(e == 0); 33 | 34 | e = js_destroy_env(env); 35 | assert(e == 0); 36 | 37 | assert(teardown_called); 38 | 39 | e = js_destroy_platform(platform); 40 | assert(e == 0); 41 | 42 | e = uv_run(loop, UV_RUN_DEFAULT); 43 | assert(e == 0); 44 | } 45 | -------------------------------------------------------------------------------- /test/add-type-tag-delegate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_delegate_callbacks_t callbacks = {0}; 25 | 26 | js_value_t *delegate; 27 | e = js_create_delegate(env, &callbacks, NULL, NULL, NULL, &delegate); 28 | assert(e == 0); 29 | 30 | js_type_tag_t tag = {1, 2}; 31 | e = js_add_type_tag(env, delegate, &tag); 32 | assert(e == 0); 33 | 34 | bool result; 35 | e = js_check_type_tag(env, delegate, &tag, &result); 36 | assert(e == 0); 37 | 38 | assert(result); 39 | 40 | e = js_close_handle_scope(env, scope); 41 | assert(e == 0); 42 | 43 | e = js_destroy_env(env); 44 | assert(e == 0); 45 | 46 | e = js_destroy_platform(platform); 47 | assert(e == 0); 48 | 49 | e = uv_run(loop, UV_RUN_DEFAULT); 50 | assert(e == 0); 51 | } 52 | -------------------------------------------------------------------------------- /test/add-type-tag.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *object; 25 | e = js_create_object(env, &object); 26 | assert(e == 0); 27 | 28 | js_type_tag_t tag = {1, 2}; 29 | e = js_add_type_tag(env, object, &tag); 30 | assert(e == 0); 31 | 32 | bool result; 33 | 34 | e = js_check_type_tag(env, object, &tag, &result); 35 | assert(e == 0); 36 | 37 | assert(result); 38 | 39 | e = js_check_type_tag(env, object, &(js_type_tag_t) {2, 3}, &result); 40 | assert(e == 0); 41 | 42 | assert(!result); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_destroy_env(env); 48 | assert(e == 0); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/adjust-external-memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | int64_t external_memory = 0; 21 | 22 | e = js_adjust_external_memory(env, 100, &external_memory); 23 | assert(e == 0); 24 | 25 | assert(external_memory == 100); 26 | 27 | e = js_adjust_external_memory(env, -50, &external_memory); 28 | assert(e == 0); 29 | 30 | assert(external_memory == 50); 31 | 32 | e = js_adjust_external_memory(env, -50, &external_memory); 33 | assert(e == 0); 34 | 35 | assert(external_memory == 0); 36 | 37 | e = js_destroy_env(env); 38 | assert(e == 0); 39 | 40 | e = js_destroy_platform(platform); 41 | assert(e == 0); 42 | 43 | e = uv_run(loop, UV_RUN_DEFAULT); 44 | assert(e == 0); 45 | } 46 | -------------------------------------------------------------------------------- /test/atomics-wait-timeout-notify.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | #include "fixtures/atomics-wait-timeout-notify.js.h" 9 | 10 | int 11 | main() { 12 | int e; 13 | 14 | uv_loop_t *loop = uv_default_loop(); 15 | 16 | js_platform_t *platform; 17 | e = js_create_platform(loop, NULL, &platform); 18 | assert(e == 0); 19 | 20 | js_env_t *env; 21 | e = js_create_env(loop, platform, NULL, &env); 22 | assert(e == 0); 23 | 24 | js_handle_scope_t *scope; 25 | e = js_open_handle_scope(env, &scope); 26 | assert(e == 0); 27 | 28 | js_value_t *script; 29 | e = js_create_string_utf8(env, atomics_wait_timeout_notify_js, atomics_wait_timeout_notify_js_len, &script); 30 | assert(e == 0); 31 | 32 | js_value_t *promise; 33 | e = js_run_script(env, NULL, 0, 0, script, &promise); 34 | assert(e == 0); 35 | 36 | uv_run(loop, UV_RUN_DEFAULT); 37 | 38 | js_value_t *result; 39 | e = js_get_promise_result(env, promise, &result); 40 | assert(e == 0); 41 | 42 | utf8_t value[10]; 43 | e = js_get_value_string_utf8(env, result, value, 10, NULL); 44 | assert(e == 0); 45 | 46 | assert(strcmp((char *) value, "ok") == 0); 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/atomics-wait-timeout.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | #include "fixtures/atomics-wait-timeout.js.h" 9 | 10 | int 11 | main() { 12 | int e; 13 | 14 | uv_loop_t *loop = uv_default_loop(); 15 | 16 | js_platform_t *platform; 17 | e = js_create_platform(loop, NULL, &platform); 18 | assert(e == 0); 19 | 20 | js_env_t *env; 21 | e = js_create_env(loop, platform, NULL, &env); 22 | assert(e == 0); 23 | 24 | js_handle_scope_t *scope; 25 | e = js_open_handle_scope(env, &scope); 26 | assert(e == 0); 27 | 28 | js_value_t *script; 29 | e = js_create_string_utf8(env, atomics_wait_timeout_js, atomics_wait_timeout_js_len, &script); 30 | assert(e == 0); 31 | 32 | js_value_t *promise; 33 | e = js_run_script(env, NULL, 0, 0, script, &promise); 34 | assert(e == 0); 35 | 36 | uv_run(loop, UV_RUN_DEFAULT); 37 | 38 | js_value_t *result; 39 | e = js_get_promise_result(env, promise, &result); 40 | assert(e == 0); 41 | 42 | utf8_t value[10]; 43 | e = js_get_value_string_utf8(env, result, value, 10, NULL); 44 | assert(e == 0); 45 | 46 | assert(strcmp((char *) value, "timed-out") == 0); 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/call-function-ignore-return.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "(x) => x + 42", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *fn; 30 | e = js_run_script(env, NULL, 0, 0, script, &fn); 31 | assert(e == 0); 32 | 33 | js_value_t *global; 34 | e = js_get_global(env, &global); 35 | assert(e == 0); 36 | 37 | js_value_t *args[1]; 38 | e = js_create_uint32(env, 42, &args[0]); 39 | assert(e == 0); 40 | 41 | e = js_call_function(env, global, fn, 1, args, NULL); 42 | assert(e == 0); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_destroy_env(env); 48 | assert(e == 0); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/call-function-in-context.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_context_t *context; 26 | e = js_create_context(env, &context); 27 | assert(e == 0); 28 | 29 | e = js_enter_context(env, context); 30 | assert(e == 0); 31 | 32 | js_value_t *script; 33 | e = js_create_string_utf8(env, (utf8_t *) "() => globalThis", -1, &script); 34 | assert(e == 0); 35 | 36 | js_value_t *fn; 37 | e = js_run_script(env, NULL, 0, 0, script, &fn); 38 | assert(e == 0); 39 | 40 | js_value_t *global; 41 | e = js_get_global(env, &global); 42 | assert(e == 0); 43 | 44 | js_value_t *result; 45 | e = js_call_function(env, global, fn, 0, NULL, &result); 46 | assert(e == 0); 47 | 48 | { 49 | js_value_t *global; 50 | e = js_get_global(env, &global); 51 | assert(e == 0); 52 | 53 | bool equals; 54 | e = js_strict_equals(env, result, global, &equals); 55 | assert(e == 0); 56 | 57 | assert(equals); 58 | } 59 | 60 | e = js_exit_context(env, context); 61 | assert(e == 0); 62 | 63 | { 64 | js_value_t *global; 65 | e = js_get_global(env, &global); 66 | assert(e == 0); 67 | 68 | bool equals; 69 | e = js_strict_equals(env, result, global, &equals); 70 | assert(e == 0); 71 | 72 | assert(equals == false); 73 | } 74 | 75 | e = js_destroy_context(env, context); 76 | assert(e == 0); 77 | 78 | e = js_close_handle_scope(env, scope); 79 | assert(e == 0); 80 | 81 | e = js_destroy_env(env); 82 | assert(e == 0); 83 | 84 | e = js_destroy_platform(platform); 85 | assert(e == 0); 86 | 87 | e = uv_run(loop, UV_RUN_DEFAULT); 88 | assert(e == 0); 89 | } 90 | -------------------------------------------------------------------------------- /test/call-function-microtask.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *global; 26 | e = js_get_global(env, &global); 27 | assert(e == 0); 28 | 29 | { 30 | js_value_t *property; 31 | e = js_create_uint32(env, 42, &property); 32 | assert(e == 0); 33 | 34 | e = js_set_named_property(env, global, "value", property); 35 | assert(e == 0); 36 | } 37 | 38 | js_value_t *script; 39 | e = js_create_string_utf8(env, (utf8_t *) "() => { Promise.resolve().then(() => value *= 2); return value }", -1, &script); 40 | assert(e == 0); 41 | 42 | js_value_t *fn; 43 | e = js_run_script(env, NULL, 0, 0, script, &fn); 44 | assert(e == 0); 45 | 46 | js_value_t *result; 47 | e = js_call_function(env, global, fn, 0, NULL, &result); 48 | assert(e == 0); 49 | 50 | uint32_t value; 51 | e = js_get_value_uint32(env, result, &value); 52 | assert(e == 0); 53 | 54 | assert(value == 42); 55 | 56 | // The call to `js_call_function()` happened without JavaScript already 57 | // executing on the stack and so a microtask checkpoint was performed before 58 | // returning. 59 | 60 | { 61 | js_value_t *property; 62 | e = js_get_named_property(env, global, "value", &property); 63 | assert(e == 0); 64 | 65 | uint32_t value; 66 | e = js_get_value_uint32(env, property, &value); 67 | assert(e == 0); 68 | 69 | assert(value == 84); 70 | } 71 | 72 | e = js_close_handle_scope(env, scope); 73 | assert(e == 0); 74 | 75 | e = js_destroy_env(env); 76 | assert(e == 0); 77 | 78 | e = js_destroy_platform(platform); 79 | assert(e == 0); 80 | 81 | e = uv_run(loop, UV_RUN_DEFAULT); 82 | assert(e == 0); 83 | } 84 | -------------------------------------------------------------------------------- /test/call-function-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "../include/js.h" 8 | 9 | int uncaught_called = 0; 10 | 11 | static void 12 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 13 | int e; 14 | 15 | uncaught_called++; 16 | 17 | bool has_exception; 18 | e = js_is_exception_pending(env, &has_exception); 19 | assert(e == 0); 20 | 21 | assert(!has_exception); 22 | 23 | utf8_t value[4]; 24 | e = js_get_value_string_utf8(env, error, value, 4, NULL); 25 | assert(e == 0); 26 | 27 | assert(strcmp((char *) value, "err") == 0); 28 | } 29 | 30 | int 31 | main() { 32 | int e; 33 | 34 | uv_loop_t *loop = uv_default_loop(); 35 | 36 | js_platform_t *platform; 37 | e = js_create_platform(loop, NULL, &platform); 38 | assert(e == 0); 39 | 40 | js_env_t *env; 41 | e = js_create_env(loop, platform, NULL, &env); 42 | assert(e == 0); 43 | 44 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 45 | assert(e == 0); 46 | 47 | js_handle_scope_t *scope; 48 | e = js_open_handle_scope(env, &scope); 49 | assert(e == 0); 50 | 51 | js_value_t *script; 52 | e = js_create_string_utf8(env, (utf8_t *) "() => { throw 'err' }", -1, &script); 53 | assert(e == 0); 54 | 55 | js_value_t *fn; 56 | e = js_run_script(env, NULL, 0, 0, script, &fn); 57 | assert(e == 0); 58 | 59 | js_value_t *global; 60 | e = js_get_global(env, &global); 61 | assert(e == 0); 62 | 63 | js_value_t *result; 64 | e = js_call_function(env, global, fn, 0, NULL, &result); 65 | assert(e == js_uncaught_exception); 66 | 67 | assert(uncaught_called == 1); 68 | 69 | e = js_close_handle_scope(env, scope); 70 | assert(e == 0); 71 | 72 | e = js_destroy_env(env); 73 | assert(e == 0); 74 | 75 | e = js_destroy_platform(platform); 76 | assert(e == 0); 77 | 78 | e = uv_run(loop, UV_RUN_DEFAULT); 79 | assert(e == 0); 80 | } 81 | -------------------------------------------------------------------------------- /test/call-function.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "(x) => x + 42", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *fn; 30 | e = js_run_script(env, NULL, 0, 0, script, &fn); 31 | assert(e == 0); 32 | 33 | js_value_t *global; 34 | e = js_get_global(env, &global); 35 | assert(e == 0); 36 | 37 | js_value_t *args[1]; 38 | e = js_create_uint32(env, 42, &args[0]); 39 | assert(e == 0); 40 | 41 | js_value_t *result; 42 | e = js_call_function(env, global, fn, 1, args, &result); 43 | assert(e == 0); 44 | 45 | uint32_t value; 46 | e = js_get_value_uint32(env, result, &value); 47 | assert(e == 0); 48 | 49 | assert(value == 84); 50 | 51 | e = js_close_handle_scope(env, scope); 52 | assert(e == 0); 53 | 54 | e = js_destroy_env(env); 55 | assert(e == 0); 56 | 57 | e = js_destroy_platform(platform); 58 | assert(e == 0); 59 | 60 | e = uv_run(loop, UV_RUN_DEFAULT); 61 | assert(e == 0); 62 | } 63 | -------------------------------------------------------------------------------- /test/call-threadsafe-function-after-release.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | bool fn_called = false; 8 | 9 | js_value_t * 10 | on_call(js_env_t *env, js_callback_info_t *info) { 11 | int e; 12 | 13 | fn_called = true; 14 | 15 | return NULL; 16 | } 17 | 18 | int 19 | main() { 20 | int e; 21 | 22 | uv_loop_t *loop = uv_default_loop(); 23 | 24 | js_platform_t *platform; 25 | e = js_create_platform(loop, NULL, &platform); 26 | assert(e == 0); 27 | 28 | js_env_t *env; 29 | e = js_create_env(loop, platform, NULL, &env); 30 | assert(e == 0); 31 | 32 | js_handle_scope_t *scope; 33 | e = js_open_handle_scope(env, &scope); 34 | assert(e == 0); 35 | 36 | js_value_t *fn; 37 | e = js_create_function(env, "hello", -1, on_call, NULL, &fn); 38 | assert(e == 0); 39 | 40 | js_threadsafe_function_t *tsfn; 41 | e = js_create_threadsafe_function(env, fn, 0, 1, NULL, NULL, NULL, NULL, &tsfn); 42 | assert(e == 0); 43 | 44 | e = js_call_threadsafe_function(tsfn, NULL, js_threadsafe_function_nonblocking); 45 | assert(e == 0); 46 | 47 | e = js_release_threadsafe_function(tsfn, js_threadsafe_function_release); 48 | assert(e == 0); 49 | 50 | e = js_call_threadsafe_function(tsfn, NULL, js_threadsafe_function_nonblocking); 51 | assert(e != 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | 56 | assert(fn_called); 57 | 58 | e = js_close_handle_scope(env, scope); 59 | assert(e == 0); 60 | 61 | e = js_destroy_env(env); 62 | assert(e == 0); 63 | 64 | e = js_destroy_platform(platform); 65 | assert(e == 0); 66 | 67 | e = uv_run(loop, UV_RUN_DEFAULT); 68 | assert(e == 0); 69 | } 70 | -------------------------------------------------------------------------------- /test/call-threadsafe-function-with-data.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | bool fn_called = false; 8 | js_threadsafe_function_t *tsfn; 9 | 10 | void 11 | on_call(js_env_t *env, js_value_t *function, void *context, void *data) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | assert(data == (void *) 42); 17 | 18 | e = js_release_threadsafe_function(tsfn, js_threadsafe_function_release); 19 | assert(e == 0); 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | e = js_create_threadsafe_function(env, NULL, 0, 1, NULL, NULL, NULL, on_call, &tsfn); 41 | assert(e == 0); 42 | 43 | e = js_call_threadsafe_function(tsfn, (void *) 42, js_threadsafe_function_nonblocking); 44 | assert(e == 0); 45 | 46 | e = uv_run(loop, UV_RUN_DEFAULT); 47 | assert(e == 0); 48 | 49 | assert(fn_called); 50 | 51 | e = js_close_handle_scope(env, scope); 52 | assert(e == 0); 53 | 54 | e = js_destroy_env(env); 55 | assert(e == 0); 56 | 57 | e = js_destroy_platform(platform); 58 | assert(e == 0); 59 | 60 | e = uv_run(loop, UV_RUN_DEFAULT); 61 | assert(e == 0); 62 | } 63 | -------------------------------------------------------------------------------- /test/coerce-number-to-string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *number; 27 | e = js_create_uint32(env, 42, &number); 28 | assert(e == 0); 29 | 30 | js_value_t *string; 31 | e = js_coerce_to_string(env, number, &string); 32 | assert(e == 0); 33 | 34 | uint8_t value[3]; 35 | e = js_get_value_string_utf8(env, string, value, 3, NULL); 36 | assert(e == 0); 37 | 38 | assert(strcmp((char *) value, "42") == 0); 39 | 40 | e = js_close_handle_scope(env, scope); 41 | assert(e == 0); 42 | 43 | e = js_destroy_env(env); 44 | assert(e == 0); 45 | 46 | e = js_destroy_platform(platform); 47 | assert(e == 0); 48 | 49 | e = uv_run(loop, UV_RUN_DEFAULT); 50 | assert(e == 0); 51 | } 52 | -------------------------------------------------------------------------------- /test/coerce-object-to-string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *object; 27 | e = js_create_object(env, &object); 28 | assert(e == 0); 29 | 30 | js_value_t *string; 31 | e = js_coerce_to_string(env, object, &string); 32 | assert(e == 0); 33 | 34 | uint8_t value[16]; 35 | e = js_get_value_string_utf8(env, string, value, 16, NULL); 36 | assert(e == 0); 37 | 38 | assert(strcmp((char *) value, "[object Object]") == 0); 39 | 40 | e = js_close_handle_scope(env, scope); 41 | assert(e == 0); 42 | 43 | e = js_destroy_env(env); 44 | assert(e == 0); 45 | 46 | e = js_destroy_platform(platform); 47 | assert(e == 0); 48 | 49 | e = uv_run(loop, UV_RUN_DEFAULT); 50 | assert(e == 0); 51 | } 52 | -------------------------------------------------------------------------------- /test/coerce-string-to-number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *string; 26 | e = js_create_string_utf8(env, (utf8_t *) "42", -1, &string); 27 | assert(e == 0); 28 | 29 | js_value_t *number; 30 | e = js_coerce_to_number(env, string, &number); 31 | assert(e == 0); 32 | 33 | uint32_t value; 34 | e = js_get_value_uint32(env, number, &value); 35 | assert(e == 0); 36 | 37 | assert(value == 42); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/coerce-symbol-to-string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | #include "helpers.h" 8 | 9 | int 10 | main() { 11 | int e; 12 | 13 | uv_loop_t *loop = uv_default_loop(); 14 | 15 | js_platform_t *platform; 16 | e = js_create_platform(loop, NULL, &platform); 17 | assert(e == 0); 18 | 19 | js_env_t *env; 20 | e = js_create_env(loop, platform, NULL, &env); 21 | assert(e == 0); 22 | 23 | js_handle_scope_t *scope; 24 | e = js_open_handle_scope(env, &scope); 25 | assert(e == 0); 26 | 27 | js_value_t *description; 28 | e = js_create_string_utf8(env, (utf8_t *) "symbol", -1, &description); 29 | assert(e == 0); 30 | 31 | js_value_t *symbol; 32 | e = js_create_symbol(env, description, &symbol); 33 | assert(e == 0); 34 | 35 | js_value_t *string; 36 | e = js_coerce_to_string(env, symbol, &string); 37 | assert(e != 0); 38 | 39 | js_print_pending_exception(env); 40 | 41 | e = js_close_handle_scope(env, scope); 42 | assert(e == 0); 43 | 44 | e = js_destroy_env(env); 45 | assert(e == 0); 46 | 47 | e = js_destroy_platform(platform); 48 | assert(e == 0); 49 | 50 | e = uv_run(loop, UV_RUN_DEFAULT); 51 | assert(e == 0); 52 | } 53 | -------------------------------------------------------------------------------- /test/create-arraybuffer-too-large.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | #include "helpers.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *arraybuffer; 27 | e = js_create_arraybuffer(env, 281474976710656, NULL, &arraybuffer); 28 | assert(e != 0); 29 | 30 | js_print_pending_exception(env); 31 | 32 | e = js_close_handle_scope(env, scope); 33 | assert(e == 0); 34 | 35 | e = js_destroy_env(env); 36 | assert(e == 0); 37 | 38 | e = js_destroy_platform(platform); 39 | assert(e == 0); 40 | 41 | e = uv_run(loop, UV_RUN_DEFAULT); 42 | assert(e == 0); 43 | } 44 | -------------------------------------------------------------------------------- /test/create-arraybuffer-with-backing-store.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | uint8_t *data_a, *data_b; 27 | 28 | js_arraybuffer_backing_store_t *backing_store; 29 | 30 | { 31 | js_value_t *arraybuffer; 32 | e = js_create_arraybuffer(env, 1, (void **) &data_a, &arraybuffer); 33 | assert(e == 0); 34 | 35 | e = js_get_arraybuffer_backing_store(env, arraybuffer, &backing_store); 36 | assert(e == 0); 37 | } 38 | 39 | { 40 | js_value_t *arraybuffer; 41 | e = js_create_arraybuffer_with_backing_store(env, backing_store, (void **) &data_b, NULL, &arraybuffer); 42 | assert(e == 0); 43 | } 44 | 45 | data_a[0] = 42; 46 | 47 | assert(data_b[0] == 42); 48 | 49 | e = js_release_arraybuffer_backing_store(env, backing_store); 50 | assert(e == 0); 51 | 52 | e = js_close_handle_scope(env, scope); 53 | assert(e == 0); 54 | 55 | e = js_destroy_env(env); 56 | assert(e == 0); 57 | 58 | e = js_destroy_platform(platform); 59 | assert(e == 0); 60 | 61 | e = uv_run(loop, UV_RUN_DEFAULT); 62 | assert(e == 0); 63 | } 64 | -------------------------------------------------------------------------------- /test/create-arraybuffer.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *arraybuffer; 27 | e = js_create_arraybuffer(env, 4096, NULL, &arraybuffer); 28 | assert(e == 0); 29 | 30 | e = js_close_handle_scope(env, scope); 31 | assert(e == 0); 32 | 33 | e = js_destroy_env(env); 34 | assert(e == 0); 35 | 36 | e = js_destroy_platform(platform); 37 | assert(e == 0); 38 | 39 | e = uv_run(loop, UV_RUN_DEFAULT); 40 | assert(e == 0); 41 | } 42 | -------------------------------------------------------------------------------- /test/create-date-nan.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *date; 27 | e = js_create_date(env, NAN, &date); 28 | assert(e == 0); 29 | 30 | double value; 31 | e = js_get_value_date(env, date, &value); 32 | assert(e == 0); 33 | 34 | assert(value != value); 35 | 36 | e = js_close_handle_scope(env, scope); 37 | assert(e == 0); 38 | 39 | e = js_destroy_env(env); 40 | assert(e == 0); 41 | 42 | e = js_destroy_platform(platform); 43 | assert(e == 0); 44 | 45 | e = uv_run(loop, UV_RUN_DEFAULT); 46 | assert(e == 0); 47 | } 48 | -------------------------------------------------------------------------------- /test/create-date-overflow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *date; 26 | e = js_create_date(env, 8.64e15 + 1, &date); 27 | assert(e == 0); 28 | 29 | double value; 30 | e = js_get_value_date(env, date, &value); 31 | assert(e == 0); 32 | 33 | assert(value != value); 34 | 35 | e = js_close_handle_scope(env, scope); 36 | assert(e == 0); 37 | 38 | e = js_destroy_env(env); 39 | assert(e == 0); 40 | 41 | e = js_destroy_platform(platform); 42 | assert(e == 0); 43 | 44 | e = uv_run(loop, UV_RUN_DEFAULT); 45 | assert(e == 0); 46 | } 47 | -------------------------------------------------------------------------------- /test/create-date.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *date; 26 | e = js_create_date(env, 12345, &date); 27 | assert(e == 0); 28 | 29 | double value; 30 | e = js_get_value_date(env, date, &value); 31 | assert(e == 0); 32 | 33 | assert(value == 12345); 34 | 35 | e = js_close_handle_scope(env, scope); 36 | assert(e == 0); 37 | 38 | e = js_destroy_env(env); 39 | assert(e == 0); 40 | 41 | e = js_destroy_platform(platform); 42 | assert(e == 0); 43 | 44 | e = uv_run(loop, UV_RUN_DEFAULT); 45 | assert(e == 0); 46 | } 47 | -------------------------------------------------------------------------------- /test/create-delegate-delete-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int delete_called = 0; 9 | 10 | bool 11 | delete_property(js_env_t *env, js_value_t *property, void *data) { 12 | int e; 13 | 14 | delete_called++; 15 | 16 | e = js_throw_error(env, NULL, "nope"); 17 | assert(e == 0); 18 | 19 | return false; 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_delegate_callbacks_t callbacks = { 41 | .delete_property = delete_property, 42 | }; 43 | 44 | js_value_t *delegate; 45 | e = js_create_delegate(env, &callbacks, NULL, NULL, NULL, &delegate); 46 | assert(e == 0); 47 | 48 | bool result; 49 | e = js_delete_named_property(env, delegate, "foo", &result); 50 | (void) e; 51 | 52 | assert(delete_called && !result); 53 | 54 | e = js_close_handle_scope(env, scope); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | } 66 | -------------------------------------------------------------------------------- /test/create-delegate-get-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int get_called = 0; 9 | 10 | js_value_t * 11 | get(js_env_t *env, js_value_t *property, void *data) { 12 | int e; 13 | 14 | get_called++; 15 | 16 | e = js_throw_error(env, NULL, "nope"); 17 | assert(e == 0); 18 | 19 | return NULL; 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_delegate_callbacks_t callbacks = { 41 | .get = get, 42 | }; 43 | 44 | js_value_t *delegate; 45 | e = js_create_delegate(env, &callbacks, NULL, NULL, NULL, &delegate); 46 | assert(e == 0); 47 | 48 | js_value_t *value; 49 | e = js_get_named_property(env, delegate, "foo", &value); 50 | assert(e != 0); 51 | 52 | assert(get_called); 53 | 54 | bool has_exception; 55 | e = js_is_exception_pending(env, &has_exception); 56 | assert(has_exception); 57 | 58 | e = js_close_handle_scope(env, scope); 59 | assert(e == 0); 60 | 61 | e = js_destroy_env(env); 62 | assert(e == 0); 63 | 64 | e = js_destroy_platform(platform); 65 | assert(e == 0); 66 | 67 | e = uv_run(loop, UV_RUN_DEFAULT); 68 | assert(e == 0); 69 | } 70 | -------------------------------------------------------------------------------- /test/create-delegate-has-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int has_called = 0; 9 | 10 | bool 11 | has(js_env_t *env, js_value_t *property, void *data) { 12 | int e; 13 | 14 | has_called++; 15 | 16 | e = js_throw_error(env, NULL, "nope"); 17 | assert(e == 0); 18 | 19 | return false; 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_delegate_callbacks_t callbacks = { 41 | .has = has, 42 | }; 43 | 44 | js_value_t *delegate; 45 | e = js_create_delegate(env, &callbacks, NULL, NULL, NULL, &delegate); 46 | assert(e == 0); 47 | 48 | bool result; 49 | e = js_has_named_property(env, delegate, "foo", &result); 50 | assert(e != 0); 51 | 52 | assert(has_called); 53 | 54 | bool has_exception; 55 | e = js_is_exception_pending(env, &has_exception); 56 | assert(has_exception); 57 | 58 | e = js_close_handle_scope(env, scope); 59 | assert(e == 0); 60 | 61 | e = js_destroy_env(env); 62 | assert(e == 0); 63 | 64 | e = js_destroy_platform(platform); 65 | assert(e == 0); 66 | 67 | e = uv_run(loop, UV_RUN_DEFAULT); 68 | assert(e == 0); 69 | } 70 | -------------------------------------------------------------------------------- /test/create-delegate-keys-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int keys_called = 0; 9 | 10 | js_value_t * 11 | own_keys(js_env_t *env, void *data) { 12 | int e; 13 | 14 | keys_called++; 15 | 16 | e = js_throw_error(env, NULL, "nope"); 17 | assert(e == 0); 18 | 19 | return NULL; 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_delegate_callbacks_t callbacks = { 41 | .own_keys = own_keys, 42 | }; 43 | 44 | js_value_t *delegate; 45 | e = js_create_delegate(env, &callbacks, NULL, NULL, NULL, &delegate); 46 | assert(e == 0); 47 | 48 | js_value_t *value; 49 | e = js_get_property_names(env, delegate, &value); 50 | assert(e != 0); 51 | 52 | assert(keys_called); 53 | 54 | bool has_exception; 55 | e = js_is_exception_pending(env, &has_exception); 56 | assert(has_exception); 57 | 58 | e = js_close_handle_scope(env, scope); 59 | assert(e == 0); 60 | 61 | e = js_destroy_env(env); 62 | assert(e == 0); 63 | 64 | e = js_destroy_platform(platform); 65 | assert(e == 0); 66 | 67 | e = uv_run(loop, UV_RUN_DEFAULT); 68 | assert(e == 0); 69 | } 70 | -------------------------------------------------------------------------------- /test/create-delegate-set-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int set_called = 0; 9 | 10 | bool 11 | set(js_env_t *env, js_value_t *property, js_value_t *value, void *data) { 12 | int e; 13 | 14 | set_called++; 15 | 16 | e = js_throw_error(env, NULL, "nope"); 17 | assert(e == 0); 18 | 19 | return false; 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_delegate_callbacks_t callbacks = { 41 | .set = set, 42 | }; 43 | 44 | js_value_t *delegate; 45 | e = js_create_delegate(env, &callbacks, NULL, NULL, NULL, &delegate); 46 | assert(e == 0); 47 | 48 | js_value_t *value; 49 | e = js_create_uint32(env, 42, &value); 50 | assert(e == 0); 51 | 52 | e = js_set_named_property(env, delegate, "foo", value); 53 | assert(e != 0); 54 | 55 | assert(set_called); 56 | 57 | bool has_exception; 58 | e = js_is_exception_pending(env, &has_exception); 59 | assert(has_exception); 60 | 61 | e = js_close_handle_scope(env, scope); 62 | assert(e == 0); 63 | 64 | e = js_destroy_env(env); 65 | assert(e == 0); 66 | 67 | e = js_destroy_platform(platform); 68 | assert(e == 0); 69 | 70 | e = uv_run(loop, UV_RUN_DEFAULT); 71 | assert(e == 0); 72 | } 73 | -------------------------------------------------------------------------------- /test/create-env-multiple.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | { 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | e = js_destroy_env(env); 22 | assert(e == 0); 23 | 24 | e = uv_run(loop, UV_RUN_DEFAULT); 25 | assert(e == 0); 26 | } 27 | { 28 | js_env_t *env; 29 | e = js_create_env(loop, platform, NULL, &env); 30 | assert(e == 0); 31 | 32 | e = js_destroy_env(env); 33 | assert(e == 0); 34 | 35 | e = uv_run(loop, UV_RUN_DEFAULT); 36 | assert(e == 0); 37 | } 38 | 39 | e = js_destroy_platform(platform); 40 | assert(e == 0); 41 | 42 | e = uv_run(loop, UV_RUN_DEFAULT); 43 | assert(e == 0); 44 | } 45 | -------------------------------------------------------------------------------- /test/create-env.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | e = js_destroy_env(env); 21 | assert(e == 0); 22 | 23 | e = uv_run(loop, UV_RUN_DEFAULT); 24 | assert(e == 0); 25 | 26 | e = js_destroy_platform(platform); 27 | assert(e == 0); 28 | 29 | e = uv_run(loop, UV_RUN_DEFAULT); 30 | assert(e == 0); 31 | } 32 | -------------------------------------------------------------------------------- /test/create-external-arraybuffer-with-finalizer-detach.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static bool finalize_called = false; 8 | 9 | static void 10 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 11 | finalize_called = true; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_options_t options = { 21 | .expose_garbage_collection = true, 22 | }; 23 | 24 | js_platform_t *platform; 25 | e = js_create_platform(loop, &options, &platform); 26 | assert(e == 0); 27 | 28 | js_env_t *env; 29 | e = js_create_env(loop, platform, NULL, &env); 30 | assert(e == 0); 31 | 32 | js_handle_scope_t *scope; 33 | e = js_open_handle_scope(env, &scope); 34 | assert(e == 0); 35 | 36 | uint8_t data[] = {1, 2, 3, 4}; 37 | 38 | js_value_t *arraybuffer; 39 | e = js_create_external_arraybuffer(env, data, 4, on_finalize, NULL, &arraybuffer); 40 | assert(e == 0); 41 | 42 | e = js_detach_arraybuffer(env, arraybuffer); 43 | assert(e == 0); 44 | 45 | assert(finalize_called); 46 | 47 | e = js_close_handle_scope(env, scope); 48 | assert(e == 0); 49 | 50 | e = js_request_garbage_collection(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_env(env); 54 | assert(e == 0); 55 | 56 | e = js_destroy_platform(platform); 57 | assert(e == 0); 58 | 59 | e = uv_run(loop, UV_RUN_DEFAULT); 60 | assert(e == 0); 61 | } 62 | -------------------------------------------------------------------------------- /test/create-external-arraybuffer-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static bool finalize_called = false; 8 | 9 | static void 10 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 11 | finalize_called = true; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_options_t options = { 21 | .expose_garbage_collection = true, 22 | }; 23 | 24 | js_platform_t *platform; 25 | e = js_create_platform(loop, &options, &platform); 26 | assert(e == 0); 27 | 28 | js_env_t *env; 29 | e = js_create_env(loop, platform, NULL, &env); 30 | assert(e == 0); 31 | 32 | js_handle_scope_t *scope; 33 | e = js_open_handle_scope(env, &scope); 34 | assert(e == 0); 35 | 36 | uint8_t data[] = {1, 2, 3, 4}; 37 | 38 | js_value_t *arraybuffer; 39 | e = js_create_external_arraybuffer(env, data, 4, on_finalize, NULL, &arraybuffer); 40 | assert(e == 0); 41 | 42 | e = js_close_handle_scope(env, scope); 43 | assert(e == 0); 44 | 45 | e = js_request_garbage_collection(env); 46 | assert(e == 0); 47 | 48 | e = js_destroy_env(env); 49 | assert(e == 0); 50 | 51 | e = js_destroy_platform(platform); 52 | assert(e == 0); 53 | 54 | e = uv_run(loop, UV_RUN_DEFAULT); 55 | assert(e == 0); 56 | 57 | assert(finalize_called); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-external-arraybuffer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | uint8_t data[] = {1, 2, 3, 4}; 26 | 27 | js_value_t *arraybuffer; 28 | e = js_create_external_arraybuffer(env, data, 4, NULL, NULL, &arraybuffer); 29 | 30 | if (e == 0) { 31 | js_value_t *global; 32 | e = js_get_global(env, &global); 33 | assert(e == 0); 34 | 35 | e = js_set_named_property(env, global, "arraybuffer", arraybuffer); 36 | assert(e == 0); 37 | 38 | js_value_t *script; 39 | e = js_create_string_utf8(env, (utf8_t *) "const view = new Uint8Array(arraybuffer); view[0] = 42", -1, &script); 40 | assert(e == 0); 41 | 42 | e = js_run_script(env, NULL, 0, 0, script, NULL); 43 | assert(e == 0); 44 | 45 | assert(data[0] == 42); 46 | } 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/create-external-string-latin1-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static bool finalize_called = false; 8 | 9 | static void 10 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 11 | finalize_called = true; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_t *platform; 21 | e = js_create_platform(loop, NULL, &platform); 22 | assert(e == 0); 23 | 24 | js_env_t *env; 25 | e = js_create_env(loop, platform, NULL, &env); 26 | assert(e == 0); 27 | 28 | js_handle_scope_t *scope; 29 | e = js_open_handle_scope(env, &scope); 30 | assert(e == 0); 31 | 32 | latin1_t data[] = {'f', 'o', 'o', 0x0}; 33 | bool copied; 34 | 35 | js_value_t *string; 36 | e = js_create_external_string_latin1(env, data, 3, on_finalize, NULL, &string, &copied); 37 | assert(e == 0); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = uv_run(loop, UV_RUN_DEFAULT); 46 | assert(e == 0); 47 | 48 | assert(finalize_called); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/create-external-string-latin1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | latin1_t data[] = {'f', 'o', 'o', 0x0}; 26 | bool copied; 27 | 28 | js_value_t *string; 29 | e = js_create_external_string_latin1(env, data, 3, NULL, NULL, &string, &copied); 30 | assert(e == 0); 31 | 32 | e = js_close_handle_scope(env, scope); 33 | assert(e == 0); 34 | 35 | e = js_destroy_env(env); 36 | assert(e == 0); 37 | 38 | e = js_destroy_platform(platform); 39 | assert(e == 0); 40 | 41 | e = uv_run(loop, UV_RUN_DEFAULT); 42 | assert(e == 0); 43 | } 44 | -------------------------------------------------------------------------------- /test/create-external-string-utf16le-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static bool finalize_called = false; 8 | 9 | static void 10 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 11 | finalize_called = true; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_t *platform; 21 | e = js_create_platform(loop, NULL, &platform); 22 | assert(e == 0); 23 | 24 | js_env_t *env; 25 | e = js_create_env(loop, platform, NULL, &env); 26 | assert(e == 0); 27 | 28 | js_handle_scope_t *scope; 29 | e = js_open_handle_scope(env, &scope); 30 | assert(e == 0); 31 | 32 | utf16_t data[] = {'f', 'o', 'o', 0x0}; 33 | bool copied; 34 | 35 | js_value_t *string; 36 | e = js_create_external_string_utf16le(env, data, 3, on_finalize, NULL, &string, &copied); 37 | assert(e == 0); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = uv_run(loop, UV_RUN_DEFAULT); 46 | assert(e == 0); 47 | 48 | assert(finalize_called); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/create-external-string-utf16le.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | utf16_t data[] = {'f', 'o', 'o', 0x0}; 26 | bool copied; 27 | 28 | js_value_t *string; 29 | e = js_create_external_string_utf16le(env, data, 3, NULL, NULL, &string, &copied); 30 | assert(e == 0); 31 | 32 | e = js_close_handle_scope(env, scope); 33 | assert(e == 0); 34 | 35 | e = js_destroy_env(env); 36 | assert(e == 0); 37 | 38 | e = js_destroy_platform(platform); 39 | assert(e == 0); 40 | 41 | e = uv_run(loop, UV_RUN_DEFAULT); 42 | assert(e == 0); 43 | } 44 | -------------------------------------------------------------------------------- /test/create-external-string-utf8-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static bool finalize_called = false; 8 | 9 | static void 10 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 11 | finalize_called = true; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_t *platform; 21 | e = js_create_platform(loop, NULL, &platform); 22 | assert(e == 0); 23 | 24 | js_env_t *env; 25 | e = js_create_env(loop, platform, NULL, &env); 26 | assert(e == 0); 27 | 28 | js_handle_scope_t *scope; 29 | e = js_open_handle_scope(env, &scope); 30 | assert(e == 0); 31 | 32 | utf8_t data[] = {'f', 'o', 'o', 0x0}; 33 | bool copied; 34 | 35 | js_value_t *string; 36 | e = js_create_external_string_utf8(env, data, 3, on_finalize, NULL, &string, &copied); 37 | assert(e == 0); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = uv_run(loop, UV_RUN_DEFAULT); 46 | assert(e == 0); 47 | 48 | assert(finalize_called); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/create-external-string-utf8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | utf8_t data[] = {'f', 'o', 'o', 0x0}; 26 | bool copied; 27 | 28 | js_value_t *string; 29 | e = js_create_external_string_utf8(env, data, 3, NULL, NULL, &string, &copied); 30 | assert(e == 0); 31 | 32 | e = js_close_handle_scope(env, scope); 33 | assert(e == 0); 34 | 35 | e = js_destroy_env(env); 36 | assert(e == 0); 37 | 38 | e = js_destroy_platform(platform); 39 | assert(e == 0); 40 | 41 | e = uv_run(loop, UV_RUN_DEFAULT); 42 | assert(e == 0); 43 | } 44 | -------------------------------------------------------------------------------- /test/create-external-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | static bool finalize_called = false; 7 | 8 | static void 9 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 10 | finalize_called = true; 11 | 12 | assert((intptr_t) data == 42); 13 | } 14 | 15 | int 16 | main() { 17 | int e; 18 | 19 | uv_loop_t *loop = uv_default_loop(); 20 | 21 | js_platform_options_t options = { 22 | .expose_garbage_collection = true, 23 | .trace_garbage_collection = true, 24 | }; 25 | 26 | js_platform_t *platform; 27 | e = js_create_platform(loop, &options, &platform); 28 | assert(e == 0); 29 | 30 | js_env_t *env; 31 | e = js_create_env(loop, platform, NULL, &env); 32 | assert(e == 0); 33 | 34 | js_handle_scope_t *scope; 35 | e = js_open_handle_scope(env, &scope); 36 | assert(e == 0); 37 | 38 | js_value_t *external; 39 | e = js_create_external(env, (void *) 42, on_finalize, NULL, &external); 40 | assert(e == 0); 41 | 42 | e = js_close_handle_scope(env, scope); 43 | assert(e == 0); 44 | 45 | e = js_request_garbage_collection(env); 46 | assert(e == 0); 47 | 48 | e = js_destroy_env(env); 49 | assert(e == 0); 50 | 51 | e = js_destroy_platform(platform); 52 | assert(e == 0); 53 | 54 | e = uv_run(loop, UV_RUN_DEFAULT); 55 | assert(e == 0); 56 | 57 | assert(finalize_called); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-external.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *external; 25 | e = js_create_external(env, (void *) 42, NULL, NULL, &external); 26 | assert(e == 0); 27 | 28 | intptr_t data; 29 | e = js_get_value_external(env, external, (void **) &data); 30 | assert(e == 0); 31 | 32 | assert(data == 42); 33 | 34 | e = js_close_handle_scope(env, scope); 35 | assert(e == 0); 36 | 37 | e = js_destroy_env(env); 38 | assert(e == 0); 39 | 40 | e = js_destroy_platform(platform); 41 | assert(e == 0); 42 | 43 | e = uv_run(loop, UV_RUN_DEFAULT); 44 | assert(e == 0); 45 | } 46 | -------------------------------------------------------------------------------- /test/create-function-args-extra.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | bool fn_called = false; 9 | 10 | js_value_t * 11 | on_call(js_env_t *env, js_callback_info_t *info) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | js_value_t *argv[1]; 17 | size_t argc = 1; 18 | 19 | e = js_get_callback_info(env, info, &argc, argv, NULL, NULL); 20 | assert(e == 0); 21 | 22 | assert(argc == 2); 23 | 24 | uint32_t value; 25 | e = js_get_value_uint32(env, argv[0], &value); 26 | assert(e == 0); 27 | 28 | assert(value == 1); 29 | 30 | return NULL; 31 | } 32 | 33 | int 34 | main() { 35 | int e; 36 | 37 | uv_loop_t *loop = uv_default_loop(); 38 | 39 | js_platform_t *platform; 40 | e = js_create_platform(loop, NULL, &platform); 41 | assert(e == 0); 42 | 43 | js_env_t *env; 44 | e = js_create_env(loop, platform, NULL, &env); 45 | assert(e == 0); 46 | 47 | js_handle_scope_t *scope; 48 | e = js_open_handle_scope(env, &scope); 49 | assert(e == 0); 50 | 51 | js_value_t *fn; 52 | e = js_create_function(env, "hello", -1, on_call, NULL, &fn); 53 | assert(e == 0); 54 | 55 | js_value_t *global; 56 | e = js_get_global(env, &global); 57 | assert(e == 0); 58 | 59 | e = js_set_named_property(env, global, "hello", fn); 60 | assert(e == 0); 61 | 62 | js_value_t *script; 63 | e = js_create_string_utf8(env, (utf8_t *) "hello(1, 2)", -1, &script); 64 | assert(e == 0); 65 | 66 | js_value_t *result; 67 | e = js_run_script(env, NULL, 0, 0, script, &result); 68 | assert(e == 0); 69 | 70 | assert(fn_called); 71 | 72 | bool has_exception; 73 | e = js_is_exception_pending(env, &has_exception); 74 | assert(e == 0); 75 | 76 | assert(!has_exception); 77 | 78 | e = js_close_handle_scope(env, scope); 79 | assert(e == 0); 80 | 81 | e = js_destroy_env(env); 82 | assert(e == 0); 83 | 84 | e = js_destroy_platform(platform); 85 | assert(e == 0); 86 | 87 | e = uv_run(loop, UV_RUN_DEFAULT); 88 | assert(e == 0); 89 | } 90 | -------------------------------------------------------------------------------- /test/create-function-args-fewer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | bool fn_called = false; 9 | 10 | js_value_t * 11 | on_call(js_env_t *env, js_callback_info_t *info) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | js_value_t *argv[2]; 17 | size_t argc = 2; 18 | 19 | e = js_get_callback_info(env, info, &argc, argv, NULL, NULL); 20 | assert(e == 0); 21 | 22 | assert(argc == 1); 23 | 24 | uint32_t value; 25 | e = js_get_value_uint32(env, argv[0], &value); 26 | assert(e == 0); 27 | 28 | assert(value == 1); 29 | 30 | bool is_undefined; 31 | e = js_is_undefined(env, argv[1], &is_undefined); 32 | assert(e == 0); 33 | 34 | assert(is_undefined); 35 | 36 | return NULL; 37 | } 38 | 39 | int 40 | main() { 41 | int e; 42 | 43 | uv_loop_t *loop = uv_default_loop(); 44 | 45 | js_platform_t *platform; 46 | e = js_create_platform(loop, NULL, &platform); 47 | assert(e == 0); 48 | 49 | js_env_t *env; 50 | e = js_create_env(loop, platform, NULL, &env); 51 | assert(e == 0); 52 | 53 | js_handle_scope_t *scope; 54 | e = js_open_handle_scope(env, &scope); 55 | assert(e == 0); 56 | 57 | js_value_t *fn; 58 | e = js_create_function(env, "hello", -1, on_call, NULL, &fn); 59 | assert(e == 0); 60 | 61 | js_value_t *global; 62 | e = js_get_global(env, &global); 63 | assert(e == 0); 64 | 65 | e = js_set_named_property(env, global, "hello", fn); 66 | assert(e == 0); 67 | 68 | js_value_t *script; 69 | e = js_create_string_utf8(env, (utf8_t *) "hello(1)", -1, &script); 70 | assert(e == 0); 71 | 72 | js_value_t *result; 73 | e = js_run_script(env, NULL, 0, 0, script, &result); 74 | assert(e == 0); 75 | 76 | assert(fn_called); 77 | 78 | bool has_exception; 79 | e = js_is_exception_pending(env, &has_exception); 80 | assert(e == 0); 81 | 82 | assert(!has_exception); 83 | 84 | e = js_close_handle_scope(env, scope); 85 | assert(e == 0); 86 | 87 | e = js_destroy_env(env); 88 | assert(e == 0); 89 | 90 | e = js_destroy_platform(platform); 91 | assert(e == 0); 92 | 93 | e = uv_run(loop, UV_RUN_DEFAULT); 94 | assert(e == 0); 95 | } 96 | -------------------------------------------------------------------------------- /test/create-function-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "../include/js.h" 8 | 9 | bool fn_called = false; 10 | 11 | js_value_t * 12 | on_call(js_env_t *env, js_callback_info_t *info) { 13 | int e; 14 | 15 | fn_called = true; 16 | 17 | js_value_t *err; 18 | e = js_create_string_utf8(env, (utf8_t *) "err", -1, &err); 19 | assert(e == 0); 20 | 21 | e = js_throw(env, err); 22 | assert(e == 0); 23 | 24 | return NULL; 25 | } 26 | 27 | int 28 | main() { 29 | int e; 30 | 31 | uv_loop_t *loop = uv_default_loop(); 32 | 33 | js_platform_t *platform; 34 | e = js_create_platform(loop, NULL, &platform); 35 | assert(e == 0); 36 | 37 | js_env_t *env; 38 | e = js_create_env(loop, platform, NULL, &env); 39 | assert(e == 0); 40 | 41 | js_handle_scope_t *scope; 42 | e = js_open_handle_scope(env, &scope); 43 | assert(e == 0); 44 | 45 | js_value_t *fn; 46 | e = js_create_function(env, "hello", -1, on_call, NULL, &fn); 47 | assert(e == 0); 48 | 49 | js_value_t *global; 50 | e = js_get_global(env, &global); 51 | assert(e == 0); 52 | 53 | e = js_set_named_property(env, global, "hello", fn); 54 | assert(e == 0); 55 | 56 | js_value_t *script; 57 | e = js_create_string_utf8(env, (utf8_t *) "hello()", -1, &script); 58 | assert(e == 0); 59 | 60 | js_value_t *result; 61 | e = js_run_script(env, NULL, 0, 0, script, &result); 62 | assert(e == js_pending_exception); 63 | 64 | assert(fn_called); 65 | 66 | bool has_exception; 67 | e = js_is_exception_pending(env, &has_exception); 68 | assert(e == 0); 69 | 70 | assert(has_exception); 71 | 72 | js_value_t *error; 73 | e = js_get_and_clear_last_exception(env, &error); 74 | assert(e == 0); 75 | 76 | utf8_t value[4]; 77 | e = js_get_value_string_utf8(env, error, value, 4, NULL); 78 | assert(e == 0); 79 | 80 | assert(strcmp((char *) value, "err") == 0); 81 | 82 | e = js_close_handle_scope(env, scope); 83 | assert(e == 0); 84 | 85 | e = js_destroy_env(env); 86 | assert(e == 0); 87 | 88 | e = js_destroy_platform(platform); 89 | assert(e == 0); 90 | 91 | e = uv_run(loop, UV_RUN_DEFAULT); 92 | assert(e == 0); 93 | } 94 | -------------------------------------------------------------------------------- /test/create-function-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool finalize_called = false; 9 | 10 | static void 11 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 12 | finalize_called = true; 13 | } 14 | 15 | js_value_t * 16 | on_call(js_env_t *env, js_callback_info_t *info) { 17 | return NULL; 18 | } 19 | 20 | int 21 | main() { 22 | int e; 23 | 24 | uv_loop_t *loop = uv_default_loop(); 25 | 26 | js_platform_options_t options = { 27 | .expose_garbage_collection = true, 28 | .trace_garbage_collection = true, 29 | }; 30 | 31 | js_platform_t *platform; 32 | e = js_create_platform(loop, &options, &platform); 33 | assert(e == 0); 34 | 35 | js_env_t *env; 36 | e = js_create_env(loop, platform, NULL, &env); 37 | assert(e == 0); 38 | 39 | js_handle_scope_t *scope; 40 | e = js_open_handle_scope(env, &scope); 41 | assert(e == 0); 42 | 43 | js_value_t *fn; 44 | e = js_create_function(env, "fn", -1, on_call, NULL, &fn); 45 | assert(e == 0); 46 | 47 | e = js_add_finalizer(env, fn, NULL, on_finalize, NULL, NULL); 48 | assert(e == 0); 49 | 50 | e = js_close_handle_scope(env, scope); 51 | assert(e == 0); 52 | 53 | e = js_request_garbage_collection(env); 54 | assert(e == 0); 55 | 56 | e = js_destroy_env(env); 57 | assert(e == 0); 58 | 59 | e = js_destroy_platform(platform); 60 | assert(e == 0); 61 | 62 | e = uv_run(loop, UV_RUN_DEFAULT); 63 | assert(e == 0); 64 | 65 | assert(finalize_called); 66 | } 67 | -------------------------------------------------------------------------------- /test/create-function-with-source.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *args[1]; 27 | 28 | e = js_create_string_utf8(env, (utf8_t *) "n", -1, &args[0]); 29 | assert(e == 0); 30 | 31 | js_value_t *source; 32 | e = js_create_string_utf8(env, (utf8_t *) "return n * 2", -1, &source); 33 | assert(e == 0); 34 | 35 | js_value_t *fn; 36 | e = js_create_function_with_source(env, "hello", -1, "test.js", -1, args, 1, 0, source, &fn); 37 | assert(e == 0); 38 | 39 | js_value_t *global; 40 | e = js_get_global(env, &global); 41 | assert(e == 0); 42 | 43 | e = js_set_named_property(env, global, "hello", fn); 44 | assert(e == 0); 45 | 46 | js_value_t *script; 47 | e = js_create_string_utf8(env, (utf8_t *) "hello(42)", -1, &script); 48 | assert(e == 0); 49 | 50 | js_value_t *result; 51 | e = js_run_script(env, NULL, 0, 0, script, &result); 52 | assert(e == 0); 53 | 54 | bool has_exception; 55 | e = js_is_exception_pending(env, &has_exception); 56 | assert(e == 0); 57 | 58 | assert(!has_exception); 59 | 60 | uint32_t value; 61 | e = js_get_value_uint32(env, result, &value); 62 | assert(e == 0); 63 | 64 | assert(value == 84); 65 | 66 | e = js_close_handle_scope(env, scope); 67 | assert(e == 0); 68 | 69 | e = js_destroy_env(env); 70 | assert(e == 0); 71 | 72 | e = js_destroy_platform(platform); 73 | assert(e == 0); 74 | 75 | e = uv_run(loop, UV_RUN_DEFAULT); 76 | assert(e == 0); 77 | } 78 | -------------------------------------------------------------------------------- /test/create-function.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | bool fn_called = false; 9 | 10 | js_value_t * 11 | on_call(js_env_t *env, js_callback_info_t *info) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | js_value_t *argv[1]; 17 | size_t argc = 1; 18 | 19 | e = js_get_callback_info(env, info, &argc, argv, NULL, NULL); 20 | assert(e == 0); 21 | 22 | assert(argc == 1); 23 | 24 | uint32_t value; 25 | e = js_get_value_uint32(env, argv[0], &value); 26 | assert(e == 0); 27 | 28 | assert(value == 42); 29 | 30 | e = js_create_uint32(env, value * 2, &argv[0]); 31 | assert(e == 0); 32 | 33 | return argv[0]; 34 | } 35 | 36 | int 37 | main() { 38 | int e; 39 | 40 | uv_loop_t *loop = uv_default_loop(); 41 | 42 | js_platform_t *platform; 43 | e = js_create_platform(loop, NULL, &platform); 44 | assert(e == 0); 45 | 46 | js_env_t *env; 47 | e = js_create_env(loop, platform, NULL, &env); 48 | assert(e == 0); 49 | 50 | js_handle_scope_t *scope; 51 | e = js_open_handle_scope(env, &scope); 52 | assert(e == 0); 53 | 54 | js_value_t *fn; 55 | e = js_create_function(env, "hello", -1, on_call, NULL, &fn); 56 | assert(e == 0); 57 | 58 | js_value_t *global; 59 | e = js_get_global(env, &global); 60 | assert(e == 0); 61 | 62 | e = js_set_named_property(env, global, "hello", fn); 63 | assert(e == 0); 64 | 65 | js_value_t *script; 66 | e = js_create_string_utf8(env, (utf8_t *) "hello(42)", -1, &script); 67 | assert(e == 0); 68 | 69 | js_value_t *result; 70 | e = js_run_script(env, NULL, 0, 0, script, &result); 71 | assert(e == 0); 72 | 73 | assert(fn_called); 74 | 75 | bool has_exception; 76 | e = js_is_exception_pending(env, &has_exception); 77 | assert(e == 0); 78 | 79 | assert(!has_exception); 80 | 81 | uint32_t value; 82 | e = js_get_value_uint32(env, result, &value); 83 | assert(e == 0); 84 | 85 | assert(value == 84); 86 | 87 | e = js_close_handle_scope(env, scope); 88 | assert(e == 0); 89 | 90 | e = js_destroy_env(env); 91 | assert(e == 0); 92 | 93 | e = js_destroy_platform(platform); 94 | assert(e == 0); 95 | 96 | e = uv_run(loop, UV_RUN_DEFAULT); 97 | assert(e == 0); 98 | } 99 | -------------------------------------------------------------------------------- /test/create-promise-reject.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_deferred_t *deferred; 25 | js_value_t *promise; 26 | e = js_create_promise(env, &deferred, &promise); 27 | assert(e == 0); 28 | 29 | js_promise_state_t state; 30 | e = js_get_promise_state(env, promise, &state); 31 | assert(e == 0); 32 | 33 | assert(state == js_promise_pending); 34 | 35 | js_value_t *value; 36 | e = js_create_int32(env, 42, &value); 37 | assert(e == 0); 38 | 39 | e = js_reject_deferred(env, deferred, value); 40 | assert(e == 0); 41 | 42 | e = js_get_promise_state(env, promise, &state); 43 | assert(e == 0); 44 | 45 | assert(state == js_promise_rejected); 46 | 47 | e = js_close_handle_scope(env, scope); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-promise-resolve.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_deferred_t *deferred; 25 | js_value_t *promise; 26 | e = js_create_promise(env, &deferred, &promise); 27 | assert(e == 0); 28 | 29 | js_promise_state_t state; 30 | e = js_get_promise_state(env, promise, &state); 31 | assert(e == 0); 32 | 33 | assert(state == js_promise_pending); 34 | 35 | js_value_t *value; 36 | e = js_create_int32(env, 42, &value); 37 | assert(e == 0); 38 | 39 | e = js_resolve_deferred(env, deferred, value); 40 | assert(e == 0); 41 | 42 | e = js_get_promise_state(env, promise, &state); 43 | assert(e == 0); 44 | 45 | assert(state == js_promise_fulfilled); 46 | 47 | e = js_close_handle_scope(env, scope); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_create_array(env, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_array; 39 | e = js_is_array(env, result, &is_array); 40 | assert(e == 0); 41 | 42 | assert(is_array); 43 | 44 | e = js_delete_reference(env, ref); 45 | assert(e == 0); 46 | 47 | e = js_close_handle_scope(env, scope); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-bigint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_create_bigint_uint64(env, 42, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_bigint; 39 | e = js_is_bigint(env, result, &is_bigint); 40 | assert(e == 0); 41 | 42 | assert(is_bigint); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_delete_reference(env, ref); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-boolean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_get_boolean(env, true, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_boolean; 39 | e = js_is_boolean(env, result, &is_boolean); 40 | assert(e == 0); 41 | 42 | assert(is_boolean); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_delete_reference(env, ref); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-function.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | js_value_t * 7 | on_call(js_env_t *env, js_callback_info_t *info) { 8 | return NULL; 9 | } 10 | 11 | int 12 | main() { 13 | int e; 14 | 15 | uv_loop_t *loop = uv_default_loop(); 16 | 17 | js_platform_t *platform; 18 | e = js_create_platform(loop, NULL, &platform); 19 | assert(e == 0); 20 | 21 | js_env_t *env; 22 | e = js_create_env(loop, platform, NULL, &env); 23 | assert(e == 0); 24 | 25 | js_handle_scope_t *scope; 26 | e = js_open_handle_scope(env, &scope); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_create_function(env, "hello", -1, on_call, NULL, &value); 31 | assert(e == 0); 32 | 33 | js_ref_t *ref; 34 | e = js_create_reference(env, value, 1, &ref); 35 | assert(e == 0); 36 | 37 | js_value_t *result; 38 | e = js_get_reference_value(env, ref, &result); 39 | assert(e == 0); 40 | 41 | assert(result != NULL); 42 | 43 | bool is_function; 44 | e = js_is_function(env, result, &is_function); 45 | assert(e == 0); 46 | 47 | assert(is_function); 48 | 49 | e = js_delete_reference(env, ref); 50 | assert(e == 0); 51 | 52 | e = js_close_handle_scope(env, scope); 53 | assert(e == 0); 54 | 55 | e = js_destroy_env(env); 56 | assert(e == 0); 57 | 58 | e = js_destroy_platform(platform); 59 | assert(e == 0); 60 | 61 | e = uv_run(loop, UV_RUN_DEFAULT); 62 | assert(e == 0); 63 | } 64 | -------------------------------------------------------------------------------- /test/create-reference-null.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_get_null(env, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_null; 39 | e = js_is_null(env, result, &is_null); 40 | assert(e == 0); 41 | 42 | assert(is_null); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_delete_reference(env, ref); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_create_uint32(env, 42, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_number; 39 | e = js_is_number(env, result, &is_number); 40 | assert(e == 0); 41 | 42 | assert(is_number); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_delete_reference(env, ref); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-object-multiple.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_create_object(env, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref1; 29 | e = js_create_reference(env, value, 1, &ref1); 30 | assert(e == 0); 31 | 32 | js_ref_t *ref2; 33 | e = js_create_reference(env, value, 1, &ref2); 34 | assert(e == 0); 35 | 36 | e = js_delete_reference(env, ref1); 37 | assert(e == 0); 38 | 39 | e = js_delete_reference(env, ref2); 40 | assert(e == 0); 41 | 42 | e = js_close_handle_scope(env, scope); 43 | assert(e == 0); 44 | 45 | e = js_destroy_env(env); 46 | assert(e == 0); 47 | 48 | e = js_destroy_platform(platform); 49 | assert(e == 0); 50 | 51 | e = uv_run(loop, UV_RUN_DEFAULT); 52 | assert(e == 0); 53 | } 54 | -------------------------------------------------------------------------------- /test/create-reference-object-weak.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | js_ref_t *ref; 7 | 8 | js_value_t * 9 | on_call(js_env_t *env, js_callback_info_t *info) { 10 | int e; 11 | 12 | js_value_t *value; 13 | e = js_create_object(env, &value); 14 | assert(e == 0); 15 | 16 | e = js_create_reference(env, value, 0, &ref); 17 | assert(e == 0); 18 | 19 | js_value_t *result; 20 | e = js_get_reference_value(env, ref, &result); 21 | assert(e == 0); 22 | 23 | assert(result != NULL); 24 | 25 | bool is_object; 26 | e = js_is_object(env, result, &is_object); 27 | assert(e == 0); 28 | 29 | assert(is_object); 30 | 31 | return NULL; 32 | } 33 | 34 | int 35 | main() { 36 | int e; 37 | 38 | uv_loop_t *loop = uv_default_loop(); 39 | 40 | js_platform_options_t options = { 41 | .expose_garbage_collection = true, 42 | }; 43 | 44 | js_platform_t *platform; 45 | e = js_create_platform(loop, &options, &platform); 46 | assert(e == 0); 47 | 48 | js_env_t *env; 49 | e = js_create_env(loop, platform, NULL, &env); 50 | assert(e == 0); 51 | 52 | js_handle_scope_t *scope; 53 | e = js_open_handle_scope(env, &scope); 54 | assert(e == 0); 55 | 56 | js_value_t *fn; 57 | e = js_create_function(env, "fn", -1, on_call, NULL, &fn); 58 | assert(e == 0); 59 | 60 | js_value_t *global; 61 | e = js_get_global(env, &global); 62 | assert(e == 0); 63 | 64 | e = js_call_function(env, global, fn, 0, NULL, NULL); 65 | assert(e == 0); 66 | 67 | e = js_request_garbage_collection(env); 68 | assert(e == 0); 69 | 70 | js_value_t *result; 71 | e = js_get_reference_value(env, ref, &result); 72 | assert(e == 0); 73 | 74 | assert(result == NULL); 75 | 76 | e = js_delete_reference(env, ref); 77 | assert(e == 0); 78 | 79 | e = js_close_handle_scope(env, scope); 80 | assert(e == 0); 81 | 82 | e = js_destroy_env(env); 83 | assert(e == 0); 84 | 85 | e = js_destroy_platform(platform); 86 | assert(e == 0); 87 | 88 | e = uv_run(loop, UV_RUN_DEFAULT); 89 | assert(e == 0); 90 | } 91 | -------------------------------------------------------------------------------- /test/create-reference-object.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_create_object(env, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_object; 39 | e = js_is_object(env, result, &is_object); 40 | assert(e == 0); 41 | 42 | assert(is_object); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_delete_reference(env, ref); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-reference-string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *value; 26 | e = js_create_string_utf8(env, (utf8_t *) "hello world", -1, &value); 27 | assert(e == 0); 28 | 29 | js_ref_t *ref; 30 | e = js_create_reference(env, value, 1, &ref); 31 | assert(e == 0); 32 | 33 | js_value_t *result; 34 | e = js_get_reference_value(env, ref, &result); 35 | assert(e == 0); 36 | 37 | assert(result != NULL); 38 | 39 | bool is_string; 40 | e = js_is_string(env, result, &is_string); 41 | assert(e == 0); 42 | 43 | assert(is_string); 44 | 45 | e = js_close_handle_scope(env, scope); 46 | assert(e == 0); 47 | 48 | e = js_delete_reference(env, ref); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/create-reference-symbol.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *description; 26 | e = js_create_string_utf8(env, (utf8_t *) "hello world", -1, &description); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_create_symbol(env, description, &value); 31 | assert(e == 0); 32 | 33 | js_ref_t *ref; 34 | e = js_create_reference(env, value, 1, &ref); 35 | assert(e == 0); 36 | 37 | js_value_t *result; 38 | e = js_get_reference_value(env, ref, &result); 39 | assert(e == 0); 40 | 41 | assert(result != NULL); 42 | 43 | bool is_symbol; 44 | e = js_is_symbol(env, result, &is_symbol); 45 | assert(e == 0); 46 | 47 | assert(is_symbol); 48 | 49 | e = js_close_handle_scope(env, scope); 50 | assert(e == 0); 51 | 52 | e = js_delete_reference(env, ref); 53 | assert(e == 0); 54 | 55 | e = js_destroy_env(env); 56 | assert(e == 0); 57 | 58 | e = js_destroy_platform(platform); 59 | assert(e == 0); 60 | 61 | e = uv_run(loop, UV_RUN_DEFAULT); 62 | assert(e == 0); 63 | } 64 | -------------------------------------------------------------------------------- /test/create-reference-undefined.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_get_undefined(env, &value); 26 | assert(e == 0); 27 | 28 | js_ref_t *ref; 29 | e = js_create_reference(env, value, 1, &ref); 30 | assert(e == 0); 31 | 32 | js_value_t *result; 33 | e = js_get_reference_value(env, ref, &result); 34 | assert(e == 0); 35 | 36 | assert(result != NULL); 37 | 38 | bool is_undefined; 39 | e = js_is_undefined(env, result, &is_undefined); 40 | assert(e == 0); 41 | 42 | assert(is_undefined); 43 | 44 | e = js_delete_reference(env, ref); 45 | assert(e == 0); 46 | 47 | e = js_close_handle_scope(env, scope); 48 | assert(e == 0); 49 | 50 | e = js_destroy_env(env); 51 | assert(e == 0); 52 | 53 | e = js_destroy_platform(platform); 54 | assert(e == 0); 55 | 56 | e = uv_run(loop, UV_RUN_DEFAULT); 57 | assert(e == 0); 58 | } 59 | -------------------------------------------------------------------------------- /test/create-sharedarraybuffer-with-backing-store.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | uint8_t *data_a, *data_b; 27 | 28 | js_arraybuffer_backing_store_t *backing_store; 29 | 30 | { 31 | js_value_t *sharedarraybuffer; 32 | e = js_create_sharedarraybuffer(env, 1, (void **) &data_a, &sharedarraybuffer); 33 | assert(e == 0); 34 | 35 | e = js_get_sharedarraybuffer_backing_store(env, sharedarraybuffer, &backing_store); 36 | assert(e == 0); 37 | } 38 | 39 | { 40 | js_value_t *sharedarraybuffer; 41 | e = js_create_sharedarraybuffer_with_backing_store(env, backing_store, (void **) &data_b, NULL, &sharedarraybuffer); 42 | assert(e == 0); 43 | } 44 | 45 | data_a[0] = 42; 46 | 47 | assert(data_b[0] == 42); 48 | 49 | e = js_release_arraybuffer_backing_store(env, backing_store); 50 | assert(e == 0); 51 | 52 | e = js_close_handle_scope(env, scope); 53 | assert(e == 0); 54 | 55 | e = js_destroy_env(env); 56 | assert(e == 0); 57 | 58 | e = js_destroy_platform(platform); 59 | assert(e == 0); 60 | 61 | e = uv_run(loop, UV_RUN_DEFAULT); 62 | assert(e == 0); 63 | } 64 | -------------------------------------------------------------------------------- /test/create-sharedarraybuffer.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *sharedarraybuffer; 27 | e = js_create_sharedarraybuffer(env, 4096, NULL, &sharedarraybuffer); 28 | assert(e == 0); 29 | 30 | e = js_close_handle_scope(env, scope); 31 | assert(e == 0); 32 | 33 | e = js_destroy_env(env); 34 | assert(e == 0); 35 | 36 | e = js_destroy_platform(platform); 37 | assert(e == 0); 38 | 39 | e = uv_run(loop, UV_RUN_DEFAULT); 40 | assert(e == 0); 41 | } 42 | -------------------------------------------------------------------------------- /test/create-string-utf8-with-null.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *string; 27 | e = js_create_string_utf8(env, (utf8_t *) "hello\0world", 11, &string); 28 | assert(e == 0); 29 | 30 | size_t len; 31 | e = js_get_value_string_utf8(env, string, NULL, 0, &len); 32 | assert(e == 0); 33 | 34 | assert(len >= 11); 35 | 36 | utf8_t value[11]; 37 | e = js_get_value_string_utf8(env, string, value, len, &len); 38 | assert(e == 0); 39 | 40 | assert(len == 11); 41 | 42 | assert(memcmp(value, "hello\0world", len) == 0); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_destroy_env(env); 48 | assert(e == 0); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/create-string-utf8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *string; 27 | e = js_create_string_utf8(env, (utf8_t *) "hello world", 11, &string); 28 | assert(e == 0); 29 | 30 | size_t len; 31 | e = js_get_value_string_utf8(env, string, NULL, 0, &len); 32 | assert(e == 0); 33 | 34 | assert(len >= 11); 35 | 36 | utf8_t value[11]; 37 | e = js_get_value_string_utf8(env, string, value, len, &len); 38 | assert(e == 0); 39 | 40 | assert(len == 11); 41 | 42 | assert(memcmp(value, "hello world", len) == 0); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_destroy_env(env); 48 | assert(e == 0); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/create-threadsafe-function-with-callback.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | bool fn_called = false; 8 | js_threadsafe_function_t *tsfn; 9 | 10 | void 11 | on_call(js_env_t *env, js_value_t *function, void *context, void *data) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | e = js_release_threadsafe_function(tsfn, js_threadsafe_function_release); 17 | assert(e == 0); 18 | } 19 | 20 | int 21 | main() { 22 | int e; 23 | 24 | uv_loop_t *loop = uv_default_loop(); 25 | 26 | js_platform_t *platform; 27 | e = js_create_platform(loop, NULL, &platform); 28 | assert(e == 0); 29 | 30 | js_env_t *env; 31 | e = js_create_env(loop, platform, NULL, &env); 32 | assert(e == 0); 33 | 34 | js_handle_scope_t *scope; 35 | e = js_open_handle_scope(env, &scope); 36 | assert(e == 0); 37 | 38 | e = js_create_threadsafe_function(env, NULL, 0, 1, NULL, NULL, NULL, on_call, &tsfn); 39 | assert(e == 0); 40 | 41 | e = js_call_threadsafe_function(tsfn, NULL, js_threadsafe_function_nonblocking); 42 | assert(e == 0); 43 | 44 | e = uv_run(loop, UV_RUN_DEFAULT); 45 | assert(e == 0); 46 | 47 | assert(fn_called); 48 | 49 | e = js_close_handle_scope(env, scope); 50 | assert(e == 0); 51 | 52 | e = js_destroy_env(env); 53 | assert(e == 0); 54 | 55 | e = js_destroy_platform(platform); 56 | assert(e == 0); 57 | 58 | e = uv_run(loop, UV_RUN_DEFAULT); 59 | assert(e == 0); 60 | } 61 | -------------------------------------------------------------------------------- /test/create-threadsafe-function-with-context.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | bool fn_called = false; 8 | js_threadsafe_function_t *tsfn; 9 | 10 | void 11 | on_call(js_env_t *env, js_value_t *function, void *context, void *data) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | assert(context == (void *) 42); 17 | 18 | e = js_release_threadsafe_function(tsfn, js_threadsafe_function_release); 19 | assert(e == 0); 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | e = js_create_threadsafe_function(env, NULL, 0, 1, NULL, NULL, (void *) 42, on_call, &tsfn); 41 | assert(e == 0); 42 | 43 | e = js_call_threadsafe_function(tsfn, NULL, js_threadsafe_function_nonblocking); 44 | assert(e == 0); 45 | 46 | e = uv_run(loop, UV_RUN_DEFAULT); 47 | assert(e == 0); 48 | 49 | assert(fn_called); 50 | 51 | e = js_close_handle_scope(env, scope); 52 | assert(e == 0); 53 | 54 | e = js_destroy_env(env); 55 | assert(e == 0); 56 | 57 | e = js_destroy_platform(platform); 58 | assert(e == 0); 59 | 60 | e = uv_run(loop, UV_RUN_DEFAULT); 61 | assert(e == 0); 62 | } 63 | -------------------------------------------------------------------------------- /test/create-threadsafe-function-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static bool finalize_called = false; 8 | 9 | js_threadsafe_function_t *tsfn; 10 | 11 | void 12 | on_call(js_env_t *env, js_value_t *function, void *context, void *data) { 13 | int e; 14 | 15 | e = js_release_threadsafe_function(tsfn, js_threadsafe_function_release); 16 | assert(e == 0); 17 | } 18 | 19 | void 20 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 21 | finalize_called = true; 22 | } 23 | 24 | int 25 | main() { 26 | int e; 27 | 28 | uv_loop_t *loop = uv_default_loop(); 29 | 30 | js_platform_t *platform; 31 | e = js_create_platform(loop, NULL, &platform); 32 | assert(e == 0); 33 | 34 | js_env_t *env; 35 | e = js_create_env(loop, platform, NULL, &env); 36 | assert(e == 0); 37 | 38 | js_handle_scope_t *scope; 39 | e = js_open_handle_scope(env, &scope); 40 | assert(e == 0); 41 | 42 | e = js_create_threadsafe_function(env, NULL, 0, 1, on_finalize, NULL, NULL, on_call, &tsfn); 43 | assert(e == 0); 44 | 45 | e = js_call_threadsafe_function(tsfn, NULL, js_threadsafe_function_nonblocking); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | 51 | assert(finalize_called); 52 | 53 | e = js_close_handle_scope(env, scope); 54 | assert(e == 0); 55 | 56 | e = js_destroy_env(env); 57 | assert(e == 0); 58 | 59 | e = js_destroy_platform(platform); 60 | assert(e == 0); 61 | 62 | e = uv_run(loop, UV_RUN_DEFAULT); 63 | assert(e == 0); 64 | } 65 | -------------------------------------------------------------------------------- /test/create-threadsafe-function.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | bool fn_called = false; 8 | js_threadsafe_function_t *tsfn; 9 | 10 | js_value_t * 11 | on_call(js_env_t *env, js_callback_info_t *info) { 12 | int e; 13 | 14 | fn_called = true; 15 | 16 | e = js_release_threadsafe_function(tsfn, js_threadsafe_function_release); 17 | assert(e == 0); 18 | 19 | return NULL; 20 | } 21 | 22 | int 23 | main() { 24 | int e; 25 | 26 | uv_loop_t *loop = uv_default_loop(); 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, NULL, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_value_t *fn; 41 | e = js_create_function(env, "hello", -1, on_call, NULL, &fn); 42 | assert(e == 0); 43 | 44 | e = js_create_threadsafe_function(env, fn, 0, 1, NULL, NULL, NULL, NULL, &tsfn); 45 | assert(e == 0); 46 | 47 | e = js_call_threadsafe_function(tsfn, NULL, js_threadsafe_function_nonblocking); 48 | assert(e == 0); 49 | 50 | e = uv_run(loop, UV_RUN_DEFAULT); 51 | assert(e == 0); 52 | 53 | assert(fn_called); 54 | 55 | e = js_close_handle_scope(env, scope); 56 | assert(e == 0); 57 | 58 | e = js_destroy_env(env); 59 | assert(e == 0); 60 | 61 | e = js_destroy_platform(platform); 62 | assert(e == 0); 63 | 64 | e = uv_run(loop, UV_RUN_DEFAULT); 65 | assert(e == 0); 66 | } 67 | -------------------------------------------------------------------------------- /test/create-typed-function-no-jit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static int typed_calls = 0; 9 | static int untyped_calls = 0; 10 | 11 | uint32_t 12 | on_typed_call(js_value_t *receiver, js_typed_callback_info_t *info) { 13 | typed_calls++; 14 | 15 | return 42; 16 | } 17 | 18 | js_value_t * 19 | on_untyped_call(js_env_t *env, js_callback_info_t *info) { 20 | int e; 21 | 22 | untyped_calls++; 23 | 24 | js_value_t *result; 25 | e = js_create_uint32(env, 42, &result); 26 | assert(e == 0); 27 | 28 | return result; 29 | } 30 | 31 | int 32 | main() { 33 | int e; 34 | 35 | uv_loop_t *loop = uv_default_loop(); 36 | 37 | js_platform_options_t options = { 38 | .disable_optimizing_compiler = true, 39 | }; 40 | 41 | js_platform_t *platform; 42 | e = js_create_platform(loop, &options, &platform); 43 | assert(e == 0); 44 | 45 | js_env_t *env; 46 | e = js_create_env(loop, platform, NULL, &env); 47 | assert(e == 0); 48 | 49 | js_handle_scope_t *scope; 50 | e = js_open_handle_scope(env, &scope); 51 | assert(e == 0); 52 | 53 | js_callback_signature_t signature = { 54 | .result = js_uint32, 55 | .args_len = 1, 56 | .args = (int[]) { 57 | js_object, 58 | }, 59 | }; 60 | 61 | js_value_t *fn; 62 | e = js_create_typed_function(env, "hello", -1, on_untyped_call, &signature, on_typed_call, NULL, &fn); 63 | assert(e == 0); 64 | 65 | js_value_t *global; 66 | e = js_get_global(env, &global); 67 | assert(e == 0); 68 | 69 | e = js_set_named_property(env, global, "hello", fn); 70 | assert(e == 0); 71 | 72 | js_value_t *script; 73 | e = js_create_string_utf8(env, (utf8_t *) "let i = 0, j; while (i++ < 200000) j = hello()", -1, &script); 74 | assert(e == 0); 75 | 76 | js_value_t *result; 77 | e = js_run_script(env, NULL, 0, 0, script, &result); 78 | assert(e == 0); 79 | 80 | assert(untyped_calls == 200000); 81 | assert(typed_calls == 0); 82 | 83 | e = js_close_handle_scope(env, scope); 84 | assert(e == 0); 85 | 86 | e = js_destroy_env(env); 87 | assert(e == 0); 88 | 89 | e = js_destroy_platform(platform); 90 | assert(e == 0); 91 | 92 | e = uv_run(loop, UV_RUN_DEFAULT); 93 | assert(e == 0); 94 | } 95 | -------------------------------------------------------------------------------- /test/create-typed-function-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool finalize_called = false; 9 | 10 | static void 11 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 12 | finalize_called = true; 13 | } 14 | 15 | uint32_t 16 | on_typed_call(js_typed_callback_info_t *info) { 17 | return 42; 18 | } 19 | 20 | js_value_t * 21 | on_untyped_call(js_env_t *env, js_callback_info_t *info) { 22 | int e; 23 | 24 | js_value_t *result; 25 | e = js_create_uint32(env, 42, &result); 26 | assert(e == 0); 27 | 28 | return result; 29 | } 30 | 31 | int 32 | main() { 33 | int e; 34 | 35 | uv_loop_t *loop = uv_default_loop(); 36 | 37 | js_platform_options_t options = { 38 | .expose_garbage_collection = true, 39 | .trace_garbage_collection = true, 40 | }; 41 | 42 | js_platform_t *platform; 43 | e = js_create_platform(loop, &options, &platform); 44 | assert(e == 0); 45 | 46 | js_env_t *env; 47 | e = js_create_env(loop, platform, NULL, &env); 48 | assert(e == 0); 49 | 50 | js_handle_scope_t *scope; 51 | e = js_open_handle_scope(env, &scope); 52 | assert(e == 0); 53 | 54 | js_callback_signature_t signature = { 55 | .result = js_uint32, 56 | }; 57 | 58 | js_value_t *fn; 59 | e = js_create_typed_function(env, "hello", -1, on_untyped_call, &signature, on_typed_call, NULL, &fn); 60 | assert(e == 0); 61 | 62 | e = js_add_finalizer(env, fn, NULL, on_finalize, NULL, NULL); 63 | assert(e == 0); 64 | 65 | e = js_close_handle_scope(env, scope); 66 | assert(e == 0); 67 | 68 | e = js_request_garbage_collection(env); 69 | assert(e == 0); 70 | 71 | e = js_destroy_env(env); 72 | assert(e == 0); 73 | 74 | e = js_destroy_platform(platform); 75 | assert(e == 0); 76 | 77 | e = uv_run(loop, UV_RUN_DEFAULT); 78 | assert(e == 0); 79 | 80 | (void) finalize_called; // Might not have run 81 | } 82 | -------------------------------------------------------------------------------- /test/create-uint8array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | uint8_t *data; 26 | 27 | js_value_t *arraybuffer; 28 | e = js_create_arraybuffer(env, 10, (void **) &data, &arraybuffer); 29 | assert(e == 0); 30 | 31 | js_value_t *typedarray; 32 | e = js_create_typedarray(env, js_uint8array, 10, arraybuffer, 0, &typedarray); 33 | assert(e == 0); 34 | 35 | e = js_close_handle_scope(env, scope); 36 | assert(e == 0); 37 | 38 | e = js_destroy_env(env); 39 | assert(e == 0); 40 | 41 | e = js_destroy_platform(platform); 42 | assert(e == 0); 43 | 44 | e = uv_run(loop, UV_RUN_DEFAULT); 45 | assert(e == 0); 46 | } 47 | -------------------------------------------------------------------------------- /test/create-unsafe-arraybuffer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | uint8_t *data; 25 | 26 | js_value_t *arraybuffer; 27 | e = js_create_unsafe_arraybuffer(env, 4, (void **) &data, &arraybuffer); 28 | assert(e == 0); 29 | 30 | for (int i = 0; i < 4; i++) { 31 | printf("0x%02X\n", data[i]); 32 | } 33 | 34 | e = js_close_handle_scope(env, scope); 35 | assert(e == 0); 36 | 37 | e = js_destroy_env(env); 38 | assert(e == 0); 39 | 40 | e = js_destroy_platform(platform); 41 | assert(e == 0); 42 | 43 | e = uv_run(loop, UV_RUN_DEFAULT); 44 | assert(e == 0); 45 | } 46 | -------------------------------------------------------------------------------- /test/define-class-with-method.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static js_value_t * 8 | on_construct(js_env_t *env, js_callback_info_t *info) { 9 | return NULL; 10 | } 11 | 12 | static js_value_t * 13 | on_method(js_env_t *env, js_callback_info_t *info) { 14 | int e; 15 | 16 | js_value_t *result; 17 | e = js_create_uint32(env, 42, &result); 18 | assert(e == 0); 19 | 20 | return result; 21 | } 22 | 23 | int 24 | main() { 25 | int e; 26 | 27 | uv_loop_t *loop = uv_default_loop(); 28 | 29 | js_platform_t *platform; 30 | e = js_create_platform(loop, NULL, &platform); 31 | assert(e == 0); 32 | 33 | js_env_t *env; 34 | e = js_create_env(loop, platform, NULL, &env); 35 | assert(e == 0); 36 | 37 | js_handle_scope_t *scope; 38 | e = js_open_handle_scope(env, &scope); 39 | assert(e == 0); 40 | 41 | js_value_t *name; 42 | e = js_create_string_utf8(env, (utf8_t *) "foo", -1, &name); 43 | assert(e == 0); 44 | 45 | js_property_descriptor_t properties[] = { 46 | { 47 | .name = name, 48 | .method = on_method, 49 | }, 50 | }; 51 | 52 | js_value_t *class; 53 | e = js_define_class(env, "Foo", -1, on_construct, NULL, properties, 1, &class); 54 | assert(e == 0); 55 | 56 | js_value_t *instance; 57 | e = js_new_instance(env, class, 0, NULL, &instance); 58 | assert(e == 0); 59 | 60 | js_value_t *method; 61 | e = js_get_named_property(env, instance, "foo", &method); 62 | assert(e == 0); 63 | 64 | js_value_t *result; 65 | e = js_call_function(env, instance, method, 0, NULL, &result); 66 | assert(e == 0); 67 | 68 | uint32_t value; 69 | e = js_get_value_uint32(env, result, &value); 70 | assert(e == 0); 71 | 72 | assert(value == 42); 73 | 74 | e = js_close_handle_scope(env, scope); 75 | assert(e == 0); 76 | 77 | e = js_destroy_env(env); 78 | assert(e == 0); 79 | 80 | e = js_destroy_platform(platform); 81 | assert(e == 0); 82 | 83 | e = uv_run(loop, UV_RUN_DEFAULT); 84 | assert(e == 0); 85 | } 86 | -------------------------------------------------------------------------------- /test/define-class-with-static-method.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static js_value_t * 8 | on_construct(js_env_t *env, js_callback_info_t *info) { 9 | return NULL; 10 | } 11 | 12 | static js_value_t * 13 | on_method(js_env_t *env, js_callback_info_t *info) { 14 | int e; 15 | 16 | js_value_t *result; 17 | e = js_create_uint32(env, 42, &result); 18 | assert(e == 0); 19 | 20 | return result; 21 | } 22 | 23 | int 24 | main() { 25 | int e; 26 | 27 | uv_loop_t *loop = uv_default_loop(); 28 | 29 | js_platform_t *platform; 30 | e = js_create_platform(loop, NULL, &platform); 31 | assert(e == 0); 32 | 33 | js_env_t *env; 34 | e = js_create_env(loop, platform, NULL, &env); 35 | assert(e == 0); 36 | 37 | js_handle_scope_t *scope; 38 | e = js_open_handle_scope(env, &scope); 39 | assert(e == 0); 40 | 41 | js_value_t *name; 42 | e = js_create_string_utf8(env, (utf8_t *) "foo", -1, &name); 43 | assert(e == 0); 44 | 45 | js_property_descriptor_t properties[] = { 46 | { 47 | .name = name, 48 | .method = on_method, 49 | .attributes = js_static, 50 | }, 51 | }; 52 | 53 | js_value_t *class; 54 | e = js_define_class(env, "Foo", -1, on_construct, NULL, properties, 1, &class); 55 | assert(e == 0); 56 | 57 | js_value_t *method; 58 | e = js_get_named_property(env, class, "foo", &method); 59 | assert(e == 0); 60 | 61 | js_value_t *result; 62 | e = js_call_function(env, class, method, 0, NULL, &result); 63 | assert(e == 0); 64 | 65 | uint32_t value; 66 | e = js_get_value_uint32(env, result, &value); 67 | assert(e == 0); 68 | 69 | assert(value == 42); 70 | 71 | e = js_close_handle_scope(env, scope); 72 | assert(e == 0); 73 | 74 | e = js_destroy_env(env); 75 | assert(e == 0); 76 | 77 | e = js_destroy_platform(platform); 78 | assert(e == 0); 79 | 80 | e = uv_run(loop, UV_RUN_DEFAULT); 81 | assert(e == 0); 82 | } 83 | -------------------------------------------------------------------------------- /test/define-class-with-static-value.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static js_value_t * 8 | on_construct(js_env_t *env, js_callback_info_t *info) { 9 | return NULL; 10 | } 11 | 12 | int 13 | main() { 14 | int e; 15 | 16 | uv_loop_t *loop = uv_default_loop(); 17 | 18 | js_platform_t *platform; 19 | e = js_create_platform(loop, NULL, &platform); 20 | assert(e == 0); 21 | 22 | js_env_t *env; 23 | e = js_create_env(loop, platform, NULL, &env); 24 | assert(e == 0); 25 | 26 | js_handle_scope_t *scope; 27 | e = js_open_handle_scope(env, &scope); 28 | assert(e == 0); 29 | 30 | js_value_t *property; 31 | e = js_create_uint32(env, 42, &property); 32 | assert(e == 0); 33 | 34 | js_value_t *name; 35 | e = js_create_string_utf8(env, (utf8_t *) "foo", -1, &name); 36 | assert(e == 0); 37 | 38 | js_property_descriptor_t properties[] = { 39 | { 40 | .name = name, 41 | .value = property, 42 | .attributes = js_static, 43 | }, 44 | }; 45 | 46 | js_value_t *class; 47 | e = js_define_class(env, "Foo", -1, on_construct, NULL, properties, 1, &class); 48 | assert(e == 0); 49 | 50 | js_value_t *result; 51 | e = js_get_named_property(env, class, "foo", &result); 52 | assert(e == 0); 53 | 54 | uint32_t value; 55 | e = js_get_value_uint32(env, result, &value); 56 | assert(e == 0); 57 | 58 | assert(value == 42); 59 | 60 | e = js_close_handle_scope(env, scope); 61 | assert(e == 0); 62 | 63 | e = js_destroy_env(env); 64 | assert(e == 0); 65 | 66 | e = js_destroy_platform(platform); 67 | assert(e == 0); 68 | 69 | e = uv_run(loop, UV_RUN_DEFAULT); 70 | assert(e == 0); 71 | } 72 | -------------------------------------------------------------------------------- /test/define-class-with-symbol-property.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static js_value_t * 8 | on_construct(js_env_t *env, js_callback_info_t *info) { 9 | return NULL; 10 | } 11 | 12 | int 13 | main() { 14 | int e; 15 | 16 | uv_loop_t *loop = uv_default_loop(); 17 | 18 | js_platform_t *platform; 19 | e = js_create_platform(loop, NULL, &platform); 20 | assert(e == 0); 21 | 22 | js_env_t *env; 23 | e = js_create_env(loop, platform, NULL, &env); 24 | assert(e == 0); 25 | 26 | js_handle_scope_t *scope; 27 | e = js_open_handle_scope(env, &scope); 28 | assert(e == 0); 29 | 30 | js_value_t *name; 31 | e = js_create_symbol(env, NULL, &name); 32 | assert(e == 0); 33 | 34 | js_value_t *property; 35 | e = js_create_uint32(env, 42, &property); 36 | assert(e == 0); 37 | 38 | js_property_descriptor_t properties[] = { 39 | { 40 | .name = name, 41 | .value = property, 42 | }, 43 | }; 44 | 45 | js_value_t *class; 46 | e = js_define_class(env, "Foo", -1, on_construct, NULL, properties, 1, &class); 47 | assert(e == 0); 48 | 49 | js_value_t *instance; 50 | e = js_new_instance(env, class, 0, NULL, &instance); 51 | assert(e == 0); 52 | 53 | js_value_t *result; 54 | e = js_get_property(env, instance, name, &result); 55 | assert(e == 0); 56 | 57 | uint32_t value; 58 | e = js_get_value_uint32(env, result, &value); 59 | assert(e == 0); 60 | 61 | assert(value == 42); 62 | 63 | e = js_close_handle_scope(env, scope); 64 | assert(e == 0); 65 | 66 | e = js_destroy_env(env); 67 | assert(e == 0); 68 | 69 | e = js_destroy_platform(platform); 70 | assert(e == 0); 71 | 72 | e = uv_run(loop, UV_RUN_DEFAULT); 73 | assert(e == 0); 74 | } 75 | -------------------------------------------------------------------------------- /test/define-class-with-value.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static js_value_t * 8 | on_construct(js_env_t *env, js_callback_info_t *info) { 9 | return NULL; 10 | } 11 | 12 | int 13 | main() { 14 | int e; 15 | 16 | uv_loop_t *loop = uv_default_loop(); 17 | 18 | js_platform_t *platform; 19 | e = js_create_platform(loop, NULL, &platform); 20 | assert(e == 0); 21 | 22 | js_env_t *env; 23 | e = js_create_env(loop, platform, NULL, &env); 24 | assert(e == 0); 25 | 26 | js_handle_scope_t *scope; 27 | e = js_open_handle_scope(env, &scope); 28 | assert(e == 0); 29 | 30 | js_value_t *name; 31 | e = js_create_string_utf8(env, (utf8_t *) "foo", -1, &name); 32 | assert(e == 0); 33 | 34 | js_value_t *property; 35 | e = js_create_uint32(env, 42, &property); 36 | assert(e == 0); 37 | 38 | js_property_descriptor_t properties[] = { 39 | { 40 | .name = name, 41 | .value = property, 42 | }, 43 | }; 44 | 45 | js_value_t *class; 46 | e = js_define_class(env, "Foo", -1, on_construct, NULL, properties, 1, &class); 47 | assert(e == 0); 48 | 49 | js_value_t *instance; 50 | e = js_new_instance(env, class, 0, NULL, &instance); 51 | assert(e == 0); 52 | 53 | js_value_t *result; 54 | e = js_get_named_property(env, instance, "foo", &result); 55 | assert(e == 0); 56 | 57 | uint32_t value; 58 | e = js_get_value_uint32(env, result, &value); 59 | assert(e == 0); 60 | 61 | assert(value == 42); 62 | 63 | e = js_close_handle_scope(env, scope); 64 | assert(e == 0); 65 | 66 | e = js_destroy_env(env); 67 | assert(e == 0); 68 | 69 | e = js_destroy_platform(platform); 70 | assert(e == 0); 71 | 72 | e = uv_run(loop, UV_RUN_DEFAULT); 73 | assert(e == 0); 74 | } 75 | -------------------------------------------------------------------------------- /test/define-class.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static int constructor_called = 0; 8 | 9 | static js_value_t * 10 | on_construct(js_env_t *env, js_callback_info_t *info) { 11 | constructor_called++; 12 | 13 | return NULL; 14 | } 15 | 16 | int 17 | main() { 18 | int e; 19 | 20 | uv_loop_t *loop = uv_default_loop(); 21 | 22 | js_platform_t *platform; 23 | e = js_create_platform(loop, NULL, &platform); 24 | assert(e == 0); 25 | 26 | js_env_t *env; 27 | e = js_create_env(loop, platform, NULL, &env); 28 | assert(e == 0); 29 | 30 | js_handle_scope_t *scope; 31 | e = js_open_handle_scope(env, &scope); 32 | assert(e == 0); 33 | 34 | js_value_t *class; 35 | e = js_define_class(env, "Foo", -1, on_construct, NULL, NULL, 0, &class); 36 | assert(e == 0); 37 | 38 | js_value_t *instance; 39 | e = js_new_instance(env, class, 0, NULL, &instance); 40 | assert(e == 0); 41 | 42 | assert(constructor_called == 1); 43 | 44 | bool result; 45 | e = js_instanceof(env, instance, class, &result); 46 | assert(e == 0); 47 | 48 | assert(result); 49 | 50 | e = js_close_handle_scope(env, scope); 51 | assert(e == 0); 52 | 53 | e = js_destroy_env(env); 54 | assert(e == 0); 55 | 56 | e = js_destroy_platform(platform); 57 | assert(e == 0); 58 | 59 | e = uv_run(loop, UV_RUN_DEFAULT); 60 | assert(e == 0); 61 | } 62 | -------------------------------------------------------------------------------- /test/dynamic-import-without-handler.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int uncaught_called = 0; 8 | 9 | static void 10 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 11 | uncaught_called++; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_t *platform; 21 | e = js_create_platform(loop, NULL, &platform); 22 | assert(e == 0); 23 | 24 | js_env_t *env; 25 | e = js_create_env(loop, platform, NULL, &env); 26 | assert(e == 0); 27 | 28 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 29 | assert(e == 0); 30 | 31 | js_handle_scope_t *scope; 32 | e = js_open_handle_scope(env, &scope); 33 | assert(e == 0); 34 | 35 | js_value_t *script; 36 | e = js_create_string_utf8(env, (utf8_t *) "import('foo.js')", -1, &script); 37 | assert(e == 0); 38 | 39 | js_value_t *result; 40 | e = js_run_script(env, "test.js", -1, 0, script, &result); 41 | 42 | // The engine may fail early during script evaluation. 43 | if (e != 0) assert(uncaught_called == 1); 44 | 45 | // Otherwise, it must reject the import promise. 46 | else { 47 | js_promise_state_t state; 48 | e = js_get_promise_state(env, result, &state); 49 | assert(e == 0); 50 | 51 | assert(state == js_promise_rejected); 52 | } 53 | 54 | e = js_close_handle_scope(env, scope); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | } 66 | -------------------------------------------------------------------------------- /test/dynamic-import.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static js_module_t *module; 8 | 9 | static void 10 | on_module_evaluate(js_env_t *env, js_module_t *module, void *data) { 11 | int e; 12 | 13 | js_value_t *name; 14 | e = js_create_string_utf8(env, (utf8_t *) "foo", -1, &name); 15 | assert(e == 0); 16 | 17 | js_value_t *value; 18 | e = js_create_uint32(env, 42, &value); 19 | assert(e == 0); 20 | 21 | e = js_set_module_export(env, module, name, value); 22 | assert(e == 0); 23 | } 24 | 25 | static js_module_t * 26 | on_import(js_env_t *env, js_value_t *specifier, js_value_t *assertions, js_value_t *referrer, void *data) { 27 | int e; 28 | 29 | js_value_t *export_names[1]; 30 | e = js_create_string_utf8(env, (utf8_t *) "foo", -1, &export_names[0]); 31 | assert(e == 0); 32 | 33 | e = js_create_synthetic_module(env, "synthetic", -1, export_names, 1, on_module_evaluate, NULL, &module); 34 | assert(e == 0); 35 | 36 | e = js_instantiate_module(env, module, NULL, NULL); 37 | assert(e == 0); 38 | 39 | return module; 40 | } 41 | 42 | int 43 | main() { 44 | int e; 45 | 46 | uv_loop_t *loop = uv_default_loop(); 47 | 48 | js_platform_t *platform; 49 | e = js_create_platform(loop, NULL, &platform); 50 | assert(e == 0); 51 | 52 | js_env_t *env; 53 | e = js_create_env(loop, platform, NULL, &env); 54 | assert(e == 0); 55 | 56 | e = js_on_dynamic_import(env, on_import, NULL); 57 | assert(e == 0); 58 | 59 | js_handle_scope_t *scope; 60 | e = js_open_handle_scope(env, &scope); 61 | assert(e == 0); 62 | 63 | js_value_t *script; 64 | e = js_create_string_utf8(env, (utf8_t *) "const foo = import('foo.js')", -1, &script); 65 | assert(e == 0); 66 | 67 | js_value_t *result; 68 | e = js_run_script(env, "test.js", -1, 0, script, &result); 69 | assert(e == 0); 70 | 71 | e = js_delete_module(env, module); 72 | assert(e == 0); 73 | 74 | e = js_close_handle_scope(env, scope); 75 | assert(e == 0); 76 | 77 | e = js_destroy_env(env); 78 | assert(e == 0); 79 | 80 | e = js_destroy_platform(platform); 81 | assert(e == 0); 82 | 83 | e = uv_run(loop, UV_RUN_DEFAULT); 84 | assert(e == 0); 85 | } 86 | -------------------------------------------------------------------------------- /test/fatal-exception.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int uncaught_called = 0; 8 | 9 | static void 10 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 11 | uncaught_called++; 12 | } 13 | 14 | int 15 | main() { 16 | int e; 17 | 18 | uv_loop_t *loop = uv_default_loop(); 19 | 20 | js_platform_t *platform; 21 | e = js_create_platform(loop, NULL, &platform); 22 | assert(e == 0); 23 | 24 | js_env_t *env; 25 | e = js_create_env(loop, platform, NULL, &env); 26 | assert(e == 0); 27 | 28 | js_handle_scope_t *scope; 29 | e = js_open_handle_scope(env, &scope); 30 | assert(e == 0); 31 | 32 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 33 | assert(e == 0); 34 | 35 | js_value_t *error; 36 | e = js_create_string_utf8(env, (utf8_t *) "err", -1, &error); 37 | assert(e == 0); 38 | 39 | e = js_fatal_exception(env, error); 40 | assert(e == 0); 41 | 42 | assert(uncaught_called == 1); 43 | 44 | e = js_close_handle_scope(env, scope); 45 | assert(e == 0); 46 | 47 | e = js_destroy_env(env); 48 | assert(e == 0); 49 | 50 | e = js_destroy_platform(platform); 51 | assert(e == 0); 52 | 53 | e = uv_run(loop, UV_RUN_DEFAULT); 54 | assert(e == 0); 55 | } 56 | -------------------------------------------------------------------------------- /test/fixtures/Makefile: -------------------------------------------------------------------------------- 1 | %.h: % 2 | xxd -i $^ > $@ 3 | clang-format -i $@ 4 | 5 | all: \ 6 | atomics-wait-timeout.js.h \ 7 | atomics-wait-timeout-notify.js.h \ 8 | many-large-allocs.js.h \ 9 | many-small-allocs.js.h \ 10 | promise-rejection.js.h \ 11 | promise-rejection-unhandled.js.h \ 12 | promise-rejection-unhandled-deferred.js.h \ 13 | wasm-async.js.h \ 14 | wasm-async-log.js.h \ 15 | wasm-sync.js.h 16 | -------------------------------------------------------------------------------- /test/fixtures/atomics-wait-timeout-notify.js: -------------------------------------------------------------------------------- 1 | const buffer = new SharedArrayBuffer(4) 2 | const view = new Int32Array(buffer) 3 | 4 | const value = Atomics 5 | .waitAsync(view, 0, 0, 10000 /* Noticeable timeout */) 6 | .value 7 | 8 | Atomics.notify(view, 0, 1); 9 | 10 | value 11 | -------------------------------------------------------------------------------- /test/fixtures/atomics-wait-timeout-notify.js.h: -------------------------------------------------------------------------------- 1 | unsigned char atomics_wait_timeout_notify_js[] = { 2 | 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x53, 0x68, 0x61, 0x72, 0x65, 0x64, 0x41, 0x72, 0x72, 0x61, 0x79, 0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0x28, 0x34, 0x29, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x76, 0x69, 0x65, 0x77, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x29, 0x0a, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x20, 0x3d, 0x20, 0x41, 0x74, 0x6f, 0x6d, 0x69, 0x63, 0x73, 0x0a, 0x20, 0x20, 0x2e, 0x77, 0x61, 0x69, 0x74, 0x41, 0x73, 0x79, 0x6e, 0x63, 0x28, 0x76, 0x69, 0x65, 0x77, 0x2c, 0x20, 0x30, 0x2c, 0x20, 0x30, 0x2c, 0x20, 0x31, 0x30, 0x30, 0x30, 0x30, 0x20, 0x2f, 0x2a, 0x20, 0x4e, 0x6f, 0x74, 0x69, 0x63, 0x65, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x20, 0x2a, 0x2f, 0x29, 0x0a, 0x20, 0x20, 0x2e, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x0a, 0x0a, 0x41, 0x74, 0x6f, 0x6d, 0x69, 0x63, 0x73, 0x2e, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x28, 0x76, 0x69, 0x65, 0x77, 0x2c, 0x20, 0x30, 0x2c, 0x20, 0x31, 0x29, 0x3b, 0x0a, 0x0a, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x0a 3 | }; 4 | unsigned int atomics_wait_timeout_notify_js_len = 201; 5 | -------------------------------------------------------------------------------- /test/fixtures/atomics-wait-timeout.js: -------------------------------------------------------------------------------- 1 | const buffer = new SharedArrayBuffer(4) 2 | const view = new Int32Array(buffer) 3 | 4 | Atomics 5 | .waitAsync(view, 0, 0, 100) 6 | .value 7 | -------------------------------------------------------------------------------- /test/fixtures/atomics-wait-timeout.js.h: -------------------------------------------------------------------------------- 1 | unsigned char atomics_wait_timeout_js[] = { 2 | 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x53, 0x68, 0x61, 0x72, 0x65, 0x64, 0x41, 0x72, 0x72, 0x61, 0x79, 0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0x28, 0x34, 0x29, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x76, 0x69, 0x65, 0x77, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x29, 0x0a, 0x0a, 0x41, 0x74, 0x6f, 0x6d, 0x69, 0x63, 0x73, 0x0a, 0x20, 0x20, 0x2e, 0x77, 0x61, 0x69, 0x74, 0x41, 0x73, 0x79, 0x6e, 0x63, 0x28, 0x76, 0x69, 0x65, 0x77, 0x2c, 0x20, 0x30, 0x2c, 0x20, 0x30, 0x2c, 0x20, 0x31, 0x30, 0x30, 0x29, 0x0a, 0x20, 0x20, 0x2e, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x0a 3 | }; 4 | unsigned int atomics_wait_timeout_js_len = 124; 5 | -------------------------------------------------------------------------------- /test/fixtures/many-large-allocs.js: -------------------------------------------------------------------------------- 1 | let r 2 | 3 | for (let i = 0; i < 1e6; i++) { 4 | r = new Uint8Array(1024 * 64) 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/many-large-allocs.js.h: -------------------------------------------------------------------------------- 1 | unsigned char many_large_allocs_js[] = { 2 | 0x6c, 0x65, 0x74, 0x20, 0x72, 0x0a, 0x0a, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x6c, 0x65, 0x74, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, 0x3b, 0x20, 0x69, 0x20, 0x3c, 0x20, 0x31, 0x65, 0x36, 0x3b, 0x20, 0x69, 0x2b, 0x2b, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x72, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x55, 0x69, 0x6e, 0x74, 0x38, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x31, 0x30, 0x32, 0x34, 0x20, 0x2a, 0x20, 0x36, 0x34, 0x29, 0x0a, 0x7d, 0x0a 3 | }; 4 | unsigned int many_large_allocs_js_len = 73; 5 | -------------------------------------------------------------------------------- /test/fixtures/many-small-allocs.js: -------------------------------------------------------------------------------- 1 | let r 2 | 3 | for (let i = 0; i < 1e6; i++) { 4 | r = new Uint8Array(1024) 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/many-small-allocs.js.h: -------------------------------------------------------------------------------- 1 | unsigned char many_small_allocs_js[] = { 2 | 0x6c, 0x65, 0x74, 0x20, 0x72, 0x0a, 0x0a, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x6c, 0x65, 0x74, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, 0x3b, 0x20, 0x69, 0x20, 0x3c, 0x20, 0x31, 0x65, 0x36, 0x3b, 0x20, 0x69, 0x2b, 0x2b, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x72, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x55, 0x69, 0x6e, 0x74, 0x38, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x31, 0x30, 0x32, 0x34, 0x29, 0x0a, 0x7d, 0x0a 3 | }; 4 | unsigned int many_small_allocs_js_len = 68; 5 | -------------------------------------------------------------------------------- /test/fixtures/promise-rejection-unhandled-deferred.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => Promise.reject('err')) 2 | -------------------------------------------------------------------------------- /test/fixtures/promise-rejection-unhandled-deferred.js.h: -------------------------------------------------------------------------------- 1 | unsigned char promise_rejection_unhandled_deferred_js[] = { 2 | 0x50, 0x72, 0x6f, 0x6d, 0x69, 0x73, 0x65, 0x2e, 0x72, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x28, 0x29, 0x2e, 0x74, 0x68, 0x65, 0x6e, 0x28, 0x28, 0x29, 0x20, 0x3d, 0x3e, 0x20, 0x50, 0x72, 0x6f, 0x6d, 0x69, 0x73, 0x65, 0x2e, 0x72, 0x65, 0x6a, 0x65, 0x63, 0x74, 0x28, 0x27, 0x65, 0x72, 0x72, 0x27, 0x29, 0x29, 0x0a 3 | }; 4 | unsigned int promise_rejection_unhandled_deferred_js_len = 52; 5 | -------------------------------------------------------------------------------- /test/fixtures/promise-rejection-unhandled.js: -------------------------------------------------------------------------------- 1 | Promise.reject('err') 2 | -------------------------------------------------------------------------------- /test/fixtures/promise-rejection-unhandled.js.h: -------------------------------------------------------------------------------- 1 | unsigned char promise_rejection_unhandled_js[] = { 2 | 0x50, 0x72, 0x6f, 0x6d, 0x69, 0x73, 0x65, 0x2e, 0x72, 0x65, 0x6a, 0x65, 0x63, 0x74, 0x28, 0x27, 0x65, 0x72, 0x72, 0x27, 0x29, 0x0a 3 | }; 4 | unsigned int promise_rejection_unhandled_js_len = 22; 5 | -------------------------------------------------------------------------------- /test/fixtures/promise-rejection.js: -------------------------------------------------------------------------------- 1 | Promise.reject('err').catch(() => {}) 2 | -------------------------------------------------------------------------------- /test/fixtures/promise-rejection.js.h: -------------------------------------------------------------------------------- 1 | unsigned char promise_rejection_js[] = { 2 | 0x50, 0x72, 0x6f, 0x6d, 0x69, 0x73, 0x65, 0x2e, 0x72, 0x65, 0x6a, 0x65, 0x63, 0x74, 0x28, 0x27, 0x65, 0x72, 0x72, 0x27, 0x29, 0x2e, 0x63, 0x61, 0x74, 0x63, 0x68, 0x28, 0x28, 0x29, 0x20, 0x3d, 0x3e, 0x20, 0x7b, 0x7d, 0x29, 0x0a 3 | }; 4 | unsigned int promise_rejection_js_len = 38; 5 | -------------------------------------------------------------------------------- /test/fixtures/wasm-async-log.js: -------------------------------------------------------------------------------- 1 | const code = new Uint8Array([ 2 | 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 3 | 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 4 | 0x6e, 0x00, 0x00, 0x0a, 0x06, 0x01, 0x04, 0x00, 0x41, 0x2a, 0x0b 5 | ]) 6 | 7 | WebAssembly 8 | .instantiate(code) 9 | .then(({ instance: { exports } }) => 10 | log(exports.main()) 11 | ) 12 | -------------------------------------------------------------------------------- /test/fixtures/wasm-async-log.js.h: -------------------------------------------------------------------------------- 1 | unsigned char wasm_async_log_js[] = { 2 | 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x55, 0x69, 0x6e, 0x74, 0x38, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x5b, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x37, 0x33, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x64, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x35, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x37, 0x66, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x33, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x32, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x37, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x38, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x34, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x64, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x39, 0x2c, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x36, 0x65, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x61, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x36, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x34, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x34, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x32, 0x61, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x62, 0x0a, 0x5d, 0x29, 0x0a, 0x0a, 0x57, 0x65, 0x62, 0x41, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x0a, 0x20, 0x20, 0x2e, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x69, 0x61, 0x74, 0x65, 0x28, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, 0x20, 0x20, 0x2e, 0x74, 0x68, 0x65, 0x6e, 0x28, 0x28, 0x7b, 0x20, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x3a, 0x20, 0x7b, 0x20, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x20, 0x7d, 0x20, 0x7d, 0x29, 0x20, 0x3d, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x6f, 0x67, 0x28, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x29, 0x0a, 0x20, 0x20, 0x29, 0x0a 3 | }; 4 | unsigned int wasm_async_log_js_len = 361; 5 | -------------------------------------------------------------------------------- /test/fixtures/wasm-async.js: -------------------------------------------------------------------------------- 1 | const code = new Uint8Array([ 2 | 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 3 | 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 4 | 0x6e, 0x00, 0x00, 0x0a, 0x06, 0x01, 0x04, 0x00, 0x41, 0x2a, 0x0b 5 | ]) 6 | 7 | WebAssembly 8 | .instantiate(code) 9 | .then(({ instance: { exports } }) => 10 | exports.main() 11 | ) 12 | -------------------------------------------------------------------------------- /test/fixtures/wasm-async.js.h: -------------------------------------------------------------------------------- 1 | unsigned char wasm_async_js[] = { 2 | 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x55, 0x69, 0x6e, 0x74, 0x38, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x5b, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x37, 0x33, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x64, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x35, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x37, 0x66, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x33, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x32, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x37, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x38, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x34, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x64, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x39, 0x2c, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x36, 0x65, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x61, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x36, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x34, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x34, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x32, 0x61, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x62, 0x0a, 0x5d, 0x29, 0x0a, 0x0a, 0x57, 0x65, 0x62, 0x41, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x0a, 0x20, 0x20, 0x2e, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x69, 0x61, 0x74, 0x65, 0x28, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, 0x20, 0x20, 0x2e, 0x74, 0x68, 0x65, 0x6e, 0x28, 0x28, 0x7b, 0x20, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x3a, 0x20, 0x7b, 0x20, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x20, 0x7d, 0x20, 0x7d, 0x29, 0x20, 0x3d, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a, 0x20, 0x20, 0x29, 0x0a 3 | }; 4 | unsigned int wasm_async_js_len = 356; 5 | -------------------------------------------------------------------------------- /test/fixtures/wasm-sync.js: -------------------------------------------------------------------------------- 1 | const code = new Uint8Array([ 2 | 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 3 | 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 4 | 0x6e, 0x00, 0x00, 0x0a, 0x06, 0x01, 0x04, 0x00, 0x41, 0x2a, 0x0b 5 | ]) 6 | 7 | const { exports } = new WebAssembly.Instance(new WebAssembly.Module(code)) 8 | 9 | exports.main() 10 | -------------------------------------------------------------------------------- /test/fixtures/wasm-sync.js.h: -------------------------------------------------------------------------------- 1 | unsigned char wasm_sync_js[] = { 2 | 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x55, 0x69, 0x6e, 0x74, 0x38, 0x41, 0x72, 0x72, 0x61, 0x79, 0x28, 0x5b, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x37, 0x33, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x64, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x35, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x37, 0x66, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x33, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x32, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x37, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x38, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x34, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x64, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x36, 0x39, 0x2c, 0x0a, 0x20, 0x20, 0x30, 0x78, 0x36, 0x65, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x61, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x36, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x34, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x30, 0x2c, 0x20, 0x30, 0x78, 0x34, 0x31, 0x2c, 0x20, 0x30, 0x78, 0x32, 0x61, 0x2c, 0x20, 0x30, 0x78, 0x30, 0x62, 0x0a, 0x5d, 0x29, 0x0a, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x7b, 0x20, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x20, 0x7d, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x57, 0x65, 0x62, 0x41, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x2e, 0x49, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x28, 0x6e, 0x65, 0x77, 0x20, 0x57, 0x65, 0x62, 0x41, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x2e, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x28, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x29, 0x0a, 0x0a, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x73, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a 3 | }; 4 | unsigned int wasm_sync_js_len = 352; 5 | -------------------------------------------------------------------------------- /test/fixtures/wasm.wat: -------------------------------------------------------------------------------- 1 | (module 2 | (func (export "main") (result i32) 3 | i32.const 42 4 | ) 5 | ) 6 | -------------------------------------------------------------------------------- /test/get-arraybuffer-info.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "Uint8Array.from([1, 2, 3, 4]).buffer", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *arraybuffer; 30 | e = js_run_script(env, NULL, 0, 0, script, &arraybuffer); 31 | assert(e == 0); 32 | 33 | uint8_t *data; 34 | size_t len; 35 | e = js_get_arraybuffer_info(env, arraybuffer, (void **) &data, &len); 36 | assert(e == 0); 37 | 38 | assert(len == 4); 39 | 40 | assert(data[0] == 1); 41 | assert(data[1] == 2); 42 | assert(data[2] == 3); 43 | assert(data[3] == 4); 44 | 45 | e = js_close_handle_scope(env, scope); 46 | assert(e == 0); 47 | 48 | e = js_destroy_env(env); 49 | assert(e == 0); 50 | 51 | e = js_destroy_platform(platform); 52 | assert(e == 0); 53 | 54 | e = uv_run(loop, UV_RUN_DEFAULT); 55 | assert(e == 0); 56 | } 57 | -------------------------------------------------------------------------------- /test/get-dataview-info.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "new DataView(Uint8Array.from([1, 2, 3, 4]).buffer)", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *dataview; 30 | e = js_run_script(env, NULL, 0, 0, script, &dataview); 31 | assert(e == 0); 32 | 33 | uint8_t *data; 34 | size_t len; 35 | js_value_t *arraybuffer; 36 | size_t offset; 37 | e = js_get_dataview_info(env, dataview, (void **) &data, &len, &arraybuffer, &offset); 38 | assert(e == 0); 39 | 40 | assert(len == 4); 41 | assert(offset == 0); 42 | 43 | assert(data[0] == 1); 44 | assert(data[1] == 2); 45 | assert(data[2] == 3); 46 | assert(data[3] == 4); 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/get-platform-identifier.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | const char *identifier; 17 | e = js_get_platform_identifier(platform, &identifier); 18 | assert(e == 0); 19 | 20 | printf("identifier=%s\n", identifier); 21 | 22 | e = js_destroy_platform(platform); 23 | assert(e == 0); 24 | 25 | e = uv_run(loop, UV_RUN_DEFAULT); 26 | assert(e == 0); 27 | } 28 | -------------------------------------------------------------------------------- /test/get-platform-limits.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_platform_limits_t limits = {.version = 0}; 17 | e = js_get_platform_limits(platform, &limits); 18 | assert(e == 0); 19 | 20 | printf("limits.arraybuffer_length=%lu\n", limits.arraybuffer_length); 21 | printf("limits.string_length=%lu\n", limits.string_length); 22 | 23 | e = js_destroy_platform(platform); 24 | assert(e == 0); 25 | 26 | e = uv_run(loop, UV_RUN_DEFAULT); 27 | assert(e == 0); 28 | } 29 | -------------------------------------------------------------------------------- /test/get-platform-version.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | const char *version; 17 | e = js_get_platform_version(platform, &version); 18 | assert(e == 0); 19 | 20 | printf("version=%s\n", version); 21 | 22 | e = js_destroy_platform(platform); 23 | assert(e == 0); 24 | 25 | e = uv_run(loop, UV_RUN_DEFAULT); 26 | assert(e == 0); 27 | } 28 | -------------------------------------------------------------------------------- /test/get-property-missing.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *object; 26 | e = js_create_object(env, &object); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_get_named_property(env, object, "foo", &value); 31 | assert(e == 0); 32 | 33 | bool is_undefined; 34 | e = js_is_undefined(env, value, &is_undefined); 35 | 36 | assert(is_undefined); 37 | 38 | e = js_close_handle_scope(env, scope); 39 | assert(e == 0); 40 | 41 | e = js_destroy_env(env); 42 | assert(e == 0); 43 | 44 | e = js_destroy_platform(platform); 45 | assert(e == 0); 46 | 47 | e = uv_run(loop, UV_RUN_DEFAULT); 48 | assert(e == 0); 49 | } 50 | -------------------------------------------------------------------------------- /test/get-typedarray-info-uint16array-large.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "new Uint16Array(1024).fill(42)", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *typedarray; 30 | e = js_run_script(env, NULL, 0, 0, script, &typedarray); 31 | assert(e == 0); 32 | 33 | js_typedarray_type_t type; 34 | uint16_t *data; 35 | size_t len; 36 | size_t offset; 37 | e = js_get_typedarray_info(env, typedarray, &type, (void **) &data, &len, NULL, &offset); 38 | assert(e == 0); 39 | 40 | assert(type == js_uint16array); 41 | assert(len == 1024); 42 | assert(offset == 0); 43 | 44 | for (int i = 0; i < len; i++) { 45 | assert(data[i] == 42); 46 | } 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/get-typedarray-info-uint16array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "Uint16Array.from([1, 2, 3, 4])", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *typedarray; 30 | e = js_run_script(env, NULL, 0, 0, script, &typedarray); 31 | assert(e == 0); 32 | 33 | js_typedarray_type_t type; 34 | uint16_t *data; 35 | size_t len; 36 | js_value_t *arraybuffer; 37 | size_t offset; 38 | e = js_get_typedarray_info(env, typedarray, &type, (void **) &data, &len, &arraybuffer, &offset); 39 | assert(e == 0); 40 | 41 | assert(type == js_uint16array); 42 | assert(len == 4); 43 | assert(offset == 0); 44 | 45 | assert(data[0] == 1); 46 | assert(data[1] == 2); 47 | assert(data[2] == 3); 48 | assert(data[3] == 4); 49 | 50 | e = js_close_handle_scope(env, scope); 51 | assert(e == 0); 52 | 53 | e = js_destroy_env(env); 54 | assert(e == 0); 55 | 56 | e = js_destroy_platform(platform); 57 | assert(e == 0); 58 | 59 | e = uv_run(loop, UV_RUN_DEFAULT); 60 | assert(e == 0); 61 | } 62 | -------------------------------------------------------------------------------- /test/get-typedarray-info-uint8array-large.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "new Uint8Array(1024).fill(42)", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *typedarray; 30 | e = js_run_script(env, NULL, 0, 0, script, &typedarray); 31 | assert(e == 0); 32 | 33 | js_typedarray_type_t type; 34 | uint8_t *data; 35 | size_t len; 36 | size_t offset; 37 | e = js_get_typedarray_info(env, typedarray, &type, (void **) &data, &len, NULL, &offset); 38 | assert(e == 0); 39 | 40 | assert(type == js_uint8array); 41 | assert(len == 1024); 42 | assert(offset == 0); 43 | 44 | for (int i = 0; i < len; i++) { 45 | assert(data[i] == 42); 46 | } 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | -------------------------------------------------------------------------------- /test/get-typedarray-info-uint8array-with-offset.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "Uint8Array.from([1, 2, 3, 4, 5, 6]).subarray(2)", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *typedarray; 30 | e = js_run_script(env, NULL, 0, 0, script, &typedarray); 31 | assert(e == 0); 32 | 33 | js_typedarray_type_t type; 34 | uint8_t *data; 35 | size_t len; 36 | js_value_t *arraybuffer; 37 | size_t offset; 38 | e = js_get_typedarray_info(env, typedarray, &type, (void **) &data, &len, &arraybuffer, &offset); 39 | assert(e == 0); 40 | 41 | assert(type == js_uint8array); 42 | assert(len == 4); 43 | assert(offset == 2); 44 | 45 | assert(data[0] == 3); 46 | assert(data[1] == 4); 47 | assert(data[2] == 5); 48 | assert(data[3] == 6); 49 | 50 | e = js_close_handle_scope(env, scope); 51 | assert(e == 0); 52 | 53 | e = js_destroy_env(env); 54 | assert(e == 0); 55 | 56 | e = js_destroy_platform(platform); 57 | assert(e == 0); 58 | 59 | e = uv_run(loop, UV_RUN_DEFAULT); 60 | assert(e == 0); 61 | } 62 | -------------------------------------------------------------------------------- /test/get-typedarray-info-uint8array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "Uint8Array.from([1, 2, 3, 4])", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *typedarray; 30 | e = js_run_script(env, NULL, 0, 0, script, &typedarray); 31 | assert(e == 0); 32 | 33 | js_typedarray_type_t type; 34 | uint8_t *data; 35 | size_t len; 36 | js_value_t *arraybuffer; 37 | size_t offset; 38 | e = js_get_typedarray_info(env, typedarray, &type, (void **) &data, &len, &arraybuffer, &offset); 39 | assert(e == 0); 40 | 41 | assert(type == js_uint8array); 42 | assert(len == 4); 43 | assert(offset == 0); 44 | 45 | assert(data[0] == 1); 46 | assert(data[1] == 2); 47 | assert(data[2] == 3); 48 | assert(data[3] == 4); 49 | 50 | e = js_close_handle_scope(env, scope); 51 | assert(e == 0); 52 | 53 | e = js_destroy_env(env); 54 | assert(e == 0); 55 | 56 | e = js_destroy_platform(platform); 57 | assert(e == 0); 58 | 59 | e = uv_run(loop, UV_RUN_DEFAULT); 60 | assert(e == 0); 61 | } 62 | -------------------------------------------------------------------------------- /test/get-value-string-utf8-length.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *string; 27 | e = js_create_string_utf8(env, (utf8_t *) "hello", -1, &string); 28 | assert(e == 0); 29 | 30 | size_t len; 31 | e = js_get_value_string_utf8(env, string, NULL, 0, &len); 32 | assert(e == 0); 33 | 34 | assert(len >= 5); 35 | 36 | e = js_close_handle_scope(env, scope); 37 | assert(e == 0); 38 | 39 | e = js_destroy_env(env); 40 | assert(e == 0); 41 | 42 | e = js_destroy_platform(platform); 43 | assert(e == 0); 44 | 45 | e = uv_run(loop, UV_RUN_DEFAULT); 46 | assert(e == 0); 47 | } 48 | -------------------------------------------------------------------------------- /test/get-value-string-utf8-no-null.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *string; 27 | e = js_create_string_utf8(env, (utf8_t *) "hello", -1, &string); 28 | assert(e == 0); 29 | 30 | utf8_t value[5]; 31 | size_t written; 32 | e = js_get_value_string_utf8(env, string, value, 5, &written); 33 | assert(e == 0); 34 | 35 | assert(memcmp(value, "hello", 5) == 0); 36 | assert(written == 5); 37 | 38 | e = js_close_handle_scope(env, scope); 39 | assert(e == 0); 40 | 41 | e = js_destroy_env(env); 42 | assert(e == 0); 43 | 44 | e = js_destroy_platform(platform); 45 | assert(e == 0); 46 | 47 | e = uv_run(loop, UV_RUN_DEFAULT); 48 | assert(e == 0); 49 | } 50 | -------------------------------------------------------------------------------- /test/get-value-string-utf8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *string; 27 | e = js_create_string_utf8(env, (utf8_t *) "hello", -1, &string); 28 | assert(e == 0); 29 | 30 | utf8_t value[6]; 31 | size_t written; 32 | e = js_get_value_string_utf8(env, string, value, 6, &written); 33 | assert(e == 0); 34 | 35 | assert(strcmp((char *) value, "hello") == 0); 36 | assert(value[5] == '\0'); 37 | assert(written == 5); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/helpers.h: -------------------------------------------------------------------------------- 1 | #ifndef JS_TEST_HELPERS_H 2 | #define JS_TEST_HELPERS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "../include/js.h" 9 | 10 | static inline void 11 | js_print_pending_exception(js_env_t *env) { 12 | int err; 13 | 14 | js_value_t *exception; 15 | err = js_get_and_clear_last_exception(env, &exception); 16 | assert(err == 0); 17 | 18 | js_value_t *string; 19 | err = js_coerce_to_string(env, exception, &string); 20 | assert(err == 0); 21 | 22 | utf8_t message[1024]; 23 | err = js_get_value_string_utf8(env, string, message, 1024, NULL); 24 | assert(err == 0); 25 | 26 | printf("err=%s\n", message); 27 | } 28 | 29 | #endif // JS_TEST_HELPERS_H 30 | -------------------------------------------------------------------------------- /test/import-meta-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static void 8 | on_module_meta(js_env_t *env, js_module_t *module, js_value_t *meta, void *data) { 9 | int e; 10 | 11 | e = js_throw_error(env, NULL, "err"); 12 | assert(e == 0); 13 | } 14 | 15 | int 16 | main() { 17 | int e; 18 | 19 | uv_loop_t *loop = uv_default_loop(); 20 | 21 | js_platform_t *platform; 22 | e = js_create_platform(loop, NULL, &platform); 23 | assert(e == 0); 24 | 25 | js_env_t *env; 26 | e = js_create_env(loop, platform, NULL, &env); 27 | assert(e == 0); 28 | 29 | js_handle_scope_t *scope; 30 | e = js_open_handle_scope(env, &scope); 31 | assert(e == 0); 32 | 33 | js_value_t *source; 34 | e = js_create_string_utf8(env, (utf8_t *) "import.meta", -1, &source); 35 | assert(e == 0); 36 | 37 | js_module_t *module; 38 | e = js_create_module(env, "test.js", -1, 0, source, on_module_meta, NULL, &module); 39 | assert(e == 0); 40 | 41 | e = js_instantiate_module(env, module, NULL, NULL); 42 | assert(e == 0); 43 | 44 | js_value_t *promise; 45 | e = js_run_module(env, module, &promise); 46 | assert(e == 0); 47 | 48 | js_promise_state_t state; 49 | e = js_get_promise_state(env, promise, &state); 50 | assert(e == 0); 51 | 52 | assert(state == js_promise_rejected); 53 | 54 | e = js_delete_module(env, module); 55 | assert(e == 0); 56 | 57 | e = js_close_handle_scope(env, scope); 58 | assert(e == 0); 59 | 60 | e = js_destroy_env(env); 61 | assert(e == 0); 62 | 63 | e = js_destroy_platform(platform); 64 | assert(e == 0); 65 | 66 | e = uv_run(loop, UV_RUN_DEFAULT); 67 | assert(e == 0); 68 | } 69 | -------------------------------------------------------------------------------- /test/import-meta.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | static void 8 | on_module_meta(js_env_t *env, js_module_t *module, js_value_t *meta, void *data) { 9 | int e; 10 | 11 | js_value_t *value; 12 | e = js_create_uint32(env, 42, &value); 13 | assert(e == 0); 14 | 15 | e = js_set_named_property(env, meta, "foo", value); 16 | assert(e == 0); 17 | } 18 | 19 | int 20 | main() { 21 | int e; 22 | 23 | uv_loop_t *loop = uv_default_loop(); 24 | 25 | js_platform_t *platform; 26 | e = js_create_platform(loop, NULL, &platform); 27 | assert(e == 0); 28 | 29 | js_env_t *env; 30 | e = js_create_env(loop, platform, NULL, &env); 31 | assert(e == 0); 32 | 33 | js_handle_scope_t *scope; 34 | e = js_open_handle_scope(env, &scope); 35 | assert(e == 0); 36 | 37 | js_value_t *source; 38 | e = js_create_string_utf8(env, (utf8_t *) "export const foo = import.meta.foo", -1, &source); 39 | assert(e == 0); 40 | 41 | js_module_t *module; 42 | e = js_create_module(env, "test.js", -1, 0, source, on_module_meta, NULL, &module); 43 | assert(e == 0); 44 | 45 | e = js_instantiate_module(env, module, NULL, NULL); 46 | assert(e == 0); 47 | 48 | js_value_t *promise; 49 | e = js_run_module(env, module, &promise); 50 | assert(e == 0); 51 | 52 | js_promise_state_t state; 53 | e = js_get_promise_state(env, promise, &state); 54 | assert(e == 0); 55 | 56 | assert(state == js_promise_fulfilled); 57 | 58 | js_value_t *namespace; 59 | e = js_get_module_namespace(env, module, &namespace); 60 | assert(e == 0); 61 | 62 | js_value_t *property; 63 | e = js_get_named_property(env, namespace, "foo", &property); 64 | assert(e == 0); 65 | 66 | uint32_t value; 67 | e = js_get_value_uint32(env, property, &value); 68 | assert(e == 0); 69 | 70 | assert(value == 42); 71 | 72 | e = js_delete_module(env, module); 73 | assert(e == 0); 74 | 75 | e = js_close_handle_scope(env, scope); 76 | assert(e == 0); 77 | 78 | e = js_destroy_env(env); 79 | assert(e == 0); 80 | 81 | e = js_destroy_platform(platform); 82 | assert(e == 0); 83 | 84 | e = uv_run(loop, UV_RUN_DEFAULT); 85 | assert(e == 0); 86 | } 87 | -------------------------------------------------------------------------------- /test/inspector.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static int message_called = 0; 9 | 10 | static void 11 | on_response(js_env_t *env, js_inspector_t *inspector, js_value_t *message, void *data) { 12 | int e; 13 | 14 | message_called++; 15 | 16 | size_t len; 17 | e = js_get_value_string_utf8(env, message, NULL, 0, &len); 18 | assert(e == 0); 19 | 20 | len += 1 /* NULL */; 21 | 22 | utf8_t *response = malloc(len); 23 | e = js_get_value_string_utf8(env, message, response, len, NULL); 24 | assert(e == 0); 25 | 26 | printf("response=%s\n", response); 27 | 28 | free(response); 29 | } 30 | 31 | int 32 | main() { 33 | int e; 34 | 35 | uv_loop_t *loop = uv_default_loop(); 36 | 37 | js_platform_t *platform; 38 | e = js_create_platform(loop, NULL, &platform); 39 | assert(e == 0); 40 | 41 | js_env_t *env; 42 | e = js_create_env(loop, platform, NULL, &env); 43 | assert(e == 0); 44 | 45 | js_handle_scope_t *scope; 46 | e = js_open_handle_scope(env, &scope); 47 | assert(e == 0); 48 | 49 | js_inspector_t *inspector; 50 | e = js_create_inspector(env, &inspector); 51 | assert(e == 0); 52 | 53 | e = js_on_inspector_response(env, inspector, on_response, NULL); 54 | assert(e == 0); 55 | 56 | e = js_connect_inspector(env, inspector); 57 | assert(e == 0); 58 | 59 | js_value_t *message; 60 | e = js_create_string_utf8(env, (utf8_t *) "{ \"id\": 1, \"method\": \"Runtime.evaluate\", \"params\": { \"expression\": \"1 + 2\" } }", -1, &message); 61 | assert(e == 0); 62 | 63 | e = js_send_inspector_request(env, inspector, message); 64 | assert(e == 0); 65 | 66 | assert(message_called); 67 | 68 | e = js_destroy_inspector(env, inspector); 69 | assert(e == 0); 70 | 71 | e = js_close_handle_scope(env, scope); 72 | assert(e == 0); 73 | 74 | e = js_destroy_env(env); 75 | assert(e == 0); 76 | 77 | e = js_destroy_platform(platform); 78 | assert(e == 0); 79 | 80 | e = uv_run(loop, UV_RUN_DEFAULT); 81 | assert(e == 0); 82 | } 83 | -------------------------------------------------------------------------------- /test/many-large-allocs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | #include "fixtures/many-large-allocs.js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *script; 27 | e = js_create_string_utf8(env, many_large_allocs_js, many_large_allocs_js_len, &script); 28 | assert(e == 0); 29 | 30 | e = js_run_script(env, NULL, 0, 0, script, NULL); 31 | assert(e == 0); 32 | 33 | e = js_close_handle_scope(env, scope); 34 | assert(e == 0); 35 | 36 | e = js_destroy_env(env); 37 | assert(e == 0); 38 | 39 | e = js_destroy_platform(platform); 40 | assert(e == 0); 41 | 42 | e = uv_run(loop, UV_RUN_DEFAULT); 43 | assert(e == 0); 44 | } 45 | -------------------------------------------------------------------------------- /test/many-small-allocs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | #include "fixtures/many-small-allocs.js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *script; 27 | e = js_create_string_utf8(env, many_small_allocs_js, many_small_allocs_js_len, &script); 28 | assert(e == 0); 29 | 30 | e = js_run_script(env, NULL, 0, 0, script, NULL); 31 | assert(e == 0); 32 | 33 | e = js_close_handle_scope(env, scope); 34 | assert(e == 0); 35 | 36 | e = js_destroy_env(env); 37 | assert(e == 0); 38 | 39 | e = js_destroy_platform(platform); 40 | assert(e == 0); 41 | 42 | e = uv_run(loop, UV_RUN_DEFAULT); 43 | assert(e == 0); 44 | } 45 | -------------------------------------------------------------------------------- /test/promise-rejection-unhandled-reentrant-deferred.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | #include "fixtures/promise-rejection-unhandled-deferred.js.h" 7 | 8 | int unhandled_called = 0; 9 | 10 | static void 11 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 12 | assert(false); 13 | } 14 | 15 | static void 16 | on_unhandled_rejection(js_env_t *env, js_value_t *reason, js_value_t *promise, void *data) { 17 | int e; 18 | 19 | if (++unhandled_called == 3) return; 20 | 21 | js_value_t *script; 22 | e = js_create_string_utf8(env, promise_rejection_unhandled_deferred_js, promise_rejection_unhandled_deferred_js_len, &script); 23 | assert(e == 0); 24 | 25 | js_value_t *result; 26 | e = js_run_script(env, NULL, 0, 0, script, &result); 27 | assert(e == 0); 28 | } 29 | 30 | int 31 | main() { 32 | int e; 33 | 34 | uv_loop_t *loop = uv_default_loop(); 35 | 36 | js_platform_t *platform; 37 | e = js_create_platform(loop, NULL, &platform); 38 | assert(e == 0); 39 | 40 | js_env_t *env; 41 | e = js_create_env(loop, platform, NULL, &env); 42 | assert(e == 0); 43 | 44 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 45 | assert(e == 0); 46 | 47 | e = js_on_unhandled_rejection(env, on_unhandled_rejection, NULL); 48 | assert(e == 0); 49 | 50 | js_handle_scope_t *scope; 51 | e = js_open_handle_scope(env, &scope); 52 | assert(e == 0); 53 | 54 | js_value_t *script; 55 | e = js_create_string_utf8(env, promise_rejection_unhandled_deferred_js, promise_rejection_unhandled_deferred_js_len, &script); 56 | assert(e == 0); 57 | 58 | js_value_t *result; 59 | e = js_run_script(env, NULL, 0, 0, script, &result); 60 | assert(e == 0); 61 | 62 | assert(unhandled_called == 3); 63 | 64 | e = js_close_handle_scope(env, scope); 65 | assert(e == 0); 66 | 67 | e = js_destroy_env(env); 68 | assert(e == 0); 69 | 70 | e = js_destroy_platform(platform); 71 | assert(e == 0); 72 | 73 | e = uv_run(loop, UV_RUN_DEFAULT); 74 | assert(e == 0); 75 | } 76 | -------------------------------------------------------------------------------- /test/promise-rejection-unhandled-reentrant.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | #include "fixtures/promise-rejection-unhandled.js.h" 7 | 8 | int unhandled_called = 0; 9 | 10 | static void 11 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 12 | assert(false); 13 | } 14 | 15 | static void 16 | on_unhandled_rejection(js_env_t *env, js_value_t *reason, js_value_t *promise, void *data) { 17 | int e; 18 | 19 | if (++unhandled_called == 3) return; 20 | 21 | js_value_t *script; 22 | e = js_create_string_utf8(env, promise_rejection_unhandled_js, promise_rejection_unhandled_js_len, &script); 23 | assert(e == 0); 24 | 25 | js_value_t *result; 26 | e = js_run_script(env, NULL, 0, 0, script, &result); 27 | assert(e == 0); 28 | } 29 | 30 | int 31 | main() { 32 | int e; 33 | 34 | uv_loop_t *loop = uv_default_loop(); 35 | 36 | js_platform_t *platform; 37 | e = js_create_platform(loop, NULL, &platform); 38 | assert(e == 0); 39 | 40 | js_env_t *env; 41 | e = js_create_env(loop, platform, NULL, &env); 42 | assert(e == 0); 43 | 44 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 45 | assert(e == 0); 46 | 47 | e = js_on_unhandled_rejection(env, on_unhandled_rejection, NULL); 48 | assert(e == 0); 49 | 50 | js_handle_scope_t *scope; 51 | e = js_open_handle_scope(env, &scope); 52 | assert(e == 0); 53 | 54 | js_value_t *script; 55 | e = js_create_string_utf8(env, promise_rejection_unhandled_js, promise_rejection_unhandled_js_len, &script); 56 | assert(e == 0); 57 | 58 | js_value_t *result; 59 | e = js_run_script(env, NULL, 0, 0, script, &result); 60 | assert(e == 0); 61 | 62 | assert(unhandled_called == 3); 63 | 64 | e = js_close_handle_scope(env, scope); 65 | assert(e == 0); 66 | 67 | e = js_destroy_env(env); 68 | assert(e == 0); 69 | 70 | e = js_destroy_platform(platform); 71 | assert(e == 0); 72 | 73 | e = uv_run(loop, UV_RUN_DEFAULT); 74 | assert(e == 0); 75 | } 76 | -------------------------------------------------------------------------------- /test/promise-rejection-unhandled.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | #include "fixtures/promise-rejection-unhandled.js.h" 7 | 8 | int unhandled_called = 0; 9 | 10 | static void 11 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 12 | assert(false); 13 | } 14 | 15 | static void 16 | on_unhandled_rejection(js_env_t *env, js_value_t *reason, js_value_t *promise, void *data) { 17 | unhandled_called++; 18 | } 19 | 20 | int 21 | main() { 22 | int e; 23 | 24 | uv_loop_t *loop = uv_default_loop(); 25 | 26 | js_platform_t *platform; 27 | e = js_create_platform(loop, NULL, &platform); 28 | assert(e == 0); 29 | 30 | js_env_t *env; 31 | e = js_create_env(loop, platform, NULL, &env); 32 | assert(e == 0); 33 | 34 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 35 | assert(e == 0); 36 | 37 | e = js_on_unhandled_rejection(env, on_unhandled_rejection, NULL); 38 | assert(e == 0); 39 | 40 | js_handle_scope_t *scope; 41 | e = js_open_handle_scope(env, &scope); 42 | assert(e == 0); 43 | 44 | js_value_t *script; 45 | e = js_create_string_utf8(env, promise_rejection_unhandled_js, promise_rejection_unhandled_js_len, &script); 46 | assert(e == 0); 47 | 48 | js_value_t *result; 49 | e = js_run_script(env, NULL, 0, 0, script, &result); 50 | assert(e == 0); 51 | 52 | assert(unhandled_called == 1); 53 | 54 | e = js_close_handle_scope(env, scope); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | } 66 | -------------------------------------------------------------------------------- /test/promise-rejection.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | #include "fixtures/promise-rejection.js.h" 7 | 8 | int unhandled_called = 0; 9 | 10 | static void 11 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 12 | assert(false); 13 | } 14 | 15 | static void 16 | on_unhandled_rejection(js_env_t *env, js_value_t *reason, js_value_t *promise, void *data) { 17 | unhandled_called++; 18 | } 19 | 20 | int 21 | main() { 22 | int e; 23 | 24 | uv_loop_t *loop = uv_default_loop(); 25 | 26 | js_platform_t *platform; 27 | e = js_create_platform(loop, NULL, &platform); 28 | assert(e == 0); 29 | 30 | js_env_t *env; 31 | e = js_create_env(loop, platform, NULL, &env); 32 | assert(e == 0); 33 | 34 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 35 | assert(e == 0); 36 | 37 | e = js_on_unhandled_rejection(env, on_unhandled_rejection, NULL); 38 | assert(e == 0); 39 | 40 | js_handle_scope_t *scope; 41 | e = js_open_handle_scope(env, &scope); 42 | assert(e == 0); 43 | 44 | js_value_t *script; 45 | e = js_create_string_utf8(env, promise_rejection_js, promise_rejection_js_len, &script); 46 | assert(e == 0); 47 | 48 | js_value_t *result; 49 | e = js_run_script(env, NULL, 0, 0, script, &result); 50 | assert(e == 0); 51 | 52 | assert(unhandled_called == 0); 53 | 54 | e = js_close_handle_scope(env, scope); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | } 66 | -------------------------------------------------------------------------------- /test/run-module-nested-import.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static js_module_t *foo; 9 | static js_module_t *bar; 10 | 11 | static js_module_t * 12 | on_module_resolve(js_env_t *env, js_value_t *specifier, js_value_t *assertions, js_module_t *referrer, void *data) { 13 | int e; 14 | 15 | utf8_t file[1024]; 16 | e = js_get_value_string_utf8(env, specifier, file, 1024, NULL); 17 | assert(e == 0); 18 | 19 | assert(strcmp((char *) file, "bar.js") == 0); 20 | 21 | js_value_t *source; 22 | e = js_create_string_utf8(env, (utf8_t *) "export default 42", -1, &source); 23 | assert(e == 0); 24 | 25 | e = js_create_module(env, "bar.js", -1, 0, source, NULL, NULL, &bar); 26 | assert(e == 0); 27 | 28 | return bar; 29 | } 30 | 31 | int 32 | main() { 33 | int e; 34 | 35 | uv_loop_t *loop = uv_default_loop(); 36 | 37 | js_platform_t *platform; 38 | e = js_create_platform(loop, NULL, &platform); 39 | assert(e == 0); 40 | 41 | js_env_t *env; 42 | e = js_create_env(loop, platform, NULL, &env); 43 | assert(e == 0); 44 | 45 | js_handle_scope_t *scope; 46 | e = js_open_handle_scope(env, &scope); 47 | assert(e == 0); 48 | 49 | js_value_t *source; 50 | e = js_create_string_utf8(env, (utf8_t *) "import bar from 'bar.js'", -1, &source); 51 | assert(e == 0); 52 | 53 | e = js_create_module(env, "foo.js", -1, 0, source, NULL, NULL, &foo); 54 | assert(e == 0); 55 | 56 | e = js_instantiate_module(env, foo, on_module_resolve, NULL); 57 | assert(e == 0); 58 | 59 | js_value_t *promise; 60 | e = js_run_module(env, foo, &promise); 61 | assert(e == 0); 62 | 63 | js_promise_state_t state; 64 | e = js_get_promise_state(env, promise, &state); 65 | assert(e == 0); 66 | 67 | assert(state == js_promise_fulfilled); 68 | 69 | e = js_delete_module(env, foo); 70 | assert(e == 0); 71 | 72 | e = js_delete_module(env, bar); 73 | assert(e == 0); 74 | 75 | e = js_close_handle_scope(env, scope); 76 | assert(e == 0); 77 | 78 | e = js_destroy_env(env); 79 | assert(e == 0); 80 | 81 | e = js_destroy_platform(platform); 82 | assert(e == 0); 83 | 84 | e = uv_run(loop, UV_RUN_DEFAULT); 85 | assert(e == 0); 86 | } 87 | -------------------------------------------------------------------------------- /test/run-module-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int unhandled_called = 0; 8 | 9 | static void 10 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 11 | assert(false); 12 | } 13 | 14 | static void 15 | on_unhandled_rejection(js_env_t *env, js_value_t *reason, js_value_t *promise, void *data) { 16 | unhandled_called++; 17 | } 18 | 19 | int 20 | main() { 21 | int e; 22 | 23 | uv_loop_t *loop = uv_default_loop(); 24 | 25 | js_platform_t *platform; 26 | e = js_create_platform(loop, NULL, &platform); 27 | assert(e == 0); 28 | 29 | js_env_t *env; 30 | e = js_create_env(loop, platform, NULL, &env); 31 | assert(e == 0); 32 | 33 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 34 | assert(e == 0); 35 | 36 | e = js_on_unhandled_rejection(env, on_unhandled_rejection, NULL); 37 | assert(e == 0); 38 | 39 | js_handle_scope_t *scope; 40 | e = js_open_handle_scope(env, &scope); 41 | assert(e == 0); 42 | 43 | js_value_t *source; 44 | e = js_create_string_utf8(env, (utf8_t *) "throw 'err'", -1, &source); 45 | assert(e == 0); 46 | 47 | js_module_t *module; 48 | e = js_create_module(env, "test.js", -1, 0, source, NULL, NULL, &module); 49 | assert(e == 0); 50 | 51 | e = js_instantiate_module(env, module, NULL, NULL); 52 | assert(e == 0); 53 | 54 | js_value_t *promise; 55 | e = js_run_module(env, module, &promise); 56 | assert(e == 0); 57 | 58 | assert(unhandled_called >= 1); 59 | 60 | js_promise_state_t state; 61 | e = js_get_promise_state(env, promise, &state); 62 | assert(e == 0); 63 | 64 | assert(state == js_promise_rejected); 65 | 66 | e = js_delete_module(env, module); 67 | assert(e == 0); 68 | 69 | e = js_close_handle_scope(env, scope); 70 | assert(e == 0); 71 | 72 | e = js_destroy_env(env); 73 | assert(e == 0); 74 | 75 | e = js_destroy_platform(platform); 76 | assert(e == 0); 77 | 78 | e = uv_run(loop, UV_RUN_DEFAULT); 79 | assert(e == 0); 80 | } 81 | -------------------------------------------------------------------------------- /test/run-script-ignore-return.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "12345", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *result; 30 | e = js_run_script(env, NULL, 0, 0, script, NULL); 31 | assert(e == 0); 32 | 33 | e = js_close_handle_scope(env, scope); 34 | assert(e == 0); 35 | 36 | e = js_destroy_env(env); 37 | assert(e == 0); 38 | 39 | e = js_destroy_platform(platform); 40 | assert(e == 0); 41 | 42 | e = uv_run(loop, UV_RUN_DEFAULT); 43 | assert(e == 0); 44 | } 45 | -------------------------------------------------------------------------------- /test/run-script-in-context.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_context_t *context; 26 | e = js_create_context(env, &context); 27 | assert(e == 0); 28 | 29 | e = js_enter_context(env, context); 30 | assert(e == 0); 31 | 32 | js_value_t *script; 33 | e = js_create_string_utf8(env, (utf8_t *) "globalThis", -1, &script); 34 | assert(e == 0); 35 | 36 | js_value_t *result; 37 | e = js_run_script(env, NULL, 0, 0, script, &result); 38 | assert(e == 0); 39 | 40 | { 41 | js_value_t *global; 42 | e = js_get_global(env, &global); 43 | assert(e == 0); 44 | 45 | bool equals; 46 | e = js_strict_equals(env, result, global, &equals); 47 | assert(e == 0); 48 | 49 | assert(equals); 50 | } 51 | 52 | e = js_exit_context(env, context); 53 | assert(e == 0); 54 | 55 | { 56 | js_value_t *global; 57 | e = js_get_global(env, &global); 58 | assert(e == 0); 59 | 60 | bool equals; 61 | e = js_strict_equals(env, result, global, &equals); 62 | assert(e == 0); 63 | 64 | assert(equals == false); 65 | } 66 | 67 | e = js_destroy_context(env, context); 68 | assert(e == 0); 69 | 70 | e = js_close_handle_scope(env, scope); 71 | assert(e == 0); 72 | 73 | e = js_destroy_env(env); 74 | assert(e == 0); 75 | 76 | e = js_destroy_platform(platform); 77 | assert(e == 0); 78 | 79 | e = uv_run(loop, UV_RUN_DEFAULT); 80 | assert(e == 0); 81 | } 82 | -------------------------------------------------------------------------------- /test/run-script-throw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "../include/js.h" 8 | 9 | int uncaught_called = 0; 10 | 11 | static void 12 | on_uncaught_exception(js_env_t *env, js_value_t *error, void *data) { 13 | int e; 14 | 15 | uncaught_called++; 16 | 17 | bool has_exception; 18 | e = js_is_exception_pending(env, &has_exception); 19 | assert(e == 0); 20 | 21 | assert(!has_exception); 22 | 23 | utf8_t value[4]; 24 | e = js_get_value_string_utf8(env, error, value, 4, NULL); 25 | assert(e == 0); 26 | 27 | assert(strcmp((char *) value, "err") == 0); 28 | } 29 | 30 | int 31 | main() { 32 | int e; 33 | 34 | uv_loop_t *loop = uv_default_loop(); 35 | 36 | js_platform_t *platform; 37 | e = js_create_platform(loop, NULL, &platform); 38 | assert(e == 0); 39 | 40 | js_env_t *env; 41 | e = js_create_env(loop, platform, NULL, &env); 42 | assert(e == 0); 43 | 44 | js_handle_scope_t *scope; 45 | e = js_open_handle_scope(env, &scope); 46 | assert(e == 0); 47 | 48 | e = js_on_uncaught_exception(env, on_uncaught_exception, NULL); 49 | assert(e == 0); 50 | 51 | js_value_t *script; 52 | e = js_create_string_utf8(env, (utf8_t *) "throw 'err'", -1, &script); 53 | assert(e == 0); 54 | 55 | js_value_t *result; 56 | e = js_run_script(env, NULL, 0, 0, script, &result); 57 | assert(e == js_uncaught_exception); 58 | 59 | assert(uncaught_called == 1); 60 | 61 | e = js_close_handle_scope(env, scope); 62 | assert(e == 0); 63 | 64 | e = js_destroy_env(env); 65 | assert(e == 0); 66 | 67 | e = js_destroy_platform(platform); 68 | assert(e == 0); 69 | 70 | e = uv_run(loop, UV_RUN_DEFAULT); 71 | assert(e == 0); 72 | } 73 | -------------------------------------------------------------------------------- /test/run-script.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "12345", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *result; 30 | e = js_run_script(env, NULL, 0, 0, script, &result); 31 | assert(e == 0); 32 | 33 | uint32_t value; 34 | e = js_get_value_uint32(env, result, &value); 35 | assert(e == 0); 36 | 37 | assert(value == 12345); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/set-named-property.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | { 26 | js_value_t *global; 27 | e = js_get_global(env, &global); 28 | assert(e == 0); 29 | 30 | js_value_t *value; 31 | e = js_create_uint32(env, 42, &value); 32 | assert(e == 0); 33 | 34 | e = js_set_named_property(env, global, "life", value); 35 | assert(e == 0); 36 | } 37 | 38 | js_value_t *script; 39 | e = js_create_string_utf8(env, (utf8_t *) "life", -1, &script); 40 | assert(e == 0); 41 | 42 | js_value_t *result; 43 | e = js_run_script(env, NULL, 0, 0, script, &result); 44 | assert(e == 0); 45 | 46 | { 47 | uint32_t value; 48 | js_get_value_uint32(env, result, &value); 49 | assert(e == 0); 50 | 51 | assert(value == 42); 52 | } 53 | 54 | e = js_close_handle_scope(env, scope); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | } 66 | -------------------------------------------------------------------------------- /test/threads.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | js_platform_t *platform; 7 | 8 | void 9 | on_thread(void *data) { 10 | int e; 11 | 12 | uv_loop_t loop; 13 | e = uv_loop_init(&loop); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(&loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | e = uv_run(&loop, UV_RUN_DEFAULT); 21 | assert(e == 0); 22 | 23 | e = js_destroy_env(env); 24 | assert(e == 0); 25 | 26 | e = uv_run(&loop, UV_RUN_DEFAULT); 27 | assert(e == 0); 28 | 29 | e = uv_loop_close(&loop); 30 | assert(e == 0); 31 | } 32 | 33 | int 34 | main() { 35 | int e; 36 | 37 | uv_loop_t *loop = uv_default_loop(); 38 | 39 | e = js_create_platform(loop, NULL, &platform); 40 | assert(e == 0); 41 | 42 | js_env_t *env; 43 | e = js_create_env(loop, platform, NULL, &env); 44 | assert(e == 0); 45 | 46 | uv_thread_t thread; 47 | e = uv_thread_create(&thread, on_thread, NULL); 48 | assert(e == 0); 49 | 50 | e = uv_run(loop, UV_RUN_DEFAULT); 51 | assert(e == 0); 52 | 53 | uv_thread_join(&thread); 54 | 55 | e = js_destroy_env(env); 56 | assert(e == 0); 57 | 58 | e = js_destroy_platform(platform); 59 | assert(e == 0); 60 | 61 | e = uv_run(loop, UV_RUN_DEFAULT); 62 | assert(e == 0); 63 | } 64 | -------------------------------------------------------------------------------- /test/throw-error-formatted.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | e = js_throw_errorf(env, "code", "%s", "message"); 26 | assert(e == 0); 27 | 28 | e = js_close_handle_scope(env, scope); 29 | assert(e == 0); 30 | 31 | e = js_destroy_env(env); 32 | assert(e == 0); 33 | 34 | e = js_destroy_platform(platform); 35 | assert(e == 0); 36 | 37 | e = uv_run(loop, UV_RUN_DEFAULT); 38 | assert(e == 0); 39 | } 40 | -------------------------------------------------------------------------------- /test/throw-error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | e = js_throw_error(env, "code", "message"); 26 | assert(e == 0); 27 | 28 | e = js_close_handle_scope(env, scope); 29 | assert(e == 0); 30 | 31 | e = js_destroy_env(env); 32 | assert(e == 0); 33 | 34 | e = js_destroy_platform(platform); 35 | assert(e == 0); 36 | 37 | e = uv_run(loop, UV_RUN_DEFAULT); 38 | assert(e == 0); 39 | } 40 | -------------------------------------------------------------------------------- /test/typeof-bigint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "12345n", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_bigint); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-boolean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "true", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_boolean); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-external.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | int 7 | main() { 8 | int e; 9 | 10 | uv_loop_t *loop = uv_default_loop(); 11 | 12 | js_platform_t *platform; 13 | e = js_create_platform(loop, NULL, &platform); 14 | assert(e == 0); 15 | 16 | js_env_t *env; 17 | e = js_create_env(loop, platform, NULL, &env); 18 | assert(e == 0); 19 | 20 | js_handle_scope_t *scope; 21 | e = js_open_handle_scope(env, &scope); 22 | assert(e == 0); 23 | 24 | js_value_t *value; 25 | e = js_create_external(env, (void *) 42, NULL, NULL, &value); 26 | assert(e == 0); 27 | 28 | js_value_type_t type; 29 | e = js_typeof(env, value, &type); 30 | assert(e == 0); 31 | 32 | assert(type == js_external); 33 | 34 | e = js_close_handle_scope(env, scope); 35 | assert(e == 0); 36 | 37 | e = js_destroy_env(env); 38 | assert(e == 0); 39 | 40 | e = js_destroy_platform(platform); 41 | assert(e == 0); 42 | 43 | e = uv_run(loop, UV_RUN_DEFAULT); 44 | assert(e == 0); 45 | } 46 | -------------------------------------------------------------------------------- /test/typeof-function.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "() => {}", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_function); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-null.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "null", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_null); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "12345", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_number); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-object.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "({ hello: 'world' })", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_object); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "'hello world'", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_string); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-symbol.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "Symbol('foo')", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_symbol); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/typeof-undefined.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "undefined", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *value; 30 | e = js_run_script(env, NULL, 0, 0, script, &value); 31 | assert(e == 0); 32 | 33 | js_value_type_t type; 34 | e = js_typeof(env, value, &type); 35 | assert(e == 0); 36 | 37 | assert(type == js_undefined); 38 | 39 | e = js_close_handle_scope(env, scope); 40 | assert(e == 0); 41 | 42 | e = js_destroy_env(env); 43 | assert(e == 0); 44 | 45 | e = js_destroy_platform(platform); 46 | assert(e == 0); 47 | 48 | e = uv_run(loop, UV_RUN_DEFAULT); 49 | assert(e == 0); 50 | } 51 | -------------------------------------------------------------------------------- /test/wasm-async-io.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | #include "fixtures/wasm-async.js.h" 7 | 8 | static js_env_t *env; 9 | static js_ref_t *ref; 10 | 11 | static void 12 | on_timer(uv_timer_t *handle) { 13 | int e; 14 | 15 | js_handle_scope_t *scope; 16 | e = js_open_handle_scope(env, &scope); 17 | assert(e == 0); 18 | 19 | js_value_t *script; 20 | e = js_create_string_utf8(env, wasm_async_js, wasm_async_js_len, &script); 21 | assert(e == 0); 22 | 23 | js_value_t *promise; 24 | e = js_run_script(env, NULL, 0, 0, script, &promise); 25 | assert(e == 0); 26 | 27 | e = js_create_reference(env, promise, 1, &ref); 28 | assert(e == 0); 29 | 30 | e = js_close_handle_scope(env, scope); 31 | assert(e == 0); 32 | } 33 | 34 | int 35 | main() { 36 | int e; 37 | 38 | uv_loop_t *loop = uv_default_loop(); 39 | 40 | js_platform_t *platform; 41 | e = js_create_platform(loop, NULL, &platform); 42 | assert(e == 0); 43 | 44 | e = js_create_env(loop, platform, NULL, &env); 45 | assert(e == 0); 46 | 47 | uv_timer_t timer; 48 | e = uv_timer_init(loop, &timer); 49 | assert(e == 0); 50 | 51 | e = uv_timer_start(&timer, on_timer, 100, 0); 52 | assert(e == 0); 53 | 54 | uv_run(loop, UV_RUN_DEFAULT); 55 | 56 | js_handle_scope_t *scope; 57 | e = js_open_handle_scope(env, &scope); 58 | assert(e == 0); 59 | 60 | js_value_t *promise; 61 | e = js_get_reference_value(env, ref, &promise); 62 | assert(e == 0); 63 | 64 | js_value_t *result; 65 | e = js_get_promise_result(env, promise, &result); 66 | assert(e == 0); 67 | 68 | uint32_t value; 69 | e = js_get_value_uint32(env, result, &value); 70 | assert(e == 0); 71 | 72 | assert(value == 42); 73 | 74 | e = js_delete_reference(env, ref); 75 | assert(e == 0); 76 | 77 | e = js_close_handle_scope(env, scope); 78 | assert(e == 0); 79 | 80 | e = js_destroy_env(env); 81 | assert(e == 0); 82 | 83 | e = js_destroy_platform(platform); 84 | assert(e == 0); 85 | 86 | e = uv_run(loop, UV_RUN_DEFAULT); 87 | assert(e == 0); 88 | } 89 | -------------------------------------------------------------------------------- /test/wasm-async.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | #include "fixtures/wasm-async.js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *script; 27 | e = js_create_string_utf8(env, wasm_async_js, wasm_async_js_len, &script); 28 | assert(e == 0); 29 | 30 | js_value_t *promise; 31 | e = js_run_script(env, NULL, 0, 0, script, &promise); 32 | assert(e == 0); 33 | 34 | uv_run(loop, UV_RUN_DEFAULT); 35 | 36 | js_value_t *result; 37 | e = js_get_promise_result(env, promise, &result); 38 | assert(e == 0); 39 | 40 | uint32_t value; 41 | e = js_get_value_uint32(env, result, &value); 42 | assert(e == 0); 43 | 44 | assert(value == 42); 45 | 46 | e = js_close_handle_scope(env, scope); 47 | assert(e == 0); 48 | 49 | e = js_destroy_env(env); 50 | assert(e == 0); 51 | 52 | e = js_destroy_platform(platform); 53 | assert(e == 0); 54 | 55 | e = uv_run(loop, UV_RUN_DEFAULT); 56 | assert(e == 0); 57 | } 58 | -------------------------------------------------------------------------------- /test/wasm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "../include/js.h" 5 | 6 | #include "fixtures/wasm-sync.js.h" 7 | 8 | int 9 | main() { 10 | int e; 11 | 12 | uv_loop_t *loop = uv_default_loop(); 13 | 14 | js_platform_t *platform; 15 | e = js_create_platform(loop, NULL, &platform); 16 | assert(e == 0); 17 | 18 | js_env_t *env; 19 | e = js_create_env(loop, platform, NULL, &env); 20 | assert(e == 0); 21 | 22 | js_handle_scope_t *scope; 23 | e = js_open_handle_scope(env, &scope); 24 | assert(e == 0); 25 | 26 | js_value_t *script; 27 | e = js_create_string_utf8(env, wasm_sync_js, wasm_sync_js_len, &script); 28 | assert(e == 0); 29 | 30 | js_value_t *result; 31 | e = js_run_script(env, NULL, 0, 0, script, &result); 32 | assert(e == 0); 33 | 34 | uint32_t value; 35 | e = js_get_value_uint32(env, result, &value); 36 | assert(e == 0); 37 | 38 | assert(value == 42); 39 | 40 | e = js_close_handle_scope(env, scope); 41 | assert(e == 0); 42 | 43 | e = js_destroy_env(env); 44 | assert(e == 0); 45 | 46 | e = js_destroy_platform(platform); 47 | assert(e == 0); 48 | 49 | e = uv_run(loop, UV_RUN_DEFAULT); 50 | assert(e == 0); 51 | } 52 | -------------------------------------------------------------------------------- /test/wrap-remove-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool finalize_called = false; 9 | 10 | static void 11 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 12 | finalize_called = true; 13 | } 14 | 15 | int 16 | main() { 17 | int e; 18 | 19 | uv_loop_t *loop = uv_default_loop(); 20 | 21 | js_platform_options_t options = { 22 | .expose_garbage_collection = true, 23 | .trace_garbage_collection = true, 24 | }; 25 | 26 | js_platform_t *platform; 27 | e = js_create_platform(loop, &options, &platform); 28 | assert(e == 0); 29 | 30 | js_env_t *env; 31 | e = js_create_env(loop, platform, NULL, &env); 32 | assert(e == 0); 33 | 34 | js_handle_scope_t *scope; 35 | e = js_open_handle_scope(env, &scope); 36 | assert(e == 0); 37 | 38 | js_value_t *script; 39 | e = js_create_string_utf8(env, (utf8_t *) "({ hello: 'world' })", -1, &script); 40 | assert(e == 0); 41 | 42 | js_value_t *object; 43 | e = js_run_script(env, NULL, 0, 0, script, &object); 44 | assert(e == 0); 45 | 46 | e = js_wrap(env, object, (void *) 42, on_finalize, NULL, NULL); 47 | assert(e == 0); 48 | 49 | e = js_remove_wrap(env, object, NULL); 50 | assert(e == 0); 51 | 52 | e = js_close_handle_scope(env, scope); 53 | assert(e == 0); 54 | 55 | e = js_request_garbage_collection(env); 56 | assert(e == 0); 57 | 58 | e = js_destroy_env(env); 59 | assert(e == 0); 60 | 61 | e = js_destroy_platform(platform); 62 | assert(e == 0); 63 | 64 | e = uv_run(loop, UV_RUN_DEFAULT); 65 | assert(e == 0); 66 | 67 | assert(!finalize_called); 68 | } 69 | -------------------------------------------------------------------------------- /test/wrap-with-finalizer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool finalize_called = false; 9 | 10 | static void 11 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 12 | finalize_called = true; 13 | 14 | assert((intptr_t) data == 42); 15 | } 16 | 17 | int 18 | main() { 19 | int e; 20 | 21 | uv_loop_t *loop = uv_default_loop(); 22 | 23 | js_platform_options_t options = { 24 | .expose_garbage_collection = true, 25 | .trace_garbage_collection = true, 26 | }; 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, &options, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_value_t *script; 41 | e = js_create_string_utf8(env, (utf8_t *) "({ hello: 'world' })", -1, &script); 42 | assert(e == 0); 43 | 44 | js_value_t *object; 45 | e = js_run_script(env, NULL, 0, 0, script, &object); 46 | assert(e == 0); 47 | 48 | e = js_wrap(env, object, (void *) 42, on_finalize, NULL, NULL); 49 | assert(e == 0); 50 | 51 | e = js_close_handle_scope(env, scope); 52 | assert(e == 0); 53 | 54 | e = js_request_garbage_collection(env); 55 | assert(e == 0); 56 | 57 | e = js_destroy_env(env); 58 | assert(e == 0); 59 | 60 | e = js_destroy_platform(platform); 61 | assert(e == 0); 62 | 63 | e = uv_run(loop, UV_RUN_DEFAULT); 64 | assert(e == 0); 65 | 66 | assert(finalize_called); 67 | } 68 | -------------------------------------------------------------------------------- /test/wrap-with-reference.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "../include/js.h" 7 | 8 | static bool finalize_called = false; 9 | 10 | static void 11 | on_finalize(js_env_t *env, void *data, void *finalize_hint) { 12 | finalize_called = true; 13 | 14 | assert((intptr_t) data == 42); 15 | } 16 | 17 | int 18 | main() { 19 | int e; 20 | 21 | uv_loop_t *loop = uv_default_loop(); 22 | 23 | js_platform_options_t options = { 24 | .expose_garbage_collection = true, 25 | .trace_garbage_collection = true, 26 | }; 27 | 28 | js_platform_t *platform; 29 | e = js_create_platform(loop, &options, &platform); 30 | assert(e == 0); 31 | 32 | js_env_t *env; 33 | e = js_create_env(loop, platform, NULL, &env); 34 | assert(e == 0); 35 | 36 | js_handle_scope_t *scope; 37 | e = js_open_handle_scope(env, &scope); 38 | assert(e == 0); 39 | 40 | js_value_t *script; 41 | e = js_create_string_utf8(env, (utf8_t *) "({ hello: 'world' })", -1, &script); 42 | assert(e == 0); 43 | 44 | js_value_t *object; 45 | e = js_run_script(env, NULL, 0, 0, script, &object); 46 | assert(e == 0); 47 | 48 | js_ref_t *ref; 49 | e = js_wrap(env, object, (void *) 42, on_finalize, NULL, &ref); 50 | assert(e == 0); 51 | 52 | e = js_reference_ref(env, ref, NULL); 53 | assert(e == 0); 54 | 55 | e = js_close_handle_scope(env, scope); 56 | assert(e == 0); 57 | 58 | e = js_request_garbage_collection(env); 59 | assert(e == 0); 60 | 61 | assert(!finalize_called); 62 | 63 | e = js_delete_reference(env, ref); 64 | assert(e == 0); 65 | 66 | e = js_request_garbage_collection(env); 67 | assert(e == 0); 68 | 69 | e = js_destroy_env(env); 70 | assert(e == 0); 71 | 72 | e = js_destroy_platform(platform); 73 | assert(e == 0); 74 | 75 | e = uv_run(loop, UV_RUN_DEFAULT); 76 | assert(e == 0); 77 | 78 | assert(finalize_called); 79 | } 80 | -------------------------------------------------------------------------------- /test/wrap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/js.h" 6 | 7 | int 8 | main() { 9 | int e; 10 | 11 | uv_loop_t *loop = uv_default_loop(); 12 | 13 | js_platform_t *platform; 14 | e = js_create_platform(loop, NULL, &platform); 15 | assert(e == 0); 16 | 17 | js_env_t *env; 18 | e = js_create_env(loop, platform, NULL, &env); 19 | assert(e == 0); 20 | 21 | js_handle_scope_t *scope; 22 | e = js_open_handle_scope(env, &scope); 23 | assert(e == 0); 24 | 25 | js_value_t *script; 26 | e = js_create_string_utf8(env, (utf8_t *) "({ hello: 'world' })", -1, &script); 27 | assert(e == 0); 28 | 29 | js_value_t *object; 30 | e = js_run_script(env, NULL, 0, 0, script, &object); 31 | assert(e == 0); 32 | 33 | e = js_wrap(env, object, (void *) 42, NULL, NULL, NULL); 34 | assert(e == 0); 35 | 36 | bool is_wrapped; 37 | e = js_is_wrapped(env, object, &is_wrapped); 38 | assert(e == 0); 39 | 40 | assert(is_wrapped); 41 | 42 | intptr_t value; 43 | e = js_unwrap(env, object, (void **) &value); 44 | assert(e == 0); 45 | 46 | assert(value == 42); 47 | 48 | e = js_close_handle_scope(env, scope); 49 | assert(e == 0); 50 | 51 | e = js_destroy_env(env); 52 | assert(e == 0); 53 | 54 | e = js_destroy_platform(platform); 55 | assert(e == 0); 56 | 57 | e = uv_run(loop, UV_RUN_DEFAULT); 58 | assert(e == 0); 59 | } 60 | --------------------------------------------------------------------------------