01: /*
02: * @(#)_finally.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 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 org.pnuts.lib;
10:
11: import pnuts.lang.*;
12:
13: /*
14: * function finally( { func1,} func2)
15: */
16: public class _finally extends PnutsFunction {
17:
18: private final static Object[] noarg = new Object[0];
19:
20: public _finally() {
21: super ("finally");
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 1 || nargs == 2;
26: }
27:
28: protected Object exec(final Object args[], Context context) {
29: int nargs = args.length;
30: if (nargs == 1) {
31: context.setExitHook(new Executable() {
32: public Object run(Context ctx) {
33: return ((PnutsFunction) args[0]).call(noarg, ctx);
34: }
35: });
36: return null;
37: } else if (nargs == 2) {
38: PnutsFunction f1 = (PnutsFunction) args[0];
39: PnutsFunction f2 = (PnutsFunction) args[1];
40: try {
41: return f1.call(noarg, context);
42: } finally {
43: f2.call(noarg, context);
44: }
45: } else {
46: undefined(args, context);
47: return null;
48: }
49: }
50:
51: public String toString() {
52: return "function finally( { tryFunc(), } finallyFunc())";
53: }
54: }
|