{"name":"napi-macros","version":"2.2.0","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":"b507455c5e3bfec5d08baeec05565f11a5fd5422","_id":"napi-macros@2.2.0","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"integrity":"sha512-AcSjcwwZyBtRMOeseW/o3Q9x7x21+yFjn4FGQv2UJVISfgID4ZbyBmkyE9a0n76/f6CDC3W8J9It6V1Ck6LC1Q==","shasum":"5bc4eeb1f5542738b84509e1f763d664f79639b6","tarball":"http://registry-npm.ywwl.com/napi-macros/download/napi-macros-2.2.0.tgz","fileCount":9,"unpackedSize":17325,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCha4+tQSBSigFXUeXbmjsaJDQC2QCXAjSH0W3a5oWetwIgewG88SNDn4X0QVhAxQ4DqjLEkyCEFHcwDoSLG6NCbYo="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjq2EWACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpaXQ/8CKoC48QvVQzbHArNgHmGExEFT+fMHzLupXnXfsDQaruXZrRc\r\n3kHq9jE4egmLQWYsBCiQnOOh5pzAK48T8Oc9J1JZ2KDavQXa8SZFaqd0o98H\r\n7j5x7LYsNr+3NDG/go3/y8MAea67naNWNjoYCC5ea0KHdd6VxTnUvpc9NxY6\r\nKvznQaq0Xm0KkVfFjaTo3e68Qtn+RxPizRqpobmL2xIgu36VJsVg4cTnZ4qz\r\n8TFWUgD/+YjhLe+PHxgO5hLgx5j8nrv+1o1pGRfUIEoRrqe1wCvM8WqJljWB\r\n9PFlY2ppyH+zMlNLCl0uR8x4iAonCgPaPMHjacva84Hb5Gmu5LzaBQZUvEMG\r\nOzO+x8o1fnm6adAEGKAZeH2Ta0ixo5A1Uq/Q4vcrcYjUL09xapGhso1x35Zl\r\nw8QTg6yk53OW2i1pU+uh6M7Rl+vXDM0kIhNgwZQc/G1VUdcZktib9MUBM72p\r\n1kPMqQwYHzUTRbXXYxbMnnkCc+L0erWi9LoZjZWpWKslPywVQdRgUK6sTAeH\r\nE0kIe4M6wub7F8AuCJ3WMXfQ8wYLGp+1bkaO/XyI6DeziVPe0OS5WRTuo0gs\r\nWNsV9C0B1Qy6JD0/JW2xILT7OwLD8b5wO5UiUFTwxJItPBALYrxr82O3P1wR\r\nn+lU4U7irHo444Q0A7HK+nkEtM3zvKQZVDw=\r\n=4K8Q\r\n-----END PGP SIGNATURE-----\r\n","size":4885,"noattachment":false,"key":"/napi-macros/-/napi-macros-2.2.0.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.0_1672175894703_0.4688112019496573"},"_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":1672175894859,"_cnpm_publish_time":1672175894859,"dist-tags":{"latest":"2.2.2"}}