{"name":"napi-macros","version":"1.4.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":"47afdadedb13d860eff588735bc8c909994cec81","_id":"napi-macros@1.4.1","_npmVersion":"6.1.0","_nodeVersion":"10.7.0","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"integrity":"sha512-vr+YZ/g+6rYpHxj7mSCiJivd/jusCTVs8kgVlSFrZxBkLkeqBeC1ut0QG7rkQHRcW2ijUGgOUNLZ32IIQkwkCQ==","shasum":"cba103bda95e09f5c5b0e43bdba3d72dd65b19c4","tarball":"http://registry-npm.ywwl.com/napi-macros/download/napi-macros-1.4.1.tgz","fileCount":9,"unpackedSize":11971,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbVh9TCRA9TVsSAnZWagAA5g8P/3qXMKU2h9XSdIUzDXhQ\nznPPIUuyXwzl19SrHESWN2Ifj+lGSKN754yGapjDC/cK+EeI+u5ZuHjpYPmC\nq6mdSyt0geYSQNE98CwgoFx8y6ovetYPYUSzyCN6HOahD/UUwSTsTW2AN+Lh\nf4neToEHkSPiZOumUcetjYdN6rxyiGmTRr42yF/jppJZMUwXhKYV/OaT118s\n9hsnPjWUBapTvy+HP2KnLfVSFgHMNB9Kd35+c6A7tKQUO8fyi/nSPPMHNvPO\nk0URp6F8P5eHHALsfD89XT6yIX15rUYi2Rp9LWGvTfBkNEcmcr65uP92/kUV\nHUBH83MVrtMmk+92YEILHevtiPOlED0MARiQRdKdIZZpoJDHxsMg/SBhU0VI\nakqtk1ZPlWZqAjz0BwZjROcDiKncKQfCF8vuMfNOvwrczF4qEreN0pMjecwX\nHQaoWE/WiA3czJhcdkPeHcafPp6fdVHvp8BAfNHysBy2jCe7DDTM0IiQhgUa\nhAZ0Qj9zdyXU/1GG53g7jO3OKQ5XSaRUDRjczrB2uuDardNMVvmtWsCyeOsV\n4eKsSMc5Emz7KzNGNjrAS3/ejaYUDgDfqoMrz+OdcmRU4nvAKYm4bsvCyp8H\nMReoM8HlBI2zrmMv4/67AN3eVF0tKCkeff8Q9fG6oX+3k+jJL4M2K3hvDfFM\nEhpr\r\n=biEI\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIG2XLhZuYy/DNUAAYHRe2cQCH+oFX14/5GNJB8mHpdRwAiEAnwRrZzlK/8+Zfozw5zGGN/gCAGO81nDsmL1gQ3pv14U="}],"size":3799,"noattachment":false,"key":"/napi-macros/-/napi-macros-1.4.1.tgz"},"maintainers":[{"email":"github@tixz.dk","name":"emilbayes"},{"email":"mathiasbuus@gmail.com","name":"mafintosh"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-macros_1.4.1_1532370771127_0.5435785227594478"},"_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":1532370771259,"_cnpm_publish_time":1532370771259,"dist-tags":{"latest":"2.2.2"}}