1
+ #ifdef NDEBUG
2
+ #undef NDEBUG
3
+ #endif
4
+ #include <assert.h>
1
5
#include <stdlib.h>
2
6
#include "quickjs.h"
3
7
4
8
#define MAX_TIME 10
5
9
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
-
15
10
static int timeout_interrupt_handler (JSRuntime * rt , void * opaque )
16
11
{
17
12
int * time = (int * )opaque ;
@@ -34,12 +29,12 @@ static void sync_call(void)
34
29
int time = 0 ;
35
30
JS_SetInterruptHandler (rt , timeout_interrupt_handler , & time );
36
31
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 ));
39
34
JS_FreeValue (ctx , ret );
40
- expect (JS_HasException (ctx ));
35
+ assert (JS_HasException (ctx ));
41
36
JSValue e = JS_GetException (ctx );
42
- expect (JS_IsUncatchableError (ctx , e ));
37
+ assert (JS_IsUncatchableError (ctx , e ));
43
38
JS_FreeValue (ctx , e );
44
39
JS_FreeContext (ctx );
45
40
JS_FreeRuntime (rt );
@@ -61,26 +56,26 @@ static void async_call(void)
61
56
int time = 0 ;
62
57
JS_SetInterruptHandler (rt , timeout_interrupt_handler , & time );
63
58
JSValue ret = JS_Eval (ctx , code , strlen (code ), "<input>" , JS_EVAL_TYPE_GLOBAL );
64
- expect (!JS_IsException (ret ));
59
+ assert (!JS_IsException (ret ));
65
60
JS_FreeValue (ctx , ret );
66
- expect (JS_IsJobPending (rt ));
61
+ assert (JS_IsJobPending (rt ));
67
62
int r = 0 ;
68
63
while (JS_IsJobPending (rt )) {
69
64
r = JS_ExecutePendingJob (rt , & ctx );
70
65
}
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 ));
74
69
JSValue e = JS_GetException (ctx );
75
- expect (JS_IsUncatchableError (ctx , e ));
70
+ assert (JS_IsUncatchableError (ctx , e ));
76
71
JS_FreeValue (ctx , e );
77
72
JS_FreeContext (ctx );
78
73
JS_FreeRuntime (rt );
79
74
}
80
75
81
76
static JSValue save_value (JSContext * ctx , JSValue this_val , int argc , JSValue * argv )
82
77
{
83
- expect (argc == 1 );
78
+ assert (argc == 1 );
84
79
JSValue * p = (JSValue * )JS_GetContextOpaque (ctx );
85
80
* p = JS_DupValue (ctx , argv [0 ]);
86
81
return JS_UNDEFINED ;
@@ -109,26 +104,54 @@ static void async_call_stack_overflow(void)
109
104
JS_SetPropertyStr (ctx , global , "save_value" , JS_NewCFunction (ctx , save_value , "save_value" , 1 ));
110
105
JS_FreeValue (ctx , global );
111
106
JSValue ret = JS_Eval (ctx , code , strlen (code ), "<input>" , JS_EVAL_TYPE_GLOBAL );
112
- expect (!JS_IsException (ret ));
107
+ assert (!JS_IsException (ret ));
113
108
JS_FreeValue (ctx , ret );
114
- expect (JS_IsJobPending (rt ));
109
+ assert (JS_IsJobPending (rt ));
115
110
int r = 0 ;
116
111
while (JS_IsJobPending (rt )) {
117
112
r = JS_ExecutePendingJob (rt , & ctx );
118
113
}
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
122
117
JS_FreeValue (ctx , value );
123
118
JS_FreeContext (ctx );
124
119
JS_FreeRuntime (rt );
125
120
}
126
121
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 )
128
151
{
129
152
sync_call ();
130
153
async_call ();
131
154
async_call_stack_overflow ();
132
- printf ( "interrupt-test passed\n" );
155
+ raw_context_global_var ( );
133
156
return 0 ;
134
- }
157
+ }
0 commit comments