v8 源代码中 ArrayMap 函数的 callbackfn 参数是什么?
What is the callbackfn argument of ArrayMap function in v8's source code?
内置数组-gen.cc
TF_BUILTIN(ArrayMap, ArrayBuiltinCodeStubAssembler) {
Node* argc =
ChangeInt32ToIntPtr(Parameter(BuiltinDescriptor::kArgumentsCount));
CodeStubArguments args(this, argc);
Node* context = Parameter(BuiltinDescriptor::kContext);
Node* new_target = Parameter(BuiltinDescriptor::kNewTarget);
Node* receiver = args.GetReceiver();
Node* callbackfn = args.GetOptionalArgumentValue(0, UndefinedConstant());
Node* this_arg = args.GetOptionalArgumentValue(1, UndefinedConstant());
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, this_arg,
new_target, argc);
GenerateIteratingArrayBuiltinBody(
"Array.prototype.map", &ArrayBuiltinCodeStubAssembler::MapResultGenerator,
&ArrayBuiltinCodeStubAssembler::MapProcessor,
&ArrayBuiltinCodeStubAssembler::NullPostLoopAction,
Builtins::CallableFor(isolate(), Builtins::kArrayMapLoopContinuation));
}
我不知道这个 callbackfn
是什么意思。
这是this_arg
这个指针吗?
但是在我看来,第一个参数应该是this
指针,所以我很困惑。
感谢您的帮助。
看看 the documentation 的 Array.prototype.map
:它的签名是:
arr.map(function callback(currentValue[, index[, array]]) {...} [, thisArg])
这正是您发现的内置函数所反映的内容,只是用 V8 的内部符号表示。
receiver
是调用的接收者,即您调用 .map
的数组,在 MDN 示例中为 arr
。
callbackfn
是回调函数,callback
MDN 调用它。
this_arg
是 MDN 调用的可选 thisArg
。
该代码认为 callbackfn
是可选的这一事实并未反映(也不需要反映)规范;这只是安全处理用户未传递回调函数的情况的最便捷方式。重要的是由此产生的行为,即当 callbackfn
不可调用或缺失时抛出 TypeError
(这是 "not callable" 的特例,因为缺失的参数未定义,并且未定义不是函数)。
内置数组-gen.cc
TF_BUILTIN(ArrayMap, ArrayBuiltinCodeStubAssembler) {
Node* argc =
ChangeInt32ToIntPtr(Parameter(BuiltinDescriptor::kArgumentsCount));
CodeStubArguments args(this, argc);
Node* context = Parameter(BuiltinDescriptor::kContext);
Node* new_target = Parameter(BuiltinDescriptor::kNewTarget);
Node* receiver = args.GetReceiver();
Node* callbackfn = args.GetOptionalArgumentValue(0, UndefinedConstant());
Node* this_arg = args.GetOptionalArgumentValue(1, UndefinedConstant());
InitIteratingArrayBuiltinBody(context, receiver, callbackfn, this_arg,
new_target, argc);
GenerateIteratingArrayBuiltinBody(
"Array.prototype.map", &ArrayBuiltinCodeStubAssembler::MapResultGenerator,
&ArrayBuiltinCodeStubAssembler::MapProcessor,
&ArrayBuiltinCodeStubAssembler::NullPostLoopAction,
Builtins::CallableFor(isolate(), Builtins::kArrayMapLoopContinuation));
}
我不知道这个 callbackfn
是什么意思。
这是this_arg
这个指针吗?
但是在我看来,第一个参数应该是this
指针,所以我很困惑。
感谢您的帮助。
看看 the documentation 的 Array.prototype.map
:它的签名是:
arr.map(function callback(currentValue[, index[, array]]) {...} [, thisArg])
这正是您发现的内置函数所反映的内容,只是用 V8 的内部符号表示。
receiver
是调用的接收者,即您调用 .map
的数组,在 MDN 示例中为 arr
。
callbackfn
是回调函数,callback
MDN 调用它。
this_arg
是 MDN 调用的可选 thisArg
。
该代码认为 callbackfn
是可选的这一事实并未反映(也不需要反映)规范;这只是安全处理用户未传递回调函数的情况的最便捷方式。重要的是由此产生的行为,即当 callbackfn
不可调用或缺失时抛出 TypeError
(这是 "not callable" 的特例,因为缺失的参数未定义,并且未定义不是函数)。