Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/site/modules/core/list.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ System.print(list) //> [0, 1, 2, 3, 4, 5, 6]

Returns the added items.

### **capacity**

The number of elements that can fit in the underlying container before it needs to be resized.

### **clear**()

Removes all elements from the list.
Expand Down Expand Up @@ -136,6 +140,10 @@ System.print(["a", "b", "c"].removeAt(1)) //> b

It is a runtime error if the index is not an integer or is out of bounds.

### **reserve**(capacity)

Resize the underlying container size to `capacity` if it is greater than its current size.

### **sort**(), **sort**(comparer)

Sorts the elements of a list in-place; altering the list. The default sort is implemented using the quicksort algorithm.
Expand Down
16 changes: 16 additions & 0 deletions src/vm/wren_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ DEF_PRIMITIVE(list_addCore)
RETURN_VAL(args[0]);
}

DEF_PRIMITIVE(list_capacity)
{
RETURN_NUM(AS_LIST(args[0])->elements.capacity);
}

DEF_PRIMITIVE(list_clear)
{
wrenValueBufferClear(vm, &AS_LIST(args[0])->elements);
Expand Down Expand Up @@ -406,6 +411,15 @@ DEF_PRIMITIVE(list_removeValue) {
RETURN_VAL(wrenListRemoveAt(vm, list, index));
}

DEF_PRIMITIVE(list_reserve)
{
ObjList* list = AS_LIST(args[0]);
if (!validateInt(vm, args[1], "New capacity")) return false;
double newCapacity = AS_NUM(args[1]);
wrenValueBufferReserve(vm, &list->elements, newCapacity);
RETURN_NULL;
}

DEF_PRIMITIVE(list_indexOf)
{
ObjList* list = AS_LIST(args[0]);
Expand Down Expand Up @@ -1434,13 +1448,15 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->listClass, "[_]=(_)", list_subscriptSetter);
PRIMITIVE(vm->listClass, "add(_)", list_add);
PRIMITIVE(vm->listClass, "addCore_(_)", list_addCore);
PRIMITIVE(vm->listClass, "capacity", list_capacity);
PRIMITIVE(vm->listClass, "clear()", list_clear);
PRIMITIVE(vm->listClass, "count", list_count);
PRIMITIVE(vm->listClass, "insert(_,_)", list_insert);
PRIMITIVE(vm->listClass, "iterate(_)", list_iterate);
PRIMITIVE(vm->listClass, "iteratorValue(_)", list_iteratorValue);
PRIMITIVE(vm->listClass, "removeAt(_)", list_removeAt);
PRIMITIVE(vm->listClass, "remove(_)", list_removeValue);
PRIMITIVE(vm->listClass, "reserve(_)", list_reserve);
PRIMITIVE(vm->listClass, "indexOf(_)", list_indexOf);
PRIMITIVE(vm->listClass, "swap(_,_)", list_swap);

Expand Down
22 changes: 15 additions & 7 deletions src/vm/wren_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ typedef struct sObjString ObjString;
void wren##name##BufferClear(WrenVM* vm, name##Buffer* buffer); \
void wren##name##BufferFill(WrenVM* vm, name##Buffer* buffer, type data, \
int count); \
void wren##name##BufferReserve(WrenVM* vm, name##Buffer* buffer, \
int newCapacity); \
void wren##name##BufferWrite(WrenVM* vm, name##Buffer* buffer, type data)

// This should be used once for each type instantiation, somewhere in a .c file.
Expand All @@ -44,20 +46,26 @@ typedef struct sObjString ObjString;
void wren##name##BufferFill(WrenVM* vm, name##Buffer* buffer, type data, \
int count) \
{ \
if (buffer->capacity < buffer->count + count) \
{ \
int capacity = wrenPowerOf2Ceil(buffer->count + count); \
buffer->data = (type*)wrenReallocate(vm, buffer->data, \
buffer->capacity * sizeof(type), capacity * sizeof(type)); \
buffer->capacity = capacity; \
} \
wren##name##BufferReserve(vm, buffer, buffer->count + count); \
\
for (int i = 0; i < count; i++) \
{ \
buffer->data[buffer->count++] = data; \
} \
} \
\
void wren##name##BufferReserve(WrenVM* vm, name##Buffer* buffer, \
int newCapacity) \
{ \
if (buffer->capacity < newCapacity) \
{ \
newCapacity = wrenPowerOf2Ceil(newCapacity); \
buffer->data = (type*)wrenReallocate(vm, buffer->data, \
buffer->capacity * sizeof(type), newCapacity * sizeof(type)); \
buffer->capacity = newCapacity; \
} \
} \
\
void wren##name##BufferWrite(WrenVM* vm, name##Buffer* buffer, type data) \
{ \
wren##name##BufferFill(vm, buffer, data, 1); \
Expand Down