01: /*
02: * $Id: InlinedFunctionNode.java,v 1.7 2002/09/16 08:05:04 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.core.Any;
13: import anvil.core.runtime.AnyFunction;
14: import anvil.ErrorListener;
15: import anvil.script.CompilableFunction;
16: import anvil.codec.Code;
17: import anvil.codec.ConstantPool;
18: import anvil.script.Grammar;
19: import anvil.script.compiler.ByteCompiler;
20: import anvil.script.Type;
21: import anvil.script.Context;
22: import anvil.script.statements.FunctionStatement;
23: import java.io.IOException;
24:
25: /**
26: * class InlinedFunctionNode
27: *
28: * @author: Jani Lehtimäki
29: */
30: public class InlinedFunctionNode extends Node {
31:
32: protected FunctionStatement _context;
33: protected FunctionStatement _function;
34:
35: public InlinedFunctionNode(FunctionStatement context,
36: FunctionStatement function) {
37: super ();
38: _context = context;
39: _function = function;
40: }
41:
42: public int typeOf() {
43: return Node.EXPR_TYPE;
44: }
45:
46: public boolean isConstant() {
47: return false;
48: }
49:
50: public Node optimize() {
51: return this ;
52: }
53:
54: public void compile(ByteCompiler context, int operation) {
55: boolean in_class = (_function.getType() != Type.FUNCTION);
56: String name = (in_class ? "m_" : "f_") + _function.getName();
57: String signature = in_class ? "(Lanvil/core/Any;Lanvil/script/Function;Lanvil/script/StackFrame;)V"
58: : "(Lanvil/script/Function;Lanvil/script/StackFrame;)V";
59: Code code = context.getCode();
60: ConstantPool pool = code.getPool();
61: int anyfunction = pool
62: .addClass("anvil/core/runtime/AnyFunction");
63: code.anew(anyfunction);
64: code.dup();
65: int parent = _function.getParent().getTypeRef(pool);
66: int field = pool.addFieldRef(parent, name,
67: "Lanvil/script/Function;");
68: if (in_class) {
69: code.self();
70: }
71: code.getstatic(field);
72: code.aload(_context.getFrameIndex());
73: int depth = Grammar.countEscapeDepth(_context, _function);
74: if (depth >= 0) {
75: code.iconst(depth + 1);
76: code.invokevirtual(pool.addMethodRef(
77: "anvil/script/StackFrame", "getEscape",
78: "(I)Lanvil/script/StackFrame;"));
79: }
80: code.invokespecial(pool.addMethodRef(anyfunction, "<init>",
81: signature));
82: if (operation == GET_BOOLEAN) {
83: context.any2boolean();
84: }
85: }
86:
87: }
|