rm top level fns
This commit is contained in:
317
source/quickjs.c
317
source/quickjs.c
@@ -29533,170 +29533,6 @@ fail:
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static JSValue js_array_from(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
// from(items, mapfn = void 0, this_arg = void 0)
|
||||
JSValueConst items = argv[0], mapfn, this_arg;
|
||||
JSValueConst args[2];
|
||||
JSValue iter, r, v, v2, arrayLike, next_method, enum_obj;
|
||||
int64_t k, len;
|
||||
int done, mapping;
|
||||
|
||||
mapping = FALSE;
|
||||
mapfn = JS_NULL;
|
||||
this_arg = JS_NULL;
|
||||
r = JS_NULL;
|
||||
arrayLike = JS_NULL;
|
||||
iter = JS_NULL;
|
||||
enum_obj = JS_NULL;
|
||||
next_method = JS_NULL;
|
||||
|
||||
if (argc > 1) {
|
||||
mapfn = argv[1];
|
||||
if (!JS_IsNull(mapfn)) {
|
||||
if (check_function(ctx, mapfn))
|
||||
goto exception;
|
||||
mapping = 1;
|
||||
if (argc > 2)
|
||||
this_arg = argv[2];
|
||||
}
|
||||
}
|
||||
iter = JS_GetProperty(ctx, items, JS_ATOM_Symbol_iterator);
|
||||
if (JS_IsException(iter))
|
||||
goto exception;
|
||||
if (!JS_IsNull(iter) && !JS_IsNull(iter)) {
|
||||
if (!JS_IsFunction(ctx, iter)) {
|
||||
JS_ThrowTypeError(ctx, "value is not iterable");
|
||||
goto exception;
|
||||
}
|
||||
if (JS_IsConstructor(ctx, this_val))
|
||||
r = JS_CallConstructor(ctx, this_val, 0, NULL);
|
||||
else
|
||||
r = JS_NewArray(ctx);
|
||||
if (JS_IsException(r))
|
||||
goto exception;
|
||||
enum_obj = JS_GetIterator2(ctx, items, iter);
|
||||
if (JS_IsException(enum_obj))
|
||||
goto exception;
|
||||
next_method = JS_GetProperty(ctx, enum_obj, JS_ATOM_next);
|
||||
if (JS_IsException(next_method))
|
||||
goto exception;
|
||||
for (k = 0;; k++) {
|
||||
v = JS_IteratorNext(ctx, enum_obj, next_method, 0, NULL, &done);
|
||||
if (JS_IsException(v))
|
||||
goto exception;
|
||||
if (done)
|
||||
break;
|
||||
if (mapping) {
|
||||
args[0] = v;
|
||||
args[1] = JS_NewInt32(ctx, k);
|
||||
v2 = JS_Call(ctx, mapfn, this_arg, 2, args);
|
||||
JS_FreeValue(ctx, v);
|
||||
v = v2;
|
||||
if (JS_IsException(v))
|
||||
goto exception_close;
|
||||
}
|
||||
if (JS_DefinePropertyValueInt64(ctx, r, k, v,
|
||||
JS_PROP_C_W_E | JS_PROP_THROW) < 0)
|
||||
goto exception_close;
|
||||
}
|
||||
} else {
|
||||
arrayLike = JS_ToObject(ctx, items);
|
||||
if (JS_IsException(arrayLike))
|
||||
goto exception;
|
||||
if (js_get_length64(ctx, &len, arrayLike) < 0)
|
||||
goto exception;
|
||||
v = JS_NewInt64(ctx, len);
|
||||
args[0] = v;
|
||||
if (JS_IsConstructor(ctx, this_val)) {
|
||||
r = JS_CallConstructor(ctx, this_val, 1, args);
|
||||
} else {
|
||||
r = js_array_constructor(ctx, JS_NULL, 1, args);
|
||||
}
|
||||
JS_FreeValue(ctx, v);
|
||||
if (JS_IsException(r))
|
||||
goto exception;
|
||||
for(k = 0; k < len; k++) {
|
||||
v = JS_GetPropertyInt64(ctx, arrayLike, k);
|
||||
if (JS_IsException(v))
|
||||
goto exception;
|
||||
if (mapping) {
|
||||
args[0] = v;
|
||||
args[1] = JS_NewInt32(ctx, k);
|
||||
v2 = JS_Call(ctx, mapfn, this_arg, 2, args);
|
||||
JS_FreeValue(ctx, v);
|
||||
v = v2;
|
||||
if (JS_IsException(v))
|
||||
goto exception;
|
||||
}
|
||||
if (JS_DefinePropertyValueInt64(ctx, r, k, v,
|
||||
JS_PROP_C_W_E | JS_PROP_THROW) < 0)
|
||||
goto exception;
|
||||
}
|
||||
}
|
||||
if (JS_SetProperty(ctx, r, JS_ATOM_length, JS_NewUint32(ctx, k)) < 0)
|
||||
goto exception;
|
||||
goto done;
|
||||
|
||||
exception_close:
|
||||
JS_IteratorClose(ctx, enum_obj, TRUE);
|
||||
exception:
|
||||
JS_FreeValue(ctx, r);
|
||||
r = JS_EXCEPTION;
|
||||
done:
|
||||
JS_FreeValue(ctx, arrayLike);
|
||||
JS_FreeValue(ctx, iter);
|
||||
JS_FreeValue(ctx, enum_obj);
|
||||
JS_FreeValue(ctx, next_method);
|
||||
return r;
|
||||
}
|
||||
|
||||
static JSValue js_array_of(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue obj, args[1];
|
||||
int i;
|
||||
|
||||
if (JS_IsConstructor(ctx, this_val)) {
|
||||
args[0] = JS_NewInt32(ctx, argc);
|
||||
obj = JS_CallConstructor(ctx, this_val, 1, (JSValueConst *)args);
|
||||
} else {
|
||||
obj = JS_NewArray(ctx);
|
||||
}
|
||||
if (JS_IsException(obj))
|
||||
return JS_EXCEPTION;
|
||||
for(i = 0; i < argc; i++) {
|
||||
if (JS_CreateDataPropertyUint32(ctx, obj, i, JS_DupValue(ctx, argv[i]),
|
||||
JS_PROP_THROW) < 0) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewUint32(ctx, argc)) < 0) {
|
||||
fail:
|
||||
JS_FreeValue(ctx, obj);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
static JSValue js_array_isArray(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
int ret;
|
||||
ret = JS_IsArray(ctx, argv[0]);
|
||||
if (ret < 0)
|
||||
return JS_EXCEPTION;
|
||||
else
|
||||
return JS_NewBool(ctx, ret);
|
||||
}
|
||||
|
||||
static JSValue js_get_this(JSContext *ctx,
|
||||
JSValueConst this_val)
|
||||
{
|
||||
return JS_DupValue(ctx, this_val);
|
||||
}
|
||||
|
||||
static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj,
|
||||
JSValueConst len_val)
|
||||
{
|
||||
@@ -29743,13 +29579,6 @@ static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj,
|
||||
}
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_array_funcs[] = {
|
||||
JS_CFUNC_DEF("isArray", 1, js_array_isArray ),
|
||||
JS_CFUNC_DEF("from", 1, js_array_from ),
|
||||
JS_CFUNC_DEF("of", 0, js_array_of ),
|
||||
JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
|
||||
};
|
||||
|
||||
static int JS_isConcatSpreadable(JSContext *ctx, JSValueConst obj)
|
||||
{
|
||||
JSValue val;
|
||||
@@ -34587,7 +34416,6 @@ done:
|
||||
|
||||
static const JSCFunctionListEntry js_regexp_funcs[] = {
|
||||
JS_CFUNC_DEF("escape", 1, js_regexp_escape ),
|
||||
JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
|
||||
};
|
||||
|
||||
static const JSCFunctionListEntry js_regexp_proto_funcs[] = {
|
||||
@@ -35779,20 +35607,6 @@ static JSValue js_cell_number_remainder(JSContext *ctx, JSValueConst this_val,
|
||||
return JS_NewFloat64(ctx, dividend - (trunc(dividend / divisor) * divisor));
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_cell_number_funcs[] = {
|
||||
JS_CFUNC_DEF("whole", 1, js_cell_number_whole),
|
||||
JS_CFUNC_DEF("fraction", 1, js_cell_number_fraction),
|
||||
JS_CFUNC_DEF("floor", 2, js_cell_number_floor),
|
||||
JS_CFUNC_DEF("ceiling", 2, js_cell_number_ceiling),
|
||||
JS_CFUNC_DEF("abs", 1, js_cell_number_abs),
|
||||
JS_CFUNC_DEF("round", 2, js_cell_number_round),
|
||||
JS_CFUNC_DEF("sign", 1, js_cell_number_sign),
|
||||
JS_CFUNC_DEF("trunc", 2, js_cell_number_trunc),
|
||||
JS_CFUNC_DEF("min", 0, js_cell_number_min),
|
||||
JS_CFUNC_DEF("max", 0, js_cell_number_max),
|
||||
JS_CFUNC_DEF("remainder", 2, js_cell_number_remainder),
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* text function and sub-functions
|
||||
* ---------------------------------------------------------------------------- */
|
||||
@@ -36093,22 +35907,34 @@ static JSValue js_cell_text(JSContext *ctx, JSValueConst this_val,
|
||||
if (argc == 1)
|
||||
return JS_DupValue(ctx, arg);
|
||||
|
||||
/* text(string, from, to) - substring */
|
||||
if (argc >= 3) {
|
||||
int from, to;
|
||||
JSString *p = JS_VALUE_GET_STRING(arg);
|
||||
int len = p->len;
|
||||
JSString *p = JS_VALUE_GET_STRING(arg);
|
||||
int len = p->len;
|
||||
|
||||
/* text(string, from) - substring from index to end */
|
||||
/* text(string, from, to) - substring */
|
||||
if (argc >= 2 && (JS_VALUE_GET_TAG(argv[1]) == JS_TAG_INT ||
|
||||
JS_VALUE_GET_TAG(argv[1]) == JS_TAG_FLOAT64)) {
|
||||
int from;
|
||||
if (JS_ToInt32(ctx, &from, argv[1]))
|
||||
return JS_NULL;
|
||||
if (JS_ToInt32(ctx, &to, argv[2]))
|
||||
return JS_NULL;
|
||||
|
||||
/* Adjust negative indices */
|
||||
/* Adjust negative index */
|
||||
if (from < 0) from += len;
|
||||
if (to < 0) to += len;
|
||||
if (from < 0) from = 0;
|
||||
if (from > len) from = len;
|
||||
|
||||
if (from < 0 || from > to || to > len)
|
||||
int to = len; /* Default: to end of string */
|
||||
if (argc >= 3) {
|
||||
if (JS_ToInt32(ctx, &to, argv[2]))
|
||||
return JS_NULL;
|
||||
|
||||
/* Adjust negative index */
|
||||
if (to < 0) to += len;
|
||||
if (to < 0) to = 0;
|
||||
if (to > len) to = len;
|
||||
}
|
||||
|
||||
if (from > to)
|
||||
return JS_NULL;
|
||||
|
||||
return js_sub_string(ctx, p, from, to);
|
||||
@@ -36644,15 +36470,6 @@ fail:
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_cell_text_funcs[] = {
|
||||
JS_CFUNC_DEF("lower", 1, js_cell_text_lower),
|
||||
JS_CFUNC_DEF("upper", 1, js_cell_text_upper),
|
||||
JS_CFUNC_DEF("trim", 2, js_cell_text_trim),
|
||||
JS_CFUNC_DEF("codepoint", 1, js_cell_text_codepoint),
|
||||
JS_CFUNC_DEF("search", 3, js_cell_text_search),
|
||||
JS_CFUNC_DEF("replace", 4, js_cell_text_replace),
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* array function and sub-functions
|
||||
* ---------------------------------------------------------------------------- */
|
||||
@@ -37407,14 +37224,6 @@ static JSValue js_cell_array_sort(JSContext *ctx, JSValueConst this_val,
|
||||
return result;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_cell_array_funcs[] = {
|
||||
JS_CFUNC_DEF("reduce", 4, js_cell_array_reduce),
|
||||
JS_CFUNC_DEF("for", 4, js_cell_array_for),
|
||||
JS_CFUNC_DEF("find", 4, js_cell_array_find),
|
||||
JS_CFUNC_DEF("filter", 2, js_cell_array_filter),
|
||||
JS_CFUNC_DEF("sort", 2, js_cell_array_sort),
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* object function and sub-functions
|
||||
* ---------------------------------------------------------------------------- */
|
||||
@@ -37680,10 +37489,6 @@ static JSValue js_cell_fn_apply(JSContext *ctx, JSValueConst this_val,
|
||||
return result;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_cell_fn_funcs[] = {
|
||||
JS_CFUNC_DEF("apply", 2, js_cell_fn_apply),
|
||||
};
|
||||
|
||||
/* ============================================================================
|
||||
* Blob Intrinsic Type
|
||||
* ============================================================================ */
|
||||
@@ -39008,8 +38813,6 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
obj = JS_NewGlobalCConstructor(ctx, "Array", js_array_constructor, 1,
|
||||
ctx->class_proto[JS_CLASS_ARRAY]);
|
||||
ctx->array_ctor = JS_DupValue(ctx, obj);
|
||||
JS_SetPropertyFunctionList(ctx, obj, js_array_funcs,
|
||||
countof(js_array_funcs));
|
||||
|
||||
/* XXX: create auto_initializer */
|
||||
{
|
||||
@@ -39090,17 +38893,14 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
/* Cell Script global functions: text, number, array, object, fn */
|
||||
{
|
||||
JSValue text_func = JS_NewCFunction(ctx, js_cell_text, "text", 2);
|
||||
JS_SetPropertyFunctionList(ctx, text_func, js_cell_text_funcs, countof(js_cell_text_funcs));
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "text", text_func,
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
JSValue number_func = JS_NewCFunction(ctx, js_cell_number, "number", 2);
|
||||
JS_SetPropertyFunctionList(ctx, number_func, js_cell_number_funcs, countof(js_cell_number_funcs));
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "number", number_func,
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
JSValue array_func = JS_NewCFunction(ctx, js_cell_array, "array", 4);
|
||||
JS_SetPropertyFunctionList(ctx, array_func, js_cell_array_funcs, countof(js_cell_array_funcs));
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "array", array_func,
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
@@ -39110,7 +38910,6 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
JSValue fn_obj = JS_NewObject(ctx);
|
||||
JS_SetPropertyFunctionList(ctx, fn_obj, js_cell_fn_funcs, countof(js_cell_fn_funcs));
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "fn", fn_obj,
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
@@ -39186,6 +38985,78 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
|
||||
JS_NewCFunction(ctx, js_cell_is_proto, "is_proto", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "apply",
|
||||
JS_NewCFunction(ctx, js_cell_fn_apply, "apply", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "replace",
|
||||
JS_NewCFunction(ctx, js_cell_text_replace, "replace", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "lower",
|
||||
JS_NewCFunction(ctx, js_cell_text_lower, "lower", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "upper",
|
||||
JS_NewCFunction(ctx, js_cell_text_upper, "upper", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "trim",
|
||||
JS_NewCFunction(ctx, js_cell_text_trim, "trim", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "codepoint",
|
||||
JS_NewCFunction(ctx, js_cell_text_codepoint, "codepoint", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "search",
|
||||
JS_NewCFunction(ctx, js_cell_text_search, "search", 3),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "reduce",
|
||||
JS_NewCFunction(ctx, js_cell_array_reduce, "reduce", 4),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "for",
|
||||
JS_NewCFunction(ctx, js_cell_array_for, "for", 4),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "find",
|
||||
JS_NewCFunction(ctx, js_cell_array_find, "find", 4),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "filter",
|
||||
JS_NewCFunction(ctx, js_cell_array_filter, "filter", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "sort",
|
||||
JS_NewCFunction(ctx, js_cell_array_sort, "sort", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
/* Number utility functions as globals */
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "whole",
|
||||
JS_NewCFunction(ctx, js_cell_number_whole, "whole", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "fraction",
|
||||
JS_NewCFunction(ctx, js_cell_number_fraction, "fraction", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "floor",
|
||||
JS_NewCFunction(ctx, js_cell_number_floor, "floor", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "ceiling",
|
||||
JS_NewCFunction(ctx, js_cell_number_ceiling, "ceiling", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "abs",
|
||||
JS_NewCFunction(ctx, js_cell_number_abs, "abs", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "round",
|
||||
JS_NewCFunction(ctx, js_cell_number_round, "round", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "sign",
|
||||
JS_NewCFunction(ctx, js_cell_number_sign, "sign", 1),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "trunc",
|
||||
JS_NewCFunction(ctx, js_cell_number_trunc, "trunc", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "min",
|
||||
JS_NewCFunction(ctx, js_cell_number_min, "min", 0),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "max",
|
||||
JS_NewCFunction(ctx, js_cell_number_max, "max", 0),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "remainder",
|
||||
JS_NewCFunction(ctx, js_cell_number_remainder, "remainder", 2),
|
||||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
|
||||
|
||||
/* reverse() - reverse an array */
|
||||
JS_DefinePropertyValueStr(ctx, ctx->global_obj, "reverse",
|
||||
JS_NewCFunction(ctx, js_cell_reverse, "reverse", 1),
|
||||
|
||||
Reference in New Issue
Block a user