Skip to content

Commit

Permalink
Add value.SameValue(otherValue) function (rogchap#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith authored Sep 8, 2021
1 parent 9ec924d commit a05dd22
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support for calling constructors functions with NewInstance on Function
- Build v8 with i18n support
- Access "this" from function callback
- value.SameValue(otherValue) function to compare values for sameness

### Changed
- Removed error return value from Context.Isolate() which never fails
Expand Down
2 changes: 1 addition & 1 deletion function_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func goFunctionCallback(ctxref int, cbref int, thisAndArgs *C.ValuePtr, argsCoun
args: make([]*Value, argsCount),
}

argv := (*[1 << 30]C.ValuePtr)(unsafe.Pointer(thisAndArgs))[1:argsCount + 1:argsCount + 1]
argv := (*[1 << 30]C.ValuePtr)(unsafe.Pointer(thisAndArgs))[1 : argsCount+1 : argsCount+1]
for i, v := range argv {
val := &Value{ptr: v, ctx: ctx}
info.args[i] = val
Expand Down
11 changes: 11 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,17 @@ ValuePtr ValueToObject(ValuePtr ptr) {
return tracked_value(ctx, new_val);
}

int ValueSameValue(ValuePtr ptr, ValuePtr otherPtr) {
m_value* val1 = static_cast<m_value*>(ptr);
m_value* val2 = static_cast<m_value*>(otherPtr);

ISOLATE_SCOPE(val1->iso);
Local<Value> value1 = val1->ptr.Get(iso);
Local<Value> value2 = val2->ptr.Get(iso);

return value1->SameValue(value2);
}

int ValueIsUndefined(ValuePtr ptr) {
LOCAL_VALUE(ptr);
return value->IsUndefined();
Expand Down
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const char* ValueToDetailString(ValuePtr ptr);
uint32_t ValueToUint32(ValuePtr ptr);
extern ValueBigInt ValueToBigInt(ValuePtr ptr);
extern ValuePtr ValueToObject(ValuePtr ptr);
int ValueSameValue(ValuePtr ptr, ValuePtr otherPtr);
int ValueIsUndefined(ValuePtr ptr);
int ValueIsNull(ValuePtr ptr);
int ValueIsNullOrUndefined(ValuePtr ptr);
Expand Down
6 changes: 6 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func (v *Value) Uint32() uint32 {
return uint32(C.ValueToUint32(v.ptr))
}

// SameValue returns true if the other value is the same value.
// This is equivalent to `Object.is(v, other)` in JS.
func (v *Value) SameValue(other *Value) bool {
return C.ValueSameValue(v.ptr, other.ptr) != 0
}

// IsUndefined returns true if this value is the undefined value. See ECMA-262 4.3.10.
func (v *Value) IsUndefined() bool {
return C.ValueIsUndefined(v.ptr) != 0
Expand Down
21 changes: 21 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,27 @@ func TestValueFunction(t *testing.T) {

}

func TestValueSameValue(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
defer iso.Dispose()
ctx, _ := v8go.NewContext(iso)
defer ctx.Close()

objTempl := v8go.NewObjectTemplate(iso)
obj1, err := objTempl.NewInstance(ctx)
failIf(t, err)
obj2, err := objTempl.NewInstance(ctx)
failIf(t, err)

if obj1.Value.SameValue(obj2.Value) != false {
t.Errorf("SameValue on two different values didn't return false")
}
if obj1.Value.SameValue(obj1.Value) != true {
t.Errorf("SameValue on two of the same value didn't return true")
}
}

func TestValueIsXXX(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
Expand Down

0 comments on commit a05dd22

Please sign in to comment.