feat: add list subscript setter and refactor parameter validation
Implement list subscript setter (`[ ]=`) native method in wren_core.c, enabling assignment to list elements via bracket syntax. Extract duplicate parameter count validation logic into a shared `validateNumParameters` function in wren_compiler.c, replacing inline checks in both `parameterList` and `methodCall`. Add comprehensive test suite covering basic assignment, return value semantics, negative indices, and error cases for out-of-range and excessive arguments.
This commit is contained in:
@@ -212,6 +212,19 @@ DEF_NATIVE(list_subscript)
|
||||
return list->elements[index];
|
||||
}
|
||||
|
||||
DEF_NATIVE(list_subscriptSetter)
|
||||
{
|
||||
ObjList* list = AS_LIST(args[0]);
|
||||
|
||||
int index = validateIndex(args[1], list->count);
|
||||
// TODO: Instead of returning null here, should signal an error explicitly
|
||||
// somehow.
|
||||
if (index == -1) return NULL_VAL;
|
||||
|
||||
list->elements[index] = args[2];
|
||||
return args[2];
|
||||
}
|
||||
|
||||
DEF_NATIVE(null_toString)
|
||||
{
|
||||
// TODO: Intern this string or something.
|
||||
@@ -483,6 +496,7 @@ void wrenInitializeCore(WrenVM* vm)
|
||||
NATIVE(vm->listClass, "insert ", list_insert);
|
||||
NATIVE(vm->listClass, "removeAt ", list_removeAt);
|
||||
NATIVE(vm->listClass, "[ ]", list_subscript);
|
||||
NATIVE(vm->listClass, "[ ]=", list_subscriptSetter);
|
||||
|
||||
vm->nullClass = defineClass(vm, "Null", vm->objectClass);
|
||||
NATIVE(vm->nullClass, "toString", null_toString);
|
||||
|
||||
Reference in New Issue
Block a user