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 public interface JSObject {
  38     /**
  39      * Call this object as a JavaScript function. This is equivalent to
  40      * 'func.apply(thiz, args)' in JavaScript.
  41      *
  42      * @param thiz 'this' object to be passed to the function
  43      * @param args arguments to method
  44      * @return result of call
  45      */
  46     public Object call(final Object thiz, final Object... args);
  47 
  48     /**
  49      * Call this 'constructor' JavaScript function to create a new object.
  50      * This is equivalent to 'new func(arg1, arg2...)' in JavaScript.
  51      *
  52      * @param args arguments to method
  53      * @return result of constructor call
  54      */
  55     public Object newObject(final Object... args);
  56 
  57     /**
  58      * Evaluate a JavaScript expression.
  59      *
  60      * @param s JavaScript expression to evaluate
  61      * @return evaluation result
  62      */
  63     public Object eval(final String s);
  64 
  65     /**
  66      * Retrieves a named member of this JavaScript object.
  67      *
  68      * @param name of member
  69      * @return member
  70      */
  71     public Object getMember(final String name);
  72 
  73     /**
  74      * Retrieves an indexed member of this JavaScript object.
  75      *
  76      * @param index index slot to retrieve
  77      * @return member
  78      */
  79     public Object getSlot(final int index);
  80 
  81     /**
  82      * Does this object have a named member?
  83      *
  84      * @param name name of member
  85      * @return true if this object has a member of the given name
  86      */
  87     public boolean hasMember(final String name);
  88 
  89     /**
  90      * Does this object have a indexed property?
  91      *
  92      * @param slot index to check
  93      * @return true if this object has a slot
  94      */
  95     public boolean hasSlot(final int slot);
  96 
  97     /**
  98      * Remove a named member from this JavaScript object
  99      *
 100      * @param name name of the member
 101      */
 102     public void removeMember(final String name);
 103 
 104     /**
 105      * Set a named member in this JavaScript object
 106      *
 107      * @param name  name of the member
 108      * @param value value of the member
 109      */
 110     public void setMember(final String name, final Object value);
 111 
 112     /**
 113      * Set an indexed member in this JavaScript object
 114      *
 115      * @param index index of the member slot
 116      * @param value value of the member
 117      */
 118     public void setSlot(final int index, final Object value);
 119 
 120     // property and value iteration
 121 
 122     /**
 123      * Returns the set of all property names of this object.
 124      *
 125      * @return set of property names
 126      */
 127     public Set<String> keySet();
 128 
 129     /**
 130      * Returns the set of all property values of this object.
 131      *
 132      * @return set of property values.
 133      */
 134     public Collection<Object> values();
 135 
 136     // JavaScript instanceof check
 137 
 138     /**
 139      * Checking whether the given object is an instance of 'this' object.
 140      *
 141      * @param instance instace to check
 142      * @return true if the given 'instance' is an instance of this 'function' object
 143      */
 144     public boolean isInstance(final Object instance);
 145 
 146     /**
 147      * Checking whether this object is an instance of the given 'clazz' object.
 148      *
 149      * @param clazz clazz to check
 150      * @return true if this object is an instance of the given 'clazz'
 151      */
 152     public boolean isInstanceOf(final Object clazz);
 153 
 154     /**
 155      * ECMA [[Class]] property
 156      *
 157      * @return ECMA [[Class]] property value of this object
 158      */
 159     public String getClassName();
 160 
 161     /**
 162      * Is this a function object?
 163      *
 164      * @return if this mirror wraps a ECMAScript function instance
 165      */
 166     public boolean isFunction();
 167 
 168     /**
 169      * Is this a 'use strict' function object?
 170      *
 171      * @return true if this mirror represents a ECMAScript 'use strict' function
 172      */
 173     public boolean isStrictFunction();
 174 
 175     /**
 176      * Is this an array object?
 177      *
 178      * @return if this mirror wraps a ECMAScript array object
 179      */
 180     public boolean isArray();
 181 
 182     /**
 183      * Returns this object's numeric value.
 184      *
 185      * @return this object's numeric value.
 186      */
 187     public double toNumber();
 188 }