2003/06/16 06:24:31
[org.ibex.core.git] / src / org / xwt / js / Context.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.js;
3
4 import org.xwt.util.*;
5 import java.io.*;
6 import java.util.*;
7
8 /** encapsulates a single JavaScript thread and its state */
9 public class Context {
10
11     private static Hashtable javaThreadToContextMap = new Hashtable();
12     static Hashtable currentFunction = new Hashtable();
13
14     public Vec stack = new Vec();
15
16     /**
17      *  at any point in time, one JS Context can be bound to each Java thread;
18      *  this determines which context any call()s execute in
19      */
20     public void bindToCurrentThread() { javaThreadToContextMap.put(Thread.currentThread(), this); }
21
22     /** returns the current file and line number; intended for displaying to the user */
23     public static String getCurrentSourceNameAndLine() {
24         return getContextForThread(Thread.currentThread()).getCurrentFunction().getSourceName() + ":??";
25     }
26
27     public static String getSourceNameAndLineForThread(Thread t) {
28         CompiledFunction cf = getContextForThread(t).getCurrentFunction();
29         if (cf == null) return "null";
30         return cf.getSourceName() + ":??";
31     }
32
33     /** fetches the currently-executing javascript function */
34     public CompiledFunction getCurrentFunction() { return (CompiledFunction)currentFunction.get(Thread.currentThread()); }
35
36     public static Context getContextForThread(Thread t) {
37         Context ret = (Context)javaThreadToContextMap.get(t);
38         if (ret == null) {
39             ret = new Context();
40             ret.bindToCurrentThread();
41         }
42         return ret;
43     }
44
45     public static class CallMarker { public CallMarker() { } }
46     public static class LoopMarker {
47         public int location;
48         public String label;
49         public LoopMarker(int location, String label) {
50             this.location = location;
51             this.label = label;
52         }
53     }
54     public static class TryMarker {
55         public int location;
56         public JS.Scope scope;
57         public TryMarker(int location, JS.Scope scope) {
58             this.location = location;
59             this.scope = scope;
60         }
61     }
62
63 }