summaryrefslogtreecommitdiffhomepage
path: root/src/nodejs/unit-http/nxt_napi.h
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2019-08-20 16:32:05 +0300
committerMax Romanov <max.romanov@nginx.com>2019-08-20 16:32:05 +0300
commite291841b3379f8787a10ad4f91e4aeae2ae323a4 (patch)
tree49a2f4629e5b8d6cd48f7436d7eeba4c99905675 /src/nodejs/unit-http/nxt_napi.h
parente501c74ddceab86e48c031ca9b5e154f52dcdae0 (diff)
downloadunit-e291841b3379f8787a10ad4f91e4aeae2ae323a4.tar.gz
unit-e291841b3379f8787a10ad4f91e4aeae2ae323a4.tar.bz2
Node.js: introducing websocket support.
Diffstat (limited to 'src/nodejs/unit-http/nxt_napi.h')
-rw-r--r--src/nodejs/unit-http/nxt_napi.h195
1 files changed, 189 insertions, 6 deletions
diff --git a/src/nodejs/unit-http/nxt_napi.h b/src/nodejs/unit-http/nxt_napi.h
index 9bcf3a21..d9721a40 100644
--- a/src/nodejs/unit-http/nxt_napi.h
+++ b/src/nodejs/unit-http/nxt_napi.h
@@ -188,6 +188,21 @@ struct nxt_napi {
}
+ inline void *
+ get_buffer_info(napi_value val, size_t &size)
+ {
+ void *res;
+ napi_status status;
+
+ status = napi_get_buffer_info(env_, val, &res, &size);
+ if (status != napi_ok) {
+ throw exception("Failed to get buffer info");
+ }
+
+ return res;
+ }
+
+
inline napi_value
get_cb_info(napi_callback_info info, size_t &argc, napi_value *argv)
{
@@ -219,6 +234,23 @@ struct nxt_napi {
inline napi_value
+ get_cb_info(napi_callback_info info, napi_value &arg)
+ {
+ size_t argc;
+ napi_value res;
+
+ argc = 1;
+ res = get_cb_info(info, argc, &arg);
+
+ if (argc != 1) {
+ throw exception("Wrong args count. Expected 1");
+ }
+
+ return res;
+ }
+
+
+ inline napi_value
get_element(napi_value obj, uint32_t i)
{
napi_value res;
@@ -311,15 +343,22 @@ struct nxt_napi {
inline nxt_unit_request_info_t *
get_request_info(napi_value obj)
{
- int64_t n;
+ return (nxt_unit_request_info_t *) unwrap(obj);
+ }
+
+
+ inline uint32_t
+ get_value_bool(napi_value obj)
+ {
+ bool res;
napi_status status;
- status = napi_get_value_int64(env_, obj, &n);
+ status = napi_get_value_bool(env_, obj, &res);
if (status != napi_ok) {
- throw exception("Failed to get request pointer");
+ throw exception("Failed to get bool");
}
- return (nxt_unit_request_info_t *) (intptr_t) n;
+ return res;
}
@@ -353,6 +392,21 @@ struct nxt_napi {
}
+ inline size_t
+ get_value_string_utf8(napi_value val, char *buf, size_t bufsize)
+ {
+ size_t res;
+ napi_status status;
+
+ status = napi_get_value_string_utf8(env_, val, buf, bufsize, &res);
+ if (status != napi_ok) {
+ throw exception("Failed to get string utf8");
+ }
+
+ return res;
+ }
+
+
inline bool
is_array(napi_value val)
{
@@ -368,6 +422,21 @@ struct nxt_napi {
}
+ inline bool
+ is_buffer(napi_value val)
+ {
+ bool res;
+ napi_status status;
+
+ status = napi_is_buffer(env_, val, &res);
+ if (status != napi_ok) {
+ throw exception("Failed to confirm value is buffer");
+ }
+
+ return res;
+ }
+
+
inline napi_value
make_callback(napi_async_context ctx, napi_value val, napi_value func,
int argc, const napi_value *argv)
@@ -398,6 +467,41 @@ struct nxt_napi {
inline napi_value
+ make_callback(napi_async_context ctx, napi_value val, napi_value func)
+ {
+ return make_callback(ctx, val, func, 0, NULL);
+ }
+
+
+ inline napi_value
+ make_callback(napi_async_context ctx, napi_value val, napi_value func,
+ napi_value arg1)
+ {
+ return make_callback(ctx, val, func, 1, &arg1);
+ }
+
+
+ inline napi_value
+ make_callback(napi_async_context ctx, napi_value val, napi_value func,
+ napi_value arg1, napi_value arg2)
+ {
+ napi_value args[2] = { arg1, arg2 };
+
+ return make_callback(ctx, val, func, 2, args);
+ }
+
+
+ inline napi_value
+ make_callback(napi_async_context ctx, napi_value val, napi_value func,
+ napi_value arg1, napi_value arg2, napi_value arg3)
+ {
+ napi_value args[3] = { arg1, arg2, arg3 };
+
+ return make_callback(ctx, val, func, 3, args);
+ }
+
+
+ inline napi_value
new_instance(napi_value ctor)
{
napi_value res;
@@ -427,6 +531,22 @@ struct nxt_napi {
}
+ inline napi_value
+ new_instance(napi_value ctor, napi_value param1, napi_value param2)
+ {
+ napi_value res;
+ napi_status status;
+ napi_value param[2] = { param1, param2 };
+
+ status = napi_new_instance(env_, ctor, 2, param, &res);
+ if (status != napi_ok) {
+ throw exception("Failed to create instance");
+ }
+
+ return res;
+ }
+
+
inline void
set_element(napi_value obj, uint32_t i, napi_value val)
{
@@ -472,8 +592,46 @@ struct nxt_napi {
}
+ template<typename T>
inline void
- set_named_property(napi_value obj, const char *name, intptr_t val)
+ set_named_property(napi_value obj, const char *name, T val)
+ {
+ set_named_property(obj, name, create(val));
+ }
+
+
+ inline napi_value
+ create(int32_t val)
+ {
+ napi_value ptr;
+ napi_status status;
+
+ status = napi_create_int32(env_, val, &ptr);
+ if (status != napi_ok) {
+ throw exception("Failed to create int32");
+ }
+
+ return ptr;
+ }
+
+
+ inline napi_value
+ create(uint32_t val)
+ {
+ napi_value ptr;
+ napi_status status;
+
+ status = napi_create_uint32(env_, val, &ptr);
+ if (status != napi_ok) {
+ throw exception("Failed to create uint32");
+ }
+
+ return ptr;
+ }
+
+
+ inline napi_value
+ create(int64_t val)
{
napi_value ptr;
napi_status status;
@@ -483,7 +641,32 @@ struct nxt_napi {
throw exception("Failed to create int64");
}
- set_named_property(obj, name, ptr);
+ return ptr;
+ }
+
+
+ inline void
+ remove_wrap(napi_ref& ref)
+ {
+ if (ref != nullptr) {
+ remove_wrap(get_reference_value(ref));
+ ref = nullptr;
+ }
+ }
+
+
+ inline void *
+ remove_wrap(napi_value val)
+ {
+ void *res;
+ napi_status status;
+
+ status = napi_remove_wrap(env_, val, &res);
+ if (status != napi_ok) {
+ throw exception("Failed to remove_wrap");
+ }
+
+ return res;
}