Browse Source

Support conversion from string types to []byte. Fixes #333

Dmitry Panov 4 years ago
parent
commit
7efcb634c6
2 changed files with 15 additions and 1 deletions
  1. 2 1
      runtime.go
  2. 13 0
      runtime_test.go

+ 2 - 1
runtime.go

@@ -33,6 +33,7 @@ var (
 	typeValue    = reflect.TypeOf((*Value)(nil)).Elem()
 	typeObject   = reflect.TypeOf((*Object)(nil))
 	typeTime     = reflect.TypeOf(time.Time{})
+	typeBytes    = reflect.TypeOf(([]byte)(nil))
 )
 
 type iterationKind int
@@ -1825,7 +1826,7 @@ func (r *Runtime) toReflectValue(v Value, dst reflect.Value, ctx *objectExportCt
 			return nil
 		}
 		expKind := et.Kind()
-		if expKind == kind && et.ConvertibleTo(typ) {
+		if expKind == kind && et.ConvertibleTo(typ) || expKind == reflect.String && typ == typeBytes {
 			ev := reflect.ValueOf(exportValue(v, ctx))
 			for ; i > 0; i-- {
 				ev = ev.Elem()

+ 13 - 0
runtime_test.go

@@ -2262,6 +2262,19 @@ func TestAnonFuncName(t *testing.T) {
 	testScript1(SCRIPT, valueTrue, t)
 }
 
+func TestStringToBytesConversion(t *testing.T) {
+	vm := New()
+	v := vm.ToValue("Test")
+	var b []byte
+	err := vm.ExportTo(v, &b)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if string(b) != "Test" {
+		t.Fatal(b)
+	}
+}
+
 /*
 func TestArrayConcatSparse(t *testing.T) {
 function foo(a,b,c)