omdev-cext 0.0.72__tar.gz → 0.0.74__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {omdev_cext-0.0.72/omdev_cext.egg-info → omdev_cext-0.0.74}/PKG-INFO +2 -2
  2. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs.c +322 -85
  3. {omdev_cext-0.0.72 → omdev_cext-0.0.74/omdev_cext.egg-info}/PKG-INFO +2 -2
  4. omdev_cext-0.0.74/omdev_cext.egg-info/requires.txt +1 -0
  5. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/pyproject.toml +2 -2
  6. omdev_cext-0.0.72/omdev_cext.egg-info/requires.txt +0 -1
  7. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/LICENSE +0 -0
  8. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/MANIFEST.in +0 -0
  9. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/README.md +0 -0
  10. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/cexts/_boilerplate.cc +0 -0
  11. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_pyqjsng.c +0 -0
  12. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/builtin-array-fromasync.h +0 -0
  13. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/builtin-iterator-zip-keyed.h +0 -0
  14. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/builtin-iterator-zip.h +0 -0
  15. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/cutils.h +0 -0
  16. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/dtoa.c +0 -0
  17. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/dtoa.h +0 -0
  18. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/libregexp-opcode.h +0 -0
  19. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/libregexp.c +0 -0
  20. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/libregexp.h +0 -0
  21. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/libunicode-table.h +0 -0
  22. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/libunicode.c +0 -0
  23. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/libunicode.h +0 -0
  24. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/list.h +0 -0
  25. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs-atom.h +0 -0
  26. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs-c-atomics.h +0 -0
  27. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs-libc.c +0 -0
  28. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs-libc.h +0 -0
  29. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs-opcode.h +0 -0
  30. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev/js/quickjs/_quickjs/quickjs.h +0 -0
  31. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev_cext.egg-info/SOURCES.txt +0 -0
  32. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev_cext.egg-info/dependency_links.txt +0 -0
  33. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/omdev_cext.egg-info/top_level.txt +0 -0
  34. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/setup.cfg +0 -0
  35. {omdev_cext-0.0.72 → omdev_cext-0.0.74}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: omdev-cext
3
- Version: 0.0.72
3
+ Version: 0.0.74
4
4
  Summary: omdev
5
5
  Author: wrmsr
6
6
  License-Expression: BSD-3-Clause
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.15
14
14
  Requires-Python: >=3.14
15
15
  Description-Content-Type: text/markdown
16
16
  License-File: LICENSE
17
- Requires-Dist: omdev==0.0.72
17
+ Requires-Dist: omdev==0.0.74
18
18
  Dynamic: license-file
19
19
 
20
20
  # Overview
@@ -269,6 +269,14 @@ typedef struct JSMallocState {
269
269
  #define JS_ARENA_MAX_SMALL_SIZE 512
270
270
  #define JS_ARENA_FREE_NIL 0xffff
271
271
 
272
+ // gcc defines __SANITIZE_ADDRESS__ under -fsanitize=address, clang only
273
+ // answers __has_feature(address_sanitizer)
274
+ #if !defined(__SANITIZE_ADDRESS__) && defined(__has_feature)
275
+ #if __has_feature(address_sanitizer)
276
+ #define __SANITIZE_ADDRESS__ 1
277
+ #endif
278
+ #endif
279
+
272
280
  #if defined(__SANITIZE_ADDRESS__) || defined(FORCE_GC_AT_MALLOC)
273
281
  /* Route every allocation through the backing malloc so each block is seen
274
282
  individually: ASan can then poison a freed block, and a GC-stress build
@@ -479,13 +487,13 @@ typedef struct JSVarRef {
479
487
  is itself a GC object, so the cycle collector can see the closure ->
480
488
  var_ref -> coroutine edge and keep the suspended coroutine (hence the
481
489
  captured variable it points into) alive. The coroutine is recovered
482
- as stack_frame->cur_gc_obj (valid while open). This is a *snapshot* of
483
- cur_gc_obj != NULL taken at creation, NOT rederived at read time:
484
- cur_gc_obj transitions NULL -> owner (it is set only after a
485
- generator's initial resume), so var_refs created during the prologue
486
- (e.g. mapped `arguments`) must stay non-coro even though cur_gc_obj is
487
- later set. Cleared when the var_ref is detached; the ref is released on
488
- detach/free. */
490
+ as stack_frame->cur_gc_obj (valid while open). The flag says whether
491
+ this var_ref holds that reference; it is NOT rederived from
492
+ cur_gc_obj at read time. For a generator, cur_gc_obj is only set
493
+ after the initial resume: the var_refs created before that (mapped
494
+ `arguments`, closures in parameter initialisers) are adopted by
495
+ async_func_set_gc_obj(). Cleared when the var_ref is detached; the
496
+ ref is released on detach/free. */
489
497
  uint8_t is_coro;
490
498
  JSValue *pvalue; /* pointer to the value, either on the stack or
491
499
  to 'value' */
@@ -780,6 +788,12 @@ typedef struct JSClosureVar {
780
788
  #define ARG_SCOPE_INDEX 1
781
789
  #define ARG_SCOPE_END (-2)
782
790
 
791
+ // decode the scope operand of OP_eval and OP_apply_eval
792
+ static inline int eval_scope_operand_decode(int operand)
793
+ {
794
+ return operand == 0xFFFF ? ARG_SCOPE_END : operand - 1;
795
+ }
796
+
783
797
  typedef struct JSVarScope {
784
798
  int parent; /* index into fd->scopes of the enclosing scope */
785
799
  int first; /* index into fd->vars of the last variable in this scope */
@@ -1473,6 +1487,7 @@ static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
1473
1487
  const char *input, size_t input_len,
1474
1488
  const char *filename, int line, int flags, int scope_idx);
1475
1489
  static void js_free_module_def(JSContext *ctx, JSModuleDef *m);
1490
+ static void js_unresolve_modules(JSContext *ctx, JSModuleDef *failed);
1476
1491
  static int js_module_attributes_equal(JSContext *ctx, JSValueConst attr1,
1477
1492
  JSValueConst attr2);
1478
1493
  static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m,
@@ -2963,6 +2978,10 @@ typedef enum JSFreeModuleEnum {
2963
2978
  static void js_free_modules(JSContext *ctx, JSFreeModuleEnum flag)
2964
2979
  {
2965
2980
  struct list_head *el, *el1;
2981
+ // a module that requests an unresolved module is not resolved either
2982
+ // and must go with it: it holds a pointer to it
2983
+ if (flag == JS_FREE_MODULE_NOT_RESOLVED)
2984
+ js_unresolve_modules(ctx, NULL);
2966
2985
  list_for_each_safe(el, el1, &ctx->loaded_modules) {
2967
2986
  JSModuleDef *m = list_entry(el, JSModuleDef, link);
2968
2987
  if (flag == JS_FREE_MODULE_ALL ||
@@ -3541,12 +3560,16 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type)
3541
3560
  }
3542
3561
 
3543
3562
  if (str) {
3544
- if (str->atom_type == 0) {
3563
+ if (str->atom_type == 0 && str->kind == JS_STRING_KIND_NORMAL) {
3545
3564
  p = str;
3546
3565
  p->atom_type = atom_type;
3547
3566
  } else {
3548
- p = js_malloc_rt(rt, sizeof(JSString) +
3549
- (str->len << str->is_wide_char) +
3567
+ // 'str' is already an atom of another type, or it is a slice
3568
+ // of another string. Make a flat copy: an atom owns its
3569
+ // characters, and JS_FreeAtomStruct() would not release the
3570
+ // parent of a slice.
3571
+ size_t size = (size_t)str->len << str->is_wide_char;
3572
+ p = js_malloc_rt(rt, sizeof(JSString) + size +
3550
3573
  1 - str->is_wide_char);
3551
3574
  if (unlikely(!p))
3552
3575
  goto fail;
@@ -3557,8 +3580,10 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type)
3557
3580
  #ifdef ENABLE_DUMPS // JS_DUMP_LEAKS
3558
3581
  list_add_tail(&p->link, &rt->string_list);
3559
3582
  #endif
3560
- memcpy(str8(p), str8(str), (str->len << str->is_wide_char) +
3561
- 1 - str->is_wide_char);
3583
+ memcpy(str8(p), str8(str), size);
3584
+ // the characters of a slice are not followed by a null byte
3585
+ if (!str->is_wide_char)
3586
+ str8(p)[size] = '\0';
3562
3587
  js_free_string(rt, str);
3563
3588
  }
3564
3589
  } else {
@@ -5466,7 +5491,9 @@ static JSValue JS_ConcatString2(JSContext *ctx, JSValue op1, JSValue op2)
5466
5491
  if (p2->len == 0) {
5467
5492
  goto ret_op1;
5468
5493
  }
5469
- if (JS_REF_COUNT(p1) == 1 && p1->is_wide_char == p2->is_wide_char
5494
+ // not if p1 is an atom: the atom table finds it by its characters
5495
+ if (JS_REF_COUNT(p1) == 1 && p1->atom_type == 0
5496
+ && p1->is_wide_char == p2->is_wide_char
5470
5497
  && js_malloc_usable_size(ctx, p1) >= sizeof(*p1) + ((p1->len + p2->len) << p2->is_wide_char) + 1 - p1->is_wide_char) {
5471
5498
  /* Concatenate in place in available space at the end of p1 */
5472
5499
  if (p1->is_wide_char) {
@@ -7174,7 +7201,15 @@ static void js_free_value_rt(JSRuntime *rt, JSValue v)
7174
7201
  }
7175
7202
  break;
7176
7203
  case JS_TAG_MODULE:
7177
- abort(); /* never freed here */
7204
+ {
7205
+ // The module list of the context holds a reference, so the
7206
+ // last one can only go away after js_free_module_def() has
7207
+ // released the contents and unlinked the definition.
7208
+ JSModuleDef *m = JS_VALUE_GET_PTR(v);
7209
+ if (m->link.next != NULL)
7210
+ abort();
7211
+ js_free_rt(rt, m);
7212
+ }
7178
7213
  break;
7179
7214
  case JS_TAG_BIG_INT:
7180
7215
  {
@@ -17039,9 +17074,12 @@ static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj,
17039
17074
  return JS_EXCEPTION;
17040
17075
  }
17041
17076
 
17042
- static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
17043
- JSValueConst method,
17044
- int argc, JSValueConst *argv, int *pdone)
17077
+ enum { JS_ITERATOR_NEXT_DROP_VALUE = 1 };
17078
+
17079
+ static JSValue JS_IteratorNext1(JSContext *ctx, JSValueConst enum_obj,
17080
+ JSValueConst method,
17081
+ int argc, JSValueConst *argv,
17082
+ int *pdone, int flags)
17045
17083
  {
17046
17084
  JSValue obj, value, done_val;
17047
17085
  int done;
@@ -17062,7 +17100,7 @@ static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
17062
17100
  goto fail;
17063
17101
  *pdone = JS_ToBoolFree(ctx, done_val);
17064
17102
  value = JS_UNDEFINED;
17065
- if (!*pdone) {
17103
+ if (!*pdone && !(flags & JS_ITERATOR_NEXT_DROP_VALUE)) {
17066
17104
  value = JS_GetProperty(ctx, obj, JS_ATOM_value);
17067
17105
  }
17068
17106
  JS_FreeValue(ctx, obj);
@@ -17074,6 +17112,13 @@ static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
17074
17112
  return JS_EXCEPTION;
17075
17113
  }
17076
17114
 
17115
+ static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
17116
+ JSValueConst method,
17117
+ int argc, JSValueConst *argv, int *pdone)
17118
+ {
17119
+ return JS_IteratorNext1(ctx, enum_obj, method, argc, argv, pdone, 0);
17120
+ }
17121
+
17077
17122
  /* return < 0 in case of exception */
17078
17123
  static int JS_IteratorClose(JSContext *ctx, JSValueConst enum_obj,
17079
17124
  bool is_exception_pending)
@@ -18733,7 +18778,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
18733
18778
  JSValue obj;
18734
18779
  int scope_idx;
18735
18780
  call_argc = get_u16(pc);
18736
- scope_idx = get_u16(pc + 2) - 1;
18781
+ scope_idx = eval_scope_operand_decode(get_u16(pc + 2));
18737
18782
  pc += 4;
18738
18783
  call_argv = sp - call_argc;
18739
18784
  sf->cur_pc = pc;
@@ -18765,7 +18810,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
18765
18810
  JSValue *tab;
18766
18811
  JSValue obj;
18767
18812
 
18768
- scope_idx = get_u16(pc) - 1;
18813
+ scope_idx = eval_scope_operand_decode(get_u16(pc));
18769
18814
  pc += 2;
18770
18815
  sf->cur_pc = pc;
18771
18816
  tab = build_arg_list(ctx, &len, sp[-1]);
@@ -21207,6 +21252,31 @@ static __exception int async_func_init(JSContext *ctx, JSAsyncFunctionState *s,
21207
21252
  return 0;
21208
21253
  }
21209
21254
 
21255
+ // Set the GC object that owns the frame of a generator, once it exists.
21256
+ // The generator object is created after the function prologue has run
21257
+ // (up to OP_initial_yield), and the prologue can already create open
21258
+ // var_refs: it builds the mapped arguments object and evaluates the
21259
+ // parameter initialisers, which may create closures. Those var_refs were
21260
+ // created without an owner. Make them hold a reference to the generator
21261
+ // like the ones created later, otherwise nothing keeps the frame they
21262
+ // point into alive.
21263
+ static void async_func_set_gc_obj(JSRuntime *rt, JSAsyncFunctionState *s,
21264
+ JSGCObjectHeader *gc_obj)
21265
+ {
21266
+ JSStackFrame *sf = &s->frame;
21267
+ int i;
21268
+
21269
+ sf->cur_gc_obj = gc_obj;
21270
+ for(i = 0; i < sf->var_ref_count; i++) {
21271
+ JSVarRef *var_ref = sf->var_refs[i];
21272
+ if (var_ref && !var_ref->is_coro) {
21273
+ var_ref->is_coro = true;
21274
+ JS_REF_COUNT(gc_obj)++;
21275
+ add_gc_object(rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF);
21276
+ }
21277
+ }
21278
+ }
21279
+
21210
21280
  static void async_func_mark(JSRuntime *rt, JSAsyncFunctionState *s,
21211
21281
  JS_MarkFunc *mark_func)
21212
21282
  {
@@ -21432,10 +21502,10 @@ static JSValue js_call_generator_function(JSContext *ctx, JSValueConst func_obj,
21432
21502
  if (JS_IsException(obj))
21433
21503
  goto fail;
21434
21504
  JS_SetOpaqueInternal(obj, s);
21435
- /* the body only starts running on the first next(); root captured
21436
- locals against the generator object from now on (the initial resume
21437
- above only reaches OP_initial_yield, before any user code) */
21438
- s->func_state.frame.cur_gc_obj = &JS_VALUE_GET_OBJ(obj)->header;
21505
+ // root captured locals against the generator object from now on,
21506
+ // including those captured while the prologue ran
21507
+ async_func_set_gc_obj(ctx->rt, &s->func_state,
21508
+ &JS_VALUE_GET_OBJ(obj)->header);
21439
21509
  return obj;
21440
21510
  fail:
21441
21511
  free_generator_stack_rt(ctx->rt, s);
@@ -22088,9 +22158,9 @@ static JSValue js_async_generator_function_call(JSContext *ctx,
22088
22158
  if (JS_IsException(obj))
22089
22159
  goto fail;
22090
22160
  s->generator = JS_VALUE_GET_OBJ(obj);
22091
- /* root captured locals against the async generator object (the initial
22092
- resume above only reaches OP_initial_yield, before any user code) */
22093
- s->func_state.frame.cur_gc_obj = &s->generator->header;
22161
+ // root captured locals against the async generator object from now
22162
+ // on, including those captured while the prologue ran
22163
+ async_func_set_gc_obj(ctx->rt, &s->func_state, &s->generator->header);
22094
22164
  JS_SetOpaqueInternal(obj, s);
22095
22165
  return obj;
22096
22166
  fail:
@@ -22317,6 +22387,11 @@ typedef struct JSFunctionDef {
22317
22387
  bool need_home_object : 1;
22318
22388
  bool use_short_opcodes : 1; /* true if short opcodes are used in byte_code */
22319
22389
  bool has_await : 1; /* true if await is used (used in module eval) */
22390
+ // set when a variable, argument or closure variable could not be
22391
+ // added (limit reached or out of memory). The exception is pending.
22392
+ // Most callers in the variable resolver cannot report a failure, so
22393
+ // js_create_function() checks this instead.
22394
+ bool has_compile_error : 1;
22320
22395
 
22321
22396
  JSFunctionKindEnum func_kind : 8;
22322
22397
  JSParseFunctionEnum func_type : 7;
@@ -24454,18 +24529,21 @@ static int add_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
24454
24529
  // XXX: add_var() should take JSParseState *s and use js_parse_error
24455
24530
  JS_ThrowSyntaxError(ctx, "too many variables declared (only %d allowed)",
24456
24531
  JS_MAX_LOCAL_VARS - 1);
24457
- return -1;
24532
+ goto fail;
24458
24533
  }
24459
24534
  if (js_resize_array(ctx, (void **)&fd->vars, sizeof(fd->vars[0]),
24460
24535
  &fd->var_size, fd->var_count + 1))
24461
- return -1;
24536
+ goto fail;
24462
24537
  vd = &fd->vars[fd->var_count++];
24463
24538
  memset(vd, 0, sizeof(*vd));
24464
24539
  vd->var_name = JS_DupAtom(ctx, name);
24465
24540
  vd->func_pool_idx = -1;
24466
24541
  if (update_var_htab(ctx, fd))
24467
- return -1;
24542
+ goto fail;
24468
24543
  return fd->var_count - 1;
24544
+ fail:
24545
+ fd->has_compile_error = true;
24546
+ return -1;
24469
24547
  }
24470
24548
 
24471
24549
  static int add_scope_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name,
@@ -24538,16 +24616,19 @@ static int add_arg(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
24538
24616
  // XXX: add_arg() should take JSParseState *s and use js_parse_error
24539
24617
  JS_ThrowSyntaxError(ctx, "too many parameters in function definition (only %d allowed)",
24540
24618
  JS_MAX_LOCAL_VARS - 1);
24541
- return -1;
24619
+ goto fail;
24542
24620
  }
24543
24621
  if (js_resize_array(ctx, (void **)&fd->args, sizeof(fd->args[0]),
24544
24622
  &fd->arg_size, fd->arg_count + 1))
24545
- return -1;
24623
+ goto fail;
24546
24624
  vd = &fd->args[fd->arg_count++];
24547
24625
  memset(vd, 0, sizeof(*vd));
24548
24626
  vd->var_name = JS_DupAtom(ctx, name);
24549
24627
  vd->func_pool_idx = -1;
24550
24628
  return fd->arg_count - 1;
24629
+ fail:
24630
+ fd->has_compile_error = true;
24631
+ return -1;
24551
24632
  }
24552
24633
 
24553
24634
  /* add a global variable definition */
@@ -27317,6 +27398,10 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags)
27317
27398
  if (s->token.val == '(') {
27318
27399
  if (!s->cur_func->super_call_allowed)
27319
27400
  return js_parse_error(s, "super() is only valid in a derived class constructor");
27401
+ // `new super()`: the operand of `new` is parsed without the
27402
+ // call, so nothing would be pushed for it
27403
+ if (!accept_lparen)
27404
+ return js_parse_error(s, "'super' keyword unexpected here");
27320
27405
  call_type = FUNC_CALL_SUPER_CTOR;
27321
27406
  } else if (s->token.val == '.' || s->token.val == '[') {
27322
27407
  if (!s->cur_func->super_allowed)
@@ -29096,6 +29181,10 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name,
29096
29181
  bool is_await_using = false;
29097
29182
  if (tok == TOK_AWAIT) {
29098
29183
  int u;
29184
+ // 'await' is also a token in the plain functions of a module and
29185
+ // in class static blocks, where there is nothing to await in
29186
+ if (!(s->cur_func->func_kind & JS_FUNC_ASYNC))
29187
+ return js_parse_error(s, "unexpected 'await' keyword");
29099
29188
  if (next_token(s))
29100
29189
  return -1;
29101
29190
  u = is_using(s, false);
@@ -30381,7 +30470,15 @@ static void js_free_module_def(JSContext *ctx, JSModuleDef *m)
30381
30470
  JS_FreeValue(ctx, m->private_value);
30382
30471
  JS_FreeValue(ctx, m->attributes);
30383
30472
  list_del(&m->link);
30384
- js_free(ctx, m);
30473
+ // JS_TAG_MODULE values hold references to the definition and can
30474
+ // outlive it: the reactions attached to the evaluation promise of a
30475
+ // module with a pending top-level await are only freed with the
30476
+ // runtime, after the context and its module list. Drop the reference
30477
+ // of the list; the memory goes with the last one. Until then leave
30478
+ // nothing dangling behind.
30479
+ memset(m, 0, sizeof(*m));
30480
+ if (--JS_REF_COUNT(m) == 0)
30481
+ js_free(ctx, m);
30385
30482
  }
30386
30483
 
30387
30484
  #ifndef QJS_DISABLE_PARSER
@@ -31364,14 +31461,61 @@ static int js_resolve_module(JSContext *ctx, JSModuleDef *m)
31364
31461
  rme->module_name,
31365
31462
  rme->attributes);
31366
31463
  if (!m1)
31367
- return -1;
31464
+ goto fail;
31368
31465
  rme->module = m1;
31369
31466
  /* already done in js_host_resolve_imported_module() except if
31370
31467
  the module was loaded with JS_EvalBinary() */
31371
31468
  if (js_resolve_module(ctx, m1) < 0)
31372
- return -1;
31469
+ goto fail;
31373
31470
  }
31374
31471
  return 0;
31472
+ fail:
31473
+ // the flag was set early to stop at import cycles
31474
+ m->resolved = false;
31475
+ return -1;
31476
+ }
31477
+
31478
+ // Modules are marked as resolved before their requested modules are, so
31479
+ // that import cycles terminate. When resolution then fails, a module in
31480
+ // such a cycle is left marked as resolved although it requests, directly
31481
+ // or not, a module that is not: reset those.
31482
+ //
31483
+ // 'failed', if not NULL, is a module about to be freed: also remove the
31484
+ // pointers that other modules recorded to it. They are resolved again
31485
+ // when they are next needed.
31486
+ static void js_unresolve_modules(JSContext *ctx, JSModuleDef *failed)
31487
+ {
31488
+ struct list_head *el;
31489
+ bool changed;
31490
+ int i;
31491
+
31492
+ if (failed)
31493
+ failed->resolved = false;
31494
+ do {
31495
+ changed = false;
31496
+ list_for_each(el, &ctx->loaded_modules) {
31497
+ JSModuleDef *m = list_entry(el, JSModuleDef, link);
31498
+ if (!m->resolved)
31499
+ continue;
31500
+ for(i = 0; i < m->req_module_entries_count; i++) {
31501
+ JSModuleDef *m1 = m->req_module_entries[i].module;
31502
+ if (m1 && !m1->resolved) {
31503
+ m->resolved = false;
31504
+ changed = true;
31505
+ break;
31506
+ }
31507
+ }
31508
+ }
31509
+ } while (changed);
31510
+ if (failed) {
31511
+ list_for_each(el, &ctx->loaded_modules) {
31512
+ JSModuleDef *m = list_entry(el, JSModuleDef, link);
31513
+ for(i = 0; i < m->req_module_entries_count; i++) {
31514
+ if (m->req_module_entries[i].module == failed)
31515
+ m->req_module_entries[i].module = NULL;
31516
+ }
31517
+ }
31518
+ }
31375
31519
  }
31376
31520
 
31377
31521
  static JSVarRef *js_create_module_var(JSContext *ctx, bool is_lexical)
@@ -32277,7 +32421,7 @@ static int js_execute_async_module(JSContext *ctx, JSModuleDef *m)
32277
32421
  return -1;
32278
32422
  m_obj = JS_NewModuleValue(ctx, m);
32279
32423
  resolve_funcs[0] = JS_NewCFunctionData(ctx, js_async_module_execution_fulfilled, 0, 0, 1, vc(&m_obj));
32280
- resolve_funcs[1] = JS_NewCFunctionData(ctx, js_async_module_execution_rejected, 0, 0, 1, vc(&m_obj));
32424
+ resolve_funcs[1] = JS_NewCFunctionData(ctx, js_async_module_execution_rejected, 1, 0, 1, vc(&m_obj));
32281
32425
  ret_val = js_promise_then(ctx, promise, 2, vc(resolve_funcs));
32282
32426
  JS_FreeValue(ctx, ret_val);
32283
32427
  JS_FreeValue(ctx, m_obj);
@@ -33683,13 +33827,13 @@ static int add_closure_var(JSContext *ctx, JSFunctionDef *s,
33683
33827
  // XXX: add_closure_var() should take JSParseState *s and use js_parse_error
33684
33828
  JS_ThrowSyntaxError(ctx, "too many closure variables used (only %d allowed)",
33685
33829
  JS_MAX_LOCAL_VARS - 1);
33686
- return -1;
33830
+ goto fail;
33687
33831
  }
33688
33832
 
33689
33833
  if (js_resize_array(ctx, (void **)&s->closure_var,
33690
33834
  sizeof(s->closure_var[0]),
33691
33835
  &s->closure_var_size, s->closure_var_count + 1))
33692
- return -1;
33836
+ goto fail;
33693
33837
  cv = &s->closure_var[s->closure_var_count++];
33694
33838
  cv->closure_type = closure_type;
33695
33839
  cv->is_const = is_const;
@@ -33698,6 +33842,9 @@ static int add_closure_var(JSContext *ctx, JSFunctionDef *s,
33698
33842
  cv->var_idx = var_idx;
33699
33843
  cv->var_name = JS_DupAtom(ctx, var_name);
33700
33844
  return s->closure_var_count - 1;
33845
+ fail:
33846
+ s->has_compile_error = true;
33847
+ return -1;
33701
33848
  }
33702
33849
 
33703
33850
  static int find_closure_var(JSContext *ctx, JSFunctionDef *s,
@@ -33726,8 +33873,10 @@ static int get_closure_var(JSContext *ctx, JSFunctionDef *s,
33726
33873
  var_idx = get_closure_var(ctx, s->parent, fd, closure_type,
33727
33874
  var_idx, var_name,
33728
33875
  is_const, is_lexical, var_kind);
33729
- if (var_idx < 0)
33876
+ if (var_idx < 0) {
33877
+ s->has_compile_error = true;
33730
33878
  return -1;
33879
+ }
33731
33880
  if (closure_type != JS_CLOSURE_GLOBAL_REF)
33732
33881
  closure_type = JS_CLOSURE_REF;
33733
33882
  }
@@ -33923,6 +34072,17 @@ static void var_object_test(JSContext *ctx, JSFunctionDef *s,
33923
34072
  static inline void capture_var(JSFunctionDef *s, JSVarDef *vd)
33924
34073
  {
33925
34074
  if (!vd->is_captured) {
34075
+ // the variable reference indexes and their count are stored on
34076
+ // 16 bits. Arguments and variables are limited separately, so
34077
+ // together they can exceed that.
34078
+ if (s->var_ref_count >= JS_MAX_LOCAL_VARS) {
34079
+ if (!s->has_compile_error) {
34080
+ JS_ThrowSyntaxError(s->ctx, "too many captured variables (only %d allowed)",
34081
+ JS_MAX_LOCAL_VARS);
34082
+ s->has_compile_error = true;
34083
+ }
34084
+ return;
34085
+ }
33926
34086
  vd->is_captured = 1;
33927
34087
  vd->var_ref_idx = s->var_ref_count++;
33928
34088
  }
@@ -34818,6 +34978,15 @@ static __exception int add_closure_variables(JSContext *ctx, JSFunctionDef *s,
34818
34978
  cv->var_idx = i;
34819
34979
  cv->var_name = JS_DupAtom(ctx, cv0->var_name);
34820
34980
  }
34981
+ // same limit as add_closure_var(): the indexes and the count are
34982
+ // stored on 16 bits. The arguments, variables and closure variables
34983
+ // of the calling function are limited separately, so their sum can
34984
+ // exceed it.
34985
+ if (s->closure_var_count > JS_MAX_LOCAL_VARS) {
34986
+ JS_ThrowSyntaxError(ctx, "too many closure variables used (only %d allowed)",
34987
+ JS_MAX_LOCAL_VARS - 1);
34988
+ return -1;
34989
+ }
34821
34990
  return 0;
34822
34991
  }
34823
34992
 
@@ -35174,6 +35343,25 @@ static int get_label_pos(JSFunctionDef *s, int label)
35174
35343
 
35175
35344
  /* convert global variable accesses to local variables or closure
35176
35345
  variables when necessary */
35346
+ // The scope operand of OP_eval and OP_apply_eval is the index of the first
35347
+ // variable of the scope, stored on 16 bits as index + 1 so that -1 (no
35348
+ // variable) fits. 0xFFFF stands for ARG_SCOPE_END, the end of an argument
35349
+ // scope that has no variable of its own; see eval_scope_operand_decode().
35350
+ // Return -1 if the index cannot be encoded.
35351
+ #define EVAL_SCOPE_ARG_END 0xFFFF
35352
+
35353
+ static int eval_scope_operand(JSContext *ctx, int first)
35354
+ {
35355
+ if (first == ARG_SCOPE_END)
35356
+ return EVAL_SCOPE_ARG_END;
35357
+ if (first + 1 >= EVAL_SCOPE_ARG_END) {
35358
+ JS_ThrowSyntaxError(ctx, "too many variables declared (only %d allowed)",
35359
+ JS_MAX_LOCAL_VARS - 1);
35360
+ return -1;
35361
+ }
35362
+ return first + 1;
35363
+ }
35364
+
35177
35365
  static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s)
35178
35366
  {
35179
35367
  int pos, pos_next, bc_len, op, len, i, idx, line_num, col_num;
@@ -35242,18 +35430,28 @@ static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s)
35242
35430
  case OP_eval: /* convert scope index to adjusted variable index */
35243
35431
  {
35244
35432
  int call_argc = get_u16(bc_buf + pos + 1);
35433
+ int operand;
35245
35434
  scope = get_u16(bc_buf + pos + 1 + 2);
35246
35435
  mark_eval_captured_variables(ctx, s, scope);
35436
+ operand = eval_scope_operand(ctx, s->scopes[scope].first);
35437
+ if (operand < 0)
35438
+ goto fail;
35247
35439
  dbuf_putc(&bc_out, op);
35248
35440
  dbuf_put_u16(&bc_out, call_argc);
35249
- dbuf_put_u16(&bc_out, s->scopes[scope].first + 1);
35441
+ dbuf_put_u16(&bc_out, operand);
35250
35442
  }
35251
35443
  break;
35252
35444
  case OP_apply_eval: /* convert scope index to adjusted variable index */
35253
- scope = get_u16(bc_buf + pos + 1);
35254
- mark_eval_captured_variables(ctx, s, scope);
35255
- dbuf_putc(&bc_out, op);
35256
- dbuf_put_u16(&bc_out, s->scopes[scope].first + 1);
35445
+ {
35446
+ int operand;
35447
+ scope = get_u16(bc_buf + pos + 1);
35448
+ mark_eval_captured_variables(ctx, s, scope);
35449
+ operand = eval_scope_operand(ctx, s->scopes[scope].first);
35450
+ if (operand < 0)
35451
+ goto fail;
35452
+ dbuf_putc(&bc_out, op);
35453
+ dbuf_put_u16(&bc_out, operand);
35454
+ }
35257
35455
  break;
35258
35456
  case OP_scope_get_var_undef:
35259
35457
  case OP_scope_get_var:
@@ -37097,6 +37295,8 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd)
37097
37295
 
37098
37296
  if (resolve_variables(ctx, fd))
37099
37297
  goto fail;
37298
+ if (fd->has_compile_error)
37299
+ goto fail;
37100
37300
 
37101
37301
  #ifdef ENABLE_DUMPS // JS_DUMP_BYTECODE_PASS2
37102
37302
  if (check_dump_flag(ctx->rt, JS_DUMP_BYTECODE_PASS2)) {
@@ -37112,6 +37312,8 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd)
37112
37312
 
37113
37313
  if (resolve_labels(ctx, fd))
37114
37314
  goto fail;
37315
+ if (fd->has_compile_error)
37316
+ goto fail;
37115
37317
 
37116
37318
  if (compute_stack_size(ctx, fd, &stack_size) < 0)
37117
37319
  goto fail;
@@ -37673,6 +37875,8 @@ static __exception int js_parse_function_decl2(JSParseState *s,
37673
37875
  } else {
37674
37876
  /* unnamed arg for destructuring */
37675
37877
  idx = add_arg(ctx, fd, JS_ATOM_NULL);
37878
+ if (idx < 0)
37879
+ goto fail;
37676
37880
  emit_op(s, OP_get_arg);
37677
37881
  emit_u16(s, idx);
37678
37882
  }
@@ -38320,9 +38524,12 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
38320
38524
  }
38321
38525
  return ret_val;
38322
38526
  fail1:
38323
- /* XXX: should free all the unresolved dependencies */
38324
- if (m)
38527
+ if (m) {
38528
+ // modules that were resolved while this one was being resolved
38529
+ // (import cycles) may point to it
38530
+ js_unresolve_modules(ctx, m);
38325
38531
  js_free_module_def(ctx, m);
38532
+ }
38326
38533
  return JS_EXCEPTION;
38327
38534
  }
38328
38535
 
@@ -40026,10 +40233,9 @@ static JSValue JS_ReadModule(BCReaderState *s)
40026
40233
  m = js_new_module_def(ctx, module_name);
40027
40234
  if (!m)
40028
40235
  goto fail;
40029
- obj = js_dup(JS_MKPTR(JS_TAG_MODULE, m));
40236
+ obj = JS_NewModuleValue(ctx, m);
40030
40237
  if (bc_get_leb128_int(s, &m->req_module_entries_count))
40031
40238
  goto fail;
40032
- obj = JS_NewModuleValue(ctx, m);
40033
40239
  if (m->req_module_entries_count != 0) {
40034
40240
  m->req_module_entries_size = m->req_module_entries_count;
40035
40241
  m->req_module_entries = js_mallocz(ctx, sizeof(m->req_module_entries[0]) * m->req_module_entries_size);
@@ -40126,6 +40332,7 @@ static JSValue JS_ReadModule(BCReaderState *s)
40126
40332
  fail:
40127
40333
  if (m) {
40128
40334
  js_free_module_def(ctx, m);
40335
+ JS_FreeValue(ctx, obj);
40129
40336
  }
40130
40337
  return JS_EXCEPTION;
40131
40338
  }
@@ -46248,16 +46455,15 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
46248
46455
  goto close;
46249
46456
  while (it->count > 0) {
46250
46457
  it->count--;
46251
- item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone);
46458
+ item = JS_IteratorNext1(ctx, it->obj, it->next, 0, NULL, pdone,
46459
+ JS_ITERATOR_NEXT_DROP_VALUE);
46252
46460
  if (JS_IsException(item))
46253
46461
  goto step_fail;
46254
- JS_FreeValue(ctx, item);
46255
46462
  if (*pdone) {
46256
46463
  ret = JS_UNDEFINED;
46257
46464
  goto done;
46258
46465
  }
46259
46466
  }
46260
-
46261
46467
  item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone);
46262
46468
  if (JS_IsException(item))
46263
46469
  goto step_fail;
@@ -47330,6 +47536,8 @@ slow_path:
47330
47536
  static int string_cmp(JSString *p1, JSString *p2, int x1, int x2, int len)
47331
47537
  {
47332
47538
  int i, c1, c2;
47539
+ if (!p1->is_wide_char && !p2->is_wide_char)
47540
+ return memcmp(str8(p1) + x1, str8(p2) + x2, len);
47333
47541
  for (i = 0; i < len; i++) {
47334
47542
  if ((c1 = string_get(p1, x1 + i)) != (c2 = string_get(p2, x2 + i)))
47335
47543
  return c1 - c2;
@@ -47347,11 +47555,10 @@ static int string_indexof_char(JSString *p, int c, int from)
47347
47555
  return i;
47348
47556
  }
47349
47557
  } else {
47350
- if ((c & ~0xff) == 0) {
47351
- for (i = from; i < len; i++) {
47352
- if (str8(p)[i] == (uint8_t)c)
47353
- return i;
47354
- }
47558
+ if ((c & ~0xff) == 0 && from < len) {
47559
+ const uint8_t *q = memchr(str8(p) + from, c, len - from);
47560
+ if (q)
47561
+ return q - str8(p);
47355
47562
  }
47356
47563
  }
47357
47564
  return -1;
@@ -47505,7 +47712,10 @@ static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val,
47505
47712
  inc = 1;
47506
47713
  }
47507
47714
  ret = -1;
47508
- if (len >= v_len && inc * (stop - start) >= 0) {
47715
+ if (inc > 0) {
47716
+ if (len >= v_len && start <= stop)
47717
+ ret = string_indexof(p, p1, start);
47718
+ } else if (len >= v_len && inc * (stop - start) >= 0) {
47509
47719
  for (i = start;; i += inc) {
47510
47720
  if (!string_cmp(p, p1, i, 0, v_len)) {
47511
47721
  ret = i;
@@ -47532,7 +47742,7 @@ static JSValue js_string_includes(JSContext *ctx, JSValueConst this_val,
47532
47742
  int argc, JSValueConst *argv, int magic)
47533
47743
  {
47534
47744
  JSValue str, v = JS_UNDEFINED;
47535
- int i, len, v_len, pos, start, stop, ret;
47745
+ int len, v_len, pos, start, stop, ret;
47536
47746
  JSString *p;
47537
47747
  JSString *p1;
47538
47748
 
@@ -47572,14 +47782,10 @@ static JSValue js_string_includes(JSContext *ctx, JSValueConst this_val,
47572
47782
  start = stop = pos;
47573
47783
  }
47574
47784
  if (start >= 0 && start <= stop) {
47575
- for (i = start;; i++) {
47576
- if (!string_cmp(p, p1, i, 0, v_len)) {
47577
- ret = 1;
47578
- break;
47579
- }
47580
- if (i == stop)
47581
- break;
47582
- }
47785
+ if (magic == 0)
47786
+ ret = string_indexof(p, p1, start) >= 0;
47787
+ else
47788
+ ret = !string_cmp(p, p1, start, 0, v_len);
47583
47789
  }
47584
47790
  done:
47585
47791
  JS_FreeValue(ctx, str);
@@ -48317,16 +48523,14 @@ static JSValue js_string_localeCompare(JSContext *ctx, JSValueConst this_val,
48317
48523
  goto exception;
48318
48524
 
48319
48525
  // TODO(bnoordhuis) skip normalization when input is latin1
48320
- an = unicode_normalize(&ts, as, an, UNICODE_NFC, ctx,
48321
- (DynBufReallocFunc *)js_realloc);
48526
+ an = unicode_normalize(&ts, as, an, UNICODE_NFC, ctx, js_dbuf_realloc);
48322
48527
  if (an == -1)
48323
48528
  goto exception;
48324
48529
  js_free(ctx, as);
48325
48530
  as = ts;
48326
48531
 
48327
48532
  // TODO(bnoordhuis) skip normalization when input is latin1
48328
- bn = unicode_normalize(&ts, bs, bn, UNICODE_NFC, ctx,
48329
- (DynBufReallocFunc *)js_realloc);
48533
+ bn = unicode_normalize(&ts, bs, bn, UNICODE_NFC, ctx, js_dbuf_realloc);
48330
48534
  if (bn == -1)
48331
48535
  goto exception;
48332
48536
  js_free(ctx, bs);
@@ -48468,7 +48672,7 @@ static JSValue js_string_normalize(JSContext *ctx, JSValueConst this_val,
48468
48672
  }
48469
48673
 
48470
48674
  out_len = unicode_normalize(&out_buf, buf, buf_len, n_type,
48471
- ctx->rt, (DynBufReallocFunc *)js_realloc_rt);
48675
+ ctx, js_dbuf_realloc);
48472
48676
  js_free(ctx, buf);
48473
48677
  if (out_len < 0)
48474
48678
  return JS_EXCEPTION;
@@ -53776,14 +53980,21 @@ static JSValue js_map_clear(JSContext *ctx, JSValueConst this_val,
53776
53980
  int argc, JSValueConst *argv, int magic)
53777
53981
  {
53778
53982
  JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
53779
- struct list_head *el, *el1;
53983
+ struct list_head *el;
53780
53984
  JSMapRecord *mr;
53781
53985
 
53782
53986
  if (!s)
53783
53987
  return JS_EXCEPTION;
53784
- list_for_each_safe(el, el1, &s->records) {
53988
+ el = s->records.next;
53989
+ while (el != &s->records) {
53785
53990
  mr = list_entry(el, JSMapRecord, link);
53991
+ // Freeing the key or the value can free other records: a map
53992
+ // iterator may be the last holder of an already deleted record.
53993
+ // Keep 'mr' alive and read its successor only afterwards.
53994
+ mr->ref_count++;
53786
53995
  map_delete_record(ctx->rt, s, mr);
53996
+ el = el->next;
53997
+ map_decref_record(ctx->rt, mr);
53787
53998
  }
53788
53999
  return JS_UNDEFINED;
53789
54000
  }
@@ -57599,7 +57810,7 @@ static JSValue set_date_field(JSContext *ctx, JSValueConst this_val,
57599
57810
  {
57600
57811
  // _field(obj, first_field, end_field, args, is_local)
57601
57812
  double fields[9];
57602
- int res, first_field, end_field, is_local, i, n;
57813
+ int res, res1, first_field, end_field, is_local, i, n;
57603
57814
  double d, a;
57604
57815
 
57605
57816
  d = NAN;
@@ -57610,6 +57821,7 @@ static JSValue set_date_field(JSContext *ctx, JSValueConst this_val,
57610
57821
  res = get_date_fields(ctx, this_val, fields, is_local, first_field == 0);
57611
57822
  if (res < 0)
57612
57823
  return JS_EXCEPTION;
57824
+ res1 = res;
57613
57825
 
57614
57826
  // Argument coercion is observable and must be done unconditionally.
57615
57827
  n = min_int(argc, end_field - first_field);
@@ -57621,10 +57833,11 @@ static JSValue set_date_field(JSContext *ctx, JSValueConst this_val,
57621
57833
  fields[first_field + i] = trunc(a);
57622
57834
  }
57623
57835
 
57624
- if (!res)
57625
- return JS_NAN;
57836
+ if (!res1)
57837
+ return JS_NAN; /* thisTimeValue is NaN: leave it */
57626
57838
 
57627
- if (argc > 0)
57839
+ /* a non-finite argument makes the new time value NaN */
57840
+ if (res && argc > 0)
57628
57841
  d = set_date_fields(fields, is_local);
57629
57842
 
57630
57843
  return JS_SetThisTimeValue(ctx, this_val, d);
@@ -60060,7 +60273,7 @@ JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
60060
60273
  if (pbyte_offset)
60061
60274
  *pbyte_offset = ta->offset;
60062
60275
  if (pbyte_length)
60063
- *pbyte_length = ta->length;
60276
+ *pbyte_length = (size_t)p->u.array.count << typed_array_size_log2(p->class_id);
60064
60277
  if (pbytes_per_element) {
60065
60278
  *pbytes_per_element = 1 << typed_array_size_log2(p->class_id);
60066
60279
  }
@@ -60088,7 +60301,7 @@ uint8_t *JS_GetUint8Array(JSContext *ctx, size_t *psize, JSValueConst obj)
60088
60301
  ta = p->u.typed_array;
60089
60302
  abuf = ta->buffer->u.array_buffer;
60090
60303
 
60091
- *psize = ta->length;
60304
+ *psize = p->u.array.count;
60092
60305
  return abuf->data + ta->offset;
60093
60306
  fail:
60094
60307
  *psize = 0;
@@ -63373,18 +63586,31 @@ static JSValue js_finrec_unregister(JSContext *ctx, JSValueConst this_val, int a
63373
63586
  return JS_ThrowTypeError(ctx, "invalid unregister token");
63374
63587
 
63375
63588
  struct list_head *el, *el1;
63376
- bool removed = false;
63589
+ struct list_head removed;
63590
+ bool found;
63591
+
63592
+ // First pass: unlink the matching entries and drop their weak
63593
+ // references. Freeing a held value can free the target of another
63594
+ // entry of this registry, which would otherwise unlink and free that
63595
+ // entry while we iterate.
63596
+ init_list_head(&removed);
63377
63597
  list_for_each_safe(el, el1, &frd->entries) {
63378
63598
  JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link);
63379
63599
  if (js_same_value(ctx, fre->token, token)) {
63380
63600
  list_del(&fre->link);
63381
63601
  delete_finrec_weakref(ctx->rt, fre);
63382
- js_finrec_free(ctx->rt, fre);
63383
- removed = true;
63602
+ list_add_tail(&fre->link, &removed);
63384
63603
  }
63385
63604
  }
63605
+ found = !list_empty(&removed);
63606
+ // Second pass: free them. Nothing else can reach 'removed'.
63607
+ list_for_each_safe(el, el1, &removed) {
63608
+ JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link);
63609
+ list_del(&fre->link);
63610
+ js_finrec_free(ctx->rt, fre);
63611
+ }
63386
63612
 
63387
- return js_bool(removed);
63613
+ return js_bool(found);
63388
63614
  }
63389
63615
 
63390
63616
  static const JSCFunctionListEntry js_finrec_proto_funcs[] = {
@@ -63609,7 +63835,19 @@ static void js_new_callsite_data(JSContext *ctx, JSCallSiteData *csd, JSStackFra
63609
63835
  JSObject *p;
63610
63836
 
63611
63837
  csd->constructor = sf->is_constructor;
63612
- csd->func = js_dup(sf->cur_func);
63838
+ p = JS_VALUE_GET_OBJ(sf->cur_func);
63839
+ // Do not hand out functions that script cannot obtain otherwise and
63840
+ // that are not meant to be called by it: the engine's own closures
63841
+ // (promise reactions, async generator continuations, ...) and the
63842
+ // body of a module, which is async bytecode in a plain function
63843
+ // object and only runs correctly through js_async_function_call().
63844
+ if (p->class_id == JS_CLASS_C_FUNCTION_DATA ||
63845
+ (p->class_id == JS_CLASS_BYTECODE_FUNCTION &&
63846
+ p->u.func.function_bytecode->func_kind != JS_FUNC_NORMAL)) {
63847
+ csd->func = JS_UNDEFINED;
63848
+ } else {
63849
+ csd->func = js_dup(sf->cur_func);
63850
+ }
63613
63851
  /* func_name_str is UTF-8 encoded if needed */
63614
63852
  func_name_str = get_func_name(ctx, sf->cur_func);
63615
63853
  if (!func_name_str || func_name_str[0] == '\0')
@@ -63620,7 +63858,6 @@ static void js_new_callsite_data(JSContext *ctx, JSCallSiteData *csd, JSStackFra
63620
63858
  if (JS_IsException(csd->func_name))
63621
63859
  csd->func_name = JS_NULL;
63622
63860
 
63623
- p = JS_VALUE_GET_OBJ(sf->cur_func);
63624
63861
  if (js_class_has_bytecode(p->class_id)) {
63625
63862
  JSFunctionBytecode *b = p->u.func.function_bytecode;
63626
63863
  int line_num1, col_num1;
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: omdev-cext
3
- Version: 0.0.72
3
+ Version: 0.0.74
4
4
  Summary: omdev
5
5
  Author: wrmsr
6
6
  License-Expression: BSD-3-Clause
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.15
14
14
  Requires-Python: >=3.14
15
15
  Description-Content-Type: text/markdown
16
16
  License-File: LICENSE
17
- Requires-Dist: omdev==0.0.72
17
+ Requires-Dist: omdev==0.0.74
18
18
  Dynamic: license-file
19
19
 
20
20
  # Overview
@@ -0,0 +1 @@
1
+ omdev==0.0.74
@@ -13,7 +13,7 @@ urls = {source = 'https://github.com/wrmsr/om'}
13
13
  license = 'BSD-3-Clause'
14
14
  readme = 'README.md'
15
15
  requires-python = '>=3.14'
16
- version = '0.0.72'
16
+ version = '0.0.74'
17
17
  classifiers = [
18
18
  'Intended Audience :: Developers',
19
19
  'Operating System :: OS Independent',
@@ -24,7 +24,7 @@ classifiers = [
24
24
  ]
25
25
  description = 'omdev'
26
26
  dependencies = [
27
- 'omdev == 0.0.72',
27
+ 'omdev == 0.0.74',
28
28
  ]
29
29
 
30
30
  [tool.setuptools]
@@ -1 +0,0 @@
1
- omdev==0.0.72
File without changes
File without changes
File without changes
File without changes
File without changes