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 jdk.nashorn.internal.runtime.ScriptFunction; 29 import jdk.nashorn.internal.runtime.ScriptRuntime; 30 31 /** 32 * Utilities that are to be called from script code 33 */ 34 public final class ScriptUtils { 35 private ScriptUtils() {} 36 37 /** 38 * Returns AST as JSON compatible string. This is used to 39 * implement "parse" function in resources/parse.js script. 40 * 41 * @param code code to be parsed 42 * @param name name of the code source (used for location) 43 * @param includeLoc tells whether to include location information for nodes or not 44 * @return JSON string representation of AST of the supplied code 45 */ 46 public static String parse(final String code, final String name, final boolean includeLoc) { 47 return ScriptRuntime.parse(code, name, includeLoc); 48 } 49 50 /** 51 * Method which converts javascript types to java types for the 52 * String.format method (jrunscript function sprintf). 53 * 54 * @param format a format string 55 * @param args arguments referenced by the format specifiers in format 56 * @return a formatted string 57 */ 58 public static String format(final String format, final Object[] args) { 59 return Formatter.format(format, args); 60 } 61 62 /** 63 * Create a wrapper function that calls {@code func} synchronized on {@code sync} or, if that is undefined, 64 * {@code self}. Used to implement "sync" function in resources/mozilla_compat.js. 65 * 66 * @param func the function to invoke 67 * @param sync the object to synchronize on 68 * @return a synchronizing wrapper function 69 */ 70 public static Object makeSynchronizedFunction(final ScriptFunction func, final Object sync) { 71 return func.makeSynchronizedFunction(sync); 72 } 73 74 } --- EOF ---