001:/*
002: * $Id: AnyScope.java,v 1.4 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010:package anvil.core.runtime;
011:
012:import java.io.IOException;
013:import java.io.Writer;
014:import java.util.Enumeration;
015:import anvil.java.util.BindingEnumeration;
016:import anvil.core.Any;
017:import anvil.core.AnyAbstractClass;
018:import anvil.core.Register;
019:import anvil.core.Serializer;
020:import anvil.core.Unserializer;
021:import anvil.core.UnserializationException;
022:import anvil.script.Context;
023:import anvil.script.Type;
024:import anvil.script.CompilableFunction;
025:import anvil.script.Function;
026:import anvil.script.FunctionDispatcher;
027:import anvil.script.Scope;
028:import anvil.script.Module;
029:import anvil.script.VariableType;
030:
031:/// @class Scope
032:/// Scope is a wrapper for modules and namespaces.
033:/// It provides the contained entities with
034:/// attributes or references.
035:
036:/**
037: * class AnyScope
038: *
039: * @author: Jani Lehtimäki
040: */
041:public class AnyScope extends AnyAbstractClass
042:{
043:
044: public static final anvil.script.compiler.NativeClass __class__ =
045: new anvil.script.compiler.NativeClass("Scope", AnyScope.class,
046: //DOC{{
047: ""+
048: " @class Scope\n" +
049: " Scope is a wrapper for modules and namespaces.\n" +
050: " It provides the contained entities with\n" +
051: " attributes or references.\n"
052: //}}DOC
053: );
054: static {
055: RuntimeModule.class.getName();
056: }
057:
058:
059: private Scope _scope;
060:
061:
062: public AnyScope(Scope scope)
063: {
064: _scope = scope;
065: }
066:
067:
068: public final anvil.script.ClassType classOf()
069: {
070: return __class__;
071: }
072:
073:
074: public final Type type()
075: {
076: return _scope;
077: }
078:
079:
080: public String toString()
081: {
082: return _scope.toString();
083: }
084:
085:
086: public Writer toAnvil(Writer writer) throws IOException
087: {
088: writer.write(_scope.getName());
089: return writer;
090: }
091:
092:
093: public Object toObject()
094: {
095: return _scope;
096: }
097:
098: private VariableType getVariable(String name)
099: {
100: Type type = _scope.lookupDeclaration(name);
101: if (type instanceof VariableType) {
102: return (VariableType)type;
103: }
104: return null;
105: }
106:
107:
108: public Any getAttribute(Context context, String attribute)
109: {
110: VariableType var = getVariable(attribute);
111: if (var != null) {
112: return var.getValue();
113: }
114: return UNDEFINED;
115: }
116:
117:
118: public Any setAttribute(Context context, String attribute, Any value)
119: {
120: VariableType var = getVariable(attribute);
121: if (var != null) {
122: var.setValue(value);
123: }
124: return value;
125: }
126:
127:
128: public Any checkAttribute(Context context, String attribute)
129: {
130: VariableType var = getVariable(attribute);
131: if (var != null) {
132: return var.getValue();
133: }
134: return UNDEFINED;
135: }
136:
137:
138: public boolean deleteAttribute(Context context, String attribute)
139: {
140: return false;
141: }
142:
143:
144: public Any getReference(Context context, Any index)
145: {
146: return getAttribute(context, index.toString());
147: }
148:
149:
150: public Any setReference(Context context, Any index, Any value)
151: {
152: return setAttribute(context, index.toString(), value);
153: }
154:
155:
156: public Any setReference(Context context, Any value)
157: {
158: return this ;
159: }
160:
161:
162: public Any checkReference(Context context, Any index)
163: {
164: return checkAttribute(context, index.toString());
165: }
166:
167:
168: public boolean deleteReference(Context context, Any index)
169: {
170: return false;
171: }
172:
173:
174: public BindingEnumeration enumeration()
175: {
176: return new VariableFilter(_scope.getDeclarations());
177: }
178:
179:
180: public static final class VariableFilter implements BindingEnumeration
181: {
182: private Enumeration _enum;
183: private boolean _hasNext;
184: private VariableType _nextElement;
185:
186: public VariableFilter(Enumeration enum)
187: {
188: _enum = enum;
189: forward();
190: }
191:
192: private void forward()
193: {
194: Enumeration enum = _enum;
195: while(enum.hasMoreElements()) {
196: Object elem = enum.nextElement();
197: if (elem instanceof VariableType) {
198: _hasNext = true;
199: _nextElement = (VariableType)elem;
200: return;
201: }
202: }
203: _hasNext = false;
204: }
205:
206: public boolean hasMoreElements()
207: {
208: return _hasNext;
209: }
210:
211: public Object nextKey()
212: {
213: return _nextElement.getName();
214: }
215:
216: public Object nextElement()
217: {
218: Any value = _nextElement.getValue();
219: forward();
220: return value;
221: }
222:
223: }
224:
225:
226: public final void serialize(Serializer serializer) throws IOException
227: {
228: if (serializer.register(this )) {
229: return;
230: }
231: serializer.write('M');
232: AnyType.serializeType(serializer, _scope);
233: }
234:
235:
236: public static final Any unserialize(Unserializer unserializer) throws UnserializationException
237: {
238: Type type = AnyType.unserializeType(unserializer);
239: if (type instanceof Scope) {
240: AnyScope scope = new AnyScope((Scope)type);
241: unserializer.register(scope);
242: return scope;
243: }
244: throw new UnserializationException();
245: }
246:
247:
248: public boolean has(String methodName)
249: {
250: Type type = _scope.lookupDeclaration(methodName);
251: return ((type != null) && (type.getType() == Type.FUNCTION));
252: }
253:
254:
255: protected FunctionDispatcher getDispatcherFor(Context context, String name)
256: {
257: Type type = _scope.lookupDeclaration(name);
258: if ((type != null) && (type.getType() == Type.FUNCTION)) {
259: return ((CompilableFunction)type).getDispatcher(context);
260: } else {
261: throw context.NoSuchMethod(_scope.getName() + '.' + name);
262: }
263: }
264:
265: protected FunctionDispatcher getDispatcherFor(Context context, int index)
266: {
267: String name = Register.getNameOf(index);
268: Type type = _scope.lookupDeclaration(name);
269: if ((type != null) && (type.getType() == Type.FUNCTION)) {
270: return ((CompilableFunction)type).getDispatcher(context);
271: } else {
272: throw context.NoSuchMethod(_scope.getName() + '.' + name);
273: }
274: }
275:
276:
277: public Any invoke(Context context, int methodIndex, Any[] parameters)
278: {
279: return getDispatcherFor(context, methodIndex).execute(context, null, parameters);
280: }
281:
282: public Any invoke(Context context, int methodIndex)
283: {
284: return getDispatcherFor(context, methodIndex).execute(context, null);
285: }
286:
287: public Any invoke(Context context, int methodIndex, Any param1)
288: {
289: return getDispatcherFor(context, methodIndex).execute(context, null, param1);
290: }
291:
292: public Any invoke(Context context, int methodIndex, Any param1, Any param2)
293: {
294: return getDispatcherFor(context, methodIndex).execute(context, null, param1, param2);
295: }
296:
297: public Any invoke(Context context, int methodIndex, Any param1, Any param2, Any param3)
298: {
299: return getDispatcherFor(context, methodIndex).execute(context, null, param1, param2, param3);
300: }
301:
302: public Any invoke(Context context, int methodIndex, Any param1, Any param2, Any param3, Any param4)
303: {
304: return getDispatcherFor(context, methodIndex).execute(context, null, param1, param2, param3, param4);
305: }
306:
307:
308: public Any invoke(Context context, String methodName, Any[] parameters)
309: {
310: return getDispatcherFor(context, methodName).execute(context, null, parameters);
311: }
312:
313: public Any invoke(Context context, String methodName)
314: {
315: return getDispatcherFor(context, methodName).execute(context, null);
316: }
317:
318: public Any invoke(Context context, String methodName, Any param1)
319: {
320: return getDispatcherFor(context, methodName).execute(context, null, param1);
321: }
322:
323: public Any invoke(Context context, String methodName, Any param1, Any param2)
324: {
325: return getDispatcherFor(context, methodName).execute(context, null, param1, param2);
326: }
327:
328: public Any invoke(Context context, String methodName, Any param1, Any param2, Any param3)
329: {
330: return getDispatcherFor(context, methodName).execute(context, null, param1, param2, param3);
331: }
332:
333: public Any invoke(Context context, String methodName, Any param1, Any param2, Any param3, Any param4)
334: {
335: return getDispatcherFor(context, methodName).execute(context, null, param1, param2, param3, param4);
336: }
337:
338:
339:
340:
341:}
|