Fix regexp match pair end-index == -1 assumption. (r=dmandelin, a=blocker b=605754)
[mozilla-central.git] / js / jsd / jsd_hook.c
blob45c6be8e59794e5758adb8a3358471b2bcc19c04
1 /* -*- Mode: C; tab-width: 8; 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 - Hook support
42 #include "jsd.h"
44 JSTrapStatus
45 jsd_InterruptHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval,
46 void *closure)
48 JSDScript* jsdscript;
49 JSDContext* jsdc = (JSDContext*) closure;
50 JSD_ExecutionHookProc hook;
51 void* hookData;
53 if( ! jsdc || ! jsdc->inited )
54 return JSTRAP_CONTINUE;
56 if( JSD_IS_DANGEROUS_THREAD(jsdc) )
57 return JSTRAP_CONTINUE;
59 /* local in case jsdc->interruptHook gets cleared on another thread */
60 JSD_LOCK();
61 hook = jsdc->interruptHook;
62 hookData = jsdc->interruptHookData;
63 JSD_UNLOCK();
65 if (!hook)
66 return JSTRAP_CONTINUE;
68 JSD_LOCK_SCRIPTS(jsdc);
69 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, NULL);
70 JSD_UNLOCK_SCRIPTS(jsdc);
71 if( ! jsdscript )
72 return JSTRAP_CONTINUE;
74 #ifdef LIVEWIRE
75 if( ! jsdlw_UserCodeAtPC(jsdc, jsdscript, (jsuword)pc) )
76 return JSTRAP_CONTINUE;
77 #endif
79 return jsd_CallExecutionHook(jsdc, cx, JSD_HOOK_INTERRUPTED,
80 hook, hookData, rval);
83 JSTrapStatus
84 jsd_DebuggerHandler(JSContext *cx, JSScript *script, jsbytecode *pc,
85 jsval *rval, void *closure)
87 JSDScript* jsdscript;
88 JSDContext* jsdc = (JSDContext*) closure;
89 JSD_ExecutionHookProc hook;
90 void* hookData;
92 if( ! jsdc || ! jsdc->inited )
93 return JSTRAP_CONTINUE;
95 if( JSD_IS_DANGEROUS_THREAD(jsdc) )
96 return JSTRAP_CONTINUE;
98 /* local in case jsdc->debuggerHook gets cleared on another thread */
99 JSD_LOCK();
100 hook = jsdc->debuggerHook;
101 hookData = jsdc->debuggerHookData;
102 JSD_UNLOCK();
103 if(!hook)
104 return JSTRAP_CONTINUE;
106 JSD_LOCK_SCRIPTS(jsdc);
107 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, NULL);
108 JSD_UNLOCK_SCRIPTS(jsdc);
109 if( ! jsdscript )
110 return JSTRAP_CONTINUE;
112 return jsd_CallExecutionHook(jsdc, cx, JSD_HOOK_DEBUGGER_KEYWORD,
113 hook, hookData, rval);
117 JSTrapStatus
118 jsd_ThrowHandler(JSContext *cx, JSScript *script, jsbytecode *pc,
119 jsval *rval, void *closure)
121 JSDScript* jsdscript;
122 JSDContext* jsdc = (JSDContext*) closure;
123 JSD_ExecutionHookProc hook;
124 void* hookData;
126 if( ! jsdc || ! jsdc->inited )
127 return JSD_HOOK_RETURN_CONTINUE_THROW;
129 if( JSD_IS_DANGEROUS_THREAD(jsdc) )
130 return JSD_HOOK_RETURN_CONTINUE_THROW;
132 /* local in case jsdc->throwHook gets cleared on another thread */
133 JSD_LOCK();
134 hook = jsdc->throwHook;
135 hookData = jsdc->throwHookData;
136 JSD_UNLOCK();
137 if (!hook)
138 return JSD_HOOK_RETURN_CONTINUE_THROW;
140 JSD_LOCK_SCRIPTS(jsdc);
141 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, NULL);
142 JSD_UNLOCK_SCRIPTS(jsdc);
143 if( ! jsdscript )
144 return JSD_HOOK_RETURN_CONTINUE_THROW;
146 JS_GetPendingException(cx, rval);
148 return jsd_CallExecutionHook(jsdc, cx, JSD_HOOK_THROW,
149 hook, hookData, rval);
152 JSTrapStatus
153 jsd_CallExecutionHook(JSDContext* jsdc,
154 JSContext *cx,
155 uintN type,
156 JSD_ExecutionHookProc hook,
157 void* hookData,
158 jsval* rval)
160 uintN hookanswer = JSD_HOOK_THROW == type ?
161 JSD_HOOK_RETURN_CONTINUE_THROW :
162 JSD_HOOK_RETURN_CONTINUE;
163 JSDThreadState* jsdthreadstate;
165 if(hook && NULL != (jsdthreadstate = jsd_NewThreadState(jsdc,cx)))
167 if ((type != JSD_HOOK_THROW && type != JSD_HOOK_INTERRUPTED) ||
168 jsdc->flags & JSD_MASK_TOP_FRAME_ONLY ||
169 !(jsdthreadstate->flags & TS_HAS_DISABLED_FRAME))
172 * if it's not a throw and it's not an interrupt,
173 * or we're only masking the top frame,
174 * or there are no disabled frames in this stack,
175 * then call out.
177 hookanswer = hook(jsdc, jsdthreadstate, type, hookData, rval);
178 jsd_DestroyThreadState(jsdc, jsdthreadstate);
182 switch(hookanswer)
184 case JSD_HOOK_RETURN_ABORT:
185 case JSD_HOOK_RETURN_HOOK_ERROR:
186 return JSTRAP_ERROR;
187 case JSD_HOOK_RETURN_RET_WITH_VAL:
188 return JSTRAP_RETURN;
189 case JSD_HOOK_RETURN_THROW_WITH_VAL:
190 return JSTRAP_THROW;
191 case JSD_HOOK_RETURN_CONTINUE:
192 break;
193 case JSD_HOOK_RETURN_CONTINUE_THROW:
194 /* only makes sense for jsd_ThrowHandler (which init'd rval) */
195 JS_ASSERT(JSD_HOOK_THROW == type);
196 return JSTRAP_THROW;
197 default:
198 JS_ASSERT(0);
199 break;
201 return JSTRAP_CONTINUE;
204 JSBool
205 jsd_CallCallHook (JSDContext* jsdc,
206 JSContext *cx,
207 uintN type,
208 JSD_CallHookProc hook,
209 void* hookData)
211 JSBool hookanswer;
212 JSDThreadState* jsdthreadstate;
214 hookanswer = JS_FALSE;
215 if(hook && NULL != (jsdthreadstate = jsd_NewThreadState(jsdc, cx)))
217 hookanswer = hook(jsdc, jsdthreadstate, type, hookData);
218 jsd_DestroyThreadState(jsdc, jsdthreadstate);
221 return hookanswer;
224 JSBool
225 jsd_SetInterruptHook(JSDContext* jsdc,
226 JSD_ExecutionHookProc hook,
227 void* callerdata)
229 JSD_LOCK();
230 jsdc->interruptHookData = callerdata;
231 jsdc->interruptHook = hook;
232 JS_SetInterrupt(jsdc->jsrt, jsd_InterruptHandler, (void*) jsdc);
233 JSD_UNLOCK();
235 return JS_TRUE;
238 JSBool
239 jsd_ClearInterruptHook(JSDContext* jsdc)
241 JSD_LOCK();
242 JS_ClearInterrupt(jsdc->jsrt, NULL, NULL );
243 jsdc->interruptHook = NULL;
244 JSD_UNLOCK();
246 return JS_TRUE;
249 JSBool
250 jsd_SetDebugBreakHook(JSDContext* jsdc,
251 JSD_ExecutionHookProc hook,
252 void* callerdata)
254 JSD_LOCK();
255 jsdc->debugBreakHookData = callerdata;
256 jsdc->debugBreakHook = hook;
257 JSD_UNLOCK();
259 return JS_TRUE;
262 JSBool
263 jsd_ClearDebugBreakHook(JSDContext* jsdc)
265 JSD_LOCK();
266 jsdc->debugBreakHook = NULL;
267 JSD_UNLOCK();
269 return JS_TRUE;
272 JSBool
273 jsd_SetDebuggerHook(JSDContext* jsdc,
274 JSD_ExecutionHookProc hook,
275 void* callerdata)
277 JSD_LOCK();
278 jsdc->debuggerHookData = callerdata;
279 jsdc->debuggerHook = hook;
280 JSD_UNLOCK();
282 return JS_TRUE;
285 JSBool
286 jsd_ClearDebuggerHook(JSDContext* jsdc)
288 JSD_LOCK();
289 jsdc->debuggerHook = NULL;
290 JSD_UNLOCK();
292 return JS_TRUE;
295 JSBool
296 jsd_SetThrowHook(JSDContext* jsdc,
297 JSD_ExecutionHookProc hook,
298 void* callerdata)
300 JSD_LOCK();
301 jsdc->throwHookData = callerdata;
302 jsdc->throwHook = hook;
303 JSD_UNLOCK();
305 return JS_TRUE;
308 JSBool
309 jsd_ClearThrowHook(JSDContext* jsdc)
311 JSD_LOCK();
312 jsdc->throwHook = NULL;
313 JSD_UNLOCK();
315 return JS_TRUE;
318 JSBool
319 jsd_SetFunctionHook(JSDContext* jsdc,
320 JSD_CallHookProc hook,
321 void* callerdata)
323 JSD_LOCK();
324 jsdc->functionHookData = callerdata;
325 jsdc->functionHook = hook;
326 JSD_UNLOCK();
328 return JS_TRUE;
331 JSBool
332 jsd_ClearFunctionHook(JSDContext* jsdc)
334 JSD_LOCK();
335 jsdc->functionHook = NULL;
336 JSD_UNLOCK();
338 return JS_TRUE;
341 JSBool
342 jsd_SetTopLevelHook(JSDContext* jsdc,
343 JSD_CallHookProc hook,
344 void* callerdata)
346 JSD_LOCK();
347 jsdc->toplevelHookData = callerdata;
348 jsdc->toplevelHook = hook;
349 JSD_UNLOCK();
351 return JS_TRUE;
354 JSBool
355 jsd_ClearTopLevelHook(JSDContext* jsdc)
357 JSD_LOCK();
358 jsdc->toplevelHook = NULL;
359 JSD_UNLOCK();
361 return JS_TRUE;