Class ScriptObjectMirror

java.lang.Object
org.openjdk.nashorn.api.scripting.AbstractJSObject
org.openjdk.nashorn.api.scripting.ScriptObjectMirror
All Implemented Interfaces:
Map<String,Object>, Bindings, JSObject

public final class ScriptObjectMirror extends AbstractJSObject implements Bindings
Mirror object that wraps a given Nashorn Script object.
Since:
1.8u40
  • Method Details

    • equals

      public boolean equals(Object other)
      Specified by:
      equals in interface Map<String,Object>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<String,Object>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • call

      public Object call(Object thiz, Object... args)
      Description copied from interface: JSObject
      Call this object as a JavaScript function. This is equivalent to 'func.apply(thiz, args)' in JavaScript.
      Specified by:
      call in interface JSObject
      Overrides:
      call in class AbstractJSObject
      Parameters:
      thiz - 'this' object to be passed to the function. This may be null.
      args - arguments to method
      Returns:
      result of call
    • newObject

      public Object newObject(Object... args)
      Description copied from interface: JSObject
      Call this 'constructor' JavaScript function to create a new object. This is equivalent to 'new func(arg1, arg2...)' in JavaScript.
      Specified by:
      newObject in interface JSObject
      Overrides:
      newObject in class AbstractJSObject
      Parameters:
      args - arguments to method
      Returns:
      result of constructor call
    • eval

      public Object eval(String s)
      Description copied from interface: JSObject
      Evaluate a JavaScript expression.
      Specified by:
      eval in interface JSObject
      Overrides:
      eval in class AbstractJSObject
      Parameters:
      s - JavaScript expression to evaluate
      Returns:
      evaluation result
    • callMember

      public Object callMember(String functionName, Object... args)
      Call member function
      Parameters:
      functionName - function name
      args - arguments
      Returns:
      return value of function
    • getMember

      public Object getMember(String name)
      Description copied from interface: JSObject
      Retrieves a named member of this JavaScript object.
      Specified by:
      getMember in interface JSObject
      Overrides:
      getMember in class AbstractJSObject
      Parameters:
      name - of member
      Returns:
      member
    • getSlot

      public Object getSlot(int index)
      Description copied from interface: JSObject
      Retrieves an indexed member of this JavaScript object.
      Specified by:
      getSlot in interface JSObject
      Overrides:
      getSlot in class AbstractJSObject
      Parameters:
      index - index slot to retrieve
      Returns:
      member
    • hasMember

      public boolean hasMember(String name)
      Description copied from interface: JSObject
      Does this object have a named member?
      Specified by:
      hasMember in interface JSObject
      Overrides:
      hasMember in class AbstractJSObject
      Parameters:
      name - name of member
      Returns:
      true if this object has a member of the given name
    • hasSlot

      public boolean hasSlot(int slot)
      Description copied from interface: JSObject
      Does this object have a indexed property?
      Specified by:
      hasSlot in interface JSObject
      Overrides:
      hasSlot in class AbstractJSObject
      Parameters:
      slot - index to check
      Returns:
      true if this object has a slot
    • removeMember

      public void removeMember(String name)
      Description copied from interface: JSObject
      Remove a named member from this JavaScript object
      Specified by:
      removeMember in interface JSObject
      Overrides:
      removeMember in class AbstractJSObject
      Parameters:
      name - name of the member
    • setMember

      public void setMember(String name, Object value)
      Description copied from interface: JSObject
      Set a named member in this JavaScript object
      Specified by:
      setMember in interface JSObject
      Overrides:
      setMember in class AbstractJSObject
      Parameters:
      name - name of the member
      value - value of the member
    • setSlot

      public void setSlot(int index, Object value)
      Description copied from interface: JSObject
      Set an indexed member in this JavaScript object
      Specified by:
      setSlot in interface JSObject
      Overrides:
      setSlot in class AbstractJSObject
      Parameters:
      index - index of the member slot
      value - value of the member
    • setIndexedPropertiesToExternalArrayData

      public void setIndexedPropertiesToExternalArrayData(ByteBuffer buf)
      Nashorn extension: setIndexedPropertiesToExternalArrayData. set indexed properties be exposed from a given nio ByteBuffer.
      Parameters:
      buf - external buffer - should be a nio ByteBuffer
    • isInstance

      public boolean isInstance(Object instance)
      Description copied from interface: JSObject
      Checking whether the given object is an instance of 'this' object.
      Specified by:
      isInstance in interface JSObject
      Overrides:
      isInstance in class AbstractJSObject
      Parameters:
      instance - instance to check
      Returns:
      true if the given 'instance' is an instance of this 'function' object
    • getClassName

      public String getClassName()
      Description copied from interface: JSObject
      ECMA [[Class]] property
      Specified by:
      getClassName in interface JSObject
      Overrides:
      getClassName in class AbstractJSObject
      Returns:
      ECMA [[Class]] property value of this object
    • isFunction

      public boolean isFunction()
      Description copied from interface: JSObject
      Is this a function object?
      Specified by:
      isFunction in interface JSObject
      Overrides:
      isFunction in class AbstractJSObject
      Returns:
      if this mirror wraps a ECMAScript function instance
    • isStrictFunction

      public boolean isStrictFunction()
      Description copied from interface: JSObject
      Is this a 'use strict' function object?
      Specified by:
      isStrictFunction in interface JSObject
      Overrides:
      isStrictFunction in class AbstractJSObject
      Returns:
      true if this mirror represents a ECMAScript 'use strict' function
    • isArray

      public boolean isArray()
      Description copied from interface: JSObject
      Is this an array object?
      Specified by:
      isArray in interface JSObject
      Overrides:
      isArray in class AbstractJSObject
      Returns:
      if this mirror wraps a ECMAScript array object
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,Object>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Bindings
      Specified by:
      containsKey in interface Map<String,Object>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<String,Object>
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Specified by:
      entrySet in interface Map<String,Object>
    • get

      public Object get(Object key)
      Specified by:
      get in interface Bindings
      Specified by:
      get in interface Map<String,Object>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,Object>
    • keySet

      public Set<String> keySet()
      Description copied from interface: JSObject
      Returns the set of all property names of this object.
      Specified by:
      keySet in interface JSObject
      Specified by:
      keySet in interface Map<String,Object>
      Overrides:
      keySet in class AbstractJSObject
      Returns:
      set of property names
    • put

      public Object put(String key, Object value)
      Specified by:
      put in interface Bindings
      Specified by:
      put in interface Map<String,Object>
    • putAll

      public void putAll(Map<? extends String,?> map)
      Specified by:
      putAll in interface Bindings
      Specified by:
      putAll in interface Map<String,Object>
    • remove

      public Object remove(Object key)
      Specified by:
      remove in interface Bindings
      Specified by:
      remove in interface Map<String,Object>
    • delete

      public boolean delete(Object key)
      Delete a property from this object.
      Parameters:
      key - the property to be deleted
      Returns:
      if the delete was successful or not
    • size

      public int size()
      Specified by:
      size in interface Map<String,Object>
    • values

      public Collection<Object> values()
      Description copied from interface: JSObject
      Returns the set of all property values of this object.
      Specified by:
      values in interface JSObject
      Specified by:
      values in interface Map<String,Object>
      Overrides:
      values in class AbstractJSObject
      Returns:
      set of property values.
    • getProto

      public Object getProto()
      Return the __proto__ of this object.
      Returns:
      __proto__ object.
    • setProto

      public void setProto(Object proto)
      Set the __proto__ of this object.
      Parameters:
      proto - new proto for this object
    • getOwnPropertyDescriptor

      public Object getOwnPropertyDescriptor(String key)
      ECMA 8.12.1 [[GetOwnProperty]] (P)
      Parameters:
      key - property key
      Returns:
      Returns the Property Descriptor of the named own property of this object, or undefined if absent.
    • getOwnKeys

      public String[] getOwnKeys(boolean all)
      return an array of own property keys associated with the object.
      Parameters:
      all - True if to include non-enumerable keys.
      Returns:
      Array of keys.
    • preventExtensions

      public ScriptObjectMirror preventExtensions()
      Flag this script object as non extensible
      Returns:
      the object after being made non extensible
    • isExtensible

      public boolean isExtensible()
      Check if this script object is extensible
      Returns:
      true if extensible
    • seal

      public ScriptObjectMirror seal()
      ECMAScript 15.2.3.8 - seal implementation
      Returns:
      the sealed script object
    • isSealed

      public boolean isSealed()
      Check whether this script object is sealed
      Returns:
      true if sealed
    • freeze

      public ScriptObjectMirror freeze()
      ECMA 15.2.39 - freeze implementation. Freeze this script object
      Returns:
      the frozen script object
    • isFrozen

      public boolean isFrozen()
      Check whether this script object is frozen
      Returns:
      true if frozen
    • isUndefined

      public static boolean isUndefined(Object obj)
      Utility to check if given object is ECMAScript undefined value
      Parameters:
      obj - object to check
      Returns:
      true if 'obj' is ECMAScript undefined value
    • to

      public <T> T to(Class<T> type)
      Utility to convert this script object to the given type.
      Type Parameters:
      T - destination type to convert to
      Parameters:
      type - destination type to convert to
      Returns:
      converted object
    • wrap

      public static Object wrap(Object obj, Object homeGlobal)
      Make a script object mirror on given object if needed.
      Parameters:
      obj - object to be wrapped/converted
      homeGlobal - global to which this object belongs.
      Returns:
      wrapped/converted object
    • wrapAsJSONCompatible

      public static Object wrapAsJSONCompatible(Object obj, Object homeGlobal)
      Make a script object mirror on given object if needed. The created wrapper will implement the Java List interface if obj is a JavaScript Array object; this is compatible with Java JSON libraries expectations. Arrays retrieved through its properties (transitively) will also implement the list interface.
      Parameters:
      obj - object to be wrapped/converted
      homeGlobal - global to which this object belongs.
      Returns:
      wrapped/converted object
    • unwrap

      public static Object unwrap(Object obj, Object homeGlobal)
      Unwrap a script object mirror if needed.
      Parameters:
      obj - object to be unwrapped
      homeGlobal - global to which this object belongs
      Returns:
      unwrapped object
    • wrapArray

      public static Object[] wrapArray(Object[] args, Object homeGlobal)
      Wrap an array of object to script object mirrors if needed.
      Parameters:
      args - array to be unwrapped
      homeGlobal - global to which this object belongs
      Returns:
      wrapped array
    • unwrapArray

      public static Object[] unwrapArray(Object[] args, Object homeGlobal)
      Unwrap an array of script object mirrors if needed.
      Parameters:
      args - array to be unwrapped
      homeGlobal - global to which this object belongs
      Returns:
      unwrapped array
    • identical

      public static boolean identical(Object obj1, Object obj2)
      Are the given objects mirrors to same underlying object?
      Parameters:
      obj1 - first object
      obj2 - second object
      Returns:
      true if obj1 and obj2 are identical script objects or mirrors of it.
    • toNumber

      @Deprecated public double toNumber()
      Deprecated.
      Description copied from class: AbstractJSObject
      Returns this object's numeric value.
      Specified by:
      toNumber in interface JSObject
      Overrides:
      toNumber in class AbstractJSObject
      Returns:
      this object's numeric value.
    • getDefaultValue

      public Object getDefaultValue(Class<?> hint)
      Description copied from interface: JSObject
      Implements this object's [[DefaultValue]] method as per ECMAScript 5.1 section 8.6.2.
      Specified by:
      getDefaultValue in interface JSObject
      Parameters:
      hint - the type hint. Should be either null, Number.class or String.class.
      Returns:
      this object's default value.