Re: Array property of custom object
Mihai Dobrescu <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.jseng |
|---|---|
| Message-ID | <[email protected]> |
On Tuesday, May 3, 2016 at 5:01:42 AM UTC+3, Boris Zbarsky wrote:
> On 5/2/16 4:30 PM, Mihai Dobrescu wrote:
> > I need to e able to replace array elements, probably I will need to add/remove elements too
>
> OK. I have a hard time reconciling adding elements that with your claim
> that your backing storage is a constant-sized array, but I assume you
> know what you're doing.
>
> > Aren't all the JS objects containing private data proxies (the one with the JSCLASS_HAS_PRIVATE flag)?
>
> Not at all.
>
> -Boris
...well, I did not say js::proxies.
I could try to post a sample of my approach for a property getter t be passed to the JS_PSGS macro (extremely simplified as it is template based solution):
template<typename jsType, typename jsPropertyType, typename jsPropertyType::PrivateType PrivateType::*Property>
static bool GetProperty(JSContext *cx, unsigned argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
PrivateType* data = (PrivateType*)(JS_GetPrivate(&args.thisv().toObject()));
if (!data)
return false; // whatever...
JSObject *obj = JS_NewObject(cx, &jsDataClass<jsPropertyType>::fClass);
JS_SetPrivate(obj, new jsPropertyType::PrivateType(data->*Property));
args.rval().setObject(*obj);
return true;
}
where jsPropertyType is a wrapper for the property's type. jsType is the property owner wrapper object. All of these have a PrivateType typedef for the wrapped C type. It should be pretty clear for a person with SpiderMonkey knowledge.
It is my approach I could find for embedding API's types.
I could ask also, how do you know should be done? For any of these, simple and array properties. If you do it from scratch, what is your approach?