From 7520aa4474ea30985cf26631c6bbdebf38484a0d Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 9 Jan 2026 19:10:27 +0100 Subject: feat: initial support for maps --- vm/type.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'vm/type.go') diff --git a/vm/type.go b/vm/type.go index 3a8d901..88e3dc0 100644 --- a/vm/type.go +++ b/vm/type.go @@ -38,8 +38,21 @@ type Value struct { } // NewValue returns an addressable zero value for the specified type. -func NewValue(typ *Type) Value { - if typ.Rtype.Kind() == reflect.Func { +func NewValue(typ *Type, arg ...int) Value { + switch typ.Rtype.Kind() { + case reflect.Slice: + if len(arg) == 1 { + v := reflect.New(typ.Rtype).Elem() + v.Set(reflect.MakeSlice(typ.Rtype, arg[0], arg[0])) + return Value{Type: typ, Value: v} + } + case reflect.Map: + if len(arg) == 1 { + v := reflect.New(typ.Rtype).Elem() + v.Set(reflect.MakeMapWithSize(typ.Rtype, arg[0])) + return Value{Type: typ, Value: v} + } + case reflect.Func: typ = TypeOf(0) // Function value is its index in the code segment. } return Value{Type: typ, Value: reflect.New(typ.Rtype).Elem()} @@ -71,6 +84,11 @@ func SliceOf(t *Type) *Type { return &Type{Rtype: reflect.SliceOf(t.Rtype)} } +// MapOf returns the map type with the given key and element types. +func MapOf(k, e *Type) *Type { + return &Type{Rtype: reflect.MapOf(k.Rtype, e.Rtype)} +} + // FuncOf returns the function type with the given argument and result types. func FuncOf(arg, ret []*Type, variadic bool) *Type { a := make([]reflect.Type, len(arg)) -- cgit v1.2.3