scriptableobject.java

来自「主要的怎么样结合java 和 javascript!」· Java 代码 · 共 1,766 行 · 第 1/5 页

JAVA
1,766
字号
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (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.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are * Copyright (C) 1997-1999 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * Norris Boyd * Igor Bukanov * Roger Lawrence * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (the "GPL"), in which case the * provisions of the GPL are applicable instead of those above. * If you wish to allow use of your version of this file only * under the terms of the GPL and not to allow others to use your * version of this file under the NPL, indicate your decision by * deleting the provisions above and replace them with the notice * and other provisions required by the GPL.  If you do not delete * the provisions above, a recipient may use your version of this * file under either the NPL or the GPL. */// API classpackage org.mozilla.javascript;import java.lang.reflect.*;import java.util.Hashtable;import java.io.*;import org.mozilla.javascript.debug.DebuggableObject;/** * This is the default implementation of the Scriptable interface. This * class provides convenient default behavior that makes it easier to * define host objects. * <p> * Various properties and methods of JavaScript objects can be conveniently * defined using methods of ScriptableObject. * <p> * Classes extending ScriptableObject must define the getClassName method. * * @see org.mozilla.javascript.Scriptable * @author Norris Boyd */public abstract class ScriptableObject implements Scriptable, Serializable,                                                  DebuggableObject{    /**     * The empty property attribute.     *     * Used by getAttributes() and setAttributes().     *     * @see org.mozilla.javascript.ScriptableObject#getAttributes     * @see org.mozilla.javascript.ScriptableObject#setAttributes     */    public static final int EMPTY =     0x00;    /**     * Property attribute indicating assignment to this property is ignored.     *     * @see org.mozilla.javascript.ScriptableObject#put     * @see org.mozilla.javascript.ScriptableObject#getAttributes     * @see org.mozilla.javascript.ScriptableObject#setAttributes     */    public static final int READONLY =  0x01;    /**     * Property attribute indicating property is not enumerated.     *     * Only enumerated properties will be returned by getIds().     *     * @see org.mozilla.javascript.ScriptableObject#getIds     * @see org.mozilla.javascript.ScriptableObject#getAttributes     * @see org.mozilla.javascript.ScriptableObject#setAttributes     */    public static final int DONTENUM =  0x02;    /**     * Property attribute indicating property cannot be deleted.     *     * @see org.mozilla.javascript.ScriptableObject#delete     * @see org.mozilla.javascript.ScriptableObject#getAttributes     * @see org.mozilla.javascript.ScriptableObject#setAttributes     */    public static final int PERMANENT = 0x04;    static void checkValidAttributes(int attributes)    {        final int mask = READONLY | DONTENUM | PERMANENT;        if ((attributes & ~mask) != 0) {            throw new IllegalArgumentException(String.valueOf(attributes));        }    }    public ScriptableObject()    {    }    public ScriptableObject(Scriptable scope, Scriptable prototype)    {        if (scope == null)            throw new IllegalArgumentException();        parentScopeObject = scope;        prototypeObject = prototype;    }    /**     * Return the name of the class.     *     * This is typically the same name as the constructor.     * Classes extending ScriptableObject must implement this abstract     * method.     */    public abstract String getClassName();    /**     * Returns true if the named property is defined.     *     * @param name the name of the property     * @param start the object in which the lookup began     * @return true if and only if the property was found in the object     */    public boolean has(String name, Scriptable start)    {        return null != getNamedSlot(name);    }    /**     * Returns true if the property index is defined.     *     * @param index the numeric index for the property     * @param start the object in which the lookup began     * @return true if and only if the property was found in the object     */    public boolean has(int index, Scriptable start)    {        return null != getSlot(null, index);    }    /**     * Returns the value of the named property or NOT_FOUND.     *     * If the property was created using defineProperty, the     * appropriate getter method is called.     *     * @param name the name of the property     * @param start the object in which the lookup began     * @return the value of the property (may be null), or NOT_FOUND     */    public Object get(String name, Scriptable start)    {        Slot slot = getNamedSlot(name);        if (slot == null) {            return Scriptable.NOT_FOUND;        }        if (slot instanceof GetterSlot) {            GetterSlot gslot = (GetterSlot)slot;            if (gslot.getter != null) {                return getByGetter(gslot, start);            }        }        return slot.value;    }    /**     * Returns the value of the indexed property or NOT_FOUND.     *     * @param index the numeric index for the property     * @param start the object in which the lookup began     * @return the value of the property (may be null), or NOT_FOUND     */    public Object get(int index, Scriptable start)    {        Slot slot = getSlot(null, index);        if (slot == null) {            return Scriptable.NOT_FOUND;        }        return slot.value;    }    /**     * Sets the value of the named property, creating it if need be.     *     * If the property was created using defineProperty, the     * appropriate setter method is called. <p>     *     * If the property's attributes include READONLY, no action is     * taken.     * This method will actually set the property in the start     * object.     *     * @param name the name of the property     * @param start the object whose property is being set     * @param value value to set the property to     */    public void put(String name, Scriptable start, Object value)    {        Slot slot = lastAccess; // Get local copy        if (name != slot.stringKey || slot.wasDeleted != 0) {            int hash = name.hashCode();            slot = getSlot(name, hash);            if (slot == null) {                if (start != this) {                    start.put(name, start, value);                    return;                }                slot = addSlot(name, hash, null);            }            // Note: cache is not updated in put        }        if (start == this && isSealed()) {            throw Context.reportRuntimeError1("msg.modify.sealed", name);        }        if ((slot.attributes & ScriptableObject.READONLY) != 0) {            return;        }        if (slot instanceof GetterSlot) {            GetterSlot gslot = (GetterSlot)slot;            if (gslot.setter != null) {                setBySetter(gslot, start, value);            }            return;        }        if (this == start) {            slot.value = value;        } else {            start.put(name, start, value);        }    }    /**     * Sets the value of the indexed property, creating it if need be.     *     * @param index the numeric index for the property     * @param start the object whose property is being set     * @param value value to set the property to     */    public void put(int index, Scriptable start, Object value)    {        Slot slot = getSlot(null, index);        if (slot == null) {            if (start != this) {                start.put(index, start, value);                return;            }            slot = addSlot(null, index, null);        }        if (start == this && isSealed()) {            throw Context.reportRuntimeError1("msg.modify.sealed",                                              Integer.toString(index));        }        if ((slot.attributes & ScriptableObject.READONLY) != 0) {            return;        }        if (this == start) {            slot.value = value;        } else {            start.put(index, start, value);        }    }    /**     * Removes a named property from the object.     *     * If the property is not found, or it has the PERMANENT attribute,     * no action is taken.     *     * @param name the name of the property     */    public void delete(String name) {        removeSlot(name, name.hashCode());    }    /**     * Removes the indexed property from the object.     *     * If the property is not found, or it has the PERMANENT attribute,     * no action is taken.     *     * @param index the numeric index for the property     */    public void delete(int index) {        removeSlot(null, index);    }    /**     * @deprecated Use {@link #getAttributes(String name)}. The engine always     * ignored the start argument.     */    public final int getAttributes(String name, Scriptable start)    {        return getAttributes(name);    }    /**     * @deprecated Use {@link #getAttributes(int index)}. The engine always     * ignored the start argument.     */    public final int getAttributes(int index, Scriptable start)    {        return getAttributes(index);    }    /**     * @deprecated Use {@link #setAttributes(String name, int attributes)}.     * The engine always ignored the start argument.     */    public final void setAttributes(String name, Scriptable start,                                    int attributes)    {        setAttributes(name, attributes);    }    /**     * @deprecated Use {@link #setAttributes(int index, int attributes)}.     * The engine always ignored the start argument.     */    public void setAttributes(int index, Scriptable start,                              int attributes)    {        setAttributes(index, attributes);    }    /**     * Get the attributes of a named property.     *     * The property is specified by <code>name</code>     * as defined for <code>has</code>.<p>     *     * @param name the identifier for the property     * @return the bitset of attributes     * @exception EvaluatorException if the named property is not found     * @see org.mozilla.javascript.ScriptableObject#has     * @see org.mozilla.javascript.ScriptableObject#READONLY     * @see org.mozilla.javascript.ScriptableObject#DONTENUM

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?