{"name":"napi-macros","version":"2.2.2","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":"2b803162101cf1932c937512a9597a1a510f5e17","_id":"napi-macros@2.2.2","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"integrity":"sha512-hmEVtAGYzVQpCKdbQea4skABsdXW4RUh5t5mJ2zzqowJS2OyXZTU1KhDVFhx+NlWZ4ap9mqR9TcDO3LTTttd+g==","shasum":"817fef20c3e0e40a963fbf7b37d1600bd0201044","tarball":"http://registry-npm.ywwl.com/napi-macros/download/napi-macros-2.2.2.tgz","fileCount":5,"unpackedSize":16797,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDF4qUyih6M0u0aLD0RywY4UIyf1CZq1ABrRpJPpTcFkwIgDZ1NZmL3kmO5mIawhCrj0lA+lsE7BH2T6yA8Vh4UgQI="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjq2OwACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqiqBAAnCud+U4bbmAf3TM8Z8ygay92f13wMwdO3nWNwGK3e4A/dpG9\r\nQiT82M4/NeDsK9AvIQ0M1wpBP3vbbtqA9Jf80ojSgy2+/mOjteyYlBe8ukDG\r\nsqBg4ps0+XSwSt1xpIk6TrZACVDJbscgazhXGssH1xSDHazkjrCGj6vWN7JU\r\naqgO6vLUv/NLNvRZ1PIhfg9K/LVEgltWtex+RZV+fnZpLJBnx3SdD/8I2kXr\r\n3nnSsxjpbbMUt8UwDx1Yq23QaznR4lKQ/WmKLEbtU32lWTP359f57QD7oiQZ\r\nOiFYozhp+yfBPZhJ0R1SnkTTQ2QmnEfqZQVgtd3JmGw8p6bArv0Rgb/UUKrV\r\nZVUN2iQO0Sc+8L6Wc63WumT1CjZbDNuY2WSVhGvI94C+M7FGXRGHh+cHXlHQ\r\ntraNrTQrZ4ghgm4fUrL68q3m4YoeJgQOWu3MaxPLvdl5p1wrElaBgeXjJHPj\r\nAPEOpVSQ1+e1lE/CMvGxIA5X0QcshB38OD/DEIPFd4c54SZPixxed6W9H6Wl\r\nJAWK64LYhMXH81OyohlSr82MwWYZiJxZ1dZU/IoCzhkBoeaqRjNWKwEseerq\r\npH5QhlsSREyiS7iXAJn3/XbandK1u9ADh2HQYUiDpc6aG+kXm9+DmJxUybDa\r\nKdxhxPl9TZ5XbYx8YD8mpkJ8RDtutfgln9Y=\r\n=hD+8\r\n-----END PGP SIGNATURE-----\r\n","size":4649,"noattachment":false,"key":"/napi-macros/-/napi-macros-2.2.2.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.2.2_1672176560088_0.9081449138521442"},"_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":1672176560282,"_cnpm_publish_time":1672176560282,"dist-tags":{"latest":"2.2.2"}}