1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  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.api.scripting;
  27 
  28 import java.util.Collection;
  29 import java.util.Set;
  30 
  31 /**
  32  * This interface can be implemented by an arbitrary Java class. Nashorn will
  33  * treat objects of such classes just like nashorn script objects. Usual nashorn
  34  * operations like obj[i], obj.foo, obj.func(), delete obj.foo will be glued
  35  * to appropriate method call of this interface.
  36  *
  37  * @since 1.8u40
  38  */
  39 @jdk.Exported
  40 public interface JSObject {
  41     /**
  42      * Call this object as a JavaScript function. This is equivalent to
  43      * 'func.apply(thiz, args)' in JavaScript.
  44      *
  45      * @param thiz 'this' object to be passed to the function
  46      * @param args arguments to method
  47      * @return result of call
  48      */
  49     public Object call(final Object thiz, final Object... args);
  50 
  51     /**
  52      * Call this 'constructor' JavaScript function to create a new object.
  53      * This is equivalent to 'new func(arg1, arg2...)' in JavaScript.
  54      *
  55      * @param args arguments to method
  56      * @return result of constructor call
  57      */
  58     public Object newObject(final Object... args);
  59 
  60     /**
  61      * Evaluate a JavaScript expression.
  62      *
  63      * @param s JavaScript expression to evaluate
  64      * @return evaluation result
  65      */
  66     public Object eval(final String s);
  67 
  68     /**
  69      * Retrieves a named member of this JavaScript object.
  70      *
  71      * @param name of member
  72      * @return member
  73      */
  74     public Object getMember(final String name);
  75 
  76     /**
  77      * Retrieves an indexed member of this JavaScript object.
  78      *
  79      * @param index index slot to retrieve
  80      * @return member
  81      */
  82     public Object getSlot(final int index);
  83 
  84     /**
  85      * Does this object have a named member?
  86      *
  87      * @param name name of member
  88      * @return true if this object has a member of the given name
  89      */
  90     public boolean hasMember(final String name);
  91 
  92     /**
  93      * Does this object have a indexed property?
  94      *
  95      * @param slot index to check
  96      * @return true if this object has a slot
  97      */
  98     public boolean hasSlot(final int slot);
  99 
 100     /**
 101      * Remove a named member from this JavaScript object
 102      *
 103      * @param name name of the member
 104      */
 105     public void removeMember(final String name);
 106 
 107     /**
 108      * Set a named member in this JavaScript object
 109      *
 110      * @param name  name of the member
 111      * @param value value of the member
 112      */
 113     public void setMember(final String name, final Object value);
 114 
 115     /**
 116      * Set an indexed member in this JavaScript object
 117      *
 118      * @param index index of the member slot
 119      * @param value value of the member
 120      */
 121     public void setSlot(final int index, final Object value);
 122 
 123     // property and value iteration
 124 
 125     /**
 126      * Returns the set of all property names of this object.
 127      *
 128      * @return set of property names
 129      */
 130     public Set<String> keySet();
 131 
 132     /**
 133      * Returns the set of all property values of this object.
 134      *
 135      * @return set of property values.
 136      */
 137     public Collection<Object> values();
 138 
 139     // JavaScript instanceof check
 140 
 141     /**
 142      * Checking whether the given object is an instance of 'this' object.
 143      *
 144      * @param instance instace to check
 145      * @return true if the given 'instance' is an instance of this 'function' object
 146      */
 147     public boolean isInstance(final Object instance);
 148 
 149     /**
 150      * Checking whether this object is an instance of the given 'clazz' object.
 151      *
 152      * @param clazz clazz to check
 153      * @return true if this object is an instance of the given 'clazz'
 154      */
 155     public boolean isInstanceOf(final Object clazz);
 156 
 157     /**
 158      * ECMA [[Class]] property
 159      *
 160      * @return ECMA [[Class]] property value of this object
 161      */
 162     public String getClassName();
 163 
 164     /**
 165      * Is this a function object?
 166      *
 167      * @return if this mirror wraps a ECMAScript function instance
 168      */
 169     public boolean isFunction();
 170 
 171     /**
 172      * Is this a 'use strict' function object?
 173      *
 174      * @return true if this mirror represents a ECMAScript 'use strict' function
 175      */
 176     public boolean isStrictFunction();
 177 
 178     /**
 179      * Is this an array object?
 180      *
 181      * @return if this mirror wraps a ECMAScript array object
 182      */
 183     public boolean isArray();
 184 
 185     /**
 186      * Returns this object's numeric value.
 187      *
 188      * @return this object's numeric value.
 189      */
 190     public double toNumber();
 191 }