Re: Embedding - functions with OUT parameters

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

Re: Embedding - functions with OUT parameters

Boris Zbarsky
On 1/9/16 2:11 PM, [hidden email] wrote:
> How are these out parameters passed back to the javascript objects?

The way this is done in XPConnect is that the function signature is
defined to take an object for each out parameter, and the implementation
sets a property named "value" on that object, whose value is the thing
being returned.

So in JS you would do:

   var longPath = {}, isFile = {};
   var exists = foo.exists(longPath, isFile);
   alert(exists, longPath.value, isFile.value);

It's pretty ugly, but insisting on out params in a language that doesn't
really have them is just going to be ugly.  :(

-Boris
_______________________________________________
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: Embedding - functions with OUT parameters

Boris Zbarsky
On 1/9/16 3:48 PM, [hidden email] wrote:
> Thanks, but how is it done in C/C++ API part?

Throw if args[n] is not an object, then

   JS_DefineProperty(cx, &args[n].toObject(), "value", whatever)

-Boris
_______________________________________________
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: Embedding - functions with OUT parameters

Benjamin Smedberg
In reply to this post by Boris Zbarsky


On 1/9/2016 3:09 PM, Boris Zbarsky wrote:

> On 1/9/16 2:11 PM, [hidden email] wrote:
>> How are these out parameters passed back to the javascript objects?
>
> The way this is done in XPConnect is that the function signature is
> defined to take an object for each out parameter, and the
> implementation sets a property named "value" on that object, whose
> value is the thing being returned.
>
> So in JS you would do:
>
>   var longPath = {}, isFile = {};
>   var exists = foo.exists(longPath, isFile);
>   alert(exists, longPath.value, isFile.value);
>
> It's pretty ugly, but insisting on out params in a language that
> doesn't really have them is just going to be ugly.  :(

If we were building XPConnect from scratch today, I'm pretty sure we'd
implement these as destructuring return values, e.g.

let [exists, isFile] = foo.exists(longPath);

But I doubt it's worth going back and doing this to xpconnect.

--BDS

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