mirror of
https://github.com/robertkrimen/otto.git
synced 2025-12-24 12:58:05 +08:00
Add Array.every (sdgoij)
This commit is contained in:
@@ -354,3 +354,14 @@ func TestArray_lastIndexOf(t *testing.T) {
|
||||
abc.lastIndexOf('b');
|
||||
`, "3")
|
||||
}
|
||||
|
||||
func TestArray_every(t *testing.T) {
|
||||
Terst(t)
|
||||
|
||||
test := runTest()
|
||||
test(`raise: [].every()`, "TypeError")
|
||||
test(`raise: [].every("abc")`, "TypeError")
|
||||
test(`[].every(function() { return false })`, "true")
|
||||
test(`[1,2,3].every(function() { return false })`, "false")
|
||||
test(`[1,2,3].every(function() { return true })`, "true")
|
||||
}
|
||||
|
||||
@@ -513,3 +513,20 @@ func builtinArray_lastIndexOf(call FunctionCall) Value {
|
||||
}
|
||||
return toValue_int(-1)
|
||||
}
|
||||
|
||||
func builtinArray_every(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(uint(index)); thisObject.hasProperty(key) {
|
||||
if value := thisObject.get(key); fn.call(thisValue, value, toValue_int64(index), toValue_object(thisObject)).isTrue() {
|
||||
continue
|
||||
}
|
||||
return FalseValue()
|
||||
}
|
||||
}
|
||||
return TrueValue()
|
||||
}
|
||||
panic(newTypeError())
|
||||
}
|
||||
|
||||
8
inline
8
inline
@@ -64,6 +64,13 @@ func toValue_bool(value bool) Value {
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func toValue_object(value *_object) Value {
|
||||
return Value{
|
||||
_valueType: valueObject,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
_END_
|
||||
|
||||
close $fmt;
|
||||
@@ -220,6 +227,7 @@ sub newContext {
|
||||
"sort", 0,
|
||||
"indexOf", 1,
|
||||
"lastIndexOf", 1,
|
||||
"every", 1,
|
||||
);
|
||||
return
|
||||
".${class}Prototype =",
|
||||
|
||||
33
inline.go
33
inline.go
@@ -991,6 +991,25 @@ func _newContext(runtime *_runtime) {
|
||||
call: _nativeCallFunction(builtinArray_lastIndexOf),
|
||||
},
|
||||
}
|
||||
every_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_every),
|
||||
},
|
||||
}
|
||||
isArray_function := &_object{
|
||||
runtime: runtime,
|
||||
class: "Function",
|
||||
@@ -1123,6 +1142,13 @@ func _newContext(runtime *_runtime) {
|
||||
value: lastIndexOf_function,
|
||||
},
|
||||
},
|
||||
"every": _property{
|
||||
mode: 0101,
|
||||
value: Value{
|
||||
_valueType: valueObject,
|
||||
value: every_function,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
runtime.Global.Array = &_object{
|
||||
@@ -5162,3 +5188,10 @@ func toValue_bool(value bool) Value {
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func toValue_object(value *_object) Value {
|
||||
return Value{
|
||||
_valueType: valueObject,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user