Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Object.Set with an empty key string #276

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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