Deleted Added
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6#ifndef _NXT_NODEJS_NAPI_H_INCLUDED_
7#define _NXT_NODEJS_NAPI_H_INCLUDED_
8

--- 174 unchanged lines hidden (view full) ---

183 if (status != napi_ok) {
184 throw exception("Failed to get array length");
185 }
186
187 return res;
188 }
189
190
191 inline void *
192 get_buffer_info(napi_value val, size_t &size)
193 {
194 void *res;
195 napi_status status;
196
197 status = napi_get_buffer_info(env_, val, &res, &size);
198 if (status != napi_ok) {
199 throw exception("Failed to get buffer info");
200 }
201
202 return res;
203 }
204
205
206 inline napi_value
207 get_cb_info(napi_callback_info info, size_t &argc, napi_value *argv)
208 {
209 napi_value res;
210 napi_status status;
211
212 status = napi_get_cb_info(env_, info, &argc, argv, &res, nullptr);
213 if (status != napi_ok) {

--- 15 unchanged lines hidden (view full) ---

229 throw exception("Failed to get arguments from js");
230 }
231
232 return res;
233 }
234
235
236 inline napi_value
237 get_cb_info(napi_callback_info info, napi_value &arg)
238 {
239 size_t argc;
240 napi_value res;
241
242 argc = 1;
243 res = get_cb_info(info, argc, &arg);
244
245 if (argc != 1) {
246 throw exception("Wrong args count. Expected 1");
247 }
248
249 return res;
250 }
251
252
253 inline napi_value
254 get_element(napi_value obj, uint32_t i)
255 {
256 napi_value res;
257 napi_status status;
258
259 status = napi_get_element(env_, obj, i, &res);
260 if (status != napi_ok) {
261 throw exception("Failed to get element");

--- 76 unchanged lines hidden (view full) ---

338
339 return res;
340 }
341
342
343 inline nxt_unit_request_info_t *
344 get_request_info(napi_value obj)
345 {
346 return (nxt_unit_request_info_t *) unwrap(obj);
347 }
348
349
350 inline uint32_t
351 get_value_bool(napi_value obj)
352 {
353 bool res;
354 napi_status status;
355
356 status = napi_get_value_bool(env_, obj, &res);
357 if (status != napi_ok) {
358 throw exception("Failed to get bool");
359 }
360
361 return res;
362 }
363
364
365 inline size_t
366 get_value_string_latin1(napi_value val, char *buf, size_t bufsize)
367 {
368 size_t res;
369 napi_status status;

--- 17 unchanged lines hidden (view full) ---

387 if (status != napi_ok) {
388 throw exception("Failed to get uint32_t");
389 }
390
391 return res;
392 }
393
394
395 inline size_t
396 get_value_string_utf8(napi_value val, char *buf, size_t bufsize)
397 {
398 size_t res;
399 napi_status status;
400
401 status = napi_get_value_string_utf8(env_, val, buf, bufsize, &res);
402 if (status != napi_ok) {
403 throw exception("Failed to get string utf8");
404 }
405
406 return res;
407 }
408
409
410 inline bool
411 is_array(napi_value val)
412 {
413 bool res;
414 napi_status status;
415
416 status = napi_is_array(env_, val, &res);
417 if (status != napi_ok) {
418 throw exception("Failed to confirm value is array");
419 }
420
421 return res;
422 }
423
424
425 inline bool
426 is_buffer(napi_value val)
427 {
428 bool res;
429 napi_status status;
430
431 status = napi_is_buffer(env_, val, &res);
432 if (status != napi_ok) {
433 throw exception("Failed to confirm value is buffer");
434 }
435
436 return res;
437 }
438
439
440 inline napi_value
441 make_callback(napi_async_context ctx, napi_value val, napi_value func,
442 int argc, const napi_value *argv)
443 {
444 napi_value res, ex;
445 napi_status status;
446
447 status = napi_make_callback(env_, ctx, val, func, argc, argv, &res);

--- 14 unchanged lines hidden (view full) ---

462 }
463 }
464
465 return res;
466 }
467
468
469 inline napi_value
470 make_callback(napi_async_context ctx, napi_value val, napi_value func)
471 {
472 return make_callback(ctx, val, func, 0, NULL);
473 }
474
475
476 inline napi_value
477 make_callback(napi_async_context ctx, napi_value val, napi_value func,
478 napi_value arg1)
479 {
480 return make_callback(ctx, val, func, 1, &arg1);
481 }
482
483
484 inline napi_value
485 make_callback(napi_async_context ctx, napi_value val, napi_value func,
486 napi_value arg1, napi_value arg2)
487 {
488 napi_value args[2] = { arg1, arg2 };
489
490 return make_callback(ctx, val, func, 2, args);
491 }
492
493
494 inline napi_value
495 make_callback(napi_async_context ctx, napi_value val, napi_value func,
496 napi_value arg1, napi_value arg2, napi_value arg3)
497 {
498 napi_value args[3] = { arg1, arg2, arg3 };
499
500 return make_callback(ctx, val, func, 3, args);
501 }
502
503
504 inline napi_value
505 new_instance(napi_value ctor)
506 {
507 napi_value res;
508 napi_status status;
509
510 status = napi_new_instance(env_, ctor, 0, NULL, &res);
511 if (status != napi_ok) {
512 throw exception("Failed to create instance");

--- 13 unchanged lines hidden (view full) ---

526 if (status != napi_ok) {
527 throw exception("Failed to create instance");
528 }
529
530 return res;
531 }
532
533
534 inline napi_value
535 new_instance(napi_value ctor, napi_value param1, napi_value param2)
536 {
537 napi_value res;
538 napi_status status;
539 napi_value param[2] = { param1, param2 };
540
541 status = napi_new_instance(env_, ctor, 2, param, &res);
542 if (status != napi_ok) {
543 throw exception("Failed to create instance");
544 }
545
546 return res;
547 }
548
549
550 inline void
551 set_element(napi_value obj, uint32_t i, napi_value val)
552 {
553 napi_status status;
554
555 status = napi_set_element(env_, obj, i, val);
556 if (status != napi_ok) {
557 throw exception("Failed to set element");

--- 29 unchanged lines hidden (view full) ---

587 str = create_string_latin1(val, len);
588
589 set_named_property(obj, name, str);
590
591 return str;
592 }
593
594
595 template<typename T>
596 inline void
597 set_named_property(napi_value obj, const char *name, T val)
598 {
599 set_named_property(obj, name, create(val));
600 }
601
602
603 inline napi_value
604 create(int32_t val)
605 {
606 napi_value ptr;
607 napi_status status;
608
609 status = napi_create_int32(env_, val, &ptr);
610 if (status != napi_ok) {
611 throw exception("Failed to create int32");
612 }
613
614 return ptr;
615 }
616
617
618 inline napi_value
619 create(uint32_t val)
620 {
621 napi_value ptr;
622 napi_status status;
623
624 status = napi_create_uint32(env_, val, &ptr);
625 if (status != napi_ok) {
626 throw exception("Failed to create uint32");
627 }
628
629 return ptr;
630 }
631
632
633 inline napi_value
634 create(int64_t val)
635 {
636 napi_value ptr;
637 napi_status status;
638
639 status = napi_create_int64(env_, val, &ptr);
640 if (status != napi_ok) {
641 throw exception("Failed to create int64");
642 }
643
644 return ptr;
645 }
646
647
648 inline void
649 remove_wrap(napi_ref& ref)
650 {
651 if (ref != nullptr) {
652 remove_wrap(get_reference_value(ref));
653 ref = nullptr;
654 }
655 }
656
657
658 inline void *
659 remove_wrap(napi_value val)
660 {
661 void *res;
662 napi_status status;
663
664 status = napi_remove_wrap(env_, val, &res);
665 if (status != napi_ok) {
666 throw exception("Failed to remove_wrap");
667 }
668
669 return res;
670 }
671
672
673 inline void
674 throw_error(const char *str)
675 {
676 napi_throw_error(env_, NULL, str);
677 }
678
679
680 inline void
681 throw_error(const exception &e)

--- 158 unchanged lines hidden ---