src/jdk/nashorn/api/scripting/ScriptUtils.java

Print this page




   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.lang.invoke.MethodHandle;
  29 import jdk.internal.dynalink.beans.StaticClass;
  30 import jdk.internal.dynalink.linker.LinkerServices;
  31 import jdk.nashorn.internal.runtime.Context;
  32 import jdk.nashorn.internal.runtime.ScriptFunction;
  33 import jdk.nashorn.internal.runtime.ScriptObject;
  34 import jdk.nashorn.internal.runtime.ScriptRuntime;
  35 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  36 
  37 /**
  38  * Utilities that are to be called from script code.
  39  */
  40 public final class ScriptUtils {
  41     private ScriptUtils() {}
  42 
  43     /**
  44      * Returns AST as JSON compatible string. This is used to
  45      * implement "parse" function in resources/parse.js script.
  46      *
  47      * @param code code to be parsed


  52     public static String parse(final String code, final String name, final boolean includeLoc) {
  53         return ScriptRuntime.parse(code, name, includeLoc);
  54     }
  55 
  56     /**
  57      * Method which converts javascript types to java types for the
  58      * String.format method (jrunscript function sprintf).
  59      *
  60      * @param format a format string
  61      * @param args arguments referenced by the format specifiers in format
  62      * @return a formatted string
  63      */
  64     public static String format(final String format, final Object[] args) {
  65         return Formatter.format(format, args);
  66     }
  67 
  68     /**
  69      * Create a wrapper function that calls {@code func} synchronized on {@code sync} or, if that is undefined,
  70      * {@code self}. Used to implement "sync" function in resources/mozilla_compat.js.
  71      *
  72      * @param func the function to invoke
  73      * @param sync the object to synchronize on
  74      * @return a synchronizing wrapper function
  75      */
  76     public static Object makeSynchronizedFunction(final ScriptFunction func, final Object sync) {
  77         return func.makeSynchronizedFunction(sync);

  78     }
  79 
  80     /**
  81      * Make a script object mirror on given object if needed.
  82      *
  83      * @param obj object to be wrapped
  84      * @return wrapped object
  85      */
  86     public static Object wrap(final Object obj) {
  87         if (obj instanceof ScriptObject) {
  88             return ScriptObjectMirror.wrap(obj, Context.getGlobal());
  89         }
  90 
  91         return obj;
  92     }
  93 
  94     /**
  95      * Unwrap a script object mirror if needed.
  96      *
  97      * @param obj object to be unwrapped




   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 static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  29 
  30 import java.lang.invoke.MethodHandle;
  31 import jdk.internal.dynalink.beans.StaticClass;
  32 import jdk.internal.dynalink.linker.LinkerServices;
  33 import jdk.nashorn.internal.runtime.Context;
  34 import jdk.nashorn.internal.runtime.ScriptFunction;
  35 import jdk.nashorn.internal.runtime.ScriptObject;
  36 import jdk.nashorn.internal.runtime.ScriptRuntime;
  37 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  38 
  39 /**
  40  * Utilities that are to be called from script code.
  41  */
  42 public final class ScriptUtils {
  43     private ScriptUtils() {}
  44 
  45     /**
  46      * Returns AST as JSON compatible string. This is used to
  47      * implement "parse" function in resources/parse.js script.
  48      *
  49      * @param code code to be parsed


  54     public static String parse(final String code, final String name, final boolean includeLoc) {
  55         return ScriptRuntime.parse(code, name, includeLoc);
  56     }
  57 
  58     /**
  59      * Method which converts javascript types to java types for the
  60      * String.format method (jrunscript function sprintf).
  61      *
  62      * @param format a format string
  63      * @param args arguments referenced by the format specifiers in format
  64      * @return a formatted string
  65      */
  66     public static String format(final String format, final Object[] args) {
  67         return Formatter.format(format, args);
  68     }
  69 
  70     /**
  71      * Create a wrapper function that calls {@code func} synchronized on {@code sync} or, if that is undefined,
  72      * {@code self}. Used to implement "sync" function in resources/mozilla_compat.js.
  73      *
  74      * @param func the function to wrap
  75      * @param sync the object to synchronize on
  76      * @return a synchronizing wrapper function
  77      */
  78     public static Object makeSynchronizedFunction(final Object func, final Object sync) {
  79         return func instanceof ScriptFunction?
  80             ((ScriptFunction)func).makeSynchronizedFunction(sync) : UNDEFINED;
  81     }
  82 
  83     /**
  84      * Make a script object mirror on given object if needed.
  85      *
  86      * @param obj object to be wrapped
  87      * @return wrapped object
  88      */
  89     public static Object wrap(final Object obj) {
  90         if (obj instanceof ScriptObject) {
  91             return ScriptObjectMirror.wrap(obj, Context.getGlobal());
  92         }
  93 
  94         return obj;
  95     }
  96 
  97     /**
  98      * Unwrap a script object mirror if needed.
  99      *
 100      * @param obj object to be unwrapped