diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d10c568..8c7a58e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,16 @@ add_library( src/cpp/flashlight_binding.cc ) +# Flashlight <> JS finalizer bindings +add_library(flashlight_finalizer_binding SHARED src/cpp/finalizer.cc) +target_include_directories(flashlight_finalizer_binding PRIVATE src/cpp/napi/) +target_link_libraries( + flashlight_finalizer_binding + PRIVATE + flashlight::flashlight +) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -undefined dynamic_lookup") + # Write lib to the project root set_target_properties( flashlight_binding diff --git a/examples/test.ts b/examples/test.ts index f60c811e..3d227af8 100644 --- a/examples/test.ts +++ b/examples/test.ts @@ -16,4 +16,5 @@ for (let i = 0; i < 10000; ++i) { m += a.add(b).mean().toFloat32() } const t1 = performance.now() / 1e3 +Bun.gc(true) console.log(t1 - t0, 'seconds to calculate', m) diff --git a/examples/train.js b/examples/train.js index 62ffd200..935a5ba4 100644 --- a/examples/train.js +++ b/examples/train.js @@ -63,7 +63,7 @@ const optimize = (...args) => { } } -const show_timing = false +const show_timing = true const traint0 = performance.now() let floss = 0 for (let i = 0; i < 1000; ++i) { diff --git a/src/cpp/finalizer.cc b/src/cpp/finalizer.cc new file mode 100644 index 00000000..a8b74726 --- /dev/null +++ b/src/cpp/finalizer.cc @@ -0,0 +1,37 @@ +#include +#include + +#include "flashlight/fl/tensor/tensor.h" + +#define DOUBLE_ENCODE_OFFSET_BIT 49 +#define DOUBLE_ENCODE_OFFSET (1ll << DOUBLE_ENCODE_OFFSET_BIT) + +extern "C" { + +void deleter(napi_env env, void *t, void * /*ignore*/) { + auto *tensor = reinterpret_cast(t); + delete tensor; +} + +napi_value finalize(napi_env env, napi_callback_info info) { + napi_value this_arg; + napi_value argv[2]; + size_t argc = 2; + auto status = napi_get_cb_info(env, info, &argc, argv, &this_arg, NULL); + // grab the 2nd argument directly + int64_t ptr_int = ((int64_t *)info)[7]; + void *ptr = (void *)(ptr_int - DOUBLE_ENCODE_OFFSET); + auto *tensor = reinterpret_cast(ptr); + int64_t adjusted; + napi_adjust_external_memory(env, tensor->bytes(), &adjusted); + napi_add_finalizer(env, argv[0], ptr, deleter, NULL, NULL); + return (napi_value)0xaaaaaa; +} + +napi_value napi_register_module_v1(napi_env env, napi_value exports) { + napi_property_descriptor desc = {"finalize", 0, finalize, 0, + 0, 0, napi_default, 0}; + auto status = napi_define_properties(env, exports, 1, &desc); + return exports; +} +} diff --git a/src/cpp/napi/js_native_api.h b/src/cpp/napi/js_native_api.h new file mode 100644 index 00000000..40db9dec --- /dev/null +++ b/src/cpp/napi/js_native_api.h @@ -0,0 +1,458 @@ +#ifndef SRC_JS_NATIVE_API_H_ +#define SRC_JS_NATIVE_API_H_ + +// This file needs to be compatible with C compilers. +#include // NOLINT(modernize-deprecated-headers) +#include // NOLINT(modernize-deprecated-headers) + +// Use INT_MAX, this should only be consumed by the pre-processor anyway. +#define NAPI_VERSION_EXPERIMENTAL 2147483647 +#ifndef NAPI_VERSION +#ifdef NAPI_EXPERIMENTAL +#define NAPI_VERSION NAPI_VERSION_EXPERIMENTAL +#else +// The baseline version for N-API. +// The NAPI_VERSION controls which version will be used by default when +// compilling a native addon. If the addon developer specifically wants to use +// functions available in a new version of N-API that is not yet ported in all +// LTS versions, they can set NAPI_VERSION knowing that they have specifically +// depended on that version. +#define NAPI_VERSION 8 +#endif +#endif + +#include "js_native_api_types.h" + +// If you need __declspec(dllimport), either include instead, or +// define NAPI_EXTERN as __declspec(dllimport) on the compiler's command line. +#ifndef NAPI_EXTERN +#ifdef _WIN32 +#define NAPI_EXTERN __declspec(dllexport) +#elif defined(__wasm32__) +#define NAPI_EXTERN \ + __attribute__((visibility("default"))) \ + __attribute__((__import_module__("napi"))) +#else +#define NAPI_EXTERN __attribute__((visibility("default"))) +#endif +#endif + +#define NAPI_AUTO_LENGTH SIZE_MAX + +#ifdef __cplusplus +#define EXTERN_C_START extern "C" { +#define EXTERN_C_END } +#else +#define EXTERN_C_START +#define EXTERN_C_END +#endif + +EXTERN_C_START + +NAPI_EXTERN napi_status +napi_get_last_error_info(napi_env env, const napi_extended_error_info **result); + +// Getters for defined singletons +NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value *result); +NAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value *result); +NAPI_EXTERN napi_status napi_get_global(napi_env env, napi_value *result); +NAPI_EXTERN napi_status napi_get_boolean(napi_env env, bool value, + napi_value *result); + +// Methods to create Primitive types/Objects +NAPI_EXTERN napi_status napi_create_object(napi_env env, napi_value *result); +NAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value *result); +NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env, + size_t length, + napi_value *result); +NAPI_EXTERN napi_status napi_create_double(napi_env env, double value, + napi_value *result); +NAPI_EXTERN napi_status napi_create_int32(napi_env env, int32_t value, + napi_value *result); +NAPI_EXTERN napi_status napi_create_uint32(napi_env env, uint32_t value, + napi_value *result); +NAPI_EXTERN napi_status napi_create_int64(napi_env env, int64_t value, + napi_value *result); +NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, const char *str, + size_t length, + napi_value *result); +NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env, const char *str, + size_t length, + napi_value *result); +NAPI_EXTERN napi_status napi_create_string_utf16(napi_env env, + const char16_t *str, + size_t length, + napi_value *result); +NAPI_EXTERN napi_status napi_create_symbol(napi_env env, napi_value description, + napi_value *result); +#ifdef NAPI_EXPERIMENTAL +NAPI_EXTERN napi_status node_api_symbol_for(napi_env env, + const char *utf8description, + size_t length, napi_value *result); +#endif // NAPI_EXPERIMENTAL +NAPI_EXTERN napi_status napi_create_function(napi_env env, const char *utf8name, + size_t length, napi_callback cb, + void *data, napi_value *result); +NAPI_EXTERN napi_status napi_create_error(napi_env env, napi_value code, + napi_value msg, napi_value *result); +NAPI_EXTERN napi_status napi_create_type_error(napi_env env, napi_value code, + napi_value msg, + napi_value *result); +NAPI_EXTERN napi_status napi_create_range_error(napi_env env, napi_value code, + napi_value msg, + napi_value *result); +#ifdef NAPI_EXPERIMENTAL +NAPI_EXTERN napi_status node_api_create_syntax_error(napi_env env, + napi_value code, + napi_value msg, + napi_value *result); +#endif // NAPI_EXPERIMENTAL + +// Methods to get the native napi_value from Primitive type +NAPI_EXTERN napi_status napi_typeof(napi_env env, napi_value value, + napi_valuetype *result); +NAPI_EXTERN napi_status napi_get_value_double(napi_env env, napi_value value, + double *result); +NAPI_EXTERN napi_status napi_get_value_int32(napi_env env, napi_value value, + int32_t *result); +NAPI_EXTERN napi_status napi_get_value_uint32(napi_env env, napi_value value, + uint32_t *result); +NAPI_EXTERN napi_status napi_get_value_int64(napi_env env, napi_value value, + int64_t *result); +NAPI_EXTERN napi_status napi_get_value_bool(napi_env env, napi_value value, + bool *result); + +// Copies LATIN-1 encoded bytes from a string into a buffer. +NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env, + napi_value value, + char *buf, size_t bufsize, + size_t *result); + +// Copies UTF-8 encoded bytes from a string into a buffer. +NAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env, + napi_value value, char *buf, + size_t bufsize, + size_t *result); + +// Copies UTF-16 encoded bytes from a string into a buffer. +NAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env, + napi_value value, + char16_t *buf, + size_t bufsize, + size_t *result); + +// Methods to coerce values +// These APIs may execute user scripts +NAPI_EXTERN napi_status napi_coerce_to_bool(napi_env env, napi_value value, + napi_value *result); +NAPI_EXTERN napi_status napi_coerce_to_number(napi_env env, napi_value value, + napi_value *result); +NAPI_EXTERN napi_status napi_coerce_to_object(napi_env env, napi_value value, + napi_value *result); +NAPI_EXTERN napi_status napi_coerce_to_string(napi_env env, napi_value value, + napi_value *result); + +// Methods to work with Objects +NAPI_EXTERN napi_status napi_get_prototype(napi_env env, napi_value object, + napi_value *result); +NAPI_EXTERN napi_status napi_get_property_names(napi_env env, napi_value object, + napi_value *result); +NAPI_EXTERN napi_status napi_set_property(napi_env env, napi_value object, + napi_value key, napi_value value); +NAPI_EXTERN napi_status napi_has_property(napi_env env, napi_value object, + napi_value key, bool *result); +NAPI_EXTERN napi_status napi_get_property(napi_env env, napi_value object, + napi_value key, napi_value *result); +NAPI_EXTERN napi_status napi_delete_property(napi_env env, napi_value object, + napi_value key, bool *result); +NAPI_EXTERN napi_status napi_has_own_property(napi_env env, napi_value object, + napi_value key, bool *result); +NAPI_EXTERN napi_status napi_set_named_property(napi_env env, napi_value object, + const char *utf8name, + napi_value value); +NAPI_EXTERN napi_status napi_has_named_property(napi_env env, napi_value object, + const char *utf8name, + bool *result); +NAPI_EXTERN napi_status napi_get_named_property(napi_env env, napi_value object, + const char *utf8name, + napi_value *result); +NAPI_EXTERN napi_status napi_set_element(napi_env env, napi_value object, + uint32_t index, napi_value value); +NAPI_EXTERN napi_status napi_has_element(napi_env env, napi_value object, + uint32_t index, bool *result); +NAPI_EXTERN napi_status napi_get_element(napi_env env, napi_value object, + uint32_t index, napi_value *result); +NAPI_EXTERN napi_status napi_delete_element(napi_env env, napi_value object, + uint32_t index, bool *result); +NAPI_EXTERN napi_status +napi_define_properties(napi_env env, napi_value object, size_t property_count, + const napi_property_descriptor *properties); + +// Methods to work with Arrays +NAPI_EXTERN napi_status napi_is_array(napi_env env, napi_value value, + bool *result); +NAPI_EXTERN napi_status napi_get_array_length(napi_env env, napi_value value, + uint32_t *result); + +// Methods to compare values +NAPI_EXTERN napi_status napi_strict_equals(napi_env env, napi_value lhs, + napi_value rhs, bool *result); + +// Methods to work with Functions +NAPI_EXTERN napi_status napi_call_function(napi_env env, napi_value recv, + napi_value func, size_t argc, + const napi_value *argv, + napi_value *result); +NAPI_EXTERN napi_status napi_new_instance(napi_env env, napi_value constructor, + size_t argc, const napi_value *argv, + napi_value *result); +NAPI_EXTERN napi_status napi_instanceof(napi_env env, napi_value object, + napi_value constructor, bool *result); + +// Methods to work with napi_callbacks + +// Gets all callback info in a single call. (Ugly, but faster.) +NAPI_EXTERN napi_status napi_get_cb_info( + napi_env env, // [in] NAPI environment handle + napi_callback_info cbinfo, // [in] Opaque callback-info handle + size_t *argc, // [in-out] Specifies the size of the provided argv array + // and receives the actual count of args. + napi_value *argv, // [out] Array of values + napi_value *this_arg, // [out] Receives the JS 'this' arg for the call + void **data); // [out] Receives the data pointer for the callback. + +NAPI_EXTERN napi_status napi_get_new_target(napi_env env, + napi_callback_info cbinfo, + napi_value *result); +NAPI_EXTERN napi_status napi_define_class( + napi_env env, const char *utf8name, size_t length, + napi_callback constructor, void *data, size_t property_count, + const napi_property_descriptor *properties, napi_value *result); + +// Methods to work with external data objects +NAPI_EXTERN napi_status napi_wrap(napi_env env, napi_value js_object, + void *native_object, + napi_finalize finalize_cb, + void *finalize_hint, napi_ref *result); +NAPI_EXTERN napi_status napi_unwrap(napi_env env, napi_value js_object, + void **result); +NAPI_EXTERN napi_status napi_remove_wrap(napi_env env, napi_value js_object, + void **result); +NAPI_EXTERN napi_status napi_create_external(napi_env env, void *data, + napi_finalize finalize_cb, + void *finalize_hint, + napi_value *result); +NAPI_EXTERN napi_status napi_get_value_external(napi_env env, napi_value value, + void **result); + +// Methods to control object lifespan + +// Set initial_refcount to 0 for a weak reference, >0 for a strong reference. +NAPI_EXTERN napi_status napi_create_reference(napi_env env, napi_value value, + uint32_t initial_refcount, + napi_ref *result); + +// Deletes a reference. The referenced value is released, and may +// be GC'd unless there are other references to it. +NAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref); + +// Increments the reference count, optionally returning the resulting count. +// After this call the reference will be a strong reference because its +// refcount is >0, and the referenced object is effectively "pinned". +// Calling this when the refcount is 0 and the object is unavailable +// results in an error. +NAPI_EXTERN napi_status napi_reference_ref(napi_env env, napi_ref ref, + uint32_t *result); + +// Decrements the reference count, optionally returning the resulting count. +// If the result is 0 the reference is now weak and the object may be GC'd +// at any time if there are no other references. Calling this when the +// refcount is already 0 results in an error. +NAPI_EXTERN napi_status napi_reference_unref(napi_env env, napi_ref ref, + uint32_t *result); + +// Attempts to get a referenced value. If the reference is weak, +// the value might no longer be available, in that case the call +// is still successful but the result is NULL. +NAPI_EXTERN napi_status napi_get_reference_value(napi_env env, napi_ref ref, + napi_value *result); + +NAPI_EXTERN napi_status napi_open_handle_scope(napi_env env, + napi_handle_scope *result); +NAPI_EXTERN napi_status napi_close_handle_scope(napi_env env, + napi_handle_scope scope); +NAPI_EXTERN napi_status napi_open_escapable_handle_scope( + napi_env env, napi_escapable_handle_scope *result); +NAPI_EXTERN napi_status napi_close_escapable_handle_scope( + napi_env env, napi_escapable_handle_scope scope); + +NAPI_EXTERN napi_status napi_escape_handle(napi_env env, + napi_escapable_handle_scope scope, + napi_value escapee, + napi_value *result); + +// Methods to support error handling +NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error); +NAPI_EXTERN napi_status napi_throw_error(napi_env env, const char *code, + const char *msg); +NAPI_EXTERN napi_status napi_throw_type_error(napi_env env, const char *code, + const char *msg); +NAPI_EXTERN napi_status napi_throw_range_error(napi_env env, const char *code, + const char *msg); +#ifdef NAPI_EXPERIMENTAL +NAPI_EXTERN napi_status node_api_throw_syntax_error(napi_env env, + const char *code, + const char *msg); +#endif // NAPI_EXPERIMENTAL +NAPI_EXTERN napi_status napi_is_error(napi_env env, napi_value value, + bool *result); + +// Methods to support catching exceptions +NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool *result); +NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env, + napi_value *result); + +// Methods to work with array buffers and typed arrays +NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env, napi_value value, + bool *result); +NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env, + size_t byte_length, void **data, + napi_value *result); +NAPI_EXTERN napi_status napi_create_external_arraybuffer( + napi_env env, void *external_data, size_t byte_length, + napi_finalize finalize_cb, void *finalize_hint, napi_value *result); +NAPI_EXTERN napi_status napi_get_arraybuffer_info(napi_env env, + napi_value arraybuffer, + void **data, + size_t *byte_length); +NAPI_EXTERN napi_status napi_is_typedarray(napi_env env, napi_value value, + bool *result); +NAPI_EXTERN napi_status napi_create_typedarray( + napi_env env, napi_typedarray_type type, size_t length, + napi_value arraybuffer, size_t byte_offset, napi_value *result); +NAPI_EXTERN napi_status napi_get_typedarray_info( + napi_env env, napi_value typedarray, napi_typedarray_type *type, + size_t *length, void **data, napi_value *arraybuffer, size_t *byte_offset); + +NAPI_EXTERN napi_status napi_create_dataview(napi_env env, size_t length, + napi_value arraybuffer, + size_t byte_offset, + napi_value *result); +NAPI_EXTERN napi_status napi_is_dataview(napi_env env, napi_value value, + bool *result); +NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env, + napi_value dataview, + size_t *bytelength, void **data, + napi_value *arraybuffer, + size_t *byte_offset); + +// version management +NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t *result); + +// Promises +NAPI_EXTERN napi_status napi_create_promise(napi_env env, + napi_deferred *deferred, + napi_value *promise); +NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution); +NAPI_EXTERN napi_status napi_reject_deferred(napi_env env, + napi_deferred deferred, + napi_value rejection); +NAPI_EXTERN napi_status napi_is_promise(napi_env env, napi_value value, + bool *is_promise); + +// Running a script +NAPI_EXTERN napi_status napi_run_script(napi_env env, napi_value script, + napi_value *result); + +// Memory management +NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env, + int64_t change_in_bytes, + int64_t *adjusted_value); + +#if NAPI_VERSION >= 5 + +// Dates +NAPI_EXTERN napi_status napi_create_date(napi_env env, double time, + napi_value *result); + +NAPI_EXTERN napi_status napi_is_date(napi_env env, napi_value value, + bool *is_date); + +NAPI_EXTERN napi_status napi_get_date_value(napi_env env, napi_value value, + double *result); + +// Add finalizer for pointer +NAPI_EXTERN napi_status napi_add_finalizer(napi_env env, napi_value js_object, + void *native_object, + napi_finalize finalize_cb, + void *finalize_hint, + napi_ref *result); + +#endif // NAPI_VERSION >= 5 + +#if NAPI_VERSION >= 6 + +// BigInt +NAPI_EXTERN napi_status napi_create_bigint_int64(napi_env env, int64_t value, + napi_value *result); +NAPI_EXTERN napi_status napi_create_bigint_uint64(napi_env env, uint64_t value, + napi_value *result); +NAPI_EXTERN napi_status napi_create_bigint_words(napi_env env, int sign_bit, + size_t word_count, + const uint64_t *words, + napi_value *result); +NAPI_EXTERN napi_status napi_get_value_bigint_int64(napi_env env, + napi_value value, + int64_t *result, + bool *lossless); +NAPI_EXTERN napi_status napi_get_value_bigint_uint64(napi_env env, + napi_value value, + uint64_t *result, + bool *lossless); +NAPI_EXTERN napi_status napi_get_value_bigint_words(napi_env env, + napi_value value, + int *sign_bit, + size_t *word_count, + uint64_t *words); + +// Object +NAPI_EXTERN napi_status napi_get_all_property_names( + napi_env env, napi_value object, napi_key_collection_mode key_mode, + napi_key_filter key_filter, napi_key_conversion key_conversion, + napi_value *result); + +// Instance data +NAPI_EXTERN napi_status napi_set_instance_data(napi_env env, void *data, + napi_finalize finalize_cb, + void *finalize_hint); + +NAPI_EXTERN napi_status napi_get_instance_data(napi_env env, void **data); +#endif // NAPI_VERSION >= 6 + +#if NAPI_VERSION >= 7 +// ArrayBuffer detaching +NAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env, + napi_value arraybuffer); + +NAPI_EXTERN napi_status napi_is_detached_arraybuffer(napi_env env, + napi_value value, + bool *result); +#endif // NAPI_VERSION >= 7 + +#if NAPI_VERSION >= 8 +// Type tagging +NAPI_EXTERN napi_status napi_type_tag_object(napi_env env, napi_value value, + const napi_type_tag *type_tag); + +NAPI_EXTERN napi_status +napi_check_object_type_tag(napi_env env, napi_value value, + const napi_type_tag *type_tag, bool *result); +NAPI_EXTERN napi_status napi_object_freeze(napi_env env, napi_value object); +NAPI_EXTERN napi_status napi_object_seal(napi_env env, napi_value object); +#endif // NAPI_VERSION >= 8 + +EXTERN_C_END + +#endif // SRC_JS_NATIVE_API_H_ diff --git a/src/cpp/napi/js_native_api_types.h b/src/cpp/napi/js_native_api_types.h new file mode 100644 index 00000000..2cfe45c5 --- /dev/null +++ b/src/cpp/napi/js_native_api_types.h @@ -0,0 +1,156 @@ +#ifndef SRC_JS_NATIVE_API_TYPES_H_ +#define SRC_JS_NATIVE_API_TYPES_H_ + +// This file needs to be compatible with C compilers. +// This is a public include file, and these includes have essentially +// became part of it's API. +#include // NOLINT(modernize-deprecated-headers) +#include // NOLINT(modernize-deprecated-headers) + +#if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900) +typedef uint16_t char16_t; +#endif + +// JSVM API types are all opaque pointers for ABI stability +// typedef undefined structs instead of void* for compile time type safety +typedef struct napi_env__ *napi_env; +typedef struct napi_value__ *napi_value; +typedef struct napi_ref__ *napi_ref; +typedef struct napi_handle_scope__ *napi_handle_scope; +typedef struct napi_escapable_handle_scope__ *napi_escapable_handle_scope; +typedef struct napi_callback_info__ *napi_callback_info; +typedef struct napi_deferred__ *napi_deferred; + +typedef enum { + napi_default = 0, + napi_writable = 1 << 0, + napi_enumerable = 1 << 1, + napi_configurable = 1 << 2, + + // Used with napi_define_class to distinguish static properties + // from instance properties. Ignored by napi_define_properties. + napi_static = 1 << 10, + +#if NAPI_VERSION >= 8 + // Default for class methods. + napi_default_method = napi_writable | napi_configurable, + + // Default for object properties, like in JS obj[prop]. + napi_default_jsproperty = napi_writable | napi_enumerable | napi_configurable, +#endif // NAPI_VERSION >= 8 +} napi_property_attributes; + +typedef enum { + // ES6 types (corresponds to typeof) + napi_undefined, + napi_null, + napi_boolean, + napi_number, + napi_string, + napi_symbol, + napi_object, + napi_function, + napi_external, + napi_bigint, +} napi_valuetype; + +typedef enum { + napi_int8_array, + napi_uint8_array, + napi_uint8_clamped_array, + napi_int16_array, + napi_uint16_array, + napi_int32_array, + napi_uint32_array, + napi_float32_array, + napi_float64_array, + napi_bigint64_array, + napi_biguint64_array, +} napi_typedarray_type; + +typedef enum { + napi_ok, + napi_invalid_arg, + napi_object_expected, + napi_string_expected, + napi_name_expected, + napi_function_expected, + napi_number_expected, + napi_boolean_expected, + napi_array_expected, + napi_generic_failure, + napi_pending_exception, + napi_cancelled, + napi_escape_called_twice, + napi_handle_scope_mismatch, + napi_callback_scope_mismatch, + napi_queue_full, + napi_closing, + napi_bigint_expected, + napi_date_expected, + napi_arraybuffer_expected, + napi_detachable_arraybuffer_expected, + napi_would_deadlock // unused +} napi_status; +// Note: when adding a new enum value to `napi_status`, please also update +// * `const int last_status` in the definition of `napi_get_last_error_info()' +// in file js_native_api_v8.cc. +// * `const char* error_messages[]` in file js_native_api_v8.cc with a brief +// message explaining the error. +// * the definition of `napi_status` in doc/api/n-api.md to reflect the newly +// added value(s). + +typedef napi_value (*napi_callback)(napi_env env, napi_callback_info info); +typedef void (*napi_finalize)(napi_env env, void *finalize_data, + void *finalize_hint); + +typedef struct { + // One of utf8name or name should be NULL. + const char *utf8name; + napi_value name; + + napi_callback method; + napi_callback getter; + napi_callback setter; + napi_value value; + + napi_property_attributes attributes; + void *data; +} napi_property_descriptor; + +typedef struct { + const char *error_message; + void *engine_reserved; + uint32_t engine_error_code; + napi_status error_code; +} napi_extended_error_info; + +#if NAPI_VERSION >= 6 +typedef enum { + napi_key_include_prototypes, + napi_key_own_only +} napi_key_collection_mode; + +typedef enum { + napi_key_all_properties = 0, + napi_key_writable = 1, + napi_key_enumerable = 1 << 1, + napi_key_configurable = 1 << 2, + napi_key_skip_strings = 1 << 3, + napi_key_skip_symbols = 1 << 4 +} napi_key_filter; + +typedef enum { + napi_key_keep_numbers, + napi_key_numbers_to_strings +} napi_key_conversion; +#endif // NAPI_VERSION >= 6 + +#if NAPI_VERSION >= 8 +typedef struct { + uint64_t lower; + uint64_t upper; +} napi_type_tag; +#endif // NAPI_VERSION >= 8 + +#endif // SRC_JS_NATIVE_API_TYPES_H_ diff --git a/src/cpp/napi/napi.zig b/src/cpp/napi/napi.zig new file mode 100644 index 00000000..84a1008e --- /dev/null +++ b/src/cpp/napi/napi.zig @@ -0,0 +1,1561 @@ +const std = @import("std"); +const JSC = @import("javascript_core"); +const strings = @import("strings"); +const bun = @import("../global.zig"); +const Lock = @import("../lock.zig").Lock; +const JSValue = JSC.JSValue; +const ZigString = JSC.ZigString; +const TODO_EXCEPTION: JSC.C.ExceptionRef = null; + +const Channel = @import("../sync.zig").Channel; + +pub const napi_env = *JSC.JSGlobalObject; +pub const napi_ref = struct_napi_ref__; +pub const napi_handle_scope = napi_env; +pub const napi_escapable_handle_scope = struct_napi_escapable_handle_scope__; +pub const napi_callback_info = *JSC.CallFrame; +pub const napi_deferred = *JSC.JSPromise; + +pub const napi_value = JSC.JSValue; +pub const struct_napi_ref__ = opaque {}; +pub const struct_napi_escapable_handle_scope__ = opaque {}; +pub const struct_napi_deferred__ = opaque {}; + +const char16_t = u16; +pub const napi_default: c_int = 0; +pub const napi_writable: c_int = 1; +pub const napi_enumerable: c_int = 2; +pub const napi_configurable: c_int = 4; +pub const napi_static: c_int = 1024; +pub const napi_default_method: c_int = 5; +pub const napi_default_jsproperty: c_int = 7; +pub const napi_property_attributes = c_uint; +pub const napi_valuetype = enum(c_uint) { + @"undefined" = 0, + @"null" = 1, + @"boolean" = 2, + @"number" = 3, + @"string" = 4, + @"symbol" = 5, + @"object" = 6, + @"function" = 7, + @"external" = 8, + @"bigint" = 9, +}; +pub const napi_typedarray_type = enum(c_uint) { + int8_array = 0, + uint8_array = 1, + uint8_clamped_array = 2, + int16_array = 3, + uint16_array = 4, + int32_array = 5, + uint32_array = 6, + float32_array = 7, + float64_array = 8, + bigint64_array = 9, + biguint64_array = 10, + + pub fn fromJSType(this: JSC.JSValue.JSType) ?napi_typedarray_type { + return switch (this) { + .Int8Array => napi_typedarray_type.int8_array, + .Uint8Array => napi_typedarray_type.uint8_array, + .Uint8ClampedArray => napi_typedarray_type.uint8_clamped_array, + .Int16Array => napi_typedarray_type.int16_array, + .Uint16Array => napi_typedarray_type.uint16_array, + .Int32Array => napi_typedarray_type.int32_array, + .Uint32Array => napi_typedarray_type.uint32_array, + .Float32Array => napi_typedarray_type.float32_array, + .Float64Array => napi_typedarray_type.float64_array, + .BigInt64Array => napi_typedarray_type.bigint64_array, + .BigUint64Array => napi_typedarray_type.biguint64_array, + else => null, + }; + } + + pub fn toJSType(this: napi_typedarray_type) JSC.JSValue.JSType { + return switch (this) { + .int8_array => .Int8Array, + .uint8_array => .Uint8Array, + .uint8_clamped_array => .Uint8ClampedArray, + .int16_array => .Int16Array, + .uint16_array => .Uint16Array, + .int32_array => .Int32Array, + .uint32_array => .Uint32Array, + .float32_array => .Float32Array, + .float64_array => .Float64Array, + .bigint64_array => .BigInt64Array, + .biguint64_array => .BigUint64Array, + }; + } + + pub fn toC(this: napi_typedarray_type) JSC.C.JSTypedArrayType { + return this.toJSType().toC(); + } +}; +pub const napi_status = enum(c_uint) { + ok = 0, + invalid_arg = 1, + object_expected = 2, + string_expected = 3, + name_expected = 4, + function_expected = 5, + number_expected = 6, + boolean_expected = 7, + array_expected = 8, + generic_failure = 9, + pending_exception = 10, + cancelled = 11, + escape_called_twice = 12, + handle_scope_mismatch = 13, + callback_scope_mismatch = 14, + queue_full = 15, + closing = 16, + bigint_expected = 17, + date_expected = 18, + arraybuffer_expected = 19, + detachable_arraybuffer_expected = 20, + would_deadlock = 21, +}; +pub const napi_callback = ?fn (napi_env, napi_callback_info) callconv(.C) napi_value; +pub const napi_finalize = ?fn (napi_env, ?*anyopaque, ?*anyopaque) callconv(.C) void; +pub const napi_property_descriptor = extern struct { + utf8name: [*c]const u8, + name: napi_value, + method: napi_callback, + getter: napi_callback, + setter: napi_callback, + value: napi_value, + attributes: napi_property_attributes, + data: ?*anyopaque, +}; +pub const napi_extended_error_info = extern struct { + error_message: [*c]const u8, + engine_reserved: ?*anyopaque, + engine_error_code: u32, + error_code: napi_status, +}; +pub const napi_key_include_prototypes: c_int = 0; +pub const napi_key_own_only: c_int = 1; +pub const napi_key_collection_mode = c_uint; +pub const napi_key_all_properties: c_int = 0; +pub const napi_key_writable: c_int = 1; +pub const napi_key_enumerable: c_int = 2; +pub const napi_key_configurable: c_int = 4; +pub const napi_key_skip_strings: c_int = 8; +pub const napi_key_skip_symbols: c_int = 16; +pub const napi_key_filter = c_uint; +pub const napi_key_keep_numbers: c_int = 0; +pub const napi_key_numbers_to_strings: c_int = 1; +pub const napi_key_conversion = c_uint; +pub const napi_type_tag = extern struct { + lower: u64, + upper: u64, +}; +pub extern fn napi_get_last_error_info(env: napi_env, result: [*c][*c]const napi_extended_error_info) napi_status; +pub export fn napi_get_undefined(_: napi_env, result: *napi_value) napi_status { + result.* = JSValue.jsUndefined(); + return .ok; +} +pub export fn napi_get_null(_: napi_env, result: *napi_value) napi_status { + result.* = JSValue.jsNull(); + return .ok; +} +pub extern fn napi_get_global(env: napi_env, result: *napi_value) napi_status; +pub export fn napi_get_boolean(_: napi_env, value: bool, result: *napi_value) napi_status { + result.* = JSValue.jsBoolean(value); + return .ok; +} +pub export fn napi_create_object(env: napi_env, result: *napi_value) napi_status { + result.* = JSValue.createEmptyObject(env, 0); + return .ok; +} +pub export fn napi_create_array(env: napi_env, result: *napi_value) napi_status { + result.* = JSValue.c(JSC.C.JSObjectMakeArray(env.ref(), 0, null, null)); + return .ok; +} +const prefilled_undefined_args_array: [128]JSC.JSValue = brk: { + var args: [128]JSC.JSValue = undefined; + for (args) |_, i| { + args[i] = JSValue.jsUndefined(); + } + break :brk args; +}; +pub export fn napi_create_array_with_length(env: napi_env, length: usize, result: *napi_value) napi_status { + if (length < prefilled_undefined_args_array.len) { + result.* = JSValue.c(JSC.C.JSObjectMakeArray(env.ref(), length, @ptrCast([*]const JSC.C.JSValueRef, &prefilled_undefined_args_array[0..length]), null)); + return .ok; + } + + const allocator = env.bunVM().allocator; + var undefined_args = allocator.alloc(JSC.C.JSValueRef, length) catch return .generic_failure; + defer allocator.free(undefined_args); + for (undefined_args) |_, i| { + undefined_args[i] = JSValue.jsUndefined().asObjectRef(); + } + result.* = JSValue.c(JSC.C.JSObjectMakeArray(env.ref(), length, undefined_args.ptr, null)); + + return .ok; +} +pub export fn napi_create_double(_: napi_env, value: f64, result: *napi_value) napi_status { + result.* = JSValue.jsNumber(value); + return .ok; +} +pub export fn napi_create_int32(_: napi_env, value: i32, result: *napi_value) napi_status { + result.* = JSValue.jsNumber(value); + return .ok; +} +pub export fn napi_create_uint32(_: napi_env, value: u32, result: *napi_value) napi_status { + result.* = JSValue.jsNumber(value); + return .ok; +} +pub export fn napi_create_int64(_: napi_env, value: i64, result: *napi_value) napi_status { + result.* = JSValue.jsNumber(value); + return .ok; +} +pub export fn napi_create_string_latin1(env: napi_env, str: [*]const u8, length: usize, result: *napi_value) napi_status { + var len = length; + if (NAPI_AUTO_LENGTH == length) { + len = bun.span(std.meta.assumeSentinel(str, 0)).len; + } + result.* = JSC.ZigString.init(str[0..len]).toValueGC(env); + return .ok; +} +pub export fn napi_create_string_utf8(env: napi_env, str: [*]const u8, length: usize, result: *napi_value) napi_status { + var len = length; + if (NAPI_AUTO_LENGTH == length) { + len = bun.span(std.meta.assumeSentinel(str, 0)).len; + } + result.* = JSC.ZigString.init(str[0..len]).withEncoding().toValueGC(env); + return .ok; +} +pub export fn napi_create_string_utf16(env: napi_env, str: [*]const char16_t, length: usize, result: *napi_value) napi_status { + var len = length; + if (NAPI_AUTO_LENGTH == length) { + len = bun.span(std.meta.assumeSentinel(str, 0)).len; + } + result.* = JSC.ZigString.from16(str, len).toValueGC(env); + return .ok; +} +pub export fn napi_create_symbol(env: napi_env, description: napi_value, result: *napi_value) napi_status { + var string_ref = JSC.C.JSValueToStringCopy(env.ref(), description.asObjectRef(), null); + defer JSC.C.JSStringRelease(string_ref); + result.* = JSValue.c(JSC.C.JSValueMakeSymbol(env.ref(), string_ref)); + return .ok; +} +pub export fn napi_create_error(env: napi_env, code: napi_value, msg: napi_value, result: *napi_value) napi_status { + const system_error = JSC.SystemError{ + .code = if (!code.isEmptyOrUndefinedOrNull()) code.getZigString(env) else ZigString.Empty, + .message = msg.getZigString(env), + }; + result.* = system_error.toErrorInstance(env); + return .ok; +} +pub extern fn napi_create_type_error(env: napi_env, code: napi_value, msg: napi_value, result: *napi_value) napi_status; +pub extern fn napi_create_range_error(env: napi_env, code: napi_value, msg: napi_value, result: *napi_value) napi_status; +pub export fn napi_typeof(env: napi_env, value: napi_value, result: *napi_valuetype) napi_status { + if (value.isEmpty()) { + result.* = .@"undefined"; + return .ok; + } + + if (value.isUndefined()) { + result.* = .@"undefined"; + return .ok; + } + + if (value.isNull()) { + result.* = .@"null"; + return .ok; + } + + if (value.isBoolean()) { + result.* = .boolean; + return .ok; + } + + if (value.isNumber()) { + result.* = .number; + return .ok; + } + + if (value.isString()) { + result.* = .string; + return .ok; + } + + if (value.isSymbol()) { + result.* = .symbol; + return .ok; + } + + if (value.isBigInt()) { + result.* = .bigint; + return .ok; + } + + if (value.isObject()) { + if (value.isCallable(env.vm())) { + result.* = .function; + return .ok; + } + result.* = .object; + return .ok; + } + + if (value.isCell() and value.isCallable(env.vm())) { + result.* = .function; + return .ok; + } + + return .invalid_arg; +} +pub export fn napi_get_value_double(_: napi_env, value: napi_value, result: *f64) napi_status { + result.* = value.asNumber(); + return .ok; +} +pub export fn napi_get_value_int32(_: napi_env, value: napi_value, result: *i32) napi_status { + result.* = value.to(i32); + return .ok; +} +pub export fn napi_get_value_uint32(_: napi_env, value: napi_value, result: *u32) napi_status { + result.* = value.to(u32); + return .ok; +} +pub export fn napi_get_value_int64(_: napi_env, value: napi_value, result: *i64) napi_status { + result.* = value.to(i64); + return .ok; +} +pub export fn napi_get_value_bool(_: napi_env, value: napi_value, result: *bool) napi_status { + result.* = value.to(bool); + return .ok; +} +inline fn maybeAppendNull(ptr: anytype, doit: bool) void { + if (doit) { + ptr.* = 0; + } +} +pub export fn napi_get_value_string_latin1(env: napi_env, value: napi_value, buf_ptr: [*c]u8, bufsize: usize, result: *usize) napi_status { + defer value.ensureStillAlive(); + const zig_str = value.getZigString(env); + var buf = buf_ptr orelse { + result.* = if (!zig_str.is16Bit()) + zig_str.len + else + // should be same length if valid latin1 + strings.elementLengthUTF16IntoUTF8([]const u16, zig_str.utf16SliceAligned()); + + return .ok; + }; + + if (zig_str.len == 0) { + result.* = 0; + buf[0] = 0; + + return .ok; + } + + var buf_ = buf[0..bufsize]; + + if (bufsize == 0) { + buf_ = bun.span(std.meta.assumeSentinel(buf_, 0)); + if (buf_.len == 0) { + result.* = 0; + return .ok; + } + } + + if (zig_str.is16Bit()) { + const utf16 = zig_str.utf16SliceAligned(); + const wrote = JSC.WebCore.Encoder.writeU16(utf16.ptr, utf16.len, buf, buf_.len, .latin1); + if (wrote < 0) { + return .generic_failure; + } + maybeAppendNull(&buf[@intCast(usize, wrote)], bufsize == 0); + // if zero terminated, report the length of the string without the null + result.* = @intCast(@TypeOf(result.*), wrote); + return .ok; + } + const to_copy = @minimum(zig_str.len, buf_.len); + @memcpy(buf, zig_str.slice().ptr, to_copy); + buf[to_copy] = 0; + // if zero terminated, report the length of the string without the null + result.* = to_copy; + return .ok; +} + +/// Copies a JavaScript string into a UTF-8 string buffer. The result is the +/// number of bytes (excluding the null terminator) copied into buf. +/// A sufficient buffer size should be greater than the length of string, +/// reserving space for null terminator. +/// If bufsize is insufficient, the string will be truncated and null terminated. +/// If buf is NULL, this method returns the length of the string (in bytes) +/// via the result parameter. +/// The result argument is optional unless buf is NULL. +pub export fn napi_get_value_string_utf8(env: napi_env, value: napi_value, buf_ptr: [*c]u8, bufsize: usize, result_ptr: ?*usize) napi_status { + defer value.ensureStillAlive(); + if (!value.isString()) { + return .string_expected; + } + + const zig_str = value.getZigString(env); + var buf = buf_ptr orelse { + if (result_ptr) |result| { + result.* = if (!zig_str.is16Bit()) + zig_str.len + else + JSC.WebCore.Encoder.byteLengthU16(zig_str.utf16SliceAligned().ptr, zig_str.utf16SliceAligned().len, .utf8); + } + + return .ok; + }; + + if (zig_str.len == 0) { + if (result_ptr) |result| { + result.* = 0; + } + return .ok; + } + + var buf_ = buf[0..bufsize]; + + if (bufsize == 0) { + buf_ = bun.span(std.meta.assumeSentinel(buf_, 0)); + if (buf_.len == 0) { + if (result_ptr) |result| { + result.* = 0; + } + return .ok; + } + } + + if (zig_str.is16Bit()) { + const utf16 = zig_str.utf16SliceAligned(); + const wrote = JSC.WebCore.Encoder.writeU16(utf16.ptr, utf16.len, buf, buf_.len, .utf8); + if (wrote < 0) { + return .generic_failure; + } + buf[@intCast(usize, wrote)] = 0; + if (result_ptr) |result| { + result.* = @intCast(@TypeOf(result.*), wrote); + } + + return .ok; + } + + const to_copy = @minimum(zig_str.len, buf_.len); + @memcpy(buf, zig_str.slice().ptr, to_copy); + buf[to_copy] = 0; + if (result_ptr) |result| { + result.* = @intCast(@TypeOf(result.*), to_copy); + } + + return .ok; +} +pub export fn napi_get_value_string_utf16(env: napi_env, value: napi_value, buf_ptr: [*c]char16_t, bufsize: usize, result_ptr: ?*usize) napi_status { + defer value.ensureStillAlive(); + const zig_str = value.getZigString(env); + var buf = buf_ptr orelse { + if (result_ptr) |result| { + result.* = if (zig_str.is16Bit()) + zig_str.len + else + JSC.WebCore.Encoder.byteLengthU16(zig_str.utf16SliceAligned().ptr, zig_str.utf16SliceAligned().len, .latin1); + } + + return .ok; + }; + + if (zig_str.len == 0) { + if (result_ptr) |result| { + result.* = 0; + } + buf[0] = 0; + + return .ok; + } + + var buf_ = buf[0..bufsize]; + + if (bufsize == 0) { + buf_ = bun.span(std.meta.assumeSentinel(buf_, 0)); + if (buf_.len == 0) { + if (result_ptr) |result| { + result.* = 0; + } + return .ok; + } + } + + if (!zig_str.is16Bit()) { + const slice = zig_str.slice(); + const encode_into_result = strings.copyLatin1IntoUTF16([]char16_t, buf_, []const u8, slice); + buf[@intCast(usize, encode_into_result.written)] = 0; + + if (result_ptr) |result| { + result.* = encode_into_result.written; + } + return .ok; + } + + const to_copy = @minimum(zig_str.len, buf_.len) * 2; + @memcpy(std.mem.sliceAsBytes(buf_).ptr, std.mem.sliceAsBytes(zig_str.utf16SliceAligned()).ptr, to_copy); + buf[to_copy] = 0; + // if zero terminated, report the length of the string without the null + if (result_ptr) |result| { + result.* = to_copy; + } + + return .ok; +} +pub export fn napi_coerce_to_bool(_: napi_env, value: napi_value, result: *napi_value) napi_status { + result.* = JSValue.jsBoolean(value.to(bool)); + return .ok; +} +pub export fn napi_coerce_to_number(env: napi_env, value: napi_value, result: *napi_value) napi_status { + result.* = JSC.JSValue.jsNumber(JSC.C.JSValueToNumber(env.ref(), value.asObjectRef(), TODO_EXCEPTION)); + return .ok; +} +pub export fn napi_coerce_to_object(env: napi_env, value: napi_value, result: *napi_value) napi_status { + result.* = JSValue.c(JSC.C.JSValueToObject(env.ref(), value.asObjectRef(), TODO_EXCEPTION)); + return .ok; +} +pub export fn napi_get_prototype(env: napi_env, object: napi_value, result: *napi_value) napi_status { + if (!object.isObject()) { + return .object_expected; + } + + result.* = JSValue.c(JSC.C.JSObjectGetPrototype(env.ref(), object.asObjectRef())); + return .ok; +} +// TODO: bind JSC::ownKeys +// pub export fn napi_get_property_names(env: napi_env, object: napi_value, result: *napi_value) napi_status { +// if (!object.isObject()) { +// return .object_expected; +// } + +// result.* = +// } +pub export fn napi_set_element(env: napi_env, object: napi_value, index: c_uint, value: napi_value) napi_status { + if (!object.jsType().isIndexable()) { + return .array_expected; + } + if (value.isEmpty()) + return .invalid_arg; + JSC.C.JSObjectSetPropertyAtIndex(env.ref(), object.asObjectRef(), index, value.asObjectRef(), TODO_EXCEPTION); + return .ok; +} +pub export fn napi_has_element(env: napi_env, object: napi_value, index: c_uint, result: *bool) napi_status { + if (!object.jsType().isIndexable()) { + return .array_expected; + } + + result.* = object.getLengthOfArray(env) > index; + return .ok; +} +pub export fn napi_get_element(env: napi_env, object: napi_value, index: u32, result: *napi_value) napi_status { + if (!object.jsType().isIndexable()) { + return .array_expected; + } + + result.* = JSC.JSObject.getIndex(object, env, index); + return .ok; +} +pub extern fn napi_define_properties(env: napi_env, object: napi_value, property_count: usize, properties: [*c]const napi_property_descriptor) napi_status; +pub export fn napi_is_array(_: napi_env, value: napi_value, result: *bool) napi_status { + result.* = value.jsType().isArray(); + return .ok; +} +pub export fn napi_get_array_length(env: napi_env, value: napi_value, result: [*c]u32) napi_status { + if (!value.jsType().isArray()) { + return .array_expected; + } + + result.* = value.getLengthOfArray(env); + return .ok; +} +pub export fn napi_strict_equals(env: napi_env, lhs: napi_value, rhs: napi_value, result: *bool) napi_status { + // there is some nuance with NaN here i'm not sure about + result.* = lhs.isSameValue(rhs, env); + return .ok; +} +pub export fn napi_call_function(env: napi_env, recv: napi_value, func: napi_value, argc: usize, argv: [*c]const napi_value, result: *napi_value) napi_status { + if (comptime JSC.is_bindgen) unreachable; + var exception = [_]JSC.C.JSValueRef{null}; + result.* = + JSC.C.JSObjectCallAsFunctionReturnValue( + env.ref(), + func.asObjectRef(), + recv.asObjectRef(), + argc, + @ptrCast([*]const JSC.C.JSValueRef, argv), + ); + if (exception[0] != null) { + return .generic_failure; + } + + return .ok; +} +pub export fn napi_new_instance(env: napi_env, constructor: napi_value, argc: usize, argv: [*c]const napi_value, result: *napi_value) napi_status { + var exception = [_]JSC.C.JSValueRef{null}; + result.* = JSValue.c( + JSC.C.JSObjectCallAsConstructor( + env.ref(), + constructor.asObjectRef(), + argc, + @ptrCast([*]const JSC.C.JSValueRef, argv), + &exception, + ), + ); + if (exception[0] != null) { + return .generic_failure; + } + + return .ok; +} +pub export fn napi_instanceof(env: napi_env, object: napi_value, constructor: napi_value, result: *bool) napi_status { + // TODO: does this throw object_expected in node? + result.* = object.isInstanceOf(env, constructor); + return .ok; +} +pub extern fn napi_get_cb_info(env: napi_env, cbinfo: napi_callback_info, argc: [*c]usize, argv: *napi_value, this_arg: *napi_value, data: [*]*anyopaque) napi_status; +pub extern fn napi_get_new_target(env: napi_env, cbinfo: napi_callback_info, result: *napi_value) napi_status; +pub extern fn napi_define_class( + env: napi_env, + utf8name: [*c]const u8, + length: usize, + constructor: napi_callback, + data: ?*anyopaque, + property_count: usize, + properties: [*c]const napi_property_descriptor, + result: *napi_value, +) napi_status; +pub extern fn napi_wrap(env: napi_env, js_object: napi_value, native_object: ?*anyopaque, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque, result: [*c]napi_ref) napi_status; +pub extern fn napi_unwrap(env: napi_env, js_object: napi_value, result: [*]*anyopaque) napi_status; +pub extern fn napi_remove_wrap(env: napi_env, js_object: napi_value, result: [*]*anyopaque) napi_status; +pub extern fn napi_create_external(env: napi_env, data: ?*anyopaque, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque, result: *napi_value) napi_status; +pub extern fn napi_get_value_external(env: napi_env, value: napi_value, result: [*]*anyopaque) napi_status; +pub extern fn napi_create_reference(env: napi_env, value: napi_value, initial_refcount: u32, result: [*c]napi_ref) napi_status; +pub extern fn napi_delete_reference(env: napi_env, ref: napi_ref) napi_status; +pub extern fn napi_reference_ref(env: napi_env, ref: napi_ref, result: [*c]u32) napi_status; +pub extern fn napi_reference_unref(env: napi_env, ref: napi_ref, result: [*c]u32) napi_status; +pub extern fn napi_get_reference_value(env: napi_env, ref: napi_ref, result: *napi_value) napi_status; + +// JSC scans the stack +// we don't need this +pub export fn napi_open_handle_scope(env: napi_env, result: *napi_handle_scope) napi_status { + result.* = env; + return .ok; +} +// JSC scans the stack +// we don't need this +pub export fn napi_close_handle_scope(_: napi_env, _: napi_handle_scope) napi_status { + return .ok; +} +pub extern fn napi_open_escapable_handle_scope(env: napi_env, result: [*c]napi_escapable_handle_scope) napi_status; +pub extern fn napi_close_escapable_handle_scope(env: napi_env, scope: napi_escapable_handle_scope) napi_status; +pub extern fn napi_escape_handle(env: napi_env, scope: napi_escapable_handle_scope, escapee: napi_value, result: *napi_value) napi_status; +pub extern fn napi_throw(env: napi_env, @"error": napi_value) napi_status; +pub extern fn napi_throw_error(env: napi_env, code: [*c]const u8, msg: [*c]const u8) napi_status; +pub extern fn napi_throw_type_error(env: napi_env, code: [*c]const u8, msg: [*c]const u8) napi_status; +pub extern fn napi_throw_range_error(env: napi_env, code: [*c]const u8, msg: [*c]const u8) napi_status; +pub export fn napi_is_error(env: napi_env, value: napi_value, result: *bool) napi_status { + result.* = value.isAnyError(env); + return .ok; +} +pub extern fn napi_is_exception_pending(env: napi_env, result: *bool) napi_status; +pub extern fn napi_get_and_clear_last_exception(env: napi_env, result: *napi_value) napi_status; +pub export fn napi_is_arraybuffer(_: napi_env, value: napi_value, result: *bool) napi_status { + result.* = !value.isNumber() and value.jsTypeLoose() == .ArrayBuffer; + return .ok; +} +pub export fn napi_create_arraybuffer(env: napi_env, byte_length: usize, data: [*]const u8, result: *napi_value) napi_status { + var typed_array = JSC.C.JSObjectMakeTypedArray(env.ref(), .kJSTypedArrayTypeArrayBuffer, byte_length, TODO_EXCEPTION); + var array_buffer = JSValue.c(typed_array).asArrayBuffer(env) orelse return .generic_failure; + @memcpy(array_buffer.ptr, data, @minimum(array_buffer.len, @truncate(u32, byte_length))); + result.* = JSValue.c(typed_array); + return .ok; +} + +pub export fn napi_create_external_arraybuffer(env: napi_env, external_data: ?*anyopaque, byte_length: usize, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque, result: *napi_value) napi_status { + var external = JSC.ExternalBuffer.create( + finalize_hint, + @ptrCast([*]u8, external_data.?)[0..byte_length], + env, + finalize_cb, + env.bunVM().allocator, + ) catch { + return .generic_failure; + }; + result.* = external.toArrayBuffer(env); + return .ok; +} +pub export fn napi_get_arraybuffer_info(env: napi_env, arraybuffer: napi_value, data: *[*]u8, byte_length: *usize) napi_status { + const array_buffer = arraybuffer.asArrayBuffer(env) orelse return .arraybuffer_expected; + var slice = array_buffer.slice(); + data.* = slice.ptr; + byte_length.* = slice.len; + return .ok; +} +pub export fn napi_is_typedarray(_: napi_env, value: napi_value, result: *bool) napi_status { + result.* = value.jsTypeLoose().isTypedArray(); + return .ok; +} +pub export fn napi_create_typedarray(env: napi_env, @"type": napi_typedarray_type, length: usize, arraybuffer: napi_value, byte_offset: usize, result: *napi_value) napi_status { + result.* = JSValue.c( + JSC.C.JSObjectMakeTypedArrayWithArrayBufferAndOffset( + env.ref(), + @"type".toC(), + arraybuffer.asObjectRef(), + byte_offset, + length, + TODO_EXCEPTION, + ), + ); + return .ok; +} +pub export fn napi_get_typedarray_info( + env: napi_env, + typedarray: napi_value, + @"type": ?*napi_typedarray_type, + length: ?*usize, + data: ?*[*]u8, + arraybuffer: ?*napi_value, + byte_offset: ?*usize, +) napi_status { + if (typedarray.isEmptyOrUndefinedOrNull()) + return .invalid_arg; + defer typedarray.ensureStillAlive(); + + const array_buffer = typedarray.asArrayBuffer(env) orelse return .invalid_arg; + if (@"type" != null) + @"type".?.* = napi_typedarray_type.fromJSType(array_buffer.typed_array_type) orelse return .invalid_arg; + var slice = array_buffer.slice(); + + if (data != null) + data.?.* = slice.ptr; + + if (length != null) + length.?.* = slice.len; + + if (arraybuffer != null) + arraybuffer.?.* = JSValue.c(JSC.C.JSObjectGetTypedArrayBuffer(env.ref(), typedarray.asObjectRef(), null)); + + if (byte_offset != null) + byte_offset.?.* = array_buffer.offset; + return .ok; +} +pub extern fn napi_create_dataview(env: napi_env, length: usize, arraybuffer: napi_value, byte_offset: usize, result: *napi_value) napi_status; +pub export fn napi_is_dataview(_: napi_env, value: napi_value, result: *bool) napi_status { + result.* = !value.isEmptyOrUndefinedOrNull() and value.jsTypeLoose() == .DataView; + return .ok; +} +pub export fn napi_get_dataview_info(env: napi_env, dataview: napi_value, bytelength: *usize, data: *?[*]u8, arraybuffer: *napi_value, byte_offset: *usize) napi_status { + var array_buffer = dataview.asArrayBuffer(env) orelse return .object_expected; + bytelength.* = array_buffer.byte_len; + data.* = array_buffer.ptr; + // TODO: will this work? will it fail due to being a DataView instead of a TypedArray? + arraybuffer.* = JSValue.c(JSC.C.JSObjectGetTypedArrayBuffer(env.ref(), dataview.asObjectRef(), null)); + byte_offset.* = array_buffer.offset; + return .ok; +} +pub export fn napi_get_version(_: napi_env, result: *u32) napi_status { + result.* = NAPI_VERSION; + return .ok; +} +pub export fn napi_create_promise(env: napi_env, deferred: *napi_deferred, promise: *napi_value) napi_status { + deferred.* = JSC.JSPromise.create(env); + promise.* = deferred.*.asValue(env); + return .ok; +} +pub export fn napi_resolve_deferred(env: napi_env, deferred: napi_deferred, resolution: napi_value) napi_status { + deferred.resolve(env, resolution); + return .ok; +} +pub export fn napi_reject_deferred(env: napi_env, deferred: napi_deferred, rejection: napi_value) napi_status { + deferred.reject(env, rejection); + return .ok; +} +pub export fn napi_is_promise(_: napi_env, value: napi_value, is_promise: *bool) napi_status { + if (value.isEmptyOrUndefinedOrNull()) { + is_promise.* = false; + return .ok; + } + + is_promise.* = value.asPromise() != null or value.asInternalPromise() != null; + return .ok; +} +pub export fn napi_run_script(env: napi_env, script: napi_value, result: *napi_value) napi_status { + // TODO: don't copy + var ref = JSC.C.JSValueToStringCopy(env.ref(), script.asObjectRef(), TODO_EXCEPTION); + defer JSC.C.JSStringRelease(ref); + + var exception = [_]JSC.C.JSValueRef{null}; + const val = JSC.C.JSEvaluateScript(env.ref(), ref, env.ref(), null, 0, &exception); + if (exception[0] != null) { + return .generic_failure; + } + + result.* = JSValue.c(val); + return .ok; +} +pub extern fn napi_adjust_external_memory(env: napi_env, change_in_bytes: i64, adjusted_value: [*c]i64) napi_status; +pub export fn napi_create_date(env: napi_env, time: f64, result: *napi_value) napi_status { + var args = [_]JSC.C.JSValueRef{JSC.JSValue.jsNumber(time).asObjectRef()}; + result.* = JSValue.c(JSC.C.JSObjectMakeDate(env.ref(), 1, &args, TODO_EXCEPTION)); + return .ok; +} +pub export fn napi_is_date(_: napi_env, value: napi_value, is_date: *bool) napi_status { + is_date.* = value.jsTypeLoose() == .JSDate; + return .ok; +} +pub export fn napi_get_date_value(env: napi_env, value: napi_value, result: *f64) napi_status { + const getTimeFunction = value.get(env, "getTime") orelse { + return .date_expected; + }; + + result.* = JSValue.c( + JSC.C.JSObjectCallAsFunction(env.ref(), getTimeFunction.asObjectRef(), value.asObjectRef(), 0, null, TODO_EXCEPTION), + ).asNumber(); + return .ok; +} +pub extern fn napi_add_finalizer(env: napi_env, js_object: napi_value, native_object: ?*anyopaque, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque, result: *napi_ref) napi_status; +pub export fn napi_create_bigint_int64(env: napi_env, value: i64, result: *napi_value) napi_status { + result.* = JSC.JSValue.fromInt64NoTruncate(env, value); + return .ok; +} +pub export fn napi_create_bigint_uint64(env: napi_env, value: u64, result: *napi_value) napi_status { + result.* = JSC.JSValue.fromUInt64NoTruncate(env, value); + return .ok; +} +pub extern fn napi_create_bigint_words(env: napi_env, sign_bit: c_int, word_count: usize, words: [*c]const u64, result: *napi_value) napi_status; +// TODO: lossless +pub export fn napi_get_value_bigint_int64(_: napi_env, value: napi_value, result: *i64, _: *bool) napi_status { + result.* = value.toInt64(); + return .ok; +} +// TODO: lossless +pub export fn napi_get_value_bigint_uint64(_: napi_env, value: napi_value, result: *u64, _: *bool) napi_status { + result.* = value.toUInt64NoTruncate(); + return .ok; +} +pub extern fn napi_get_value_bigint_words(env: napi_env, value: napi_value, sign_bit: [*c]c_int, word_count: [*c]usize, words: [*c]u64) napi_status; +pub extern fn napi_get_all_property_names(env: napi_env, object: napi_value, key_mode: napi_key_collection_mode, key_filter: napi_key_filter, key_conversion: napi_key_conversion, result: *napi_value) napi_status; +pub extern fn napi_set_instance_data(env: napi_env, data: ?*anyopaque, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque) napi_status; +pub extern fn napi_get_instance_data(env: napi_env, data: [*]*anyopaque) napi_status; +pub extern fn napi_detach_arraybuffer(env: napi_env, arraybuffer: napi_value) napi_status; +pub extern fn napi_is_detached_arraybuffer(env: napi_env, value: napi_value, result: *bool) napi_status; +pub extern fn napi_type_tag_object(env: napi_env, value: napi_value, type_tag: [*c]const napi_type_tag) napi_status; +pub extern fn napi_check_object_type_tag(env: napi_env, value: napi_value, type_tag: [*c]const napi_type_tag, result: *bool) napi_status; +pub const struct_napi_async_work__ = opaque {}; +const WorkPool = @import("../work_pool.zig").WorkPool; +const WorkPoolTask = @import("../work_pool.zig").Task; + +/// must be globally allocated +pub const napi_async_work = struct { + task: WorkPoolTask = .{ .callback = runFromThreadPool }, + completion_task: ?*anyopaque = null, + event_loop: *JSC.EventLoop, + global: napi_env, + execute: napi_async_execute_callback = null, + complete: napi_async_complete_callback = null, + ctx: ?*anyopaque = null, + status: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0), + can_deinit: bool = false, + wait_for_deinit: bool = false, + scheduled: bool = false, + pub const Status = enum(u32) { + pending = 0, + started = 1, + completed = 2, + cancelled = 3, + }; + + pub fn create(global: napi_env, execute: napi_async_execute_callback, complete: napi_async_complete_callback, ctx: ?*anyopaque) !*napi_async_work { + var work = try bun.default_allocator.create(napi_async_work); + work.* = .{ + .global = global, + .execute = execute, + .event_loop = global.bunVM().eventLoop(), + .complete = complete, + .ctx = ctx, + }; + return work; + } + + pub fn runFromThreadPool(task: *WorkPoolTask) void { + var this = @fieldParentPtr(napi_async_work, "task", task); + + this.run(); + } + pub fn run(this: *napi_async_work) void { + if (this.status.compareAndSwap(@enumToInt(Status.pending), @enumToInt(Status.started), .SeqCst, .SeqCst)) |state| { + if (state == @enumToInt(Status.cancelled)) { + if (this.wait_for_deinit) { + // this might cause a segfault due to Task using a linked list! + bun.default_allocator.destroy(this); + } + } + return; + } + this.execute.?(this.global, this.ctx); + this.status.store(@enumToInt(Status.completed), .SeqCst); + + this.event_loop.enqueueTaskConcurrent(JSC.Task.init(this)); + } + + pub fn schedule(this: *napi_async_work) void { + if (this.scheduled) return; + this.scheduled = true; + WorkPool.schedule(&this.task); + } + + pub fn cancel(this: *napi_async_work) bool { + return this.status.compareAndSwap(@enumToInt(Status.cancelled), @enumToInt(Status.pending), .SeqCst, .SeqCst) != null; + } + + pub fn deinit(this: *napi_async_work) void { + if (this.can_deinit) { + bun.default_allocator.destroy(this); + return; + } + this.wait_for_deinit = true; + } + + pub fn runFromJS(this: *napi_async_work) void { + this.complete.?( + this.global, + if (this.status.load(.SeqCst) == @enumToInt(Status.cancelled)) + napi_status.cancelled + else + napi_status.ok, + this.ctx.?, + ); + } +}; +pub const napi_threadsafe_function = *ThreadSafeFunction; +pub const napi_threadsafe_function_release_mode = enum(c_uint) { + release = 0, + abort = 1, +}; +pub const napi_tsfn_nonblocking = 0; +pub const napi_tsfn_blocking = 1; +pub const napi_threadsafe_function_call_mode = c_uint; +pub const napi_async_execute_callback = ?fn (napi_env, ?*anyopaque) callconv(.C) void; +pub const napi_async_complete_callback = ?fn (napi_env, napi_status, ?*anyopaque) callconv(.C) void; +pub const napi_threadsafe_function_call_js = fn (napi_env, napi_value, ?*anyopaque, ?*anyopaque) callconv(.C) void; +pub const napi_node_version = extern struct { + major: u32, + minor: u32, + patch: u32, + release: [*c]const u8, + + pub const global: napi_node_version = .{ + .major = 17, + .minor = 7, + .patch = 17, + .release = "Bun!!!", + }; +}; +pub const struct_napi_async_cleanup_hook_handle__ = opaque {}; +pub const napi_async_cleanup_hook_handle = ?*struct_napi_async_cleanup_hook_handle__; +pub const napi_async_cleanup_hook = ?fn (napi_async_cleanup_hook_handle, ?*anyopaque) callconv(.C) void; + +pub const napi_addon_register_func = ?fn (napi_env, napi_value) callconv(.C) napi_value; +pub const struct_napi_module = extern struct { + nm_version: c_int, + nm_flags: c_uint, + nm_filename: [*c]const u8, + nm_register_func: napi_addon_register_func, + nm_modname: [*c]const u8, + nm_priv: ?*anyopaque, + reserved: [4]?*anyopaque, +}; +pub const napi_module = struct_napi_module; +fn napiSpan(ptr: anytype, len: usize) []const u8 { + if (ptr == null) + return &[_]u8{}; + + if (len == NAPI_AUTO_LENGTH) { + return std.mem.sliceTo(ptr.?, 0); + } + + return ptr.?[0..len]; +} +pub export fn napi_fatal_error(location_ptr: ?[*:0]const u8, location_len: usize, message_ptr: ?[*:0]const u8, message_len_: usize) noreturn { + var message = napiSpan(message_ptr, message_len_); + if (message.len == 0) { + message = "fatal error"; + } + + const location = napiSpan(location_ptr, location_len); + if (location.len > 0) { + bun.Global.panic("napi: {s}\n {s}", .{ message, location }); + } + + bun.Global.panic("napi: {s}", .{message}); +} +pub export fn napi_create_buffer(env: napi_env, length: usize, data: [*]*anyopaque, result: *napi_value) napi_status { + var buf = JSC.ExternalBuffer.create(null, @ptrCast([*]u8, data)[0..length], env, null, env.bunVM().allocator) catch { + return .generic_failure; + }; + + result.* = buf.toJS(env); + return .ok; +} +pub export fn napi_create_external_buffer(env: napi_env, length: usize, data: ?*anyopaque, finalize_cb: napi_finalize, finalize_hint: ?*anyopaque, result: *napi_value) napi_status { + var buf = JSC.ExternalBuffer.create(finalize_hint, @ptrCast([*]u8, data.?)[0..length], env, finalize_cb, env.bunVM().allocator) catch { + return .generic_failure; + }; + + result.* = buf.toJS(env); + return .ok; +} +pub export fn napi_create_buffer_copy(env: napi_env, length: usize, data: [*]u8, result_data: ?*?*anyopaque, result: *napi_value) napi_status { + var duped = env.bunVM().allocator.alloc(u8, length) catch { + return .generic_failure; + }; + @memcpy(duped.ptr, data, length); + if (result_data) |res| { + res.* = duped.ptr; + } + + result.* = JSC.JSValue.createBuffer(env, duped, env.bunVM().allocator); + + return .ok; +} +pub export fn napi_is_buffer(env: napi_env, value: napi_value, result: *bool) napi_status { + result.* = value.isBuffer(env); + return .ok; +} +pub export fn napi_get_buffer_info(env: napi_env, value: napi_value, data: *[*]u8, length: *usize) napi_status { + const array_buf = value.asArrayBuffer(env) orelse { + // TODO: is invalid_arg what to return here? + return .arraybuffer_expected; + }; + + data.* = array_buf.ptr; + length.* = array_buf.byte_len; + return .ok; +} +pub export fn napi_create_async_work( + env: napi_env, + _: napi_value, + _: [*:0]const u8, + execute: napi_async_execute_callback, + complete: napi_async_complete_callback, + data: ?*anyopaque, + result: **napi_async_work, +) napi_status { + result.* = napi_async_work.create(env, execute, complete, data) catch { + return .generic_failure; + }; + return .ok; +} +pub export fn napi_delete_async_work(env: napi_env, work: *napi_async_work) napi_status { + std.debug.assert(env == work.global); + work.deinit(); + return .ok; +} +pub export fn napi_queue_async_work(env: napi_env, work: *napi_async_work) napi_status { + std.debug.assert(env == work.global); + work.schedule(); + return .ok; +} +pub export fn napi_cancel_async_work(env: napi_env, work: *napi_async_work) napi_status { + std.debug.assert(env == work.global); + if (work.cancel()) { + return .ok; + } + + return napi_status.generic_failure; +} +pub export fn napi_get_node_version(_: napi_env, version: **const napi_node_version) napi_status { + version.* = &napi_node_version.global; + return .ok; +} +pub export fn napi_get_uv_event_loop(env: napi_env, loop: **JSC.EventLoop) napi_status { + // lol + loop.* = env.bunVM().eventLoop(); + return .ok; +} +pub extern fn napi_fatal_exception(env: napi_env, err: napi_value) napi_status; + +// We use a linked list here because we assume removing these is relatively rare +// and array reallocations are relatively expensive. +pub export fn napi_add_env_cleanup_hook(env: napi_env, fun: ?fn (?*anyopaque) callconv(.C) void, arg: ?*anyopaque) napi_status { + if (fun == null) + return .ok; + + env.bunVM().rareData().pushCleanupHook(env, arg, fun.?); + return .ok; +} +pub export fn napi_remove_env_cleanup_hook(env: napi_env, fun: ?fn (?*anyopaque) callconv(.C) void, arg: ?*anyopaque) napi_status { + if (env.bunVM().rare_data == null or fun == null) + return .ok; + + var rare_data = env.bunVM().rare_data.?; + var hook = rare_data.cleanup_hook orelse return .ok; + const cmp = JSC.RareData.CleanupHook.from(env, arg, fun.?); + if (hook.eql(cmp)) { + env.bunVM().allocator.destroy(hook); + rare_data.cleanup_hook = null; + rare_data.tail_cleanup_hook = null; + } + while (hook.next) |current| { + if (hook.eql(cmp)) { + if (current.next) |next| { + hook.next = next; + } else { + hook.next = null; + } + env.bunVM().allocator.destroy(current); + return .ok; + } + hook = current; + } + + return .ok; +} + +pub const Finalizer = struct { + fun: napi_finalize, + ctx: ?*anyopaque = null, +}; + +// TODO: generate comptime version of this instead of runtime checking +pub const ThreadSafeFunction = struct { + /// thread-safe functions can be "referenced" and "unreferenced". A + /// "referenced" thread-safe function will cause the event loop on the thread + /// on which it is created to remain alive until the thread-safe function is + /// destroyed. In contrast, an "unreferenced" thread-safe function will not + /// prevent the event loop from exiting. The APIs napi_ref_threadsafe_function + /// and napi_unref_threadsafe_function exist for this purpose. + /// + /// Neither does napi_unref_threadsafe_function mark the thread-safe + /// functions as able to be destroyed nor does napi_ref_threadsafe_function + /// prevent it from being destroyed. + ref_for_process_exit: bool = false, + + owning_threads: std.AutoArrayHashMapUnmanaged(u64, void) = .{}, + owning_thread_lock: Lock = Lock.init(), + event_loop: *JSC.EventLoop, + + javascript_function: JSValue, + finalizer_task: JSC.AnyTask = undefined, + finalizer: Finalizer = Finalizer{ .fun = null, .ctx = null }, + channel: Queue, + + ctx: ?*anyopaque = null, + + call_js: ?napi_threadsafe_function_call_js = null, + + const ThreadSafeFunctionTask = JSC.AnyTask.New(@This(), call); + pub const Queue = union(enum) { + sized: Channel(?*anyopaque, .Slice), + unsized: Channel(?*anyopaque, .Dynamic), + + pub fn isClosed(this: *const @This()) bool { + return @atomicLoad( + bool, + switch (this.*) { + .sized => &this.sized.is_closed, + .unsized => &this.unsized.is_closed, + }, + .SeqCst, + ); + } + + pub fn close(this: *@This()) void { + switch (this.*) { + .sized => this.sized.close(), + .unsized => this.unsized.close(), + } + } + + pub fn init(size: usize, allocator: std.mem.Allocator) @This() { + switch (size) { + 0 => { + return .{ + .unsized = Channel(?*anyopaque, .Dynamic).init(allocator), + }; + }, + else => { + var slice = allocator.alloc(?*anyopaque, size) catch unreachable; + return .{ + .sized = Channel(?*anyopaque, .Slice).init(slice), + }; + }, + } + } + + pub fn writeItem(this: *@This(), value: ?*anyopaque) !void { + switch (this.*) { + .sized => try this.sized.writeItem(value), + .unsized => try this.unsized.writeItem(value), + } + } + + pub fn readItem(this: *@This()) !?*anyopaque { + return switch (this.*) { + .sized => try this.sized.readItem(), + .unsized => try this.unsized.readItem(), + }; + } + + pub fn tryWriteItem(this: *@This(), value: ?*anyopaque) !bool { + return switch (this.*) { + .sized => try this.sized.tryWriteItem(value), + .unsized => try this.unsized.tryWriteItem(value), + }; + } + + pub fn tryReadItem(this: *@This()) !??*anyopaque { + return switch (this.*) { + .sized => try this.sized.tryReadItem(), + .unsized => try this.unsized.tryReadItem(), + }; + } + }; + + pub fn call(this: *ThreadSafeFunction) void { + var task = this.channel.tryReadItem() catch null orelse return; + if (this.call_js) |cb| { + cb(this.event_loop.global, this.javascript_function, task, this.ctx); + } else { + // TODO: wrapper that reports errors + _ = JSC.C.JSObjectCallAsFunction( + this.event_loop.global.ref(), + this.javascript_function.asObjectRef(), + JSC.JSValue.jsUndefined().asObjectRef(), + 0, + null, + null, + ); + } + } + + pub fn enqueue(this: *ThreadSafeFunction, ctx: ?*anyopaque, block: bool) !void { + if (block) { + try this.channel.writeItem(ctx); + } else { + if (!try this.channel.tryWriteItem(ctx)) { + return error.WouldBlock; + } + } + + this.event_loop.enqueueTaskConcurrent(JSC.Task.init(this)); + } + + pub fn finalize(opaq: *anyopaque) void { + var this = bun.cast(*ThreadSafeFunction, opaq); + if (this.finalizer.fun) |fun| { + fun(this.event_loop.global, opaq, this.finalizer.ctx); + } + + JSC.C.JSValueUnprotect(this.event_loop.global.ref(), this.javascript_function.asObjectRef()); + bun.default_allocator.destroy(this); + } + + pub fn ref(this: *ThreadSafeFunction) void { + this.ref_for_process_exit = true; + } + + pub fn unref(this: *ThreadSafeFunction) void { + this.ref_for_process_exit = false; + } + + pub fn acquire(this: *ThreadSafeFunction) !void { + this.owning_thread_lock.lock(); + defer this.owning_thread_lock.unlock(); + if (this.channel.isClosed()) + return error.Closed; + _ = this.owning_threads.getOrPut(bun.default_allocator, std.Thread.getCurrentId()) catch unreachable; + } + + pub fn release(this: *ThreadSafeFunction, mode: napi_threadsafe_function_release_mode) void { + this.owning_thread_lock.lock(); + defer this.owning_thread_lock.unlock(); + if (!this.owning_threads.swapRemove(std.Thread.getCurrentId())) + return; + + if (mode == .abort) { + this.channel.close(); + } + + if (this.owning_threads.count() == 0) { + this.finalizer_task = JSC.AnyTask{ .ctx = this, .callback = finalize }; + this.event_loop.enqueueTaskConcurrent(JSC.Task.init(&this.finalizer_task)); + return; + } + } +}; + +pub export fn napi_create_threadsafe_function( + env: napi_env, + func: napi_value, + _: napi_value, + _: napi_value, + max_queue_size: usize, + initial_thread_count: usize, + thread_finalize_data: ?*anyopaque, + thread_finalize_cb: napi_finalize, + context: ?*anyopaque, + call_js_cb: ?napi_threadsafe_function_call_js, + result: *napi_threadsafe_function, +) napi_status { + // TODO: don't do this + // just have a GC hook for this... + JSC.C.JSValueProtect(env.ref(), func.asObjectRef()); + var function = bun.default_allocator.create(ThreadSafeFunction) catch return .generic_failure; + function.* = .{ + .event_loop = env.bunVM().eventLoop(), + .javascript_function = func, + .call_js = call_js_cb, + .ctx = context, + .channel = ThreadSafeFunction.Queue.init(max_queue_size, bun.default_allocator), + .owning_threads = .{}, + }; + function.owning_threads.ensureTotalCapacity(bun.default_allocator, initial_thread_count) catch return .generic_failure; + function.finalizer = .{ .ctx = thread_finalize_data, .fun = thread_finalize_cb }; + result.* = function; + return .ok; +} +pub export fn napi_get_threadsafe_function_context(func: napi_threadsafe_function, result: *?*anyopaque) napi_status { + result.* = func.ctx; + return .ok; +} +pub export fn napi_call_threadsafe_function(func: napi_threadsafe_function, data: ?*anyopaque, is_blocking: napi_threadsafe_function_call_mode) napi_status { + func.enqueue(data, is_blocking == napi_tsfn_blocking) catch |err| { + switch (err) { + error.WouldBlock => { + return napi_status.queue_full; + }, + + else => return .closing, + } + }; + return .ok; +} +pub export fn napi_acquire_threadsafe_function(func: napi_threadsafe_function) napi_status { + func.acquire() catch return .closing; + return .ok; +} +pub export fn napi_release_threadsafe_function(func: napi_threadsafe_function, mode: napi_threadsafe_function_release_mode) napi_status { + func.release(mode); + return .ok; +} +pub export fn napi_unref_threadsafe_function(env: napi_env, func: napi_threadsafe_function) napi_status { + std.debug.assert(func.event_loop.global == env); + + func.unref(); + return .ok; +} +pub export fn napi_ref_threadsafe_function(env: napi_env, func: napi_threadsafe_function) napi_status { + std.debug.assert(func.event_loop.global == env); + + func.ref(); + return .ok; +} + +pub export fn napi_add_async_cleanup_hook(_: napi_env, _: napi_async_cleanup_hook, _: ?*anyopaque, _: [*c]napi_async_cleanup_hook_handle) napi_status { + // TODO: + return .ok; +} +pub export fn napi_remove_async_cleanup_hook(_: napi_async_cleanup_hook_handle) napi_status { + // TODO: + return .ok; +} + +pub const NAPI_VERSION_EXPERIMENTAL = @import("std").zig.c_translation.promoteIntLiteral(c_int, 2147483647, .decimal); +pub const NAPI_VERSION = @as(c_int, 8); +pub const NAPI_AUTO_LENGTH = std.math.maxInt(usize); +pub const SRC_NODE_API_TYPES_H_ = ""; +pub const NAPI_MODULE_VERSION = @as(c_int, 1); + +pub fn fixDeadCodeElimination() void { + if (comptime JSC.is_bindgen) unreachable; + std.mem.doNotOptimizeAway(&napi_get_undefined); + std.mem.doNotOptimizeAway(&napi_get_null); + std.mem.doNotOptimizeAway(&napi_get_boolean); + std.mem.doNotOptimizeAway(&napi_create_object); + std.mem.doNotOptimizeAway(&napi_create_array); + std.mem.doNotOptimizeAway(&napi_create_array_with_length); + std.mem.doNotOptimizeAway(&napi_create_double); + std.mem.doNotOptimizeAway(&napi_create_int32); + std.mem.doNotOptimizeAway(&napi_create_uint32); + std.mem.doNotOptimizeAway(&napi_create_int64); + std.mem.doNotOptimizeAway(&napi_create_string_latin1); + std.mem.doNotOptimizeAway(&napi_create_string_utf8); + std.mem.doNotOptimizeAway(&napi_create_string_utf16); + std.mem.doNotOptimizeAway(&napi_create_symbol); + std.mem.doNotOptimizeAway(&napi_create_error); + std.mem.doNotOptimizeAway(&napi_typeof); + std.mem.doNotOptimizeAway(&napi_get_value_double); + std.mem.doNotOptimizeAway(&napi_get_value_int32); + std.mem.doNotOptimizeAway(&napi_get_value_uint32); + std.mem.doNotOptimizeAway(&napi_get_value_int64); + std.mem.doNotOptimizeAway(&napi_get_value_bool); + std.mem.doNotOptimizeAway(&napi_get_value_string_latin1); + std.mem.doNotOptimizeAway(&napi_get_value_string_utf8); + std.mem.doNotOptimizeAway(&napi_get_value_string_utf16); + std.mem.doNotOptimizeAway(&napi_coerce_to_bool); + std.mem.doNotOptimizeAway(&napi_coerce_to_number); + std.mem.doNotOptimizeAway(&napi_coerce_to_object); + // std.mem.doNotOptimizeAway(&napi_coerce_to_string); + std.mem.doNotOptimizeAway(&napi_get_prototype); + // std.mem.doNotOptimizeAway(&napi_get_property_names); + std.mem.doNotOptimizeAway(&napi_set_element); + std.mem.doNotOptimizeAway(&napi_has_element); + std.mem.doNotOptimizeAway(&napi_get_element); + // std.mem.doNotOptimizeAway(&napi_delete_element); + std.mem.doNotOptimizeAway(&napi_is_array); + std.mem.doNotOptimizeAway(&napi_get_array_length); + std.mem.doNotOptimizeAway(&napi_strict_equals); + std.mem.doNotOptimizeAway(&napi_call_function); + std.mem.doNotOptimizeAway(&napi_new_instance); + std.mem.doNotOptimizeAway(&napi_instanceof); + std.mem.doNotOptimizeAway(&napi_open_handle_scope); + std.mem.doNotOptimizeAway(&napi_close_handle_scope); + std.mem.doNotOptimizeAway(&napi_is_error); + std.mem.doNotOptimizeAway(&napi_is_arraybuffer); + std.mem.doNotOptimizeAway(&napi_create_arraybuffer); + std.mem.doNotOptimizeAway(&napi_create_external_arraybuffer); + std.mem.doNotOptimizeAway(&napi_get_arraybuffer_info); + std.mem.doNotOptimizeAway(&napi_is_typedarray); + std.mem.doNotOptimizeAway(&napi_create_typedarray); + std.mem.doNotOptimizeAway(&napi_get_typedarray_info); + std.mem.doNotOptimizeAway(&napi_is_dataview); + std.mem.doNotOptimizeAway(&napi_get_dataview_info); + std.mem.doNotOptimizeAway(&napi_get_version); + std.mem.doNotOptimizeAway(&napi_create_promise); + std.mem.doNotOptimizeAway(&napi_resolve_deferred); + std.mem.doNotOptimizeAway(&napi_reject_deferred); + std.mem.doNotOptimizeAway(&napi_is_promise); + std.mem.doNotOptimizeAway(&napi_run_script); + std.mem.doNotOptimizeAway(&napi_create_date); + std.mem.doNotOptimizeAway(&napi_is_date); + std.mem.doNotOptimizeAway(&napi_get_date_value); + std.mem.doNotOptimizeAway(&napi_create_bigint_int64); + std.mem.doNotOptimizeAway(&napi_create_bigint_uint64); + std.mem.doNotOptimizeAway(&napi_get_value_bigint_int64); + std.mem.doNotOptimizeAway(&napi_get_value_bigint_uint64); + std.mem.doNotOptimizeAway(&napi_fatal_error); + std.mem.doNotOptimizeAway(&napi_create_buffer); + std.mem.doNotOptimizeAway(&napi_create_external_buffer); + std.mem.doNotOptimizeAway(&napi_create_buffer_copy); + std.mem.doNotOptimizeAway(&napi_is_buffer); + std.mem.doNotOptimizeAway(&napi_get_buffer_info); + std.mem.doNotOptimizeAway(&napi_create_async_work); + std.mem.doNotOptimizeAway(&napi_delete_async_work); + std.mem.doNotOptimizeAway(&napi_queue_async_work); + std.mem.doNotOptimizeAway(&napi_cancel_async_work); + std.mem.doNotOptimizeAway(&napi_get_node_version); + std.mem.doNotOptimizeAway(&napi_get_uv_event_loop); + std.mem.doNotOptimizeAway(&napi_add_env_cleanup_hook); + std.mem.doNotOptimizeAway(&napi_remove_env_cleanup_hook); + std.mem.doNotOptimizeAway(&napi_create_threadsafe_function); + std.mem.doNotOptimizeAway(&napi_get_threadsafe_function_context); + std.mem.doNotOptimizeAway(&napi_call_threadsafe_function); + std.mem.doNotOptimizeAway(&napi_acquire_threadsafe_function); + std.mem.doNotOptimizeAway(&napi_release_threadsafe_function); + std.mem.doNotOptimizeAway(&napi_unref_threadsafe_function); + std.mem.doNotOptimizeAway(&napi_ref_threadsafe_function); + std.mem.doNotOptimizeAway(&napi_add_async_cleanup_hook); + std.mem.doNotOptimizeAway(&napi_remove_async_cleanup_hook); + std.mem.doNotOptimizeAway(&napi_add_finalizer); + + std.mem.doNotOptimizeAway(&@import("../bun.js/node/buffer.zig").BufferVectorized.fill); +} + +comptime { + if (!JSC.is_bindgen) { + _ = napi_get_undefined; + _ = napi_get_null; + _ = napi_get_boolean; + _ = napi_create_object; + _ = napi_create_array; + _ = napi_create_array_with_length; + _ = napi_create_double; + _ = napi_create_int32; + _ = napi_create_uint32; + _ = napi_create_int64; + _ = napi_create_string_latin1; + _ = napi_create_string_utf8; + _ = napi_create_string_utf16; + _ = napi_create_symbol; + _ = napi_create_error; + _ = napi_typeof; + _ = napi_get_value_double; + _ = napi_get_value_int32; + _ = napi_get_value_uint32; + _ = napi_get_value_int64; + _ = napi_get_value_bool; + _ = napi_get_value_string_latin1; + _ = napi_get_value_string_utf8; + _ = napi_get_value_string_utf16; + _ = napi_coerce_to_bool; + _ = napi_coerce_to_number; + _ = napi_coerce_to_object; + _ = napi_get_prototype; + + _ = napi_set_element; + _ = napi_has_element; + _ = napi_get_element; + _ = napi_is_array; + _ = napi_get_array_length; + _ = napi_strict_equals; + _ = napi_call_function; + _ = napi_new_instance; + _ = napi_instanceof; + _ = napi_open_handle_scope; + _ = napi_close_handle_scope; + _ = napi_is_error; + _ = napi_is_arraybuffer; + _ = napi_create_arraybuffer; + _ = napi_create_external_arraybuffer; + _ = napi_get_arraybuffer_info; + _ = napi_is_typedarray; + _ = napi_create_typedarray; + _ = napi_get_typedarray_info; + _ = napi_is_dataview; + _ = napi_get_dataview_info; + _ = napi_get_version; + _ = napi_create_promise; + _ = napi_resolve_deferred; + _ = napi_reject_deferred; + _ = napi_is_promise; + _ = napi_run_script; + _ = napi_create_date; + _ = napi_is_date; + _ = napi_get_date_value; + _ = napi_create_bigint_int64; + _ = napi_create_bigint_uint64; + _ = napi_get_value_bigint_int64; + _ = napi_get_value_bigint_uint64; + _ = napi_fatal_error; + _ = napi_create_buffer; + _ = napi_create_external_buffer; + _ = napi_create_buffer_copy; + _ = napi_is_buffer; + _ = napi_get_buffer_info; + _ = napi_create_async_work; + _ = napi_delete_async_work; + _ = napi_queue_async_work; + _ = napi_cancel_async_work; + _ = napi_get_node_version; + _ = napi_get_uv_event_loop; + _ = napi_add_env_cleanup_hook; + _ = napi_remove_env_cleanup_hook; + _ = napi_create_threadsafe_function; + _ = napi_get_threadsafe_function_context; + _ = napi_call_threadsafe_function; + _ = napi_acquire_threadsafe_function; + _ = napi_release_threadsafe_function; + _ = napi_unref_threadsafe_function; + _ = napi_ref_threadsafe_function; + _ = napi_add_async_cleanup_hook; + _ = napi_remove_async_cleanup_hook; + _ = @import("../bun.js/node/buffer.zig").BufferVectorized.fill; + _ = napi_add_finalizer; + } +} diff --git a/src/cpp/napi/node_api.h b/src/cpp/napi/node_api.h new file mode 100644 index 00000000..0360ea32 --- /dev/null +++ b/src/cpp/napi/node_api.h @@ -0,0 +1,242 @@ +#ifndef SRC_NODE_API_H_ +#define SRC_NODE_API_H_ + +#ifdef BUILDING_NODE_EXTENSION +#ifdef _WIN32 +// Building native module against node +#define NAPI_EXTERN __declspec(dllimport) +#elif defined(__wasm32__) +#define NAPI_EXTERN __attribute__((__import_module__("napi"))) +#endif +#endif +#include "js_native_api.h" +#include "node_api_types.h" + +struct uv_loop_s; // Forward declaration. + +#ifdef _WIN32 +#define NAPI_MODULE_EXPORT __declspec(dllexport) +#else +#define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) +#endif + +#if defined(__GNUC__) +#define NAPI_NO_RETURN __attribute__((noreturn)) +#elif defined(_WIN32) +#define NAPI_NO_RETURN __declspec(noreturn) +#else +#define NAPI_NO_RETURN +#endif + +typedef napi_value (*napi_addon_register_func)(napi_env env, + napi_value exports); + +typedef struct napi_module { + int nm_version; + unsigned int nm_flags; + const char *nm_filename; + napi_addon_register_func nm_register_func; + const char *nm_modname; + void *nm_priv; + void *reserved[4]; +} napi_module; + +#define NAPI_MODULE_VERSION 1 + +#if defined(_MSC_VER) +#pragma section(".CRT$XCU", read) +#define NAPI_C_CTOR(fn) \ + static void __cdecl fn(void); \ + __declspec(dllexport, allocate(".CRT$XCU")) void(__cdecl * fn##_)(void) = \ + fn; \ + static void __cdecl fn(void) +#else +#define NAPI_C_CTOR(fn) \ + static void fn(void) __attribute__((constructor)); \ + static void fn(void) +#endif + +#define NAPI_MODULE_X(modname, regfunc, priv, flags) \ + EXTERN_C_START \ + static napi_module _module = { \ + NAPI_MODULE_VERSION, flags, __FILE__, regfunc, #modname, priv, {0}, \ + }; \ + NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \ + EXTERN_C_END + +#define NAPI_MODULE_INITIALIZER_X(base, version) \ + NAPI_MODULE_INITIALIZER_X_HELPER(base, version) +#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version + +#ifdef __wasm32__ +#define NAPI_WASM_INITIALIZER \ + NAPI_MODULE_INITIALIZER_X(napi_register_wasm_v, NAPI_MODULE_VERSION) +#define NAPI_MODULE(modname, regfunc) \ + EXTERN_C_START \ + NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \ + napi_value exports) { \ + return regfunc(env, exports); \ + } \ + EXTERN_C_END +#else +#define NAPI_MODULE(modname, regfunc) \ + NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage) +#endif + +#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v + +#define NAPI_MODULE_INITIALIZER \ + NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION) + +#define NAPI_MODULE_INIT() \ + EXTERN_C_START \ + NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \ + napi_value exports); \ + EXTERN_C_END \ + NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \ + napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports) + +EXTERN_C_START + +NAPI_EXTERN void napi_module_register(napi_module *mod); + +NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char *location, + size_t location_len, + const char *message, + size_t message_len); + +// Methods for custom handling of async operations +NAPI_EXTERN napi_status napi_async_init(napi_env env, napi_value async_resource, + napi_value async_resource_name, + napi_async_context *result); + +NAPI_EXTERN napi_status napi_async_destroy(napi_env env, + napi_async_context async_context); + +NAPI_EXTERN napi_status napi_make_callback(napi_env env, + napi_async_context async_context, + napi_value recv, napi_value func, + size_t argc, const napi_value *argv, + napi_value *result); + +// Methods to provide node::Buffer functionality with napi types +NAPI_EXTERN napi_status napi_create_buffer(napi_env env, size_t length, + void **data, napi_value *result); +NAPI_EXTERN napi_status napi_create_external_buffer(napi_env env, size_t length, + void *data, + napi_finalize finalize_cb, + void *finalize_hint, + napi_value *result); +NAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env, size_t length, + const void *data, + void **result_data, + napi_value *result); +NAPI_EXTERN napi_status napi_is_buffer(napi_env env, napi_value value, + bool *result); +NAPI_EXTERN napi_status napi_get_buffer_info(napi_env env, napi_value value, + void **data, size_t *length); + +// Methods to manage simple async operations +NAPI_EXTERN +napi_status napi_create_async_work(napi_env env, napi_value async_resource, + napi_value async_resource_name, + napi_async_execute_callback execute, + napi_async_complete_callback complete, + void *data, napi_async_work *result); +NAPI_EXTERN napi_status napi_delete_async_work(napi_env env, + napi_async_work work); +NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, + napi_async_work work); +NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, + napi_async_work work); + +// version management +NAPI_EXTERN +napi_status napi_get_node_version(napi_env env, + const napi_node_version **version); + +#if NAPI_VERSION >= 2 + +// Return the current libuv event loop for a given environment +NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, + struct uv_loop_s **loop); + +#endif // NAPI_VERSION >= 2 + +#if NAPI_VERSION >= 3 + +NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err); + +NAPI_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env, + void (*fun)(void *arg), + void *arg); + +NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env, + void (*fun)(void *arg), + void *arg); + +NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env, + napi_value resource_object, + napi_async_context context, + napi_callback_scope *result); + +NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env, + napi_callback_scope scope); + +#endif // NAPI_VERSION >= 3 + +#if NAPI_VERSION >= 4 + +#ifndef __wasm32__ +// Calling into JS from other threads +NAPI_EXTERN napi_status napi_create_threadsafe_function( + napi_env env, napi_value func, napi_value async_resource, + napi_value async_resource_name, size_t max_queue_size, + size_t initial_thread_count, void *thread_finalize_data, + napi_finalize thread_finalize_cb, void *context, + napi_threadsafe_function_call_js call_js_cb, + napi_threadsafe_function *result); + +NAPI_EXTERN napi_status napi_get_threadsafe_function_context( + napi_threadsafe_function func, void **result); + +NAPI_EXTERN napi_status +napi_call_threadsafe_function(napi_threadsafe_function func, void *data, + napi_threadsafe_function_call_mode is_blocking); + +NAPI_EXTERN napi_status +napi_acquire_threadsafe_function(napi_threadsafe_function func); + +NAPI_EXTERN napi_status napi_release_threadsafe_function( + napi_threadsafe_function func, napi_threadsafe_function_release_mode mode); + +NAPI_EXTERN napi_status +napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func); + +NAPI_EXTERN napi_status +napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func); +#endif // __wasm32__ + +#endif // NAPI_VERSION >= 4 + +#if NAPI_VERSION >= 8 + +NAPI_EXTERN napi_status napi_add_async_cleanup_hook( + napi_env env, napi_async_cleanup_hook hook, void *arg, + napi_async_cleanup_hook_handle *remove_handle); + +NAPI_EXTERN napi_status +napi_remove_async_cleanup_hook(napi_async_cleanup_hook_handle remove_handle); + +#endif // NAPI_VERSION >= 8 + +#ifdef NAPI_EXPERIMENTAL + +NAPI_EXTERN napi_status node_api_get_module_file_name(napi_env env, + const char **result); + +#endif // NAPI_EXPERIMENTAL + +EXTERN_C_END + +#endif // SRC_NODE_API_H_ diff --git a/src/cpp/napi/node_api_types.h b/src/cpp/napi/node_api_types.h new file mode 100644 index 00000000..a5785a83 --- /dev/null +++ b/src/cpp/napi/node_api_types.h @@ -0,0 +1,47 @@ +#ifndef SRC_NODE_API_TYPES_H_ +#define SRC_NODE_API_TYPES_H_ + +#include "js_native_api_types.h" + +typedef struct napi_callback_scope__ *napi_callback_scope; +typedef struct napi_async_context__ *napi_async_context; +typedef struct napi_async_work__ *napi_async_work; +#if NAPI_VERSION >= 4 +typedef struct napi_threadsafe_function__ *napi_threadsafe_function; +#endif // NAPI_VERSION >= 4 + +#if NAPI_VERSION >= 4 +typedef enum { + napi_tsfn_release, + napi_tsfn_abort +} napi_threadsafe_function_release_mode; + +typedef enum { + napi_tsfn_nonblocking, + napi_tsfn_blocking +} napi_threadsafe_function_call_mode; +#endif // NAPI_VERSION >= 4 + +typedef void (*napi_async_execute_callback)(napi_env env, void *data); +typedef void (*napi_async_complete_callback)(napi_env env, napi_status status, + void *data); +#if NAPI_VERSION >= 4 +typedef void (*napi_threadsafe_function_call_js)(napi_env env, + napi_value js_callback, + void *context, void *data); +#endif // NAPI_VERSION >= 4 + +typedef struct { + uint32_t major; + uint32_t minor; + uint32_t patch; + const char *release; +} napi_node_version; + +#if NAPI_VERSION >= 8 +typedef struct napi_async_cleanup_hook_handle__ *napi_async_cleanup_hook_handle; +typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle, + void *data); +#endif // NAPI_VERSION >= 8 + +#endif // SRC_NODE_API_TYPES_H_ diff --git a/src/ffi/ffi_flashlight.ts b/src/ffi/ffi_flashlight.ts index d643d858..88f9ce54 100644 --- a/src/ffi/ffi_flashlight.ts +++ b/src/ffi/ffi_flashlight.ts @@ -7,6 +7,8 @@ import { ffi_tensor_ops } from './ffi_tensor_ops_gen' // Either that or find some nice way to load that lib in debug mode. const pkgname = `@shumai/${process.platform}_${process.arch}_shumai_flashlight` +const finalizer = require(`${pkgname}/finalizer.node`) + const BINDING_NAME_PREFIX = 'libflashlight_binding' const path = import.meta.resolveSync(`${pkgname}/${BINDING_NAME_PREFIX}.${suffix}`) const { symbols: fl } = (() => { @@ -34,4 +36,4 @@ const { symbols: fl } = (() => { } })() -export { fl } +export { fl, finalizer } diff --git a/src/tensor/tensor.ts b/src/tensor/tensor.ts index 0fa8d179..10f6ae47 100644 --- a/src/tensor/tensor.ts +++ b/src/tensor/tensor.ts @@ -1,5 +1,5 @@ import { ptr, toArrayBuffer, FFIType } from 'bun:ffi' -import { fl } from '../ffi/ffi_flashlight' +import { fl, finalizer } from '../ffi/ffi_flashlight' import { arrayArg } from '../ffi/ffi_bind_utils' import { TensorInterface } from './tensor_interface' import { TensorOpsInterface } from './tensor_ops_interface_gen' @@ -122,7 +122,7 @@ function backward(base_t: Tensor, jacobian: Tensor) { } class Tensor { - underlying: ArrayBuffer + underlying = null deps: Array = [] requires_grad = false grad: Tensor = null @@ -133,7 +133,12 @@ class Tensor { const numel = Number(fl.elements.native(_ptr)) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - overload toArrayBuffer params - this.underlying = toArrayBuffer(_ptr, 0, numel * 4, fl.genTensorDestroyer.native()) + this._ptr = _ptr + //{ + this.underlying = {} + finalizer.finalize(this.underlying, _ptr) + //} + //this.underlying = toArrayBuffer(_ptr, 0, numel * 4, fl.genTensorDestroyer.native()) } backward(jacobian) { @@ -163,7 +168,8 @@ class Tensor { } get ptr() { - return ptr(this.underlying) + //return ptr(this.underlying) + return this._ptr } get ndim() {