{"name":"napi-macros","version":"2.1.1","description":"Set of utility macros to make writing N-API modules a little easier.","main":"index.js","dependencies":{},"devDependencies":{},"repository":{"type":"git","url":"git+https://github.com/mafintosh/napi-macros.git"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","bugs":{"url":"https://github.com/mafintosh/napi-macros/issues"},"homepage":"https://github.com/mafintosh/napi-macros","gitHead":"9f7e9119201b8adffb5cdd947c685c7325aeba84","_id":"napi-macros@2.1.1","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"integrity":"sha512-Q5w6SrLzogp1bg1wn2WlFCrRLn2eckE20aNk2J8fK5i9+I6P7tc3Z4rkDAZ+abRzdbFDOemCTgoopSAkRfsa4g==","shasum":"2405c71a375ce1325ee5fb37e767dddc71c3cf9d","tarball":"http://registry-npm.ywwl.com/napi-macros/download/napi-macros-2.1.1.tgz","fileCount":9,"unpackedSize":16554,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAZsJ/s6bBcbxo+oaHoXY1tSdgdW8jcisKumNWI5G01fAiAC0xto1Pa9wJsQSLQXwUrvnyxA5mTPfdP1m+WOYFgwQw=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjmZh4ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqY7Q/8DedPYbNLDyN675H+rR/r/sO2wQ32wjUNrIj34YCQVL+JLP9K\r\ntMlTq+Mxv4uE8uSFh559WeHLfTX70O9E3dbYmX3m4lDZoHDlQOMt7Ij/lSmS\r\nLWXXCRIN0TJInRpzKwAh69B6JNrmQ1ojMC0wDNozkWc0WyExJ347PHh5j99s\r\n3LaBFqA7wLxKwgAxBxNVrntLr9SLvP3EORU2NgllJvzBWtU7OAd0I39ugQL8\r\nKP+0gaSCnYvDwkVMMpMaCQqrUUVWOBlc+VHP9q1yWHvGe8vDr/+ycq2pLKDg\r\nDzlAHbXq1E3SkgjPr5td+mYWdFyStmqVK/DYygPwjjxIHPhl/cHfGOJi5JP1\r\nQVWFoAHO4ErRMmv2Tm3qAzvhBhiD7LHCONGp/ple/RdO50XQjvdSFAcgoysi\r\nGWVjSmuDlf1rO9+Y+4ueMGSdrXUSLsWKccPUkDXN8ATrGRGe8wLmnNEYJqOG\r\nCHyYkN0uzFGA0GQ5icp8n6BrMUVg4t7ET2hXjJNlYNh0km7ZTnTCckmadDNc\r\nrzGG9Pmf6DiNSokMeCCeqnNXPatjhGtCCTGMYY2Xu/7mwxtmXyhFVF+lkluc\r\nwz4rA+m/3CQop/n9jvMtAUp8WFX8o0Po96CrOwes0ECXWboafZZa7b7WGuJk\r\nYrg8HBN1dou3R3kQQfNGMQTDMwyZSoIf75k=\r\n=DMXs\r\n-----END PGP SIGNATURE-----\r\n","size":4698,"noattachment":false,"key":"/napi-macros/-/napi-macros-2.1.1.tgz"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"ralphtheninja","email":"ralphtheninja@riseup.net"},{"name":"emilbayes","email":"github@tixz.dk"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-macros_2.1.1_1671010423967_0.6841343592936155"},"_hasShrinkwrap":false,"readme":"# napi-macros\n\nSet of utility macros to make writing [N-API](https://nodejs.org/dist/latest-v9.x/docs/api/n-api.html) modules a little easier.\n\n```\nnpm install napi-macros\n```\n\nThen add the following to your target in your binding.gyp file\n\n```\n\"include_dirs\": [\n  \"<!(node -e \\\"require('napi-macros')\\\")\"\n],\n```\n\nThese patterns mostly reflect how I use N-API so far. Feel free\nto PR more.\n\n## Usage\n\n``` c\n#include <node_api.h>\n#include <napi-macros.h>\n\nNAPI_METHOD(times_two) {\n  NAPI_ARGV(1)\n  NAPI_ARGV_INT32(number, 0)\n\n  number *= 2;\n\n  NAPI_RETURN_INT32(number)\n}\n\nNAPI_INIT() {\n  NAPI_EXPORT_FUNCTION(times_two)\n}\n```\n\nFull working example can be found in the [example/](https://github.com/mafintosh/napi-macros/tree/master/example) folder.\n\n## API\n\n#### `NAPI_INIT()`\n\nSetup init boilerplate. Pass the function body after.\n\n``` c\nstatic char *my_string = \"hello\";\n\nNAPI_INIT() {\n  EXPORT_STRING(my_string)\n}\n```\n\n#### `NAPI_METHOD(name)`\n\nSetup method boilerplate. Pass the function body after.\n\n``` c\nNAPI_METHOD(add) {\n  NAPI_ARGV(2)\n  NAPI_ARGV_INT32(a, 0)\n  NAPI_ARGV_INT32(b, 1)\n\n  a = a + b\n\n  NAPI_RETURN_INT32(a)\n}\n```\n\n#### `NAPI_ARGV(n)`\n\nSetup up argv boilerplate. `n` is how many arguments you are expecting.\nExpects the `napi_env` to be in scope as `env` and the `napi_callback_info` to be in scope as `info`.\n\n#### `NAPI_ARGV_BUFFER(name, index)`\n\nGet a buffer out of the arguments at the corresponding index.\nSets `char *name` and `size_t name_len` with the buffer and buffer length.\n\n#### `NAPI_ARGV_BUFFER_CAST(type, name, index)`\n\nGet a buffer out and cast the pointer to the specified type.\nNote that the type should include the pointer star, i.e.\n\n``` c\nNAPI_ARGV_BUFFER_CAST(uv_udp_t *, handle, 0)\n```\n\nWill cast the 1st argument as `uv_udp_t` pointer.\n\n#### `NAPI_ARGV_UINT32(name, index)`\n\nGet an argument as a uint32.\nWill throw if argument is not the right type.\n\n#### `NAPI_ARGV_INT32(name, index)`\n\nGet an argument as an int32.\nWill throw if argument is not the right type.\n\n#### `NAPI_ARGV_UTF8(name, length, index)`\n\nGet an argument as a utf8 string.\n\n`name` will be a `char[length]` array.\n\nWill throw if argument is not the right type.\n\n#### `NAPI_ARGV_UTF8_MALLOC(name, index)`\n\nGet an argument as a utf8 string.\n\n`name` will be a `char*`.\n\nLike `NAPI_ARGV_UTF8()` but allocates `name` on the heap using `malloc()`, which should be `free()`'d after usage.\n\n#### `NAPI_BUFFER_CAST(type, name, var)`\n\nSame as `NAPI_ARGV_BUFFER_CAST` but takes a generic `napi_value` variable instead of an argv index.\n\n#### `NAPI_BUFFER(name, var)`\n\nSame as `NAPI_ARGV_BUFFER` but takes a generic `napi_value` variable instead of an argv index.\n\n#### `NAPI_UTF8(name, size, var)`\n\nSame as `NAPI_ARGV_UTF8` but takes a generic `napi_value` variable instead of an argv index.\n\n#### `NAPI_UTF8_MALLOC(name, var)`\n\nSame as `NAPI_ARGV_UTF8_MALLOC` but takes a generic `napi_value` variable instead of an argv index.\n\n#### `NAPI_UINT32(name, var)`\n\nSame as `NAPI_ARGV_UINT32` but takes a generic `napi_value` variable instead of an argv index.\n\n#### `NAPI_INT32(name, var)`\n\nSame as `NAPI_ARGV_INT32` but takes a generic `napi_value` variable instead of an argv index.\n\n#### `NAPI_EXPORT_FUNCTION(fn)`\n\nWill export a function in the Init method. Expects the env and `exports` to be in scope.\nThe name of the exported function is the same name as the c function.\n\n#### `NAPI_EXPORT_SIZEOF(struct)`\n\nExport the size of a strict. The exported name is `sizeof_{struct-name}`.\n\n#### `NAPI_EXPORT_UINT32(name)`\n\nExport a uint32.\nThe name of the exported number is the same name as the c variable.\n\n#### `NAPI_EXPORT_INT32(name)`\n\nExport an int32.\nThe name of the exported number is the same name as the c variable.\n\n#### `NAPI_EXPORT_UTF8(name, len)`\n\nExport a utf8 string. `len` should be the length of the string.\nThe name of the exported string is the same name as the c variable.\n\n#### `NAPI_EXPORT_STRING(name)`\n\nExport a null terminated string.\nThe name of the exported string is the same name as the c variable.\n\n#### `NAPI_EXPORT_SIZEOF(type)`\n\nExports `sizeof(type)`.\nThe name of the exported number is the same name as the c variable.\n\n#### `NAPI_EXPORT_SIZEOF_STRUCT(structName)`\n\nExports `sizeof(struct structName)`.\nThe name of the exported number is the same name as the c variable.\n\n#### `NAPI_EXPORT_ALIGNMENTOF(type)`\n\nExports the byte alignment of `type`.\nThe name of the exported number is the same name as the c variable.\n\n#### `NAPI_EXPORT_ALIGNMENTOF_STRUCT(structName)`\n\nExports the byte alignment of `struct structName`.\nThe name of the exported number is the same name as the c variable.\n\n#### `NAPI_EXPORT_OFFSETOF(type, name)`\n\nExports the byte offset of `name` within `type`.\nThe name of the exported number is the same name as the c variables.\n\n#### `NAPI_EXPORT_OFFSETOF_STRUCT(structName, name)`\n\nExports the byte offset of `name` within `struct structName`.\nThe name of the exported number is the same name as the c variables.\n\n#### `NAPI_FOR_EACH(array, element)`\n\nIterate over an array. `array` should be a `napi_value` containing a javascript array\nand `element` is the variable name an element will be exposed as. Expects the loop body\nto be passed after.\n\n``` c\nnapi_value buffers = argv[0] // first argument is a js array\nNAPI_FOR_EACH(buffers, buffer) {\n  NAPI_BUFFER(cbuf, buffer)\n  printf(\"cbuf is now a char * pointer: %s\\n\", cbuf);\n}\n```\n\n#### `NAPI_RETURN_UINT32(name)`\n\nReturns a uint32.\n\n#### `NAPI_RETURN_INT32(name)`\n\nReturns an int32.\n\n#### `NAPI_RETURN_UTF8(name, len)`\n\nReturn a utf8 string. `len` should be the length of the string.\n\n#### `NAPI_RETURN_STRING(name)`\n\nReturn a null terminated string.\n\n#### `NAPI_STATUS_THROWS(call)`\n\nChecks the return status of any `napi_*` function returning a `napi_status` type. This simplifies using a `napi_status` variable and comparing the result with `napi_ok`. It's used internally but can be used stand alone as well.\n\n```c\nNAPI_STATUS_THROWS(\n  napi_create_threadsafe_function(\n    NULL,\n    callback,\n    0,\n    async_resource_name,\n    0,\n    3,\n    0,\n    my_finalize,\n    NULL,\n    my_callback,\n    &threadsafe_function\n  )\n);\n```\n\nAbove example will fail because the first `env` parameter is `NULL` and throw the following error:\n\n```\nError: napi_create_threadsafe_function(NULL, callback, 0, async_resource_name, 0, 3, 0, my_finalize, \\\nNULL, my_callback, &threadsafe_function) failed!\n```\n\n#### `NAPI_UV_THROWS(err, fn)`\n\nChecks if a libuv call fails and if so, throws an error.\n\n``` c\nint err;\nNAPI_UV_THROWS(err, uv_ip4_addr((char *) &ip, port, &addr))\n```\n\n#### `NAPI_MAKE_CALLBACK(env, async_ctx, ctx, func, argc, argv, result)`\n\nSame as `napi_make_callback` except it checks if the JS function throw an exception\nand triggers a `process.on('uncaughtException')` if so.\n\n## License\n\nMIT\n","publish_time":1671010424157,"_cnpm_publish_time":1671010424157,"dist-tags":{"latest":"2.2.2"}}