| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | // API class |
| 8 | |
| 9 | package org.mozilla.javascript; |
| 10 | |
| 11 | import java.lang.reflect.Method; |
| 12 | import java.lang.reflect.Member; |
| 13 | import java.util.Iterator; |
| 14 | |
| 15 | public abstract class VMBridge |
| 16 | { |
| 17 | |
| 18 | static final VMBridge instance = makeInstance(); |
| 19 | |
| 20 | private static VMBridge makeInstance() |
| 21 | { |
| 22 | String[] classNames = { |
| 23 | "org.mozilla.javascript.VMBridge_custom", |
| 24 | "org.mozilla.javascript.jdk15.VMBridge_jdk15", |
| 25 | "org.mozilla.javascript.jdk13.VMBridge_jdk13", |
| 26 | "org.mozilla.javascript.jdk11.VMBridge_jdk11", |
| 27 | }; |
| 28 | for (int i = 0; i != classNames.length; ++i) { |
| 29 | String className = classNames[i]; |
| 30 | Class<?> cl = Kit.classOrNull(className); |
| 31 | if (cl != null) { |
| 32 | VMBridge bridge = (VMBridge)Kit.newInstanceOrNull(cl); |
| 33 | if (bridge != null) { |
| 34 | return bridge; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | throw new IllegalStateException("Failed to create VMBridge instance"); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Return a helper object to optimize {@link Context} access. |
| 43 | * <p> |
| 44 | * The runtime will pass the resulting helper object to the subsequent |
| 45 | * calls to {@link #getContext(Object contextHelper)} and |
| 46 | * {@link #setContext(Object contextHelper, Context cx)} methods. |
| 47 | * In this way the implementation can use the helper to cache |
| 48 | * information about current thread to make {@link Context} access faster. |
| 49 | */ |
| 50 | protected abstract Object getThreadContextHelper(); |
| 51 | |
| 52 | /** |
| 53 | * Get {@link Context} instance associated with the current thread |
| 54 | * or null if none. |
| 55 | * |
| 56 | * @param contextHelper The result of {@link #getThreadContextHelper()} |
| 57 | * called from the current thread. |
| 58 | */ |
| 59 | protected abstract Context getContext(Object contextHelper); |
| 60 | |
| 61 | /** |
| 62 | * Associate {@link Context} instance with the current thread or remove |
| 63 | * the current association if <tt>cx</tt> is null. |
| 64 | * |
| 65 | * @param contextHelper The result of {@link #getThreadContextHelper()} |
| 66 | * called from the current thread. |
| 67 | */ |
| 68 | protected abstract void setContext(Object contextHelper, Context cx); |
| 69 | |
| 70 | /** |
| 71 | * Return the ClassLoader instance associated with the current thread. |
| 72 | */ |
| 73 | protected abstract ClassLoader getCurrentThreadClassLoader(); |
| 74 | |
| 75 | /** |
| 76 | * In many JVMSs, public methods in private |
| 77 | * classes are not accessible by default (Sun Bug #4071593). |
| 78 | * VMBridge instance should try to workaround that via, for example, |
| 79 | * calling method.setAccessible(true) when it is available. |
| 80 | * The implementation is responsible to catch all possible exceptions |
| 81 | * like SecurityException if the workaround is not available. |
| 82 | * |
| 83 | * @return true if it was possible to make method accessible |
| 84 | * or false otherwise. |
| 85 | */ |
| 86 | protected abstract boolean tryToMakeAccessible(Object accessibleObject); |
| 87 | |
| 88 | /** |
| 89 | * Create helper object to create later proxies implementing the specified |
| 90 | * interfaces later. Under JDK 1.3 the implementation can look like: |
| 91 | * <pre> |
| 92 | * return java.lang.reflect.Proxy.getProxyClass(..., interfaces). |
| 93 | * getConstructor(new Class[] { |
| 94 | * java.lang.reflect.InvocationHandler.class }); |
| 95 | * </pre> |
| 96 | * |
| 97 | * @param interfaces Array with one or more interface class objects. |
| 98 | */ |
| 99 | protected Object getInterfaceProxyHelper(ContextFactory cf, |
| 100 | Class<?>[] interfaces) |
| 101 | { |
| 102 | throw Context.reportRuntimeError( |
| 103 | "VMBridge.getInterfaceProxyHelper is not supported"); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Create proxy object for {@link InterfaceAdapter}. The proxy should call |
| 108 | * {@link InterfaceAdapter#invoke(ContextFactory, Object, Scriptable, |
| 109 | * Method, Object[])} |
| 110 | * as implementation of interface methods associated with |
| 111 | * <tt>proxyHelper</tt>. {@link Method} |
| 112 | * |
| 113 | * @param proxyHelper The result of the previous call to |
| 114 | * {@link #getInterfaceProxyHelper(ContextFactory, Class[])}. |
| 115 | */ |
| 116 | protected Object newInterfaceProxy(Object proxyHelper, |
| 117 | ContextFactory cf, |
| 118 | InterfaceAdapter adapter, |
| 119 | Object target, |
| 120 | Scriptable topScope) |
| 121 | { |
| 122 | throw Context.reportRuntimeError( |
| 123 | "VMBridge.newInterfaceProxy is not supported"); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns whether or not a given member (method or constructor) |
| 128 | * has variable arguments. |
| 129 | * Variable argument methods have only been supported in Java since |
| 130 | * JDK 1.5. |
| 131 | */ |
| 132 | protected abstract boolean isVarArgs(Member member); |
| 133 | |
| 134 | /** |
| 135 | * If "obj" is a java.util.Iterator or a java.lang.Iterable, return a |
| 136 | * wrapping as a JavaScript Iterator. Otherwise, return null. |
| 137 | * This method is in VMBridge since Iterable is a JDK 1.5 addition. |
| 138 | */ |
| 139 | public Iterator<?> getJavaIterator(Context cx, Scriptable scope, Object obj) { |
| 140 | if (obj instanceof Wrapper) { |
| 141 | Object unwrapped = ((Wrapper) obj).unwrap(); |
| 142 | Iterator<?> iterator = null; |
| 143 | if (unwrapped instanceof Iterator) |
| 144 | iterator = (Iterator<?>) unwrapped; |
| 145 | return iterator; |
| 146 | } |
| 147 | return null; |
| 148 | } |
| 149 | } |