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

Print this page




  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.runtime.JSType;

  37 import jdk.nashorn.internal.runtime.ScriptFunction;
  38 import jdk.nashorn.internal.runtime.ScriptObject;
  39 import jdk.nashorn.internal.runtime.ScriptRuntime;

  40 
  41 /**
  42  * ECMA 15.3 Function Objects
  43  *
  44  * Note: instances of this class are never created. This class is not even a
  45  * subclass of ScriptObject. But, we use this class to generate prototype and
  46  * constructor for "Function".
  47  */
  48 @ScriptClass("Function")
  49 public final class NativeFunction {
  50     // do *not* create me!
  51     private NativeFunction() {
  52     }
  53 
  54     /**
  55      * ECMA 15.3.4.2 Function.prototype.toString ( )
  56      *
  57      * @param self self reference
  58      * @return string representation of Function
  59      */


 170         }
 171         return ((ScriptFunction)self).toSource();
 172     }
 173 
 174     /**
 175      * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 176      *
 177      * Constructor
 178      *
 179      * @param newObj is the new operator used for constructing this function
 180      * @param self   self reference
 181      * @param args   arguments
 182      * @return new NativeFunction
 183      */
 184     @Constructor(arity = 1)
 185     public static Object function(final boolean newObj, final Object self, final Object... args) {
 186         final StringBuilder sb = new StringBuilder();
 187 
 188         sb.append("(function (");
 189         if (args.length > 0) {

 190             for (int i = 0; i < args.length - 1; i++) {
 191                 sb.append(JSType.toString(args[i]));
 192                 if (i < args.length - 2) {
 193                     sb.append(",");
 194                 }
 195             }






 196         }
 197         sb.append(") {\n");
 198         if (args.length > 0) {
 199             sb.append(JSType.toString(args[args.length - 1]));


 200             sb.append('\n');
 201         }
 202         sb.append("})");
 203 
 204         final Global global = Global.instance();
 205 
 206         return Global.directEval(global, sb.toString(), global, "<function>", Global.isStrict());




















 207     }
 208 }


  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  */
  52 @ScriptClass("Function")
  53 public final class NativeFunction {
  54     // do *not* create me!
  55     private NativeFunction() {
  56     }
  57 
  58     /**
  59      * ECMA 15.3.4.2 Function.prototype.toString ( )
  60      *
  61      * @param self self reference
  62      * @return string representation of Function
  63      */


 174         }
 175         return ((ScriptFunction)self).toSource();
 176     }
 177 
 178     /**
 179      * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 180      *
 181      * Constructor
 182      *
 183      * @param newObj is the new operator used for constructing this function
 184      * @param self   self reference
 185      * @param args   arguments
 186      * @return new NativeFunction
 187      */
 188     @Constructor(arity = 1)
 189     public static Object function(final boolean newObj, final Object self, final Object... args) {
 190         final StringBuilder sb = new StringBuilder();
 191 
 192         sb.append("(function (");
 193         if (args.length > 0) {
 194             final StringBuilder paramListBuf = new StringBuilder();
 195             for (int i = 0; i < args.length - 1; i++) {
 196                 paramListBuf.append(JSType.toString(args[i]));
 197                 if (i < args.length - 2) {
 198                     paramListBuf.append(",");
 199                 }
 200             }
 201 
 202             final String paramList = paramListBuf.toString();
 203             if (! paramList.isEmpty()) {
 204                 checkFunctionParameters(paramList);
 205                 sb.append(paramList);
 206             }
 207         }
 208         sb.append(") {\n");
 209         if (args.length > 0) {
 210             final String funcBody = JSType.toString(args[args.length - 1]);
 211             checkFunctionBody(funcBody);
 212             sb.append(funcBody);
 213             sb.append('\n');
 214         }
 215         sb.append("})");
 216 
 217         final Global global = Global.instance();
 218 
 219         return Global.directEval(global, sb.toString(), global, "<function>", Global.isStrict());
 220     }
 221 
 222     private static void checkFunctionParameters(final String params) {
 223         final Source src = new Source("<function>", params);
 224         final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
 225         try {
 226             parser.parseFormalParameterList();
 227         } catch (final ParserException pe) {
 228             pe.throwAsEcmaException();
 229         }
 230     }
 231 
 232     private static void checkFunctionBody(final String funcBody) {
 233         final Source src = new Source("<function>", funcBody);
 234         final Parser parser = new Parser(Global.getEnv(), src, new Context.ThrowErrorManager());
 235         try {
 236             parser.parseFunctionBody();
 237         } catch (final ParserException pe) {
 238             pe.throwAsEcmaException();
 239         }
 240     }
 241 }