add array length accessor

This commit is contained in:
2025-02-24 11:22:34 -06:00
parent 2563e78e6d
commit ec219aeb38
2 changed files with 28 additions and 14 deletions

View File

@@ -167,7 +167,6 @@ typedef enum JSErrorEnum {
#define __exception __attribute__((warn_unused_result)) #define __exception __attribute__((warn_unused_result))
typedef struct JSShape JSShape;
typedef struct JSString JSString; typedef struct JSString JSString;
typedef struct JSString JSAtomStruct; typedef struct JSString JSAtomStruct;
typedef struct JSObject JSObject; typedef struct JSObject JSObject;
@@ -5071,6 +5070,13 @@ static __maybe_unused void JS_DumpShapes(JSRuntime *rt)
fprintf(dumpout, "}\n"); fprintf(dumpout, "}\n");
} }
JSShape *JS_ObjectShape(JSContext *js, JSValue v)
{
JSObject *p = JS_VALUE_GET_OBJ(v);
if (!p || !p->shape) return NULL;
return p->shape;
}
static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID class_id) static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID class_id)
{ {
JSObject *p; JSObject *p;
@@ -13431,6 +13437,19 @@ int JS_IsArray(JSContext *ctx, JSValueConst val)
} }
} }
int JS_ArrayLength(JSContext *ctx, JSValueConst val)
{
if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
JSObject *p = JS_VALUE_GET_OBJ(val);
if (p->class_id != JS_CLASS_ARRAY) return 0;
uint32_t len;
js_get_length32(ctx, &len, val);
return len;
}
return 0;
}
static double js_pow(double a, double b) static double js_pow(double a, double b)
{ {
if (unlikely(!isfinite(b)) && fabs(a) == 1) { if (unlikely(!isfinite(b)) && fabs(a) == 1) {
@@ -14882,12 +14901,6 @@ static __exception int js_has_unscopable(JSContext *ctx, JSValueConst obj,
return ret; return ret;
} }
static JSAtom js_atom_typeof(JSContext *js, JSValueConst o)
{
JSObject *p = JS_VALUE_GET_OBJ(o);
return js->rt->class_array[p->class_id].class_name;
}
static __exception int js_operator_instanceof(JSContext *ctx, JSValue *sp) static __exception int js_operator_instanceof(JSContext *ctx, JSValue *sp)
{ {
JSValue op1, op2; JSValue op1, op2;
@@ -16455,8 +16468,8 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
} }
b = p->u.func.function_bytecode; b = p->u.func.function_bytecode;
// if (unlikely(ctx->fn_start_hook)) if (unlikely(caller_ctx->fn_start_hook))
// ctx->fn_start_hook(ctx,func_obj); caller_ctx->fn_start_hook(caller_ctx,func_obj);
if (unlikely(argc < b->arg_count || (flags & JS_CALL_FLAG_COPY_ARGV))) { if (unlikely(argc < b->arg_count || (flags & JS_CALL_FLAG_COPY_ARGV))) {
arg_allocated_size = b->arg_count; arg_allocated_size = b->arg_count;
@@ -19016,7 +19029,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
// ctx->fn_end_hook(ctx,func_obj); // ctx->fn_end_hook(ctx,func_obj);
return ret_val; return ret_val;
} }
JSValue JS_Call(JSContext *ctx, JSValueConst func_obj, JSValueConst this_obj, JSValue JS_Call(JSContext *ctx, JSValueConst func_obj, JSValueConst this_obj,
@@ -54946,8 +54959,6 @@ JSValue js_debugger_backtrace_fns(JSContext *ctx, const uint8_t *cur_pc)
{ {
JSValue ret = JS_NewArray(ctx); JSValue ret = JS_NewArray(ctx);
JSStackFrame *sf; JSStackFrame *sf;
const char *func_name_str;
JSObject *p;
uint32_t stack_index = 0; uint32_t stack_index = 0;
for(sf = ctx->rt->current_stack_frame; sf != NULL; sf = sf->prev_frame) { for(sf = ctx->rt->current_stack_frame; sf != NULL; sf = sf->prev_frame) {

View File

@@ -50,6 +50,7 @@ extern "C" {
typedef struct JSRuntime JSRuntime; typedef struct JSRuntime JSRuntime;
typedef struct JSContext JSContext; typedef struct JSContext JSContext;
typedef struct JSClass JSClass; typedef struct JSClass JSClass;
typedef struct JSShape JSShape;
typedef uint32_t JSClassID; typedef uint32_t JSClassID;
typedef uint32_t JSAtom; typedef uint32_t JSAtom;
@@ -783,7 +784,7 @@ static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v)
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
p->ref_count++; p->ref_count++;
} }
return (JSValue)v; return v;
} }
static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v) static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
@@ -792,7 +793,7 @@ static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
p->ref_count++; p->ref_count++;
} }
return (JSValue)v; return v;
} }
JS_BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2); JS_BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2);
@@ -843,6 +844,8 @@ JS_BOOL JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, JS_BOOL val)
JSValue JS_NewArray(JSContext *ctx); JSValue JS_NewArray(JSContext *ctx);
int JS_IsArray(JSContext *ctx, JSValueConst val); int JS_IsArray(JSContext *ctx, JSValueConst val);
int JS_ArrayLength(JSContext *js, JSValueConst val);
JSShape *JS_ObjectShape(JSContext *js, JSValueConst val);
JSValue JS_NewDate(JSContext *ctx, double epoch_ms); JSValue JS_NewDate(JSContext *ctx, double epoch_ms);