1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 ***** */
38 /* this is all going away... replaced by code in js/jsd/java */
45 #include "_gen/netscape_jsdebug_DebugController.h"
46 #include "_gen/netscape_jsdebug_Script.h"
47 #include "_gen/netscape_jsdebug_JSThreadState.h"
48 #include "_gen/netscape_jsdebug_JSStackFrameInfo.h"
49 #include "_gen/netscape_jsdebug_JSPC.h"
50 #include "_gen/netscape_jsdebug_JSSourceTextProvider.h"
51 #include "_gen/netscape_jsdebug_JSErrorReporter.h"
53 static JSDContext* context = 0;
54 static struct Hnetscape_jsdebug_DebugController* controller = 0;
56 /***************************************************************************/
60 _constructInteger(ExecEnv *ee, long i)
63 execute_java_constructor(ee, "java/lang/Integer", 0, "(I)", i);
67 _putHash(ExecEnv *ee, JHandle* tbl, JHandle* key, JHandle* ob)
70 execute_java_dynamic_method(
72 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
77 _getHash(ExecEnv *ee, JHandle* tbl, JHandle* key)
80 execute_java_dynamic_method(
82 "(Ljava/lang/Object;)Ljava/lang/Object;",
87 _removeHash(ExecEnv *ee, JHandle* tbl, JHandle* key)
90 execute_java_dynamic_method(
92 "(Ljava/lang/Object;)Ljava/lang/Object;",
96 struct Hnetscape_jsdebug_JSStackFrameInfo*
97 _constructJSStackFrameInfo( ExecEnv* ee, JSDStackFrameInfo* jsdframe,
98 struct Hnetscape_jsdebug_JSThreadState* threadState )
100 struct Hnetscape_jsdebug_JSStackFrameInfo* frame;
102 frame = (struct Hnetscape_jsdebug_JSStackFrameInfo*)
103 execute_java_constructor( ee, "netscape/jsdebug/JSStackFrameInfo", 0,
104 "(Lnetscape/jsdebug/JSThreadState;)",
109 /* XXX fill in additional fields */
110 unhand(frame)->_nativePtr = (long) jsdframe;
115 struct Hnetscape_jsdebug_JSPC*
116 _constructJSPC( ExecEnv* ee, struct Hnetscape_jsdebug_Script* script, long pc )
118 struct Hnetscape_jsdebug_JSPC * pcOb;
120 pcOb = (struct Hnetscape_jsdebug_JSPC *)
121 execute_java_constructor( ee, "netscape/jsdebug/JSPC", 0,
122 "(Lnetscape/jsdebug/Script;I)",
127 /* XXX fill in additional fields */
132 static Hnetscape_jsdebug_Script*
133 _scriptObFromJSDScriptPtr( ExecEnv* ee, JSDScript* jsdscript )
135 JHandle* tbl = (JHandle*) unhand(controller)->scriptTable;
136 JHandle* key = _constructInteger(ee,(long)jsdscript);
137 return (Hnetscape_jsdebug_Script*) _getHash( ee, tbl, key );
140 /***************************************************************************/
143 _scriptHook( JSDContext* jsdc,
144 JSDScript* jsdscript,
148 Hnetscape_jsdebug_Script* script;
151 if( ! context || ! controller || ! ee )
156 char* url = (char*)JSD_GetScriptFilename (jsdc, jsdscript);
157 JSString* function = JSD_GetScriptFunctionId (jsdc, jsdscript);
158 int base = JSD_GetScriptBaseLineNumber (jsdc, jsdscript);
159 int extent = JSD_GetScriptLineExtent (jsdc, jsdscript);
167 /* create Java Object for Script */
168 script = (Hnetscape_jsdebug_Script*)
169 execute_java_constructor(ee, "netscape/jsdebug/Script", 0, "()");
174 /* set the members */
175 unhand(script)->_url = makeJavaString(url,strlen(url));
176 unhand(script)->_function = function ? makeJavaString(function,strlen(function)) : 0;
177 unhand(script)->_baseLineNumber = base;
178 unhand(script)->_lineExtent = extent;
179 unhand(script)->_nativePtr = (long)jsdscript;
181 /* add it to the hash table */
182 _putHash( ee, (JHandle*) unhand(controller)->scriptTable,
183 _constructInteger(ee, (long)jsdscript), (JHandle*)script );
186 if( unhand(controller)->scriptHook )
188 execute_java_dynamic_method( ee,(JHandle*)unhand(controller)->scriptHook,
190 "(Lnetscape/jsdebug/Script;)V",
196 JHandle* tbl = (JHandle*) unhand(controller)->scriptTable;
197 JHandle* key = _constructInteger(ee,(long)jsdscript);
199 /* find Java Object for Script */
200 script = (Hnetscape_jsdebug_Script*) _getHash( ee, tbl, key );
205 /* remove it from the hash table */
206 _removeHash( ee, tbl, key );
209 if( unhand(controller)->scriptHook )
211 execute_java_dynamic_method( ee,(JHandle*)unhand(controller)->scriptHook,
212 "aboutToUnloadScript",
213 "(Lnetscape/jsdebug/Script;)V",
216 /* set the Script as invalid */
217 execute_java_dynamic_method( ee,(JHandle*)script,
223 /***************************************************************************/
225 _executionHook( JSDContext* jsdc,
226 JSDThreadState* jsdstate,
230 Hnetscape_jsdebug_JSThreadState* threadState;
231 Hnetscape_jsdebug_Script* script;
233 JSDStackFrameInfo* jsdframe;
234 JSDScript* jsdscript;
240 if( ! context || ! controller || ! ee )
241 return JSD_HOOK_RETURN_HOOK_ERROR;
243 /* get the JSDStackFrameInfo */
244 jsdframe = JSD_GetStackFrame(jsdc, jsdstate);
246 return JSD_HOOK_RETURN_HOOK_ERROR;
248 /* get the JSDScript */
249 jsdscript = JSD_GetScriptForStackFrame(jsdc, jsdstate, jsdframe);
251 return JSD_HOOK_RETURN_HOOK_ERROR;
253 /* find Java Object for Script */
254 tblScript = (JHandle*) unhand(controller)->scriptTable;
255 keyScript = _constructInteger(ee, (long)jsdscript);
256 script = (Hnetscape_jsdebug_Script*) _getHash( ee, tblScript, keyScript );
258 return JSD_HOOK_RETURN_HOOK_ERROR;
260 /* generate a JSPC */
261 pc = JSD_GetPCForStackFrame(jsdc, jsdstate, jsdframe);
264 _constructJSPC(ee, script, pc);
266 return JSD_HOOK_RETURN_HOOK_ERROR;
268 /* build a JSThreadState */
269 threadState = (struct Hnetscape_jsdebug_JSThreadState*)
270 execute_java_constructor( ee, "netscape/jsdebug/JSThreadState",0,"()");
272 return JSD_HOOK_RETURN_HOOK_ERROR;
274 /* populate the ThreadState */
275 /* XXX FILL IN THE REST... */
276 unhand(threadState)->valid = 1; /* correct value for true? */
277 unhand(threadState)->currentFramePtr = (long) jsdframe;
278 unhand(threadState)->nativeThreadState = (long) jsdstate;
279 unhand(threadState)->continueState = netscape_jsdebug_JSThreadState_DEBUG_STATE_RUN;
281 /* XXX FILL IN THE REST... */
284 /* find and call the appropriate Hook */
285 if( JSD_HOOK_INTERRUPTED == type )
289 /* clear the JSD level hook (must reset on next sendInterrupt0()*/
290 JSD_ClearInterruptHook(context);
292 hook = (JHandle*) unhand(controller)->interruptHook;
294 return JSD_HOOK_RETURN_HOOK_ERROR;
297 execute_java_dynamic_method(
298 ee, hook, "aboutToExecute",
299 "(Lnetscape/jsdebug/ThreadStateBase;Lnetscape/jsdebug/PC;)V",
302 else if( JSD_HOOK_DEBUG_REQUESTED == type )
306 hook = (JHandle*) unhand(controller)->debugBreakHook;
308 return JSD_HOOK_RETURN_HOOK_ERROR;
311 execute_java_dynamic_method(
312 ee, hook, "aboutToExecute",
313 "(Lnetscape/jsdebug/ThreadStateBase;Lnetscape/jsdebug/PC;)V",
316 else if( JSD_HOOK_BREAKPOINT == type )
321 execute_java_dynamic_method(
322 ee,(JHandle*)controller,
323 "getInstructionHook0",
324 "(Lnetscape/jsdebug/PC;)Lnetscape/jsdebug/InstructionHook;",
327 return JSD_HOOK_RETURN_HOOK_ERROR;
330 execute_java_dynamic_method(
331 ee, hook, "aboutToExecute",
332 "(Lnetscape/jsdebug/ThreadStateBase;)V",
336 if( netscape_jsdebug_JSThreadState_DEBUG_STATE_THROW ==
337 unhand(threadState)->continueState )
338 return JSD_HOOK_RETURN_ABORT;
340 return JSD_HOOK_RETURN_CONTINUE;
344 _errorReporter( JSDContext* jsdc,
347 JSErrorReport* report,
352 JHandle* filename = NULL;
354 JHandle* linebuf = NULL;
358 if( ! context || ! controller || ! ee )
359 return JSD_ERROR_REPORTER_PASS_ALONG;
361 reporter = (JHandle*) unhand(controller)->errorReporter;
363 return JSD_ERROR_REPORTER_PASS_ALONG;
366 msg = (JHandle*) makeJavaString((char*)message, strlen(message));
367 if( report && report->filename )
368 filename = (JHandle*) makeJavaString((char*)report->filename, strlen(report->filename));
369 if( report && report->linebuf )
370 linebuf = (JHandle*) makeJavaString((char*)report->linebuf, strlen(report->linebuf));
372 lineno = report->lineno;
373 if( report && report->linebuf && report->tokenptr )
374 tokenOffset = report->tokenptr - report->linebuf;
377 execute_java_dynamic_method(
378 ee, reporter, "reportError",
379 "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;I)I",
387 /***************************************************************************/
388 /* from "_gen\netscape_jsdebug_DebugController.h" */
392 JSDContext* _simContext = 0;
394 void netscape_jsdebug_DebugController__setController(struct Hnetscape_jsdebug_DebugController * self,/*boolean*/ long on)
398 context = JSD_DebuggerOn();
402 _simContext = context; /* XXX HACK */
404 unhand(self)->_nativeContext = (long) context;
406 JSD_SetScriptHook(context, _scriptHook, (void*)1 );
407 JSD_SetErrorReporter(context, _errorReporter, (void*)1 );
408 JSD_SetDebugBreakHook(context, _executionHook, (void*)1 );
412 /* XXX stop somehow... */
414 JSD_SetDebugBreakHook(context, NULL, NULL );
415 JSD_SetErrorReporter(context, NULL, NULL);
416 JSD_SetScriptHook(context, NULL, NULL);
422 void netscape_jsdebug_DebugController_setInstructionHook0(struct Hnetscape_jsdebug_DebugController * self,struct Hnetscape_jsdebug_PC * pcOb)
424 Hnetscape_jsdebug_Script* script;
425 JSDScript* jsdscript;
429 if( ! context || ! controller || ! ee )
432 script = (Hnetscape_jsdebug_Script*)
433 execute_java_dynamic_method(ee, (JHandle*)pcOb, "getScript","()Lnetscape/jsdebug/Script;");
438 jsdscript = (JSDScript*) unhand(script)->_nativePtr;
443 execute_java_dynamic_method(ee, (JHandle*)pcOb, "getPC","()I");
445 JSD_SetExecutionHook(context, jsdscript, pc, _executionHook, 0);
448 void netscape_jsdebug_DebugController_sendInterrupt0(struct Hnetscape_jsdebug_DebugController * self)
450 if( ! context || ! controller )
452 JSD_SetInterruptHook(context, _executionHook, 0);
455 struct Hjava_lang_String *netscape_jsdebug_DebugController_executeScriptInStackFrame0(struct Hnetscape_jsdebug_DebugController *self,struct Hnetscape_jsdebug_JSStackFrameInfo *frame,struct Hjava_lang_String *src,struct Hjava_lang_String *filename,long lineno)
457 struct Hnetscape_jsdebug_JSThreadState* threadStateOb;
458 JSDThreadState* jsdthreadstate;
459 JSDStackFrameInfo* jsdframe;
467 threadStateOb = (struct Hnetscape_jsdebug_JSThreadState*)unhand(frame)->threadState;
468 jsdthreadstate = (JSDThreadState*) unhand(threadStateOb)->nativeThreadState;
470 jsdframe = (JSDStackFrameInfo*) unhand(frame)->_nativePtr;
472 if( ! context || ! controller || ! jsdframe )
475 filenameC = allocCString(filename);
478 srcC = allocCString(src);
485 srclen = strlen(srcC);
487 success = JSD_EvaluateScriptInStackFrame(context, jsdthreadstate, jsdframe,
489 filenameC, lineno, &rval);
491 /* XXX crashing on Windows under Symantec (though I can't see why!)*/
500 if( JSVAL_IS_NULL(rval) || JSVAL_IS_VOID(rval) )
503 jsstr = JSD_ValToStringInStackFrame(context,jsdthreadstate,jsdframe,rval);
507 /* XXXbe should use JS_GetStringChars and preserve Unicode. */
508 return makeJavaString((char*)JS_GetStringBytes(jsstr), JS_GetStringLength(jsstr));
511 long netscape_jsdebug_DebugController_getNativeMajorVersion(struct Hnetscape_jsdebug_DebugController* self)
513 return (long) JSD_GetMajorVersion();
516 long netscape_jsdebug_DebugController_getNativeMinorVersion(struct Hnetscape_jsdebug_DebugController* self)
518 return (long) JSD_GetMinorVersion();
521 /***************************************************************************/
522 /* from "_gen\netscape_jsdebug_Script.h" */
524 struct Hnetscape_jsdebug_JSPC *netscape_jsdebug_Script_getClosestPC(struct Hnetscape_jsdebug_Script * self,long line)
527 JSDScript* jsdscript;
529 if( ! context || ! controller )
532 jsdscript = (JSDScript*) unhand(self)->_nativePtr;
536 pc = JSD_GetClosestPC(context, jsdscript, line);
540 return _constructJSPC( 0, self, pc);
543 /***************************************************************************/
544 /* from "_gen\netscape_jsdebug_JSThreadState.h" */
546 long netscape_jsdebug_JSThreadState_countStackFrames(struct Hnetscape_jsdebug_JSThreadState * self)
548 JSDThreadState* jsdstate;
550 if( ! context || ! controller )
553 jsdstate = (JSDThreadState*) unhand(self)->nativeThreadState;
558 return (long) JSD_GetCountOfStackFrames(context, jsdstate);
561 struct Hnetscape_jsdebug_StackFrameInfo *netscape_jsdebug_JSThreadState_getCurrentFrame(struct Hnetscape_jsdebug_JSThreadState * self)
563 JSDThreadState* jsdstate;
564 JSDStackFrameInfo* jsdframe;
566 if( ! context || ! controller )
569 jsdstate = (JSDThreadState*) unhand(self)->nativeThreadState;
574 jsdframe = JSD_GetStackFrame(context, jsdstate);
578 return (struct Hnetscape_jsdebug_StackFrameInfo*)
579 _constructJSStackFrameInfo( 0, jsdframe, self );
583 /***************************************************************************/
584 /* from "_gen\netscape_jsdebug_JSStackFrameInfo.h" */
586 struct Hnetscape_jsdebug_StackFrameInfo *netscape_jsdebug_JSStackFrameInfo_getCaller0(struct Hnetscape_jsdebug_JSStackFrameInfo * self)
588 JSDStackFrameInfo* jsdframeCur;
589 JSDStackFrameInfo* jsdframeCaller;
590 struct Hnetscape_jsdebug_JSThreadState* threadState;
591 JSDThreadState* jsdthreadstate;
593 if( ! context || ! controller )
596 jsdframeCur = (JSDStackFrameInfo*) unhand(self)->_nativePtr;
600 threadState = (struct Hnetscape_jsdebug_JSThreadState*) unhand(self)->threadState;
604 jsdthreadstate = (JSDThreadState*) unhand(threadState)->nativeThreadState;
605 if( ! jsdthreadstate )
608 jsdframeCaller = JSD_GetCallingStackFrame(context, jsdthreadstate, jsdframeCur);
609 if( ! jsdframeCaller )
612 return (struct Hnetscape_jsdebug_StackFrameInfo*)
613 _constructJSStackFrameInfo( 0, jsdframeCaller, threadState );
615 struct Hnetscape_jsdebug_PC *netscape_jsdebug_JSStackFrameInfo_getPC(struct Hnetscape_jsdebug_JSStackFrameInfo * self)
617 JSDScript* jsdscript;
618 JSDStackFrameInfo* jsdframe;
619 struct Hnetscape_jsdebug_Script* script;
620 struct Hnetscape_jsdebug_JSThreadState* threadState;
621 JSDThreadState* jsdthreadstate;
625 if( ! context || ! controller || ! ee )
628 jsdframe = (JSDStackFrameInfo*) unhand(self)->_nativePtr;
632 threadState = (struct Hnetscape_jsdebug_JSThreadState*) unhand(self)->threadState;
636 jsdthreadstate = (JSDThreadState*) unhand(threadState)->nativeThreadState;
637 if( ! jsdthreadstate )
640 jsdscript = JSD_GetScriptForStackFrame(context, jsdthreadstate, jsdframe );
644 script = _scriptObFromJSDScriptPtr(ee, jsdscript);
648 pc = JSD_GetPCForStackFrame(context, jsdthreadstate, jsdframe);
652 return (struct Hnetscape_jsdebug_PC*) _constructJSPC(ee, script, pc);
655 /***************************************************************************/
656 /* from "_gen\netscape_jsdebug_JSPC.h" */
658 struct Hnetscape_jsdebug_SourceLocation *netscape_jsdebug_JSPC_getSourceLocation(struct Hnetscape_jsdebug_JSPC * self)
660 JSDScript* jsdscript;
661 struct Hnetscape_jsdebug_Script* script;
662 struct Hnetscape_jsdebug_JSPC* newPCOb;
668 if( ! context || ! controller || ! ee )
671 script = unhand(self)->script;
676 jsdscript = (JSDScript*) unhand(script)->_nativePtr;
679 pc = unhand(self)->pc;
681 line = JSD_GetClosestLine(context, jsdscript, pc);
682 newpc = JSD_GetClosestPC(context, jsdscript, line);
684 newPCOb = _constructJSPC(ee, script, newpc );
688 return (struct Hnetscape_jsdebug_SourceLocation *)
689 execute_java_constructor( ee, "netscape/jsdebug/JSSourceLocation", 0,
690 "(Lnetscape/jsdebug/JSPC;I)",
694 /***************************************************************************/
695 /* from "_gen\netscape_jsdebug_JSSourceTextProvider.h" */
697 struct Hnetscape_jsdebug_SourceTextItem *netscape_jsdebug_JSSourceTextProvider_loadSourceTextItem0(struct Hnetscape_jsdebug_JSSourceTextProvider * self,struct Hjava_lang_String * url)
699 /* this should attempt to load the source for the indicated URL */
703 void netscape_jsdebug_JSSourceTextProvider_refreshSourceTextVector(struct Hnetscape_jsdebug_JSSourceTextProvider * self)
708 JSDSourceText* iterp = 0;
711 struct Hjava_lang_String* urlOb;
714 if( ! context || ! controller || ! ee )
717 /* create new vector */
718 vec = (JHandle*) execute_java_constructor(ee, "netscape/util/Vector", 0, "()");
722 /* lock the native subsystem */
723 JSD_LockSourceTextSubsystem(context);
725 /* iterate through the native items */
726 while( 0 != (item = JSD_IterateSources(context, &iterp)) )
729 int status = JSD_GetSourceStatus(context,item);
731 /* try to find Java object */
732 url = JSD_GetSourceURL(context, item);
733 if( ! url || 0 == (urlStrLen = strlen(url)) ) /* ignoring those with no url */
736 urlOb = makeJavaString((char*)url,urlStrLen);
741 execute_java_dynamic_method( ee, (JHandle*)self, "findSourceTextItem0",
742 "(Ljava/lang/String;)Lnetscape/jsdebug/SourceTextItem;",
747 /* if not found then generate new item */
748 struct Hjava_lang_String* textOb;
752 if( ! JSD_GetSourceText(context, item, &str, &length ) )
757 textOb = makeJavaString((char*)str, length);
760 execute_java_constructor(ee, "netscape/jsdebug/SourceTextItem",0,
761 "(Ljava/lang/String;Ljava/lang/String;I)",
762 urlOb, textOb, status );
764 else if( JSD_IsSourceDirty(context, item) &&
765 JSD_SOURCE_CLEARED != status )
767 /* if found and dirty then update */
768 struct Hjava_lang_String* textOb;
772 if( ! JSD_GetSourceText(context, item, &str, &length ) )
777 textOb = makeJavaString((char*)str, length);
778 execute_java_dynamic_method(ee, itemOb, "setText",
779 "(Ljava/lang/String;)V", textOb);
780 execute_java_dynamic_method(ee, itemOb, "setStatus",
782 execute_java_dynamic_method(ee, itemOb, "setDirty", "(Z)V", 1 );
785 /* we have our copy; clear the native cached text */
786 if( JSD_SOURCE_INITED != status &&
787 JSD_SOURCE_PARTIAL != status &&
788 JSD_SOURCE_CLEARED != status )
790 JSD_ClearSourceText(context, item);
793 /* set the item clean */
794 JSD_SetSourceDirty(context, item, FALSE );
796 /* add the item to the vector */
798 execute_java_dynamic_method(ee, vec, "addElement",
799 "(Ljava/lang/Object;)V", itemOb );
801 /* unlock the native subsystem */
802 JSD_UnlockSourceTextSubsystem(context);
804 /* set main vector to our new vector */
806 unhand(self)->_sourceTextVector = (struct Hnetscape_util_Vector*) vec;