Implement Array.prototype.forEach

This commit is contained in:
Junyoung Cho 2016-12-20 14:43:37 +09:00
commit 212c455b52

View file

@ -285,6 +285,34 @@ static Value builtinArraySlice(ExecutionState& state, Value thisValue, size_t ar
return array;
}
static Value builtinArrayForEach(ExecutionState& state, Value thisValue, size_t argc, Value* argv, bool isNewExpression)
{
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, Array, forEach);
uint32_t len = thisObject->length(state);
Value callbackfn = argv[0];
if (!callbackfn.isFunction()) {
ErrorObject::throwBuiltinError(state, ErrorObject::TypeError, state.context()->staticStrings().Array.string(), true,
state.context()->staticStrings().forEach.string(), errorMessage_GlobalObject_CallbackNotCallable);
}
Value thisArg = argv[1];
uint32_t k = 0;
while (k < len) {
Value Pk = Value(k);
auto res = thisObject->get(state, ObjectPropertyName(state, Pk));
if (res.hasValue()) {
Value kValue = res.value();
Value args[3] = { kValue, Pk, thisObject };
callbackfn.asFunction()->call(state, thisArg, 3, args, false);
k++;
} else {
k = Object::nextIndexForward(state, thisObject, k, len, false);
}
}
return Value();
}
void GlobalObject::installArray(ExecutionState& state)
{
m_array = new FunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().Array, builtinArrayConstructor, 1, [](ExecutionState& state, size_t argc, Value* argv) -> Object* {
@ -299,6 +327,8 @@ void GlobalObject::installArray(ExecutionState& state)
m_arrayPrototype->defineOwnPropertyThrowsException(state, ObjectPropertyName(state.context()->staticStrings().concat),
ObjectPropertyDescriptor(new FunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().concat, builtinArrayConcat, 1, nullptr, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_arrayPrototype->defineOwnPropertyThrowsException(state, ObjectPropertyName(state.context()->staticStrings().forEach),
ObjectPropertyDescriptor(new FunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().forEach, builtinArrayForEach, 2, nullptr, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_arrayPrototype->defineOwnPropertyThrowsException(state, ObjectPropertyName(state.context()->staticStrings().join),
ObjectPropertyDescriptor(new FunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().join, builtinArrayJoin, 1, nullptr, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_arrayPrototype->defineOwnPropertyThrowsException(state, ObjectPropertyName(state.context()->staticStrings().sort),