- public static abstract class ObjFunction extends Obj implements Function { }
-
- /** if JS calls a Java method, and the Java method throws an exception, it can only be caught by JS if it is a subclass of Exn. */
- public static class Exn extends RuntimeException {
- private Object js = null;
- public Exn(Object js) { this.js = js; }
- public Object getObject() { return js; }
- }
+ public static class Script extends Function {
+ Vector e = null;
+ private Script(Vector e) { this.e = e; }
+ public String getSourceName() throws JS.Exn { return ((Parser.Expr)e.elementAt(0)).sourceName; }
+ public Object _call(JS.Array args) throws JS.Exn {
+ Scope rootScope = (Scope)args.elementAt(0);
+ Function saved = (Function)currentFunction.get(Thread.currentThread());
+ currentFunction.put(Thread.currentThread(), this);
+ try {
+ for(int i=0; i<e.size(); i++)
+ ((Parser.Expr)e.elementAt(i)).eval(rootScope);
+ } catch (Parser.ReturnException e) {
+ // ignore
+ } catch (Parser.ControlTransferException e) {
+ throw new JS.Exn("block threw a ControlTransferException: " + e);
+ } finally {
+ if (saved == null) currentFunction.remove(Thread.currentThread());
+ else currentFunction.put(Thread.currentThread(), saved);
+ }
+ return null;
+ }
+ public static Script parse(Reader r, String sourceName, int line) throws IOException {
+ Parser p = new Parser(r, sourceName, line);
+ try {
+ Vector exprs = new Vector();
+ while(true) {
+ Parser.Expr ret = p.parseBlock(false);
+ if (ret == null || (ret.code == Parser.LC && ret.left == null)) break;
+ exprs.addElement(ret);
+ }
+ return new Script(exprs);
+ } catch (Exception e) {
+ if (Log.on) Log.log(Parser.class, e);
+ return null;
+ }
+ }
+ }