diff --git a/doc/site/modules/core/list.markdown b/doc/site/modules/core/list.markdown index 204ffa39d..94857c92a 100644 --- a/doc/site/modules/core/list.markdown +++ b/doc/site/modules/core/list.markdown @@ -32,6 +32,16 @@ Removes all elements from the list. The number of elements in the list. +### **indexOf(value)** + +Returns the index of `value` in the list, if found. If not found, returns -1. + +
+var list = [0, 1, 2, 3, 4]
+System.print(list.indexOf(3)) //> 3
+System.print(list.indexOf(20)) //> -1
+
+ ### **insert**(index, item) Inserts the `item` at `index` in the list. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index ecf8d3c62..d9450b622 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -394,6 +394,12 @@ DEF_PRIMITIVE(list_removeAt) RETURN_VAL(wrenListRemoveAt(vm, list, index)); } +DEF_PRIMITIVE(list_indexOf) +{ + ObjList* list = AS_LIST(args[0]); + RETURN_NUM(wrenListIndexOf(vm, list, args[1])); +} + DEF_PRIMITIVE(list_subscript) { ObjList* list = AS_LIST(args[0]); @@ -1360,6 +1366,7 @@ void wrenInitializeCore(WrenVM* vm) PRIMITIVE(vm->listClass, "iterate(_)", list_iterate); PRIMITIVE(vm->listClass, "iteratorValue(_)", list_iteratorValue); PRIMITIVE(vm->listClass, "removeAt(_)", list_removeAt); + PRIMITIVE(vm->listClass, "indexOf(_)", list_indexOf); vm->mapClass = AS_CLASS(wrenFindVariable(vm, coreModule, "Map")); PRIMITIVE(vm->mapClass->obj.classObj, "new()", map_new); diff --git a/src/vm/wren_value.c b/src/vm/wren_value.c index d475b88b4..92bea7949 100644 --- a/src/vm/wren_value.c +++ b/src/vm/wren_value.c @@ -319,6 +319,19 @@ void wrenListInsert(WrenVM* vm, ObjList* list, Value value, uint32_t index) list->elements.data[index] = value; } +int wrenListIndexOf(WrenVM* vm, ObjList* list, Value value) +{ + int count = list->elements.count; + for (int i = 0; i < count; i++) + { + Value item = list->elements.data[i]; + if(wrenValuesEqual(item, value)) { + return i; + } + } + return -1; +} + Value wrenListRemoveAt(WrenVM* vm, ObjList* list, uint32_t index) { Value removed = list->elements.data[index]; diff --git a/src/vm/wren_value.h b/src/vm/wren_value.h index 349534d69..c1da47e81 100644 --- a/src/vm/wren_value.h +++ b/src/vm/wren_value.h @@ -680,6 +680,9 @@ void wrenListInsert(WrenVM* vm, ObjList* list, Value value, uint32_t index); // Removes and returns the item at [index] from [list]. Value wrenListRemoveAt(WrenVM* vm, ObjList* list, uint32_t index); +// Searches for [value] in [list], returns the index or -1 if not found. +int wrenListIndexOf(WrenVM* vm, ObjList* list, Value value); + // Creates a new empty map. ObjMap* wrenNewMap(WrenVM* vm); diff --git a/test/core/list/index_of.wren b/test/core/list/index_of.wren new file mode 100644 index 000000000..1b06bef7e --- /dev/null +++ b/test/core/list/index_of.wren @@ -0,0 +1,8 @@ + +var list = [0, 1, 2, 3, 4] +System.print(list.indexOf(4)) // expect: 4 +System.print(list.indexOf(2)) // expect: 2 +System.print(list.indexOf(3)) // expect: 3 +System.print(list.indexOf(0)) // expect: 0 +System.print(list.indexOf(100)) // expect: -1 +System.print(list.indexOf(-1)) // expect: -1 \ No newline at end of file