refactor objects
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
2024-04-01 17:34:10 -04:00
parent 803f330e82
commit 99f7553d67
15 changed files with 101 additions and 94 deletions

View File

@@ -102,7 +102,7 @@ func (s *VMState) ExportedHash() *object.Hash {
if symbol.Scope == compiler.GlobalScope {
obj := s.Globals[symbol.Index]
s := object.String{Value: name}
pairs[s.HashKey()] = object.HashPair{Key: s, Value: obj}
pairs[s.Hash()] = object.HashPair{Key: s, Value: obj}
}
}
}
@@ -210,8 +210,8 @@ func (vm *VM) executeSetGlobal() error {
globalIndex := vm.currentFrame().ReadUint16()
ref := vm.pop()
if immutable, ok := ref.(object.Immutable); ok {
vm.state.Globals[globalIndex] = immutable.Clone()
if obj, ok := ref.(object.Copyable); ok {
vm.state.Globals[globalIndex] = obj.Copy()
} else {
vm.state.Globals[globalIndex] = ref
}
@@ -223,8 +223,8 @@ func (vm *VM) executeSetLocal() error {
localIndex := vm.currentFrame().ReadUint8()
ref := vm.pop()
if immutable, ok := ref.(object.Immutable); ok {
vm.stack[vm.currentFrame().basePointer+int(localIndex)] = immutable.Clone()
if obj, ok := ref.(object.Copyable); ok {
vm.stack[vm.currentFrame().basePointer+int(localIndex)] = obj.Copy()
} else {
vm.stack[vm.currentFrame().basePointer+int(localIndex)] = ref
}
@@ -241,12 +241,12 @@ func (vm *VM) buildHash(startIndex, endIndex int) (object.Object, error) {
pair := object.HashPair{Key: key, Value: value}
hashKey, ok := key.(object.Hashable)
hashKey, ok := key.(object.Hasher)
if !ok {
return nil, fmt.Errorf("unusable as hash key: %s", key.Type())
}
hashedPairs[hashKey.HashKey()] = pair
hashedPairs[hashKey.Hash()] = pair
}
return &object.Hash{Pairs: hashedPairs}, nil

View File

@@ -379,15 +379,15 @@ func TestHashLiterals(t *testing.T) {
{
"{1: 2, 2: 3}",
map[object.HashKey]int64{
(object.Integer{Value: 1}).HashKey(): 2,
(object.Integer{Value: 2}).HashKey(): 3,
(object.Integer{Value: 1}).Hash(): 2,
(object.Integer{Value: 2}).Hash(): 3,
},
},
{
"{1 + 1: 2 * 2, 3 + 3: 4 * 4}",
map[object.HashKey]int64{
(object.Integer{Value: 2}).HashKey(): 4,
(object.Integer{Value: 6}).HashKey(): 16,
(object.Integer{Value: 2}).Hash(): 4,
(object.Integer{Value: 6}).Hash(): 16,
},
},
}
@@ -400,14 +400,14 @@ func TestHashMerging(t *testing.T) {
{
`{} + {"a": 1}`,
map[object.HashKey]int64{
(object.String{Value: "a"}).HashKey(): 1,
(object.String{Value: "a"}).Hash(): 1,
},
},
{
`{"a": 1} + {"b": 2}`,
map[object.HashKey]int64{
(object.String{Value: "a"}).HashKey(): 1,
(object.String{Value: "b"}).HashKey(): 2,
(object.String{Value: "a"}).Hash(): 1,
(object.String{Value: "b"}).Hash(): 2,
},
},
}