Skip to content

Commit

Permalink
Add Undefined(iso) and Null(iso) functions (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith authored Sep 8, 2021
1 parent a05dd22 commit 0eeb865
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Build v8 with i18n support
- Access "this" from function callback
- value.SameValue(otherValue) function to compare values for sameness
- Undefined, Null functions to get these constant values for the isolate

### Changed
- Removed error return value from Context.Isolate() which never fails
Expand Down
5 changes: 5 additions & 0 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Isolate struct {
cbMutex sync.RWMutex
cbSeq int
cbs map[int]FunctionCallback

null *Value
undefined *Value
}

// HeapStatistics represents V8 isolate heap statistics
Expand Down Expand Up @@ -54,6 +57,8 @@ func NewIsolate() (*Isolate, error) {
ptr: C.NewIsolate(),
cbs: make(map[int]FunctionCallback),
}
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
// TODO: [RC] catch any C++ exceptions and return as error
return iso, nil
}
Expand Down
20 changes: 20 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,26 @@ ValuePtr NewValueString(IsolatePtr iso_ptr, const char* v) {
return tracked_value(ctx, val);
}

ValuePtr NewValueNull(IsolatePtr iso_ptr) {
ISOLATE_SCOPE_INTERNAL_CONTEXT(iso_ptr);
m_value* val = new m_value;
val->iso = iso;
val->ctx = ctx;
val->ptr = Persistent<Value, CopyablePersistentTraits<Value>>(
iso, Null(iso));
return tracked_value(ctx, val);
}

ValuePtr NewValueUndefined(IsolatePtr iso_ptr) {
ISOLATE_SCOPE_INTERNAL_CONTEXT(iso_ptr);
m_value* val = new m_value;
val->iso = iso;
val->ctx = ctx;
val->ptr = Persistent<Value, CopyablePersistentTraits<Value>>(
iso, Undefined(iso));
return tracked_value(ctx, val);
}

ValuePtr NewValueBoolean(IsolatePtr iso_ptr, int v) {
ISOLATE_SCOPE_INTERNAL_CONTEXT(iso_ptr);
m_value* val = new m_value;
Expand Down
2 changes: 2 additions & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ extern TemplatePtr NewFunctionTemplate(IsolatePtr iso_ptr, int callback_ref);
extern ValuePtr FunctionTemplateGetFunction(TemplatePtr ptr,
ContextPtr ctx_ptr);

extern ValuePtr NewValueNull(IsolatePtr iso_ptr);
extern ValuePtr NewValueUndefined(IsolatePtr iso_ptr);
extern ValuePtr NewValueInteger(IsolatePtr iso_ptr, int32_t v);
extern ValuePtr NewValueIntegerFromUnsigned(IsolatePtr iso_ptr, uint32_t v);
extern ValuePtr NewValueString(IsolatePtr iso_ptr, const char* v);
Expand Down
22 changes: 22 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ func (v *Value) value() *Value {
return v
}

func newValueNull(iso *Isolate) *Value {
return &Value{
ptr: C.NewValueNull(iso.ptr),
}
}

func newValueUndefined(iso *Isolate) *Value {
return &Value{
ptr: C.NewValueUndefined(iso.ptr),
}
}

// Undefined returns the `undefined` JS value
func Undefined(iso *Isolate) *Value {
return iso.undefined
}

// Null returns the `null` JS value
func Null(iso *Isolate) *Value {
return iso.null
}

// NewValue will create a primitive value. Supported values types to create are:
// string -> V8::String
// int32 -> V8::Integer
Expand Down
30 changes: 30 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,36 @@ func TestValueBoolean(t *testing.T) {
}
}

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

tests := [...]struct {
source string
value *v8go.Value
same bool
}{
{"undefined", v8go.Undefined(iso), true},
{"null", v8go.Null(iso), true},
{"undefined", v8go.Null(iso), false},
}

for _, tt := range tests {
tt := tt

val, err := ctx.RunScript(tt.source, "test.js")
failIf(t, err)

if tt.value.SameValue(val) != tt.same {
t.Errorf("SameValue on JS `%s` and V8 value %+v didn't return %v",
tt.source, tt.value, tt.same)
}
}
}

func TestValueArrayIndex(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext(nil)
Expand Down

0 comments on commit 0eeb865

Please sign in to comment.