Skip to content

Commit 5e91d3d

Browse files
authored
Fix Object.Set with an empty key string (#276)
1 parent 1d433f7 commit 5e91d3d

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Fixed
1010
- Use string length to ensure null character-containing strings in Go/JS are not terminated early.
11+
- Object.Set with an empty key string is now supported
1112

1213
## [v0.7.0] - 2021-12-09
1314

object.go

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package v8go
88
// #include "v8go.h"
99
import "C"
1010
import (
11-
"errors"
1211
"fmt"
1312
"math/big"
1413
"unsafe"
@@ -54,10 +53,6 @@ func coerceValue(iso *Isolate, val interface{}) (*Value, error) {
5453
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
5554
// then a *Value will be created and set as the value property.
5655
func (o *Object) Set(key string, val interface{}) error {
57-
if len(key) == 0 {
58-
return errors.New("v8go: You must provide a valid property key")
59-
}
60-
6156
value, err := coerceValue(o.ctx.iso, val)
6257
if err != nil {
6358
return err

object_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,24 @@ func TestObjectSet(t *testing.T) {
5353
defer ctx.Close()
5454
val, _ := ctx.RunScript("const foo = {}; foo", "")
5555
obj, _ := val.AsObject()
56-
obj.Set("bar", "baz")
56+
if err := obj.Set("bar", "baz"); err != nil {
57+
t.Errorf("unexpected error: %v", err)
58+
}
5759
baz, _ := ctx.RunScript("foo.bar", "")
5860
if baz.String() != "baz" {
5961
t.Errorf("unexpected value: %q", baz)
6062
}
61-
if err := obj.Set("", nil); err == nil {
63+
64+
if err := obj.Set("", "zero"); err != nil {
65+
t.Errorf("unexpected error: %v", err)
66+
}
67+
val, err := ctx.RunScript("foo['']", "")
68+
fatalIf(t, err)
69+
if val.String() != "zero" {
70+
t.Errorf("unexpected value: %q", val)
71+
}
72+
73+
if err := obj.Set("a", nil); err == nil {
6274
t.Error("expected error but got <nil>")
6375
}
6476
if err := obj.Set("a", 0); err == nil {

0 commit comments

Comments
 (0)