-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Add object get property names #260
base: master
Are you sure you want to change the base?
Changes from all commits
295ff1a
6b3e465
1c87026
fb170db
15f2c0c
6dab91b
b70d63b
97bce5a
b6fc419
ebc0627
fab0a50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package v8go_test | |
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
v8 "rogchap.com/v8go" | ||
|
@@ -202,6 +203,27 @@ func TestObjectDelete(t *testing.T) { | |
|
||
} | ||
|
||
func TestGetEnumerablePropertyNames(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := v8.NewContext() | ||
defer ctx.Isolate().Dispose() | ||
defer ctx.Close() | ||
val, _ := ctx.RunScript("const foo = {}; foo", "") | ||
obj, _ := val.AsObject() | ||
obj.Set("bar2", "baz2") | ||
obj.Set("foo", "foobar") | ||
obj.Set("hello", "world") | ||
|
||
expectedProperties := []string{"bar2", "foo", "hello"} | ||
properties, err := obj.GetEnumerablePropertyNames() | ||
fatalIf(t, err) | ||
|
||
if !reflect.DeepEqual(properties, expectedProperties) { | ||
t.Error("properteis are not the same") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. properties* |
||
} | ||
} | ||
|
||
func ExampleObject_global() { | ||
iso := v8.NewIsolate() | ||
defer iso.Dispose() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1297,6 +1297,38 @@ void ObjectSet(ValuePtr ptr, const char* key, ValuePtr prop_val) { | |
obj->Set(local_ctx, key_val, prop_val->ptr.Get(iso)).Check(); | ||
} | ||
|
||
RtnStrings ObjectGetPropertyNames(ValuePtr ptr) { | ||
LOCAL_OBJECT(ptr); | ||
RtnStrings rtn = {}; | ||
|
||
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 = (const char**)malloc(length * sizeof(const char*)); | ||
|
||
for (uint32_t i = 0; i < length; i++) { | ||
genevieve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Local<Value> name_from_array; | ||
if (!names->Get(local_ctx, i).ToLocal(&name_from_array)) { | ||
for (i = i - 1; i > 0; i--) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this miss freeing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! We can't use Would the code maybe be clearer if it matched the outer loop, e.g. length = i;
for (i = 0; i < length; i++) {
free(&strings[i]); or by using a separate loop variable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this inner loop is supposed to be from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it should be: for (uint32_t i = 0; i < length; i++) {
Local<Value> name_from_array;
if (!names->Get(local_ctx, i).ToLocal(&name_from_array)) {
for (j = 0; j < i; j++) {
free(&strings[j]);
}
free(strings);
rtn.error = ExceptionError(try_catch, iso, local_ctx);
return rtn;
}
String::Utf8Value ds(iso, name_from_array);
strings[i] = CopyString(ds);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
right, which is my code snippet started with Your suggestion is what I meant by "or by using a separate loop variable", except that the for loop line needs to declare the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right let’s go with your suggestion. Think I got confused by the inner loop variable having the same name. |
||
free(&strings[i]); | ||
} | ||
free(strings); | ||
rtn.error = ExceptionError(try_catch, iso, local_ctx); | ||
return rtn; | ||
} | ||
String::Utf8Value ds(iso, name_from_array); | ||
strings[i] = CopyString(ds); | ||
} | ||
|
||
rtn.strings = strings; | ||
rtn.length = length; | ||
return rtn; | ||
} | ||
|
||
void ObjectSetIdx(ValuePtr ptr, uint32_t idx, ValuePtr prop_val) { | ||
LOCAL_OBJECT(ptr); | ||
obj->Set(local_ctx, idx, prop_val->ptr.Get(iso)).Check(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we know the size of
result
, we should be able to preallocate: