2013-04-16 13:47:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 04:12:37 -07:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JavaScript Debugging support - Object support
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "jsd.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* #define JSD_TRACE 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef JSD_TRACE
|
|
|
|
#define TRACEOBJ(jsdc, jsdobj, which) _traceObj(jsdc, jsdobj, which)
|
|
|
|
|
|
|
|
static char *
|
|
|
|
_describeObj(JSDContext* jsdc, JSDObject *jsdobj)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
JS_smprintf("%0x new'd in %s at line %d using ctor %s in %s at line %d",
|
|
|
|
(int)jsdobj,
|
|
|
|
JSD_GetObjectNewURL(jsdc, jsdobj),
|
|
|
|
JSD_GetObjectNewLineNumber(jsdc, jsdobj),
|
|
|
|
JSD_GetObjectConstructorName(jsdc, jsdobj),
|
|
|
|
JSD_GetObjectConstructorURL(jsdc, jsdobj),
|
|
|
|
JSD_GetObjectConstructorLineNumber(jsdc, jsdobj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_traceObj(JSDContext* jsdc, JSDObject* jsdobj, int which)
|
|
|
|
{
|
|
|
|
char* description;
|
|
|
|
|
|
|
|
if( !jsdobj )
|
|
|
|
return;
|
|
|
|
|
|
|
|
description = _describeObj(jsdc, jsdobj);
|
|
|
|
|
|
|
|
printf("%s : %s\n",
|
|
|
|
which == 0 ? "new " :
|
|
|
|
which == 1 ? "final" :
|
|
|
|
"ctor ",
|
|
|
|
description);
|
|
|
|
if(description)
|
|
|
|
free(description);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define TRACEOBJ(jsdc, jsdobj, which) ((void)0)
|
|
|
|
#endif /* JSD_TRACE */
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void JSD_ASSERT_VALID_OBJECT(JSDObject* jsdobj)
|
|
|
|
{
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(jsdobj);
|
|
|
|
MOZ_ASSERT(!JS_CLIST_IS_EMPTY(&jsdobj->links));
|
|
|
|
MOZ_ASSERT(jsdobj->obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
_destroyJSDObject(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(JSD_OBJECTS_LOCKED(jsdc));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JS_REMOVE_LINK(&jsdobj->links);
|
|
|
|
JS_HashTableRemove(jsdc->objectsTable, jsdobj->obj);
|
|
|
|
|
|
|
|
if(jsdobj->newURL)
|
|
|
|
jsd_DropAtom(jsdc, jsdobj->newURL);
|
|
|
|
if(jsdobj->ctorURL)
|
|
|
|
jsd_DropAtom(jsdc, jsdobj->ctorURL);
|
|
|
|
if(jsdobj->ctorName)
|
|
|
|
jsd_DropAtom(jsdc, jsdobj->ctorName);
|
|
|
|
free(jsdobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_Constructing(JSDContext* jsdc, JSContext *cx, JSObject *obj,
|
2013-01-25 00:49:29 -08:00
|
|
|
JSAbstractFramePtr frame)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSDObject* jsdobj;
|
2013-05-12 06:13:22 -07:00
|
|
|
JS::RootedScript script(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
JSDScript* jsdscript;
|
|
|
|
const char* ctorURL;
|
2011-02-01 12:44:41 -08:00
|
|
|
JSString* ctorNameStr;
|
2007-03-22 10:30:00 -07:00
|
|
|
const char* ctorName;
|
|
|
|
|
|
|
|
JSD_LOCK_OBJECTS(jsdc);
|
|
|
|
jsdobj = jsd_GetJSDObjectForJSObject(jsdc, obj);
|
2012-08-17 18:05:06 -07:00
|
|
|
if( jsdobj && !jsdobj->ctorURL )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-25 00:49:29 -08:00
|
|
|
script = frame.script();
|
2007-03-22 10:30:00 -07:00
|
|
|
if( script )
|
|
|
|
{
|
2014-02-25 07:43:14 -08:00
|
|
|
ctorURL = JS_GetScriptFilename(script);
|
2007-03-22 10:30:00 -07:00
|
|
|
if( ctorURL )
|
|
|
|
jsdobj->ctorURL = jsd_AddAtom(jsdc, ctorURL);
|
|
|
|
|
|
|
|
JSD_LOCK_SCRIPTS(jsdc);
|
2013-01-25 00:49:29 -08:00
|
|
|
jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, frame);
|
2007-03-22 10:30:00 -07:00
|
|
|
JSD_UNLOCK_SCRIPTS(jsdc);
|
2011-02-01 12:44:41 -08:00
|
|
|
if( jsdscript && (ctorNameStr = jsd_GetScriptFunctionId(jsdc, jsdscript)) ) {
|
|
|
|
if( (ctorName = JS_EncodeString(cx, ctorNameStr)) ) {
|
2007-03-22 10:30:00 -07:00
|
|
|
jsdobj->ctorName = jsd_AddAtom(jsdc, ctorName);
|
2011-02-01 12:44:41 -08:00
|
|
|
JS_free(cx, (void *) ctorName);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
jsdobj->ctorLineno = JS_GetScriptBaseLineNumber(cx, script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACEOBJ(jsdc, jsdobj, 3);
|
|
|
|
JSD_UNLOCK_OBJECTS(jsdc);
|
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static JSHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
_hash_root(const void *key)
|
|
|
|
{
|
2010-12-04 02:05:45 -08:00
|
|
|
return ((JSHashNumber)(ptrdiff_t) key) >> 2; /* help lame MSVC1.5 on Win16 */
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
jsd_InitObjectManager(JSDContext* jsdc)
|
|
|
|
{
|
|
|
|
JS_INIT_CLIST(&jsdc->objectsList);
|
|
|
|
jsdc->objectsTable = JS_NewHashTable(256, _hash_root,
|
|
|
|
JS_CompareValues, JS_CompareValues,
|
2013-09-19 12:26:36 -07:00
|
|
|
nullptr, nullptr);
|
2010-12-04 02:05:45 -08:00
|
|
|
return !!jsdc->objectsTable;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_DestroyObjectManager(JSDContext* jsdc)
|
2009-12-14 21:31:59 -08:00
|
|
|
{
|
|
|
|
jsd_DestroyObjects(jsdc);
|
|
|
|
JSD_LOCK_OBJECTS(jsdc);
|
|
|
|
JS_HashTableDestroy(jsdc->objectsTable);
|
|
|
|
JSD_UNLOCK_OBJECTS(jsdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_DestroyObjects(JSDContext* jsdc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSD_LOCK_OBJECTS(jsdc);
|
|
|
|
while( !JS_CLIST_IS_EMPTY(&jsdc->objectsList) )
|
|
|
|
_destroyJSDObject(jsdc, (JSDObject*)JS_NEXT_LINK(&jsdc->objectsList));
|
|
|
|
JSD_UNLOCK_OBJECTS(jsdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSDObject*
|
|
|
|
jsd_IterateObjects(JSDContext* jsdc, JSDObject** iterp)
|
|
|
|
{
|
|
|
|
JSDObject *jsdobj = *iterp;
|
|
|
|
|
2014-02-17 22:24:15 -08:00
|
|
|
MOZ_ASSERT(JSD_OBJECTS_LOCKED(jsdc));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if( !jsdobj )
|
|
|
|
jsdobj = (JSDObject *)jsdc->objectsList.next;
|
|
|
|
if( jsdobj == (JSDObject *)&jsdc->objectsList )
|
2013-09-19 12:26:36 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
*iterp = (JSDObject*) jsdobj->links.next;
|
|
|
|
return jsdobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
jsd_GetWrappedObject(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
return jsdobj->obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
jsd_GetObjectNewURL(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
if( jsdobj->newURL )
|
|
|
|
return JSD_ATOM_TO_STRING(jsdobj->newURL);
|
2013-09-19 12:26:36 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned
|
2007-03-22 10:30:00 -07:00
|
|
|
jsd_GetObjectNewLineNumber(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
return jsdobj->newLineno;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
jsd_GetObjectConstructorURL(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
if( jsdobj->ctorURL )
|
|
|
|
return JSD_ATOM_TO_STRING(jsdobj->ctorURL);
|
2013-09-19 12:26:36 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned
|
2007-03-22 10:30:00 -07:00
|
|
|
jsd_GetObjectConstructorLineNumber(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
return jsdobj->ctorLineno;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
jsd_GetObjectConstructorName(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
if( jsdobj->ctorName )
|
|
|
|
return JSD_ATOM_TO_STRING(jsdobj->ctorName);
|
2013-09-19 12:26:36 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSDObject*
|
|
|
|
jsd_GetJSDObjectForJSObject(JSDContext* jsdc, JSObject* jsobj)
|
|
|
|
{
|
|
|
|
JSDObject* jsdobj;
|
|
|
|
|
|
|
|
JSD_LOCK_OBJECTS(jsdc);
|
|
|
|
jsdobj = (JSDObject*) JS_HashTableLookup(jsdc->objectsTable, jsobj);
|
|
|
|
JSD_UNLOCK_OBJECTS(jsdc);
|
|
|
|
return jsdobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSDObject*
|
|
|
|
jsd_GetObjectForValue(JSDContext* jsdc, JSDValue* jsdval)
|
|
|
|
{
|
|
|
|
return jsd_GetJSDObjectForJSObject(jsdc, JSVAL_TO_OBJECT(jsdval->val));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSDValue*
|
|
|
|
jsd_GetValueForObject(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
return jsd_NewValue(jsdc, OBJECT_TO_JSVAL(jsdobj->obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
|