Package anvil.codec

Examples of anvil.codec.ClassRoom


  };

  public static FunctionDispatcher create(CompilableFunction function, boolean isstatic, ZoneClassLoader loader)
    throws Throwable
  {
    ClassRoom clazz = new ClassRoom();
    ConstantPool pool = clazz.getPool();
    String classname = getNextName();
    clazz.setClassname(classname, null);
    clazz.setSuperClassname("java/lang/Object");
    clazz.addInterface("anvil/script/FunctionDispatcher");
    clazz.setAccessFlags(ACC_PUBLIC|ACC_FINAL);

    Hashlist constants = new Hashlist();

    //generic
    {
      Method method = clazz.createMethod("execute",
       "(Lanvil/script/Context;Lanvil/core/Any;[Lanvil/core/Any;)Lanvil/core/Any;",
       ACC_PUBLIC|ACC_FINAL);
      Code code = method.getCode();
      int l_context = code.addLocal();
      int l_self = code.addLocal();
      int l_parameters = code.addLocal();
      compileDispatch(function, isstatic, clazz, code, pool, l_context, l_self, l_parameters, constants);
    }

    for(int params=0; params<=4; params++) {
   
      Method method = clazz.createMethod("execute", EXECUTE_SIGNATURES[params], ACC_PUBLIC|ACC_FINAL);
      Code code = method.getCode();
      int l_context = code.addLocal();
      int l_self = code.addLocal();
      int l_base = code.addLocal();
      code.addLocals(params);
      compileDispatch(function, isstatic, clazz, code, pool, l_context, l_self, l_base, params, constants);
    }

    createConstructor(clazz, constants);

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    clazz.write(new DataOutputStream(bytestream));
    bytestream.close();

    //FileOutputStream fout = new FileOutputStream("/data/projects/anvil/src/"+classname+".class");
    //bytestream.writeTo(fout);
    //fout.close();
View Full Code Here


    Address address = _envelope.getAddress();
    String pathinfo = address.getPathinfo();
    String classsig = _envelope.getDescriptor();
    TYPE_MODULE = classsig;
   
    ClassRoom clazz = new ClassRoom();
    pushClass(clazz);
    clazz.setClassname(classsig, null);
    clazz.setSuperClassname("anvil/script/compiler/CompiledModule");
    clazz.setAccessFlags(ACC_SUPER|ACC_PUBLIC);
    ConstantPool pool = clazz.getPool();

    _f_const = clazz.createField("_const", "[Lanvil/core/Any;", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);
    _f_text = clazz.createField("_text", "[[B", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);
    _f_switch = clazz.createField("_switch", "[Lanvil/java/util/Hashlist;", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);

    Method _m_const = clazz.createMethod("_const", "()V", ACC_PUBLIC|ACC_STATIC);
    Method _m_text = clazz.createMethod("_text", "()V", ACC_PUBLIC|ACC_STATIC);
    Method _m_switch = clazz.createMethod("_switch", "()V", ACC_PUBLIC|ACC_STATIC);
    Method _m_symbols = clazz.createMethod("_symbols", "()V", ACC_PUBLIC|ACC_STATIC);
    //Method _m_imports = clazz.createMethod("_imports", "()V", ACC_PUBLIC|ACC_STATIC);
   
    code = clazz.getStatic().getCode();
    //code.println("MODULE-START:"+classsig);
    code.invokestatic(_m_const.getIndex());
    code.invokestatic(_m_text.getIndex());
    code.invokestatic(_m_switch.getIndex());
    code.invokestatic(_m_symbols.getIndex());
    ((anvil.script.statements.ModuleStatement)getModule()).compile(this);
    //code.println("MODULE-END:"+classsig);
    code.vreturn();
   
    /* constants */
    code = _m_const.getCode();
    if (_constants.size() > 0) {
      code.iconst(_constants.size());
      code.anewarray(TYPE_ANY);
      Enumeration e = _constants.keys();
      for(int i=0; e.hasMoreElements(); i++) {
        code.dup();
        code.iconst(i);
        ((Any)e.nextElement()).toCode(code);
        code.aastore();
      }
      code.putstatic(_f_const);
    }
    code.vreturn();
   
    /* texts */
    code = _m_text.getCode();
    int getBytes = pool.addMethodRef("anvil/util/Conversions", "getBytes", "(Ljava/lang/String;)[B");
    if (_texts.size() > 0) {
      int size = _texts.size();
      code.iconst(size);
      code.anewarray("[[B", 1);
      Enumeration e = _texts.keys();
      for(int i=0; e.hasMoreElements(); i++) {
        code.dup();
        code.iconst(i);
        code.astring((String)e.nextElement());
        code.invokestatic(getBytes);
        code.aastore();
      }
      code.putstatic(_f_text);
    }
    code.vreturn();
   
    /* switchtables */
    code = _m_switch.getCode();
    int n = _switches.size();
    if (_switches.size() > 0 ) {
      int hashlistclazz = pool.addClass("anvil/java/util/Hashlist");
      int hashlistctor = pool.addMethodRef(hashlistclazz, "<init>", "()V");
      int hashlistadd = pool.addMethodRef(hashlistclazz, "add",
        "(Ljava/lang/Object;Ljava/lang/Object;)Lanvil/java/util/Hashlist;");
      int intclazz = pool.addClass("java/lang/Integer");
      int intclazzctor = pool.addMethodRef(intclazz, "<init>", "(I)V");
      code.iconst(n);
      code.anewarray(hashlistclazz);
      for(int i=0; i<n; i++) {
        code.dup();
        code.iconst(i);
        code.anew(hashlistclazz);
        code.dup();
        code.invokespecial(hashlistctor);
        int c = 0;
        Enumeration keys = (Enumeration)_switches.elementAt(i);
        while(keys.hasMoreElements()) {
          Any key = (Any)keys.nextElement();
          if (key != SwitchStatement.DEFAULT_MARKER) {
            key.toCode(code);
            code.anew(intclazz);
            code.dup();
            code.iconst(c++);
            code.invokespecial(intclazzctor);
            code.invokevirtual(hashlistadd);
          }
        }
        code.aastore();
      }
      code.putstatic(_f_switch);
    }
    code.vreturn();

    /* methodindices */
    code = _m_symbols.getCode();
    if (_symbols.size() > 0) {
      int register = pool.addMethodRef(pool.addClass("anvil/core/Register"),
        "register", "(Ljava/lang/String;)I");
      BindingEnumeration e = _symbols.keysAndElements();
      while(e.hasMoreElements()) {
        int index = ((Integer)e.nextKey()).intValue();
        String name = (String)e.nextElement();
        Field field = clazz.createField("r_"+name, "I", ACC_PUBLIC|ACC_STATIC|ACC_FINAL);
        code.astring(name);
        code.invokestatic(register);
        code.putstatic(field);
      }
    }
    code.vreturn();
   
    /* reflectionfields */
    if (_reflections.size() > 0) {
      BindingEnumeration e = _reflections.keysAndElements();
      while(e.hasMoreElements()) {
        int index = ((Integer)e.nextElement()).intValue();
        clazz.createField("java$"+index, "Lanvil/core/reflect/Reflection;", ACC_PUBLIC|ACC_STATIC);
      }
    }   
   
   
    popClass();

    try {
      clazz.write(_smith);
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
View Full Code Here

  }


  public void compileDescriptor(ByteCompiler context)
  {
    ClassRoom clazz = context.getClassRoom();
    ConstantPool pool = context.getPool();
    Parameter[] params = _params;
    int size = _size;
    int count = size;
    for(int i=0; i<size; i++) {
      if (params[i].value != null) {
        count++;
      }
    }
    Code code = clazz.getStatic().getCode();
    code.iconst(count);
    code.anewarray("java/lang/Object");
    int j = 0;
    for(int i=0; i<size; i++) {
      Parameter param = params[i];
View Full Code Here

 
  public static GeneratorDispatcher createGenerator(CompilableFunction function, ZoneClassLoader loader)
    throws Throwable
  {
    ClassRoom clazz = new ClassRoom();
    ConstantPool pool = clazz.getPool();
    String classname = getNextName();
    clazz.setClassname(classname, null);
    clazz.setSuperClassname("java/lang/Object");
    clazz.addInterface("anvil/script/GeneratorDispatcher");
    clazz.setAccessFlags(ACC_PUBLIC|ACC_FINAL);

    Method method = clazz.createMethod("<init>", "()V", ACC_PUBLIC);
    Code code = method.getCode();
    code.self();
    code.invokespecial(pool.addMethodRef(clazz.getSuperClassIndex(), "<init>", "()V"));
    code.vreturn();

    method = clazz.createMethod("execute",
      "(Lanvil/script/Context;Lanvil/core/AnyClass;Lanvil/script/Generator;)Lanvil/core/Any;",
      ACC_PUBLIC|ACC_FINAL);
    code = method.getCode();
    code.addLocals(4);
   
    boolean ismethod = (function.getType() == Type.METHOD);
    Scope parent = function.getParent();
    int parent_ref = parent.getTypeRef(pool);
    if (ismethod) {
      code.aload(2);
      code.checkcast(parent_ref);
    }
    code.aload(1);
    code.aload(3);
    int method_ref = pool.addMethodRef(
        parent_ref, "h_"+function.getName(), "(Lanvil/script/Context;Lanvil/script/Generator;)Lanvil/core/Any;");
    if (ismethod) {
      code.invokevirtual(method_ref);
    } else {
      code.invokestatic(method_ref);
    }
    code.areturn();

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    clazz.write(new DataOutputStream(bytestream));
    bytestream.close();

    Class cls = loader.define(classname, bytestream.toByteArray());
    return (GeneratorDispatcher)cls.newInstance()
  }
View Full Code Here

  }
 

  public void compile(ByteCompiler context)
  {
    ClassRoom room = context.getClassRoom();
    room.createField("s_"+_name, "Lanvil/core/Any;", context.ACC_PUBLIC|context.ACC_STATIC);
 
View Full Code Here

  }
 

  public void compile(ByteCompiler context)
  {
    ClassRoom clazz = context.getClassRoom().createClass(getDescriptor(), "i_"+_name);
    clazz.setAccessFlags(context.ACC_PUBLIC);
    ConstantPool pool = clazz.getPool();
    context.pushClass(clazz);
    Field typefield1 = clazz.createField("_class", "Lanvil/script/compiler/CompiledInterfaceType;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Field typefield2 = clazz.createField("_type", "Lanvil/core/Any;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    clazz.setSuperClassname(context.TYPE_OBJECT);
    clazz.setAccessFlags(Code.ACC_SUPER|Code.ACC_PUBLIC);

    compileMembers(context, _types.size(), _types.elements());

    Field bases = clazz.createField("_bases", "[Ljava/lang/String;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Code code = clazz.getStatic().getCode();
    context.pushCode(code);

    //code.println("INTERFACE-START:"+getDescriptor());
    code.getstatic(pool.addFieldRef(_parent.getDescriptor(),
      "_members", "[Ljava/lang/Object;"));
View Full Code Here

  }
 

  public void compile(ByteCompiler context)
  {
    ClassRoom clazz = context.getClassRoom();
    Field typefield = clazz.createField("f_"+_name, "Lanvil/script/Function;", ACC_PUBLIC|ACC_STATIC);
    clazz.createField("F_"+_name, "Lanvil/core/Any;", ACC_PUBLIC|ACC_STATIC);
    Method method = clazz.createMethod("f_"+_name, getSignature(), ACC_PUBLIC|ACC_FINAL|ACC_STATIC|(_synchronized?ACC_SYNCHRONIZED:0));
    compileBody(context, method, typefield);
  }
View Full Code Here

  }


  protected static void compileMembers(ByteCompiler context, int size, Enumeration types)
  {
    ClassRoom clazz = context.getClassRoom();
    ConstantPool pool = clazz.getPool();
    Field members = clazz.createField("_members", "[Ljava/lang/Object;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Code code = clazz.getStatic().getCode();
    context.pushCode(code);
    int intclazz = pool.addClass("java/lang/Integer");
    int intclazzctor = pool.addMethodRef(intclazz, "<init>", "(I)V");
    code.iconst(size * 4);
    code.anewarray("java/lang/Object");
View Full Code Here

  }


  public void compile(ByteCompiler context)
  {
    ClassRoom clazz = context.getClassRoom();
    Field typefield = clazz.createField("m_"+_name, "Lanvil/script/Function;", ACC_PUBLIC|ACC_STATIC);
    clazz.createField("M_"+_name, "Lanvil/core/Any;", ACC_PUBLIC|ACC_STATIC);
    Method method = clazz.createMethod("m_"+_name, getSignature(), ACC_PUBLIC|(_synchronized?ACC_SYNCHRONIZED:0));
    compileBody(context, method, typefield);
  }
View Full Code Here

  public void compile(ByteCompiler context)
  {
    _types.put(_constructor.getName(), _constructor);
   
    ClassRoom clazz = context.getClassRoom().createClass(_descriptor, "z_"+_name);
    clazz.setAccessFlags(Code.ACC_PUBLIC);
    ConstantPool pool = clazz.getPool();
    context.pushClass(clazz);
    Field typefield1 = clazz.createField("_class", "Lanvil/script/compiler/CompiledClassType;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Field typefield2 = clazz.createField("_type", "Lanvil/core/Any;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    ClassType base = getBaseClass();
    if (base != Any.__class__) {
      clazz.setSuperClassname(base.getDescriptor());
    } else {
      clazz.setSuperClassname("anvil/core/AnyClass");
    }
    clazz.setAccessFlags(Code.ACC_SUPER|Code.ACC_PUBLIC);

    Field bases = clazz.createField("_bases", "[Ljava/lang/String;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Code code = clazz.getStatic().getCode();
    context.pushCode(code);
   
    code.getstatic(pool.addFieldRef(_parent.getDescriptor(), "_members", "[Ljava/lang/Object;"));
    code.pop();

    compileConstructors(context, typefield1);

    compileMembers(context, _types.size(), _types.elements());
   
    int n = _interfaces.length;
    code.iconst(n);
    code.anewarray("java/lang/String");
    for(int i=0; i<n; i++) {
      InterfaceType interfacetype = _interfaces[i].getInterfaceType();
      code.dup();
      code.iconst(i);
      code.astring(interfacetype.getDescriptor().replace('/', '.'));
      code.aastore();
    }
    code.putstatic(bases);

    Enumeration e = _types.elements();
    while(e.hasMoreElements()) {
      Type type = (Type)e.nextElement();
      if (type.getType() == CLASS) {
        code.getstatic(pool.addFieldRef(type.getTypeRef(pool),
          "_members", "[Ljava/lang/Object;"));
        code.pop();
      }
    }

    code.vreturn();

    context.popCode();

    super.compile(context);

    compileMemberCallback(context);


    MethodStatement method = getMethod("clone");
    if (method != null) {
      compileCallback(context, method, clazz.createMethod(
        "clone", "()Ljava/lang/Object;", Code.ACC_PUBLIC), true, 0, EMPTY, 0, 2);
    } else {
      compileCopyMethod(context, method, false, typefield1);
    }

    method = getMethod("copy");
    if (method != null) {
      compileCallback(context, method, clazz.createMethod(
        "copy", "()Lanvil/core/Any;", Code.ACC_PUBLIC), true, 0, EMPTY, 0, 2);
    } else {
      compileCopyMethod(context, method, true, typefield1);
    }

    method = getMethod("toBoolean");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("toBoolean", "()Z", Code.ACC_PUBLIC), true, 0, EMPTY,
          pool.addMethodRef(context.TYPE_ANY, "toBoolean", "()Z"), 1);
    }
   
    method = getMethod("toString");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("toString", "()Ljava/lang/String;", Code.ACC_PUBLIC), true, 0, EMPTY,
          pool.addMethodRef(context.TYPE_ANY, "toString", "()Ljava/lang/String;"), 2);
    }

    method = getMethod("sizeOf");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("sizeOf", "()I", Code.ACC_PUBLIC), true, 0, EMPTY,
          pool.addMethodRef(context.TYPE_ANY, "toInt", "()I"), 1);
    }

    method = getMethod("hashCode");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("hashCode", "()I", Code.ACC_PUBLIC), true, 0, EMPTY,
          pool.addMethodRef(context.TYPE_ANY, "toInt", "()I"), 1);
    }

    method = getMethod("equals");
    if (method != null) {
      compileEqualsMethod(context, method);
    }

    method = getMethod("compareTo");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("compare", "(Lanvil/core/Any;)I", Code.ACC_PUBLIC), true, 1, CAST_P1_NODE,
          pool.addMethodRef(context.TYPE_ANY, "toInt", "()I"), 1);
    }


    method = getMethod("_get");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("getReference", "(Lanvil/script/Context;Lanvil/core/Any;)Lanvil/core/Any;", Code.ACC_PUBLIC),
          false, 2, P2_NODE, 0, 2);
    }

    method = getMethod("_check");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("checkReference", "(Lanvil/script/Context;Lanvil/core/Any;)Lanvil/core/Any;", Code.ACC_PUBLIC),
          false, 2, P2_NODE, 0, 2);
    }

    method = getMethod("_delete");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("deleteReference", "(Lanvil/script/Context;Lanvil/core/Any;)Z", Code.ACC_PUBLIC),
          false, 2, P2_NODE, pool.addMethodRef(context.TYPE_ANY, "toBoolean", "()Z"), 1);
    }

    method = getMethod("_append");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("setReference", "(Lanvil/script/Context;Lanvil/core/Any;)Lanvil/core/Any;", Code.ACC_PUBLIC),
          false, 2, P2_NODE, 0, 2);
    }

    method = getMethod("_set");
    if (method != null) {
      compileCallback(context, method, clazz.createMethod("setReference",
        "(Lanvil/script/Context;Lanvil/core/Any;Lanvil/core/Any;)Lanvil/core/Any;", Code.ACC_PUBLIC),
        false, 3, P_Q_NODE, 0, 2);
    }

    method = getMethod("_fetch");
    MethodStatement[] methods = getMethodsStartingWith("_fetch_");
    if (method != null || methods != null) {
      Method target = clazz.createMethod("getAttribute", "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/core/Any;", Code.ACC_PUBLIC);
      compileAttributeCallback(context, target, 7, false, methods, EMPTY, method, P_S2A_NODE, false);
    }

    method = getMethod("_defined");
    methods = getMethodsStartingWith("_defined_");
    if (method != null || methods != null) {
      Method target = clazz.createMethod("checkAttribute", "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/core/Any;", Code.ACC_PUBLIC);
      compileAttributeCallback(context, target, 9, false, methods, EMPTY, method, P_S2A_NODE, false);
    }

    method = getMethod("_dispose");
    methods = getMethodsStartingWith("_dispose_");
    if (method != null || methods != null) {
      Method target = clazz.createMethod("deleteAttribute", "(Lanvil/script/Context;Ljava/lang/String;)Z", Code.ACC_PUBLIC);
      compileAttributeCallback(context, target, 9, false, methods, EMPTY, method, P_S2A_NODE, true);
    }

    method = getMethod("_store");
    methods = getMethodsStartingWith("_store_");
    if (method != null || methods != null) {
      Method target = clazz.createMethod("setAttribute", "(Lanvil/script/Context;Ljava/lang/String;Lanvil/core/Any;)Lanvil/core/Any;", Code.ACC_PUBLIC);
      compileAttributeCallback(context, target, 7, true, methods, P3_NODE, method, P_S2A_Q_NODE, false);
    }

    method = getMethod("_enumeration");
    if (method != null) {
      compileCallback(context, method, clazz.createMethod("enumeration",
        "()Lanvil/java/util/BindingEnumeration;", Code.ACC_PUBLIC),
        true, 0, EMPTY, -pool.addMethodRef(context.TYPE_COMPILED_SCRIPT,
        "enumerate", "(Lanvil/core/Any;)Lanvil/java/util/BindingEnumeration;"), 2);
    }

    method = getMethod("_in");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("contains", "(Lanvil/core/Any;)Z", Code.ACC_PUBLIC),
          true, 1, P1_NODE, pool.addMethodRef(context.TYPE_ANY, "toBoolean", "()Z"), 1);
    }

    method = getMethod("_has");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("has", "(Ljava/lang/String;)Z", Code.ACC_PUBLIC),
          true, 1, P1_S2A_NODE, pool.addMethodRef(context.TYPE_ANY, "toBoolean", "()Z"), 1);
    }

    method = getMethod("_invoke");
    if (method != null) {
      compileInvokeMethod(context, clazz, method);
    }

    method = getMethod("_execute");
    if (method != null) {
      compileExecuteMethod(context, clazz, method);
    }

    method = getMethod("_getRef");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("getRef", "()Lanvil/core/Any;", Code.ACC_PUBLIC), true, 0, EMPTY, 0, 2);
    }

    method = getMethod("_setRef");
    if (method != null) {
      compileCallback(context, method,
        clazz.createMethod("setRef", "(Lanvil/core/Any;)V", Code.ACC_PUBLIC), true, 1, P1_NODE, 0, 0);
    }

    compileAux(context);
   
    //compileInvoker(context);
View Full Code Here

TOP

Related Classes of anvil.codec.ClassRoom

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.