Skip to content

Commit

Permalink
Added a note about pointers to primitive types and a corresponding te…
Browse files Browse the repository at this point in the history
…st. See dop251#158.
  • Loading branch information
dop251 committed Jun 22, 2020
1 parent f175242 commit 2faa37d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ A map type with methods is converted into a host object where properties are met
the map values are not accessible. This is to avoid ambiguity between m\["Property"\] and m.Property.

Any other type is converted to a generic reflect based host object. Depending on the underlying type it behaves similar
to a Number, String, Boolean or Object.
to a Number, String, Boolean or Object. This includes pointers to primitive types (*string, *int, etc...).
Internally they remain pointers, so changes to the pointed values will be reflected in JS.

Note that these conversions wrap the original value which means any changes made inside JS
are reflected on the value and calling Export() returns the original value. This applies to all
Expand Down
21 changes: 21 additions & 0 deletions object_goreflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,27 @@ func TestTagFieldNameMapperInvalidId(t *testing.T) {
}
}

func TestPrimitivePtr(t *testing.T) {
vm := New()
s := "test"
vm.Set("s", &s)
res, err := vm.RunString(`s instanceof String && s == "test"`) // note non-strict equality
if err != nil {
t.Fatal(err)
}
if v := res.ToBoolean(); !v {
t.Fatalf("value: %#v", res)
}
s = "test1"
res, err = vm.RunString(`s == "test1"`)
if err != nil {
t.Fatal(err)
}
if v := res.ToBoolean(); !v {
t.Fatalf("value: %#v", res)
}
}

func ExampleTagFieldNameMapper() {
vm := New()
vm.SetFieldNameMapper(TagFieldNameMapper("json", true))
Expand Down

0 comments on commit 2faa37d

Please sign in to comment.