001: /*=============================================================================
002: * Copyright Texas Instruments 2003-2004. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package oscript.data;
020:
021: import oscript.exceptions.*;
022: import oscript.classwrap.ClassWrapGen;
023: import oscript.syntaxtree.FunctionCallExpressionList;
024: import oscript.OscriptInterpreter;
025: import oscript.NodeEvaluator;
026:
027: import java.util.*;
028:
029: /**
030: * A proxy object acts as a proxy, all attempts to resolve a member go
031: * thru the <code>resolve</code> method which should be implemented by
032: * the derived script class.
033: *
034: * @author Rob Clark (rob@ti.com)
035: */
036: public class Proxy extends OObject {
037: /**
038: * The type object for an instance of Proxy.
039: */
040: public final static Value TYPE = BuiltinType
041: .makeBuiltinType("oscript.data.Proxy");
042: public final static String PARENT_TYPE_NAME = "oscript.data.OObject";
043: public final static String TYPE_NAME = "Proxy";
044: public final static String[] MEMBER_NAMES = new String[] { "getTypeMember" };
045:
046: /**
047: * Class Constructor.
048: */
049: public Proxy() {
050: super ();
051: }
052:
053: /**
054: * Class Constructor. This is the constructor that gets called via an
055: * BuiltinType instance.
056: *
057: * @param args arguments to this constructor
058: * @throws PackagedScriptObjectException(Exception) if wrong number of args
059: */
060: public Proxy(oscript.util.MemberTable args) {
061: this ();
062:
063: if (args.length() != 0)
064: throw PackagedScriptObjectException
065: .makeExceptionWrapper(new OIllegalArgumentException(
066: "wrong number of args!"));
067: }
068:
069: /**
070: * Get the type of this object. The returned type doesn't have to take
071: * into account the possibility of a script type extending a built-in
072: * type, since that is handled by {@link #getType}.
073: *
074: * @return the object's type
075: */
076: protected Value getTypeImpl() {
077: return TYPE;
078: }
079:
080: /**
081: * Get a member of this type. This uses the resolve() method implemented
082: * by the derived script class to help resolve members.
083: *
084: * @param obj an object of this type
085: * @param id the id of the symbol that maps to the member
086: * @return a reference to the member, or null
087: */
088: protected Value getTypeMember(Value obj, int id) {
089: Value name = Symbol.getSymbol(id);
090: Value val = null;
091:
092: Thread currentThread = Thread.currentThread();
093: if (!recursedSet.contains(currentThread)) {
094: try {
095: recursedSet.add(currentThread);
096: Value resolve = obj.getMember(RESOLVE, false);
097: if (resolve != null)
098: val = resolve.callAsFunction(new Value[] { name });
099: } finally {
100: recursedSet.remove(currentThread);
101: }
102: }
103:
104: if (val != null)
105: return val;
106:
107: return super .getTypeMember(obj, id);
108: }
109:
110: private Set recursedSet = new HashSet();
111:
112: private static final int RESOLVE = Symbol.getSymbol("resolve")
113: .getId();
114:
115: }
116:
117: /*
118: * Local Variables:
119: * tab-width: 2
120: * indent-tabs-mode: nil
121: * mode: java
122: * c-indentation-style: java
123: * c-basic-offset: 2
124: * eval: (c-set-offset 'substatement-open '0)
125: * eval: (c-set-offset 'case-label '+)
126: * eval: (c-set-offset 'inclass '+)
127: * eval: (c-set-offset 'inline-open '0)
128: * End:
129: */
|