rm array proto funcs
This commit is contained in:
226
source/quickjs.c
226
source/quickjs.c
@@ -27078,11 +27078,6 @@ static JSValue js_error_toString(JSContext *ctx, JSValueConst this_val,
|
||||
return JS_ConcatString(ctx, name, msg);
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_error_proto_funcs[] = {
|
||||
JS_PROP_STRING_DEF("name", "Error", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
|
||||
JS_PROP_STRING_DEF("message", "", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
|
||||
};
|
||||
|
||||
/* Array */
|
||||
|
||||
static int JS_CopySubArray(JSContext *ctx,
|
||||
@@ -27182,27 +27177,6 @@ fail:
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj,
|
||||
JSValueConst len_val)
|
||||
{
|
||||
/* Always use the array constructor - no species/constructor lookup */
|
||||
return js_array_constructor(ctx, JS_NULL, 1, &len_val);
|
||||
}
|
||||
|
||||
static int JS_isConcatSpreadable(JSContext *ctx, JSValueConst obj)
|
||||
{
|
||||
JSValue val;
|
||||
|
||||
if (!JS_IsObject(obj))
|
||||
return FALSE;
|
||||
val = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_isConcatSpreadable);
|
||||
if (JS_IsException(val))
|
||||
return -1;
|
||||
if (!JS_IsNull(val))
|
||||
return JS_ToBoolFree(ctx, val);
|
||||
return JS_IsArray(ctx, obj);
|
||||
}
|
||||
|
||||
static JSValue js_array_includes(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
@@ -27343,113 +27317,6 @@ static JSValue js_array_push(JSContext *ctx, JSValueConst this_val,
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv, int splice)
|
||||
{
|
||||
JSValue obj, arr, val, len_val;
|
||||
int64_t len, start, k, final, n, count, del_count, new_len;
|
||||
int kPresent;
|
||||
JSValue *arrp;
|
||||
uint32_t count32, i, item_count;
|
||||
|
||||
arr = JS_NULL;
|
||||
obj = JS_ToObject(ctx, this_val);
|
||||
if (js_get_length64(ctx, &len, obj))
|
||||
goto exception;
|
||||
|
||||
if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len))
|
||||
goto exception;
|
||||
|
||||
if (splice) {
|
||||
if (argc == 0) {
|
||||
item_count = 0;
|
||||
del_count = 0;
|
||||
} else
|
||||
if (argc == 1) {
|
||||
item_count = 0;
|
||||
del_count = len - start;
|
||||
} else {
|
||||
item_count = argc - 2;
|
||||
if (JS_ToInt64Clamp(ctx, &del_count, argv[1], 0, len - start, 0))
|
||||
goto exception;
|
||||
}
|
||||
if (len + item_count - del_count > MAX_SAFE_INTEGER) {
|
||||
JS_ThrowTypeError(ctx, "Array loo long");
|
||||
goto exception;
|
||||
}
|
||||
count = del_count;
|
||||
} else {
|
||||
item_count = 0; /* avoid warning */
|
||||
final = len;
|
||||
if (!JS_IsNull(argv[1])) {
|
||||
if (JS_ToInt64Clamp(ctx, &final, argv[1], 0, len, len))
|
||||
goto exception;
|
||||
}
|
||||
count = max_int64(final - start, 0);
|
||||
}
|
||||
len_val = JS_NewInt64(ctx, count);
|
||||
arr = JS_ArraySpeciesCreate(ctx, obj, len_val);
|
||||
JS_FreeValue(ctx, len_val);
|
||||
if (JS_IsException(arr))
|
||||
goto exception;
|
||||
|
||||
k = start;
|
||||
final = start + count;
|
||||
n = 0;
|
||||
/* The fast array test on arr ensures that
|
||||
JS_CreateDataPropertyUint32() won't modify obj in case arr is
|
||||
an exotic object */
|
||||
/* Special case fast arrays */
|
||||
if (js_get_fast_array(ctx, obj, &arrp, &count32) &&
|
||||
js_is_fast_array(ctx, arr)) {
|
||||
/* XXX: should share code with fast array constructor */
|
||||
for (; k < final && k < count32; k++, n++) {
|
||||
if (JS_CreateDataPropertyUint32(ctx, arr, n, JS_DupValue(ctx, arrp[k]), JS_PROP_THROW) < 0)
|
||||
goto exception;
|
||||
}
|
||||
}
|
||||
/* Copy the remaining elements if any (handle case of inherited properties) */
|
||||
for (; k < final; k++, n++) {
|
||||
kPresent = JS_TryGetPropertyInt64(ctx, obj, k, &val);
|
||||
if (kPresent < 0)
|
||||
goto exception;
|
||||
if (kPresent) {
|
||||
if (JS_CreateDataPropertyUint32(ctx, arr, n, val, JS_PROP_THROW) < 0)
|
||||
goto exception;
|
||||
}
|
||||
}
|
||||
if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, n)) < 0)
|
||||
goto exception;
|
||||
|
||||
if (splice) {
|
||||
new_len = len + item_count - del_count;
|
||||
if (item_count != del_count) {
|
||||
if (JS_CopySubArray(ctx, obj, start + item_count,
|
||||
start + del_count, len - (start + del_count),
|
||||
item_count <= del_count ? +1 : -1) < 0)
|
||||
goto exception;
|
||||
|
||||
for (k = len; k-- > new_len; ) {
|
||||
if (JS_DeletePropertyInt64(ctx, obj, k, JS_PROP_THROW) < 0)
|
||||
goto exception;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < item_count; i++) {
|
||||
if (JS_SetPropertyInt64(ctx, obj, start + i, JS_DupValue(ctx, argv[i + 2])) < 0)
|
||||
goto exception;
|
||||
}
|
||||
if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewInt64(ctx, new_len)) < 0)
|
||||
goto exception;
|
||||
}
|
||||
JS_FreeValue(ctx, obj);
|
||||
return arr;
|
||||
|
||||
exception:
|
||||
JS_FreeValue(ctx, obj);
|
||||
JS_FreeValue(ctx, arr);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static int64_t JS_FlattenIntoArray(JSContext *ctx, JSValueConst target,
|
||||
JSValueConst source, int64_t sourceLen,
|
||||
int64_t targetIndex, int depth,
|
||||
@@ -27529,15 +27396,6 @@ static JSValue js_create_array(JSContext *ctx, int len, JSValueConst *tab)
|
||||
return obj;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_array_proto_funcs[] = {
|
||||
JS_CFUNC_MAGIC_DEF("pop", 0, js_array_pop, 0 ),
|
||||
JS_CFUNC_MAGIC_DEF("push", 1, js_array_push, 0 ),
|
||||
JS_CFUNC_MAGIC_DEF("shift", 0, js_array_pop, 1 ),
|
||||
JS_CFUNC_MAGIC_DEF("unshift", 1, js_array_push, 1 ),
|
||||
JS_CFUNC_MAGIC_DEF("slice", 2, js_array_slice, 0 ),
|
||||
JS_CFUNC_MAGIC_DEF("splice", 2, js_array_slice, 1 ),
|
||||
};
|
||||
|
||||
/* String */
|
||||
static int js_string_get_own_property(JSContext *ctx,
|
||||
JSPropertyDescriptor *desc,
|
||||
@@ -32761,6 +32619,71 @@ static JSValue js_cell_reverse(JSContext *ctx, JSValueConst this_val,
|
||||
* proto() function - get prototype of an object
|
||||
* ============================================================================ */
|
||||
|
||||
static JSValue js_cell_pop(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
if (argc < 1)
|
||||
return JS_NULL;
|
||||
|
||||
JSValue obj = argv[0];
|
||||
if (!JS_IsArray(ctx, obj))
|
||||
return JS_NULL;
|
||||
|
||||
JSValue length_val = JS_GetPropertyStr(ctx, obj, "length");
|
||||
int64_t len;
|
||||
if (JS_ToInt64(ctx, &len, length_val) < 0) {
|
||||
JS_FreeValue(ctx, length_val);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
JS_FreeValue(ctx, length_val);
|
||||
|
||||
if (len <= 0)
|
||||
return JS_NULL;
|
||||
|
||||
JSValue val = JS_GetPropertyInt64(ctx, obj, len - 1);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
|
||||
if (JS_DeletePropertyInt64(ctx, obj, len - 1, JS_PROP_THROW) < 0) {
|
||||
JS_FreeValue(ctx, val);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
if (JS_SetPropertyStr(ctx, obj, "length", JS_NewInt64(ctx, len - 1)) < 0) {
|
||||
JS_FreeValue(ctx, val);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
static JSValue js_cell_push(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
if (argc < 1)
|
||||
return JS_NULL;
|
||||
|
||||
JSValue obj = argv[0];
|
||||
if (!JS_IsArray(ctx, obj))
|
||||
return JS_NULL;
|
||||
|
||||
JSValue length_val = JS_GetPropertyStr(ctx, obj, "length");
|
||||
int64_t len;
|
||||
if (JS_ToInt64(ctx, &len, length_val) < 0) {
|
||||
JS_FreeValue(ctx, length_val);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
JS_FreeValue(ctx, length_val);
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (JS_SetPropertyInt64(ctx, obj, len, JS_DupValue(ctx, argv[i])) < 0)
|
||||
return JS_EXCEPTION;
|
||||
len++;
|
||||
}
|
||||
|
||||
return JS_NewInt64(ctx, len);
|
||||
}
|
||||
|
||||
static JSValue js_cell_proto(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
@@ -33251,10 +33174,6 @@ static void JS_AddIntrinsicBasicObjects(JSContext *ctx)
|
||||
ctx->class_proto[JS_CLASS_BYTECODE_FUNCTION] = JS_DupValue(ctx, ctx->function_proto);
|
||||
ctx->class_proto[JS_CLASS_ERROR] = JS_NewObject(ctx);
|
||||
|
||||
JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_ERROR],
|
||||
js_error_proto_funcs,
|
||||
countof(js_error_proto_funcs));
|
||||
|
||||
for(i = 0; i < JS_NATIVE_ERROR_COUNT; i++) {
|
||||
proto = JS_NewObjectProto(ctx, ctx->class_proto[JS_CLASS_ERROR]);
|
||||
JS_DefinePropertyValue(ctx, proto, JS_ATOM_name,
|
||||
@@ -33325,10 +33244,6 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
}
|
||||
|
||||
/* Array */
|
||||
JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_ARRAY],
|
||||
js_array_proto_funcs,
|
||||
countof(js_array_proto_funcs));
|
||||
|
||||
obj = JS_NewGlobalCConstructor(ctx, "Array", js_array_constructor, 1,
|
||||
ctx->class_proto[JS_CLASS_ARRAY]);
|
||||
ctx->array_ctor = JS_DupValue(ctx, obj);
|
||||
@@ -33584,6 +33499,17 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "pi",
|
||||
JS_NewFloat64(ctx, 3.14159265358979323846264338327950288419716939937510),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
/* push() - add element to end of array */
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "push",
|
||||
JS_NewCFunction(ctx, js_cell_push, "push", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
/* pop() - remove and return last element of array */
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "pop",
|
||||
JS_NewCFunction(ctx, js_cell_pop, "pop", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user