Skip to content

Commit

Permalink
Return char pointer array from C for displaying Object property names
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Dec 23, 2021
1 parent 7eb0b5d commit 838df68
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
11 changes: 6 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ func valueResult(ctx *Context, rtn C.RtnValue) (*Value, error) {
return &Value{rtn.value, ctx}, nil
}

func valueResults(ctx *Context, rtn C.RtnValues) []*Value {
func valueStrings(ctx *Context, rtn C.RtnStrings) []string {
length := rtn.length
slice := (*[1 << 28]C.ValuePtr)(unsafe.Pointer(rtn.values))[:length:length]
var values []*Value
slice := (*[1 << 28]*C.char)(unsafe.Pointer(rtn.strings))[:length:length]
var result []string
for i := 0; i < len(slice); i++ {
values = append(values, &Value{slice[i], ctx})
s := slice[i]
result = append(result, C.GoString(s))
}
return values
return result
}

func objectResult(ctx *Context, rtn C.RtnValue) (*Object, error) {
Expand Down
4 changes: 2 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ 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) GetPropertyNames() []*Value {
func (o *Object) GetPropertyNames() []string {
rtn := C.ObjectGetPropertyNames(o.ptr)
return valueResults(o.ctx, rtn)
return valueStrings(o.ctx, rtn)
}

// SetInternalField sets the value of an internal field for an ObjectTemplate instance.
Expand Down
8 changes: 6 additions & 2 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package v8go_test

import (
"fmt"
"reflect"
"testing"

v8 "rogchap.com/v8go"
Expand Down Expand Up @@ -202,9 +203,12 @@ func TestObjectGetPropertyNames(t *testing.T) {
obj.Set("foo", "foobar")
obj.Set("hello", "world")

values := obj.GetPropertyNames()
expectedProperties := []string{"bar2", "foo", "hello"}
properties := obj.GetPropertyNames()

fmt.Println(values)
if !reflect.DeepEqual(properties, expectedProperties) {
t.Error("properteis are not the same")
}
}

func ExampleObject_global() {
Expand Down
16 changes: 6 additions & 10 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1291,27 +1291,23 @@ void ObjectSet(ValuePtr ptr, const char* key, ValuePtr prop_val) {
obj->Set(local_ctx, key_val, prop_val->ptr.Get(iso)).Check();
}

RtnValues ObjectGetPropertyNames(ValuePtr ptr) {
RtnStrings ObjectGetPropertyNames(ValuePtr ptr) {
LOCAL_OBJECT(ptr);
RtnValues rtn = {};
RtnStrings rtn = {};

MaybeLocal<Array> maybe_names = obj->GetPropertyNames(local_ctx);
Local<Array> names = maybe_names.ToLocalChecked();

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

for (int i = 0; i < length; i++) {
Local<Value> name_from_array = names->Get(local_ctx, i).ToLocalChecked();
m_value* new_val = new m_value;
new_val->iso = iso;
new_val->ctx = ctx;
new_val->ptr =
Persistent<Value, CopyablePersistentTraits<Value>>(iso, name_from_array);
array[i] = new_val;
String::Utf8Value ds(iso, name_from_array);
strings[i] = CopyString(ds);
}

rtn.values = array;
rtn.strings = strings;
rtn.length = length;
return rtn;
}
Expand Down
12 changes: 6 additions & 6 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ typedef struct {
} RtnValue;

typedef struct {
int length;
const char* string;
RtnError error;
ValuePtr *values;
} RtnValues;
} RtnString;

typedef struct {
const char* string;
const char **strings;
int length;
RtnError error;
} RtnString;
} RtnStrings;

typedef struct {
size_t total_heap_size;
Expand Down Expand Up @@ -276,7 +276,7 @@ int ValueIsModuleNamespaceObject(ValuePtr ptr);

extern void ObjectSet(ValuePtr ptr, const char* key, ValuePtr val_ptr);
extern void ObjectSetIdx(ValuePtr ptr, uint32_t idx, ValuePtr val_ptr);
RtnValues ObjectGetPropertyNames(ValuePtr ptr);
RtnStrings ObjectGetPropertyNames(ValuePtr ptr);
extern int ObjectSetInternalField(ValuePtr ptr, int idx, ValuePtr val_ptr);
extern int ObjectInternalFieldCount(ValuePtr ptr);
extern RtnValue ObjectGet(ValuePtr ptr, const char* key);
Expand Down

0 comments on commit 838df68

Please sign in to comment.