1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Add Array.some (sdgoij)

This commit is contained in:
Robert Krimen 2013-06-23 13:48:03 +02:00
parent 091c371aea
commit b800b809a2
4 changed files with 55 additions and 0 deletions

View File

@ -364,6 +364,18 @@ func TestArray_every(t *testing.T) {
test(`[].every(function() { return false })`, "true")
test(`[1,2,3].every(function() { return false })`, "false")
test(`[1,2,3].every(function() { return true })`, "true")
test(`[1,2,3].every(function(index) { if (index === 1) return true })`, "false")
}
func TestArray_some(t *testing.T) {
Terst(t)
test := runTest()
test(`raise: [].some("abc")`, "TypeError")
test(`[].some(function() { return true })`, "false")
test(`[1,2,3].some(function() { return false })`, "false")
test(`[1,2,3].some(function() { return true })`, "true")
test(`[1,2,3].some(function(index) { if (index === 1) return true })`, "true")
}
func TestArray_indexing(t *testing.T) {

View File

@ -527,3 +527,19 @@ func builtinArray_every(call FunctionCall) Value {
}
panic(newTypeError())
}
func builtinArray_some(call FunctionCall) Value {
if thisObject, fn := call.thisObject(), call.Argument(0); fn.isCallable() {
length := int64(toUint32(thisObject.get("length")))
thisValue := call.Argument(1)
for index := int64(0); index < length; index++ {
if key := arrayIndexToString(index); thisObject.hasProperty(key) {
if value := thisObject.get(key); fn.call(thisValue, value, toValue_int64(index), toValue_object(thisObject)).isTrue() {
return TrueValue()
}
}
}
return FalseValue()
}
panic(newTypeError())
}

1
inline
View File

@ -235,6 +235,7 @@ sub newContext {
"indexOf", 1,
"lastIndexOf", 1,
"every", 1,
"some", 1,
);
return
".${class}Prototype =",

View File

@ -1010,6 +1010,25 @@ func _newContext(runtime *_runtime) {
call: _nativeCallFunction(builtinArray_every),
},
}
some_function := &_object{
runtime: runtime,
class: "Function",
objectClass: _classObject,
prototype: runtime.Global.FunctionPrototype,
extensible: true,
property: map[string]_property{
"length": _property{
mode: 0,
value: Value{
_valueType: valueNumber,
value: 1,
},
},
},
value: _functionObject{
call: _nativeCallFunction(builtinArray_some),
},
}
isArray_function := &_object{
runtime: runtime,
class: "Function",
@ -1149,6 +1168,13 @@ func _newContext(runtime *_runtime) {
value: every_function,
},
},
"some": _property{
mode: 0101,
value: Value{
_valueType: valueObject,
value: some_function,
},
},
},
}
runtime.Global.Array = &_object{