vm,types: introduce dict type with value-key support - #421
Open
jow- wants to merge 1 commit into
Open
Conversation
jow-
force-pushed
the
dict-support
branch
6 times, most recently
from
July 25, 2026 21:24
60e0b99 to
f1c33e0
Compare
Add a new dict type that extends ucode objects by allowing arbitrary
value keys (not limited to strings). Key uniqueness follows uc_uniq()
semantics:
- Scalars (null, bool, int, double, string): compared by value
- Non-scalars (arrays, objects, etc.): compared by pointer equality
- NaN doubles are treated as equal
Dicts are distinguished from regular objects by their hash table
equal_fn function pointer, preserving ext_flag for is_constant semantics.
Provided functionality:
- dict() stdlib constructor accepting optional source object/dict/array
- keys() / values() returning actual value keys for dicts
- for...in iteration yielding value keys
- Spread operator support (dict->object converts keys to strings,
object->dict preserves strings as string values)
- Prototype chain lookup across dict/object boundaries
- GC marking for dict value keys
- JSON/stringification converting value keys to strings
Add stdlib test suite (tests/custom/03_stdlib/69_dict).
Signed-off-by: Jo-Philipp Wich <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduce a new dict data structure type that extends regular ucode objects by allowing arbitrary value keys instead of being limited to string keys.
Motivation
Regular ucode objects use null-terminated strings as keys, which is the standard JSON/JavaScript object model. However, there are use cases where you need a map-like structure with non-string keys — e.g., using booleans, numbers, or even other objects as keys. The new
dicttype fills this gap.Key Uniqueness Semantics
Key uniqueness follows
uc_uniq()semantics:This means two distinct array instances with identical contents are treated as different keys, while two integer values
42and42are the same key.Implementation Details
Dict Detection
Dicts reuse the existing
UC_OBJECTtype internally and are distinguished from regular objects by their hash table'sequal_fnfunction pointer, set to the sentineluc_dict_equal. This preservesext_flagforis_constantsemantics without requiring a new uc_type enum value.Hash Table
The dict hash table uses a custom hash function (
uc_dict_hash) and equality function (uc_dict_equal) that operate onuc_value_t*keys:ucv_free_dict_entry) releases references to both key and valueuc_value_tobjectsGC Integration
Dict keys are
uc_value_t*pointers that must be traced during garbage collection. Theucv_gc_mark()function now checksucv_is_dict()and marks both keys and values for dict objects.Prototype Chain
Prototype chain lookup works across dict/object boundaries:
Spread Operator
ucv_to_string()New API
dict()Constructorucv_is_dict()HelperInline function in
types.hfor runtime dict detection.ucv_dict_*()Functionsucv_dict_new()ucv_dict_get()ucv_dict_set()ucv_dict_delete()ucv_dict_length()ucv_dict_foreach()MacroIteration macro for traversing dict entries, yielding
uc_value_t*keys and values.Modified Builtins
The following stdlib functions now handle dicts transparently:
length()— returns entry count for dictskeys()— returns actual value keys (not strings) for dictsvalues()— returns values for dictsexists()— checks key existence using value semantics for dictsVM Integration
The VM instructions
LOAD_VAL,PEEK_VAL,STORE_VAL,UPDATE_VAL,SOBJ,MOBJ,I_NEXT/I_NEXTKV, andDELETEare all updated to dispatch to dict-specific functions whenucv_is_dict()returns true.Testing
Comprehensive test suite in
tests/custom/03_stdlib/69_dictcovering:keys(),values(),exists(),length()integrationfor...initeration with value keys