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.internal.codegen;
  27 
  28 import jdk.nashorn.internal.ir.FunctionNode;
  29 import jdk.nashorn.internal.parser.Token;
  30 import jdk.nashorn.internal.parser.TokenType;
  31 import jdk.nashorn.internal.runtime.PropertyMap;
  32 import jdk.nashorn.internal.runtime.ScriptFunction;
  33 import jdk.nashorn.internal.runtime.Source;
  34 
  35 /**
  36  * A container for data needed to instantiate a {@link ScriptFunction} at runtime.
  37  */
  38 public class ScriptFunctionData {
  39 
  40     private final String      name;
  41     private final Source      source;
  42     private final PropertyMap allocatorMap;
  43     private final long        token;
  44     private final int         arity;
  45     private final boolean     needsCallee;
  46     private final boolean     isVarArg;
  47     private final boolean     isStrict;
  48 
  49     /**
  50      * Constructor
  51      * @param fn the function node
  52      * @param allocatorMap the allocator property map
  53      */
  54     public ScriptFunctionData(final FunctionNode fn, final PropertyMap allocatorMap) {
  55 
  56         final long      firstToken = fn.getFirstToken();
  57         final long      lastToken  = fn.getLastToken();
  58         final int       position   = Token.descPosition(firstToken);
  59         final int       length     = Token.descPosition(lastToken) - position + Token.descLength(lastToken);
  60 
  61         this.name             = fn.isAnonymous() ? "" : fn.getIdent().getName();
  62         this.source           = fn.getSource();
  63         this.allocatorMap     = allocatorMap;
  64         this.token            = Token.toDesc(TokenType.FUNCTION, position, length);
  65         this.arity            = fn.getParameters().size();
  66         this.needsCallee      = fn.needsCallee();
  67         this.isVarArg         = fn.isVarArg();
  68         this.isStrict         = fn.isStrictMode();
  69     }
  70 
  71     /**
  72      * Get the arity of the function.
  73      * @return the arity
  74      */
  75     public int getArity() {
  76         return arity;
  77     }
  78 
  79     /**
  80      * Get the function name.
  81      * @return function name
  82      */
  83     public String getName() {
  84         return name;
  85     }
  86 
  87     /**
  88      * Get the source of the function.
  89      * @return the source
  90      */
  91     public Source getSource() {
  92         return source;
  93     }
  94 
  95     /**
  96      * Get the allocator property map.
  97      * @return the allocator map
  98      */
  99     public PropertyMap getAllocatorMap() {
 100         return allocatorMap;
 101     }
 102 
 103     /**
 104      * Get the function's parse token.
 105      * @return the token
 106      */
 107     public long getToken() {
 108         return token;
 109     }
 110 
 111     /**
 112      * Returns true if the function needs a callee argument.
 113      * @return the needsCallee flag
 114      */
 115     public boolean needsCallee() {
 116         return needsCallee;
 117     }
 118 
 119     /**
 120      * Returns true if this is a strict-mode function.
 121      * @return the strict flag
 122      */
 123     public boolean isStrict() {
 124         return isStrict;
 125     }
 126 
 127     /**
 128      * Returns true if this is a var-arg function.
 129      * @return the var-arg flag
 130      */
 131     public boolean isVarArg() {
 132         return isVarArg;
 133     }
 134 
 135 }