2013-04-17 00:47:10 +04: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 15:12:37 +04: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/. */
|
1998-11-05 11:57:24 +03: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)
|
|
|
|
{
|
|
|
|
JS_ASSERT(jsdobj);
|
|
|
|
JS_ASSERT(!JS_CLIST_IS_EMPTY(&jsdobj->links));
|
|
|
|
JS_ASSERT(jsdobj->obj);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
_destroyJSDObject(JSDContext* jsdc, JSDObject* jsdobj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc));
|
|
|
|
|
|
|
|
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 12:49:29 +04:00
|
|
|
JSAbstractFramePtr frame)
|
1998-11-05 11:57:24 +03:00
|
|
|
{
|
|
|
|
JSDObject* jsdobj;
|
2013-05-12 17:13:22 +04:00
|
|
|
JS::RootedScript script(cx);
|
1998-11-05 11:57:24 +03:00
|
|
|
JSDScript* jsdscript;
|
|
|
|
const char* ctorURL;
|
2011-02-01 23:44:41 +03:00
|
|
|
JSString* ctorNameStr;
|
1998-11-05 11:57:24 +03:00
|
|
|
const char* ctorName;
|
|
|
|
|
|
|
|
JSD_LOCK_OBJECTS(jsdc);
|
|
|
|
jsdobj = jsd_GetJSDObjectForJSObject(jsdc, obj);
|
2012-08-18 05:05:06 +04:00
|
|
|
if( jsdobj && !jsdobj->ctorURL )
|
1998-11-05 11:57:24 +03:00
|
|
|
{
|
2013-01-25 12:49:29 +04:00
|
|
|
script = frame.script();
|
1998-11-05 11:57:24 +03:00
|
|
|
if( script )
|
|
|
|
{
|
|
|
|
ctorURL = JS_GetScriptFilename(cx, script);
|
|
|
|
if( ctorURL )
|
|
|
|
jsdobj->ctorURL = jsd_AddAtom(jsdc, ctorURL);
|
|
|
|
|
|
|
|
JSD_LOCK_SCRIPTS(jsdc);
|
2013-01-25 12:49:29 +04:00
|
|
|
jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, frame);
|
1998-11-05 11:57:24 +03:00
|
|
|
JSD_UNLOCK_SCRIPTS(jsdc);
|
2011-02-01 23:44:41 +03:00
|
|
|
if( jsdscript && (ctorNameStr = jsd_GetScriptFunctionId(jsdc, jsdscript)) ) {
|
|
|
|
if( (ctorName = JS_EncodeString(cx, ctorNameStr)) ) {
|
1998-11-05 11:57:24 +03:00
|
|
|
jsdobj->ctorName = jsd_AddAtom(jsdc, ctorName);
|
2011-02-01 23:44:41 +03:00
|
|
|
JS_free(cx, (void *) ctorName);
|
|
|
|
}
|
1998-11-05 11:57:24 +03:00
|
|
|
}
|
|
|
|
jsdobj->ctorLineno = JS_GetScriptBaseLineNumber(cx, script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACEOBJ(jsdc, jsdobj, 3);
|
|
|
|
JSD_UNLOCK_OBJECTS(jsdc);
|
|
|
|
}
|
|
|
|
|
2008-09-07 02:21:43 +04:00
|
|
|
static JSHashNumber
|
1998-11-05 11:57:24 +03:00
|
|
|
_hash_root(const void *key)
|
|
|
|
{
|
2010-12-04 13:05:45 +03:00
|
|
|
return ((JSHashNumber)(ptrdiff_t) key) >> 2; /* help lame MSVC1.5 on Win16 */
|
1998-11-05 11:57:24 +03:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
1998-11-05 11:57:24 +03:00
|
|
|
jsd_InitObjectManager(JSDContext* jsdc)
|
|
|
|
{
|
|
|
|
JS_INIT_CLIST(&jsdc->objectsList);
|
|
|
|
jsdc->objectsTable = JS_NewHashTable(256, _hash_root,
|
|
|
|
JS_CompareValues, JS_CompareValues,
|
2013-09-19 23:26:36 +04:00
|
|
|
nullptr, nullptr);
|
2010-12-04 13:05:45 +03:00
|
|
|
return !!jsdc->objectsTable;
|
1998-11-05 11:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_DestroyObjectManager(JSDContext* jsdc)
|
2009-12-15 08:31:59 +03:00
|
|
|
{
|
|
|
|
jsd_DestroyObjects(jsdc);
|
|
|
|
JSD_LOCK_OBJECTS(jsdc);
|
|
|
|
JS_HashTableDestroy(jsdc->objectsTable);
|
|
|
|
JSD_UNLOCK_OBJECTS(jsdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsd_DestroyObjects(JSDContext* jsdc)
|
1998-11-05 11:57:24 +03: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;
|
|
|
|
|
|
|
|
JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc));
|
|
|
|
|
|
|
|
if( !jsdobj )
|
|
|
|
jsdobj = (JSDObject *)jsdc->objectsList.next;
|
|
|
|
if( jsdobj == (JSDObject *)&jsdc->objectsList )
|
2013-09-19 23:26:36 +04:00
|
|
|
return nullptr;
|
1998-11-05 11:57:24 +03: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 23:26:36 +04:00
|
|
|
return nullptr;
|
1998-11-05 11:57:24 +03:00
|
|
|
}
|
|
|
|
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned
|
1998-11-05 11:57:24 +03: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 23:26:36 +04:00
|
|
|
return nullptr;
|
1998-11-05 11:57:24 +03:00
|
|
|
}
|
|
|
|
|
2012-02-29 03:11:11 +04:00
|
|
|
unsigned
|
1998-11-05 11:57:24 +03: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 23:26:36 +04:00
|
|
|
return nullptr;
|
1998-11-05 11:57:24 +03: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|