1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
|
/* -----------------------------------------------------------------------------
* js_ctor: template for wrapping a ctor.
* - $jswrapper: wrapper of called ctor
* - $jsparent: mangled name of parent (or SWIG_NAPI_ObjectWrap if none)
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* - $jsargcount: number of arguments
* - $jsargrequired: minimum number of arguments
* - $jsmangledname: mangled name of class
* - $jsmangledtype: mangled type of class
* ----------------------------------------------------------------------------- */
%fragment("js_ctor", "templates") %{
template <typename SWIG_OBJ_WRAP>
// js_ctor
// This is the main constructor
$jsmangledname_templ<SWIG_OBJ_WRAP>::$jsmangledname_templ(const Napi::CallbackInfo &info)
: $jsparent_templ<SWIG_OBJ_WRAP>(true, info) {
Napi::Env env = info.Env();
this->info = SWIGTYPE_$jsmangledtype;
if (info.Length() == 1 && info[0].IsExternal()) {
// This constructor has been called internally from C++/SWIG
// to wrap an already existing C++ object in JS
this->self = info[0].As<Napi::External<void>>().Data();
this->owned = false;
return;
}
this->owned = true;
$jslocals
if(static_cast<int>(info.Length()) < $jsargrequired || static_cast<int>(info.Length()) > $jsargcount) {
SWIG_Error(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
}
$jscode
this->self = result;
return;
goto fail;
fail:
return;
}
// This is the bypass constructor to be used from child classes
template <typename SWIG_OBJ_WRAP>
$jsmangledname_templ<SWIG_OBJ_WRAP>::$jsmangledname_templ(bool, const Napi::CallbackInfo &info)
: $jsparent_templ<SWIG_OBJ_WRAP>(true, info) {}
%}
/* -----------------------------------------------------------------------------
* js_veto_ctor: a vetoing ctor for abstract classes
* - $jsmangledname: mangled name of class
* - $jsparent: mangled name of parent (or SWIG_NAPI_ObjectWrap if none)
* ----------------------------------------------------------------------------- */
%fragment ("js_veto_ctor", "templates")
%{
// js_veto_ctor
template <typename SWIG_OBJ_WRAP>
$jsmangledname_templ<SWIG_OBJ_WRAP>::$jsmangledname_templ(const Napi::CallbackInfo &info)
: $jsparent_templ<SWIG_OBJ_WRAP>(true, info) {
Napi::Env env = info.Env();
if (info.Length() == 1 && info[0].IsExternal()) {
// This constructor has been called internally from C++/SWIG
// to wrap an already existing C++ object in JS as its
// base abstract class
this->self = info[0].As<Napi::External<void>>().Data();
this->owned = false;
return;
}
SWIG_Error(SWIG_ERROR, "Class $jsname can not be instantiated");
return;
goto fail;
fail:
return;
}
// This is the extendable constructor to be used from child classes
template <typename SWIG_OBJ_WRAP>
$jsmangledname_templ<SWIG_OBJ_WRAP>::$jsmangledname_templ(bool, const Napi::CallbackInfo &info)
: $jsparent_templ<SWIG_OBJ_WRAP>(true, info) {
}
%}
/* -----------------------------------------------------------------------------
* js_ctor_dispatcher: dispatcher for overloaded constructors
* - $jsmangledname: mangled name of class
* - $jsparent: mangled name of parent (or SWIG_NAPI_ObjectWrap if none)
* - $jsdispatchcases: part containing code for dispatching
* - $jsmangledtype: mangled type of class
* ----------------------------------------------------------------------------- */
%fragment ("js_ctor_dispatcher", "templates")
%{
// js_ctor_dispatcher
template <typename SWIG_OBJ_WRAP>
$jsmangledname_templ<SWIG_OBJ_WRAP>::$jsmangledname_templ(const Napi::CallbackInfo &info)
: $jsparent_templ<SWIG_OBJ_WRAP>(true, info) {
Napi::Env env = info.Env();
Napi::Object self;
NAPI_CHECK_RESULT(info.This().ToObject(), self);
this->info = SWIGTYPE_$jsmangledtype;
if (info.Length() == 1 && info[0].IsExternal()) {
// This constructor has been called internally from C++/SWIG
// to wrap an already existing C++ object in JS
this->self = info[0].As<Napi::External<void>>().Data();
this->owned = false;
return;
}
// switch all cases by means of series of if-returns.
$jsdispatchcases
// default:
SWIG_Error(SWIG_ERROR, "Illegal arguments for construction of $jsmangledname");
goto fail;
fail:
return;
}
// This is the extendable constructor to be used from child classes
template <typename SWIG_OBJ_WRAP>
$jsmangledname_templ<SWIG_OBJ_WRAP>::$jsmangledname_templ(bool, const Napi::CallbackInfo &info)
: $jsparent_templ<SWIG_OBJ_WRAP>(true, info) {
}
%}
/* -----------------------------------------------------------------------------
* js_overloaded_ctor: template for wrapping a ctor.
* - $jswrapper: wrapper of called ctor
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* - $jsargcount: number of arguments
* - $jsargrequired: minimum number of arguments
* - $jsmangledtype: mangled type of class
* ----------------------------------------------------------------------------- */
%fragment("js_overloaded_ctor", "templates") %{
// js_overloaded_ctor
template <typename SWIG_OBJ_WRAP>
Napi::Value $jsmangledname_templ<SWIG_OBJ_WRAP>::$jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object self;
$jslocals
NAPI_CHECK_RESULT(info.This().ToObject(), self);
this->owned = true;
if(static_cast<int>(info.Length()) < $jsargrequired || static_cast<int>(info.Length()) > $jsargcount) {
SWIG_Error(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
}
$jscode
this->self = result;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_ctor_dispatch_case: template for a dispatch case for calling an overloaded ctor.
* - $jsargcount: number of arguments of called ctor
* - $jsargrequired: minimum number of arguments
* - $jswrapper: wrapper of called ctor
*
* Note: a try-catch-like mechanism is used to switch cases
* ----------------------------------------------------------------------------- */
%fragment ("js_ctor_dispatch_case", "templates")
%{
// js_ctor_dispatch_case
if(static_cast<int>(info.Length()) >= $jsargrequired && static_cast<int>(info.Length()) <= $jsargcount) {
#ifdef NAPI_CPP_EXCEPTIONS
bool tryNext = false;
try {
$jswrapper(info);
} catch (const Napi::TypeError &) {
tryNext = true;
} catch (const Napi::Error &e) {
throw e;
}
if (!tryNext)
return;
#else
$jswrapper(info);
if (env.IsExceptionPending()) {
Napi::Error e = env.GetAndClearPendingException();
Napi::Value typeErrorValue;
bool isTypeError;
Napi::Function typeErrorCons;
// Yes, this is ugly
// TODO: Fix this in Node.js when the core team grows up
NAPI_CHECK_RESULT(env.Global().Get("TypeError"), typeErrorValue);
typeErrorCons = typeErrorValue.As<Napi::Function>();
NAPI_CHECK_RESULT(e.Value().InstanceOf(typeErrorCons), isTypeError);
if (!isTypeError) {
// This is not the error you are looking for
e.ThrowAsJavaScriptException();
SWIG_fail;
}
} else {
return;
}
#endif
}
%}
/* -----------------------------------------------------------------------------
* js_check_arg: template for checking if an argument exists
* - $jsarg: number of argument
* ----------------------------------------------------------------------------- */
%fragment ("js_check_arg", "templates")
%{if(info.Length() > $jsarg)%}
/* -----------------------------------------------------------------------------
* js_dtor: template for a destructor wrapper
* - $jsmangledname: mangled class name
* - $jstype: class type
* ----------------------------------------------------------------------------- */
%fragment ("js_dtor", "templates")
%{
// js_dtor
template <typename SWIG_OBJ_WRAP>
$jsmangledname_templ<SWIG_OBJ_WRAP>::~$jsmangledname_templ() {
}
%}
/* -----------------------------------------------------------------------------
* js_dtoroverride: template for a destructor wrapper
* - ${classname_mangled}: mangled class name
* - $jstype: class type
* - ${destructor_action}: The custom destructor action to invoke.
* ----------------------------------------------------------------------------- */
%fragment ("js_dtoroverride", "templates")
%{
// js_dtoroverride
template <typename SWIG_OBJ_WRAP>
${classname_mangled}_templ<SWIG_OBJ_WRAP>::~${classname_mangled}_templ() {
auto arg1 = reinterpret_cast<$jstype>(this->self);
if (this->owned && arg1) {
${destructor_action}
this->self = nullptr;
}
}
%}
/* -----------------------------------------------------------------------------
* js_global_getter: template for global getter function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_global_getter", "templates")
%{
// js_global_getter
Napi::Value $jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
$jscode
return jsresult;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_global_setter: template for global setter function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_global_setter", "templates")
%{
// js_global_setter
void $jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value value = info.Length() > 0 ? info[0] : Napi::Value();
Napi::Value jsresult;
$jslocals
$jscode
return;
goto fail;
fail:
return;
}
%}
/* -----------------------------------------------------------------------------
* jsnapi_register_global_variable: template for a statement that registers a global variable
* - $jsname: variable name
* - $jsparent: parent namespace
* - $jsgetter: wrapper of the getter function
* - $jssetter: wrapper of the setter function
*
* Note: this template is also used for global variables.
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_global_variable", "templates")
%{
// jsnapi_register_global_variable
do {
Napi::PropertyDescriptor pd = Napi::PropertyDescriptor::Accessor<$jsgetter, $jssetter>("$jsname");
NAPI_CHECK_MAYBE($jsparent.DefineProperties({pd}));
} while (0);
%}
/* -----------------------------------------------------------------------------
* js_global_function: template for function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jsargcount: number of arguments
* - $jsargrequired: minimum number of arguments
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_global_function", "templates")
%{
// js_global_function
Napi::Value $jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
if(static_cast<int>(info.Length()) < $jsargrequired || static_cast<int>(info.Length()) > $jsargcount) {
SWIG_Error(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
}
$jscode
return jsresult;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_global_function_dispatcher: template for a global function dispatcher for
* global overloaded functions
* - $jswrapper: wrapper function name
* - $jsname: name of the wrapped function
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_global_function_dispatcher", "templates")
%{
// js_global_function_dispatcher
Napi::Value $jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jscode
SWIG_Error(SWIG_ERROR, "Illegal arguments for function $jsname.");
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* jsnapi_register_global_function: template for a statement that registers a global function
* - $jsname: function name
* - $jsparent: parent namespace
* - $jswrapper: name of the JS wrapper
*
* Note: this template is also used for global variables.
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_global_function", "templates")
%{
// jsnapi_register_global_function
do {
Napi::PropertyDescriptor pd = Napi::PropertyDescriptor::Function("$jsname", $jswrapper);
NAPI_CHECK_MAYBE($jsparent.DefineProperties({pd}));
} while (0);
%}
/* -----------------------------------------------------------------------------
* js_getter: template for getter function wrappers
* - $jsmangledname: mangled class name
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_getter", "templates")
%{
// js_getter
template <typename SWIG_OBJ_WRAP>
Napi::Value $jsmangledname_templ<SWIG_OBJ_WRAP>::$jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
$jscode
return jsresult;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_setter: template for setter function wrappers
* - $jsmangledname: mangled class name
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_setter", "templates")
%{
// js_setter
template <typename SWIG_OBJ_WRAP>
void $jsmangledname_templ<SWIG_OBJ_WRAP>::$jswrapper(const Napi::CallbackInfo &info, const Napi::Value &value) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
$jscode
return;
goto fail;
fail:
return;
}
%}
/* -----------------------------------------------------------------------------
* js_function: template for function wrappers
* - $jsmangledname: mangled class name
* - $jswrapper: wrapper function name
* - $jsargcount: minimum number of arguments
* - $jsargrequired: minimum number of arguments
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_function", "templates")
%{
// js_function
template <typename SWIG_OBJ_WRAP>
Napi::Value $jsmangledname_templ<SWIG_OBJ_WRAP>::$jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
if(static_cast<int>(info.Length()) < $jsargrequired || static_cast<int>(info.Length()) > $jsargcount) {
SWIG_Error(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
}
$jscode
return jsresult;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_function_dispatcher: template for a function dispatcher for overloaded functions
* - $jsmangledname: mangled class name
* - $jswrapper: wrapper function name
* - $jsname: name of the wrapped function
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_function_dispatcher", "templates")
%{
// js_function_dispatcher
template <typename SWIG_OBJ_WRAP>
Napi::Value $jsmangledname_templ<SWIG_OBJ_WRAP>::$jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jscode
SWIG_Error(SWIG_ERROR, "Illegal arguments for function $jsname.");
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_overloaded_function: template for a overloaded function
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_overloaded_function", "templates")
%{
// js_overloaded_function
template <typename SWIG_OBJ_WRAP>
Napi::Value $jsmangledname_templ<SWIG_OBJ_WRAP>::$jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
$jscode
return jsresult;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_global_overloaded_function: template for a global overloaded function
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_global_overloaded_function", "templates")
%{
// js_global_overloaded_function
Napi::Value $jswrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value jsresult;
$jslocals
$jscode
return jsresult;
goto fail;
fail:
return Napi::Value();
}
%}
/* -----------------------------------------------------------------------------
* js_function_dispatch_case: template for a case used in the function dispatcher
* - $jswrapper: wrapper function name
* - $jsargcount: number of arguments of overloaded function
* - $jsargrequired: minimum number of arguments
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_function_dispatch_case", "templates")
%{
// js_function_dispatch_case
if(static_cast<int>(info.Length()) >= $jsargrequired && static_cast<int>(info.Length()) <= $jsargcount) {
#ifdef NAPI_CPP_EXCEPTIONS
bool tryNext = false;
try {
jsresult = $jswrapper(info);
} catch (const Napi::TypeError &) {
tryNext = true;
} catch (const Napi::Error &e) {
throw e;
}
if (!tryNext)
return jsresult;
#else
$jswrapper(info);
if (env.IsExceptionPending()) {
Napi::Error e = env.GetAndClearPendingException();
Napi::Value typeErrorValue;
bool isTypeError;
Napi::Function typeErrorCons;
// Yes, this is ugly
// TODO: Fix this in Node.js when the core team grows up
NAPI_CHECK_RESULT(env.Global().Get("TypeError"), typeErrorValue);
typeErrorCons = typeErrorValue.As<Napi::Function>();
NAPI_CHECK_RESULT(e.Value().InstanceOf(typeErrorCons), isTypeError);
if (!isTypeError) {
// This is not the error you are looking for
e.ThrowAsJavaScriptException();
SWIG_fail;
}
} else {
return jsresult;
}
#endif
}
%}
/* -----------------------------------------------------------------------------
* jsnapi_class_prologue_template: template for a class prologue
* - $jsmangledname: mangled class name
* - $jsparent: mangled name of parent (or SWIG_NAPI_ObjectWrap if none)
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_class_prologue_template", "templates")
%{
// jsnapi_class_prologue_template
template <typename SWIG_OBJ_WRAP>
class $jsmangledname_templ : public $jsparent_templ<SWIG_OBJ_WRAP> {
public:
$jsmangledname_templ(const Napi::CallbackInfo &);
$jsmangledname_templ(bool, const Napi::CallbackInfo &);
%}
/* -----------------------------------------------------------------------------
* jsnapi_class_dtor_declaration: template for a class destructor declaration
* - $jsmangledname: mangled class name
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_class_dtor_declaration", "templates")
%{
virtual ~$jsmangledname_templ();
%}
/* -----------------------------------------------------------------------------
* jsnapi_class_method_declaration: template for a class method declaration
* - $jsmangledname: mangled class name
* - $jswrapper: method name
* - $jsstatic: static modifier
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_class_method_declaration", "templates")
%{
// jsnapi_class_method_declaration
$jsstatic Napi::Value $jswrapper(const Napi::CallbackInfo &);
%}
/* -----------------------------------------------------------------------------
* jsnapi_class_setter_declaration: template for a class method declaration
* - $jsmangledname: mangled class name
* - $jswrapper: method name
* - $jsstatic: static modifier
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_class_setter_declaration", "templates")
%{
// jsnapi_class_setter_declaration
$jsstatic void $jswrapper(const Napi::CallbackInfo &, const Napi::Value &);
%}
/* -----------------------------------------------------------------------------
* jsnapi_class_epilogue_template: template for a class epilogue
* - $jsmangledname: mangled class name
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_class_epilogue_template", "templates")
%{
// jsnapi_class_epilogue_template
static void JS_veto_set_static_variable(const Napi::CallbackInfo &, const Napi::Value &);
void JS_veto_set_variable(const Napi::CallbackInfo &, const Napi::Value &);
};
template <typename SWIG_OBJ_WRAP>
void $jsmangledname_templ<SWIG_OBJ_WRAP>::JS_veto_set_static_variable(const Napi::CallbackInfo &info, const Napi::Value &value) {
SWIG_NAPI_Raise(info.Env(), "Tried to write read-only variable.");
}
template <typename SWIG_OBJ_WRAP>
void $jsmangledname_templ<SWIG_OBJ_WRAP>::JS_veto_set_variable(const Napi::CallbackInfo &info, const Napi::Value &value) {
SWIG_NAPI_Raise(info.Env(), "Tried to write read-only variable.");
}
%}
/* -----------------------------------------------------------------------------
* jsnapi_class_instance: template for a class declaration instance
* - $jsmangledname: mangled class name
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_declare_class_instance", "templates")
%{
// jsnapi_class_instance
class $jsmangledname_inst : public $jsmangledname_templ<$jsmangledname_inst> {
public:
using $jsmangledname_templ::$jsmangledname_templ;
virtual ~$jsmangledname_inst() {};
static void GetMembers(
Napi::Env,
std::map<std::string, $jsmangledname_templ::PropertyDescriptor> &,
std::map<std::string, $jsmangledname_templ::PropertyDescriptor> &
);
static Napi::Function GetClass(Napi::Env);
};
%}
/*
* Inheritance is still not officially supported in NAPI
* Refer to this for my workaround:
* https://mmomtchev.medium.com/c-class-inheritance-with-node-api-and-node-addon-api-c180334d9902
*/
/* -----------------------------------------------------------------------------
* jsnapi_inherited_class_prologue_template: template for a class prologue
* - $jsmangledname: mangled class name
* - $jsparent: mangled name of parent class
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_inherited_class_prologue_template", "templates")
%{
// jsnapi_inherited_class_prologue_template
SWIG_NAPI_ClientData $jsmangledname_clientData;
template <typename SWIG_OBJ_WRAP>
class $jsmangledname_templ : public $jsparent_templ<SWIG_OBJ_WRAP> {
public:
$jsmangledname_templ(const Napi::CallbackInfo& info);
%}
/* -----------------------------------------------------------------------------
* jsnapi_getclass: template for creating a class object
* - $jsname: class name
* - $jsmangledname: mangled class name
* - $jsfunctions: member functions
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_getclass", "templates")
%{
/* Class: $jsname ($jsmangledname) */
// jsnapi_getclass
Napi::Function $jsmangledname_inst::GetClass(Napi::Env env) {
std::map<std::string, $jsmangledname_templ::PropertyDescriptor> members, staticMembers;
GetMembers(env, members, staticMembers);
std::vector<$jsmangledname_inst::PropertyDescriptor> symbolTable;
for (auto it = members.begin(); it != members.end(); it++)
symbolTable.push_back(it->second);
for (auto it = staticMembers.begin(); it != staticMembers.end(); it++)
symbolTable.push_back(it->second);
return Napi::ObjectWrap<$jsmangledname_inst>::DefineClass(env, "$jsname", symbolTable);
}
void $jsmangledname_inst::GetMembers(
Napi::Env env,
std::map<std::string, $jsmangledname_templ::PropertyDescriptor> &members,
std::map<std::string, $jsmangledname_templ::PropertyDescriptor> &staticMembers
) {
std::map<std::string, $jsparent_templ<$jsparent_inst>::PropertyDescriptor> baseMembers, baseStaticMembers;
$jsparent_inst::GetMembers(env, baseMembers, baseStaticMembers);
members.insert(baseMembers.begin(), baseMembers.end());
staticMembers.insert(staticMembers.begin(), staticMembers.end());
/* register wrapper functions */
$jsnapiwrappers
/* add static class functions and variables */
$jsnapistaticwrappers
}
%}
/* -----------------------------------------------------------------------------
* jsnapi_registerclass: template for regsitering a class object
* - $jsname: class name
* - $jsmangledname: mangled class name
* - $jsparent: parent namespace
* - $jsmangledtype: mangled class type
* - $jsclassidx: class index in the class table
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_registerclass", "templates")
%{
/* Class: $jsname ($jsmangledname) */
// jsnapi_registerclass
Napi::Function $jsmangledname_ctor = $jsmangledname_inst::GetClass(env);
$jsparent.Set("$jsname", $jsmangledname_ctor);
if (SWIGTYPE_$jsmangledtype->clientdata == nullptr) {
SWIGTYPE_$jsmangledtype->clientdata = new size_t($jsclassidx);
}
Napi::FunctionReference *$jsmangledname_ctor_ref = new Napi::FunctionReference();
*$jsmangledname_ctor_ref = Napi::Persistent($jsmangledname_ctor);
env.GetInstanceData<EnvInstanceData>()->ctor[$jsclassidx] = $jsmangledname_ctor_ref;
%}
/* -----------------------------------------------------------------------------
* jsnapi_setup_inheritance: setup inheritance between two classes
* - $jsname: class name
* - $jsmangledname: mangled class name
* - $jsparent: mangled name of parent class
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_setup_inheritance", "templates")
%{
// Inheritance for $jsmangledname ($jsname) <- $jsparent
// jsnapi_setup_inheritance
do {
Napi::Value protoBase, protoSub;
NAPI_CHECK_RESULT($jsmangledname_ctor.Get("prototype"), protoSub);
NAPI_CHECK_RESULT($jsparent_ctor.Get("prototype"), protoBase);
NAPI_CHECK_MAYBE(setProto.Call({$jsmangledname_ctor, $jsparent_ctor}));
NAPI_CHECK_MAYBE(setProto.Call({protoSub, protoBase}));
} while (0);
%}
/* -----------------------------------------------------------------------------
* jsnapi_create_namespace: template for a statement that creates a namespace object.
* - $jsmangledname: mangled namespace name
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_create_namespace", "templates")
%{
// jsnapi_create_namespace
Napi::Object $jsmangledname = Napi::Object::New(env);
%}
/* -----------------------------------------------------------------------------
* jsnapi_register_namespace: template for a statement that registers a namespace in a parent namespace.
* - $jsname: name of namespace
* - $jsmangledname: mangled name of namespace
* - $jsparent: mangled name of parent namespace
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_namespace", "templates")
%{
// jsnapi_register_namespace
NAPI_CHECK_MAYBE($jsparent.Set("$jsname", $jsmangledname));
%}
/* -----------------------------------------------------------------------------
* jsnapi_member_function_descriptor: template for a statement that registers a member function.
* - $jsmangledname: mangled class name
* - $jsname: name of the function
* - $jswrapper: wrapper of the member function
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_member_function", "templates")
%{
// jsnapi_member_function_descriptor
members.erase("$jsname");
members.insert({"$jsname",
$jsmangledname_templ::InstanceMethod("$jsname",
&$jsmangledname_templ::$jswrapper,
static_cast<napi_property_attributes>(napi_writable | napi_configurable))
});
%}
/* -----------------------------------------------------------------------------
* jsnapi_register_member_variable: template for a statement that registers a member variable.
* - $jsname: name of the function
* - $jsmangledname: mangled class name
* - $jsgetter: wrapper of the getter function
* - $jssetter: wrapper of the setter function
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_member_variable", "templates")
%{
// jsnapi_register_member_variable
members.erase("$jsname");
members.insert({"$jsname",
$jsmangledname_templ::InstanceAccessor("$jsname",
&$jsmangledname_templ::$jsgetter,
&$jsmangledname_templ::$jssetter,
static_cast<napi_property_attributes>(napi_writable | napi_enumerable | napi_configurable))
});
%}
/* -----------------------------------------------------------------------------
* jsnapi_register_static_function: template for a statement that registers a static class function.
* - $jsname: function name
* - $jsmangledname: mangled class name
* - $jswrapper: wrapper of the function
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_static_function", "templates")
%{
// jsnapi_register_static_function
staticMembers.erase("$jsname");
staticMembers.insert({"$jsname",
StaticMethod("$jsname",
&$jsmangledname_templ::$jswrapper,
static_cast<napi_property_attributes>(napi_writable | napi_configurable))
});
%}
/* -----------------------------------------------------------------------------
* jsnapi_register_static_variable: template for a statement that registers a static variable.
* - $jsname: variable name
* - $jsmangledname: mangled class name
* - $jsgetter: wrapper of the getter function
* - $jssetter: wrapper of the setter function
* ----------------------------------------------------------------------------- */
%fragment("jsnapi_register_static_variable", "templates")
%{
// jsnapi_register_static_variable
staticMembers.erase("$jsname");
staticMembers.insert({"$jsname",
StaticAccessor("$jsname",
&$jsmangledname_templ::$jsgetter,
&$jsmangledname_templ::$jssetter,
static_cast<napi_property_attributes>(napi_writable | napi_enumerable | napi_configurable))
});
%}
|