ScriptUtils.java revision 1631:93854b0b5e5e
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
26package jdk.nashorn.api.scripting;
27
28import java.lang.invoke.MethodHandle;
29import jdk.dynalink.beans.StaticClass;
30import jdk.dynalink.linker.LinkerServices;
31import jdk.nashorn.internal.runtime.Context;
32import jdk.nashorn.internal.runtime.ScriptFunction;
33import jdk.nashorn.internal.runtime.ScriptObject;
34import jdk.nashorn.internal.runtime.ScriptRuntime;
35import jdk.nashorn.internal.runtime.linker.Bootstrap;
36
37/**
38 * Utilities that are to be called from script code.
39 *
40 * @since 1.8u40
41 */
42public 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
50     * @param name name of the code source (used for location)
51     * @param includeLoc tells whether to include location information for nodes or not
52     * @return JSON string representation of AST of the supplied code
53     */
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     * @throws IllegalArgumentException if func does not represent a script function
78     */
79    public static Object makeSynchronizedFunction(final Object func, final Object sync) {
80        final Object unwrapped = unwrap(func);
81        if (unwrapped instanceof ScriptFunction) {
82            return ((ScriptFunction)unwrapped).createSynchronized(unwrap(sync));
83        }
84
85        throw new IllegalArgumentException();
86    }
87
88    /**
89     * Make a script object mirror on given object if needed.
90     *
91     * @param obj object to be wrapped
92     * @return wrapped object
93     * @throws IllegalArgumentException if obj cannot be wrapped
94     */
95    public static ScriptObjectMirror wrap(final Object obj) {
96        if (obj instanceof ScriptObjectMirror) {
97            return (ScriptObjectMirror)obj;
98        }
99
100        if (obj instanceof ScriptObject) {
101            final ScriptObject sobj = (ScriptObject)obj;
102            return (ScriptObjectMirror) ScriptObjectMirror.wrap(sobj, Context.getGlobal());
103        }
104
105        throw new IllegalArgumentException();
106    }
107
108    /**
109     * Unwrap a script object mirror if needed.
110     *
111     * @param obj object to be unwrapped
112     * @return unwrapped object
113     */
114    public static Object unwrap(final Object obj) {
115        if (obj instanceof ScriptObjectMirror) {
116            return ScriptObjectMirror.unwrap(obj, Context.getGlobal());
117        }
118
119        return obj;
120    }
121
122    /**
123     * Wrap an array of object to script object mirrors if needed.
124     *
125     * @param args array to be unwrapped
126     * @return wrapped array
127     */
128    public static Object[] wrapArray(final Object[] args) {
129        if (args == null || args.length == 0) {
130            return args;
131        }
132
133        return ScriptObjectMirror.wrapArray(args, Context.getGlobal());
134    }
135
136    /**
137     * Unwrap an array of script object mirrors if needed.
138     *
139     * @param args array to be unwrapped
140     * @return unwrapped array
141     */
142    public static Object[] unwrapArray(final Object[] args) {
143        if (args == null || args.length == 0) {
144            return args;
145        }
146
147        return ScriptObjectMirror.unwrapArray(args, Context.getGlobal());
148    }
149
150    /**
151     * Convert the given object to the given type.
152     *
153     * @param obj object to be converted
154     * @param type destination type to convert to. type is either a Class
155     * or nashorn representation of a Java type returned by Java.type() call in script.
156     * @return converted object
157     */
158    public static Object convert(final Object obj, final Object type) {
159        if (obj == null) {
160            return null;
161        }
162
163        final Class<?> clazz;
164        if (type instanceof Class) {
165            clazz = (Class<?>)type;
166        } else if (type instanceof StaticClass) {
167            clazz = ((StaticClass)type).getRepresentedClass();
168        } else {
169            throw new IllegalArgumentException("type expected");
170        }
171
172        final LinkerServices linker = Bootstrap.getLinkerServices();
173        final Object objToConvert = unwrap(obj);
174        final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(),  clazz);
175        if (converter == null) {
176            // no supported conversion!
177            throw new UnsupportedOperationException("conversion not supported");
178        }
179
180        try {
181            return converter.invoke(objToConvert);
182        } catch (final RuntimeException | Error e) {
183            throw e;
184        } catch (final Throwable t) {
185            throw new RuntimeException(t);
186        }
187    }
188}
189