mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-10 21:58:48 +01:00
added extra arguments to list constructors for default size/ value
This commit is contained in:
1
AUTHORS
1
AUTHORS
@ -16,3 +16,4 @@ Evan Hahn <me@evanhahn.com>
|
||||
Starbeamrainbowlabs <contact@starbeamrainbowlabs.com>
|
||||
Alexander Roper <minirop@gmail.com>
|
||||
Will Speak <will@willspeak.me>
|
||||
Max Ferguson <maxxferguson@gmail.com>
|
||||
|
||||
@ -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);
|
||||
|
||||
8
test/core/list/new_extra_args.wren
Normal file
8
test/core/list/new_extra_args.wren
Normal file
@ -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]
|
||||
Reference in New Issue
Block a user