diff options
author | EuAndreh <eu@euandre.org> | 2023-11-15 10:50:30 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2023-11-15 16:16:30 -0300 |
commit | af5e7495f3993c52573432cadde12169d5c05e62 (patch) | |
tree | 002fc20e6f12eac71c21c6eccc48bf59e7180274 /src/napi-sqlite.c | |
parent | src/napi-sqlite.c: Add Node-API PoC (diff) | |
download | papod-af5e7495f3993c52573432cadde12169d5c05e62.tar.gz papod-af5e7495f3993c52573432cadde12169d5c05e62.tar.xz |
Add support for multi-file C project
- have dynamic discovered dependencies via `mkdeps.hs`, and also move
the listing of JavaScript files to it.
- copy over stub C files for setting up the project skeleton.
Diffstat (limited to 'src/napi-sqlite.c')
-rw-r--r-- | src/napi-sqlite.c | 103 |
1 files changed, 81 insertions, 22 deletions
diff --git a/src/napi-sqlite.c b/src/napi-sqlite.c index 7c0b872..df3b042 100644 --- a/src/napi-sqlite.c +++ b/src/napi-sqlite.c @@ -1,7 +1,18 @@ +#include <stdio.h> + #include <node/node_api.h> +/* +FIXME +static const napi_type_tag SQLITE_DB_TYPE_TAG = { + 0x0e9614d459f746cc, 0x88b814a5dc5c4cf7 +}; +*/ + static napi_value -my_function(napi_env env, napi_callback_info info) { +myfn(napi_env env, napi_callback_info info) { + napi_value ret = NULL; + napi_status status; size_t argc = 1; int number; @@ -11,51 +22,99 @@ my_function(napi_env env, napi_callback_info info) { status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); if (status != napi_ok) { napi_throw_error(env, NULL, "Failed to parse arguments FIXME i18n"); - // FIXME: does execution somehow halt here? Or is it missing a - // return? + goto out; } status = napi_get_value_int32(env, argv[0], &number); if (status != napi_ok) { napi_throw_error(env, NULL, "Invalid number was passed as argument FIXME i18n"); - // FIXME: return? + goto out; } number = number * 2; status = napi_create_int32(env, number, &my_number); if (status != napi_ok) { napi_throw_error(env, NULL, "Unable to create return value FIXME i18n"); - // FIXME: return? + goto out; } + ret = my_number; + +out: + return ret; +} - return my_number; +static napi_value +open(napi_env env, napi_callback_info info) { + (void)env; + (void)info; + return NULL; } static napi_value +close(napi_env env, napi_callback_info info) { + (void)env; + (void)info; + return NULL; +} + +static const struct { + const char *label; + napi_value(*const handle)(napi_env env, napi_callback_info info); +} fns[] = { + { .label = "myfn", .handle = myfn, }, + { .label = "open", .handle = open, }, + { .label = "close", .handle = close, }, + { NULL, NULL }, +}; + +static napi_value init(napi_env env, napi_value exports) { + napi_value ret = exports; + napi_status status; - napi_value fn; - status = napi_create_function(env, NULL, 0, my_function, NULL, &fn); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Unable to wrap native function FIXME i18n"); - // FIXME: return? - } + for (size_t i = 0; fns[i].label && fns[i].handle; i++) { + napi_value fn; + status = napi_create_function( + env, + fns[i].label, + NAPI_AUTO_LENGTH, + fns[i].handle, + "xucrutes", + &fn + ); + if (status != napi_ok) { + ret = NULL; + napi_throw_error( + env, + "SQLITE_FN_CREATE", + "Unable to wrap native function FIXME i18n" + ); + goto out; + } - status = napi_set_named_property(env, exports, "my_function", fn); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Unable to populate exports FIXME i18n"); - // FIXME: return? + status = napi_set_named_property( + env, + exports, + fns[i].label, + fn + ); + if (status != napi_ok) { + ret = NULL; + napi_throw_error( + env, + "SQLITE_FN_SETNAME", + "Unable to populate exports FIXME i18n" + ); + goto out; + } } - return exports; +out: + return ret; } -napi_value -sqlite_napi_init(napi_env env, napi_value exports) { +NAPI_MODULE_INIT() { return init(env, exports); } - -//NAPI_MODULE(NODE_GYP_MODULE_NAME, sqlite_napi_init) -NAPI_MODULE(FIXME_CAN_THIS_BE_ANYTHING, sqlite_napi_init) |