koffi 0.9.4 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +60 -60
- package/README.md +163 -153
- package/package.json +19 -18
- package/src/call.hh +27 -27
- package/src/call_arm64.cc +482 -482
- package/src/call_arm64_fwd.S +115 -115
- package/src/call_x64_sysv.cc +477 -477
- package/src/call_x64_sysv_fwd.S +131 -131
- package/src/call_x64_win.cc +243 -243
- package/src/call_x64_win_fwd.asm +105 -105
- package/src/call_x86.cc +259 -259
- package/src/call_x86_fwd.S +48 -48
- package/src/call_x86_fwd.asm +50 -50
- package/src/ffi.cc +504 -504
- package/src/ffi.hh +135 -135
- package/src/util.cc +296 -296
- package/src/util.hh +80 -80
- package/vendor/dragonbox/CMakeLists.txt +122 -122
- package/vendor/dragonbox/LICENSE-Apache2-LLVM +218 -218
- package/vendor/dragonbox/LICENSE-Boost +23 -23
- package/vendor/dragonbox/README.md +277 -277
- package/vendor/dragonbox/cmake/dragonboxConfig.cmake +1 -1
- package/vendor/dragonbox/include/dragonbox/dragonbox.h +2670 -2670
- package/vendor/dragonbox/include/dragonbox/dragonbox_to_chars.h +108 -108
- package/vendor/dragonbox/other_files/unknown_win64_vc2019.html +539 -539
- package/vendor/dragonbox/source/dragonbox_to_chars.cpp +303 -303
- package/vendor/dragonbox/subproject/3rdparty/grisu_exact/CMakeLists.txt +23 -23
- package/vendor/dragonbox/subproject/3rdparty/grisu_exact/fp_to_chars.cpp +238 -238
- package/vendor/dragonbox/subproject/3rdparty/grisu_exact/fp_to_chars.h +95 -95
- package/vendor/dragonbox/subproject/3rdparty/grisu_exact/grisu_exact.h +2666 -2666
- package/vendor/dragonbox/subproject/3rdparty/ryu/CMakeLists.txt +16 -16
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/common.h +114 -114
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s.c +509 -509
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s_full_table.h +367 -367
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s_intrinsics.h +357 -357
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/digit_table.h +35 -35
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s.c +345 -345
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s_full_table.h +55 -55
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s_intrinsics.h +128 -128
- package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/ryu.h +46 -46
- package/vendor/dragonbox/subproject/3rdparty/schubfach/CMakeLists.txt +21 -21
- package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_32.cc +699 -699
- package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_32.h +31 -31
- package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_64.cc +1354 -1354
- package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_64.h +31 -31
- package/vendor/dragonbox/subproject/3rdparty/shaded_plots/example_shaded_plots.m +68 -68
- package/vendor/dragonbox/subproject/3rdparty/shaded_plots/license.txt +25 -25
- package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_distribution.m +92 -92
- package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_distribution_prctile.m +121 -121
- package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_histogram_shaded.m +99 -99
- package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_shaded.m +93 -93
- package/vendor/dragonbox/subproject/benchmark/CMakeLists.txt +64 -64
- package/vendor/dragonbox/subproject/benchmark/include/benchmark.h +40 -40
- package/vendor/dragonbox/subproject/benchmark/matlab/plot_benchmarks.m +21 -21
- package/vendor/dragonbox/subproject/benchmark/matlab/plot_digit_benchmark.m +78 -78
- package/vendor/dragonbox/subproject/benchmark/matlab/plot_uniform_benchmark.m +95 -95
- package/vendor/dragonbox/subproject/benchmark/source/benchmark.cpp +237 -237
- package/vendor/dragonbox/subproject/benchmark/source/dragonbox.cpp +30 -30
- package/vendor/dragonbox/subproject/benchmark/source/grisu_exact.cpp +36 -36
- package/vendor/dragonbox/subproject/benchmark/source/ryu.cpp +27 -27
- package/vendor/dragonbox/subproject/benchmark/source/schubfach.cpp +31 -31
- package/vendor/dragonbox/subproject/common/CMakeLists.txt +41 -41
- package/vendor/dragonbox/subproject/common/include/best_rational_approx.h +96 -96
- package/vendor/dragonbox/subproject/common/include/big_uint.h +217 -217
- package/vendor/dragonbox/subproject/common/include/continued_fractions.h +173 -173
- package/vendor/dragonbox/subproject/common/include/good_rational_approx.h +266 -266
- package/vendor/dragonbox/subproject/common/include/random_float.h +182 -182
- package/vendor/dragonbox/subproject/common/include/rational_continued_fractions.h +56 -56
- package/vendor/dragonbox/subproject/common/source/big_uint.cpp +601 -601
- package/vendor/dragonbox/subproject/meta/CMakeLists.txt +40 -40
- package/vendor/dragonbox/subproject/meta/results/binary32_generated_cache.txt +81 -81
- package/vendor/dragonbox/subproject/meta/results/binary64_compressed_cache_error_table.txt +9 -9
- package/vendor/dragonbox/subproject/meta/results/binary64_generated_cache.txt +622 -622
- package/vendor/dragonbox/subproject/meta/source/generate_cache.cpp +126 -126
- package/vendor/dragonbox/subproject/meta/source/live_test.cpp +81 -81
- package/vendor/dragonbox/subproject/meta/source/perf_test.cpp +104 -104
- package/vendor/dragonbox/subproject/meta/source/sandbox.cpp +20 -20
- package/vendor/dragonbox/subproject/test/CMakeLists.txt +69 -69
- package/vendor/dragonbox/subproject/test/results/binary32.csv +255 -255
- package/vendor/dragonbox/subproject/test/results/binary64.csv +2047 -2047
- package/vendor/dragonbox/subproject/test/results/plot_required_bits.m +17 -17
- package/vendor/dragonbox/subproject/test/source/test_all_shorter_interval_cases.cpp +88 -88
- package/vendor/dragonbox/subproject/test/source/uniform_random_test.cpp +95 -95
- package/vendor/dragonbox/subproject/test/source/verify_cache_precision.cpp +337 -337
- package/vendor/dragonbox/subproject/test/source/verify_compressed_cache.cpp +154 -154
- package/vendor/dragonbox/subproject/test/source/verify_fast_multiplication.cpp +168 -168
- package/vendor/dragonbox/subproject/test/source/verify_log_computation.cpp +251 -251
- package/vendor/dragonbox/subproject/test/source/verify_magic_division.cpp +113 -113
- package/vendor/libcc/libcc.cc +7651 -7651
- package/vendor/libcc/libcc.hh +4312 -4312
- package/vendor/node-addon-api/CHANGELOG.md +859 -859
- package/vendor/node-addon-api/CODE_OF_CONDUCT.md +4 -4
- package/vendor/node-addon-api/CONTRIBUTING.md +93 -93
- package/vendor/node-addon-api/LICENSE.md +12 -12
- package/vendor/node-addon-api/README.md +293 -293
- package/vendor/node-addon-api/appveyor.yml +37 -37
- package/vendor/node-addon-api/benchmark/README.md +47 -47
- package/vendor/node-addon-api/benchmark/binding.gyp +25 -25
- package/vendor/node-addon-api/benchmark/function_args.cc +217 -217
- package/vendor/node-addon-api/benchmark/function_args.js +60 -60
- package/vendor/node-addon-api/benchmark/index.js +34 -34
- package/vendor/node-addon-api/benchmark/property_descriptor.cc +91 -91
- package/vendor/node-addon-api/benchmark/property_descriptor.js +37 -37
- package/vendor/node-addon-api/common.gypi +21 -21
- package/vendor/node-addon-api/doc/addon.md +163 -163
- package/vendor/node-addon-api/doc/array.md +81 -81
- package/vendor/node-addon-api/doc/array_buffer.md +155 -155
- package/vendor/node-addon-api/doc/async_context.md +86 -86
- package/vendor/node-addon-api/doc/async_operations.md +31 -31
- package/vendor/node-addon-api/doc/async_worker.md +427 -427
- package/vendor/node-addon-api/doc/async_worker_variants.md +557 -557
- package/vendor/node-addon-api/doc/bigint.md +97 -97
- package/vendor/node-addon-api/doc/boolean.md +68 -68
- package/vendor/node-addon-api/doc/buffer.md +150 -150
- package/vendor/node-addon-api/doc/callback_scope.md +54 -54
- package/vendor/node-addon-api/doc/callbackinfo.md +97 -97
- package/vendor/node-addon-api/doc/checker-tool.md +32 -32
- package/vendor/node-addon-api/doc/class_property_descriptor.md +115 -115
- package/vendor/node-addon-api/doc/cmake-js.md +68 -68
- package/vendor/node-addon-api/doc/conversion-tool.md +27 -27
- package/vendor/node-addon-api/doc/creating_a_release.md +62 -62
- package/vendor/node-addon-api/doc/dataview.md +248 -248
- package/vendor/node-addon-api/doc/date.md +68 -68
- package/vendor/node-addon-api/doc/env.md +196 -196
- package/vendor/node-addon-api/doc/error.md +120 -120
- package/vendor/node-addon-api/doc/error_handling.md +254 -254
- package/vendor/node-addon-api/doc/escapable_handle_scope.md +80 -80
- package/vendor/node-addon-api/doc/external.md +63 -63
- package/vendor/node-addon-api/doc/function.md +402 -402
- package/vendor/node-addon-api/doc/function_reference.md +238 -238
- package/vendor/node-addon-api/doc/generator.md +13 -13
- package/vendor/node-addon-api/doc/handle_scope.md +63 -63
- package/vendor/node-addon-api/doc/hierarchy.md +91 -91
- package/vendor/node-addon-api/doc/instance_wrap.md +408 -408
- package/vendor/node-addon-api/doc/maybe.md +76 -76
- package/vendor/node-addon-api/doc/memory_management.md +27 -27
- package/vendor/node-addon-api/doc/name.md +29 -29
- package/vendor/node-addon-api/doc/node-gyp.md +82 -82
- package/vendor/node-addon-api/doc/number.md +163 -163
- package/vendor/node-addon-api/doc/object.md +432 -432
- package/vendor/node-addon-api/doc/object_lifetime_management.md +83 -83
- package/vendor/node-addon-api/doc/object_reference.md +117 -117
- package/vendor/node-addon-api/doc/object_wrap.md +561 -561
- package/vendor/node-addon-api/doc/prebuild_tools.md +16 -16
- package/vendor/node-addon-api/doc/promises.md +79 -79
- package/vendor/node-addon-api/doc/property_descriptor.md +286 -286
- package/vendor/node-addon-api/doc/propertylvalue.md +50 -50
- package/vendor/node-addon-api/doc/range_error.md +59 -59
- package/vendor/node-addon-api/doc/reference.md +113 -113
- package/vendor/node-addon-api/doc/setup.md +110 -110
- package/vendor/node-addon-api/doc/string.md +93 -93
- package/vendor/node-addon-api/doc/symbol.md +60 -60
- package/vendor/node-addon-api/doc/threadsafe.md +121 -121
- package/vendor/node-addon-api/doc/threadsafe_function.md +290 -290
- package/vendor/node-addon-api/doc/type_error.md +59 -59
- package/vendor/node-addon-api/doc/typed_array.md +78 -78
- package/vendor/node-addon-api/doc/typed_array_of.md +137 -137
- package/vendor/node-addon-api/doc/typed_threadsafe_function.md +306 -306
- package/vendor/node-addon-api/doc/value.md +340 -340
- package/vendor/node-addon-api/doc/version_management.md +43 -43
- package/vendor/node-addon-api/except.gypi +25 -25
- package/vendor/node-addon-api/index.js +11 -11
- package/vendor/node-addon-api/napi-inl.deprecated.h +192 -192
- package/vendor/node-addon-api/napi-inl.h +6209 -6209
- package/vendor/node-addon-api/napi.h +2983 -2983
- package/vendor/node-addon-api/node_api.gyp +9 -9
- package/vendor/node-addon-api/noexcept.gypi +26 -26
- package/vendor/node-addon-api/package-support.json +21 -21
- package/vendor/node-addon-api/package.json +399 -399
- package/vendor/node-addon-api/test/README.md +91 -91
- package/vendor/node-addon-api/test/addon.cc +36 -36
- package/vendor/node-addon-api/test/addon.js +11 -11
- package/vendor/node-addon-api/test/addon_build/index.js +49 -49
- package/vendor/node-addon-api/test/addon_build/tpl/addon.cc +17 -17
- package/vendor/node-addon-api/test/addon_build/tpl/binding.gyp +62 -62
- package/vendor/node-addon-api/test/addon_build/tpl/index.js +9 -9
- package/vendor/node-addon-api/test/addon_build/tpl/package.json +11 -11
- package/vendor/node-addon-api/test/addon_data.cc +99 -99
- package/vendor/node-addon-api/test/addon_data.js +46 -46
- package/vendor/node-addon-api/test/array_buffer.cc +243 -243
- package/vendor/node-addon-api/test/array_buffer.js +69 -69
- package/vendor/node-addon-api/test/async_context.cc +21 -21
- package/vendor/node-addon-api/test/async_context.js +86 -86
- package/vendor/node-addon-api/test/async_progress_queue_worker.cc +83 -83
- package/vendor/node-addon-api/test/async_progress_queue_worker.js +46 -46
- package/vendor/node-addon-api/test/async_progress_worker.cc +134 -134
- package/vendor/node-addon-api/test/async_progress_worker.js +61 -61
- package/vendor/node-addon-api/test/async_worker.cc +106 -106
- package/vendor/node-addon-api/test/async_worker.js +179 -179
- package/vendor/node-addon-api/test/async_worker_nocallback.js +13 -13
- package/vendor/node-addon-api/test/async_worker_persistent.cc +63 -63
- package/vendor/node-addon-api/test/async_worker_persistent.js +24 -24
- package/vendor/node-addon-api/test/basic_types/array.cc +40 -40
- package/vendor/node-addon-api/test/basic_types/array.js +35 -35
- package/vendor/node-addon-api/test/basic_types/boolean.cc +38 -38
- package/vendor/node-addon-api/test/basic_types/boolean.js +35 -35
- package/vendor/node-addon-api/test/basic_types/number.cc +99 -99
- package/vendor/node-addon-api/test/basic_types/number.js +114 -114
- package/vendor/node-addon-api/test/basic_types/value.cc +120 -120
- package/vendor/node-addon-api/test/basic_types/value.js +133 -133
- package/vendor/node-addon-api/test/bigint.cc +91 -91
- package/vendor/node-addon-api/test/bigint.js +53 -53
- package/vendor/node-addon-api/test/binding-swallowexcept.cc +12 -12
- package/vendor/node-addon-api/test/binding.cc +171 -171
- package/vendor/node-addon-api/test/binding.gyp +117 -117
- package/vendor/node-addon-api/test/buffer.cc +183 -183
- package/vendor/node-addon-api/test/buffer.js +69 -69
- package/vendor/node-addon-api/test/callbackscope.cc +22 -22
- package/vendor/node-addon-api/test/callbackscope.js +49 -49
- package/vendor/node-addon-api/test/common/index.js +113 -113
- package/vendor/node-addon-api/test/common/test_helper.h +61 -61
- package/vendor/node-addon-api/test/dataview/dataview.cc +48 -48
- package/vendor/node-addon-api/test/dataview/dataview.js +35 -35
- package/vendor/node-addon-api/test/dataview/dataview_read_write.cc +115 -115
- package/vendor/node-addon-api/test/dataview/dataview_read_write.js +90 -90
- package/vendor/node-addon-api/test/date.cc +44 -44
- package/vendor/node-addon-api/test/date.js +18 -18
- package/vendor/node-addon-api/test/env_cleanup.cc +88 -88
- package/vendor/node-addon-api/test/env_cleanup.js +56 -56
- package/vendor/node-addon-api/test/error.cc +287 -287
- package/vendor/node-addon-api/test/error.js +81 -81
- package/vendor/node-addon-api/test/error_handling_for_primitives.cc +13 -13
- package/vendor/node-addon-api/test/error_handling_for_primitives.js +29 -29
- package/vendor/node-addon-api/test/error_terminating_environment.js +94 -94
- package/vendor/node-addon-api/test/external.cc +81 -81
- package/vendor/node-addon-api/test/external.js +88 -88
- package/vendor/node-addon-api/test/function.cc +295 -295
- package/vendor/node-addon-api/test/function.js +121 -121
- package/vendor/node-addon-api/test/function_reference.cc +202 -202
- package/vendor/node-addon-api/test/function_reference.js +157 -157
- package/vendor/node-addon-api/test/globalObject/global_object.cc +61 -61
- package/vendor/node-addon-api/test/globalObject/global_object_delete_property.cc +31 -31
- package/vendor/node-addon-api/test/globalObject/global_object_delete_property.js +61 -61
- package/vendor/node-addon-api/test/globalObject/global_object_get_property.cc +40 -40
- package/vendor/node-addon-api/test/globalObject/global_object_get_property.js +57 -57
- package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.cc +28 -28
- package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.js +48 -48
- package/vendor/node-addon-api/test/globalObject/global_object_set_property.cc +30 -30
- package/vendor/node-addon-api/test/globalObject/global_object_set_property.js +58 -58
- package/vendor/node-addon-api/test/handlescope.cc +60 -60
- package/vendor/node-addon-api/test/handlescope.js +14 -14
- package/vendor/node-addon-api/test/index.js +136 -136
- package/vendor/node-addon-api/test/maybe/check.cc +23 -23
- package/vendor/node-addon-api/test/maybe/index.js +38 -38
- package/vendor/node-addon-api/test/memory_management.cc +17 -17
- package/vendor/node-addon-api/test/memory_management.js +9 -9
- package/vendor/node-addon-api/test/movable_callbacks.cc +23 -23
- package/vendor/node-addon-api/test/movable_callbacks.js +21 -21
- package/vendor/node-addon-api/test/name.cc +108 -108
- package/vendor/node-addon-api/test/name.js +59 -59
- package/vendor/node-addon-api/test/napi_child.js +14 -14
- package/vendor/node-addon-api/test/object/delete_property.cc +38 -38
- package/vendor/node-addon-api/test/object/delete_property.js +41 -41
- package/vendor/node-addon-api/test/object/finalizer.cc +29 -29
- package/vendor/node-addon-api/test/object/finalizer.js +28 -28
- package/vendor/node-addon-api/test/object/get_property.cc +34 -34
- package/vendor/node-addon-api/test/object/get_property.js +40 -40
- package/vendor/node-addon-api/test/object/has_own_property.cc +34 -34
- package/vendor/node-addon-api/test/object/has_own_property.js +34 -34
- package/vendor/node-addon-api/test/object/has_property.cc +38 -38
- package/vendor/node-addon-api/test/object/has_property.js +37 -37
- package/vendor/node-addon-api/test/object/object.cc +348 -348
- package/vendor/node-addon-api/test/object/object.js +217 -217
- package/vendor/node-addon-api/test/object/object_deprecated.cc +66 -66
- package/vendor/node-addon-api/test/object/object_deprecated.js +47 -47
- package/vendor/node-addon-api/test/object/object_freeze_seal.cc +25 -25
- package/vendor/node-addon-api/test/object/object_freeze_seal.js +61 -61
- package/vendor/node-addon-api/test/object/set_property.cc +37 -37
- package/vendor/node-addon-api/test/object/set_property.js +29 -29
- package/vendor/node-addon-api/test/object/subscript_operator.cc +42 -42
- package/vendor/node-addon-api/test/object/subscript_operator.js +17 -17
- package/vendor/node-addon-api/test/object_reference.cc +219 -219
- package/vendor/node-addon-api/test/object_reference.js +259 -259
- package/vendor/node-addon-api/test/objectwrap.cc +268 -268
- package/vendor/node-addon-api/test/objectwrap.js +284 -284
- package/vendor/node-addon-api/test/objectwrap_constructor_exception.cc +26 -26
- package/vendor/node-addon-api/test/objectwrap_constructor_exception.js +18 -18
- package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.cc +30 -30
- package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.js +13 -13
- package/vendor/node-addon-api/test/objectwrap_removewrap.cc +45 -45
- package/vendor/node-addon-api/test/objectwrap_removewrap.js +40 -40
- package/vendor/node-addon-api/test/objectwrap_worker_thread.js +19 -19
- package/vendor/node-addon-api/test/promise.cc +29 -29
- package/vendor/node-addon-api/test/promise.js +18 -18
- package/vendor/node-addon-api/test/reference.cc +24 -24
- package/vendor/node-addon-api/test/reference.js +14 -14
- package/vendor/node-addon-api/test/run_script.cc +56 -56
- package/vendor/node-addon-api/test/run_script.js +45 -45
- package/vendor/node-addon-api/test/symbol.cc +79 -79
- package/vendor/node-addon-api/test/symbol.js +73 -73
- package/vendor/node-addon-api/test/testUtil.js +54 -54
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.cc +195 -195
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.js +188 -188
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.cc +63 -63
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.js +12 -12
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.cc +115 -115
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.js +14 -14
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.cc +26 -26
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.js +7 -7
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.cc +225 -225
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.js +59 -59
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.cc +42 -42
- package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.js +53 -53
- package/vendor/node-addon-api/test/thunking_manual.cc +140 -140
- package/vendor/node-addon-api/test/thunking_manual.js +17 -17
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.cc +215 -215
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.js +188 -188
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc +68 -68
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js +12 -12
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc +127 -127
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.js +14 -14
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc +28 -28
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.js +7 -7
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc +237 -237
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.js +59 -59
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc +53 -53
- package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.js +53 -53
- package/vendor/node-addon-api/test/typedarray-bigint.js +58 -58
- package/vendor/node-addon-api/test/typedarray.cc +216 -216
- package/vendor/node-addon-api/test/typedarray.js +69 -69
- package/vendor/node-addon-api/test/version_management.cc +27 -27
- package/vendor/node-addon-api/test/version_management.js +31 -31
- package/vendor/node-addon-api/tools/README.md +73 -73
- package/vendor/node-addon-api/tools/check-napi.js +100 -100
- package/vendor/node-addon-api/tools/clang-format.js +68 -68
- package/vendor/node-addon-api/tools/conversion.js +309 -309
- package/vendor/node-addon-api/tools/eslint-format.js +71 -71
package/src/ffi.cc
CHANGED
|
@@ -1,504 +1,504 @@
|
|
|
1
|
-
// This program is free software: you can redistribute it and/or modify
|
|
2
|
-
// it under the terms of the GNU Affero General Public License as published by
|
|
3
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
-
// (at your option) any later version.
|
|
5
|
-
//
|
|
6
|
-
// This program is distributed in the hope that it will be useful,
|
|
7
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
-
// GNU Affero General Public License for more details.
|
|
10
|
-
//
|
|
11
|
-
// You should have received a copy of the GNU Affero General Public License
|
|
12
|
-
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
-
|
|
14
|
-
#include "vendor/libcc/libcc.hh"
|
|
15
|
-
#include "ffi.hh"
|
|
16
|
-
#include "call.hh"
|
|
17
|
-
#include "util.hh"
|
|
18
|
-
|
|
19
|
-
#ifdef _WIN32
|
|
20
|
-
#ifndef NOMINMAX
|
|
21
|
-
#define NOMINMAX
|
|
22
|
-
#endif
|
|
23
|
-
#ifndef WIN32_LEAN_AND_MEAN
|
|
24
|
-
#define WIN32_LEAN_AND_MEAN
|
|
25
|
-
#endif
|
|
26
|
-
#include <windows.h>
|
|
27
|
-
#include <ntsecapi.h>
|
|
28
|
-
#else
|
|
29
|
-
#include <dlfcn.h>
|
|
30
|
-
#include <unistd.h>
|
|
31
|
-
#endif
|
|
32
|
-
|
|
33
|
-
#include <napi.h>
|
|
34
|
-
#if NODE_WANT_INTERNALS
|
|
35
|
-
#include <env-inl.h>
|
|
36
|
-
#include <js_native_api_v8.h>
|
|
37
|
-
#endif
|
|
38
|
-
|
|
39
|
-
namespace RG {
|
|
40
|
-
|
|
41
|
-
// Value does not matter, the tag system uses memory addresses
|
|
42
|
-
static const int TypeInfoMarker = 0xdeadbeef;
|
|
43
|
-
|
|
44
|
-
static const TypeInfo *ResolveType(const InstanceData *instance, Napi::Value value)
|
|
45
|
-
{
|
|
46
|
-
if (value.IsString()) {
|
|
47
|
-
std::string str = value.As<Napi::String>();
|
|
48
|
-
|
|
49
|
-
const TypeInfo *type = instance->types_map.FindValue(str.c_str(), nullptr);
|
|
50
|
-
|
|
51
|
-
if (!type) {
|
|
52
|
-
ThrowError<Napi::TypeError>(value.Env(), "Unknown type string '%1'", str.c_str());
|
|
53
|
-
return nullptr;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return type;
|
|
57
|
-
} else if (CheckValueTag(instance, value, &TypeInfoMarker)) {
|
|
58
|
-
Napi::External<TypeInfo> external = value.As<Napi::External<TypeInfo>>();
|
|
59
|
-
|
|
60
|
-
const TypeInfo *type = external.Data();
|
|
61
|
-
RG_ASSERT(type);
|
|
62
|
-
|
|
63
|
-
return type;
|
|
64
|
-
} else {
|
|
65
|
-
ThrowError<Napi::TypeError>(value.Env(), "Unexpected %1 value as type specifier, expected string or type", GetValueType(instance, value));
|
|
66
|
-
return nullptr;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
static Napi::Value CreateStructType(const Napi::CallbackInfo &info)
|
|
71
|
-
{
|
|
72
|
-
Napi::Env env = info.Env();
|
|
73
|
-
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
74
|
-
|
|
75
|
-
if (info.Length() < 2) {
|
|
76
|
-
ThrowError<Napi::TypeError>(env, "Expected 2 arguments, got %1", info.Length());
|
|
77
|
-
return env.Null();
|
|
78
|
-
}
|
|
79
|
-
if (!info[0].IsString()) {
|
|
80
|
-
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for name, expected string", GetValueType(instance, info[0]));
|
|
81
|
-
return env.Null();
|
|
82
|
-
}
|
|
83
|
-
if (!info[1].IsObject()) {
|
|
84
|
-
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for members, expected object", GetValueType(instance, info[1]));
|
|
85
|
-
return env.Null();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
TypeInfo *type = instance->types.AppendDefault();
|
|
89
|
-
RG_DEFER_N(err_guard) { instance->types.RemoveLast(1); };
|
|
90
|
-
|
|
91
|
-
std::string name = info[0].As<Napi::String>();
|
|
92
|
-
Napi::Object obj = info[1].As<Napi::Object>();
|
|
93
|
-
Napi::Array keys = obj.GetPropertyNames();
|
|
94
|
-
|
|
95
|
-
type->name = DuplicateString(name.c_str(), &instance->str_alloc).ptr;
|
|
96
|
-
|
|
97
|
-
type->primitive = PrimitiveKind::Record;
|
|
98
|
-
type->align = 1;
|
|
99
|
-
|
|
100
|
-
for (uint32_t i = 0; i < keys.Length(); i++) {
|
|
101
|
-
RecordMember member = {};
|
|
102
|
-
|
|
103
|
-
std::string key = ((Napi::Value)keys[i]).As<Napi::String>();
|
|
104
|
-
Napi::Value value = obj[key];
|
|
105
|
-
|
|
106
|
-
member.name = DuplicateString(key.c_str(), &instance->str_alloc).ptr;
|
|
107
|
-
member.type = ResolveType(instance, value);
|
|
108
|
-
if (!member.type)
|
|
109
|
-
return env.Null();
|
|
110
|
-
|
|
111
|
-
type->size += member.type->size;
|
|
112
|
-
type->align = std::max(type->align, member.type->align);
|
|
113
|
-
|
|
114
|
-
type->members.Append(member);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
type->size = (int16_t)AlignLen(type->size, type->align);
|
|
118
|
-
|
|
119
|
-
// If the insert succeeds, we cannot fail anymore
|
|
120
|
-
if (!instance->types_map.TrySet(type).second) {
|
|
121
|
-
ThrowError<Napi::Error>(env, "Duplicate type name '%1'", type->name);
|
|
122
|
-
return env.Null();
|
|
123
|
-
}
|
|
124
|
-
err_guard.Disable();
|
|
125
|
-
|
|
126
|
-
Napi::External<TypeInfo> external = Napi::External<TypeInfo>::New(env, type);
|
|
127
|
-
SetValueTag(instance, external, &TypeInfoMarker);
|
|
128
|
-
|
|
129
|
-
return external;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
static Napi::Value CreatePointerType(const Napi::CallbackInfo &info)
|
|
133
|
-
{
|
|
134
|
-
Napi::Env env = info.Env();
|
|
135
|
-
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
136
|
-
|
|
137
|
-
if (info.Length() < 1) {
|
|
138
|
-
ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
|
|
139
|
-
return env.Null();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const TypeInfo *ref = ResolveType(instance, info[0]);
|
|
143
|
-
if (!ref)
|
|
144
|
-
return env.Null();
|
|
145
|
-
|
|
146
|
-
char name_buf[256];
|
|
147
|
-
Fmt(name_buf, "%1%2*", ref->name, ref->primitive == PrimitiveKind::Pointer ? "" : " ");
|
|
148
|
-
|
|
149
|
-
TypeInfo *type = instance->types_map.FindValue(name_buf, nullptr);
|
|
150
|
-
|
|
151
|
-
if (!type) {
|
|
152
|
-
type = instance->types.AppendDefault();
|
|
153
|
-
|
|
154
|
-
type->name = DuplicateString(name_buf, &instance->str_alloc).ptr;
|
|
155
|
-
|
|
156
|
-
type->primitive = PrimitiveKind::Pointer;
|
|
157
|
-
type->size = sizeof(void *);
|
|
158
|
-
type->align = sizeof(void *);
|
|
159
|
-
type->ref = ref;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
Napi::External<TypeInfo> external = Napi::External<TypeInfo>::New(env, type);
|
|
163
|
-
SetValueTag(instance, external, &TypeInfoMarker);
|
|
164
|
-
|
|
165
|
-
return external;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
|
|
169
|
-
{
|
|
170
|
-
Napi::Env env = info.Env();
|
|
171
|
-
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
172
|
-
|
|
173
|
-
if (info.Length() < 2) {
|
|
174
|
-
ThrowError<Napi::TypeError>(env, "Expected 2 arguments, not %1", info.Length());
|
|
175
|
-
return env.Null();
|
|
176
|
-
}
|
|
177
|
-
if (!info[0].IsString() && !info[0].IsNull()) {
|
|
178
|
-
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for filename, expected string or null", GetValueType(instance, info[0]));
|
|
179
|
-
return env.Null();
|
|
180
|
-
}
|
|
181
|
-
if (!info[1].IsObject()) {
|
|
182
|
-
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for functions, expected object", GetValueType(instance, info[1]));
|
|
183
|
-
return env.Null();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
std::shared_ptr<LibraryData> lib = std::make_shared<LibraryData>();
|
|
187
|
-
|
|
188
|
-
Napi::Object obj = Napi::Object::New(env);
|
|
189
|
-
|
|
190
|
-
// Load shared library
|
|
191
|
-
#ifdef _WIN32
|
|
192
|
-
if (info[0].IsString()) {
|
|
193
|
-
std::u16string filename = info[0].As<Napi::String>();
|
|
194
|
-
lib->module = LoadLibraryW((LPCWSTR)filename.c_str());
|
|
195
|
-
|
|
196
|
-
if (!lib->module) {
|
|
197
|
-
ThrowError<Napi::Error>(env, "Failed to load shared library: %1", GetWin32ErrorString());
|
|
198
|
-
return env.Null();
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
lib->module = GetModuleHandle(nullptr);
|
|
202
|
-
RG_ASSERT(lib->module);
|
|
203
|
-
}
|
|
204
|
-
#else
|
|
205
|
-
if (info[0].IsString()) {
|
|
206
|
-
std::string filename = info[0].As<Napi::String>();
|
|
207
|
-
lib->module = dlopen(filename.c_str(), RTLD_NOW);
|
|
208
|
-
|
|
209
|
-
if (!lib->module) {
|
|
210
|
-
const char *msg = dlerror();
|
|
211
|
-
|
|
212
|
-
if (StartsWith(msg, filename.c_str())) {
|
|
213
|
-
msg += filename.length();
|
|
214
|
-
}
|
|
215
|
-
while (strchr(": ", msg[0])) {
|
|
216
|
-
msg++;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
ThrowError<Napi::Error>(env, "Failed to load shared library: %1", msg);
|
|
220
|
-
return env.Null();
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
lib->module = RTLD_DEFAULT;
|
|
224
|
-
}
|
|
225
|
-
#endif
|
|
226
|
-
|
|
227
|
-
Napi::Object functions = info[1].As<Napi::Array>();
|
|
228
|
-
Napi::Array keys = functions.GetPropertyNames();
|
|
229
|
-
Size extra_size = 0;
|
|
230
|
-
|
|
231
|
-
for (uint32_t i = 0; i < keys.Length(); i++) {
|
|
232
|
-
FunctionInfo *func = new FunctionInfo();
|
|
233
|
-
RG_DEFER_N(func_guard) { delete func; };
|
|
234
|
-
|
|
235
|
-
std::string key = ((Napi::Value)keys[i]).As<Napi::String>();
|
|
236
|
-
Napi::Array value = ((Napi::Value)functions[key]).As<Napi::Array>();
|
|
237
|
-
|
|
238
|
-
func->name = DuplicateString(key.c_str(), &lib->str_alloc).ptr;
|
|
239
|
-
func->decorated_name = func->name;
|
|
240
|
-
func->lib = lib;
|
|
241
|
-
|
|
242
|
-
if (!value.IsArray()) {
|
|
243
|
-
ThrowError<Napi::TypeError>(env, "Unexpexted %1 value for signature of '%2', expected an array", GetValueType(instance, value), func->name);
|
|
244
|
-
return env.Null();
|
|
245
|
-
}
|
|
246
|
-
if (value.Length() < 2 || value.Length() > 3) {
|
|
247
|
-
ThrowError<Napi::TypeError>(env, "Unexpected array of length %1 for '%2', expected 2 or 3 elements", value.Length(), func->name);
|
|
248
|
-
return env.Null();
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
Napi::Array parameters;
|
|
252
|
-
|
|
253
|
-
if (((Napi::Value)value[1u]).IsString()) {
|
|
254
|
-
std::string conv = ((Napi::Value)value[1u]).As<Napi::String>();
|
|
255
|
-
|
|
256
|
-
if (conv == "cdecl" || conv == "__cdecl") {
|
|
257
|
-
func->convention = CallConvention::Default;
|
|
258
|
-
} else if (conv == "stdcall" || conv == "__stdcall") {
|
|
259
|
-
func->convention = CallConvention::Stdcall;
|
|
260
|
-
} else {
|
|
261
|
-
ThrowError<Napi::Error>(env, "Unknown calling convention '%1'", conv.c_str());
|
|
262
|
-
return env.Null();
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (!((Napi::Value)value[2u]).IsArray()) {
|
|
266
|
-
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for parameters of '%2', expected an array", GetValueType(instance, (Napi::Value)value[1u]), func->name);
|
|
267
|
-
return env.Null();
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
parameters = ((Napi::Value)value[2u]).As<Napi::Array>();
|
|
271
|
-
} else {
|
|
272
|
-
if (!((Napi::Value)value[1u]).IsArray()) {
|
|
273
|
-
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for parameters of '%2', expected an array", GetValueType(instance, (Napi::Value)value[1u]), func->name);
|
|
274
|
-
return env.Null();
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
parameters = ((Napi::Value)value[1u]).As<Napi::Array>();
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
func->ret.type = ResolveType(instance, value[0u]);
|
|
281
|
-
if (!func->ret.type)
|
|
282
|
-
return env.Null();
|
|
283
|
-
func->scratch_size = AlignLen(func->ret.type->size, 16);
|
|
284
|
-
|
|
285
|
-
for (uint32_t j = 0; j < parameters.Length(); j++) {
|
|
286
|
-
ParameterInfo param = {};
|
|
287
|
-
|
|
288
|
-
param.type = ResolveType(instance, parameters[j]);
|
|
289
|
-
if (!param.type)
|
|
290
|
-
return env.Null();
|
|
291
|
-
if (param.type->primitive == PrimitiveKind::Void) {
|
|
292
|
-
ThrowError<Napi::TypeError>(env, "Type void cannot be used as a parameter");
|
|
293
|
-
return env.Null();
|
|
294
|
-
}
|
|
295
|
-
func->parameters.Append(param);
|
|
296
|
-
|
|
297
|
-
func->scratch_size += AlignLen(param.type->size, 16);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// That's enough extra space for pretty much every ABI supported, with 16 bytes of
|
|
301
|
-
// bonus for each in case we need to put pointers and exta-align them.
|
|
302
|
-
extra_size = std::max(extra_size, func->scratch_size +
|
|
303
|
-
AlignLen(8 * (func->parameters.len + 1), 16));
|
|
304
|
-
|
|
305
|
-
if (!AnalyseFunction(func))
|
|
306
|
-
return env.Null();
|
|
307
|
-
|
|
308
|
-
#ifdef _WIN32
|
|
309
|
-
func->func = (void *)GetProcAddress((HMODULE)lib->module, func->decorated_name);
|
|
310
|
-
#else
|
|
311
|
-
func->func = dlsym(lib->module, func->decorated_name);
|
|
312
|
-
#endif
|
|
313
|
-
if (!func->func) {
|
|
314
|
-
ThrowError<Napi::Error>(env, "Cannot find function '%1' in shared library", key.c_str());
|
|
315
|
-
return env.Null();
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
Napi::Function wrapper = Napi::Function::New(env, TranslateCall, key.c_str(), (void *)func);
|
|
319
|
-
wrapper.AddFinalizer([](Napi::Env, FunctionInfo *func) { delete func; }, func);
|
|
320
|
-
func_guard.Disable();
|
|
321
|
-
|
|
322
|
-
obj.Set(key, wrapper);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
lib->stack.AppendDefault(Mebibytes(1) + extra_size);
|
|
326
|
-
|
|
327
|
-
return obj;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
LibraryData::~LibraryData()
|
|
331
|
-
{
|
|
332
|
-
#ifdef _WIN32
|
|
333
|
-
if (module && module != GetModuleHandle(nullptr)) {
|
|
334
|
-
FreeLibrary((HMODULE)module);
|
|
335
|
-
}
|
|
336
|
-
#else
|
|
337
|
-
if (module && module != RTLD_DEFAULT) {
|
|
338
|
-
dlclose(module);
|
|
339
|
-
}
|
|
340
|
-
#endif
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
static void RegisterPrimitiveType(InstanceData *instance, const char *name, PrimitiveKind primitive, int16_t size)
|
|
344
|
-
{
|
|
345
|
-
TypeInfo *type = instance->types.AppendDefault();
|
|
346
|
-
|
|
347
|
-
type->name = name;
|
|
348
|
-
|
|
349
|
-
type->primitive = primitive;
|
|
350
|
-
type->size = size;
|
|
351
|
-
type->align = size;
|
|
352
|
-
|
|
353
|
-
RG_ASSERT(!instance->types_map.Find(name));
|
|
354
|
-
instance->types_map.Set(type);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
static Napi::Object InitBaseTypes(Napi::Env env)
|
|
358
|
-
{
|
|
359
|
-
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
360
|
-
|
|
361
|
-
RG_ASSERT(!instance->types.len);
|
|
362
|
-
|
|
363
|
-
RegisterPrimitiveType(instance, "void", PrimitiveKind::Void, 0);
|
|
364
|
-
RegisterPrimitiveType(instance, "bool", PrimitiveKind::Bool, 1);
|
|
365
|
-
RegisterPrimitiveType(instance, "int8", PrimitiveKind::Int8, 1);
|
|
366
|
-
RegisterPrimitiveType(instance, "int8_t", PrimitiveKind::Int8, 1);
|
|
367
|
-
RegisterPrimitiveType(instance, "uint8", PrimitiveKind::UInt8, 1);
|
|
368
|
-
RegisterPrimitiveType(instance, "uint8_t", PrimitiveKind::UInt8, 1);
|
|
369
|
-
RegisterPrimitiveType(instance, "char", PrimitiveKind::Int8, 1);
|
|
370
|
-
RegisterPrimitiveType(instance, "uchar", PrimitiveKind::UInt8, 1);
|
|
371
|
-
RegisterPrimitiveType(instance, "unsigned char", PrimitiveKind::UInt8, 1);
|
|
372
|
-
RegisterPrimitiveType(instance, "int16", PrimitiveKind::Int16, 2);
|
|
373
|
-
RegisterPrimitiveType(instance, "int16_t", PrimitiveKind::Int16, 2);
|
|
374
|
-
RegisterPrimitiveType(instance, "uint16", PrimitiveKind::UInt16, 2);
|
|
375
|
-
RegisterPrimitiveType(instance, "uint16_t", PrimitiveKind::UInt16, 2);
|
|
376
|
-
RegisterPrimitiveType(instance, "short", PrimitiveKind::Int16, 2);
|
|
377
|
-
RegisterPrimitiveType(instance, "ushort", PrimitiveKind::UInt16, 2);
|
|
378
|
-
RegisterPrimitiveType(instance, "unsigned short", PrimitiveKind::UInt16, 2);
|
|
379
|
-
RegisterPrimitiveType(instance, "int32", PrimitiveKind::Int32, 4);
|
|
380
|
-
RegisterPrimitiveType(instance, "int32_t", PrimitiveKind::Int32, 4);
|
|
381
|
-
RegisterPrimitiveType(instance, "uint32", PrimitiveKind::UInt32, 4);
|
|
382
|
-
RegisterPrimitiveType(instance, "uint32_t", PrimitiveKind::UInt32, 4);
|
|
383
|
-
RegisterPrimitiveType(instance, "int", PrimitiveKind::Int32, 4);
|
|
384
|
-
RegisterPrimitiveType(instance, "uint", PrimitiveKind::UInt32, 4);
|
|
385
|
-
RegisterPrimitiveType(instance, "unsigned int", PrimitiveKind::UInt32, 4);
|
|
386
|
-
RegisterPrimitiveType(instance, "int64", PrimitiveKind::Int64, 8);
|
|
387
|
-
RegisterPrimitiveType(instance, "int64_t", PrimitiveKind::Int64, 8);
|
|
388
|
-
RegisterPrimitiveType(instance, "uint64", PrimitiveKind::UInt64, 8);
|
|
389
|
-
RegisterPrimitiveType(instance, "uint64_t", PrimitiveKind::UInt64, 8);
|
|
390
|
-
#if ULONG_MAX == UINT64_MAX
|
|
391
|
-
RegisterPrimitiveType(instance, "long", PrimitiveKind::Int64, 8);
|
|
392
|
-
RegisterPrimitiveType(instance, "ulong", PrimitiveKind::UInt64, 8);
|
|
393
|
-
RegisterPrimitiveType(instance, "unsigned long", PrimitiveKind::UInt64, 8);
|
|
394
|
-
#else
|
|
395
|
-
RegisterPrimitiveType(instance, "long", PrimitiveKind::Int32, 4);
|
|
396
|
-
RegisterPrimitiveType(instance, "ulong", PrimitiveKind::UInt32, 4);
|
|
397
|
-
RegisterPrimitiveType(instance, "unsigned long", PrimitiveKind::UInt64, 4);
|
|
398
|
-
#endif
|
|
399
|
-
RegisterPrimitiveType(instance, "longlong", PrimitiveKind::Int64, 8);
|
|
400
|
-
RegisterPrimitiveType(instance, "long long", PrimitiveKind::Int64, 8);
|
|
401
|
-
RegisterPrimitiveType(instance, "ulonglong", PrimitiveKind::UInt64, 8);
|
|
402
|
-
RegisterPrimitiveType(instance, "unsigned long long", PrimitiveKind::UInt64, 8);
|
|
403
|
-
RegisterPrimitiveType(instance, "float32", PrimitiveKind::Float32, 4);
|
|
404
|
-
RegisterPrimitiveType(instance, "float64", PrimitiveKind::Float64, 8);
|
|
405
|
-
RegisterPrimitiveType(instance, "float", PrimitiveKind::Float32, 4);
|
|
406
|
-
RegisterPrimitiveType(instance, "double", PrimitiveKind::Float64, 8);
|
|
407
|
-
RegisterPrimitiveType(instance, "string", PrimitiveKind::String, sizeof(void *));
|
|
408
|
-
|
|
409
|
-
Napi::Object types = Napi::Object::New(env);
|
|
410
|
-
for (TypeInfo &type: instance->types) {
|
|
411
|
-
Napi::External<TypeInfo> external = Napi::External<TypeInfo>::New(env, &type);
|
|
412
|
-
SetValueTag(instance, external, &TypeInfoMarker);
|
|
413
|
-
|
|
414
|
-
types.Set(type.name, external);
|
|
415
|
-
|
|
416
|
-
if (strchr(type.name, ' ')) {
|
|
417
|
-
char name_buf[64] = {};
|
|
418
|
-
for (Size i = 0; type.name[i]; i++) {
|
|
419
|
-
RG_ASSERT(i < RG_SIZE(name_buf) - 1);
|
|
420
|
-
name_buf[i] = type.name[i] != ' ' ? type.name[i] : '_';
|
|
421
|
-
}
|
|
422
|
-
types.Set(name_buf, external);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
types.Freeze();
|
|
426
|
-
|
|
427
|
-
return types;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
#if NODE_WANT_INTERNALS
|
|
433
|
-
|
|
434
|
-
static void SetValue(node::Environment *env, v8::Local<v8::Object> target,
|
|
435
|
-
const char *name, Napi::Value value)
|
|
436
|
-
{
|
|
437
|
-
v8::Isolate *isolate = env->isolate();
|
|
438
|
-
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
439
|
-
|
|
440
|
-
v8::NewStringType str_type = v8::NewStringType::kInternalized;
|
|
441
|
-
v8::Local<v8::String> str = v8::String::NewFromUtf8(isolate, name, str_type).ToLocalChecked();
|
|
442
|
-
|
|
443
|
-
target->Set(context, str, v8impl::V8LocalValueFromJsValue(value)).Check();
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
static void InitInternal(v8::Local<v8::Object> target, v8::Local<v8::Value>,
|
|
447
|
-
v8::Local<v8::Context> context, void *)
|
|
448
|
-
{
|
|
449
|
-
using namespace RG;
|
|
450
|
-
|
|
451
|
-
node::Environment *env = node::Environment::GetCurrent(context);
|
|
452
|
-
|
|
453
|
-
// Not very clean but I don't know enough about Node and V8 to do better...
|
|
454
|
-
// ... and it seems to work okay.
|
|
455
|
-
napi_env env_napi = new napi_env__(context);
|
|
456
|
-
Napi::Env env_cxx(env_napi);
|
|
457
|
-
env->AtExit([](void *udata) {
|
|
458
|
-
napi_env env_napi = (napi_env)udata;
|
|
459
|
-
delete env_napi;
|
|
460
|
-
}, env_napi);
|
|
461
|
-
|
|
462
|
-
InstanceData *instance = new InstanceData();
|
|
463
|
-
env_cxx.SetInstanceData(instance);
|
|
464
|
-
|
|
465
|
-
FillRandomSafe(&instance->tag_lower, RG_SIZE(instance->tag_lower));
|
|
466
|
-
|
|
467
|
-
SetValue(env, target, "struct", Napi::Function::New(env_napi, CreateStructType));
|
|
468
|
-
SetValue(env, target, "pointer", Napi::Function::New(env_napi, CreatePointerType));
|
|
469
|
-
SetValue(env, target, "load", Napi::Function::New(env_napi, LoadSharedLibrary));
|
|
470
|
-
SetValue(env, target, "internal", Napi::Boolean::New(env_napi, true));
|
|
471
|
-
|
|
472
|
-
Napi::Object types = InitBaseTypes(env_cxx);
|
|
473
|
-
SetValue(env, target, "types", types);
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
#else
|
|
477
|
-
|
|
478
|
-
static Napi::Object InitModule(Napi::Env env, Napi::Object exports)
|
|
479
|
-
{
|
|
480
|
-
using namespace RG;
|
|
481
|
-
|
|
482
|
-
InstanceData *instance = new InstanceData();
|
|
483
|
-
env.SetInstanceData(instance);
|
|
484
|
-
|
|
485
|
-
FillRandomSafe(&instance->tag_lower, RG_SIZE(instance->tag_lower));
|
|
486
|
-
|
|
487
|
-
exports.Set("struct", Napi::Function::New(env, CreateStructType));
|
|
488
|
-
exports.Set("pointer", Napi::Function::New(env, CreatePointerType));
|
|
489
|
-
exports.Set("load", Napi::Function::New(env, LoadSharedLibrary));
|
|
490
|
-
exports.Set("internal", Napi::Boolean::New(env, false));
|
|
491
|
-
|
|
492
|
-
Napi::Object types = InitBaseTypes(env);
|
|
493
|
-
exports.Set("types", types);
|
|
494
|
-
|
|
495
|
-
return exports;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
#endif
|
|
499
|
-
|
|
500
|
-
#if NODE_WANT_INTERNALS
|
|
501
|
-
NODE_MODULE_CONTEXT_AWARE_INTERNAL(koffi, InitInternal);
|
|
502
|
-
#else
|
|
503
|
-
NODE_API_MODULE(koffi, InitModule);
|
|
504
|
-
#endif
|
|
1
|
+
// This program is free software: you can redistribute it and/or modify
|
|
2
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
3
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
+
// (at your option) any later version.
|
|
5
|
+
//
|
|
6
|
+
// This program is distributed in the hope that it will be useful,
|
|
7
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
+
// GNU Affero General Public License for more details.
|
|
10
|
+
//
|
|
11
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
12
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
+
|
|
14
|
+
#include "vendor/libcc/libcc.hh"
|
|
15
|
+
#include "ffi.hh"
|
|
16
|
+
#include "call.hh"
|
|
17
|
+
#include "util.hh"
|
|
18
|
+
|
|
19
|
+
#ifdef _WIN32
|
|
20
|
+
#ifndef NOMINMAX
|
|
21
|
+
#define NOMINMAX
|
|
22
|
+
#endif
|
|
23
|
+
#ifndef WIN32_LEAN_AND_MEAN
|
|
24
|
+
#define WIN32_LEAN_AND_MEAN
|
|
25
|
+
#endif
|
|
26
|
+
#include <windows.h>
|
|
27
|
+
#include <ntsecapi.h>
|
|
28
|
+
#else
|
|
29
|
+
#include <dlfcn.h>
|
|
30
|
+
#include <unistd.h>
|
|
31
|
+
#endif
|
|
32
|
+
|
|
33
|
+
#include <napi.h>
|
|
34
|
+
#if NODE_WANT_INTERNALS
|
|
35
|
+
#include <env-inl.h>
|
|
36
|
+
#include <js_native_api_v8.h>
|
|
37
|
+
#endif
|
|
38
|
+
|
|
39
|
+
namespace RG {
|
|
40
|
+
|
|
41
|
+
// Value does not matter, the tag system uses memory addresses
|
|
42
|
+
static const int TypeInfoMarker = 0xdeadbeef;
|
|
43
|
+
|
|
44
|
+
static const TypeInfo *ResolveType(const InstanceData *instance, Napi::Value value)
|
|
45
|
+
{
|
|
46
|
+
if (value.IsString()) {
|
|
47
|
+
std::string str = value.As<Napi::String>();
|
|
48
|
+
|
|
49
|
+
const TypeInfo *type = instance->types_map.FindValue(str.c_str(), nullptr);
|
|
50
|
+
|
|
51
|
+
if (!type) {
|
|
52
|
+
ThrowError<Napi::TypeError>(value.Env(), "Unknown type string '%1'", str.c_str());
|
|
53
|
+
return nullptr;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return type;
|
|
57
|
+
} else if (CheckValueTag(instance, value, &TypeInfoMarker)) {
|
|
58
|
+
Napi::External<TypeInfo> external = value.As<Napi::External<TypeInfo>>();
|
|
59
|
+
|
|
60
|
+
const TypeInfo *type = external.Data();
|
|
61
|
+
RG_ASSERT(type);
|
|
62
|
+
|
|
63
|
+
return type;
|
|
64
|
+
} else {
|
|
65
|
+
ThrowError<Napi::TypeError>(value.Env(), "Unexpected %1 value as type specifier, expected string or type", GetValueType(instance, value));
|
|
66
|
+
return nullptr;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static Napi::Value CreateStructType(const Napi::CallbackInfo &info)
|
|
71
|
+
{
|
|
72
|
+
Napi::Env env = info.Env();
|
|
73
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
74
|
+
|
|
75
|
+
if (info.Length() < 2) {
|
|
76
|
+
ThrowError<Napi::TypeError>(env, "Expected 2 arguments, got %1", info.Length());
|
|
77
|
+
return env.Null();
|
|
78
|
+
}
|
|
79
|
+
if (!info[0].IsString()) {
|
|
80
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for name, expected string", GetValueType(instance, info[0]));
|
|
81
|
+
return env.Null();
|
|
82
|
+
}
|
|
83
|
+
if (!info[1].IsObject()) {
|
|
84
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for members, expected object", GetValueType(instance, info[1]));
|
|
85
|
+
return env.Null();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
TypeInfo *type = instance->types.AppendDefault();
|
|
89
|
+
RG_DEFER_N(err_guard) { instance->types.RemoveLast(1); };
|
|
90
|
+
|
|
91
|
+
std::string name = info[0].As<Napi::String>();
|
|
92
|
+
Napi::Object obj = info[1].As<Napi::Object>();
|
|
93
|
+
Napi::Array keys = obj.GetPropertyNames();
|
|
94
|
+
|
|
95
|
+
type->name = DuplicateString(name.c_str(), &instance->str_alloc).ptr;
|
|
96
|
+
|
|
97
|
+
type->primitive = PrimitiveKind::Record;
|
|
98
|
+
type->align = 1;
|
|
99
|
+
|
|
100
|
+
for (uint32_t i = 0; i < keys.Length(); i++) {
|
|
101
|
+
RecordMember member = {};
|
|
102
|
+
|
|
103
|
+
std::string key = ((Napi::Value)keys[i]).As<Napi::String>();
|
|
104
|
+
Napi::Value value = obj[key];
|
|
105
|
+
|
|
106
|
+
member.name = DuplicateString(key.c_str(), &instance->str_alloc).ptr;
|
|
107
|
+
member.type = ResolveType(instance, value);
|
|
108
|
+
if (!member.type)
|
|
109
|
+
return env.Null();
|
|
110
|
+
|
|
111
|
+
type->size += member.type->size;
|
|
112
|
+
type->align = std::max(type->align, member.type->align);
|
|
113
|
+
|
|
114
|
+
type->members.Append(member);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
type->size = (int16_t)AlignLen(type->size, type->align);
|
|
118
|
+
|
|
119
|
+
// If the insert succeeds, we cannot fail anymore
|
|
120
|
+
if (!instance->types_map.TrySet(type).second) {
|
|
121
|
+
ThrowError<Napi::Error>(env, "Duplicate type name '%1'", type->name);
|
|
122
|
+
return env.Null();
|
|
123
|
+
}
|
|
124
|
+
err_guard.Disable();
|
|
125
|
+
|
|
126
|
+
Napi::External<TypeInfo> external = Napi::External<TypeInfo>::New(env, type);
|
|
127
|
+
SetValueTag(instance, external, &TypeInfoMarker);
|
|
128
|
+
|
|
129
|
+
return external;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static Napi::Value CreatePointerType(const Napi::CallbackInfo &info)
|
|
133
|
+
{
|
|
134
|
+
Napi::Env env = info.Env();
|
|
135
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
136
|
+
|
|
137
|
+
if (info.Length() < 1) {
|
|
138
|
+
ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
|
|
139
|
+
return env.Null();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const TypeInfo *ref = ResolveType(instance, info[0]);
|
|
143
|
+
if (!ref)
|
|
144
|
+
return env.Null();
|
|
145
|
+
|
|
146
|
+
char name_buf[256];
|
|
147
|
+
Fmt(name_buf, "%1%2*", ref->name, ref->primitive == PrimitiveKind::Pointer ? "" : " ");
|
|
148
|
+
|
|
149
|
+
TypeInfo *type = instance->types_map.FindValue(name_buf, nullptr);
|
|
150
|
+
|
|
151
|
+
if (!type) {
|
|
152
|
+
type = instance->types.AppendDefault();
|
|
153
|
+
|
|
154
|
+
type->name = DuplicateString(name_buf, &instance->str_alloc).ptr;
|
|
155
|
+
|
|
156
|
+
type->primitive = PrimitiveKind::Pointer;
|
|
157
|
+
type->size = sizeof(void *);
|
|
158
|
+
type->align = sizeof(void *);
|
|
159
|
+
type->ref = ref;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
Napi::External<TypeInfo> external = Napi::External<TypeInfo>::New(env, type);
|
|
163
|
+
SetValueTag(instance, external, &TypeInfoMarker);
|
|
164
|
+
|
|
165
|
+
return external;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
|
|
169
|
+
{
|
|
170
|
+
Napi::Env env = info.Env();
|
|
171
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
172
|
+
|
|
173
|
+
if (info.Length() < 2) {
|
|
174
|
+
ThrowError<Napi::TypeError>(env, "Expected 2 arguments, not %1", info.Length());
|
|
175
|
+
return env.Null();
|
|
176
|
+
}
|
|
177
|
+
if (!info[0].IsString() && !info[0].IsNull()) {
|
|
178
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for filename, expected string or null", GetValueType(instance, info[0]));
|
|
179
|
+
return env.Null();
|
|
180
|
+
}
|
|
181
|
+
if (!info[1].IsObject()) {
|
|
182
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for functions, expected object", GetValueType(instance, info[1]));
|
|
183
|
+
return env.Null();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
std::shared_ptr<LibraryData> lib = std::make_shared<LibraryData>();
|
|
187
|
+
|
|
188
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
189
|
+
|
|
190
|
+
// Load shared library
|
|
191
|
+
#ifdef _WIN32
|
|
192
|
+
if (info[0].IsString()) {
|
|
193
|
+
std::u16string filename = info[0].As<Napi::String>();
|
|
194
|
+
lib->module = LoadLibraryW((LPCWSTR)filename.c_str());
|
|
195
|
+
|
|
196
|
+
if (!lib->module) {
|
|
197
|
+
ThrowError<Napi::Error>(env, "Failed to load shared library: %1", GetWin32ErrorString());
|
|
198
|
+
return env.Null();
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
lib->module = GetModuleHandle(nullptr);
|
|
202
|
+
RG_ASSERT(lib->module);
|
|
203
|
+
}
|
|
204
|
+
#else
|
|
205
|
+
if (info[0].IsString()) {
|
|
206
|
+
std::string filename = info[0].As<Napi::String>();
|
|
207
|
+
lib->module = dlopen(filename.c_str(), RTLD_NOW);
|
|
208
|
+
|
|
209
|
+
if (!lib->module) {
|
|
210
|
+
const char *msg = dlerror();
|
|
211
|
+
|
|
212
|
+
if (StartsWith(msg, filename.c_str())) {
|
|
213
|
+
msg += filename.length();
|
|
214
|
+
}
|
|
215
|
+
while (strchr(": ", msg[0])) {
|
|
216
|
+
msg++;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
ThrowError<Napi::Error>(env, "Failed to load shared library: %1", msg);
|
|
220
|
+
return env.Null();
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
lib->module = RTLD_DEFAULT;
|
|
224
|
+
}
|
|
225
|
+
#endif
|
|
226
|
+
|
|
227
|
+
Napi::Object functions = info[1].As<Napi::Array>();
|
|
228
|
+
Napi::Array keys = functions.GetPropertyNames();
|
|
229
|
+
Size extra_size = 0;
|
|
230
|
+
|
|
231
|
+
for (uint32_t i = 0; i < keys.Length(); i++) {
|
|
232
|
+
FunctionInfo *func = new FunctionInfo();
|
|
233
|
+
RG_DEFER_N(func_guard) { delete func; };
|
|
234
|
+
|
|
235
|
+
std::string key = ((Napi::Value)keys[i]).As<Napi::String>();
|
|
236
|
+
Napi::Array value = ((Napi::Value)functions[key]).As<Napi::Array>();
|
|
237
|
+
|
|
238
|
+
func->name = DuplicateString(key.c_str(), &lib->str_alloc).ptr;
|
|
239
|
+
func->decorated_name = func->name;
|
|
240
|
+
func->lib = lib;
|
|
241
|
+
|
|
242
|
+
if (!value.IsArray()) {
|
|
243
|
+
ThrowError<Napi::TypeError>(env, "Unexpexted %1 value for signature of '%2', expected an array", GetValueType(instance, value), func->name);
|
|
244
|
+
return env.Null();
|
|
245
|
+
}
|
|
246
|
+
if (value.Length() < 2 || value.Length() > 3) {
|
|
247
|
+
ThrowError<Napi::TypeError>(env, "Unexpected array of length %1 for '%2', expected 2 or 3 elements", value.Length(), func->name);
|
|
248
|
+
return env.Null();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
Napi::Array parameters;
|
|
252
|
+
|
|
253
|
+
if (((Napi::Value)value[1u]).IsString()) {
|
|
254
|
+
std::string conv = ((Napi::Value)value[1u]).As<Napi::String>();
|
|
255
|
+
|
|
256
|
+
if (conv == "cdecl" || conv == "__cdecl") {
|
|
257
|
+
func->convention = CallConvention::Default;
|
|
258
|
+
} else if (conv == "stdcall" || conv == "__stdcall") {
|
|
259
|
+
func->convention = CallConvention::Stdcall;
|
|
260
|
+
} else {
|
|
261
|
+
ThrowError<Napi::Error>(env, "Unknown calling convention '%1'", conv.c_str());
|
|
262
|
+
return env.Null();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (!((Napi::Value)value[2u]).IsArray()) {
|
|
266
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for parameters of '%2', expected an array", GetValueType(instance, (Napi::Value)value[1u]), func->name);
|
|
267
|
+
return env.Null();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
parameters = ((Napi::Value)value[2u]).As<Napi::Array>();
|
|
271
|
+
} else {
|
|
272
|
+
if (!((Napi::Value)value[1u]).IsArray()) {
|
|
273
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for parameters of '%2', expected an array", GetValueType(instance, (Napi::Value)value[1u]), func->name);
|
|
274
|
+
return env.Null();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
parameters = ((Napi::Value)value[1u]).As<Napi::Array>();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
func->ret.type = ResolveType(instance, value[0u]);
|
|
281
|
+
if (!func->ret.type)
|
|
282
|
+
return env.Null();
|
|
283
|
+
func->scratch_size = AlignLen(func->ret.type->size, 16);
|
|
284
|
+
|
|
285
|
+
for (uint32_t j = 0; j < parameters.Length(); j++) {
|
|
286
|
+
ParameterInfo param = {};
|
|
287
|
+
|
|
288
|
+
param.type = ResolveType(instance, parameters[j]);
|
|
289
|
+
if (!param.type)
|
|
290
|
+
return env.Null();
|
|
291
|
+
if (param.type->primitive == PrimitiveKind::Void) {
|
|
292
|
+
ThrowError<Napi::TypeError>(env, "Type void cannot be used as a parameter");
|
|
293
|
+
return env.Null();
|
|
294
|
+
}
|
|
295
|
+
func->parameters.Append(param);
|
|
296
|
+
|
|
297
|
+
func->scratch_size += AlignLen(param.type->size, 16);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// That's enough extra space for pretty much every ABI supported, with 16 bytes of
|
|
301
|
+
// bonus for each in case we need to put pointers and exta-align them.
|
|
302
|
+
extra_size = std::max(extra_size, func->scratch_size +
|
|
303
|
+
AlignLen(8 * (func->parameters.len + 1), 16));
|
|
304
|
+
|
|
305
|
+
if (!AnalyseFunction(func))
|
|
306
|
+
return env.Null();
|
|
307
|
+
|
|
308
|
+
#ifdef _WIN32
|
|
309
|
+
func->func = (void *)GetProcAddress((HMODULE)lib->module, func->decorated_name);
|
|
310
|
+
#else
|
|
311
|
+
func->func = dlsym(lib->module, func->decorated_name);
|
|
312
|
+
#endif
|
|
313
|
+
if (!func->func) {
|
|
314
|
+
ThrowError<Napi::Error>(env, "Cannot find function '%1' in shared library", key.c_str());
|
|
315
|
+
return env.Null();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
Napi::Function wrapper = Napi::Function::New(env, TranslateCall, key.c_str(), (void *)func);
|
|
319
|
+
wrapper.AddFinalizer([](Napi::Env, FunctionInfo *func) { delete func; }, func);
|
|
320
|
+
func_guard.Disable();
|
|
321
|
+
|
|
322
|
+
obj.Set(key, wrapper);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
lib->stack.AppendDefault(Mebibytes(1) + extra_size);
|
|
326
|
+
|
|
327
|
+
return obj;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
LibraryData::~LibraryData()
|
|
331
|
+
{
|
|
332
|
+
#ifdef _WIN32
|
|
333
|
+
if (module && module != GetModuleHandle(nullptr)) {
|
|
334
|
+
FreeLibrary((HMODULE)module);
|
|
335
|
+
}
|
|
336
|
+
#else
|
|
337
|
+
if (module && module != RTLD_DEFAULT) {
|
|
338
|
+
dlclose(module);
|
|
339
|
+
}
|
|
340
|
+
#endif
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static void RegisterPrimitiveType(InstanceData *instance, const char *name, PrimitiveKind primitive, int16_t size)
|
|
344
|
+
{
|
|
345
|
+
TypeInfo *type = instance->types.AppendDefault();
|
|
346
|
+
|
|
347
|
+
type->name = name;
|
|
348
|
+
|
|
349
|
+
type->primitive = primitive;
|
|
350
|
+
type->size = size;
|
|
351
|
+
type->align = size;
|
|
352
|
+
|
|
353
|
+
RG_ASSERT(!instance->types_map.Find(name));
|
|
354
|
+
instance->types_map.Set(type);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
static Napi::Object InitBaseTypes(Napi::Env env)
|
|
358
|
+
{
|
|
359
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
360
|
+
|
|
361
|
+
RG_ASSERT(!instance->types.len);
|
|
362
|
+
|
|
363
|
+
RegisterPrimitiveType(instance, "void", PrimitiveKind::Void, 0);
|
|
364
|
+
RegisterPrimitiveType(instance, "bool", PrimitiveKind::Bool, 1);
|
|
365
|
+
RegisterPrimitiveType(instance, "int8", PrimitiveKind::Int8, 1);
|
|
366
|
+
RegisterPrimitiveType(instance, "int8_t", PrimitiveKind::Int8, 1);
|
|
367
|
+
RegisterPrimitiveType(instance, "uint8", PrimitiveKind::UInt8, 1);
|
|
368
|
+
RegisterPrimitiveType(instance, "uint8_t", PrimitiveKind::UInt8, 1);
|
|
369
|
+
RegisterPrimitiveType(instance, "char", PrimitiveKind::Int8, 1);
|
|
370
|
+
RegisterPrimitiveType(instance, "uchar", PrimitiveKind::UInt8, 1);
|
|
371
|
+
RegisterPrimitiveType(instance, "unsigned char", PrimitiveKind::UInt8, 1);
|
|
372
|
+
RegisterPrimitiveType(instance, "int16", PrimitiveKind::Int16, 2);
|
|
373
|
+
RegisterPrimitiveType(instance, "int16_t", PrimitiveKind::Int16, 2);
|
|
374
|
+
RegisterPrimitiveType(instance, "uint16", PrimitiveKind::UInt16, 2);
|
|
375
|
+
RegisterPrimitiveType(instance, "uint16_t", PrimitiveKind::UInt16, 2);
|
|
376
|
+
RegisterPrimitiveType(instance, "short", PrimitiveKind::Int16, 2);
|
|
377
|
+
RegisterPrimitiveType(instance, "ushort", PrimitiveKind::UInt16, 2);
|
|
378
|
+
RegisterPrimitiveType(instance, "unsigned short", PrimitiveKind::UInt16, 2);
|
|
379
|
+
RegisterPrimitiveType(instance, "int32", PrimitiveKind::Int32, 4);
|
|
380
|
+
RegisterPrimitiveType(instance, "int32_t", PrimitiveKind::Int32, 4);
|
|
381
|
+
RegisterPrimitiveType(instance, "uint32", PrimitiveKind::UInt32, 4);
|
|
382
|
+
RegisterPrimitiveType(instance, "uint32_t", PrimitiveKind::UInt32, 4);
|
|
383
|
+
RegisterPrimitiveType(instance, "int", PrimitiveKind::Int32, 4);
|
|
384
|
+
RegisterPrimitiveType(instance, "uint", PrimitiveKind::UInt32, 4);
|
|
385
|
+
RegisterPrimitiveType(instance, "unsigned int", PrimitiveKind::UInt32, 4);
|
|
386
|
+
RegisterPrimitiveType(instance, "int64", PrimitiveKind::Int64, 8);
|
|
387
|
+
RegisterPrimitiveType(instance, "int64_t", PrimitiveKind::Int64, 8);
|
|
388
|
+
RegisterPrimitiveType(instance, "uint64", PrimitiveKind::UInt64, 8);
|
|
389
|
+
RegisterPrimitiveType(instance, "uint64_t", PrimitiveKind::UInt64, 8);
|
|
390
|
+
#if ULONG_MAX == UINT64_MAX
|
|
391
|
+
RegisterPrimitiveType(instance, "long", PrimitiveKind::Int64, 8);
|
|
392
|
+
RegisterPrimitiveType(instance, "ulong", PrimitiveKind::UInt64, 8);
|
|
393
|
+
RegisterPrimitiveType(instance, "unsigned long", PrimitiveKind::UInt64, 8);
|
|
394
|
+
#else
|
|
395
|
+
RegisterPrimitiveType(instance, "long", PrimitiveKind::Int32, 4);
|
|
396
|
+
RegisterPrimitiveType(instance, "ulong", PrimitiveKind::UInt32, 4);
|
|
397
|
+
RegisterPrimitiveType(instance, "unsigned long", PrimitiveKind::UInt64, 4);
|
|
398
|
+
#endif
|
|
399
|
+
RegisterPrimitiveType(instance, "longlong", PrimitiveKind::Int64, 8);
|
|
400
|
+
RegisterPrimitiveType(instance, "long long", PrimitiveKind::Int64, 8);
|
|
401
|
+
RegisterPrimitiveType(instance, "ulonglong", PrimitiveKind::UInt64, 8);
|
|
402
|
+
RegisterPrimitiveType(instance, "unsigned long long", PrimitiveKind::UInt64, 8);
|
|
403
|
+
RegisterPrimitiveType(instance, "float32", PrimitiveKind::Float32, 4);
|
|
404
|
+
RegisterPrimitiveType(instance, "float64", PrimitiveKind::Float64, 8);
|
|
405
|
+
RegisterPrimitiveType(instance, "float", PrimitiveKind::Float32, 4);
|
|
406
|
+
RegisterPrimitiveType(instance, "double", PrimitiveKind::Float64, 8);
|
|
407
|
+
RegisterPrimitiveType(instance, "string", PrimitiveKind::String, sizeof(void *));
|
|
408
|
+
|
|
409
|
+
Napi::Object types = Napi::Object::New(env);
|
|
410
|
+
for (TypeInfo &type: instance->types) {
|
|
411
|
+
Napi::External<TypeInfo> external = Napi::External<TypeInfo>::New(env, &type);
|
|
412
|
+
SetValueTag(instance, external, &TypeInfoMarker);
|
|
413
|
+
|
|
414
|
+
types.Set(type.name, external);
|
|
415
|
+
|
|
416
|
+
if (strchr(type.name, ' ')) {
|
|
417
|
+
char name_buf[64] = {};
|
|
418
|
+
for (Size i = 0; type.name[i]; i++) {
|
|
419
|
+
RG_ASSERT(i < RG_SIZE(name_buf) - 1);
|
|
420
|
+
name_buf[i] = type.name[i] != ' ' ? type.name[i] : '_';
|
|
421
|
+
}
|
|
422
|
+
types.Set(name_buf, external);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
types.Freeze();
|
|
426
|
+
|
|
427
|
+
return types;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
#if NODE_WANT_INTERNALS
|
|
433
|
+
|
|
434
|
+
static void SetValue(node::Environment *env, v8::Local<v8::Object> target,
|
|
435
|
+
const char *name, Napi::Value value)
|
|
436
|
+
{
|
|
437
|
+
v8::Isolate *isolate = env->isolate();
|
|
438
|
+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
439
|
+
|
|
440
|
+
v8::NewStringType str_type = v8::NewStringType::kInternalized;
|
|
441
|
+
v8::Local<v8::String> str = v8::String::NewFromUtf8(isolate, name, str_type).ToLocalChecked();
|
|
442
|
+
|
|
443
|
+
target->Set(context, str, v8impl::V8LocalValueFromJsValue(value)).Check();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
static void InitInternal(v8::Local<v8::Object> target, v8::Local<v8::Value>,
|
|
447
|
+
v8::Local<v8::Context> context, void *)
|
|
448
|
+
{
|
|
449
|
+
using namespace RG;
|
|
450
|
+
|
|
451
|
+
node::Environment *env = node::Environment::GetCurrent(context);
|
|
452
|
+
|
|
453
|
+
// Not very clean but I don't know enough about Node and V8 to do better...
|
|
454
|
+
// ... and it seems to work okay.
|
|
455
|
+
napi_env env_napi = new napi_env__(context);
|
|
456
|
+
Napi::Env env_cxx(env_napi);
|
|
457
|
+
env->AtExit([](void *udata) {
|
|
458
|
+
napi_env env_napi = (napi_env)udata;
|
|
459
|
+
delete env_napi;
|
|
460
|
+
}, env_napi);
|
|
461
|
+
|
|
462
|
+
InstanceData *instance = new InstanceData();
|
|
463
|
+
env_cxx.SetInstanceData(instance);
|
|
464
|
+
|
|
465
|
+
FillRandomSafe(&instance->tag_lower, RG_SIZE(instance->tag_lower));
|
|
466
|
+
|
|
467
|
+
SetValue(env, target, "struct", Napi::Function::New(env_napi, CreateStructType));
|
|
468
|
+
SetValue(env, target, "pointer", Napi::Function::New(env_napi, CreatePointerType));
|
|
469
|
+
SetValue(env, target, "load", Napi::Function::New(env_napi, LoadSharedLibrary));
|
|
470
|
+
SetValue(env, target, "internal", Napi::Boolean::New(env_napi, true));
|
|
471
|
+
|
|
472
|
+
Napi::Object types = InitBaseTypes(env_cxx);
|
|
473
|
+
SetValue(env, target, "types", types);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#else
|
|
477
|
+
|
|
478
|
+
static Napi::Object InitModule(Napi::Env env, Napi::Object exports)
|
|
479
|
+
{
|
|
480
|
+
using namespace RG;
|
|
481
|
+
|
|
482
|
+
InstanceData *instance = new InstanceData();
|
|
483
|
+
env.SetInstanceData(instance);
|
|
484
|
+
|
|
485
|
+
FillRandomSafe(&instance->tag_lower, RG_SIZE(instance->tag_lower));
|
|
486
|
+
|
|
487
|
+
exports.Set("struct", Napi::Function::New(env, CreateStructType));
|
|
488
|
+
exports.Set("pointer", Napi::Function::New(env, CreatePointerType));
|
|
489
|
+
exports.Set("load", Napi::Function::New(env, LoadSharedLibrary));
|
|
490
|
+
exports.Set("internal", Napi::Boolean::New(env, false));
|
|
491
|
+
|
|
492
|
+
Napi::Object types = InitBaseTypes(env);
|
|
493
|
+
exports.Set("types", types);
|
|
494
|
+
|
|
495
|
+
return exports;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
#endif
|
|
499
|
+
|
|
500
|
+
#if NODE_WANT_INTERNALS
|
|
501
|
+
NODE_MODULE_CONTEXT_AWARE_INTERNAL(koffi, InitInternal);
|
|
502
|
+
#else
|
|
503
|
+
NODE_API_MODULE(koffi, InitModule);
|
|
504
|
+
#endif
|