@@ -35,6 +35,20 @@ func (o *Object) MethodCall(methodName string, args ...Valuer) (*Value, error) {
35
35
return fn .Call (o , args ... )
36
36
}
37
37
38
+ func coerceValue (iso * Isolate , val interface {}) (* Value , error ) {
39
+ switch v := val .(type ) {
40
+ case string , int32 , uint32 , int64 , uint64 , float64 , bool , * big.Int :
41
+ // ignoring error as code cannot reach the error state as we are already
42
+ // validating the new value types in this case statement
43
+ value , _ := NewValue (iso , v )
44
+ return value , nil
45
+ case Valuer :
46
+ return v .value (), nil
47
+ default :
48
+ return nil , fmt .Errorf ("v8go: unsupported object property type `%T`" , v )
49
+ }
50
+ }
51
+
38
52
// Set will set a property on the Object to a given value.
39
53
// Supports all value types, eg: Object, Array, Date, Set, Map etc
40
54
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
@@ -43,35 +57,26 @@ func (o *Object) Set(key string, val interface{}) error {
43
57
if len (key ) == 0 {
44
58
return errors .New ("v8go: You must provide a valid property key" )
45
59
}
46
- return set (o , key , 0 , val )
60
+
61
+ value , err := coerceValue (o .ctx .iso , val )
62
+ if err != nil {
63
+ return err
64
+ }
65
+
66
+ ckey := C .CString (key )
67
+ defer C .free (unsafe .Pointer (ckey ))
68
+ C .ObjectSet (o .ptr , ckey , value .ptr )
69
+ return nil
47
70
}
48
71
49
72
// Set will set a given index on the Object to a given value.
50
73
// Supports all value types, eg: Object, Array, Date, Set, Map etc
51
74
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
52
75
// then a *Value will be created and set as the value property.
53
76
func (o * Object ) SetIdx (idx uint32 , val interface {}) error {
54
- return set (o , "" , idx , val )
55
- }
56
-
57
- func set (o * Object , key string , idx uint32 , val interface {}) error {
58
- var value * Value
59
- switch v := val .(type ) {
60
- case string , int32 , uint32 , int64 , uint64 , float64 , bool , * big.Int :
61
- // ignoring error as code cannot reach the error state as we are already
62
- // validating the new value types in this case statement
63
- value , _ = NewValue (o .ctx .iso , v )
64
- case Valuer :
65
- value = v .value ()
66
- default :
67
- return fmt .Errorf ("v8go: unsupported object property type `%T`" , v )
68
- }
69
-
70
- if len (key ) > 0 {
71
- ckey := C .CString (key )
72
- defer C .free (unsafe .Pointer (ckey ))
73
- C .ObjectSet (o .ptr , ckey , value .ptr )
74
- return nil
77
+ value , err := coerceValue (o .ctx .iso , val )
78
+ if err != nil {
79
+ return err
75
80
}
76
81
77
82
C .ObjectSetIdx (o .ptr , C .uint32_t (idx ), value .ptr )
0 commit comments