forked from Mirror/wren
Merge branch 'new-list-constructors' of https://github.com/mooxen/wren into mooxen-new-list-constructors
This commit is contained in:
1
AUTHORS
1
AUTHORS
@ -17,3 +17,4 @@ Starbeamrainbowlabs <contact@starbeamrainbowlabs.com>
|
|||||||
Alexander Roper <minirop@gmail.com>
|
Alexander Roper <minirop@gmail.com>
|
||||||
Will Speak <will@willspeak.me>
|
Will Speak <will@willspeak.me>
|
||||||
Damien Radtke <damienradtke@gmail.com>
|
Damien Radtke <damienradtke@gmail.com>
|
||||||
|
Max Ferguson <maxxferguson@gmail.com>
|
||||||
|
|||||||
@ -239,6 +239,41 @@ DEF_PRIMITIVE(list_new)
|
|||||||
RETURN_OBJ(wrenNewList(vm, 0));
|
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)
|
DEF_PRIMITIVE(list_add)
|
||||||
{
|
{
|
||||||
wrenValueBufferWrite(vm, &AS_LIST(args[0])->elements, args[1]);
|
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"));
|
vm->listClass = AS_CLASS(wrenFindVariable(vm, coreModule, "List"));
|
||||||
PRIMITIVE(vm->listClass->obj.classObj, "new()", list_new);
|
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_subscript);
|
||||||
PRIMITIVE(vm->listClass, "[_]=(_)", list_subscriptSetter);
|
PRIMITIVE(vm->listClass, "[_]=(_)", list_subscriptSetter);
|
||||||
PRIMITIVE(vm->listClass, "add(_)", list_add);
|
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.filled(5, 2)
|
||||||
|
System.print(list2.count) // expect: 5
|
||||||
|
System.print(list2) // expect: [2, 2, 2, 2, 2]
|
||||||
Reference in New Issue
Block a user