src/jdk/nashorn/internal/objects/NativeFunction.java

Print this page




  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.internal.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  30 
  31 import java.util.List;

  32 import jdk.nashorn.internal.objects.annotations.Attribute;
  33 import jdk.nashorn.internal.objects.annotations.Constructor;
  34 import jdk.nashorn.internal.objects.annotations.Function;
  35 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  36 import jdk.nashorn.internal.parser.Parser;
  37 import jdk.nashorn.internal.runtime.Context;
  38 import jdk.nashorn.internal.runtime.JSType;
  39 import jdk.nashorn.internal.runtime.ParserException;
  40 import jdk.nashorn.internal.runtime.ScriptFunction;
  41 import jdk.nashorn.internal.runtime.ScriptObject;
  42 import jdk.nashorn.internal.runtime.ScriptRuntime;
  43 import jdk.nashorn.internal.runtime.Source;
  44 
  45 /**
  46  * ECMA 15.3 Function Objects
  47  *
  48  * Note: instances of this class are never created. This class is not even a
  49  * subclass of ScriptObject. But, we use this class to generate prototype and
  50  * constructor for "Function".
  51  */


  85 
  86         Object[] args = null;
  87 
  88         if (array instanceof ScriptObject) {
  89             // look for array-like object
  90             final ScriptObject sobj = (ScriptObject)array;
  91             final Object       len  = sobj.getLength();
  92             final int n = (int)JSType.toUint32(len);
  93 
  94             args = new Object[n];
  95             for (int i = 0; i < args.length; i++) {
  96                 args[i] = sobj.get(i);
  97             }
  98         } else if (array instanceof Object[]) {
  99             args = (Object[])array;
 100         } else if (array instanceof List) {
 101             final List<?> list = (List<?>)array;
 102             list.toArray(args = new Object[list.size()]);
 103         } else if (array == null || array == UNDEFINED) {
 104             args = ScriptRuntime.EMPTY_ARRAY;










 105         } else {
 106             throw typeError("function.apply.expects.array");
 107         }
 108 
 109         return ScriptRuntime.apply((ScriptFunction)self, thiz, args);
 110     }
 111 
 112     /**
 113      * ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
 114      *
 115      * @param self self reference
 116      * @param args arguments for call
 117      * @return result of call
 118      */
 119     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
 120     public static Object call(final Object self, final Object... args) {
 121         if (!(self instanceof ScriptFunction)) {
 122             throw typeError("not.a.function", ScriptRuntime.safeToString(self));
 123         }
 124 




  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.internal.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  30 
  31 import java.util.List;
  32 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  33 import jdk.nashorn.internal.objects.annotations.Attribute;
  34 import jdk.nashorn.internal.objects.annotations.Constructor;
  35 import jdk.nashorn.internal.objects.annotations.Function;
  36 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  37 import jdk.nashorn.internal.parser.Parser;
  38 import jdk.nashorn.internal.runtime.Context;
  39 import jdk.nashorn.internal.runtime.JSType;
  40 import jdk.nashorn.internal.runtime.ParserException;
  41 import jdk.nashorn.internal.runtime.ScriptFunction;
  42 import jdk.nashorn.internal.runtime.ScriptObject;
  43 import jdk.nashorn.internal.runtime.ScriptRuntime;
  44 import jdk.nashorn.internal.runtime.Source;
  45 
  46 /**
  47  * ECMA 15.3 Function Objects
  48  *
  49  * Note: instances of this class are never created. This class is not even a
  50  * subclass of ScriptObject. But, we use this class to generate prototype and
  51  * constructor for "Function".
  52  */


  86 
  87         Object[] args = null;
  88 
  89         if (array instanceof ScriptObject) {
  90             // look for array-like object
  91             final ScriptObject sobj = (ScriptObject)array;
  92             final Object       len  = sobj.getLength();
  93             final int n = (int)JSType.toUint32(len);
  94 
  95             args = new Object[n];
  96             for (int i = 0; i < args.length; i++) {
  97                 args[i] = sobj.get(i);
  98             }
  99         } else if (array instanceof Object[]) {
 100             args = (Object[])array;
 101         } else if (array instanceof List) {
 102             final List<?> list = (List<?>)array;
 103             list.toArray(args = new Object[list.size()]);
 104         } else if (array == null || array == UNDEFINED) {
 105             args = ScriptRuntime.EMPTY_ARRAY;
 106         } else if (array instanceof ScriptObjectMirror) {
 107             // look for array-like ScriptObjectMirror object
 108             final ScriptObjectMirror mirror = (ScriptObjectMirror)array;
 109             final Object       len  = mirror.containsKey("length")? mirror.getMember("length") : Integer.valueOf(0);
 110             final int n = (int)JSType.toUint32(len);
 111 
 112             args = new Object[n];
 113             for (int i = 0; i < args.length; i++) {
 114                 args[i] = mirror.containsKey(i)? mirror.getSlot(i) : UNDEFINED;
 115             }
 116         } else {
 117             throw typeError("function.apply.expects.array");
 118         }
 119 
 120         return ScriptRuntime.apply((ScriptFunction)self, thiz, args);
 121     }
 122 
 123     /**
 124      * ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
 125      *
 126      * @param self self reference
 127      * @param args arguments for call
 128      * @return result of call
 129      */
 130     @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
 131     public static Object call(final Object self, final Object... args) {
 132         if (!(self instanceof ScriptFunction)) {
 133             throw typeError("not.a.function", ScriptRuntime.safeToString(self));
 134         }
 135