Implement len() for map types

This commit is contained in:
Ayke van Laethem
2018-08-23 23:14:54 +02:00
parent 005665aee6
commit e884221fad
2 changed files with 8 additions and 0 deletions
+7
View File
@@ -1095,6 +1095,13 @@ func (c *Compiler) parseBuiltin(frame *Frame, args []ssa.Value, callName string)
default:
return llvm.Value{}, errors.New("todo: len: unknown basic type")
}
case *types.Map:
indices := []llvm.Value{
llvm.ConstInt(llvm.Int32Type(), 0, false),
llvm.ConstInt(llvm.Int32Type(), 2, false), // hashmap.count
}
ptr := c.builder.CreateGEP(value, indices, "lenptr")
return c.builder.CreateLoad(ptr, "len"), nil
case *types.Slice:
return c.builder.CreateExtractValue(value, 1, "len"), nil
default:
+1
View File
@@ -98,6 +98,7 @@ func hashmapSet(m *hashmap, key string, value unsafe.Pointer) {
bucket = bucket.next
}
if emptySlotKey != nil {
m.count++
*emptySlotKey = key
memcpy(emptySlotValue, value, uintptr(m.valueSize))
*emptySlotTophash = tophash