-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix uncatchable error inside a promise (#810)
- Loading branch information
Showing
3 changed files
with
120 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <assert.h> | ||
#include "quickjs.h" | ||
|
||
#define MAX_VAL 10 | ||
|
||
static int timeout_interrupt_handler(JSRuntime *rt, void *opaque) { | ||
int *time = (int *)opaque; | ||
if (*time <= MAX_VAL) { | ||
*time += 1; | ||
} | ||
return *time > MAX_VAL; | ||
} | ||
|
||
static void sync_call() | ||
{ | ||
const char *code = | ||
"(function() { \ | ||
try { \ | ||
while (true) {} \ | ||
} catch (e) {} \ | ||
})();"; | ||
|
||
JSRuntime *rt = JS_NewRuntime(); | ||
JSContext *ctx = JS_NewContext(rt); | ||
int time = 0; | ||
JS_SetInterruptHandler(rt, timeout_interrupt_handler, &time); | ||
JSValue ret = JS_Eval(ctx, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL); | ||
assert(time > MAX_VAL); | ||
assert(JS_IsException(ret)); | ||
JS_FreeValue(ctx, ret); | ||
assert(JS_HasException(ctx)); | ||
JSValue e = JS_GetException(ctx); | ||
assert(JS_IsUncatchableError(ctx, e)); | ||
JS_FreeValue(ctx, e); | ||
JS_FreeContext(ctx); | ||
JS_FreeRuntime(rt); | ||
} | ||
|
||
static void async_call() | ||
{ | ||
const char *code = | ||
"(async function() { \ | ||
const loop = async () => { \ | ||
await Promise.resolve(); \ | ||
while (true) {} \ | ||
}; \ | ||
while (true) { \ | ||
await loop().catch(() => {}); \ | ||
} \ | ||
})();"; | ||
|
||
JSRuntime *rt = JS_NewRuntime(); | ||
JSContext *ctx = JS_NewContext(rt); | ||
int time = 0; | ||
JS_SetInterruptHandler(rt, timeout_interrupt_handler, &time); | ||
JSValue ret = JS_Eval(ctx, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL); | ||
assert(!JS_IsException(ret)); | ||
JS_FreeValue(ctx, ret); | ||
assert(JS_IsJobPending(rt)); | ||
int r = 0; | ||
while (JS_IsJobPending(rt)) { | ||
r = JS_ExecutePendingJob(rt, &ctx); | ||
} | ||
assert(time > MAX_VAL); | ||
assert(r == -1); | ||
assert(JS_HasException(ctx)); | ||
JSValue e = JS_GetException(ctx); | ||
assert(JS_IsUncatchableError(ctx, e)); | ||
JS_FreeValue(ctx, e); | ||
JS_FreeContext(ctx); | ||
JS_FreeRuntime(rt); | ||
} | ||
|
||
int main() | ||
{ | ||
sync_call(); | ||
async_call(); | ||
printf("interrupt-test pass\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters