1+ #ifdef NDEBUG
2+ #undef NDEBUG
3+ #endif
4+ #include <assert.h>
15#include <stdlib.h>
26#include "quickjs.h"
37
48#define MAX_TIME 10
59
6- #define expect (condition ) \
7- do { \
8- if (!(condition)) { \
9- fprintf(stderr, "Failed: %s, file %s, line %d\n", \
10- #condition, __FILE__, __LINE__); \
11- exit(EXIT_FAILURE); \
12- } \
13- } while (0)
14-
1510static int timeout_interrupt_handler (JSRuntime * rt , void * opaque )
1611{
1712 int * time = (int * )opaque ;
@@ -34,12 +29,12 @@ static void sync_call(void)
3429 int time = 0 ;
3530 JS_SetInterruptHandler (rt , timeout_interrupt_handler , & time );
3631 JSValue ret = JS_Eval (ctx , code , strlen (code ), "<input>" , JS_EVAL_TYPE_GLOBAL );
37- expect (time > MAX_TIME );
38- expect (JS_IsException (ret ));
32+ assert (time > MAX_TIME );
33+ assert (JS_IsException (ret ));
3934 JS_FreeValue (ctx , ret );
40- expect (JS_HasException (ctx ));
35+ assert (JS_HasException (ctx ));
4136 JSValue e = JS_GetException (ctx );
42- expect (JS_IsUncatchableError (ctx , e ));
37+ assert (JS_IsUncatchableError (ctx , e ));
4338 JS_FreeValue (ctx , e );
4439 JS_FreeContext (ctx );
4540 JS_FreeRuntime (rt );
@@ -61,26 +56,26 @@ static void async_call(void)
6156 int time = 0 ;
6257 JS_SetInterruptHandler (rt , timeout_interrupt_handler , & time );
6358 JSValue ret = JS_Eval (ctx , code , strlen (code ), "<input>" , JS_EVAL_TYPE_GLOBAL );
64- expect (!JS_IsException (ret ));
59+ assert (!JS_IsException (ret ));
6560 JS_FreeValue (ctx , ret );
66- expect (JS_IsJobPending (rt ));
61+ assert (JS_IsJobPending (rt ));
6762 int r = 0 ;
6863 while (JS_IsJobPending (rt )) {
6964 r = JS_ExecutePendingJob (rt , & ctx );
7065 }
71- expect (time > MAX_TIME );
72- expect (r == -1 );
73- expect (JS_HasException (ctx ));
66+ assert (time > MAX_TIME );
67+ assert (r == -1 );
68+ assert (JS_HasException (ctx ));
7469 JSValue e = JS_GetException (ctx );
75- expect (JS_IsUncatchableError (ctx , e ));
70+ assert (JS_IsUncatchableError (ctx , e ));
7671 JS_FreeValue (ctx , e );
7772 JS_FreeContext (ctx );
7873 JS_FreeRuntime (rt );
7974}
8075
8176static JSValue save_value (JSContext * ctx , JSValue this_val , int argc , JSValue * argv )
8277{
83- expect (argc == 1 );
78+ assert (argc == 1 );
8479 JSValue * p = (JSValue * )JS_GetContextOpaque (ctx );
8580 * p = JS_DupValue (ctx , argv [0 ]);
8681 return JS_UNDEFINED ;
@@ -109,26 +104,54 @@ static void async_call_stack_overflow(void)
109104 JS_SetPropertyStr (ctx , global , "save_value" , JS_NewCFunction (ctx , save_value , "save_value" , 1 ));
110105 JS_FreeValue (ctx , global );
111106 JSValue ret = JS_Eval (ctx , code , strlen (code ), "<input>" , JS_EVAL_TYPE_GLOBAL );
112- expect (!JS_IsException (ret ));
107+ assert (!JS_IsException (ret ));
113108 JS_FreeValue (ctx , ret );
114- expect (JS_IsJobPending (rt ));
109+ assert (JS_IsJobPending (rt ));
115110 int r = 0 ;
116111 while (JS_IsJobPending (rt )) {
117112 r = JS_ExecutePendingJob (rt , & ctx );
118113 }
119- expect (r == 1 );
120- expect (!JS_HasException (ctx ));
121- expect (JS_IsError (ctx , value )); /* StackOverflow should be caught */
114+ assert (r == 1 );
115+ assert (!JS_HasException (ctx ));
116+ assert (JS_IsError (ctx , value )); // stack overflow should be caught
122117 JS_FreeValue (ctx , value );
123118 JS_FreeContext (ctx );
124119 JS_FreeRuntime (rt );
125120}
126121
127- int main ()
122+ // https://github.com/quickjs-ng/quickjs/issues/914
123+ static void raw_context_global_var (void )
124+ {
125+ JSRuntime * rt = JS_NewRuntime ();
126+ JSContext * ctx = JS_NewContextRaw (rt );
127+ JS_AddIntrinsicEval (ctx );
128+ {
129+ static const char code [] = "globalThis" ;
130+ JSValue ret = JS_Eval (ctx , code , strlen (code ), "*" , JS_EVAL_TYPE_GLOBAL );
131+ assert (JS_IsException (ret ));
132+ JS_FreeValue (ctx , ret );
133+ }
134+ {
135+ static const char code [] = "var x = 42" ;
136+ JSValue ret = JS_Eval (ctx , code , strlen (code ), "*" , JS_EVAL_TYPE_GLOBAL );
137+ assert (JS_IsUndefined (ret ));
138+ JS_FreeValue (ctx , ret );
139+ }
140+ {
141+ static const char code [] = "function f() {}" ;
142+ JSValue ret = JS_Eval (ctx , code , strlen (code ), "*" , JS_EVAL_TYPE_GLOBAL );
143+ assert (JS_IsUndefined (ret ));
144+ JS_FreeValue (ctx , ret );
145+ }
146+ JS_FreeContext (ctx );
147+ JS_FreeRuntime (rt );
148+ }
149+
150+ int main (void )
128151{
129152 sync_call ();
130153 async_call ();
131154 async_call_stack_overflow ();
132- printf ( "interrupt-test passed\n" );
155+ raw_context_global_var ( );
133156 return 0 ;
134- }
157+ }
0 commit comments