Bug 880697 - Stop using clunky C API in JSD and start using RAII classes. r=gabor

This commit is contained in:
Bobby Holley 2013-06-12 14:17:54 -07:00
Родитель c0601a55a3
Коммит f66756e79c
2 изменённых файлов: 89 добавлений и 152 удалений

Просмотреть файл

@ -504,7 +504,6 @@ uintptr_t
jsd_GetClosestPC(JSDContext* jsdc, JSDScript* jsdscript, unsigned line)
{
uintptr_t pc;
JSCompartment *oldCompartment;
if( !jsdscript )
return 0;
@ -518,24 +517,24 @@ jsd_GetClosestPC(JSDContext* jsdc, JSDScript* jsdscript, unsigned line)
}
#endif
oldCompartment = JS_EnterCompartmentOfScript(jsdc->dumbContext, jsdscript->script);
pc = (uintptr_t) JS_LineNumberToPC(jsdc->dumbContext, jsdscript->script, line );
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
JSContext *cx = jsdc->dumbContext;
JSAutoCompartment ac(cx, jsdscript->script);
pc = (uintptr_t) JS_LineNumberToPC(cx, jsdscript->script, line );
return pc;
}
unsigned
jsd_GetClosestLine(JSDContext* jsdc, JSDScript* jsdscript, uintptr_t pc)
{
JSCompartment* oldCompartment;
unsigned first = jsdscript->lineBase;
unsigned last = first + jsd_GetScriptLineExtent(jsdc, jsdscript) - 1;
unsigned line = 0;
oldCompartment = JS_EnterCompartmentOfScript(jsdc->dumbContext, jsdscript->script);
if (pc)
if (pc) {
JSContext *cx = jsdc->dumbContext;
JSAutoCompartment ac(cx, jsdscript->script);
line = JS_PCToLineNumber(jsdc->dumbContext, jsdscript->script, (jsbytecode*)pc);
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
}
if( line < first )
return first;
@ -559,7 +558,6 @@ jsd_GetLinePCs(JSDContext* jsdc, JSDScript* jsdscript,
unsigned startLine, unsigned maxLines,
unsigned* count, unsigned** retLines, uintptr_t** retPCs)
{
JSCompartment* oldCompartment;
unsigned first = jsdscript->lineBase;
unsigned last = first + jsd_GetScriptLineExtent(jsdc, jsdscript) - 1;
JSBool ok;
@ -569,7 +567,8 @@ jsd_GetLinePCs(JSDContext* jsdc, JSDScript* jsdscript,
if (last < startLine)
return JS_TRUE;
oldCompartment = JS_EnterCompartmentOfScript(jsdc->dumbContext, jsdscript->script);
JSContext *cx = jsdc->dumbContext;
JSAutoCompartment ac(cx, jsdscript->script);
ok = JS_GetLinePCs(jsdc->dumbContext, jsdscript->script,
startLine, maxLines,
@ -585,7 +584,6 @@ jsd_GetLinePCs(JSDContext* jsdc, JSDScript* jsdscript,
JS_free(jsdc->dumbContext, pcs);
}
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
return ok;
}
@ -614,13 +612,12 @@ jsd_GetScriptHook(JSDContext* jsdc, JSD_ScriptHookProc* hook, void** callerdata)
JSBool
jsd_EnableSingleStepInterrupts(JSDContext* jsdc, JSDScript* jsdscript, JSBool enable)
{
JSCompartment* oldCompartment;
JSBool rv;
oldCompartment = JS_EnterCompartmentOfScript(jsdc->dumbContext, jsdscript->script);
JSContext *cx = jsdc->dumbContext;
JSAutoCompartment ac(cx, jsdscript->script);
JSD_LOCK();
rv = JS_SetSingleStepMode(jsdc->dumbContext, jsdscript->script, enable);
JSD_UNLOCK();
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
return rv;
}
@ -830,7 +827,6 @@ jsd_SetExecutionHook(JSDContext* jsdc,
{
JSDExecHook* jsdhook;
JSBool rv;
JSCompartment* oldCompartment;
JSD_LOCK();
if( ! hook )
@ -860,13 +856,13 @@ jsd_SetExecutionHook(JSDContext* jsdc,
jsdhook->hook = hook;
jsdhook->callerdata = callerdata;
oldCompartment = JS_EnterCompartmentOfScript(jsdc->dumbContext, jsdscript->script);
rv = JS_SetTrap(jsdc->dumbContext, jsdscript->script,
(jsbytecode*)pc, jsd_TrapHandler,
PRIVATE_TO_JSVAL(jsdhook));
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
{
JSContext *cx = jsdc->dumbContext;
JSAutoCompartment ac(cx, jsdscript->script);
rv = JS_SetTrap(cx, jsdscript->script,
(jsbytecode*)pc, jsd_TrapHandler,
PRIVATE_TO_JSVAL(jsdhook));
}
if ( ! rv ) {
free(jsdhook);
@ -885,7 +881,6 @@ jsd_ClearExecutionHook(JSDContext* jsdc,
JSDScript* jsdscript,
uintptr_t pc)
{
JSCompartment* oldCompartment;
JSDExecHook* jsdhook;
JSD_LOCK();
@ -897,12 +892,12 @@ jsd_ClearExecutionHook(JSDContext* jsdc,
return JS_FALSE;
}
oldCompartment = JS_EnterCompartmentOfScript(jsdc->dumbContext, jsdscript->script);
JS_ClearTrap(jsdc->dumbContext, jsdscript->script,
(jsbytecode*)pc, NULL, NULL );
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
{
JSContext *cx = jsdc->dumbContext;
JSAutoCompartment ac(cx, jsdscript->script);
JS_ClearTrap(jsdc->dumbContext, jsdscript->script,
(jsbytecode*)pc, NULL, NULL );
}
JS_REMOVE_LINK(&jsdhook->links);
free(jsdhook);

Просмотреть файл

@ -119,22 +119,17 @@ JSBool
jsd_IsValueNative(JSDContext* jsdc, JSDValue* jsdval)
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JS::RootedFunction fun(cx);
JSExceptionState* exceptionState;
JSCompartment* oldCompartment = NULL;
if(jsd_IsValueFunction(jsdc, jsdval))
{
JSAutoCompartment ac(cx, JSVAL_TO_OBJECT(jsdval->val));
AutoSaveExceptionState as(cx);
JSBool ok = JS_FALSE;
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(cx, JSVAL_TO_OBJECT(jsdval->val));
exceptionState = JS_SaveExceptionState(cx);
fun = JSD_GetValueFunction(jsdc, jsdval);
JS_RestoreExceptionState(cx, exceptionState);
if(fun)
ok = JS_GetFunctionScript(cx, fun) ? JS_FALSE : JS_TRUE;
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
JS_ASSERT(fun);
return ok;
}
@ -173,8 +168,7 @@ JSString*
jsd_GetValueString(JSDContext* jsdc, JSDValue* jsdval)
{
JSContext* cx = jsdc->dumbContext;
JSExceptionState* exceptionState;
JSCompartment* oldCompartment = NULL;
JSAutoRequest ar(cx);
JS::RootedValue stringval(cx);
JS::RootedString string(cx);
JS::RootedObject scopeObj(cx);
@ -188,27 +182,19 @@ jsd_GetValueString(JSDContext* jsdc, JSDValue* jsdval)
return jsdval->string;
}
JS_BeginRequest(cx);
/* Objects call JS_ValueToString in their own compartment. */
scopeObj = !JSVAL_IS_PRIMITIVE(jsdval->val) ? JSVAL_TO_OBJECT(jsdval->val) : jsdc->glob;
oldCompartment = JS_EnterCompartment(cx, scopeObj);
exceptionState = JS_SaveExceptionState(cx);
string = JS_ValueToString(cx, jsdval->val);
JS_RestoreExceptionState(cx, exceptionState);
JS_LeaveCompartment(cx, oldCompartment);
oldCompartment = NULL;
{
JSAutoCompartment ac(cx, scopeObj);
AutoSaveExceptionState as(cx);
string = JS_ValueToString(cx, jsdval->val);
}
JSAutoCompartment ac2(cx, jsdc->glob);
if(string) {
stringval = STRING_TO_JSVAL(string);
oldCompartment = JS_EnterCompartment(cx, jsdc->glob);
}
if(!string || !JS_WrapValue(cx, stringval.address())) {
if(oldCompartment)
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
return NULL;
}
@ -216,9 +202,6 @@ jsd_GetValueString(JSDContext* jsdc, JSDValue* jsdval)
if(!JS_AddNamedStringRoot(cx, &jsdval->string, "ValueString"))
jsdval->string = NULL;
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
return jsdval->string;
}
@ -226,20 +209,14 @@ JSString*
jsd_GetValueFunctionId(JSDContext* jsdc, JSDValue* jsdval)
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JS::RootedFunction fun(cx);
JSExceptionState* exceptionState;
JSCompartment* oldCompartment = NULL;
if(!jsdval->funName && jsd_IsValueFunction(jsdc, jsdval))
{
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(jsdc->dumbContext, JSVAL_TO_OBJECT(jsdval->val));
exceptionState = JS_SaveExceptionState(cx);
JSAutoCompartment ac(cx, JSVAL_TO_OBJECT(jsdval->val));
AutoSaveExceptionState as(cx);
fun = JSD_GetValueFunction(jsdc, jsdval);
JS_RestoreExceptionState(cx, exceptionState);
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
if(!fun)
return NULL;
jsdval->funName = JS_GetFunctionId(fun);
@ -262,8 +239,8 @@ JSDValue*
jsd_NewValue(JSDContext* jsdc, jsval value)
{
JS::RootedValue val(jsdc->dumbContext, value);
JSAutoRequest ar(jsdc->dumbContext);
JSDValue* jsdval;
JSCompartment* oldCompartment = NULL;
if(!(jsdval = (JSDValue*) calloc(1, sizeof(JSDValue))))
return NULL;
@ -271,9 +248,7 @@ jsd_NewValue(JSDContext* jsdc, jsval value)
if(JSVAL_IS_GCTHING(val))
{
JSBool ok;
JS_BeginRequest(jsdc->dumbContext);
oldCompartment = JS_EnterCompartment(jsdc->dumbContext, jsdc->glob);
JSAutoCompartment ac(jsdc->dumbContext, jsdc->glob);
ok = JS_AddNamedValueRoot(jsdc->dumbContext, &jsdval->val, "JSDValue");
if(ok && JSVAL_IS_STRING(val)) {
@ -282,8 +257,6 @@ jsd_NewValue(JSDContext* jsdc, jsval value)
}
}
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
JS_EndRequest(jsdc->dumbContext);
if(!ok)
{
free(jsdval);
@ -300,19 +273,15 @@ jsd_NewValue(JSDContext* jsdc, jsval value)
void
jsd_DropValue(JSDContext* jsdc, JSDValue* jsdval)
{
JSCompartment* oldCompartment = NULL;
JS_ASSERT(jsdval->nref > 0);
if(0 == --jsdval->nref)
{
jsd_RefreshValue(jsdc, jsdval);
if(JSVAL_IS_GCTHING(jsdval->val))
{
JS_BeginRequest(jsdc->dumbContext);
oldCompartment = JS_EnterCompartment(jsdc->dumbContext, jsdc->glob);
JSAutoRequest ar(jsdc->dumbContext);
JSAutoCompartment ac(jsdc->dumbContext, jsdc->glob);
JS_RemoveValueRoot(jsdc->dumbContext, &jsdval->val);
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
JS_EndRequest(jsdc->dumbContext);
}
free(jsdval);
}
@ -384,10 +353,10 @@ static void _freeProps(JSDContext* jsdc, JSDValue* jsdval)
static JSBool _buildProps(JSDContext* jsdc, JSDValue* jsdval)
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JS::RootedObject obj(cx);
JSPropertyDescArray pda;
unsigned i;
JSCompartment* oldCompartment = NULL;
JS_ASSERT(JS_CLIST_IS_EMPTY(&jsdval->props));
JS_ASSERT(!(CHECK_BIT_FLAG(jsdval->flags, GOT_PROPS)));
@ -398,13 +367,10 @@ static JSBool _buildProps(JSDContext* jsdc, JSDValue* jsdval)
obj = JSVAL_TO_OBJECT(jsdval->val);
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(jsdc->dumbContext, obj);
JSAutoCompartment ac(cx, obj);
if(!JS_GetPropertyDescArray(cx, obj, &pda))
{
JS_EndRequest(cx);
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
return JS_FALSE;
}
@ -419,8 +385,6 @@ static JSBool _buildProps(JSDContext* jsdc, JSDValue* jsdval)
JS_APPEND_LINK(&prop->links, &jsdval->props);
}
JS_PutPropertyDescArray(cx, &pda);
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
JS_EndRequest(cx);
SET_BIT_FLAG(jsdval->flags, GOT_PROPS);
return !JS_CLIST_IS_EMPTY(&jsdval->props);
}
@ -432,18 +396,14 @@ void
jsd_RefreshValue(JSDContext* jsdc, JSDValue* jsdval)
{
JSContext* cx = jsdc->dumbContext;
JSCompartment* oldCompartment = NULL;
JSAutoRequest ar(cx);
if(jsdval->string)
{
/* if the jsval is a string, then we didn't need to root the string */
if(!JSVAL_IS_STRING(jsdval->val))
{
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(cx, jsdc->glob);
JSAutoCompartment ac(cx, jsdc->glob);
JS_RemoveStringRoot(cx, &jsdval->string);
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
}
jsdval->string = NULL;
}
@ -504,6 +464,7 @@ JSDProperty*
jsd_GetValueProperty(JSDContext* jsdc, JSDValue* jsdval, JSString* nameStr)
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JSDProperty* jsdprop;
JSDProperty* iter = NULL;
JS::RootedObject obj(cx);
@ -515,7 +476,6 @@ jsd_GetValueProperty(JSDContext* jsdc, JSDValue* jsdval, JSString* nameStr)
size_t nameLen;
JS::RootedValue val(cx), nameval(cx);
JS::RootedId nameid(cx);
JSCompartment* oldCompartment = NULL;
if(!jsd_IsValueObject(jsdc, jsdval))
return NULL;
@ -539,44 +499,38 @@ jsd_GetValueProperty(JSDContext* jsdc, JSDValue* jsdval, JSString* nameStr)
if (!(nameChars = JS_GetStringCharsZAndLength(cx, name, &nameLen)))
return NULL;
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(cx, obj);
JS_GetUCPropertyAttributes(cx, obj, nameChars, nameLen, &attrs, &found);
if (!found)
{
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
return NULL;
}
JSAutoCompartment ac(cx, obj);
JS_ClearPendingException(cx);
if(!JS_GetUCProperty(cx, obj, nameChars, nameLen, val.address()))
{
if (JS_IsExceptionPending(cx))
JS_GetUCPropertyAttributes(cx, obj, nameChars, nameLen, &attrs, &found);
if (!found)
{
if (!JS_GetPendingException(cx, &pd.value))
return NULL;
}
JS_ClearPendingException(cx);
if(!JS_GetUCProperty(cx, obj, nameChars, nameLen, val.address()))
{
if (JS_IsExceptionPending(cx))
{
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
return NULL;
if (!JS_GetPendingException(cx, &pd.value))
{
return NULL;
}
pd.flags = JSPD_EXCEPTION;
}
else
{
pd.flags = JSPD_ERROR;
pd.value = JSVAL_VOID;
}
pd.flags = JSPD_EXCEPTION;
}
else
{
pd.flags = JSPD_ERROR;
pd.value = JSVAL_VOID;
pd.value = val;
}
}
else
{
pd.value = val;
}
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
nameval = STRING_TO_JSVAL(name);
if (!JS_ValueToId(cx, nameval, nameid.address()) ||
@ -605,14 +559,12 @@ jsd_GetValueFunction(JSDContext* jsdc, JSDValue* jsdval)
JS::RootedObject obj(cx);
JS::RootedFunction fun(cx);
JSCompartment* oldCompartment = NULL;
if (JSVAL_IS_PRIMITIVE(jsdval->val))
return NULL;
obj = js::UncheckedUnwrap(JSVAL_TO_OBJECT(jsdval->val));
oldCompartment = JS_EnterCompartment(cx, obj);
JSAutoCompartment ac(cx, obj);
fun = JS_ValueToFunction(cx, OBJECT_TO_JSVAL(obj));
JS_LeaveCompartment(cx, oldCompartment);
return fun;
}
@ -644,11 +596,10 @@ jsd_GetValuePrototype(JSDContext* jsdc, JSDValue* jsdval)
JSDValue*
jsd_GetValueParent(JSDContext* jsdc, JSDValue* jsdval)
{
JSCompartment* oldCompartment = NULL;
if(!(CHECK_BIT_FLAG(jsdval->flags, GOT_PARENT)))
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JS::RootedObject obj(cx);
JS::RootedObject parent(cx);
@ -657,11 +608,10 @@ jsd_GetValueParent(JSDContext* jsdc, JSDValue* jsdval)
if(JSVAL_IS_PRIMITIVE(jsdval->val))
return NULL;
obj = JSVAL_TO_OBJECT(jsdval->val);
JS_BeginRequest(jsdc->dumbContext);
oldCompartment = JS_EnterCompartment(jsdc->dumbContext, obj);
parent = JS_GetParentOrScopeChain(jsdc->dumbContext,obj);
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
JS_EndRequest(jsdc->dumbContext);
{
JSAutoCompartment ac(cx, obj);
parent = JS_GetParentOrScopeChain(jsdc->dumbContext,obj);
}
if(!parent)
return NULL;
jsdval->parent = jsd_NewValue(jsdc, OBJECT_TO_JSVAL(parent));
@ -674,8 +624,8 @@ jsd_GetValueParent(JSDContext* jsdc, JSDValue* jsdval)
JSDValue*
jsd_GetValueConstructor(JSDContext* jsdc, JSDValue* jsdval)
{
JSCompartment* oldCompartment = NULL;
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
if(!(CHECK_BIT_FLAG(jsdval->flags, GOT_CTOR)))
{
@ -691,11 +641,10 @@ jsd_GetValueConstructor(JSDContext* jsdc, JSDValue* jsdval)
return NULL;
if(!proto)
return NULL;
JS_BeginRequest(jsdc->dumbContext);
oldCompartment = JS_EnterCompartment(jsdc->dumbContext, obj);
ctor = JS_GetConstructor(jsdc->dumbContext,proto);
JS_LeaveCompartment(jsdc->dumbContext, oldCompartment);
JS_EndRequest(jsdc->dumbContext);
{
JSAutoCompartment ac(cx, obj);
ctor = JS_GetConstructor(jsdc->dumbContext,proto);
}
if(!ctor)
return NULL;
jsdval->ctor = jsd_NewValue(jsdc, OBJECT_TO_JSVAL(ctor));
@ -709,18 +658,14 @@ const char*
jsd_GetValueClassName(JSDContext* jsdc, JSDValue* jsdval)
{
jsval val = jsdval->val;
JSCompartment* oldCompartment = NULL;
if(!jsdval->className && !JSVAL_IS_PRIMITIVE(val))
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JS::RootedObject obj(cx, JSVAL_TO_OBJECT(val));
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(cx, obj);
JSAutoCompartment ac(cx, obj);
jsdval->className = JS_GetDebugClassName(obj);
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
}
return jsdval->className;
}
@ -729,25 +674,22 @@ JSDScript*
jsd_GetScriptForValue(JSDContext* jsdc, JSDValue* jsdval)
{
JSContext* cx = jsdc->dumbContext;
JSAutoRequest ar(cx);
JS::RootedValue val(cx, jsdval->val);
JSFunction* fun = NULL;
JSExceptionState* exceptionState;
JS::RootedScript script(cx);
JSDScript* jsdscript;
JSCompartment* oldCompartment = NULL;
if (!jsd_IsValueFunction(jsdc, jsdval))
return NULL;
JS_BeginRequest(cx);
oldCompartment = JS_EnterCompartment(cx, JSVAL_TO_OBJECT(val));
exceptionState = JS_SaveExceptionState(cx);
fun = JSD_GetValueFunction(jsdc, jsdval);
JS_RestoreExceptionState(cx, exceptionState);
if (fun)
script = JS_GetFunctionScript(cx, fun);
JS_LeaveCompartment(cx, oldCompartment);
JS_EndRequest(cx);
{
JSAutoCompartment ac(cx, JSVAL_TO_OBJECT(val));
AutoSaveExceptionState as(cx);
fun = JSD_GetValueFunction(jsdc, jsdval);
if (fun)
script = JS_GetFunctionScript(cx, fun);
}
if (!script)
return NULL;