01: /*
02: * $Id: ThunkNode.java,v 1.5 2002/09/16 08:05:05 jkl Exp $
03: *
04: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
05: *
06: * Use is subject to license terms, as defined in
07: * Anvil Sofware License, Version 1.1. See LICENSE
08: * file, or http://njet.org/license-1.1.txt
09: */
10: package anvil.script.expression;
11:
12: import anvil.ErrorListener;
13: import anvil.Location;
14: import anvil.core.Any;
15: import anvil.codec.Code;
16: import anvil.codec.ConstantPool;
17: import anvil.script.compiler.ByteCompiler;
18: import anvil.script.statements.FunctionStatement;
19: import anvil.script.Context;
20: import anvil.script.Function;
21: import anvil.script.Type;
22: import java.io.IOException;
23:
24: /**
25: * class ThunkNode
26: *
27: * @author: Jani Lehtimäki
28: */
29: public class ThunkNode extends Node {
30:
31: protected FunctionStatement _context;
32: protected Function _thunk;
33:
34: public ThunkNode(FunctionStatement context, Function thunk) {
35: super ();
36: _context = context;
37: _thunk = thunk;
38: }
39:
40: public int typeOf() {
41: return Node.EXPR_THUNK;
42: }
43:
44: public boolean isConstant() {
45: return false;
46: }
47:
48: public void compile(ByteCompiler context, int operation) {
49: Code code = context.getCode();
50: ConstantPool pool = code.getPool();
51:
52: boolean inclass = (_thunk.getType() != Type.FUNCTION);
53:
54: int clazz = pool.addClass("anvil/script/Thunk");
55: code.anew(clazz);
56: code.dup();
57:
58: String name = (inclass ? "m_" : "f_") + _thunk.getName();
59: String signature = inclass ? "(Lanvil/script/StackFrame;Lanvil/script/Function;Lanvil/core/Any;)V"
60: : "(Lanvil/script/StackFrame;Lanvil/script/Function;)V";
61:
62: if (_context != null) {
63: code.aload(_context.getFrameIndex());
64: } else {
65: code.aconst_null();
66: }
67: code.getstatic(pool.addFieldRef(_thunk.getParent().getTypeRef(
68: pool), name, "Lanvil/script/Function;"));
69: if (inclass) {
70: code.self();
71: }
72: code.invokespecial(pool
73: .addMethodRef(clazz, "<init>", signature));
74:
75: if (operation == GET_BOOLEAN) {
76: context.any2boolean();
77: }
78: }
79:
80: }
|