Browse Source

Added a note about pointers to primitive types and a corresponding test. See #158.

Dmitry Panov 5 years ago
parent
commit
2faa37d3de
2 changed files with 23 additions and 1 deletions
  1. 2 1
      README.md
  2. 21 0
      object_goreflect_test.go

+ 2 - 1
README.md

@@ -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.
 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
 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
 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
 are reflected on the value and calling Export() returns the original value. This applies to all

+ 21 - 0
object_goreflect_test.go

@@ -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() {
 func ExampleTagFieldNameMapper() {
 	vm := New()
 	vm := New()
 	vm.SetFieldNameMapper(TagFieldNameMapper("json", true))
 	vm.SetFieldNameMapper(TagFieldNameMapper("json", true))