-
-
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?
Add object get property names #260
Conversation
d934d91
to
7e6661c
Compare
Codecov Report
@@ Coverage Diff @@
## master #260 +/- ##
==========================================
- Coverage 95.86% 95.60% -0.26%
==========================================
Files 17 17
Lines 580 592 +12
==========================================
+ Hits 556 566 +10
- Misses 15 16 +1
- Partials 9 10 +1
Continue to review full report at Codecov.
|
…make sur to free the array and the strings
84ff923
to
ebc0627
Compare
for (uint32_t i = 0; i < length; i++) { | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Will this miss freeing strings[0]
if the index doesn't get there? Should the middle param be i >=0
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.
Good catch!
We can't use i >=0
though, since the integer is unsigned, so that would always be true. If we wanted to do that, we could change i
to int64_t.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I think this inner loop is supposed to be from 0 to i
whatever i
is because it might need to free all strings if an error occurs which means that it might not be equal to length
and we'd be trying to free elements we don't have yet.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I think this inner loop is supposed to be from
0 to i
right, which is my code snippet started with length = i;
Your suggestion is what I meant by "or by using a separate loop variable", except that the for loop line needs to declare the j
variable by prefixing its assignment with its type: for (uint32_t j = 0; j < i; j++) {
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.
Ah right let’s go with your suggestion. Think I got confused by the inner loop variable having the same name.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
properties*
} | ||
length := rtn.length | ||
slice := (*[1 << 28]*C.char)(unsafe.Pointer(rtn.strings))[:length:length] | ||
var result []string |
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:
result := make([]string, len(slice))
for i := 0; i < len(slice); i++ {
s := slice[i]
defer C.free(unsafe.Pointer(s))
result[i] = C.GoString(s)
}
defer C.free(unsafe.Pointer(rtn.strings))
return result, nil
Related to #213
I'm trying to give a try to my first PR that adds new logic both in Go and in C++.
I'm not 100% sure that want I'm doing is the right thing to do and I would love some guidance if possible. @dylanahsmith @rogchap
Basically, I'm trying to return an array with all the property names from V8 using the
obj->GetPropertyNames(local_ctx)
.Is there anything that I'm missing?
Thanks 😄