Fix regexp match pair end-index == -1 assumption. (r=dmandelin, a=blocker b=605754)
[mozilla-central.git] / js / jsd / jsd_obj.c
blobc927447dff37b017b73886b43fbe17a94c3ad139
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
13 * License.
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.
22 * Contributor(s):
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
42 #include "jsd.h"
45 * #define JSD_TRACE 1
48 #ifdef JSD_TRACE
49 #define TRACEOBJ(jsdc, jsdobj, which) _traceObj(jsdc, jsdobj, which)
51 static char *
52 _describeObj(JSDContext* jsdc, JSDObject *jsdobj)
54 return
55 JS_smprintf("%0x new'd in %s at line %d using ctor %s in %s at line %d",
56 (int)jsdobj,
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));
64 static void
65 _traceObj(JSDContext* jsdc, JSDObject* jsdobj, int which)
67 char* description;
69 if( !jsdobj )
70 return;
72 description = _describeObj(jsdc, jsdobj);
74 printf("%s : %s\n",
75 which == 0 ? "new " :
76 which == 1 ? "final" :
77 "ctor ",
78 description);
79 if(description)
80 free(description);
82 #else
83 #define TRACEOBJ(jsdc, jsdobj, which) ((void)0)
84 #endif /* JSD_TRACE */
86 #ifdef DEBUG
87 void JSD_ASSERT_VALID_OBJECT(JSDObject* jsdobj)
89 JS_ASSERT(jsdobj);
90 JS_ASSERT(!JS_CLIST_IS_EMPTY(&jsdobj->links));
91 JS_ASSERT(jsdobj->obj);
93 #endif
96 static void
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);
104 if(jsdobj->newURL)
105 jsd_DropAtom(jsdc, jsdobj->newURL);
106 if(jsdobj->ctorURL)
107 jsd_DropAtom(jsdc, jsdobj->ctorURL);
108 if(jsdobj->ctorName)
109 jsd_DropAtom(jsdc, jsdobj->ctorName);
110 free(jsdobj);
113 static JSDObject*
114 _createJSDObject(JSDContext* jsdc, JSContext *cx, JSObject *obj)
116 JSDObject* jsdobj;
117 JSStackFrame* fp;
118 JSStackFrame* iter = NULL;
119 const char* newURL;
120 jsbytecode* pc;
122 JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc));
124 jsdobj = (JSDObject*) calloc(1, sizeof(JSDObject));
125 if (jsdobj)
127 JS_INIT_CLIST(&jsdobj->links);
128 JS_APPEND_LINK(&jsdobj->links, &jsdc->objectsList);
129 jsdobj->obj = obj;
130 JS_HashTableAdd(jsdc->objectsTable, obj, jsdobj);
132 return jsdobj;
135 void
136 jsd_Constructing(JSDContext* jsdc, JSContext *cx, JSObject *obj,
137 JSStackFrame *fp)
139 JSDObject* jsdobj;
140 JSScript* script;
141 JSDScript* jsdscript;
142 const char* ctorURL;
143 const char* ctorName;
145 JSD_LOCK_OBJECTS(jsdc);
146 jsdobj = jsd_GetJSDObjectForJSObject(jsdc, obj);
147 if( jsdobj && !jsdobj->ctorURL && JS_IsScriptFrame(cx, fp) )
149 script = JS_GetFrameScript(cx, fp);
150 if( script )
152 ctorURL = JS_GetScriptFilename(cx, script);
153 if( ctorURL )
154 jsdobj->ctorURL = jsd_AddAtom(jsdc, ctorURL);
156 JSD_LOCK_SCRIPTS(jsdc);
157 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, fp);
158 JSD_UNLOCK_SCRIPTS(jsdc);
159 if( jsdscript )
161 ctorName = jsd_GetScriptFunctionName(jsdc, jsdscript);
162 if( ctorName )
163 jsdobj->ctorName = jsd_AddAtom(jsdc, ctorName);
165 jsdobj->ctorLineno = JS_GetScriptBaseLineNumber(cx, script);
168 TRACEOBJ(jsdc, jsdobj, 3);
169 JSD_UNLOCK_OBJECTS(jsdc);
172 static JSHashNumber
173 _hash_root(const void *key)
175 return ((JSHashNumber) key) >> 2; /* help lame MSVC1.5 on Win16 */
178 JSBool
179 jsd_InitObjectManager(JSDContext* jsdc)
181 JS_INIT_CLIST(&jsdc->objectsList);
182 jsdc->objectsTable = JS_NewHashTable(256, _hash_root,
183 JS_CompareValues, JS_CompareValues,
184 NULL, NULL);
185 return (JSBool) jsdc->objectsTable;
188 void
189 jsd_DestroyObjectManager(JSDContext* jsdc)
191 jsd_DestroyObjects(jsdc);
192 JSD_LOCK_OBJECTS(jsdc);
193 JS_HashTableDestroy(jsdc->objectsTable);
194 JSD_UNLOCK_OBJECTS(jsdc);
197 void
198 jsd_DestroyObjects(JSDContext* jsdc)
200 JSD_LOCK_OBJECTS(jsdc);
201 while( !JS_CLIST_IS_EMPTY(&jsdc->objectsList) )
202 _destroyJSDObject(jsdc, (JSDObject*)JS_NEXT_LINK(&jsdc->objectsList));
203 JSD_UNLOCK_OBJECTS(jsdc);
206 JSDObject*
207 jsd_IterateObjects(JSDContext* jsdc, JSDObject** iterp)
209 JSDObject *jsdobj = *iterp;
211 JS_ASSERT(JSD_OBJECTS_LOCKED(jsdc));
213 if( !jsdobj )
214 jsdobj = (JSDObject *)jsdc->objectsList.next;
215 if( jsdobj == (JSDObject *)&jsdc->objectsList )
216 return NULL;
217 *iterp = (JSDObject*) jsdobj->links.next;
218 return jsdobj;
221 JSObject*
222 jsd_GetWrappedObject(JSDContext* jsdc, JSDObject* jsdobj)
224 return jsdobj->obj;
227 const char*
228 jsd_GetObjectNewURL(JSDContext* jsdc, JSDObject* jsdobj)
230 if( jsdobj->newURL )
231 return JSD_ATOM_TO_STRING(jsdobj->newURL);
232 return NULL;
235 uintN
236 jsd_GetObjectNewLineNumber(JSDContext* jsdc, JSDObject* jsdobj)
238 return jsdobj->newLineno;
241 const char*
242 jsd_GetObjectConstructorURL(JSDContext* jsdc, JSDObject* jsdobj)
244 if( jsdobj->ctorURL )
245 return JSD_ATOM_TO_STRING(jsdobj->ctorURL);
246 return NULL;
249 uintN
250 jsd_GetObjectConstructorLineNumber(JSDContext* jsdc, JSDObject* jsdobj)
252 return jsdobj->ctorLineno;
255 const char*
256 jsd_GetObjectConstructorName(JSDContext* jsdc, JSDObject* jsdobj)
258 if( jsdobj->ctorName )
259 return JSD_ATOM_TO_STRING(jsdobj->ctorName);
260 return NULL;
263 JSDObject*
264 jsd_GetJSDObjectForJSObject(JSDContext* jsdc, JSObject* jsobj)
266 JSDObject* jsdobj;
268 JSD_LOCK_OBJECTS(jsdc);
269 jsdobj = (JSDObject*) JS_HashTableLookup(jsdc->objectsTable, jsobj);
270 JSD_UNLOCK_OBJECTS(jsdc);
271 return jsdobj;
274 JSDObject*
275 jsd_GetObjectForValue(JSDContext* jsdc, JSDValue* jsdval)
277 return jsd_GetJSDObjectForJSObject(jsdc, JSVAL_TO_OBJECT(jsdval->val));
280 JSDValue*
281 jsd_GetValueForObject(JSDContext* jsdc, JSDObject* jsdobj)
283 return jsd_NewValue(jsdc, OBJECT_TO_JSVAL(jsdobj->obj));