rm function proto funcs
This commit is contained in:
162
source/quickjs.c
162
source/quickjs.c
@@ -11601,32 +11601,6 @@ static JSValue js_throw_type_error(JSContext *ctx, JSValueConst this_val,
|
||||
return JS_ThrowTypeError(ctx, "invalid property access");
|
||||
}
|
||||
|
||||
static JSValue js_function_proto_fileName(JSContext *ctx,
|
||||
JSValueConst this_val)
|
||||
{
|
||||
JSFunctionBytecode *b = JS_GetFunctionBytecode(this_val);
|
||||
if (b && b->has_debug) {
|
||||
return JS_AtomToString(ctx, b->debug.filename);
|
||||
}
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
static JSValue js_function_proto_lineNumber(JSContext *ctx,
|
||||
JSValueConst this_val, int is_col)
|
||||
{
|
||||
JSFunctionBytecode *b = JS_GetFunctionBytecode(this_val);
|
||||
if (b && b->has_debug) {
|
||||
int line_num, col_num;
|
||||
line_num = find_line_num(ctx, b, -1, &col_num);
|
||||
if (is_col)
|
||||
return JS_NewInt32(ctx, col_num);
|
||||
else
|
||||
return JS_NewInt32(ctx, line_num);
|
||||
}
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
|
||||
#define GLOBAL_VAR_OFFSET 0x40000000
|
||||
#define ARGUMENT_VAR_OFFSET 0x20000000
|
||||
|
||||
@@ -29375,143 +29349,7 @@ static JSValue js_function_apply(JSContext *ctx, JSValueConst this_val,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_function_call(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
if (argc <= 0) {
|
||||
return JS_Call(ctx, this_val, JS_NULL, 0, NULL);
|
||||
} else {
|
||||
return JS_Call(ctx, this_val, argv[0], argc - 1, argv + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static JSValue js_function_bind(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSBoundFunction *bf;
|
||||
JSValue func_obj, name1, len_val;
|
||||
JSObject *p;
|
||||
int arg_count, i, ret;
|
||||
|
||||
if (check_function(ctx, this_val))
|
||||
return JS_EXCEPTION;
|
||||
|
||||
func_obj = JS_NewObjectProtoClass(ctx, ctx->function_proto,
|
||||
JS_CLASS_BOUND_FUNCTION);
|
||||
if (JS_IsException(func_obj))
|
||||
return JS_EXCEPTION;
|
||||
p = JS_VALUE_GET_OBJ(func_obj);
|
||||
p->is_constructor = JS_IsConstructor(ctx, this_val);
|
||||
arg_count = max_int(0, argc - 1);
|
||||
bf = js_malloc(ctx, sizeof(*bf) + arg_count * sizeof(JSValue));
|
||||
if (!bf)
|
||||
goto exception;
|
||||
bf->func_obj = JS_DupValue(ctx, this_val);
|
||||
bf->this_val = JS_DupValue(ctx, argv[0]);
|
||||
bf->argc = arg_count;
|
||||
for(i = 0; i < arg_count; i++) {
|
||||
bf->argv[i] = JS_DupValue(ctx, argv[i + 1]);
|
||||
}
|
||||
p->u.bound_function = bf;
|
||||
|
||||
/* XXX: the spec could be simpler by only using GetOwnProperty */
|
||||
ret = JS_GetOwnProperty(ctx, NULL, this_val, JS_ATOM_length);
|
||||
if (ret < 0)
|
||||
goto exception;
|
||||
if (!ret) {
|
||||
len_val = JS_NewInt32(ctx, 0);
|
||||
} else {
|
||||
len_val = JS_GetProperty(ctx, this_val, JS_ATOM_length);
|
||||
if (JS_IsException(len_val))
|
||||
goto exception;
|
||||
if (JS_VALUE_GET_TAG(len_val) == JS_TAG_INT) {
|
||||
/* most common case */
|
||||
int len1 = JS_VALUE_GET_INT(len_val);
|
||||
if (len1 <= arg_count)
|
||||
len1 = 0;
|
||||
else
|
||||
len1 -= arg_count;
|
||||
len_val = JS_NewInt32(ctx, len1);
|
||||
} else if (JS_VALUE_GET_NORM_TAG(len_val) == JS_TAG_FLOAT64) {
|
||||
double d = JS_VALUE_GET_FLOAT64(len_val);
|
||||
if (isnan(d)) {
|
||||
d = 0.0;
|
||||
} else {
|
||||
d = trunc(d);
|
||||
if (d <= (double)arg_count)
|
||||
d = 0.0;
|
||||
else
|
||||
d -= (double)arg_count; /* also converts -0 to +0 */
|
||||
}
|
||||
len_val = JS_NewFloat64(ctx, d);
|
||||
} else {
|
||||
JS_FreeValue(ctx, len_val);
|
||||
len_val = JS_NewInt32(ctx, 0);
|
||||
}
|
||||
}
|
||||
JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_length,
|
||||
len_val, JS_PROP_CONFIGURABLE);
|
||||
|
||||
name1 = JS_GetProperty(ctx, this_val, JS_ATOM_name);
|
||||
if (JS_IsException(name1))
|
||||
goto exception;
|
||||
if (!JS_IsString(name1)) {
|
||||
JS_FreeValue(ctx, name1);
|
||||
name1 = JS_AtomToString(ctx, JS_ATOM_empty_string);
|
||||
}
|
||||
name1 = JS_ConcatString3(ctx, "bound ", name1, "");
|
||||
if (JS_IsException(name1))
|
||||
goto exception;
|
||||
JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_name, name1,
|
||||
JS_PROP_CONFIGURABLE);
|
||||
return func_obj;
|
||||
exception:
|
||||
JS_FreeValue(ctx, func_obj);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
JSObject *p;
|
||||
|
||||
if (check_function(ctx, this_val))
|
||||
return JS_EXCEPTION;
|
||||
|
||||
p = JS_VALUE_GET_OBJ(this_val);
|
||||
if (js_class_has_bytecode(p->class_id)) {
|
||||
JSFunctionBytecode *b = p->u.func.function_bytecode;
|
||||
if (b->has_debug && b->debug.source) {
|
||||
return JS_NewStringLen(ctx, b->debug.source, b->debug.source_len);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
JSValue name;
|
||||
const char *pref, *suff;
|
||||
pref = "function ";
|
||||
suff = "() {\n [native code]\n}";
|
||||
/* Get name directly from structure rather than via property access */
|
||||
if (js_class_has_bytecode(p->class_id)) {
|
||||
JSFunctionBytecode *b = p->u.func.function_bytecode;
|
||||
name = JS_AtomToString(ctx, b->func_name);
|
||||
} else {
|
||||
name = JS_AtomToString(ctx, JS_ATOM_empty_string);
|
||||
}
|
||||
if (JS_IsNull(name))
|
||||
name = JS_AtomToString(ctx, JS_ATOM_empty_string);
|
||||
return JS_ConcatString3(ctx, pref, name, suff);
|
||||
}
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_function_proto_funcs[] = {
|
||||
JS_CFUNC_DEF("call", 1, js_function_call ),
|
||||
JS_CFUNC_MAGIC_DEF("apply", 2, js_function_apply, 0 ),
|
||||
JS_CFUNC_DEF("bind", 1, js_function_bind ),
|
||||
JS_CFUNC_DEF("toString", 0, js_function_toString ),
|
||||
JS_CGETSET_DEF("fileName", js_function_proto_fileName, NULL ),
|
||||
JS_CGETSET_MAGIC_DEF("lineNumber", js_function_proto_lineNumber, NULL, 0 ),
|
||||
JS_CGETSET_MAGIC_DEF("columnNumber", js_function_proto_lineNumber, NULL, 1 ),
|
||||
};
|
||||
|
||||
/* Error class */
|
||||
|
||||
Reference in New Issue
Block a user