From 5e91d3d9dcabd2986f901b6b31590e49fc3c4dd8 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 12 Jan 2022 17:06:50 -0500 Subject: [PATCH] Fix Object.Set with an empty key string (#276) --- CHANGELOG.md | 1 + object.go | 5 ----- object_test.go | 16 ++++++++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aeec892f..ab1b80bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/object.go b/object.go index 5fe43e21..08a0a6b6 100644 --- a/object.go +++ b/object.go @@ -8,7 +8,6 @@ package v8go // #include "v8go.h" import "C" import ( - "errors" "fmt" "math/big" "unsafe" @@ -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 diff --git a/object_test.go b/object_test.go index 41d73f20..b40d1b0d 100644 --- a/object_test.go +++ b/object_test.go @@ -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 ") } if err := obj.Set("a", 0); err == nil {