feat: add initial Map class with hash table, subscript, and count support
Implement the core Map data structure in Wren, including the ObjMap type with hash table storage, native methods for instantiation, subscript get/set, and count. Add Map class declaration to core.wren with a basic toString stub. Introduce MapEntry struct and hash table growth logic with configurable load factor. Add test files for map creation, count tracking, key type support (including null, bool, num, string, range, and class keys), and growth behavior. Refactor list capacity constants into shared MIN_CAPACITY and GROW_FACTOR macros. Change object equality operators to use wrenValuesSame instead of wrenValuesEqual.
This commit is contained in:
+38
-2
@@ -131,6 +131,11 @@ static const char* libSource =
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"class Map {\n"
|
||||
" // TODO: Implement this.\n"
|
||||
" toString { \"{}\" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"class Range is Sequence {}\n";
|
||||
|
||||
// Validates that the given argument in [args] is a function. Returns true if
|
||||
@@ -676,6 +681,29 @@ DEF_NATIVE(list_subscriptSetter)
|
||||
RETURN_VAL(args[2]);
|
||||
}
|
||||
|
||||
DEF_NATIVE(map_instantiate)
|
||||
{
|
||||
RETURN_OBJ(wrenNewMap(vm));
|
||||
}
|
||||
|
||||
DEF_NATIVE(map_subscript)
|
||||
{
|
||||
// TODO: Validate key type.
|
||||
RETURN_VAL(wrenMapGet(AS_MAP(args[0]), args[1]));
|
||||
}
|
||||
|
||||
DEF_NATIVE(map_subscriptSetter)
|
||||
{
|
||||
// TODO: Validate key type.
|
||||
wrenMapSet(vm, AS_MAP(args[0]), args[1], args[2]);
|
||||
RETURN_VAL(args[2]);
|
||||
}
|
||||
|
||||
DEF_NATIVE(map_count)
|
||||
{
|
||||
RETURN_NUM(AS_MAP(args[0])->count);
|
||||
}
|
||||
|
||||
DEF_NATIVE(null_not)
|
||||
{
|
||||
RETURN_VAL(TRUE_VAL);
|
||||
@@ -876,12 +904,12 @@ DEF_NATIVE(object_not)
|
||||
|
||||
DEF_NATIVE(object_eqeq)
|
||||
{
|
||||
RETURN_BOOL(wrenValuesEqual(args[0], args[1]));
|
||||
RETURN_BOOL(wrenValuesSame(args[0], args[1]));
|
||||
}
|
||||
|
||||
DEF_NATIVE(object_bangeq)
|
||||
{
|
||||
RETURN_BOOL(!wrenValuesEqual(args[0], args[1]));
|
||||
RETURN_BOOL(!wrenValuesSame(args[0], args[1]));
|
||||
}
|
||||
|
||||
DEF_NATIVE(object_new)
|
||||
@@ -1338,7 +1366,15 @@ void wrenInitializeCore(WrenVM* vm)
|
||||
NATIVE(vm->listClass, "iteratorValue ", list_iteratorValue);
|
||||
NATIVE(vm->listClass, "removeAt ", list_removeAt);
|
||||
|
||||
vm->mapClass = AS_CLASS(findGlobal(vm, "Map"));
|
||||
NATIVE(vm->mapClass->obj.classObj, " instantiate", map_instantiate);
|
||||
NATIVE(vm->mapClass, "[ ]", map_subscript);
|
||||
NATIVE(vm->mapClass, "[ ]=", map_subscriptSetter);
|
||||
NATIVE(vm->mapClass, "count", map_count);
|
||||
// TODO: More map methods.
|
||||
|
||||
vm->rangeClass = AS_CLASS(findGlobal(vm, "Range"));
|
||||
// TODO: == operator.
|
||||
NATIVE(vm->rangeClass, "from", range_from);
|
||||
NATIVE(vm->rangeClass, "to", range_to);
|
||||
NATIVE(vm->rangeClass, "min", range_min);
|
||||
|
||||
Reference in New Issue
Block a user