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.Context; 29 import jdk.nashorn.internal.runtime.ScriptFunction; 30 import jdk.nashorn.internal.runtime.ScriptObject; 31 import jdk.nashorn.internal.runtime.ScriptRuntime; 32 33 /** 34 * Utilities that are to be called from script code 35 */ 36 public final class ScriptUtils { 37 private ScriptUtils() {} 38 39 /** 40 * Returns AST as JSON compatible string. This is used to 41 * implement "parse" function in resources/parse.js script. 42 * 43 * @param code code to be parsed 44 * @param name name of the code source (used for location) 45 * @param includeLoc tells whether to include location information for nodes or not 46 * @return JSON string representation of AST of the supplied code 47 */ 48 public static String parse(final String code, final String name, final boolean includeLoc) { 49 return ScriptRuntime.parse(code, name, includeLoc); 50 } 51 52 /** 53 * Method which converts javascript types to java types for the 54 * String.format method (jrunscript function sprintf). 55 * 56 * @param format a format string 57 * @param args arguments referenced by the format specifiers in format 58 * @return a formatted string 59 */ 60 public static String format(final String format, final Object[] args) { 61 return Formatter.format(format, args); 62 } 63 64 /** 65 * Create a wrapper function that calls {@code func} synchronized on {@code sync} or, if that is undefined, 66 * {@code self}. Used to implement "sync" function in resources/mozilla_compat.js. 67 * 68 * @param func the function to invoke 69 * @param sync the object to synchronize on 70 * @return a synchronizing wrapper function 71 */ 72 public static Object makeSynchronizedFunction(final ScriptFunction func, final Object sync) { 73 return func.makeSynchronizedFunction(sync); 74 } 75 76 /** 77 * Make a script object mirror on given object if needed. 78 * 79 * @param obj object to be wrapped 80 * @return wrapped object 81 */ 82 public static Object wrap(final Object obj) { 83 if (obj instanceof ScriptObject) { 84 return ScriptObjectMirror.wrap(obj, Context.getGlobal()); 85 } 86 87 return obj; 88 } 89 90 /** 91 * Unwrap a script object mirror if needed. 92 * 93 * @param obj object to be unwrapped 94 * @return unwrapped object 95 */ 96 public static Object unwrap(final Object obj) { 97 if (obj instanceof ScriptObjectMirror) { 98 return ScriptObjectMirror.unwrap(obj, Context.getGlobal()); 99 } 100 101 return obj; 102 } 103 104 /** 105 * Wrap an array of object to script object mirrors if needed. 106 * 107 * @param args array to be unwrapped 108 * @return wrapped array 109 */ 110 public static Object[] wrapArray(final Object[] args) { 111 if (args == null || args.length == 0) { 112 return args; 113 } 114 115 return ScriptObjectMirror.wrapArray(args, Context.getGlobal()); 116 } 117 118 /** 119 * Unwrap an array of script object mirrors if needed. 120 * 121 * @param args array to be unwrapped 122 * @return unwrapped array 123 */ 124 public static Object[] unwrapArray(final Object[] args) { 125 if (args == null || args.length == 0) { 126 return args; 127 } 128 129 return ScriptObjectMirror.unwrapArray(args, Context.getGlobal()); 130 } 131 }