/*
 *  Copyright 2006 Hannes Wallnoefer <hannes@helma.at>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.helma.util;

import org.mozilla.javascript.*;

import java.util.List;
import java.util.ArrayList;

/**
 * ScriptableList is a wrapper for java.util.List instances that allows developers 
 * to interact with them like it was a native JavaScript array.
 * @desc Wraps a Java List into a JavaScript Array
 */
public class ScriptableList extends NativeJavaObject {

    List list;
    static final String CLASSNAME = "ScriptableList";

    // Set up a custom constructor, for this class is somewhere between a host class and
    // a native wrapper, for which no standard constructor class exists
    public static void init(Scriptable scope) throws NoSuchMethodException {
        BaseFunction ctor = new BaseFunction(scope, ScriptableObject.getFunctionPrototype(scope)) {
            public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
                if (args.length != 1) {
                    throw new EvaluatorException("Invalid number of arguments to ScriptableList()");
                }
                return new ScriptableList(scope, args[0]);
            }
        };
        scope.put(CLASSNAME, scope, ctor);
    }

    private ScriptableList(Scriptable scope, Object obj) {
        this.parent = scope;
        if (obj instanceof Wrapper) {
            obj = ((Wrapper) obj).unwrap();
        }
        if (obj instanceof List) {
            this.javaObject = this.list = (List) obj;
        } else if (obj == Undefined.instance) {
            this.javaObject = this.list = new ArrayList();
        } else {
            throw new EvaluatorException("Invalid argument to ScriptableList(): " + obj);
        }
        this.staticType = this.list.getClass();
        initMembers();
    }

    public ScriptableList(Scriptable scope, List list) {
        super(scope, list, list.getClass());
        this.list = list;
    }

    public void delete(int index) {
        if (list != null) {
            try {
                list.remove(index);
            } catch (RuntimeException e) {
                throw Context.throwAsScriptRuntimeEx(e);
            }
        } else {
            super.delete(index);
        }
    }

    public Object get(int index, Scriptable start) {
        if (list == null)
            return super.get(index, start);
        try {
            if (index < 0 || index >= list.size()) {
                return Undefined.instance;
            } else {
                return ScriptUtils.javaToJS(list.get(index), getParentScope());
            }
        } catch (RuntimeException e) {
            throw Context.throwAsScriptRuntimeEx(e);
        }
    }

    public boolean has(int index, Scriptable start) {
        if (list == null)
            return super.has(index, start);
        return index >= 0 && index < list.size();
    }

    public void put(int index, Scriptable start, Object value) {
        if (list != null) {
            try {
                list.set(index, Context.jsToJava(value, ScriptRuntime.ObjectClass));
            } catch (RuntimeException e) {
                Context.throwAsScriptRuntimeEx(e);
            }
        } else {
            super.put(index, start, value);
        }
    }

    public Object get(String name, Scriptable start) {
        if ("length".equals(name) && list != null) {
            return list.size();
        }
        return super.get(name, start);
    }

    public Object[] getIds() {
        if (list == null)
            return super.getIds();
        int size = list.size();
        Integer[] ids = new Integer[size];
        for (int i = 0; i < size; ++i) {
            ids[i] = i;
        }
        return ids;
    }

    public String toString() {
        if (list == null)
            return super.toString();
        return list.toString();
    }

    public Object getDefaultValue(Class typeHint) {
        return toString();
    }

    public Object unwrap() {
        return list;
    }

    public List getList() {
        return list;
    }

    public String getClassName() {
        return CLASSNAME;
    }
}
@@ -1586,55 +1586,59 @@
 on(%22
-Invalid number of arguments to ScriptableList()
+ScriptableList() requires a java.util.List argument
 %22);%0A
@@ -1785,24 +1785,178 @@
 or);%0A    %7D%0A%0A
+    /**%0A     * Create a ScriptableList wrapper around a java.util.List%0A     * @param scope the scope%0A     * @param obj the list, possibly wrapped%0A     */%0A
     private 
@@ -2515,138 +2515,786 @@
     
-%7D%0A%0A    public ScriptableList(Scriptable scope, List list) %7B%0A        super(scope, list, list.getClass());%0A        this.list = list;
+    initPrototype(scope);%0A    %7D%0A%0A%0A    /**%0A     * Create a ScriptableList wrapper around a java.util.List.%0A     * @param scope the scope%0A     * @param list the list instance%0A     */%0A    public ScriptableList(Scriptable scope, List list) %7B%0A        super(scope, list, list.getClass());%0A        this.list = list;%0A        initPrototype(scope);%0A    %7D%0A%0A    /**%0A     * Set the prototype to the Array prototype so we can use array methds such as%0A     * push, pop, shift, slice etc.%0A     * @param scope the global scope for looking up the Array constructor%0A     */%0A    protected void initPrototype(Scriptable scope) %7B%0A        Scriptable arrayProto = ScriptableObject.getClassPrototype(scope, %22Array%22);%0A        if (arrayProto != null) %7B%0A            this.setPrototype(arrayProto);%0A        %7D
 %0A   
@@ -4337,32 +4337,187 @@
           try %7B%0A
+                if (index == list.size()) %7B%0A                    list.add(Context.jsToJava(value, ScriptRuntime.ObjectClass));%0A                %7D else %7B%0A    
                 
@@ -4577,32 +4577,50 @@
 .ObjectClass));%0A
+                %7D%0A
             %7D ca

Allow ScriptableList to grow by appending if index == length, and set the prototype to Array.prototype so most Array methods like push, shift, slice are available.