- java.lang.Object
-
- org.openjdk.nashorn.api.scripting.AbstractJSObject
-
- org.openjdk.nashorn.api.scripting.ScriptObjectMirror
-
- All Implemented Interfaces:
java.util.Map<java.lang.String,java.lang.Object>
,javax.script.Bindings
,JSObject
public final class ScriptObjectMirror extends AbstractJSObject implements javax.script.Bindings
Mirror object that wraps a given Nashorn Script object.- Since:
- 1.8u40
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
call(java.lang.Object thiz, java.lang.Object... args)
Call this object as a JavaScript function.java.lang.Object
callMember(java.lang.String functionName, java.lang.Object... args)
Call member functionvoid
clear()
boolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
boolean
delete(java.lang.Object key)
Delete a property from this object.java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>
entrySet()
boolean
equals(java.lang.Object other)
java.lang.Object
eval(java.lang.String s)
Evaluate a JavaScript expression.ScriptObjectMirror
freeze()
ECMA 15.2.39 - freeze implementation.java.lang.Object
get(java.lang.Object key)
java.lang.String
getClassName()
ECMA [[Class]] propertyjava.lang.Object
getDefaultValue(java.lang.Class<?> hint)
Implements this object's[[DefaultValue]]
method as per ECMAScript 5.1 section 8.6.2.java.lang.Object
getMember(java.lang.String name)
Retrieves a named member of this JavaScript object.java.lang.String[]
getOwnKeys(boolean all)
return an array of own property keys associated with the object.java.lang.Object
getOwnPropertyDescriptor(java.lang.String key)
ECMA 8.12.1 [[GetOwnProperty]] (P)java.lang.Object
getProto()
Return the __proto__ of this object.java.lang.Object
getSlot(int index)
Retrieves an indexed member of this JavaScript object.int
hashCode()
boolean
hasMember(java.lang.String name)
Does this object have a named member?boolean
hasSlot(int slot)
Does this object have a indexed property?static boolean
identical(java.lang.Object obj1, java.lang.Object obj2)
Are the given objects mirrors to same underlying object?boolean
isArray()
Is this an array object?boolean
isEmpty()
boolean
isExtensible()
Check if this script object is extensibleboolean
isFrozen()
Check whether this script object is frozenboolean
isFunction()
Is this a function object?boolean
isInstance(java.lang.Object instance)
Checking whether the given object is an instance of 'this' object.boolean
isSealed()
Check whether this script object is sealedboolean
isStrictFunction()
Is this a 'use strict' function object?static boolean
isUndefined(java.lang.Object obj)
Utility to check if given object is ECMAScript undefined valuejava.util.Set<java.lang.String>
keySet()
Returns the set of all property names of this object.java.lang.Object
newObject(java.lang.Object... args)
Call this 'constructor' JavaScript function to create a new object.ScriptObjectMirror
preventExtensions()
Flag this script object as non extensiblejava.lang.Object
put(java.lang.String key, java.lang.Object value)
void
putAll(java.util.Map<? extends java.lang.String,?> map)
java.lang.Object
remove(java.lang.Object key)
void
removeMember(java.lang.String name)
Remove a named member from this JavaScript objectScriptObjectMirror
seal()
ECMAScript 15.2.3.8 - seal implementationvoid
setIndexedPropertiesToExternalArrayData(java.nio.ByteBuffer buf)
Nashorn extension: setIndexedPropertiesToExternalArrayData.void
setMember(java.lang.String name, java.lang.Object value)
Set a named member in this JavaScript objectvoid
setProto(java.lang.Object proto)
Set the __proto__ of this object.void
setSlot(int index, java.lang.Object value)
Set an indexed member in this JavaScript objectint
size()
<T> T
to(java.lang.Class<T> type)
Utility to convert this script object to the given type.double
toNumber()
Deprecated.java.lang.String
toString()
static java.lang.Object
unwrap(java.lang.Object obj, java.lang.Object homeGlobal)
Unwrap a script object mirror if needed.static java.lang.Object[]
unwrapArray(java.lang.Object[] args, java.lang.Object homeGlobal)
Unwrap an array of script object mirrors if needed.java.util.Collection<java.lang.Object>
values()
Returns the set of all property values of this object.static java.lang.Object
wrap(java.lang.Object obj, java.lang.Object homeGlobal)
Make a script object mirror on given object if needed.static java.lang.Object[]
wrapArray(java.lang.Object[] args, java.lang.Object homeGlobal)
Wrap an array of object to script object mirrors if needed.static java.lang.Object
wrapAsJSONCompatible(java.lang.Object obj, java.lang.Object homeGlobal)
Make a script object mirror on given object if needed.-
Methods inherited from class org.openjdk.nashorn.api.scripting.AbstractJSObject
getDefaultValue, isInstanceOf
-
-
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object other)
- Specified by:
equals
in interfacejava.util.Map<java.lang.String,java.lang.Object>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Map<java.lang.String,java.lang.Object>
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
call
public java.lang.Object call(java.lang.Object thiz, java.lang.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 interfaceJSObject
- Overrides:
call
in classAbstractJSObject
- Parameters:
thiz
- 'this' object to be passed to the function. This may be null.args
- arguments to method- Returns:
- result of call
-
newObject
public java.lang.Object newObject(java.lang.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 interfaceJSObject
- Overrides:
newObject
in classAbstractJSObject
- Parameters:
args
- arguments to method- Returns:
- result of constructor call
-
eval
public java.lang.Object eval(java.lang.String s)
Description copied from interface:JSObject
Evaluate a JavaScript expression.- Specified by:
eval
in interfaceJSObject
- Overrides:
eval
in classAbstractJSObject
- Parameters:
s
- JavaScript expression to evaluate- Returns:
- evaluation result
-
callMember
public java.lang.Object callMember(java.lang.String functionName, java.lang.Object... args)
Call member function- Parameters:
functionName
- function nameargs
- arguments- Returns:
- return value of function
-
getMember
public java.lang.Object getMember(java.lang.String name)
Description copied from interface:JSObject
Retrieves a named member of this JavaScript object.- Specified by:
getMember
in interfaceJSObject
- Overrides:
getMember
in classAbstractJSObject
- Parameters:
name
- of member- Returns:
- member
-
getSlot
public java.lang.Object getSlot(int index)
Description copied from interface:JSObject
Retrieves an indexed member of this JavaScript object.- Specified by:
getSlot
in interfaceJSObject
- Overrides:
getSlot
in classAbstractJSObject
- Parameters:
index
- index slot to retrieve- Returns:
- member
-
hasMember
public boolean hasMember(java.lang.String name)
Description copied from interface:JSObject
Does this object have a named member?- Specified by:
hasMember
in interfaceJSObject
- Overrides:
hasMember
in classAbstractJSObject
- 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 interfaceJSObject
- Overrides:
hasSlot
in classAbstractJSObject
- Parameters:
slot
- index to check- Returns:
- true if this object has a slot
-
removeMember
public void removeMember(java.lang.String name)
Description copied from interface:JSObject
Remove a named member from this JavaScript object- Specified by:
removeMember
in interfaceJSObject
- Overrides:
removeMember
in classAbstractJSObject
- Parameters:
name
- name of the member
-
setMember
public void setMember(java.lang.String name, java.lang.Object value)
Description copied from interface:JSObject
Set a named member in this JavaScript object- Specified by:
setMember
in interfaceJSObject
- Overrides:
setMember
in classAbstractJSObject
- Parameters:
name
- name of the membervalue
- value of the member
-
setSlot
public void setSlot(int index, java.lang.Object value)
Description copied from interface:JSObject
Set an indexed member in this JavaScript object- Specified by:
setSlot
in interfaceJSObject
- Overrides:
setSlot
in classAbstractJSObject
- Parameters:
index
- index of the member slotvalue
- value of the member
-
setIndexedPropertiesToExternalArrayData
public void setIndexedPropertiesToExternalArrayData(java.nio.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(java.lang.Object instance)
Description copied from interface:JSObject
Checking whether the given object is an instance of 'this' object.- Specified by:
isInstance
in interfaceJSObject
- Overrides:
isInstance
in classAbstractJSObject
- Parameters:
instance
- instance to check- Returns:
- true if the given 'instance' is an instance of this 'function' object
-
getClassName
public java.lang.String getClassName()
Description copied from interface:JSObject
ECMA [[Class]] property- Specified by:
getClassName
in interfaceJSObject
- Overrides:
getClassName
in classAbstractJSObject
- 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 interfaceJSObject
- Overrides:
isFunction
in classAbstractJSObject
- 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 interfaceJSObject
- Overrides:
isStrictFunction
in classAbstractJSObject
- 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 interfaceJSObject
- Overrides:
isArray
in classAbstractJSObject
- Returns:
- if this mirror wraps a ECMAScript array object
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKey
in interfacejavax.script.Bindings
- Specified by:
containsKey
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
entrySet
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
get
in interfacejavax.script.Bindings
- Specified by:
get
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
keySet
public java.util.Set<java.lang.String> keySet()
Description copied from interface:JSObject
Returns the set of all property names of this object.- Specified by:
keySet
in interfaceJSObject
- Specified by:
keySet
in interfacejava.util.Map<java.lang.String,java.lang.Object>
- Overrides:
keySet
in classAbstractJSObject
- Returns:
- set of property names
-
put
public java.lang.Object put(java.lang.String key, java.lang.Object value)
- Specified by:
put
in interfacejavax.script.Bindings
- Specified by:
put
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
putAll
public void putAll(java.util.Map<? extends java.lang.String,?> map)
- Specified by:
putAll
in interfacejavax.script.Bindings
- Specified by:
putAll
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
remove
public java.lang.Object remove(java.lang.Object key)
- Specified by:
remove
in interfacejavax.script.Bindings
- Specified by:
remove
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
delete
public boolean delete(java.lang.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 interfacejava.util.Map<java.lang.String,java.lang.Object>
-
values
public java.util.Collection<java.lang.Object> values()
Description copied from interface:JSObject
Returns the set of all property values of this object.- Specified by:
values
in interfaceJSObject
- Specified by:
values
in interfacejava.util.Map<java.lang.String,java.lang.Object>
- Overrides:
values
in classAbstractJSObject
- Returns:
- set of property values.
-
getProto
public java.lang.Object getProto()
Return the __proto__ of this object.- Returns:
- __proto__ object.
-
setProto
public void setProto(java.lang.Object proto)
Set the __proto__ of this object.- Parameters:
proto
- new proto for this object
-
getOwnPropertyDescriptor
public java.lang.Object getOwnPropertyDescriptor(java.lang.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 java.lang.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(java.lang.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(java.lang.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 java.lang.Object wrap(java.lang.Object obj, java.lang.Object homeGlobal)
Make a script object mirror on given object if needed.- Parameters:
obj
- object to be wrapped/convertedhomeGlobal
- global to which this object belongs.- Returns:
- wrapped/converted object
-
wrapAsJSONCompatible
public static java.lang.Object wrapAsJSONCompatible(java.lang.Object obj, java.lang.Object homeGlobal)
Make a script object mirror on given object if needed. The created wrapper will implement the JavaList
interface ifobj
is a JavaScriptArray
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/convertedhomeGlobal
- global to which this object belongs.- Returns:
- wrapped/converted object
-
unwrap
public static java.lang.Object unwrap(java.lang.Object obj, java.lang.Object homeGlobal)
Unwrap a script object mirror if needed.- Parameters:
obj
- object to be unwrappedhomeGlobal
- global to which this object belongs- Returns:
- unwrapped object
-
wrapArray
public static java.lang.Object[] wrapArray(java.lang.Object[] args, java.lang.Object homeGlobal)
Wrap an array of object to script object mirrors if needed.- Parameters:
args
- array to be unwrappedhomeGlobal
- global to which this object belongs- Returns:
- wrapped array
-
unwrapArray
public static java.lang.Object[] unwrapArray(java.lang.Object[] args, java.lang.Object homeGlobal)
Unwrap an array of script object mirrors if needed.- Parameters:
args
- array to be unwrappedhomeGlobal
- global to which this object belongs- Returns:
- unwrapped array
-
identical
public static boolean identical(java.lang.Object obj1, java.lang.Object obj2)
Are the given objects mirrors to same underlying object?- Parameters:
obj1
- first objectobj2
- 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 interfaceJSObject
- Overrides:
toNumber
in classAbstractJSObject
- Returns:
- this object's numeric value.
-
getDefaultValue
public java.lang.Object getDefaultValue(java.lang.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 interfaceJSObject
- Parameters:
hint
- the type hint. Should be eithernull
,Number.class
orString.class
.- Returns:
- this object's default value.
-
-