Skip to content

Conversation

@antocuni
Copy link
Member

@antocuni antocuni commented Feb 7, 2026

This text/code has been written by claude following my precise instructions, and has been reviewed by me.


Summary

This PR adds hashing support for strings, enabling them to be used as dictionary keys. The implementation includes a cached hash value in the string structure and uses the FNV-1a hashing algorithm.

Key Changes

  • String structure update: Added int32_t hash field to spy_Str struct to cache computed hash values
  • Hash function implementation: Implemented spy_str_hash() using FNV-1a algorithm with special handling for edge cases (hash values of 0 and -1 are remapped to avoid collisions with uninitialized state)
  • VM integration: Added w_hash_str() builtin function in the VM to expose string hashing to the language
  • Memory layout fix: Updated string data offset from 4 to 8 bytes to account for the new hash field (size_t=8 bytes on 64-bit systems)
  • Test coverage: Added test_str_keys() to verify dictionaries can use strings as keys

Implementation Details

  • Hash values are computed lazily and cached in the string object on first access
  • The hash field is initialized to 0 during string allocation, allowing detection of uncomputed hashes
  • Special values -1 and 0 are remapped to -2 and 1 respectively to preserve the invariant that 0 means "not yet computed"
  • The FNV-1a constants used are the standard 32-bit variants (offset basis: 2166136261, prime: 16777619)

https://claude.ai/code/session_014btrxaUSHiKYTzgR8dJxQy

Add a cached hash field to spy_Str struct and implement FNV-1a string
hashing. This enables using str as dictionary keys, which previously
raised "unhashable type 'str'" error.

https://claude.ai/code/session_014btrxaUSHiKYTzgR8dJxQy
The spy_Str struct now has a hash field, so static string initializers
need to include it (initialized to 0).

https://claude.ai/code/session_014btrxaUSHiKYTzgR8dJxQy
Update wasm_wrapper.py to read utf8 at offset 8 (was 4), and update
test_libspy.py to account for the hash field in mk_spy_Str, the C
literal initializer, and the raw memory read sizes.

https://claude.ai/code/session_014btrxaUSHiKYTzgR8dJxQy
Copy link
Contributor

@kanin-kearpimy kanin-kearpimy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good to me.

@antocuni antocuni merged commit fc22f44 into spylang:main Feb 9, 2026
2 checks passed
@antocuni antocuni deleted the claude/fix-dict-type-error-i95FC branch February 9, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants