Skip to content

Commit

Permalink
Fix Object.Set with an empty key string (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith authored Jan 12, 2022
1 parent 1d433f7 commit 5e91d3d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

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

Expand Down
5 changes: 0 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package v8go
// #include "v8go.h"
import "C"
import (
"errors"
"fmt"
"math/big"
"unsafe"
Expand Down Expand Up @@ -54,10 +53,6 @@ func coerceValue(iso *Isolate, val interface{}) (*Value, error) {
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
// then a *Value will be created and set as the value property.
func (o *Object) Set(key string, val interface{}) error {
if len(key) == 0 {
return errors.New("v8go: You must provide a valid property key")
}

value, err := coerceValue(o.ctx.iso, val)
if err != nil {
return err
Expand Down
16 changes: 14 additions & 2 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,24 @@ func TestObjectSet(t *testing.T) {
defer ctx.Close()
val, _ := ctx.RunScript("const foo = {}; foo", "")
obj, _ := val.AsObject()
obj.Set("bar", "baz")
if err := obj.Set("bar", "baz"); err != nil {
t.Errorf("unexpected error: %v", err)
}
baz, _ := ctx.RunScript("foo.bar", "")
if baz.String() != "baz" {
t.Errorf("unexpected value: %q", baz)
}
if err := obj.Set("", nil); err == nil {

if err := obj.Set("", "zero"); err != nil {
t.Errorf("unexpected error: %v", err)
}
val, err := ctx.RunScript("foo['']", "")
fatalIf(t, err)
if val.String() != "zero" {
t.Errorf("unexpected value: %q", val)
}

if err := obj.Set("a", nil); err == nil {
t.Error("expected error but got <nil>")
}
if err := obj.Set("a", 0); err == nil {
Expand Down

0 comments on commit 5e91d3d

Please sign in to comment.