Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 43c78db

Browse files
AssemblyScript Updates
(Revert after #277 is merged)
1 parent 6a26cc1 commit 43c78db

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

functions/assemblyscript/objects.go

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11+
"reflect"
1112
"time"
1213

1314
"hmruntime/plugins"
@@ -48,18 +49,30 @@ func readObject(ctx context.Context, mem wasm.Memory, typ plugins.TypeInfo, offs
4849
func writeObject(ctx context.Context, mod wasm.Module, typ plugins.TypeInfo, val any) (offset uint32, err error) {
4950
switch typ.Name {
5051
case "ArrayBuffer":
51-
bytes, ok := val.([]byte)
52-
if !ok {
52+
switch v := val.(type) {
53+
case []byte:
54+
return writeBytes(ctx, mod, v)
55+
case *[]byte:
56+
if v == nil {
57+
return 0, nil
58+
}
59+
return writeBytes(ctx, mod, *v)
60+
default:
5361
return 0, fmt.Errorf("input value is not a byte array")
5462
}
55-
return writeBytes(ctx, mod, bytes)
5663

5764
case "string":
58-
s, ok := val.(string)
59-
if !ok {
65+
switch v := val.(type) {
66+
case string:
67+
return WriteString(ctx, mod, v)
68+
case *string:
69+
if v == nil {
70+
return 0, nil
71+
}
72+
return WriteString(ctx, mod, *v)
73+
default:
6074
return 0, fmt.Errorf("input value is not a string")
6175
}
62-
return WriteString(ctx, mod, s)
6376

6477
case "Date":
6578
var t time.Time
@@ -70,16 +83,44 @@ func writeObject(ctx context.Context, mod wasm.Module, typ plugins.TypeInfo, val
7083
return 0, err
7184
}
7285
t = time.UnixMilli(n)
86+
case *json.Number:
87+
if v == nil {
88+
return 0, nil
89+
}
90+
n, err := v.Int64()
91+
if err != nil {
92+
return 0, err
93+
}
94+
t = time.UnixMilli(n)
7395
case string:
7496
var err error
7597
t, err = utils.ParseTime(v)
7698
if err != nil {
7799
return 0, err
78100
}
101+
case *string:
102+
if v == nil {
103+
return 0, nil
104+
}
105+
var err error
106+
t, err = utils.ParseTime(*v)
107+
if err != nil {
108+
return 0, err
109+
}
79110
case utils.JSONTime:
80111
t = time.Time(v)
112+
case *utils.JSONTime:
113+
if v == nil {
114+
return 0, nil
115+
}
116+
t = time.Time(*v)
81117
case time.Time:
82118
t = v
119+
case *time.Time:
120+
if v == nil {
121+
return 0, nil
122+
}
123+
t = *v
83124
default:
84125
return 0, fmt.Errorf("input value is not a valid for a time object")
85126
}
@@ -92,6 +133,15 @@ func writeObject(ctx context.Context, mod wasm.Module, typ plugins.TypeInfo, val
92133
return 0, err
93134
}
94135

136+
if reflect.TypeOf(val).Kind() == reflect.Ptr {
137+
v := reflect.ValueOf(val)
138+
if v.IsNil() {
139+
return 0, nil
140+
} else {
141+
val = v.Elem().Interface()
142+
}
143+
}
144+
95145
if isArrayType(typ.Path) {
96146
return writeArray(ctx, mod, def, val)
97147
} else if isMapType(typ.Path) {

functions/assemblyscript/values.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func EncodeValue(ctx context.Context, mod wasm.Module, typ plugins.TypeInfo, dat
2020

2121
// Handle null values if the type is nullable
2222
if isNullable(typ.Path) {
23-
if data == nil {
23+
if data == nil || reflect.ValueOf(data).IsNil() {
2424
return 0, nil
2525
}
2626
typ = removeNull(typ)

hostfunctions/helpers.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ import (
1414
)
1515

1616
func writeResult[T any](ctx context.Context, mod wasm.Module, val T) (uint32, error) {
17-
switch any(val).(type) {
17+
switch t := any(val).(type) {
1818
case string:
1919
// fast path for strings
20-
return assemblyscript.WriteString(ctx, mod, any(val).(string))
20+
return assemblyscript.WriteString(ctx, mod, t)
21+
case *string:
22+
// fast path for nullable strings
23+
if any(val) == nil {
24+
return 0, nil
25+
} else {
26+
return assemblyscript.WriteString(ctx, mod, *t)
27+
}
2128
default:
2229
typ, err := assemblyscript.GetTypeInfo[T]()
2330
if err != nil {
@@ -42,7 +49,19 @@ func readParam[T any](ctx context.Context, mod wasm.Module, p uint32, v *T) erro
4249
return err
4350
}
4451
*v = any(s).(T)
45-
52+
case *string:
53+
// fast path for nullable strings
54+
if p == 0 {
55+
var s *string
56+
*v = any(s).(T)
57+
} else {
58+
mem := mod.Memory()
59+
s, err := assemblyscript.ReadString(mem, p)
60+
if err != nil {
61+
return err
62+
}
63+
*v = any(&s).(T)
64+
}
4665
default:
4766
typ, err := assemblyscript.GetTypeInfo[T]()
4867
if err != nil {

0 commit comments

Comments
 (0)