remove number and boolean prototypes
This commit is contained in:
300
source/quickjs.c
300
source/quickjs.c
@@ -28065,10 +28065,6 @@ JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
|
||||
|
||||
static JSValue js_string_constructor(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv);
|
||||
static JSValue js_boolean_constructor(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv);
|
||||
static JSValue js_number_constructor(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv);
|
||||
|
||||
static int check_function(JSContext *ctx, JSValueConst obj)
|
||||
{
|
||||
@@ -31623,303 +31619,7 @@ static const JSCFunctionListEntry js_array_iterator_proto_funcs[] = {
|
||||
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Array Iterator", JS_PROP_CONFIGURABLE ),
|
||||
};
|
||||
|
||||
/* Number */
|
||||
|
||||
static JSValue js_number_constructor(JSContext *ctx, JSValueConst new_target,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue val, obj;
|
||||
if (argc == 0) {
|
||||
val = JS_NewInt32(ctx, 0);
|
||||
} else {
|
||||
val = JS_ToNumeric(ctx, argv[0]);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
}
|
||||
if (!JS_IsNull(new_target)) {
|
||||
obj = js_create_from_ctor(ctx, new_target, JS_CLASS_NUMBER);
|
||||
if (!JS_IsException(obj))
|
||||
JS_SetObjectData(ctx, obj, val);
|
||||
return obj;
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
static JSValue js_number_isNaN(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
if (!JS_IsNumber(argv[0]))
|
||||
return JS_FALSE;
|
||||
return js_global_isNaN(ctx, this_val, argc, argv);
|
||||
}
|
||||
|
||||
static JSValue js_number_isFinite(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
if (!JS_IsNumber(argv[0]))
|
||||
return JS_FALSE;
|
||||
return js_global_isFinite(ctx, this_val, argc, argv);
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_number_funcs[] = {
|
||||
/* global ParseInt and parseFloat should be defined already or delayed */
|
||||
JS_ALIAS_BASE_DEF("parseInt", "parseInt", 0 ),
|
||||
JS_ALIAS_BASE_DEF("parseFloat", "parseFloat", 0 ),
|
||||
JS_CFUNC_DEF("isNaN", 1, js_number_isNaN ),
|
||||
JS_CFUNC_DEF("isFinite", 1, js_number_isFinite ),
|
||||
JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
|
||||
};
|
||||
|
||||
static JSValue js_thisNumberValue(JSContext *ctx, JSValueConst this_val)
|
||||
{
|
||||
if (JS_IsNumber(this_val))
|
||||
return JS_DupValue(ctx, this_val);
|
||||
|
||||
if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
|
||||
JSObject *p = JS_VALUE_GET_OBJ(this_val);
|
||||
if (p->class_id == JS_CLASS_NUMBER) {
|
||||
if (JS_IsNumber(p->u.object_data))
|
||||
return JS_DupValue(ctx, p->u.object_data);
|
||||
}
|
||||
}
|
||||
return JS_ThrowTypeError(ctx, "not a number");
|
||||
}
|
||||
|
||||
static JSValue js_number_valueOf(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
return js_thisNumberValue(ctx, this_val);
|
||||
}
|
||||
|
||||
static int js_get_radix(JSContext *ctx, JSValueConst val)
|
||||
{
|
||||
int radix;
|
||||
if (JS_ToInt32Sat(ctx, &radix, val))
|
||||
return -1;
|
||||
if (radix < 2 || radix > 36) {
|
||||
JS_ThrowRangeError(ctx, "radix must be between 2 and 36");
|
||||
return -1;
|
||||
}
|
||||
return radix;
|
||||
}
|
||||
|
||||
static JSValue js_number_toString(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv, int magic)
|
||||
{
|
||||
JSValue val;
|
||||
int base, flags;
|
||||
double d;
|
||||
|
||||
val = js_thisNumberValue(ctx, this_val);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
if (magic || JS_IsNull(argv[0])) {
|
||||
base = 10;
|
||||
} else {
|
||||
base = js_get_radix(ctx, argv[0]);
|
||||
if (base < 0)
|
||||
goto fail;
|
||||
}
|
||||
if (JS_VALUE_GET_TAG(val) == JS_TAG_INT) {
|
||||
char buf1[70];
|
||||
int len;
|
||||
len = i64toa_radix(buf1, JS_VALUE_GET_INT(val), base);
|
||||
return js_new_string8_len(ctx, buf1, len);
|
||||
}
|
||||
if (JS_ToFloat64Free(ctx, &d, val))
|
||||
return JS_EXCEPTION;
|
||||
flags = JS_DTOA_FORMAT_FREE;
|
||||
if (base != 10)
|
||||
flags |= JS_DTOA_EXP_DISABLED;
|
||||
return js_dtoa2(ctx, d, base, 0, flags);
|
||||
fail:
|
||||
JS_FreeValue(ctx, val);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static JSValue js_number_toFixed(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue val;
|
||||
int f, flags;
|
||||
double d;
|
||||
|
||||
val = js_thisNumberValue(ctx, this_val);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
if (JS_ToFloat64Free(ctx, &d, val))
|
||||
return JS_EXCEPTION;
|
||||
if (JS_ToInt32Sat(ctx, &f, argv[0]))
|
||||
return JS_EXCEPTION;
|
||||
if (f < 0 || f > 100)
|
||||
return JS_ThrowRangeError(ctx, "invalid number of digits");
|
||||
if (fabs(d) >= 1e21)
|
||||
flags = JS_DTOA_FORMAT_FREE;
|
||||
else
|
||||
flags = JS_DTOA_FORMAT_FRAC;
|
||||
return js_dtoa2(ctx, d, 10, f, flags);
|
||||
}
|
||||
|
||||
static JSValue js_number_toExponential(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue val;
|
||||
int f, flags;
|
||||
double d;
|
||||
|
||||
val = js_thisNumberValue(ctx, this_val);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
if (JS_ToFloat64Free(ctx, &d, val))
|
||||
return JS_EXCEPTION;
|
||||
if (JS_ToInt32Sat(ctx, &f, argv[0]))
|
||||
return JS_EXCEPTION;
|
||||
if (!isfinite(d)) {
|
||||
return JS_ToStringFree(ctx, __JS_NewFloat64(ctx, d));
|
||||
}
|
||||
if (JS_IsNull(argv[0])) {
|
||||
flags = JS_DTOA_FORMAT_FREE;
|
||||
f = 0;
|
||||
} else {
|
||||
if (f < 0 || f > 100)
|
||||
return JS_ThrowRangeError(ctx, "invalid number of digits");
|
||||
f++;
|
||||
flags = JS_DTOA_FORMAT_FIXED;
|
||||
}
|
||||
return js_dtoa2(ctx, d, 10, f, flags | JS_DTOA_EXP_ENABLED);
|
||||
}
|
||||
|
||||
static JSValue js_number_toPrecision(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue val;
|
||||
int p;
|
||||
double d;
|
||||
|
||||
val = js_thisNumberValue(ctx, this_val);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
if (JS_ToFloat64Free(ctx, &d, val))
|
||||
return JS_EXCEPTION;
|
||||
if (JS_IsNull(argv[0]))
|
||||
goto to_string;
|
||||
if (JS_ToInt32Sat(ctx, &p, argv[0]))
|
||||
return JS_EXCEPTION;
|
||||
if (!isfinite(d)) {
|
||||
to_string:
|
||||
return JS_ToStringFree(ctx, __JS_NewFloat64(ctx, d));
|
||||
}
|
||||
if (p < 1 || p > 100)
|
||||
return JS_ThrowRangeError(ctx, "invalid number of digits");
|
||||
return js_dtoa2(ctx, d, 10, p, JS_DTOA_FORMAT_FIXED);
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_number_proto_funcs[] = {
|
||||
JS_CFUNC_DEF("toExponential", 1, js_number_toExponential ),
|
||||
JS_CFUNC_DEF("toFixed", 1, js_number_toFixed ),
|
||||
JS_CFUNC_DEF("toPrecision", 1, js_number_toPrecision ),
|
||||
JS_CFUNC_MAGIC_DEF("toString", 1, js_number_toString, 0 ),
|
||||
JS_CFUNC_MAGIC_DEF("toLocaleString", 0, js_number_toString, 1 ),
|
||||
JS_CFUNC_DEF("valueOf", 0, js_number_valueOf ),
|
||||
};
|
||||
|
||||
static JSValue js_parseInt(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
const char *str, *p;
|
||||
int radix, flags;
|
||||
JSValue ret;
|
||||
|
||||
str = JS_ToCString(ctx, argv[0]);
|
||||
if (!str)
|
||||
return JS_EXCEPTION;
|
||||
if (JS_ToInt32(ctx, &radix, argv[1])) {
|
||||
JS_FreeCString(ctx, str);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
if (radix != 0 && (radix < 2 || radix > 36)) {
|
||||
ret = JS_NAN;
|
||||
} else {
|
||||
p = str;
|
||||
p += skip_spaces(p);
|
||||
flags = ATOD_INT_ONLY | ATOD_ACCEPT_PREFIX_AFTER_SIGN;
|
||||
ret = js_atof(ctx, p, NULL, radix, flags);
|
||||
}
|
||||
JS_FreeCString(ctx, str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_parseFloat(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
const char *str, *p;
|
||||
JSValue ret;
|
||||
|
||||
str = JS_ToCString(ctx, argv[0]);
|
||||
if (!str)
|
||||
return JS_EXCEPTION;
|
||||
p = str;
|
||||
p += skip_spaces(p);
|
||||
ret = js_atof(ctx, p, NULL, 10, 0);
|
||||
JS_FreeCString(ctx, str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Boolean */
|
||||
static JSValue js_boolean_constructor(JSContext *ctx, JSValueConst new_target,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue val, obj;
|
||||
val = JS_NewBool(ctx, JS_ToBool(ctx, argv[0]));
|
||||
if (!JS_IsNull(new_target)) {
|
||||
obj = js_create_from_ctor(ctx, new_target, JS_CLASS_BOOLEAN);
|
||||
if (!JS_IsException(obj))
|
||||
JS_SetObjectData(ctx, obj, val);
|
||||
return obj;
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
static JSValue js_thisBooleanValue(JSContext *ctx, JSValueConst this_val)
|
||||
{
|
||||
if (JS_VALUE_GET_TAG(this_val) == JS_TAG_BOOL)
|
||||
return JS_DupValue(ctx, this_val);
|
||||
|
||||
if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
|
||||
JSObject *p = JS_VALUE_GET_OBJ(this_val);
|
||||
if (p->class_id == JS_CLASS_BOOLEAN) {
|
||||
if (JS_VALUE_GET_TAG(p->u.object_data) == JS_TAG_BOOL)
|
||||
return p->u.object_data;
|
||||
}
|
||||
}
|
||||
return JS_ThrowTypeError(ctx, "not a boolean");
|
||||
}
|
||||
|
||||
static JSValue js_boolean_toString(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSValue val = js_thisBooleanValue(ctx, this_val);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
return JS_AtomToString(ctx, JS_VALUE_GET_BOOL(val) ?
|
||||
JS_ATOM_true : JS_ATOM_false);
|
||||
}
|
||||
|
||||
static JSValue js_boolean_valueOf(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
return js_thisBooleanValue(ctx, this_val);
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_boolean_proto_funcs[] = {
|
||||
JS_CFUNC_DEF("toString", 0, js_boolean_toString ),
|
||||
JS_CFUNC_DEF("valueOf", 0, js_boolean_valueOf ),
|
||||
};
|
||||
|
||||
/* String */
|
||||
|
||||
static int js_string_get_own_property(JSContext *ctx,
|
||||
JSPropertyDescriptor *desc,
|
||||
JSValueConst obj, JSAtom prop)
|
||||
|
||||
Reference in New Issue
Block a user