01: /*
02: * @(#)CompiledScript.java 1.2 05/05/26
03: *
04: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.compiler;
10:
11: import java.io.StringReader;
12: import java.io.ObjectInputStream;
13: import java.io.ObjectOutputStream;
14: import java.io.IOException;
15: import pnuts.lang.Pnuts;
16: import pnuts.lang.ParseException;
17: import pnuts.lang.Context;
18: import pnuts.lang.Runtime;
19: import pnuts.lang.Visitor;
20: import pnuts.lang.Escape;
21:
22: class CompiledScript extends Pnuts implements Compiled {
23:
24: static final long serialVersionUID = -6820627137085836944L;
25:
26: /**
27: * @serial
28: */
29: private Pnuts script;
30:
31: transient Runtime rt;
32:
33: /**
34: * Constructor
35:
36: * @param rt code generated by the bytecode compiler
37: * @param script the parsed script
38: */
39: public CompiledScript(Runtime rt, Pnuts script) {
40: this .scriptSource = script.getScriptSource();
41: if (script instanceof CompiledScript) {
42: CompiledScript compiled = (CompiledScript) script;
43: this .rt = compiled.rt;
44: this .script = compiled.script;
45: } else {
46: this .rt = rt;
47: this .script = script;
48: }
49: }
50:
51: public Object accept(Visitor visitor, Context context) {
52: if (visitor instanceof Compiler) {
53: Compiler c = (Compiler) visitor;
54: if (c.automatic) {
55: return accept(context);
56: } else {
57: return this ;
58: }
59: } else {
60: throw new RuntimeException(
61: "unsupported operation for compiled code");
62: }
63: }
64:
65: public Object accept(Context context) {
66: try {
67: return rt.run(context);
68: } catch (Escape esc) {
69: return esc.getValue();
70: }
71: }
72:
73: private void writeObject(ObjectOutputStream s) throws IOException {
74: s.defaultWriteObject();
75: s.writeObject(script);
76: }
77:
78: private void readObject(ObjectInputStream s) throws IOException,
79: ClassNotFoundException {
80: s.defaultReadObject();
81: this .script = (Pnuts) s.readObject();
82: Context ctx = Runtime.getThreadContext();
83: if (ctx == null) {
84: ctx = new Context();
85: }
86: CompiledScript compiled = (CompiledScript) new Compiler()
87: .compile(script, ctx);
88: this.rt = compiled.rt;
89: }
90: }
|