/*
* $Id: CompiledModule.java,v 1.2 2002/09/16 08:05:04 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.compiler;
import anvil.core.Any;
import anvil.core.Array;
import anvil.core.AnyBindingEnumeration;
import anvil.core.AnyTuple;
import anvil.core.AnyPattern;
import anvil.core.AnyString;
import anvil.core.BindingEnumerationAdapter;
import anvil.core.ObjectPool;
import anvil.core.runtime.AnyFunction;
import anvil.core.runtime.AnyScope;
import anvil.core.runtime.AnyType;
import anvil.Location;
import anvil.ForgingException;
import anvil.doc.Doc;
import anvil.ErrorListener;
import anvil.script.ClassType;
import anvil.script.ClassRef;
import anvil.script.ConstantVariableType;
import anvil.script.Context;
import anvil.script.Type;
import anvil.script.Function;
import anvil.script.Dependency;
import anvil.script.InterfaceType;
import anvil.script.InterfaceRef;
import anvil.script.Namespace;
import anvil.script.StaticVariableType;
import anvil.script.NamespaceType;
import anvil.script.Scope;
import anvil.script.Module;
import anvil.script.ScriptException;
import anvil.server.Address;
import anvil.java.util.Hashlist;
import anvil.java.util.BindingEnumeration;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.oro.text.regex.MalformedPatternException;
/**
* class CompiledModule
*
* @author: Jani Lehtim�ki
*/
public class CompiledModule extends CompiledScope implements Module
{
protected HashMap _dependencies = new HashMap();
protected Address _address;
protected Location _location;
protected boolean _assert;
protected boolean _initialized = false;
public CompiledModule()
{
super(null, null, "module", null);
}
public void init(Context context)
{
}
public String toString()
{
return "module(" + _name + ")";
}
public synchronized void init(Address address)
{
_address = address;
_location = new Location(address.getURL());
_assert = address.getZone().getAssert();
_name = address.getPathinfo();
Class scriptclass = getClass();
try {
String[] imports = (String[])getstatic(scriptclass, "_imports");
int n = imports.length;
for(int i=0; i<n; ) {
String pathinfo = imports[i++];
String descriptor = imports[i++];
Address imported = getAddress().resolve(pathinfo);
_dependencies.put(imported, new Dependency(imported, descriptor, _location));
}
Field field;
field = scriptclass.getDeclaredField("_module");
field.set(null, this);
field = scriptclass.getDeclaredField("_type");
field.set(null, new AnyScope(this));
} catch (Throwable t) {
anvil.Log.log().error("Initialization of "+scriptclass.getName()+" failed", t);
}
ClassLoader classloader = _address.getZone().getClassLoader();
initializeMembers(classloader);
}
protected void onScript(String name, Doc doc)
{
if (doc != null) {
_document = doc;
}
}
public void destroy()
{
}
public int getVersion()
{
return 0;
}
public final int getType()
{
return MODULE;
}
public boolean getAssert()
{
return _assert;
}
public final Address getAddress()
{
return _address;
}
public final String getPathinfo()
{
return _address.getPathinfo();
}
public Type lookupDeclaration(String name)
{
Type type = (Type)_types.get(name);
if (type == null) {
Namespace ns = _address.getZone().getNamespace(name);
if (ns != null) {
type = new NamespaceBase(this, name, ns, null);
}
}
return type;
}
public int getTypeRef(anvil.codec.ConstantPool pool)
{
return pool.addClass(getClass().getName().replace('.', '/'));
}
public String getDescriptor()
{
return getClass().getName().replace('.', '/');
}
public Iterator getDependencies()
{
return _dependencies.values().iterator();
}
public Dependency getDependency(Address address)
{
return (Dependency)_dependencies.get(address);
}
public static final int switchCase(Hashlist cases, Any value)
{
Integer index = (Integer)cases.get(value);
if (index != null) {
return index.intValue();
} else {
return -1;
}
}
public static final Any toAny(boolean bool)
{
return bool ? Any.TRUE : Any.FALSE;
}
public static final Any b2a(boolean bool)
{
return bool ? Any.TRUE : Any.FALSE;
}
public static final AnyTuple rest(Any[] parameters, int startFrom)
{
int n = (parameters == null) ? 0 : parameters.length;
if (startFrom < n) {
int length = n - startFrom;
Any[] list = new Any[length];
System.arraycopy(parameters, startFrom, list, 0, length);
return new AnyTuple(list);
} else {
return (AnyTuple)Any.EMPTY_TUPLE;
}
}
public static final BindingEnumeration enumerate(Any enum)
{
if (enum instanceof AnyBindingEnumeration) {
return (BindingEnumeration)enum.toObject();
} else {
return new BindingEnumerationAdapter(enum);
}
}
public static final Any enum(Any value)
{
if (value.isEnumeration()) {
Enumeration enum = value.enumeration();
if (enum.hasMoreElements()) {
return Any.create(enum.nextElement());
} else {
return Any.UNDEFINED;
}
} else {
return new anvil.core.AnyBindingEnumeration(value.enumeration());
}
}
public final Any getType(Context context, String name)
{
Type type = (Type)_types.get(name);
if (type != null) {
return new AnyType(type);
}
throw context.NoSuchEntity(name + '@' + _name);
}
}