mirror of
https://github.com/Samsung/escargot.git
synced 2026-06-22 10:01:50 +00:00
Implement Array.prototype.forEach
This commit is contained in:
parent
36c293c12f
commit
212c455b52
1 changed files with 30 additions and 0 deletions
|
|
@ -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),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue