From 45d9794476522297df8020523b054c0d40ee008b Mon Sep 17 00:00:00 2001 From: root Date: Sun, 17 Jul 2016 12:01:50 +1000 Subject: [PATCH 1/2] added extra arguments to list constructors for default size/ value --- AUTHORS | 1 + src/vm/wren_core.c | 37 ++++++++++++++++++++++++++++++ test/core/list/new_extra_args.wren | 8 +++++++ 3 files changed, 46 insertions(+) create mode 100644 test/core/list/new_extra_args.wren diff --git a/AUTHORS b/AUTHORS index b0a402e2..d64b2b89 100644 --- a/AUTHORS +++ b/AUTHORS @@ -16,3 +16,4 @@ Evan Hahn Starbeamrainbowlabs Alexander Roper Will Speak +Max Ferguson diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 33029188..31df59b9 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]); @@ -1260,6 +1295,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, "new(_,_)", 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..b4f35ece --- /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.new(5, 2) +System.print(list2.count) // expect: 5 +System.print(list2) // expect: [2, 2, 2, 2, 2] From 244faa570098d449d2f8e4a27c9aa26fff9c1c76 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 17 Jul 2016 12:24:19 +1000 Subject: [PATCH 2/2] renamed filler constructor in list to match issue --- src/vm/wren_core.c | 2 +- test/core/list/new_extra_args.wren | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 31df59b9..83dd24f7 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -1296,7 +1296,7 @@ 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, "new(_,_)", list_newWithSizeDefault); + 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 index b4f35ece..1fc236e9 100644 --- a/test/core/list/new_extra_args.wren +++ b/test/core/list/new_extra_args.wren @@ -3,6 +3,6 @@ var list = List.new(5) System.print(list.count) // expect: 5 System.print(list) // expect: [null, null, null, null, null] -var list2 = List.new(5, 2) +var list2 = List.filled(5, 2) System.print(list2.count) // expect: 5 System.print(list2) // expect: [2, 2, 2, 2, 2]