Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Move LiveConnect / Java interop classes to own jar #1655

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.annotations.*;
import org.mozilla.javascript.lc.JavaWrapFactory;
import org.openjdk.jmh.annotations.*;

@OutputTimeUnit(TimeUnit.MICROSECONDS)
Expand All @@ -24,7 +25,6 @@ public void init()
cx = Context.enter();
cx.setOptimizationLevel(9);
cx.setLanguageVersion(Context.VERSION_ES6);

scope = cx.initStandardObjects();
ScriptableObject.defineClass(scope, AnnotatedClass.class);
IdClass.init(scope);
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/java/PrimitiveWrapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.WrapFactory;
import org.mozilla.javascript.lc.JavaWrapFactory;

/**
* An example WrapFactory that can be used to avoid wrapping of Java types that can be converted to
Expand All @@ -21,7 +21,7 @@
*
* <p>The PrimitiveWrapFactory is enabled on a Context by calling setWrapFactory on that context.
*/
public class PrimitiveWrapFactory extends WrapFactory {
public class PrimitiveWrapFactory extends JavaWrapFactory {

@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.lc.JavaWrapFactory;

/**
* This is the implementation of the standard ScriptEngine interface for Rhino.
Expand Down Expand Up @@ -277,6 +278,7 @@ private void configureContext(Context cx) throws ScriptException {
if (ol != null) {
cx.setOptimizationLevel(parseInteger(ol));
}
cx.setWrapFactory(new JavaWrapFactory());
}

private static int parseInteger(Object v) throws ScriptException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.NativeCall;
import org.mozilla.javascript.ScriptRuntime;
Expand All @@ -33,6 +32,7 @@
import org.mozilla.javascript.debug.DebuggableObject;
import org.mozilla.javascript.debug.DebuggableScript;
import org.mozilla.javascript.debug.Debugger;
import org.mozilla.javascript.lc.ImporterTopLevel;

/** Dim or Debugger Implementation for Rhino. */
public class Dim {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeConsole;
import org.mozilla.javascript.RhinoException;
Expand All @@ -52,6 +51,8 @@
import org.mozilla.javascript.commonjs.module.RequireBuilder;
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider;
import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
import org.mozilla.javascript.lc.ImporterTopLevel;
import org.mozilla.javascript.lc.JavaWrapFactory;
import org.mozilla.javascript.serialize.ScriptableInputStream;
import org.mozilla.javascript.serialize.ScriptableOutputStream;
import org.mozilla.javascript.tools.ToolErrorReporter;
Expand Down Expand Up @@ -106,6 +107,7 @@ public void init(ContextFactory factory) {
public void init(Context cx) {
// Define some global functions particular to the shell. Note
// that these functions are not part of ECMA.
cx.setWrapFactory(new JavaWrapFactory());
initStandardObjects(cx, sealedStdLib);
NativeConsole.init(this, sealedStdLib, new ShellConsolePrinter());
String[] names = {
Expand Down
1 change: 1 addition & 0 deletions rhino/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
exports org.mozilla.javascript.serialize;
exports org.mozilla.javascript.typedarrays;
exports org.mozilla.javascript.xml;
exports org.mozilla.javascript.lc;

requires java.compiler;
requires transitive java.desktop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public int getArity() {
}

@Override
String decompile(int indent, EnumSet<DecompilerFlag> flags) {
protected String decompile(int indent, EnumSet<DecompilerFlag> flags) {
if (targetFunction instanceof BaseFunction) {
return ((BaseFunction) targetFunction).decompile(indent, flags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public Scriptable createObject(Context cx, Scriptable scope) {
* @param indent How much to indent the decompiled result.
* @param flags Flags specifying format of decompilation output.
*/
String decompile(int indent, EnumSet<DecompilerFlag> flags) {
protected String decompile(int indent, EnumSet<DecompilerFlag> flags) {
StringBuilder sb = new StringBuilder();
boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY);
if (!justbody) {
Expand Down
26 changes: 10 additions & 16 deletions rhino/src/main/java/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.mozilla.javascript.ast.ScriptNode;
import org.mozilla.javascript.debug.DebuggableScript;
import org.mozilla.javascript.debug.Debugger;
import org.mozilla.javascript.lc.NativeJavaObject;
import org.mozilla.javascript.xml.XMLLib;

/**
Expand Down Expand Up @@ -637,7 +638,7 @@ public final void unseal(Object sealKey) {
}

@SuppressWarnings("DoNotCallSuggester")
static void onSealedMutation() {
public static void onSealedMutation() {
throw new IllegalStateException();
}

Expand Down Expand Up @@ -949,7 +950,7 @@ public static EvaluatorException reportRuntimeError(
throw new EvaluatorException(message, sourceName, lineno, lineSource, lineOffset);
}

static EvaluatorException reportRuntimeErrorById(String messageId, Object... args) {
public static EvaluatorException reportRuntimeErrorById(String messageId, Object... args) {
String msg = ScriptRuntime.getMessageById(messageId, args);
return reportRuntimeError(msg);
}
Expand Down Expand Up @@ -1749,14 +1750,18 @@ public static Object javaToJS(Object value, Scriptable scope, Context cx) {
if (value instanceof String
|| value instanceof Number
|| value instanceof Boolean
|| value instanceof Scriptable) {
|| value instanceof Scriptable
|| value instanceof Undefined) {
return value;
} else if (value instanceof Character) {
return String.valueOf(((Character) value).charValue());
} else {
if (cx == null) {
cx = Context.getContext();
}
if (cx.getWrapFactory() == null) {
throw new UnsupportedOperationException("Cannot convert java value " + value + "(" + (value == null ? "null" : value.getClass()) + ") to javascript");
}
return cx.getWrapFactory().wrap(cx, scope, value, null);
}
}
Expand Down Expand Up @@ -2046,7 +2051,7 @@ public final synchronized void setClassShutter(ClassShutter shutter) {
hasClassShutter = true;
}

final synchronized ClassShutter getClassShutter() {
public final synchronized ClassShutter getClassShutter() {
return classShutter;
}

Expand Down Expand Up @@ -2118,14 +2123,6 @@ public final void removeThreadLocal(Object key) {
threadLocalMap.remove(key);
}

/**
* @deprecated
* @see ClassCache#get(Scriptable)
* @see ClassCache#setCachingEnabled(boolean)
*/
@Deprecated
public static void setCachingEnabled(boolean cachingEnabled) {}

/**
* Set a WrapFactory for this Context.
*
Expand All @@ -2148,9 +2145,6 @@ public final void setWrapFactory(WrapFactory wrapFactory) {
* @since 1.5 Release 4
*/
public final WrapFactory getWrapFactory() {
if (wrapFactory == null) {
wrapFactory = new WrapFactory();
}
return wrapFactory;
}

Expand Down Expand Up @@ -2407,7 +2401,7 @@ public UnhandledRejectionTracker getUnhandledPromiseTracker() {
/* ******** end of API ********* */

/** Internal method that reports an error for missing calls to enter(). */
static Context getContext() {
public static Context getContext() {
Context cx = getCurrentContext();
if (cx == null) {
throw new RuntimeException("No Context associated with current Thread");
Expand Down
21 changes: 13 additions & 8 deletions rhino/src/main/java/org/mozilla/javascript/EqualObjectGraphs.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* comparing cyclic data structures that can memoize false equalities if two cyclic data structures
* end up being unequal.
*/
final class EqualObjectGraphs {
public final class EqualObjectGraphs {
private static final ThreadLocal<EqualObjectGraphs> instance = new ThreadLocal<>();

private static final Set<Class<?>> valueClasses =
Expand Down Expand Up @@ -142,9 +142,9 @@ private boolean equalGraphsNoMemo(Object o1, Object o2) {
if (o1 instanceof Wrapper) {
return o2 instanceof Wrapper
&& equalGraphs(((Wrapper) o1).unwrap(), ((Wrapper) o2).unwrap());
} else if (o1 instanceof NativeJavaTopPackage) {
} else if (o1 instanceof Stateless) {
// stateless objects, must check before Scriptable
return o2 instanceof NativeJavaTopPackage;
return o2 instanceof Stateless && o1.getClass() == o2.getClass();
} else if (o1 instanceof Scriptable) {
return o2 instanceof Scriptable && equalScriptables((Scriptable) o1, (Scriptable) o2);
} else if (o1 instanceof SymbolKey) {
Expand All @@ -160,10 +160,6 @@ private boolean equalGraphsNoMemo(Object o1, Object o2) {
return o2 instanceof Map<?, ?> && equalMaps((Map<?, ?>) o1, (Map<?, ?>) o2);
} else if (o1 instanceof Set<?>) {
return o2 instanceof Set<?> && equalSets((Set<?>) o1, (Set<?>) o2);
} else if (o1 instanceof NativeGlobal) {
return o2 instanceof NativeGlobal; // stateless objects
} else if (o1 instanceof JavaAdapter) {
return o2 instanceof JavaAdapter; // stateless objects
}

// Fallback case for everything else.
Expand Down Expand Up @@ -193,7 +189,7 @@ private boolean equalScriptables(final Scriptable s1, final Scriptable s2) {
return s2 instanceof NativeContinuation
&& NativeContinuation.equalImplementations(
(NativeContinuation) s1, (NativeContinuation) s2);
} else if (s1 instanceof NativeJavaPackage) {
} else if (s1 instanceof JavaEquals) {
return s1.equals(s2); // Overridden appropriately
} else if (s1 instanceof IdFunctionObject) {
return s2 instanceof IdFunctionObject
Expand Down Expand Up @@ -359,4 +355,13 @@ private static Object getValue(final Scriptable s, final Object id) {
throw new ClassCastException();
}
}

/**
* marker interface, that object is stateless and two objects are equal. if they have the same
* class
*/
public interface Stateless {}

/** marker interface, that equality is checked with java equals method */
public interface JavaEquals {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Scriptable createObject(Context cx, Scriptable scope) {
}

@Override
String decompile(int indent, EnumSet<DecompilerFlag> flags) {
protected String decompile(int indent, EnumSet<DecompilerFlag> flags) {
StringBuilder sb = new StringBuilder();
boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY);
if (!justbody) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import org.mozilla.javascript.lc.ClassCache;

/**
* Adapter to use JS function as implementation of Java interfaces with single method or multiple
Expand All @@ -24,7 +25,7 @@ public class InterfaceAdapter {
* @return The glue object or null if <code>cl</code> is not interface or has methods with
* different signatures.
*/
static Object create(Context cx, Class<?> cl, ScriptableObject object) {
public static Object create(Context cx, Class<?> cl, ScriptableObject object) {
if (!cl.isInterface()) throw new IllegalArgumentException();

Scriptable topScope = ScriptRuntime.getTopCallScope(cx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public LazilyLoadedCtor(
this(scope, propertyName, className, sealed, false);
}

LazilyLoadedCtor(
public LazilyLoadedCtor(
ScriptableObject scope,
String propertyName,
String className,
Expand Down
Loading
Loading