summaryrefslogtreecommitdiff
path: root/vm/type.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2026-01-09 19:10:27 +0100
committerMarc Vertes <mvertes@free.fr>2026-01-09 19:10:27 +0100
commit7520aa4474ea30985cf26631c6bbdebf38484a0d (patch)
treee1e344c2fdc04affc0095eab26a39eef1fc3ba91 /vm/type.go
parent6ae0a2530c9a57fc093d2159591d9cae8140d641 (diff)
feat: initial support for maps
Diffstat (limited to 'vm/type.go')
-rw-r--r--vm/type.go22
1 files changed, 20 insertions, 2 deletions
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))