diff --git a/AUTHORS b/AUTHORS index f85c823e..cbd5ae26 100644 --- a/AUTHORS +++ b/AUTHORS @@ -17,3 +17,4 @@ Starbeamrainbowlabs Alexander Roper Will Speak Damien Radtke +Max Ferguson diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 4f91d3ca..28f3cafc 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -239,6 +239,41 @@ DEF_PRIMITIVE(list_new) RETURN_OBJ(wrenNewList(vm, 0)); } +//creates a new list of size args[1], with all elements initilized to null +DEF_PRIMITIVE(list_newWithSize) +{ + if(!validateInt(vm, args[1], "Size")) return false; + + uint32_t size = (uint32_t)AS_NUM(args[1]); + ObjList* list = wrenNewList(vm, size); + + for(uint32_t i=0; i < size; i++) + { + list->elements.data[i] = NULL_VAL; + } + + RETURN_OBJ(list); +} + + +//creates a new list of size args[1], with all elements initilized to args[2] +DEF_PRIMITIVE(list_newWithSizeDefault) +{ + if(!validateInt(vm, args[1], "Size")) return false; + + uint32_t size = (uint32_t)AS_NUM(args[1]); + + ObjList* list = wrenNewList(vm, size); + + for(uint32_t i=0; i < size; i++) + { + list->elements.data[i] = args[2]; + } + + RETURN_OBJ(list); +} + + DEF_PRIMITIVE(list_add) { wrenValueBufferWrite(vm, &AS_LIST(args[0])->elements, args[1]); @@ -1274,6 +1309,8 @@ void wrenInitializeCore(WrenVM* vm) vm->listClass = AS_CLASS(wrenFindVariable(vm, coreModule, "List")); PRIMITIVE(vm->listClass->obj.classObj, "new()", list_new); + PRIMITIVE(vm->listClass->obj.classObj, "new(_)", list_newWithSize); + PRIMITIVE(vm->listClass->obj.classObj, "filled(_,_)", list_newWithSizeDefault); PRIMITIVE(vm->listClass, "[_]", list_subscript); PRIMITIVE(vm->listClass, "[_]=(_)", list_subscriptSetter); PRIMITIVE(vm->listClass, "add(_)", list_add); diff --git a/test/core/list/new_extra_args.wren b/test/core/list/new_extra_args.wren new file mode 100644 index 00000000..1fc236e9 --- /dev/null +++ b/test/core/list/new_extra_args.wren @@ -0,0 +1,8 @@ +var list = List.new(5) + +System.print(list.count) // expect: 5 +System.print(list) // expect: [null, null, null, null, null] + +var list2 = List.filled(5, 2) +System.print(list2.count) // expect: 5 +System.print(list2) // expect: [2, 2, 2, 2, 2]