JS_WrapObject and JS_InstanceOf/JS_GetClass/JS_GetPrivate

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

JS_WrapObject and JS_InstanceOf/JS_GetClass/JS_GetPrivate

ap
Hi
I have a question about problem with cross compartment solution

I have native implementation of some class:

JSClass TScript::s_testObjectClass[] =
{{
        "TestObjectClass", JSCLASS_HAS_PRIVATE,
        //... all as default
        JSCLASS_NO_OPTIONAL_MEMBERS
}};

with some functions:

JSFunctionSpec TScript::testClassFunctions[] =
{
        JS_FS("getNumber", tgetNumber, 0, 0),
        JS_FS("getString", tgetString, 0, 0),
        JS_FS_END
};

Interesting code of javascript here:
var someInternalValue = new TestObjectClass();
function dosomething()
{
        return someInternalValue;
}

The object "someInternalValue" is living in another compartment.

For getting this object from another compartment, i use JS_WrapObject
{
    JSAutoCompartment(context, another);
    jsval rcall = JSVAL_VOID;
    good = JS_EvaluateScript(context, another, script, ..., &rcall);
    ...
    good = JS_CallFunctionName(context, another, "dosomething", 0, NULL, &rcall);
    ...
    JSObject* resultObject = rcall.toObjectOrNull(); // mean TestObjectClass
    JS_InstanceOf(context, resultObject, TScript::s_testObjectClass, NULL); // OK
    return resultObject;
}

After leaving another compartment, we go back to our compartment and use JS_WrapObject
JS_WrapObject(cx, &resultObject);

And now resultObject mean Proxy
JS_InstanceOf(cx, resultObject, TScript::s_testObjectClass, NULL); // not OK, it is not TestPbjectClass

JS_GetClass will do not return my class
JS_GetPrivate will do not return my void* private data

but JS_CallFunctionName(cx, resultObject, "getNumber", 0, NULL, &rcall) calling through Proxy successfully

How can i use Proxy for determining my class and getting private pointer?

js/src at esr17 branch

Artem
Reply | Threaded
Open this post in threaded view
|

Re: JS_WrapObject and JS_InstanceOf/JS_GetClass/JS_GetPrivate

Jason Orendorff-2
On 2/24/14, 6:50 AM, ap wrote:
> How can i use Proxy for determining my class and getting private pointer?

The easiest thing is: in your C++ code, once you've detected that you
have a proxy, unwrap it to get the wrapped object, then enter that
object's compartment.

Once you're in the right compartment, all the usual APIs will work.

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

Re: JS_WrapObject and JS_InstanceOf/JS_GetClass/JS_GetPrivate

ap
In reply to this post by ap
I found what i need in js::UnwrapObjectChecked at jswrapper.h (esr17)
Thanks to Boris Zbarsky

The solution:
jsval val; // some value for analyze

void* getPrivateIfInstanceOf(JSContext* cx, jsval val, JSClass* clasp)
{
        if (!val.isObject())
        {
                return NULL;
        }
        JSObject* obj = val.toObjectOrNull();
        obj = js::UnwrapObjectChecked(cx, obj);
        {
                JSAutoCompartment ac(cx, obj);
                if (JS_InstanceOf(cx, obj, clasp, NULL) == JS_TRUE)
                {
                        return JS_GetPrivate(obj);
                }
        }
        return NULL;
}

Which is better solution?

Artem
Reply | Threaded
Open this post in threaded view
|

Re: JS_WrapObject and JS_InstanceOf/JS_GetClass/JS_GetPrivate

Jason Orendorff-2
On 2/26/14, 2:22 AM, ap wrote:
> Which is better solution?
They're the same. Boris and I suggested the same thing.

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