Skip to content

Commit

Permalink
Merge pull request #31 from Brugarolas/objecthash
Browse files Browse the repository at this point in the history
Objecthash
  • Loading branch information
Brugarolas committed Jun 1, 2024
2 parents cca2fcc + 5c86c79 commit b74bfe6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions doc/site/modules/core/object.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Compares two objects using built-in equality. This compares [value
types](../../values.html) by value, and all other objects are compared by
identity—two objects are equal only if they are the exact same object.

### **hash**

Returns a hash of the object.

### **is**(class) operator

Returns `true` if this object's class or one of its superclasses is `class`.
Expand Down
6 changes: 6 additions & 0 deletions src/vm/wren_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,11 @@ DEF_PRIMITIVE(object_is)
RETURN_BOOL(false);
}

DEF_PRIMITIVE(object_hash)
{
RETURN_NUM(wrenHash(args[0]));
}

DEF_PRIMITIVE(object_toString)
{
Obj* obj = AS_OBJ(args[0]);
Expand Down Expand Up @@ -1355,6 +1360,7 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->objectClass, "==(_)", object_eqeq);
PRIMITIVE(vm->objectClass, "~~(_)", object_eqeq);
PRIMITIVE(vm->objectClass, "!=(_)", object_bangeq);
PRIMITIVE(vm->objectClass, "hash", object_hash);
PRIMITIVE(vm->objectClass, "!~(_)", object_bangeq);
PRIMITIVE(vm->objectClass, "is(_)", object_is);
PRIMITIVE(vm->objectClass, "toString", object_toString);
Expand Down
7 changes: 3 additions & 4 deletions src/vm/wren_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,13 @@ static uint32_t hashObject(Obj* object)
return ((ObjString*)object)->hash;

default:
ASSERT(false, "Only immutable objects can be hashed.");
return 0;
return hashBits((uintptr_t)object);
}
}

// Generates a hash code for [value], which must be one of the built-in
// immutable types: null, bool, class, num, range, or string.
static uint32_t hashValue(Value value)
uint32_t wrenHash(Value value)
{
// TODO: We'll probably want to randomize this at some point.

Expand Down Expand Up @@ -470,7 +469,7 @@ static bool findEntry(MapEntry* entries, uint32_t capacity, Value key,

// Figure out where to insert it in the table. Use open addressing and
// basic linear probing.
uint32_t startIndex = hashValue(key) % capacity;
uint32_t startIndex = wrenHash(key) % capacity;
uint32_t index = startIndex;

// If we pass a tombstone and don't end up finding the key, its entry will
Expand Down
2 changes: 2 additions & 0 deletions src/vm/wren_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ ObjMap* wrenNewMap(WrenVM* vm);
// This separation exists to aid the API in surfacing errors to the developer as well.
static inline bool wrenMapIsValidKey(Value arg);

uint32_t wrenHash(Value value);

// Looks up [key] in [map]. If found, returns the value. Otherwise, returns
// `UNDEFINED_VAL`.
Value wrenMapGet(ObjMap* map, Value key);
Expand Down

0 comments on commit b74bfe6

Please sign in to comment.