001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.program;
031:
032: import com.caucho.quercus.env.Env;
033: import com.caucho.quercus.env.Value;
034: import com.caucho.quercus.expr.Expr;
035: import com.caucho.util.L10N;
036:
037: import java.util.logging.Logger;
038:
039: /**
040: * Represents a compiled function with 2 args
041: */
042: abstract public class CompiledFunction_2 extends CompiledFunction {
043: private static final Logger log = Logger
044: .getLogger(CompiledFunction_2.class.getName());
045: private static final L10N L = new L10N(CompiledFunction_2.class);
046:
047: private String _name;
048: private Expr _default_0;
049: private Expr _default_1;
050:
051: public CompiledFunction_2(String name, Expr default_0,
052: Expr default_1) {
053: _name = name;
054: _default_0 = default_0;
055: _default_1 = default_1;
056: }
057:
058: /**
059: * Returns this function's name.
060: */
061: @Override
062: public String getName() {
063: return _name;
064: }
065:
066: /**
067: * Binds the user's arguments to the actual arguments.
068: *
069: * @param args the user's arguments
070: * @return the user arguments augmented by any defaults
071: */
072: public Expr[] bindArguments(Env env, Expr fun, Expr[] args) {
073: if (args.length > 2)
074: log.fine(L.l(env.getLocation().getMessagePrefix()
075: + "incorrect number of arguments"
076: + env.getFunctionLocation()));
077:
078: return args;
079: }
080:
081: public Value call(Env env, Value[] argValues) {
082: switch (argValues.length) {
083: case 0:
084: return call(env, _default_0.eval(env), _default_1.eval(env));
085: case 1:
086: return call(env, argValues[0], _default_1.eval(env));
087: case 2:
088: default:
089: return call(env, argValues[0], argValues[1]);
090: }
091: }
092:
093: public Value call(Env env) {
094: return call(env, _default_0.eval(env), _default_1.eval(env));
095: }
096:
097: public Value call(Env env, Value a0) {
098: return call(env, a0, _default_1.eval(env));
099: }
100:
101: public String toString() {
102: return "CompiledFunction_1[" + _name + "]";
103: }
104: }
|