Skip to content

Commit

Permalink
make sure to use ToLocal rather than ToLocalChecked
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jan 11, 2022
1 parent 19f8b5c commit e289647
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ func valueResult(ctx *Context, rtn C.RtnValue) (*Value, error) {
return &Value{rtn.value, ctx}, nil
}

func valueStrings(ctx *Context, rtn C.RtnStrings) []string {
func valueStrings(ctx *Context, rtn C.RtnStrings) ([]string, error) {
if rtn.strings == nil {
return []string{}, newJSError(rtn.error)
}
length := rtn.length
slice := (*[1 << 28]*C.char)(unsafe.Pointer(rtn.strings))[:length:length]
var result []string
Expand All @@ -179,7 +182,7 @@ func valueStrings(ctx *Context, rtn C.RtnStrings) []string {
result = append(result, C.GoString(s))
}
defer C.free(unsafe.Pointer(rtn.strings))
return result
return result, nil
}

func objectResult(ctx *Context, rtn C.RtnValue) (*Object, error) {
Expand Down
2 changes: 1 addition & 1 deletion object.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (o *Object) SetIdx(idx uint32, val interface{}) error {

// Returns an array containing the names of the enumerable properties of this object,
// including properties from prototype objects
func (o *Object) GetEnumerablePropertyNames() []string {
func (o *Object) GetEnumerablePropertyNames() ([]string, error) {
rtn := C.ObjectGetPropertyNames(o.ptr)
return valueStrings(o.ctx, rtn)
}
Expand Down
3 changes: 2 additions & 1 deletion object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func TestGetEnumerablePropertyNames(t *testing.T) {
obj.Set("hello", "world")

expectedProperties := []string{"bar2", "foo", "hello"}
properties := obj.GetEnumerablePropertyNames()
properties, err := obj.GetEnumerablePropertyNames()
fatalIf(t, err)

if !reflect.DeepEqual(properties, expectedProperties) {
t.Error("properteis are not the same")
Expand Down
13 changes: 10 additions & 3 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1294,14 +1294,21 @@ RtnStrings ObjectGetPropertyNames(ValuePtr ptr) {
LOCAL_OBJECT(ptr);
RtnStrings rtn = {};

MaybeLocal<Array> maybe_names = obj->GetPropertyNames(local_ctx);
Local<Array> names = maybe_names.ToLocalChecked();
Local<Array> names;
if (!obj->GetPropertyNames(local_ctx).ToLocal(&names)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}

uint32_t length = names->Length();
const char** strings = new const char*[length];

for (uint32_t i = 0; i < length; i++) {
Local<Value> name_from_array = names->Get(local_ctx, i).ToLocalChecked();
Local<Value> name_from_array;
if (!names->Get(local_ctx, i).ToLocal(&name_from_array)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}
String::Utf8Value ds(iso, name_from_array);
strings[i] = CopyString(ds);
}
Expand Down

0 comments on commit e289647

Please sign in to comment.