Commits

Tim Vernum committed abd1e0b

Import from private branch (rev:c43e2aa64684)

Comments (0)

Files changed (11)

modules/lpc/source/java/compiler/us/terebi/lang/lpc/compiler/java/context/CompiledObjectDefinition.java

     public CompiledObjectInstance getMasterInstance();
     public CompiledObjectInstance newInstance(List<? extends LpcValue> arguments);
     public CompiledObjectInstance getInheritableInstance(ObjectInstance forInstance);
-    public CompiledObjectInstance getPrototypeInstance();
+    public CompiledObjectInstance getPrototypeInstance(ObjectInstance forInstance);
     public Map<String, ? extends CompiledMethodDefinition> getMethods();
     public String getBaseName();
 }

modules/lpc/source/java/compiler/us/terebi/lang/lpc/compiler/util/FunctionCallSupport.java

 import us.terebi.lang.lpc.runtime.ArgumentDefinition;
 import us.terebi.lang.lpc.runtime.FunctionSignature;
 import us.terebi.lang.lpc.runtime.MemberDefinition.Modifier;
+import us.terebi.lang.lpc.runtime.jvm.exception.InternalError;
 import us.terebi.lang.lpc.runtime.util.FunctionUtil;
 import us.terebi.util.Range;
 
     {
         List<FunctionReference> functions = _scope.functions().getInheritedMethods(name, argCount);
         functions = filterVirtualFunctions(functions);
-        return extractFunctionReferences(null, name, functions);
+        if (functions.isEmpty())
+        {
+            throw new InternalError("No such function " + name);
+        }
+        return functions.get(functions.size() - 1);
     }
 
     public FunctionReference findFunction(TokenNode node, String scope, String name)

modules/lpc/source/java/compiler/us/terebi/lang/lpc/runtime/jvm/object/AbstractObjectDefinition.java

         MASTER, INSTANCE, INHERIT, PROTOTYPE;
     }
 
-    protected abstract CompiledObjectInstance newInstance(long id, InstanceType type, CompiledObjectInstance forInstance, List< ? extends LpcValue> createArguments);
+    protected abstract CompiledObjectInstance newInstance(long id, InstanceType type, CompiledObjectInstance forInstance,
+            List< ? extends LpcValue> createArguments);
 
     public void instanceDestructed(ObjectInstance instance)
     {
         }
     }
 
-    public CompiledObjectInstance getPrototypeInstance()
+    public CompiledObjectInstance getPrototypeInstance(ObjectInstance forInstance)
     {
-        return newInstance(0, InstanceType.PROTOTYPE, null, Collections.<LpcValue> emptyList());
+        if (forInstance == null || forInstance instanceof CompiledObjectInstance)
+        {
+            return newInstance(0, InstanceType.PROTOTYPE, (CompiledObjectInstance) forInstance, Collections.<LpcValue> emptyList());
+        }
+        throw new IllegalArgumentException("Cannot create instance for non-compiled object " + forInstance);
     }
 
     protected LpcValue create(CompiledObjectInstance instance, List< ? extends LpcValue> createArguments)

modules/lpc/source/java/compiler/us/terebi/lang/lpc/runtime/jvm/object/CompiledDefinition.java

                 _lookup.addInherit(inherited.name(), parent);
             }
         }
-        
+
         Class[] classes = _implementation.getDeclaredClasses();
         for (Class cls : classes)
         {
         return Collections.unmodifiableMap(_inherited);
     }
 
-    protected CompiledObject<T> newInstance(long id, InstanceType type, CompiledObjectInstance forInstance, List< ? extends LpcValue> createArguments)
+    protected CompiledObject<T> newInstance( //
+            long id, InstanceType type, CompiledObjectInstance actualInstance, List< ? extends LpcValue> createArguments)
     {
         if (id != 0)
         {
             getMasterInstance();
         }
 
-        T object = createObject(forInstance == null ? null : forInstance.getImplementingObject());
+        T newImplementation = createObject(actualInstance == null ? null : actualInstance.getImplementingObject());
 
-        Map<String, ObjectInstance> parents = new HashMap<String, ObjectInstance>();
+        Map<String, ObjectInstance> parentMap = new HashMap<String, ObjectInstance>();
 
-        CompiledObject<T> instance = new CompiledObject<T>(this, id, object, parents);
+        CompiledObject<T> newInstance = new CompiledObject<T>(this, id, newImplementation, parentMap);
+        if (actualInstance == null)
+        {
+            actualInstance = newInstance;
+        }
 
-        Field[] fields = object.getClass().getDeclaredFields();
+        Field[] fields = newImplementation.getClass().getDeclaredFields();
         for (Field field : fields)
         {
-            InheritedObject< ? > inherited = setInheritedField(instance, field);
+            InheritedObject< ? > inherited = setInheritedField(newInstance, field, actualInstance);
             if (inherited != null)
             {
-                parents.put(inherited.getName(), inherited.getObjectInstance());
+                parentMap.put(inherited.getName(), inherited.getObjectInstance());
             }
         }
 
-        object.setDefinition(this);
-        object.setInstance(forInstance == null ? instance : forInstance);
+        newImplementation.setDefinition(this);
+        newImplementation.setInstance(actualInstance);
         if (type == InstanceType.MASTER || type == InstanceType.INSTANCE)
         {
-            register(instance);
-            create(instance, createArguments);
+            register(newInstance);
+            create(newInstance, createArguments);
         }
-        return instance;
+        return newInstance;
     }
 
-    private InheritedObject< ? > setInheritedField(CompiledObject<T> instance, Field field)
+    private InheritedObject< ? > setInheritedField(CompiledObject<T> parentInstance, Field field, CompiledObjectInstance actualInstance)
     {
         LpcInherited annotation = field.getAnnotation(LpcInherited.class);
         if (annotation == null)
             return null;
         }
         CompiledObjectDefinition definition = _inherited.get(annotation.name());
-        InheritedObject< ? > inherited = new InheritedObject<Object>(annotation.name(), definition.getImplementationClass(), definition, instance);
+        InheritedObject< ? > inherited = new InheritedObject<Object>( //
+                annotation.name(), definition.getImplementationClass(), definition, actualInstance);
         try
         {
             field.setAccessible(true);
-            field.set(instance.getImplementingObject(), inherited);
+            field.set(parentInstance.getImplementingObject(), inherited);
         }
         catch (Exception e)
         {

modules/lpc/source/java/compiler/us/terebi/lang/lpc/runtime/jvm/object/VirtualObjectDefinition.java

 
     protected CompiledObjectInstance newInstance(long id, InstanceType type, CompiledObjectInstance forInstance, List< ? extends LpcValue> createArguments)
     {
-        CompiledObjectInstance instance = _definition.getPrototypeInstance();
+        CompiledObjectInstance instance = _definition.getPrototypeInstance(forInstance);
         for (FieldDefinition fieldDefinition : _definition.getFields().values())
         {
             LpcValue value = fieldDefinition.getValue(_object);

modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/LpcObject.java

 
 package us.terebi.lang.lpc.runtime.jvm;
 
-
 import us.terebi.lang.lpc.compiler.java.context.ClassFinder;
 import us.terebi.lang.lpc.compiler.java.context.CompiledObjectDefinition;
 import us.terebi.lang.lpc.compiler.java.context.CompiledObjectInstance;
 import us.terebi.lang.lpc.runtime.ClassDefinition;
 import us.terebi.lang.lpc.runtime.LpcType;
 import us.terebi.lang.lpc.runtime.LpcValue;
+import us.terebi.lang.lpc.runtime.MethodDefinition;
 import us.terebi.lang.lpc.runtime.ObjectDefinition;
 import us.terebi.lang.lpc.runtime.jvm.exception.InternalError;
 import us.terebi.lang.lpc.runtime.jvm.support.CallableSupport;
 
     private Callable findMethod(String name, ObjectDefinition object)
     {
-        return CallableSupport.findMethod(name, object, getObjectInstance());
+        CompiledObjectInstance instance = getObjectInstance();
+        MethodDefinition method = CallableSupport.findMethod(name, object, instance);
+        if (method == null)
+        {
+            return null;
+        }
+        return method.getFunction(instance);
     }
 
 }

modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/efun/CallOtherEfun.java

 import us.terebi.lang.lpc.runtime.FunctionSignature;
 import us.terebi.lang.lpc.runtime.LpcType;
 import us.terebi.lang.lpc.runtime.LpcValue;
+import us.terebi.lang.lpc.runtime.MethodDefinition;
 import us.terebi.lang.lpc.runtime.ObjectDefinition;
 import us.terebi.lang.lpc.runtime.ObjectInstance;
 import us.terebi.lang.lpc.runtime.jvm.context.CallStack;
 
     public static LpcValue callOther(ObjectInstance target, String name, List< ? extends LpcValue> args)
     {
-        Callable method = CallableSupport.findMethod(name, target.getDefinition(), target);
+        MethodDefinition method = CallableSupport.findMethod(name, target.getDefinition(), target);
         if (method == null)
         {
             return NilValue.INSTANCE;
         stack.pushFrame(CallStack.Origin.CALL_OTHER, target);
         try
         {
-            return method.execute(args);
+            return method.execute(target, args);
         }
         finally
         {

modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/efun/MapArrayEfun.java

 
         LpcValue func = arguments.get(1);
 
+        List< ? extends LpcValue> args = arguments.get(2).asList();
+
+        Callable callable;
         if (isFunction(func))
         {
-            Callable callable = func.asCallable();
-            List< ? extends LpcValue> args = arguments.get(2).asList();
-            List<LpcValue> result = map_array(list, callable, args);
-            return new ArrayValue(Types.arrayOf(MiscSupport.commonType(result)), result);
+            callable = func.asCallable();
         }
-        // @TODO
-        throw new UnsupportedOperationException(getName() + "(array," + func.getActualType() + ", ..) - Not implemented");
+        else if (MiscSupport.isString(func))
+        {
+            callable = this.getFunctionReference(func.asString(), getArgument(args, 0).asObject());
+            args = args.subList(1, args.size());
+        }
+        else
+        {
+            throw new UnsupportedOperationException(getName() + "(array," + func.getActualType() + ", ..) - Not implemented");
+        }
+        List<LpcValue> result = map_array(list, callable, args);
+        return new ArrayValue(Types.arrayOf(MiscSupport.commonType(result)), result);
     }
 
     private List<LpcValue> map_array(List<LpcValue> list, Callable function, List< ? extends LpcValue> additionalArguments)

modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/support/CallableSupport.java

         }
     }
 
-    public static Callable findMethod(String name, ObjectDefinition object, ObjectInstance instance)
+    public static MethodDefinition findMethod(String name, ObjectDefinition object, ObjectInstance instance)
     {
         MethodDefinition method = object.getMethods().get(name);
         if (method != null)
         {
-            return method.getFunction(instance);
+            return method;
         }
-        List<Callable> match = new ArrayList<Callable>();
+        List<MethodDefinition> match = new ArrayList<MethodDefinition>();
         Collection< ? extends ObjectDefinition> inherited = object.getInheritedObjects().values();
         for (ObjectDefinition parent : inherited)
         {
-            Callable callable = findMethod(name, parent, instance);
-            if (callable != null)
+            method = findMethod(name, parent, instance);
+            if (method != null)
             {
-                match.add(callable);
+                match.add(method);
             }
         }
         if (match.isEmpty())

modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/util/BoundMethod.java

 import us.terebi.lang.lpc.runtime.MethodDefinition;
 import us.terebi.lang.lpc.runtime.ObjectInstance;
 import us.terebi.lang.lpc.runtime.jvm.exception.LpcRuntimeException;
+import us.terebi.lang.lpc.runtime.jvm.support.CallableSupport;
 
 /**
  * 
 
     private static MethodDefinition findMethod(String name, ObjectInstance instance)
     {
-        MethodDefinition method = instance.getDefinition().getMethods().get(name);
+        MethodDefinition method = CallableSupport.findMethod(name, instance.getDefinition(), instance);
         if (method == null)
         {
             throw new LpcRuntimeException("No such method " + name + " in " + instance);

modules/plugin.compat/build.xml

     <pathelement location="../net/output/ant/classes" />
     <pathelement location="../engine/output/ant/classes" />
     <fileset dir="../engine/lib/server" />
-    <fileset dir="./lib/" />
   </path>
 
   <target name="compile.plugin">