diff --git a/include/ant.h b/include/ant.h index 921e01f..67eae86 100644 --- a/include/ant.h +++ b/include/ant.h @@ -160,6 +160,7 @@ void js_setup_import_meta(ant_t *js, const char *filename); void js_process_promise_handlers(ant_t *js, ant_value_t promise); void js_mark_promise_trigger_dequeued(ant_t *js, ant_value_t promise); bool js_mark_promise_trigger_queued(ant_t *js, ant_value_t promise); +bool js_try_get_own_data_prop(ant_t *js, ant_value_t obj, const char *key, size_t key_len, ant_value_t *out); void js_reject_promise(ant_t *js, ant_value_t promise, ant_value_t value); void js_resolve_promise(ant_t *js, ant_value_t promise, ant_value_t value); diff --git a/src/ant.c b/src/ant.c index f6ab130..f085130 100644 --- a/src/ant.c +++ b/src/ant.c @@ -3132,6 +3132,35 @@ static inline bool lookup_string_prop_meta(ant_t *js, ant_value_t cur_obj, const return lookup_prop_meta(js, cur_obj, PROP_META_STRING, key, klen, 0, out); } +bool js_try_get_own_data_prop(ant_t *js, ant_value_t obj, const char *key, size_t key_len, ant_value_t *out) { + if (out) *out = js_mkundef(); + if (!key || !out) return false; + + uint8_t t = vtype(obj); + if (t == T_FUNC) obj = js_func_obj(obj); + else if (t != T_OBJ && t != T_ARR) return false; + + ant_value_t as_obj = js_as_obj(obj); + if (is_proxy(as_obj)) return false; + + prop_meta_t meta; + bool has_meta = lookup_string_prop_meta(js, as_obj, key, key_len, &meta); + if (has_meta && (meta.has_getter || meta.has_setter)) return false; + + ant_offset_t off = lkp(js, as_obj, key, (ant_offset_t)key_len); + if (off != 0) { + *out = propref_load(js, off); + return true; + } + + if (array_obj_ptr(as_obj) && is_length_key(key, (ant_offset_t)key_len)) { + *out = tov((double)get_array_length(js, as_obj)); + return true; + } + + return false; +} + ant_value_t js_setprop(ant_t *js, ant_value_t obj, ant_value_t k, ant_value_t v) { uint8_t ot = vtype(obj); diff --git a/src/esm/commonjs.c b/src/esm/commonjs.c index 793b6cb..188bcff 100644 --- a/src/esm/commonjs.c +++ b/src/esm/commonjs.c @@ -83,9 +83,9 @@ static ant_value_t esm_populate_cjs_namespace(ant_t *js, ant_value_t ns, ant_val while (js_prop_iter_next(&iter, &key, &key_len, NULL)) { if (key_len == 7 && memcmp(key, "default", 7) == 0) continue; - - ant_value_t value = js_get(js, exports_val, key); - if (is_err(value)) { js_prop_iter_end(&iter); return value; } + + ant_value_t value = js_mkundef(); + if (!js_try_get_own_data_prop(js, exports_val, key, key_len, &value)) continue; ant_value_t res = setprop_cstr(js, ns, key, key_len, value); if (is_err(res)) { js_prop_iter_end(&iter); return res; }