Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / jsd / jsd_java.c
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
4  *
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/
9  *
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.
14  *
15  * The Original Code is mozilla.org code.
16  *
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.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /* this is all going away... replaced by code in js/jsd/java */
39
40 #if 0
41
42 #include "native.h"
43 #include "jsdebug.h"
44
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"
52
53 static JSDContext* context = 0;
54 static struct Hnetscape_jsdebug_DebugController* controller = 0;
55
56 /***************************************************************************/
57 /* helpers */
58
59 static JHandle*
60 _constructInteger(ExecEnv *ee, long i)
61 {
62     return (JHandle*) 
63         execute_java_constructor(ee, "java/lang/Integer", 0, "(I)", i);
64 }
65
66 static JHandle*
67 _putHash(ExecEnv *ee, JHandle* tbl, JHandle* key, JHandle* ob)
68 {
69     return (JHandle*) 
70         execute_java_dynamic_method( 
71             ee, tbl, "put",
72             "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", 
73             key, ob );
74 }
75
76 static JHandle*
77 _getHash(ExecEnv *ee, JHandle* tbl, JHandle* key)
78 {
79     return (JHandle*) 
80         execute_java_dynamic_method( 
81             ee, tbl, "get",
82             "(Ljava/lang/Object;)Ljava/lang/Object;", 
83             key );
84 }
85
86 static JHandle*
87 _removeHash(ExecEnv *ee, JHandle* tbl, JHandle* key)
88 {
89     return (JHandle*) 
90         execute_java_dynamic_method( 
91             ee, tbl, "remove",
92             "(Ljava/lang/Object;)Ljava/lang/Object;", 
93             key );
94 }
95
96 struct Hnetscape_jsdebug_JSStackFrameInfo*
97 _constructJSStackFrameInfo( ExecEnv* ee, JSDStackFrameInfo* jsdframe, 
98                             struct Hnetscape_jsdebug_JSThreadState* threadState )
99 {
100     struct Hnetscape_jsdebug_JSStackFrameInfo* frame;
101
102     frame = (struct Hnetscape_jsdebug_JSStackFrameInfo*)
103         execute_java_constructor( ee, "netscape/jsdebug/JSStackFrameInfo", 0, 
104                                   "(Lnetscape/jsdebug/JSThreadState;)",
105                                   threadState );
106     if( ! frame )
107         return NULL;
108
109     /* XXX fill in additional fields */
110     unhand(frame)->_nativePtr = (long) jsdframe;
111
112     return frame;
113 }
114
115 struct Hnetscape_jsdebug_JSPC*
116 _constructJSPC( ExecEnv* ee, struct Hnetscape_jsdebug_Script* script, long pc )
117 {
118     struct Hnetscape_jsdebug_JSPC * pcOb;
119
120     pcOb = (struct Hnetscape_jsdebug_JSPC *)
121         execute_java_constructor( ee, "netscape/jsdebug/JSPC", 0, 
122                                   "(Lnetscape/jsdebug/Script;I)",
123                                   script, pc );
124     if( ! pcOb )
125         return NULL;
126
127     /* XXX fill in additional fields */
128
129     return pcOb;
130 }
131
132 static Hnetscape_jsdebug_Script*
133 _scriptObFromJSDScriptPtr( ExecEnv* ee, JSDScript* jsdscript )
134 {
135     JHandle* tbl = (JHandle*) unhand(controller)->scriptTable;
136     JHandle* key = _constructInteger(ee,(long)jsdscript);
137     return (Hnetscape_jsdebug_Script*) _getHash( ee, tbl, key );
138 }
139
140 /***************************************************************************/
141
142 void
143 _scriptHook( JSDContext* jsdc, 
144              JSDScript*  jsdscript,
145              JSBool      creating,
146              void*       callerdata )
147 {
148     Hnetscape_jsdebug_Script* script;
149     ExecEnv* ee = EE();
150
151     if( ! context || ! controller || ! ee )
152         return;
153
154     if( creating )
155     {
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);
160
161         if( ! url )
162         {
163             return;
164             /* url = ""; */
165         }
166
167         /* create Java Object for Script */
168         script = (Hnetscape_jsdebug_Script*)
169                 execute_java_constructor(ee, "netscape/jsdebug/Script", 0, "()");
170
171         if( ! script )
172             return;
173
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;
180
181         /* add it to the hash table */
182         _putHash( ee, (JHandle*) unhand(controller)->scriptTable,
183                   _constructInteger(ee, (long)jsdscript), (JHandle*)script );
184
185         /* call the hook */
186         if( unhand(controller)->scriptHook )
187         {
188             execute_java_dynamic_method( ee,(JHandle*)unhand(controller)->scriptHook,
189                                          "justLoadedScript",
190                                          "(Lnetscape/jsdebug/Script;)V", 
191                                          script );
192         }
193     }
194     else
195     {
196         JHandle* tbl = (JHandle*) unhand(controller)->scriptTable;
197         JHandle* key = _constructInteger(ee,(long)jsdscript);
198
199         /* find Java Object for Script    */
200         script = (Hnetscape_jsdebug_Script*) _getHash( ee, tbl, key );
201
202         if( ! script )
203             return;
204
205         /* remove it from the hash table  */
206         _removeHash( ee, tbl, key );
207
208         /* call the hook */
209         if( unhand(controller)->scriptHook )
210         {
211             execute_java_dynamic_method( ee,(JHandle*)unhand(controller)->scriptHook,
212                                          "aboutToUnloadScript",
213                                          "(Lnetscape/jsdebug/Script;)V", 
214                                          script );
215         }
216         /* set the Script as invalid */
217         execute_java_dynamic_method( ee,(JHandle*)script,
218                                      "_setInvalid",
219                                      "()V" );
220     }
221 }
222
223 /***************************************************************************/
224 PRUintn
225 _executionHook( JSDContext*     jsdc, 
226                 JSDThreadState* jsdstate,
227                 PRUintn         type,
228                 void*           callerdata )
229 {
230     Hnetscape_jsdebug_JSThreadState* threadState;
231     Hnetscape_jsdebug_Script* script;
232     JHandle* pcOb;
233     JSDStackFrameInfo* jsdframe;
234     JSDScript*  jsdscript;
235     int pc;
236     JHandle* tblScript;
237     JHandle* keyScript;
238     ExecEnv* ee = EE();
239
240     if( ! context || ! controller || ! ee )
241         return JSD_HOOK_RETURN_HOOK_ERROR;
242
243     /* get the JSDStackFrameInfo */
244     jsdframe = JSD_GetStackFrame(jsdc, jsdstate);
245     if( ! jsdframe )
246         return JSD_HOOK_RETURN_HOOK_ERROR;
247
248     /* get the JSDScript */
249     jsdscript = JSD_GetScriptForStackFrame(jsdc, jsdstate, jsdframe);
250     if( ! jsdscript )
251         return JSD_HOOK_RETURN_HOOK_ERROR;
252
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 );
257     if( ! script )
258         return JSD_HOOK_RETURN_HOOK_ERROR;
259
260     /* generate a JSPC */
261     pc = JSD_GetPCForStackFrame(jsdc, jsdstate, jsdframe);
262
263     pcOb = (JHandle*)
264         _constructJSPC(ee, script, pc);
265     if( ! pcOb )
266         return JSD_HOOK_RETURN_HOOK_ERROR;
267
268     /* build a JSThreadState */
269     threadState = (struct Hnetscape_jsdebug_JSThreadState*)
270             execute_java_constructor( ee, "netscape/jsdebug/JSThreadState",0,"()");
271     if( ! threadState )
272         return JSD_HOOK_RETURN_HOOK_ERROR;
273
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;
280
281     /* XXX FILL IN THE REST... */
282
283
284     /* find and call the appropriate Hook */
285     if( JSD_HOOK_INTERRUPTED == type )
286     {
287         JHandle* hook;
288
289         /* clear the JSD level hook (must reset on next sendInterrupt0()*/
290         JSD_ClearInterruptHook(context); 
291
292         hook = (JHandle*) unhand(controller)->interruptHook;
293         if( ! hook )
294             return JSD_HOOK_RETURN_HOOK_ERROR;
295
296         /* call the hook */
297         execute_java_dynamic_method( 
298             ee, hook, "aboutToExecute",
299             "(Lnetscape/jsdebug/ThreadStateBase;Lnetscape/jsdebug/PC;)V",
300             threadState, pcOb );
301     }
302     else if( JSD_HOOK_DEBUG_REQUESTED == type )
303     {
304         JHandle* hook;
305
306         hook = (JHandle*) unhand(controller)->debugBreakHook;
307         if( ! hook )
308             return JSD_HOOK_RETURN_HOOK_ERROR;
309
310         /* call the hook */
311         execute_java_dynamic_method( 
312             ee, hook, "aboutToExecute",
313             "(Lnetscape/jsdebug/ThreadStateBase;Lnetscape/jsdebug/PC;)V",
314             threadState, pcOb );
315     }
316     else if( JSD_HOOK_BREAKPOINT == type )
317     {
318         JHandle* hook;
319
320         hook = (JHandle*)
321                 execute_java_dynamic_method( 
322                         ee,(JHandle*)controller,
323                         "getInstructionHook0",
324                         "(Lnetscape/jsdebug/PC;)Lnetscape/jsdebug/InstructionHook;",
325                         pcOb );
326         if( ! hook )
327             return JSD_HOOK_RETURN_HOOK_ERROR;
328
329         /* call the hook */
330         execute_java_dynamic_method( 
331             ee, hook, "aboutToExecute",
332             "(Lnetscape/jsdebug/ThreadStateBase;)V",
333             threadState );
334     }
335
336     if( netscape_jsdebug_JSThreadState_DEBUG_STATE_THROW == 
337         unhand(threadState)->continueState )
338         return JSD_HOOK_RETURN_ABORT;
339
340     return JSD_HOOK_RETURN_CONTINUE;
341 }
342
343 PRUintn
344 _errorReporter( JSDContext*     jsdc, 
345                 JSContext*      cx,
346                 const char*     message, 
347                 JSErrorReport*  report,
348                 void*           callerdata )
349 {
350     JHandle* reporter;
351     JHandle* msg         = NULL;
352     JHandle* filename    = NULL;
353     int      lineno      = 0;
354     JHandle* linebuf     = NULL;
355     int      tokenOffset = 0;
356     ExecEnv* ee = EE();
357
358     if( ! context || ! controller || ! ee )
359         return JSD_ERROR_REPORTER_PASS_ALONG;
360
361     reporter = (JHandle*) unhand(controller)->errorReporter;
362     if( ! reporter )
363         return JSD_ERROR_REPORTER_PASS_ALONG;
364
365     if( message )
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));
371     if( report )
372         lineno = report->lineno;
373     if( report && report->linebuf && report->tokenptr )
374         tokenOffset = report->tokenptr - report->linebuf;
375
376     return (int) 
377         execute_java_dynamic_method( 
378                 ee, reporter, "reportError",
379                 "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;I)I", 
380                 msg,
381                 filename,
382                 lineno,
383                 linebuf,
384                 tokenOffset );
385 }
386
387 /***************************************************************************/
388 /* from "_gen\netscape_jsdebug_DebugController.h" */
389
390
391 /* XXX HACK */
392 JSDContext* _simContext = 0;
393
394 void netscape_jsdebug_DebugController__setController(struct Hnetscape_jsdebug_DebugController * self,/*boolean*/ long on)
395 {
396     if(on)
397     {
398         context = JSD_DebuggerOn();
399         if( ! context )
400             return;
401
402         _simContext = context; /* XXX HACK */
403
404         unhand(self)->_nativeContext = (long) context;
405         controller = self;
406         JSD_SetScriptHook(context, _scriptHook, (void*)1 );
407         JSD_SetErrorReporter(context, _errorReporter, (void*)1 );
408         JSD_SetDebugBreakHook(context, _executionHook, (void*)1 );
409     }
410     else
411     {
412         /* XXX stop somehow... */
413         /* kill context        */
414         JSD_SetDebugBreakHook(context, NULL, NULL );
415         JSD_SetErrorReporter(context, NULL, NULL);
416         JSD_SetScriptHook(context, NULL, NULL);
417         context = 0;
418         controller = 0;
419     }
420 }
421
422 void netscape_jsdebug_DebugController_setInstructionHook0(struct Hnetscape_jsdebug_DebugController * self,struct Hnetscape_jsdebug_PC * pcOb)
423 {
424     Hnetscape_jsdebug_Script* script;
425     JSDScript* jsdscript;
426     PRUintn pc;
427     ExecEnv* ee = EE();
428
429     if( ! context || ! controller || ! ee )
430         return;
431
432     script = (Hnetscape_jsdebug_Script*)
433         execute_java_dynamic_method(ee, (JHandle*)pcOb, "getScript","()Lnetscape/jsdebug/Script;");
434
435     if( ! script )
436         return;
437
438     jsdscript = (JSDScript*) unhand(script)->_nativePtr;
439     if( ! jsdscript )
440         return;
441
442     pc = (PRUintn)
443         execute_java_dynamic_method(ee, (JHandle*)pcOb, "getPC","()I");
444
445     JSD_SetExecutionHook(context, jsdscript, pc, _executionHook, 0);
446 }
447
448 void netscape_jsdebug_DebugController_sendInterrupt0(struct Hnetscape_jsdebug_DebugController * self)
449 {
450     if( ! context || ! controller )
451         return;
452     JSD_SetInterruptHook(context, _executionHook, 0);
453 }
454
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)
456 {
457     struct Hnetscape_jsdebug_JSThreadState* threadStateOb;
458     JSDThreadState* jsdthreadstate;
459     JSDStackFrameInfo* jsdframe;
460     char* filenameC;
461     char* srcC;
462     JSString* jsstr;
463     jsval rval;
464     JSBool success;
465     int srclen;
466
467     threadStateOb = (struct Hnetscape_jsdebug_JSThreadState*)unhand(frame)->threadState;
468     jsdthreadstate = (JSDThreadState*) unhand(threadStateOb)->nativeThreadState;
469
470     jsdframe = (JSDStackFrameInfo*) unhand(frame)->_nativePtr;
471
472     if( ! context || ! controller || ! jsdframe )
473         return NULL;
474
475     filenameC = allocCString(filename);
476     if( ! filenameC )
477         return NULL;
478     srcC = allocCString(src);
479     if( ! srcC )
480     {
481         free(filenameC);
482         return NULL;
483     }
484
485     srclen = strlen(srcC);
486
487     success = JSD_EvaluateScriptInStackFrame(context, jsdthreadstate, jsdframe, 
488                                              srcC, srclen,
489                                              filenameC, lineno, &rval);
490
491     /* XXX crashing on Windows under Symantec (though I can't see why!)*/
492
493     free(filenameC);
494     free(srcC);
495
496
497     if( ! success )
498         return NULL;
499
500     if( JSVAL_IS_NULL(rval) || JSVAL_IS_VOID(rval) )
501         return NULL;
502
503     jsstr = JSD_ValToStringInStackFrame(context,jsdthreadstate,jsdframe,rval);
504     if( ! jsstr )
505         return NULL;
506
507     /* XXXbe should use JS_GetStringChars and preserve Unicode. */
508     return makeJavaString((char*)JS_GetStringBytes(jsstr), JS_GetStringLength(jsstr));
509 }    
510
511 long netscape_jsdebug_DebugController_getNativeMajorVersion(struct Hnetscape_jsdebug_DebugController* self)
512 {
513     return (long) JSD_GetMajorVersion();
514 }
515
516 long netscape_jsdebug_DebugController_getNativeMinorVersion(struct Hnetscape_jsdebug_DebugController* self)
517 {
518     return (long) JSD_GetMinorVersion();
519 }
520
521 /***************************************************************************/
522 /* from "_gen\netscape_jsdebug_Script.h" */
523
524 struct Hnetscape_jsdebug_JSPC *netscape_jsdebug_Script_getClosestPC(struct Hnetscape_jsdebug_Script * self,long line)
525 {
526     PRUintn pc;
527     JSDScript* jsdscript;
528
529     if( ! context || ! controller )
530         return 0;
531
532     jsdscript = (JSDScript*) unhand(self)->_nativePtr;
533     if( ! jsdscript )
534         return 0;
535
536     pc = JSD_GetClosestPC(context, jsdscript, line);
537
538     if( -1 == pc )
539         return 0;
540     return _constructJSPC( 0, self, pc);
541 }
542
543 /***************************************************************************/
544 /* from "_gen\netscape_jsdebug_JSThreadState.h" */
545
546 long netscape_jsdebug_JSThreadState_countStackFrames(struct Hnetscape_jsdebug_JSThreadState * self)
547 {
548     JSDThreadState* jsdstate;
549         
550     if( ! context || ! controller )
551         return 0;
552
553     jsdstate = (JSDThreadState*) unhand(self)->nativeThreadState;
554
555     if( ! jsdstate )
556         return 0;
557
558     return (long) JSD_GetCountOfStackFrames(context, jsdstate);
559 }
560
561 struct Hnetscape_jsdebug_StackFrameInfo *netscape_jsdebug_JSThreadState_getCurrentFrame(struct Hnetscape_jsdebug_JSThreadState * self)
562 {
563     JSDThreadState* jsdstate;
564     JSDStackFrameInfo* jsdframe;
565         
566     if( ! context || ! controller )
567         return NULL;
568
569     jsdstate = (JSDThreadState*) unhand(self)->nativeThreadState;
570
571     if( ! jsdstate )
572         return NULL;
573
574     jsdframe = JSD_GetStackFrame(context, jsdstate);
575     if( ! jsdframe )
576         return NULL;
577
578     return (struct Hnetscape_jsdebug_StackFrameInfo*)
579                 _constructJSStackFrameInfo( 0, jsdframe, self );
580 }
581
582
583 /***************************************************************************/
584 /* from "_gen\netscape_jsdebug_JSStackFrameInfo.h" */
585
586 struct Hnetscape_jsdebug_StackFrameInfo *netscape_jsdebug_JSStackFrameInfo_getCaller0(struct Hnetscape_jsdebug_JSStackFrameInfo * self)
587 {
588     JSDStackFrameInfo* jsdframeCur;
589     JSDStackFrameInfo* jsdframeCaller;
590     struct Hnetscape_jsdebug_JSThreadState* threadState;
591     JSDThreadState* jsdthreadstate;
592
593     if( ! context || ! controller )
594         return NULL;
595
596     jsdframeCur = (JSDStackFrameInfo*) unhand(self)->_nativePtr;
597     if( ! jsdframeCur )
598         return NULL;
599
600     threadState = (struct Hnetscape_jsdebug_JSThreadState*) unhand(self)->threadState;
601     if( ! threadState )
602         return NULL;
603
604     jsdthreadstate = (JSDThreadState*) unhand(threadState)->nativeThreadState;
605     if( ! jsdthreadstate )
606         return NULL;
607
608     jsdframeCaller = JSD_GetCallingStackFrame(context, jsdthreadstate, jsdframeCur);
609     if( ! jsdframeCaller )
610         return NULL;
611
612     return (struct Hnetscape_jsdebug_StackFrameInfo*)
613                 _constructJSStackFrameInfo( 0, jsdframeCaller, threadState );
614 }
615 struct Hnetscape_jsdebug_PC *netscape_jsdebug_JSStackFrameInfo_getPC(struct Hnetscape_jsdebug_JSStackFrameInfo * self)
616 {
617     JSDScript* jsdscript;
618     JSDStackFrameInfo* jsdframe;
619     struct Hnetscape_jsdebug_Script* script;
620     struct Hnetscape_jsdebug_JSThreadState* threadState;
621     JSDThreadState* jsdthreadstate;
622     int pc;
623     ExecEnv* ee = EE();
624
625     if( ! context || ! controller || ! ee )
626         return NULL;
627
628     jsdframe = (JSDStackFrameInfo*) unhand(self)->_nativePtr;
629     if( ! jsdframe )
630         return NULL;
631
632     threadState = (struct Hnetscape_jsdebug_JSThreadState*) unhand(self)->threadState;
633     if( ! threadState )
634         return NULL;
635
636     jsdthreadstate = (JSDThreadState*) unhand(threadState)->nativeThreadState;
637     if( ! jsdthreadstate )
638         return NULL;
639
640     jsdscript = JSD_GetScriptForStackFrame(context, jsdthreadstate, jsdframe );
641     if( ! jsdscript )
642         return NULL;
643
644     script = _scriptObFromJSDScriptPtr(ee, jsdscript);
645     if( ! script )
646         return NULL;
647
648     pc = JSD_GetPCForStackFrame(context, jsdthreadstate, jsdframe);
649     if( ! pc )
650         return NULL;
651
652     return (struct Hnetscape_jsdebug_PC*) _constructJSPC(ee, script, pc);
653 }
654
655 /***************************************************************************/
656 /* from "_gen\netscape_jsdebug_JSPC.h" */
657
658 struct Hnetscape_jsdebug_SourceLocation *netscape_jsdebug_JSPC_getSourceLocation(struct Hnetscape_jsdebug_JSPC * self)
659 {
660     JSDScript* jsdscript;
661     struct Hnetscape_jsdebug_Script* script;
662     struct Hnetscape_jsdebug_JSPC* newPCOb;
663     int line;
664     int newpc;
665     int pc;
666     ExecEnv* ee = EE();
667
668     if( ! context || ! controller || ! ee )
669         return NULL;
670
671     script = unhand(self)->script;
672
673     if( ! script )
674         return NULL;
675
676     jsdscript = (JSDScript*) unhand(script)->_nativePtr;
677     if( ! jsdscript )
678         return NULL;
679     pc = unhand(self)->pc;
680
681     line = JSD_GetClosestLine(context, jsdscript, pc);
682     newpc = JSD_GetClosestPC(context, jsdscript, line);
683
684     newPCOb = _constructJSPC(ee, script, newpc );
685     if( ! newPCOb )
686         return NULL;
687
688     return (struct Hnetscape_jsdebug_SourceLocation *)
689         execute_java_constructor( ee, "netscape/jsdebug/JSSourceLocation", 0, 
690                                   "(Lnetscape/jsdebug/JSPC;I)",
691                                   newPCOb, line );
692 }
693
694 /***************************************************************************/
695 /* from "_gen\netscape_jsdebug_JSSourceTextProvider.h" */
696
697 struct Hnetscape_jsdebug_SourceTextItem *netscape_jsdebug_JSSourceTextProvider_loadSourceTextItem0(struct Hnetscape_jsdebug_JSSourceTextProvider * self,struct Hjava_lang_String * url)
698 {
699     /* this should attempt to load the source for the indicated URL */
700     return NULL;    
701 }
702
703 void netscape_jsdebug_JSSourceTextProvider_refreshSourceTextVector(struct Hnetscape_jsdebug_JSSourceTextProvider * self)
704 {
705
706     JHandle* vec;
707     JHandle* itemOb;
708     JSDSourceText* iterp = 0;
709     JSDSourceText* item;
710     const char* url;
711     struct Hjava_lang_String* urlOb;
712     ExecEnv* ee = EE();
713
714     if( ! context || ! controller || ! ee )
715         return;
716
717     /* create new vector */
718     vec = (JHandle*) execute_java_constructor(ee, "netscape/util/Vector", 0, "()");
719     if( ! vec )
720         return;
721
722     /* lock the native subsystem */
723     JSD_LockSourceTextSubsystem(context);
724
725     /* iterate through the native items */
726     while( 0 != (item = JSD_IterateSources(context, &iterp)) )
727     {
728         int urlStrLen;
729         int status = JSD_GetSourceStatus(context,item);
730
731         /* try to find Java object */
732         url = JSD_GetSourceURL(context, item);
733         if( ! url || 0 == (urlStrLen = strlen(url)) ) /* ignoring those with no url */
734             continue;
735
736         urlOb = makeJavaString((char*)url,urlStrLen);
737         if( ! urlOb )
738             continue;
739
740         itemOb = (JHandle*)
741             execute_java_dynamic_method( ee, (JHandle*)self, "findSourceTextItem0",
742                       "(Ljava/lang/String;)Lnetscape/jsdebug/SourceTextItem;",
743                       urlOb );
744                       
745         if( ! itemOb )
746         {
747             /* if not found then generate new item */
748             struct Hjava_lang_String* textOb;
749             const char* str;
750             int length;
751
752             if( ! JSD_GetSourceText(context, item, &str, &length ) )
753             {
754                 str = "";
755                 length = 0;
756             }
757             textOb = makeJavaString((char*)str, length);
758
759             itemOb = (JHandle*)
760                 execute_java_constructor(ee, "netscape/jsdebug/SourceTextItem",0,  
761                      "(Ljava/lang/String;Ljava/lang/String;I)",
762                      urlOb, textOb, status );
763         }
764         else if( JSD_IsSourceDirty(context, item) && 
765                  JSD_SOURCE_CLEARED != status )
766         {
767             /* if found and dirty then update */
768             struct Hjava_lang_String* textOb;
769             const char* str;
770             int length;
771
772             if( ! JSD_GetSourceText(context, item, &str, &length ) )
773             {
774                 str = "";
775                 length = 0;
776             }
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", 
781                                         "(I)V", status );
782             execute_java_dynamic_method(ee, itemOb, "setDirty", "(Z)V", 1 );
783         }
784
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 )
789         {
790             JSD_ClearSourceText(context, item);
791         }
792
793         /* set the item clean */
794         JSD_SetSourceDirty(context, item, FALSE );
795
796         /* add the item to the vector */
797         if( itemOb )
798             execute_java_dynamic_method(ee, vec, "addElement", 
799                                         "(Ljava/lang/Object;)V", itemOb );
800     }
801     /* unlock the native subsystem */
802     JSD_UnlockSourceTextSubsystem(context);
803
804     /* set main vector to our new vector */
805
806     unhand(self)->_sourceTextVector = (struct Hnetscape_util_Vector*) vec;
807 }    
808
809
810 #endif