55#include <stdlib.h>
66#include <string.h>
77#include "quickjs.h"
8+ #include "js_native_api.h"
89#include "cutils.h"
910
1011#define MAX_TIME 10
@@ -545,6 +546,109 @@ static void new_errors(void)
545546 JS_FreeRuntime (rt );
546547}
547548
549+ static char go_baton [] = "go baton" ;
550+
551+ static napi_value go_callback (napi_env env , napi_callback_info info )
552+ {
553+ size_t argc ;
554+ napi_value argv [4 ];
555+ napi_value this_arg ;
556+ void * data ;
557+ argc = countof (argv );
558+ assert (napi_ok == napi_get_cb_info (env , info , & argc , argv ,
559+ & this_arg , & data ));
560+ assert (argc == 3 );
561+ assert (data == (void * )go_baton );
562+ napi_value result ;
563+ assert (napi_ok == napi_create_string_utf8 (env , "ok" , 2 , & result ));
564+ return result ;
565+ }
566+
567+ static void napi (void )
568+ {
569+ JSRuntime * rt = JS_NewRuntime ();
570+ JSContext * ctx = JS_NewContext (rt );
571+ napi_env env = (void * )ctx ;
572+ {
573+ typedef struct {
574+ napi_valuetype typ ;
575+ const char source [32 ];
576+ } Entry ;
577+ static const Entry entries [] = {
578+ {napi_undefined , "undefined" },
579+ {napi_null , "null" },
580+ {napi_boolean , "true" },
581+ {napi_boolean , "false" },
582+ {napi_number , "42" },
583+ {napi_number , "13.37" },
584+ {napi_string , "''" },
585+ {napi_symbol , "Symbol('')" },
586+ {napi_object , "Object()" },
587+ {napi_function , "Function()" },
588+ {napi_bigint , "42n" },
589+ };
590+ const Entry * e ;
591+ for (e = entries ; e < endof (entries ); e ++ ) {
592+ napi_handle_scope scope ;
593+ assert (napi_ok == napi_open_handle_scope (env , & scope ));
594+ napi_value script ;
595+ assert (napi_ok == napi_create_string_utf8 (env , e -> source ,
596+ strlen (e -> source ),
597+ & script ));
598+ napi_value result ;
599+ assert (napi_ok == napi_run_script (env , script , & result ));
600+ napi_valuetype typ ;
601+ assert (napi_ok == napi_typeof (env , result , & typ ));
602+ assert (typ == e -> typ );
603+ assert (napi_ok == napi_close_handle_scope (env , scope ));
604+ assert (JS_IsUninitialized (JS_GetException (ctx )));
605+ }
606+ }
607+ {
608+ napi_handle_scope scope ;
609+ napi_value result ;
610+ assert (napi_ok == napi_open_handle_scope (env , & scope ));
611+ {
612+ napi_value obj ;
613+ napi_escapable_handle_scope inner ;
614+ assert (napi_ok == napi_open_escapable_handle_scope (env , & inner ));
615+ assert (napi_ok == napi_create_array (env , & obj ));
616+ assert (napi_ok == napi_escape_handle (env , inner , obj , & result ));
617+ assert (napi_ok == napi_close_escapable_handle_scope (env , inner ));
618+ }
619+ bool ok ;
620+ assert (napi_ok == napi_is_array (env , result , & ok ));
621+ assert (ok );
622+ assert (napi_ok == napi_close_handle_scope (env , scope ));
623+ assert (JS_IsUninitialized (JS_GetException (ctx )));
624+ }
625+ {
626+ napi_handle_scope scope ;
627+ assert (napi_ok == napi_open_handle_scope (env , & scope ));
628+ napi_value global ;
629+ assert (napi_ok == napi_get_global (env , & global ));
630+ napi_property_descriptor d = {
631+ .utf8name = "go" ,
632+ .method = go_callback ,
633+ .data = go_baton ,
634+ };
635+ assert (napi_ok == napi_define_properties (env , global , 1 , & d ));
636+ static const char source [] = "go(1,2,3)" ;
637+ napi_value script ;
638+ assert (napi_ok == napi_create_string_utf8 (env , source , strlen (source ),
639+ & script ));
640+ napi_value result ;
641+ assert (napi_ok == napi_run_script (env , script , & result ));
642+ napi_valuetype typ ;
643+ assert (napi_ok == napi_typeof (env , result , & typ ));
644+ assert (typ == napi_string );
645+ assert (napi_ok == napi_close_handle_scope (env , scope ));
646+ assert (JS_IsUninitialized (JS_GetException (ctx )));
647+ }
648+ JS_FreeContext (ctx );
649+ JS_FreeRuntime (rt );
650+ }
651+
548652int main (void )
549653{
550654 sync_call ();
@@ -558,5 +662,6 @@ int main(void)
558662 promise_hook ();
559663 dump_memory_usage ();
560664 new_errors ();
665+ napi ();
561666 return 0 ;
562667}
0 commit comments