Skip to content

Support Value.StrictEquals to Go. #106

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

Merged
merged 3 commits into from
May 21, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added

- Add support for ObjectTemplate.MarkAsUndetectable.
- Add `Value.StrictEquals` providing strict equality checks in Go code

### Changed

Expand Down
6 changes: 6 additions & 0 deletions value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,9 @@ int ValueIsModuleNamespaceObject(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsModuleNamespaceObject();
}

int ValueStrictEquals(ValuePtr ptr, ValuePtr otherPtr) {
LOCAL_VALUE(ptr);
Local<Value> other = otherPtr->ptr.Get(iso);
return value->StrictEquals(other);
}
4 changes: 4 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,7 @@ func (v *Value) SharedArrayBufferGetContents() ([]byte, func(), error) {

return byte_slice, release, nil
}

func (v *Value) StrictEquals(other *Value) bool {
return C.ValueStrictEquals(v.ptr, other.ptr) != 0
}
1 change: 1 addition & 0 deletions value.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ int ValueIsSharedArrayBuffer(ValuePtr ptr);
int ValueIsProxy(ValuePtr ptr);
int ValueIsWasmModuleObject(ValuePtr ptr);
int ValueIsModuleNamespaceObject(ValuePtr ptr);
int ValueStrictEquals(ValuePtr ptr, ValuePtr otherPtr);

extern ValuePtr NewValueNull(IsolatePtr iso_ptr);
extern ValuePtr NewValueUndefined(IsolatePtr iso_ptr);
Expand Down
36 changes: 36 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,39 @@ func TestValueArrayBufferContents(t *testing.T) {
t.Fatalf("Expected an error trying call SharedArrayBufferGetContents on value of incorrect type")
}
}

func TestValueStrictEquals(t *testing.T) {
ctx := v8.NewContext()
defer ctx.Close()

numberOne, err1 := ctx.RunScript("1", "")
numberOneB, err2 := ctx.RunScript("1", "")
numberTwo, err3 := ctx.RunScript("2", "")
stringOne, err4 := ctx.RunScript("'1'", "")
function, err5 := ctx.RunScript("const fn = () => {}; fn", "")
sameFunction, err6 := ctx.RunScript("fn", "")
anotherFunction, err7 := ctx.RunScript("const fn2 = () => {}; fn2", "")

if err := errorsJoin(err1, err2, err3, err4, err5, err6, err7); err != nil {
t.Fatal("Error getting test values", err)
}

if !numberOne.StrictEquals(numberOneB) {
t.Errorf("Number 1 and Number 1 should be strict equal")
}
if numberOne.StrictEquals(stringOne) {
t.Errorf("Number 1 and string '1' should not be strict equal")
}

if numberOne.StrictEquals(numberTwo) {
t.Errorf("Number 1 and number 2 should not be strict equal")
}

if !function.StrictEquals(sameFunction) {
t.Errorf("Getting the same function variable twice should be strict equal")
}

if function.StrictEquals(anotherFunction) {
t.Errorf("Comparing two different functions should not be strict equal")
}
}