src/jdk/nashorn/internal/runtime/ScriptRuntime.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
  29 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
  30 import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
  31 import static jdk.nashorn.internal.runtime.ECMAErrors.syntaxError;
  32 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  33 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
  34 
  35 import java.lang.invoke.MethodHandle;
  36 import java.lang.reflect.Array;
  37 import java.util.Collections;
  38 import java.util.Iterator;

  39 import java.util.NoSuchElementException;
  40 import java.util.Objects;
  41 import jdk.internal.dynalink.beans.StaticClass;
  42 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  43 import jdk.nashorn.internal.ir.debug.JSONWriter;
  44 import jdk.nashorn.internal.parser.Lexer;
  45 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  46 
  47 
  48 /**
  49  * Utilities to be called by JavaScript runtime API and generated classes.
  50  */
  51 
  52 public final class ScriptRuntime {
  53     private ScriptRuntime() {
  54     }
  55 
  56     /** Singleton representing the empty array object '[]' */
  57     public static final Object[] EMPTY_ARRAY = new Object[0];
  58 


 771 
 772     /**
 773      * ECMA 11.8.6 - The in operator - generic implementation
 774      *
 775      * @param property property to check for
 776      * @param obj object in which to check for property
 777      *
 778      * @return true if objects are equal
 779      */
 780     public static boolean IN(final Object property, final Object obj) {
 781         final JSType rvalType = JSType.of(obj);
 782 
 783         if (rvalType == JSType.OBJECT || rvalType == JSType.FUNCTION) {
 784             if (obj instanceof ScriptObject) {
 785                 return ((ScriptObject)obj).has(property);
 786             }
 787 
 788             return false;
 789         }
 790 
 791         throw typeError("in.with.non.object", rvalType.toString().toLowerCase());
 792     }
 793 
 794     /**
 795      * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 796      *
 797      * @param obj first object to compare
 798      * @param clazz type to check against
 799      *
 800      * @return true if {@code obj} is an instanceof {@code clazz}
 801      */
 802     public static boolean INSTANCEOF(final Object obj, final Object clazz) {
 803         if (clazz instanceof ScriptFunction) {
 804             if (obj instanceof ScriptObject) {
 805                 return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
 806             }
 807             return false;
 808         }
 809 
 810         if (clazz instanceof StaticClass) {
 811             return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
  29 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
  30 import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
  31 import static jdk.nashorn.internal.runtime.ECMAErrors.syntaxError;
  32 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  33 import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
  34 
  35 import java.lang.invoke.MethodHandle;
  36 import java.lang.reflect.Array;
  37 import java.util.Collections;
  38 import java.util.Iterator;
  39 import java.util.Locale;
  40 import java.util.NoSuchElementException;
  41 import java.util.Objects;
  42 import jdk.internal.dynalink.beans.StaticClass;
  43 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  44 import jdk.nashorn.internal.ir.debug.JSONWriter;
  45 import jdk.nashorn.internal.parser.Lexer;
  46 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  47 
  48 
  49 /**
  50  * Utilities to be called by JavaScript runtime API and generated classes.
  51  */
  52 
  53 public final class ScriptRuntime {
  54     private ScriptRuntime() {
  55     }
  56 
  57     /** Singleton representing the empty array object '[]' */
  58     public static final Object[] EMPTY_ARRAY = new Object[0];
  59 


 772 
 773     /**
 774      * ECMA 11.8.6 - The in operator - generic implementation
 775      *
 776      * @param property property to check for
 777      * @param obj object in which to check for property
 778      *
 779      * @return true if objects are equal
 780      */
 781     public static boolean IN(final Object property, final Object obj) {
 782         final JSType rvalType = JSType.of(obj);
 783 
 784         if (rvalType == JSType.OBJECT || rvalType == JSType.FUNCTION) {
 785             if (obj instanceof ScriptObject) {
 786                 return ((ScriptObject)obj).has(property);
 787             }
 788 
 789             return false;
 790         }
 791 
 792         throw typeError("in.with.non.object", rvalType.toString().toLowerCase(Locale.ENGLISH));
 793     }
 794 
 795     /**
 796      * ECMA 11.8.6 - The strict instanceof operator - generic implementation
 797      *
 798      * @param obj first object to compare
 799      * @param clazz type to check against
 800      *
 801      * @return true if {@code obj} is an instanceof {@code clazz}
 802      */
 803     public static boolean INSTANCEOF(final Object obj, final Object clazz) {
 804         if (clazz instanceof ScriptFunction) {
 805             if (obj instanceof ScriptObject) {
 806                 return ((ScriptObject)clazz).isInstance((ScriptObject)obj);
 807             }
 808             return false;
 809         }
 810 
 811         if (clazz instanceof StaticClass) {
 812             return ((StaticClass)clazz).getRepresentedClass().isInstance(obj);