One question about arrays

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

One question about arrays

Christian Comaschi
Hello,
I am writing a JSNative function taking an array as argument.
I have to do some operations on every element of the array:
RootedObject paramObj(cx, args[0].toObjectOrNull());
int iArrayLength = 0;
JS_GetArrayLength(cx, paramObj, &iArrayLength);
for (int i=0; i<iArrayLength; i++)
{
     JS::Value *pElement = NULL;
     JS_GetElement(cx, paramObj, 0, pElement);
     ...
}
Is this correct or should I declare a RootedValue to get a copy of the
element?
JS_GetElement expects a pointer: how can I pass the declared RootedValue
as an argument to JS_GetElement?
Thanks in advance
Regards,
Christian
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: One question about arrays

Till Schneidereit-2
Hi Christian,

On Mon, Mar 31, 2014 at 4:05 PM, Christian Comaschi <
[hidden email]> wrote:

> Hello,
> I am writing a JSNative function taking an array as argument.
> I have to do some operations on every element of the array:
> RootedObject paramObj(cx, args[0].toObjectOrNull());
> int iArrayLength = 0;
> JS_GetArrayLength(cx, paramObj, &iArrayLength);
> for (int i=0; i<iArrayLength; i++)
> {
>     JS::Value *pElement = NULL;
>     JS_GetElement(cx, paramObj, 0, pElement);
>     ...
> }
> Is this correct or should I declare a RootedValue to get a copy of the
> element?
> JS_GetElement expects a pointer: how can I pass the declared RootedValue
> as an argument to JS_GetElement?
>

Which version of SpiderMonkey are you using, I guess 24?

In any case, the current code as it exists in mozilla-central has
JS_GetElement changed to take a MutableHandleValue, so it actively requires
you to root the value. Even if you're using 24, you should absolutely root
pElement, though, and get the pointer using pElement.address().

(It's probably not important in this case, but should this be
performance-critical, you might want to move the declaration of pElement
out of the loop. That way, the Rooted will only be constructed once, and
reused for the different Values for each iteration.)


hth,
till
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: One question about arrays

Boris Zbarsky
In reply to this post by Christian Comaschi
On 3/31/14 10:05 AM, Christian Comaschi wrote:
> JS_GetArrayLength(cx, paramObj, &iArrayLength);

Do you completely control paramObj?  If not, this can throw, but you're
not checking for failure.

>      JS::Value *pElement = NULL;

This should be a RootedValue, with .address() passed to JS_GetElement.

>      JS_GetElement(cx, paramObj, 0, pElement);

s/0/i/ and again check for failure unless you completely control paramObj?

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine