Serialize JSScript which has objects

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

Serialize JSScript which has objects

wsj1518
Hi. I'm trying to serialize JSScript for cross-execution reuse.

I've encoded and decoded JSScripts which have no object by XDR api(script->hasObjects() == 0)

But I can't encode JSScript when it has objects.

This is a part of jsscript.cpp, js::XDRScript().

////////////////////////////////////////////////////////////////////////////
/*
 * Here looping from 0-to-length to xdr objects is essential to ensure that
 * all references to enclosing blocks (via FindBlockIndex below) happen
 * after the enclosing block has been XDR'd.
 */
 for (i = 0; i != nobjects; ++i) {

    HeapPtr<JSObject> *objp = &script->objects()->vector[i];
    uint32_t isBlock;
    if (mode == XDR_ENCODE) {
        JSObject *obj = *objp;
        JS_ASSERT(obj->is<JSFunction>() || obj->is<StaticBlockObject>());

        isBlock = obj->is<BlockObject>() ? 1 : 0;
    }
    if (!xdr->codeUint32(&isBlock))
        return false;
/////////////////////////////////////////////////////////////////////////////

When I run jsbench, there is a script which has 4 objects.

It's composed of 3 functions and 1 object.

The problem is a object. It occurs assertion and there's no way to serialize it even without assertion.

Is it impossible to serialize this kind of JSScript?

Thanks!
_______________________________________________
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: Serialize JSScript which has objects

Nicolas B. Pierron
On 09/08/2015 03:50 PM, [hidden email] wrote:

> Hi. I'm trying to serialize JSScript for cross-execution reuse.
>
> I've encoded and decoded JSScripts which have no object by XDR api(script->hasObjects() == 0)
>
> But I can't encode JSScript when it has objects.
>
> This is a part of jsscript.cpp, js::XDRScript().
>
>[…]
>
> The problem is a object. It occurs assertion and there's no way to serialize it even without assertion.
>
> Is it impossible to serialize this kind of JSScript?

This is possible, but you will have to set a flag on your compartment, to
prevent the runtime from re-using the objects which are stored in the
JSScript, and instead make a copy of it when the JSOP_OBJECT opcode is
visited in the interpreter.

   JS::CompartmentOptionsRef(cx).setCloneSingletons(true);

The reason why it is not possible to serialize is due to the fact that these
instruction are executed once, and we assume that the code take the
ownership of the object.  Attempting to serialize this object will lead to
some problem as these objects are mutable.  We could easily serialize a
json-like object, but then re-loading from the serialized version will load
the mutated object, and not the one present in the code originally.

Thus, as we do not want to change the semantic between loading from sources,
and loading from the serialized bytecode, we have to add this limitation
when the bytecode re-use the objects of the JSOP_OBJECT instruction.

--
Nicolas B. Pierron
_______________________________________________
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: Serialize JSScript which has objects

Boris Zbarsky
In reply to this post by wsj1518
On 9/8/15 9:50 AM, [hidden email] wrote:
> This is a part of jsscript.cpp, js::XDRScript().

Which SpiderMonkey version is this?  On mozilla-central, XDRScript seems
to handle the JSOP_OBJECT objects just fine...

There are several possible answers to this question depending on which
exact version is being used here.

-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: Serialize JSScript which has objects

wsj1518
In reply to this post by Nicolas B. Pierron
2015년 9월 8일 화요일 오후 11시 5분 36초 UTC+9, Nicolas B. Pierron 님의 말:

> On 09/08/2015 03:50 PM, [hidden email] wrote:
> > Hi. I'm trying to serialize JSScript for cross-execution reuse.
> >
> > I've encoded and decoded JSScripts which have no object by XDR api(script->hasObjects() == 0)
> >
> > But I can't encode JSScript when it has objects.
> >
> > This is a part of jsscript.cpp, js::XDRScript().
> >
> >[…]
> >
> > The problem is a object. It occurs assertion and there's no way to serialize it even without assertion.
> >
> > Is it impossible to serialize this kind of JSScript?
>
> This is possible, but you will have to set a flag on your compartment, to
> prevent the runtime from re-using the objects which are stored in the
> JSScript, and instead make a copy of it when the JSOP_OBJECT opcode is
> visited in the interpreter.
>
>    JS::CompartmentOptionsRef(cx).setCloneSingletons(true);
>
> The reason why it is not possible to serialize is due to the fact that these
> instruction are executed once, and we assume that the code take the
> ownership of the object.  Attempting to serialize this object will lead to
> some problem as these objects are mutable.  We could easily serialize a
> json-like object, but then re-loading from the serialized version will load
> the mutated object, and not the one present in the code originally.
>
> Thus, as we do not want to change the semantic between loading from sources,
> and loading from the serialized bytecode, we have to add this limitation
> when the bytecode re-use the objects of the JSOP_OBJECT instruction.
>
> --
> Nicolas B. Pierron

Thanks for reply.

I fix this by using the latest version of spidermonkey!
_______________________________________________
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: Serialize JSScript which has objects

wsj1518
In reply to this post by Boris Zbarsky
2015년 9월 8일 화요일 오후 11시 16분 30초 UTC+9, Boris Zbarsky 님의 말:

> On 9/8/15 9:50 AM, [hidden email] wrote:
> > This is a part of jsscript.cpp, js::XDRScript().
>
> Which SpiderMonkey version is this?  On mozilla-central, XDRScript seems
> to handle the JSOP_OBJECT objects just fine...
>
> There are several possible answers to this question depending on which
> exact version is being used here.
>
> -Boris

Thank you, Boris! You were right. I was using past version of spider monkey.

By the way, I succeeded serializing/deserializing JSScript on mozilla-central with commenting a few lines out. (I executed jsbench)

line 629 in js::XDRScript, jsscript.cpp
//MOZ_ASSERT(script->functionNonDelazifying() == fun);

line 1152-1153 in js::XDRScript, jsscript.cpp
//if (!XDRRelazificationInfo(xdr, fun, script, enclosingScope, &lazy))
//    return false;

I guess this is okey...

Is it truly all right to run spidermonkey without any problems?
_______________________________________________
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: Serialize JSScript which has objects

Boris Zbarsky
On 9/9/15 10:44 AM, [hidden email] wrote:
> I guess this is okey...

I would guess it's very much not.

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