diff options
Diffstat (limited to 'js/scripting-lang/baba-yaga-c/src/table.c')
-rw-r--r-- | js/scripting-lang/baba-yaga-c/src/table.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/js/scripting-lang/baba-yaga-c/src/table.c b/js/scripting-lang/baba-yaga-c/src/table.c index 18c3292..0614929 100644 --- a/js/scripting-lang/baba-yaga-c/src/table.c +++ b/js/scripting-lang/baba-yaga-c/src/table.c @@ -304,33 +304,44 @@ Value baba_yaga_value_table(void) { Value baba_yaga_table_get(const Value* table, const char* key) { if (table == NULL || table->type != VAL_TABLE || key == NULL) { + DEBUG_ERROR("Table get: invalid parameters"); return baba_yaga_value_nil(); } TableValue* table_value = (TableValue*)table->data.table; + DEBUG_DEBUG("Table get: looking for key '%s' in table with %zu entries", key, table_value->hash_table->size); + TableEntry* entry = hash_table_get_entry(table_value->hash_table, key); if (entry != NULL) { + DEBUG_DEBUG("Table get: found key '%s', returning value type %d", key, entry->value.type); return baba_yaga_value_copy(&entry->value); } + DEBUG_DEBUG("Table get: key '%s' not found", key); return baba_yaga_value_nil(); } Value baba_yaga_table_set(const Value* table, const char* key, const Value* value) { if (table == NULL || table->type != VAL_TABLE || key == NULL || value == NULL) { + DEBUG_ERROR("Table set: invalid parameters"); return baba_yaga_value_nil(); } + DEBUG_DEBUG("Table set: setting key '%s' to value type %d", key, value->type); + /* Create new table */ Value new_table = baba_yaga_value_table(); if (new_table.type != VAL_TABLE) { + DEBUG_ERROR("Table set: failed to create new table"); return baba_yaga_value_nil(); } TableValue* new_table_value = (TableValue*)new_table.data.table; TableValue* old_table_value = (TableValue*)table->data.table; + DEBUG_DEBUG("Table set: copying %zu entries from old table", old_table_value->hash_table->size); + /* Copy all entries from old table */ for (size_t i = 0; i < old_table_value->hash_table->capacity; i++) { TableEntry* entry = old_table_value->hash_table->buckets[i]; @@ -355,10 +366,12 @@ Value baba_yaga_table_set(const Value* table, const char* key, const Value* valu /* Set the new value */ if (!hash_table_set(new_table_value->hash_table, key, value)) { + DEBUG_ERROR("Table set: failed to set key '%s'", key); baba_yaga_value_destroy(&new_table); return baba_yaga_value_nil(); } + DEBUG_DEBUG("Table set: new table has %zu entries", new_table_value->hash_table->size); return new_table; } @@ -444,6 +457,75 @@ bool baba_yaga_table_has_key(const Value* table, const char* key) { return hash_table_get_entry(table_value->hash_table, key) != NULL; } +/** + * @brief Get all keys from a table + * + * @param table Table value + * @param keys Array to store keys (caller must free) + * @param max_keys Maximum number of keys to retrieve + * @return Number of keys retrieved + */ +size_t baba_yaga_table_get_keys(const Value* table, char** keys, size_t max_keys) { + if (table == NULL || table->type != VAL_TABLE || keys == NULL || max_keys == 0) { + return 0; + } + + TableValue* table_value = (TableValue*)table->data.table; + HashTable* hash_table = table_value->hash_table; + + size_t key_count = 0; + + /* Get string keys */ + for (size_t i = 0; i < hash_table->capacity && key_count < max_keys; i++) { + TableEntry* entry = hash_table->buckets[i]; + while (entry != NULL && key_count < max_keys) { + keys[key_count] = strdup(entry->key); + key_count++; + entry = entry->next; + } + } + + /* Get numeric keys (array indices) */ + for (size_t i = 0; i < hash_table->array_size && key_count < max_keys; i++) { + char* num_key = malloc(32); /* Enough for large numbers */ + if (num_key != NULL) { + snprintf(num_key, 32, "%zu", i + 1); /* 1-based indexing */ + keys[key_count] = num_key; + key_count++; + } + } + + return key_count; +} + +/** + * @brief Get a value from table by key (supports both string and numeric keys) + * + * @param table Table value + * @param key Key (string or numeric as string) + * @return Value at key, or nil if not found + */ +Value baba_yaga_table_get_by_key(const Value* table, const char* key) { + if (table == NULL || table->type != VAL_TABLE || key == NULL) { + return baba_yaga_value_nil(); + } + + /* Try as string key first */ + Value result = baba_yaga_table_get(table, key); + if (result.type != VAL_NIL) { + return result; + } + + /* Try as numeric key */ + char* endptr; + long index = strtol(key, &endptr, 10); + if (*endptr == '\0' && index > 0) { + return baba_yaga_table_get_index(table, (int)index); + } + + return baba_yaga_value_nil(); +} + /* ============================================================================ * Internal Table Management * ============================================================================ */ |