src/jdk/nashorn/internal/objects/NativeObject.java
Print this page
@@ -26,15 +26,17 @@
package jdk.nashorn.internal.objects;
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
+import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.objects.annotations.Attribute;
import jdk.nashorn.internal.objects.annotations.Constructor;
import jdk.nashorn.internal.objects.annotations.Function;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
import jdk.nashorn.internal.objects.annotations.Where;
+import jdk.nashorn.internal.runtime.ECMAException;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.linker.InvokeByName;
@@ -52,22 +54,30 @@
private static final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
private NativeObject() {
}
+ private static ECMAException notAnObject(final Object obj) {
+ return typeError("not.an.object", ScriptRuntime.safeToString(obj));
+ }
+
/**
* ECMA 15.2.3.2 Object.getPrototypeOf ( O )
*
* @param self self reference
* @param obj object to get prototype from
* @return the prototype of an object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getPrototypeOf(final Object self, final Object obj) {
- Global.checkObject(obj);
-
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).getProto();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).getProto();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.3 Object.getOwnPropertyDescriptor ( O, P )
*
@@ -76,16 +86,23 @@
* @param prop property descriptor
* @return property descriptor
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getOwnPropertyDescriptor(final Object self, final Object obj, final Object prop) {
- Global.checkObject(obj);
-
+ if (obj instanceof ScriptObject) {
final String key = JSType.toString(prop);
final ScriptObject sobj = (ScriptObject)obj;
return sobj.getOwnPropertyDescriptor(key);
+ } else if (obj instanceof ScriptObjectMirror) {
+ final String key = JSType.toString(prop);
+ final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
+
+ return sobjMirror.getOwnPropertyDescriptor(key);
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.4 Object.getOwnPropertyNames ( O )
*
@@ -93,13 +110,17 @@
* @param obj object to query for property names
* @return array of property names
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getOwnPropertyNames(final Object self, final Object obj) {
- Global.checkObject(obj);
-
+ if (obj instanceof ScriptObject) {
return new NativeArray(((ScriptObject)obj).getOwnKeys(true));
+ } else if (obj instanceof ScriptObjectMirror) {
+ return new NativeArray(((ScriptObjectMirror)obj).getOwnKeys(true));
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.5 Object.create ( O [, Properties] )
*
@@ -173,12 +194,17 @@
* @param obj object to seal
* @return sealed object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object seal(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).seal();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).seal();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.9 Object.freeze ( O )
@@ -187,12 +213,17 @@
* @param obj object to freeze
* @return frozen object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object freeze(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).freeze();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).freeze();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.10 Object.preventExtensions ( O )
*
@@ -200,12 +231,17 @@
* @param obj object, for which to set the internal extensible property to false
* @return object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object preventExtensions(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).preventExtensions();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).preventExtensions();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.11 Object.isSealed ( O )
*
@@ -213,12 +249,17 @@
* @param obj check whether an object is sealed
* @return true if sealed, false otherwise
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object isSealed(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).isSealed();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).isSealed();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.12 Object.isFrozen ( O )
*
@@ -226,12 +267,17 @@
* @param obj check whether an object
* @return true if object is frozen, false otherwise
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object isFrozen(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).isFrozen();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).isFrozen();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.13 Object.isExtensible ( O )
*
@@ -239,12 +285,17 @@
* @param obj check whether an object is extensible
* @return true if object is extensible, false otherwise
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object isExtensible(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
return ((ScriptObject)obj).isExtensible();
+ } else if (obj instanceof ScriptObjectMirror) {
+ return ((ScriptObjectMirror)obj).isExtensible();
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.3.14 Object.keys ( O )
*
@@ -252,13 +303,19 @@
* @param obj object from which to extract keys
* @return array of keys in object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object keys(final Object self, final Object obj) {
- Global.checkObject(obj);
+ if (obj instanceof ScriptObject) {
final ScriptObject sobj = (ScriptObject)obj;
return new NativeArray(sobj.getOwnKeys(false));
+ } else if (obj instanceof ScriptObjectMirror) {
+ final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
+ return new NativeArray(sobjMirror.getOwnKeys(false));
+ } else {
+ throw notAnObject(obj);
+ }
}
/**
* ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
*