Skip to content

Commit b4338d3

Browse files
past-dueFabrice Bellard
andcommitted
fixed module async evaluation logic - added JS_DUMP_MODULE_EXEC
Port of bellard/quickjs@2fd48bf Co-authored-by: Fabrice Bellard <[email protected]>
1 parent fb496c3 commit b4338d3

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

quickjs.c

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ struct JSModuleDef {
849849
int async_parent_modules_count;
850850
int async_parent_modules_size;
851851
int pending_async_dependencies;
852+
/* true: async_evaluation_timestamp corresponds to [[AsyncEvaluationOrder]]
853+
false: [[AsyncEvaluationOrder]] is UNSET or DONE */
852854
bool async_evaluation;
853855
int64_t async_evaluation_timestamp;
854856
JSModuleDef *cycle_root;
@@ -28849,6 +28851,15 @@ static int js_execute_async_module(JSContext *ctx, JSModuleDef *m);
2884928851
static int js_execute_sync_module(JSContext *ctx, JSModuleDef *m,
2885028852
JSValue *pvalue);
2885128853

28854+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
28855+
static void js_dump_module(JSContext *ctx, const char *str, JSModuleDef *m)
28856+
{
28857+
char buf1[ATOM_GET_STR_BUF_SIZE];
28858+
static const char *module_status_str[] = { "unlinked", "linking", "linked", "evaluating", "evaluating_async", "evaluated" };
28859+
printf("%s: %s status=%s\n", str, JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name), module_status_str[m->status]);
28860+
}
28861+
#endif
28862+
2885228863
static JSValue js_async_module_execution_rejected(JSContext *ctx, JSValueConst this_val,
2885328864
int argc, JSValueConst *argv, int magic,
2885428865
JSValueConst *func_data)
@@ -28857,6 +28868,12 @@ static JSValue js_async_module_execution_rejected(JSContext *ctx, JSValueConst t
2885728868
JSValueConst error = argv[0];
2885828869
int i;
2885928870

28871+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
28872+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
28873+
js_dump_module(ctx, __func__, module);
28874+
}
28875+
#endif
28876+
2886028877
if (js_check_stack_overflow(ctx->rt, 0))
2886128878
return JS_ThrowStackOverflow(ctx);
2886228879

@@ -28872,6 +28889,7 @@ static JSValue js_async_module_execution_rejected(JSContext *ctx, JSValueConst t
2887228889
module->eval_has_exception = true;
2887328890
module->eval_exception = js_dup(error);
2887428891
module->status = JS_MODULE_STATUS_EVALUATED;
28892+
module->async_evaluation = false;
2887528893

2887628894
for(i = 0; i < module->async_parent_modules_count; i++) {
2887728895
JSModuleDef *m = module->async_parent_modules[i];
@@ -28899,6 +28917,12 @@ static JSValue js_async_module_execution_fulfilled(JSContext *ctx, JSValueConst
2889928917
ExecModuleList exec_list_s, *exec_list = &exec_list_s;
2890028918
int i;
2890128919

28920+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
28921+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
28922+
js_dump_module(ctx, __func__, module);
28923+
}
28924+
#endif
28925+
2890228926
if (module->status == JS_MODULE_STATUS_EVALUATED) {
2890328927
assert(module->eval_has_exception);
2890428928
return JS_UNDEFINED;
@@ -28924,6 +28948,11 @@ static JSValue js_async_module_execution_fulfilled(JSContext *ctx, JSValueConst
2892428948

2892528949
for(i = 0; i < exec_list->count; i++) {
2892628950
JSModuleDef *m = exec_list->tab[i];
28951+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
28952+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
28953+
printf(" %d/%d", i, exec_list->count); js_dump_module(ctx, "", m);
28954+
}
28955+
#endif
2892728956
if (m->status == JS_MODULE_STATUS_EVALUATED) {
2892828957
assert(m->eval_has_exception);
2892928958
} else if (m->has_tla) {
@@ -28938,6 +28967,7 @@ static JSValue js_async_module_execution_fulfilled(JSContext *ctx, JSValueConst
2893828967
JS_FreeValue(ctx, m_obj);
2893928968
JS_FreeValue(ctx, error);
2894028969
} else {
28970+
m->async_evaluation = false;
2894128971
js_set_module_evaluated(ctx, m);
2894228972
}
2894328973
}
@@ -28950,6 +28980,11 @@ static int js_execute_async_module(JSContext *ctx, JSModuleDef *m)
2895028980
{
2895128981
JSValue promise, m_obj;
2895228982
JSValue resolve_funcs[2], ret_val;
28983+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
28984+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
28985+
js_dump_module(ctx, __func__, m);
28986+
}
28987+
#endif
2895328988
promise = js_async_function_call(ctx, m->func_obj, JS_UNDEFINED, 0, NULL, 0);
2895428989
if (JS_IsException(promise))
2895528990
return -1;
@@ -28969,6 +29004,11 @@ static int js_execute_async_module(JSContext *ctx, JSModuleDef *m)
2896929004
static int js_execute_sync_module(JSContext *ctx, JSModuleDef *m,
2897029005
JSValue *pvalue)
2897129006
{
29007+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
29008+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
29009+
js_dump_module(ctx, __func__, m);
29010+
}
29011+
#endif
2897229012
if (m->init_func) {
2897329013
/* C module init : no asynchronous execution */
2897429014
if (m->init_func(ctx, m) < 0)
@@ -29008,19 +29048,18 @@ static int js_inner_module_evaluation(JSContext *ctx, JSModuleDef *m,
2900829048
JSModuleDef *m1;
2900929049
int i;
2901029050

29051+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
29052+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
29053+
js_dump_module(ctx, __func__, m);
29054+
}
29055+
#endif
29056+
2901129057
if (js_check_stack_overflow(ctx->rt, 0)) {
2901229058
JS_ThrowStackOverflow(ctx);
2901329059
*pvalue = JS_GetException(ctx);
2901429060
return -1;
2901529061
}
2901629062

29017-
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_RESOLVE
29018-
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_RESOLVE)) {
29019-
char buf1[ATOM_GET_STR_BUF_SIZE];
29020-
printf("js_inner_module_evaluation '%s':\n", JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name));
29021-
}
29022-
#endif
29023-
2902429063
if (m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
2902529064
m->status == JS_MODULE_STATUS_EVALUATED) {
2902629065
if (m->eval_has_exception) {
@@ -29121,6 +29160,12 @@ static JSValue js_evaluate_module(JSContext *ctx, JSModuleDef *m)
2912129160
JSModuleDef *m1, *stack_top;
2912229161
JSValue ret_val, result;
2912329162

29163+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
29164+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
29165+
js_dump_module(ctx, __func__, m);
29166+
}
29167+
#endif
29168+
2912429169
assert(m->status == JS_MODULE_STATUS_LINKED ||
2912529170
m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
2912629171
m->status == JS_MODULE_STATUS_EVALUATED);
@@ -29153,6 +29198,11 @@ static JSValue js_evaluate_module(JSContext *ctx, JSModuleDef *m)
2915329198
1, vc(&m->eval_exception));
2915429199
JS_FreeValue(ctx, ret_val);
2915529200
} else {
29201+
#ifdef ENABLE_DUMPS // JS_DUMP_MODULE_EXEC
29202+
if (check_dump_flag(ctx->rt, JS_DUMP_MODULE_EXEC)) {
29203+
js_dump_module(ctx, " done", m);
29204+
}
29205+
#endif
2915629206
assert(m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
2915729207
m->status == JS_MODULE_STATUS_EVALUATED);
2915829208
assert(!m->eval_has_exception);

quickjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ typedef struct JSMallocFunctions {
425425
#define JS_DUMP_OBJECTS 0x20000 /* dump objects in JS_FreeRuntime */
426426
#define JS_DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */
427427
#define JS_DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */
428+
#define JS_DUMP_MODULE_EXEC 0x100000
428429

429430
// Finalizers run in LIFO order at the very end of JS_FreeRuntime.
430431
// Intended for cleanup of associated resources; the runtime itself

test262_errors.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ test262/test/language/expressions/in/private-field-invalid-assignment-target.js:
7979
test262/test/language/expressions/in/private-field-invalid-assignment-target.js:23: strict mode: unexpected error type: Test262: This statement should not be evaluated.
8080
test262/test/language/expressions/object/computed-property-name-topropertykey-before-value-evaluation.js:31: Test262Error: Expected SameValue(«"bad"», «"ok"») to be true
8181
test262/test/language/expressions/object/computed-property-name-topropertykey-before-value-evaluation.js:31: strict mode: Test262Error: Expected SameValue(«"bad"», «"ok"») to be true
82-
test262/test/language/module-code/top-level-await/module-graphs-does-not-hang.js:10: TypeError: $DONE() not called
8382
test262/test/language/statements/class/elements/syntax/valid/grammar-field-named-get-followed-by-generator-asi.js:40: SyntaxError: invalid property name
8483
test262/test/language/statements/class/elements/syntax/valid/grammar-field-named-get-followed-by-generator-asi.js:40: strict mode: SyntaxError: invalid property name
8584
test262/test/language/statements/class/elements/syntax/valid/grammar-field-named-set-followed-by-generator-asi.js:40: SyntaxError: invalid property name

0 commit comments

Comments
 (0)