Bug 920322 - Provide better XDR interface for coding constants. r=luke

This commit is contained in:
Nicolas B. Pierron 2013-12-19 09:20:35 -08:00
parent fd58306d40
commit b605f62af4
4 changed files with 46 additions and 20 deletions

View File

@ -280,8 +280,8 @@ js::FillBindingVector(HandleScript fromScript, BindingVector *vec)
}
template<XDRMode mode>
static bool
XDRScriptConst(XDRState<mode> *xdr, HeapValue *vp)
bool
js::XDRScriptConst(XDRState<mode> *xdr, MutableHandleValue vp)
{
JSContext *cx = xdr->cx();
@ -302,20 +302,20 @@ XDRScriptConst(XDRState<mode> *xdr, HeapValue *vp)
uint32_t tag;
if (mode == XDR_ENCODE) {
if (vp->isInt32()) {
if (vp.isInt32()) {
tag = SCRIPT_INT;
} else if (vp->isDouble()) {
} else if (vp.isDouble()) {
tag = SCRIPT_DOUBLE;
} else if (vp->isString()) {
} else if (vp.isString()) {
tag = SCRIPT_ATOM;
} else if (vp->isTrue()) {
} else if (vp.isTrue()) {
tag = SCRIPT_TRUE;
} else if (vp->isFalse()) {
} else if (vp.isFalse()) {
tag = SCRIPT_FALSE;
} else if (vp->isNull()) {
} else if (vp.isNull()) {
tag = SCRIPT_NULL;
} else {
JS_ASSERT(vp->isUndefined());
JS_ASSERT(vp.isUndefined());
tag = SCRIPT_VOID;
}
}
@ -327,53 +327,59 @@ XDRScriptConst(XDRState<mode> *xdr, HeapValue *vp)
case SCRIPT_INT: {
uint32_t i;
if (mode == XDR_ENCODE)
i = uint32_t(vp->toInt32());
i = uint32_t(vp.toInt32());
if (!xdr->codeUint32(&i))
return false;
if (mode == XDR_DECODE)
vp->init(Int32Value(int32_t(i)));
vp.set(Int32Value(int32_t(i)));
break;
}
case SCRIPT_DOUBLE: {
double d;
if (mode == XDR_ENCODE)
d = vp->toDouble();
d = vp.toDouble();
if (!xdr->codeDouble(&d))
return false;
if (mode == XDR_DECODE)
vp->init(DoubleValue(d));
vp.set(DoubleValue(d));
break;
}
case SCRIPT_ATOM: {
RootedAtom atom(cx);
if (mode == XDR_ENCODE)
atom = &vp->toString()->asAtom();
atom = &vp.toString()->asAtom();
if (!XDRAtom(xdr, &atom))
return false;
if (mode == XDR_DECODE)
vp->init(StringValue(atom));
vp.set(StringValue(atom));
break;
}
case SCRIPT_TRUE:
if (mode == XDR_DECODE)
vp->init(BooleanValue(true));
vp.set(BooleanValue(true));
break;
case SCRIPT_FALSE:
if (mode == XDR_DECODE)
vp->init(BooleanValue(false));
vp.set(BooleanValue(false));
break;
case SCRIPT_NULL:
if (mode == XDR_DECODE)
vp->init(NullValue());
vp.set(NullValue());
break;
case SCRIPT_VOID:
if (mode == XDR_DECODE)
vp->init(UndefinedValue());
vp.set(UndefinedValue());
break;
}
return true;
}
template bool
js::XDRScriptConst(XDRState<XDR_ENCODE> *, MutableHandleValue);
template bool
js::XDRScriptConst(XDRState<XDR_DECODE> *, MutableHandleValue);
static inline uint32_t
FindBlockIndex(JSScript *script, StaticBlockObject &block)
{
@ -691,9 +697,14 @@ js::XDRScript(XDRState<mode> *xdr, HandleObject enclosingScope, HandleScript enc
if (nconsts) {
HeapValue *vector = script->consts()->vector;
RootedValue val(cx);
for (i = 0; i != nconsts; ++i) {
if (!XDRScriptConst(xdr, &vector[i]))
if (mode == XDR_ENCODE)
val = vector[i];
if (!XDRScriptConst(xdr, &val))
return false;
if (mode == XDR_DECODE)
vector[i].init(val);
}
}

View File

@ -520,6 +520,13 @@ JSScript *
CloneScript(JSContext *cx, HandleObject enclosingScope, HandleFunction fun, HandleScript script,
NewObjectKind newKind = GenericObject);
/*
* Code any constant value.
*/
template<XDRMode mode>
bool
XDRScriptConst(XDRState<mode> *xdr, MutableHandleValue vp);
} /* namespace js */
class JSScript : public js::gc::BarrieredCell<JSScript>

View File

@ -114,6 +114,13 @@ XDRState<mode>::codeScript(MutableHandleScript scriptp)
return true;
}
template<XDRMode mode>
bool
XDRState<mode>::codeConstValue(MutableHandleValue vp)
{
return XDRScriptConst(this, vp);
}
XDRDecoder::XDRDecoder(JSContext *cx, const void *data, uint32_t length,
JSPrincipals *principals, JSPrincipals *originPrincipals)
: XDRState<XDR_DECODE>(cx)

View File

@ -210,6 +210,7 @@ class XDRState {
bool codeFunction(JS::MutableHandleObject objp);
bool codeScript(MutableHandleScript scriptp);
bool codeConstValue(MutableHandleValue vp);
};
class XDREncoder : public XDRState<XDR_ENCODE> {