1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * JavaScript Debugging support - Object support
49 #define TRACEOBJ(jsdc, jsdobj, which) _traceObj(jsdc, jsdobj, which)
52 _describeObj(JSDContext
* jsdc
, JSDObject
*jsdobj
)
55 JS_smprintf("%0x new'd in %s at line %d using ctor %s in %s at line %d",
57 JSD_GetObjectNewURL(jsdc
, jsdobj
),
58 JSD_GetObjectNewLineNumber(jsdc
, jsdobj
),
59 JSD_GetObjectConstructorName(jsdc
, jsdobj
),
60 JSD_GetObjectConstructorURL(jsdc
, jsdobj
),
61 JSD_GetObjectConstructorLineNumber(jsdc
, jsdobj
));
65 _traceObj(JSDContext
* jsdc
, JSDObject
* jsdobj
, int which
)
72 description
= _describeObj(jsdc
, jsdobj
);
76 which
== 1 ? "final" :
83 #define TRACEOBJ(jsdc, jsdobj, which) ((void)0)
84 #endif /* JSD_TRACE */
87 void JSD_ASSERT_VALID_OBJECT(JSDObject
* jsdobj
)
90 JS_ASSERT(!JS_CLIST_IS_EMPTY(&jsdobj
->links
));
91 JS_ASSERT(jsdobj
->obj
);
97 _destroyJSDObject(JSDContext
* jsdc
, JSDObject
* jsdobj
)
99 JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc
));
101 JS_REMOVE_LINK(&jsdobj
->links
);
102 JS_HashTableRemove(jsdc
->objectsTable
, jsdobj
->obj
);
105 jsd_DropAtom(jsdc
, jsdobj
->newURL
);
107 jsd_DropAtom(jsdc
, jsdobj
->ctorURL
);
109 jsd_DropAtom(jsdc
, jsdobj
->ctorName
);
114 _createJSDObject(JSDContext
* jsdc
, JSContext
*cx
, JSObject
*obj
)
118 JSStackFrame
* iter
= NULL
;
122 JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc
));
124 jsdobj
= (JSDObject
*) calloc(1, sizeof(JSDObject
));
127 JS_INIT_CLIST(&jsdobj
->links
);
128 JS_APPEND_LINK(&jsdobj
->links
, &jsdc
->objectsList
);
130 JS_HashTableAdd(jsdc
->objectsTable
, obj
, jsdobj
);
132 if (jsdc
->flags
& JSD_DISABLE_OBJECT_TRACE
)
135 /* walk the stack to find js frame (if any) causing creation */
136 while (NULL
!= (fp
= JS_FrameIterator(cx
, &iter
)))
138 if( !JS_IsNativeFrame(cx
, fp
) )
140 JSScript
* script
= JS_GetFrameScript(cx
, fp
);
144 newURL
= JS_GetScriptFilename(cx
, script
);
146 jsdobj
->newURL
= jsd_AddAtom(jsdc
, newURL
);
148 pc
= JS_GetFramePC(cx
, fp
);
150 jsdobj
->newLineno
= JS_PCToLineNumber(cx
, script
, pc
);
160 jsd_ObjectHook(JSContext
*cx
, JSObject
*obj
, JSBool isNew
, void *closure
)
163 JSDContext
* jsdc
= (JSDContext
*) closure
;
165 if( ! jsdc
|| ! jsdc
->inited
)
168 JSD_LOCK_OBJECTS(jsdc
);
171 jsdobj
= _createJSDObject(jsdc
, cx
, obj
);
172 TRACEOBJ(jsdc
, jsdobj
, 0);
176 jsdobj
= jsd_GetJSDObjectForJSObject(jsdc
, obj
);
179 TRACEOBJ(jsdc
, jsdobj
, 1);
180 _destroyJSDObject(jsdc
, jsdobj
);
183 JSD_UNLOCK_OBJECTS(jsdc
);
187 jsd_Constructing(JSDContext
* jsdc
, JSContext
*cx
, JSObject
*obj
,
192 JSDScript
* jsdscript
;
194 const char* ctorName
;
196 JSD_LOCK_OBJECTS(jsdc
);
197 jsdobj
= jsd_GetJSDObjectForJSObject(jsdc
, obj
);
198 if( jsdobj
&& !jsdobj
->ctorURL
&& !JS_IsNativeFrame(cx
, fp
) )
200 script
= JS_GetFrameScript(cx
, fp
);
203 ctorURL
= JS_GetScriptFilename(cx
, script
);
205 jsdobj
->ctorURL
= jsd_AddAtom(jsdc
, ctorURL
);
207 JSD_LOCK_SCRIPTS(jsdc
);
208 jsdscript
= jsd_FindJSDScript(jsdc
, script
);
209 JSD_UNLOCK_SCRIPTS(jsdc
);
212 ctorName
= jsd_GetScriptFunctionName(jsdc
, jsdscript
);
214 jsdobj
->ctorName
= jsd_AddAtom(jsdc
, ctorName
);
216 jsdobj
->ctorLineno
= JS_GetScriptBaseLineNumber(cx
, script
);
219 TRACEOBJ(jsdc
, jsdobj
, 3);
220 JSD_UNLOCK_OBJECTS(jsdc
);
224 _hash_root(const void *key
)
226 return ((JSHashNumber
) key
) >> 2; /* help lame MSVC1.5 on Win16 */
230 jsd_InitObjectManager(JSDContext
* jsdc
)
232 JS_INIT_CLIST(&jsdc
->objectsList
);
233 jsdc
->objectsTable
= JS_NewHashTable(256, _hash_root
,
234 JS_CompareValues
, JS_CompareValues
,
236 return (JSBool
) jsdc
->objectsTable
;
240 jsd_DestroyObjectManager(JSDContext
* jsdc
)
242 JSD_LOCK_OBJECTS(jsdc
);
243 while( !JS_CLIST_IS_EMPTY(&jsdc
->objectsList
) )
244 _destroyJSDObject(jsdc
, (JSDObject
*)JS_NEXT_LINK(&jsdc
->objectsList
));
245 JS_HashTableDestroy(jsdc
->objectsTable
);
246 JSD_UNLOCK_OBJECTS(jsdc
);
250 jsd_IterateObjects(JSDContext
* jsdc
, JSDObject
** iterp
)
252 JSDObject
*jsdobj
= *iterp
;
254 JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc
));
257 jsdobj
= (JSDObject
*)jsdc
->objectsList
.next
;
258 if( jsdobj
== (JSDObject
*)&jsdc
->objectsList
)
260 *iterp
= (JSDObject
*) jsdobj
->links
.next
;
265 jsd_GetWrappedObject(JSDContext
* jsdc
, JSDObject
* jsdobj
)
271 jsd_GetObjectNewURL(JSDContext
* jsdc
, JSDObject
* jsdobj
)
274 return JSD_ATOM_TO_STRING(jsdobj
->newURL
);
279 jsd_GetObjectNewLineNumber(JSDContext
* jsdc
, JSDObject
* jsdobj
)
281 return jsdobj
->newLineno
;
285 jsd_GetObjectConstructorURL(JSDContext
* jsdc
, JSDObject
* jsdobj
)
287 if( jsdobj
->ctorURL
)
288 return JSD_ATOM_TO_STRING(jsdobj
->ctorURL
);
293 jsd_GetObjectConstructorLineNumber(JSDContext
* jsdc
, JSDObject
* jsdobj
)
295 return jsdobj
->ctorLineno
;
299 jsd_GetObjectConstructorName(JSDContext
* jsdc
, JSDObject
* jsdobj
)
301 if( jsdobj
->ctorName
)
302 return JSD_ATOM_TO_STRING(jsdobj
->ctorName
);
307 jsd_GetJSDObjectForJSObject(JSDContext
* jsdc
, JSObject
* jsobj
)
311 JSD_LOCK_OBJECTS(jsdc
);
312 jsdobj
= (JSDObject
*) JS_HashTableLookup(jsdc
->objectsTable
, jsobj
);
313 JSD_UNLOCK_OBJECTS(jsdc
);
318 jsd_GetObjectForValue(JSDContext
* jsdc
, JSDValue
* jsdval
)
320 return jsd_GetJSDObjectForJSObject(jsdc
, JSVAL_TO_OBJECT(jsdval
->val
));
324 jsd_GetValueForObject(JSDContext
* jsdc
, JSDObject
* jsdobj
)
326 return jsd_NewValue(jsdc
, OBJECT_TO_JSVAL(jsdobj
->obj
));