diff --git a/benchmarks/src/jmh/java/org/mozilla/javascript/benchmarks/BuiltinBenchmark.java b/benchmarks/src/jmh/java/org/mozilla/javascript/benchmarks/BuiltinBenchmark.java
index 2e943bb443..f1941411ed 100644
--- a/benchmarks/src/jmh/java/org/mozilla/javascript/benchmarks/BuiltinBenchmark.java
+++ b/benchmarks/src/jmh/java/org/mozilla/javascript/benchmarks/BuiltinBenchmark.java
@@ -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)
@@ -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);
diff --git a/examples/src/main/java/PrimitiveWrapFactory.java b/examples/src/main/java/PrimitiveWrapFactory.java
index fcb786c9e5..c99845607f 100644
--- a/examples/src/main/java/PrimitiveWrapFactory.java
+++ b/examples/src/main/java/PrimitiveWrapFactory.java
@@ -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
@@ -21,7 +21,7 @@
*
*
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) {
diff --git a/rhino-engine/src/main/java/org/mozilla/javascript/engine/RhinoScriptEngine.java b/rhino-engine/src/main/java/org/mozilla/javascript/engine/RhinoScriptEngine.java
index d49dd44e9b..408fd208e9 100644
--- a/rhino-engine/src/main/java/org/mozilla/javascript/engine/RhinoScriptEngine.java
+++ b/rhino-engine/src/main/java/org/mozilla/javascript/engine/RhinoScriptEngine.java
@@ -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.
@@ -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 {
diff --git a/rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/Dim.java b/rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/Dim.java
index 7749dd13aa..c4e5455c09 100644
--- a/rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/Dim.java
+++ b/rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/Dim.java
@@ -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;
@@ -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 {
diff --git a/rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Global.java b/rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Global.java
index 0d14b405be..13cfefbc0b 100644
--- a/rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Global.java
+++ b/rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Global.java
@@ -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;
@@ -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;
@@ -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 = {
diff --git a/rhino/src/main/java/module-info.java b/rhino/src/main/java/module-info.java
index 2aea58119a..44a6663281 100644
--- a/rhino/src/main/java/module-info.java
+++ b/rhino/src/main/java/module-info.java
@@ -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;
diff --git a/rhino/src/main/java/org/mozilla/javascript/ArrowFunction.java b/rhino/src/main/java/org/mozilla/javascript/ArrowFunction.java
index 45044f8856..cc14547e1c 100644
--- a/rhino/src/main/java/org/mozilla/javascript/ArrowFunction.java
+++ b/rhino/src/main/java/org/mozilla/javascript/ArrowFunction.java
@@ -69,7 +69,7 @@ public int getArity() {
}
@Override
- String decompile(int indent, EnumSet flags) {
+ protected String decompile(int indent, EnumSet flags) {
if (targetFunction instanceof BaseFunction) {
return ((BaseFunction) targetFunction).decompile(indent, flags);
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java b/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java
index 5229ac4d7e..6f8fba35b6 100644
--- a/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java
+++ b/rhino/src/main/java/org/mozilla/javascript/BaseFunction.java
@@ -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 flags) {
+ protected String decompile(int indent, EnumSet flags) {
StringBuilder sb = new StringBuilder();
boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY);
if (!justbody) {
diff --git a/rhino/src/main/java/org/mozilla/javascript/Context.java b/rhino/src/main/java/org/mozilla/javascript/Context.java
index 95c73d6a5d..f1e135300f 100644
--- a/rhino/src/main/java/org/mozilla/javascript/Context.java
+++ b/rhino/src/main/java/org/mozilla/javascript/Context.java
@@ -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;
/**
@@ -637,7 +638,7 @@ public final void unseal(Object sealKey) {
}
@SuppressWarnings("DoNotCallSuggester")
- static void onSealedMutation() {
+ public static void onSealedMutation() {
throw new IllegalStateException();
}
@@ -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);
}
@@ -1749,7 +1750,8 @@ 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());
@@ -1757,6 +1759,9 @@ public static Object javaToJS(Object value, Scriptable scope, Context cx) {
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);
}
}
@@ -2046,7 +2051,7 @@ public final synchronized void setClassShutter(ClassShutter shutter) {
hasClassShutter = true;
}
- final synchronized ClassShutter getClassShutter() {
+ public final synchronized ClassShutter getClassShutter() {
return classShutter;
}
@@ -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.
*
@@ -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;
}
@@ -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");
diff --git a/rhino/src/main/java/org/mozilla/javascript/EqualObjectGraphs.java b/rhino/src/main/java/org/mozilla/javascript/EqualObjectGraphs.java
index 559e592d89..95ca5e9248 100644
--- a/rhino/src/main/java/org/mozilla/javascript/EqualObjectGraphs.java
+++ b/rhino/src/main/java/org/mozilla/javascript/EqualObjectGraphs.java
@@ -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 instance = new ThreadLocal<>();
private static final Set> valueClasses =
@@ -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) {
@@ -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.
@@ -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
@@ -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 {}
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java b/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java
index 0417c0ed54..da13c28189 100644
--- a/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java
+++ b/rhino/src/main/java/org/mozilla/javascript/IdFunctionObject.java
@@ -98,7 +98,7 @@ public Scriptable createObject(Context cx, Scriptable scope) {
}
@Override
- String decompile(int indent, EnumSet flags) {
+ protected String decompile(int indent, EnumSet flags) {
StringBuilder sb = new StringBuilder();
boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY);
if (!justbody) {
diff --git a/rhino/src/main/java/org/mozilla/javascript/InterfaceAdapter.java b/rhino/src/main/java/org/mozilla/javascript/InterfaceAdapter.java
index 513101920b..016611728e 100644
--- a/rhino/src/main/java/org/mozilla/javascript/InterfaceAdapter.java
+++ b/rhino/src/main/java/org/mozilla/javascript/InterfaceAdapter.java
@@ -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
@@ -24,7 +25,7 @@ public class InterfaceAdapter {
* @return The glue object or null if cl
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);
diff --git a/rhino/src/main/java/org/mozilla/javascript/LazilyLoadedCtor.java b/rhino/src/main/java/org/mozilla/javascript/LazilyLoadedCtor.java
index b069bcfafc..602e953b32 100644
--- a/rhino/src/main/java/org/mozilla/javascript/LazilyLoadedCtor.java
+++ b/rhino/src/main/java/org/mozilla/javascript/LazilyLoadedCtor.java
@@ -35,7 +35,7 @@ public LazilyLoadedCtor(
this(scope, propertyName, className, sealed, false);
}
- LazilyLoadedCtor(
+ public LazilyLoadedCtor(
ScriptableObject scope,
String propertyName,
String className,
diff --git a/rhino/src/main/java/org/mozilla/javascript/MemberBox.java b/rhino/src/main/java/org/mozilla/javascript/MemberBox.java
index 1f6ab40c14..52ca594a64 100644
--- a/rhino/src/main/java/org/mozilla/javascript/MemberBox.java
+++ b/rhino/src/main/java/org/mozilla/javascript/MemberBox.java
@@ -22,7 +22,7 @@
*
* @author Igor Bukanov
*/
-final class MemberBox implements Serializable {
+public final class MemberBox implements Serializable {
private static final long serialVersionUID = 6358550398665688245L;
private transient Member memberObject;
@@ -33,11 +33,11 @@ final class MemberBox implements Serializable {
transient Function asSetterFunction;
transient Object delegateTo;
- MemberBox(Method method) {
+ public MemberBox(Method method) {
init(method);
}
- MemberBox(Constructor> constructor) {
+ public MemberBox(Constructor> constructor) {
init(constructor);
}
@@ -53,11 +53,11 @@ private void init(Constructor> constructor) {
this.vararg = constructor.isVarArgs();
}
- Method method() {
+ public Method method() {
return (Method) memberObject;
}
- Constructor> ctor() {
+ public Constructor> ctor() {
return (Constructor>) memberObject;
}
@@ -65,55 +65,43 @@ Member member() {
return memberObject;
}
- boolean isMethod() {
+ public boolean isMethod() {
return memberObject instanceof Method;
}
- boolean isCtor() {
+ public boolean isCtor() {
return memberObject instanceof Constructor;
}
- boolean isStatic() {
+ public boolean isStatic() {
return Modifier.isStatic(memberObject.getModifiers());
}
- boolean isPublic() {
+ public boolean isPublic() {
return Modifier.isPublic(memberObject.getModifiers());
}
- String getName() {
+ public String getName() {
return memberObject.getName();
}
- Class> getDeclaringClass() {
+ public Class> getDeclaringClass() {
return memberObject.getDeclaringClass();
}
- String toJavaDeclaration() {
- StringBuilder sb = new StringBuilder();
- if (isMethod()) {
- Method method = method();
- sb.append(method.getReturnType());
- sb.append(' ');
- sb.append(method.getName());
- } else {
- Constructor> ctor = ctor();
- String name = ctor.getDeclaringClass().getName();
- int lastDot = name.lastIndexOf('.');
- if (lastDot >= 0) {
- name = name.substring(lastDot + 1);
- }
- sb.append(name);
- }
- sb.append(JavaMembers.liveConnectSignature(argTypes));
- return sb.toString();
- }
-
@Override
public String toString() {
return memberObject.toString();
}
+ public Class>[] getArgTypes() {
+ return argTypes;
+ }
+
+ public boolean isVararg() {
+ return vararg;
+ }
+
/** Function returned by calls to __lookupGetter__ */
Function asGetterFunction(final String name, final Scriptable scope) {
// Note: scope is the scriptable this function is related to; therefore this function
@@ -195,7 +183,7 @@ public String getFunctionName() {
return asSetterFunction;
}
- Object invoke(Object target, Object[] args) {
+ public Object invoke(Object target, Object[] args) {
Method method = method();
// handle delegators
@@ -239,7 +227,7 @@ Object invoke(Object target, Object[] args) {
}
}
- Object newInstance(Object[] args) {
+ public Object newInstance(Object[] args) {
Constructor> ctor = ctor();
try {
try {
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeDate.java b/rhino/src/main/java/org/mozilla/javascript/NativeDate.java
index 36588bb20f..a87fef2ca7 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeDate.java
+++ b/rhino/src/main/java/org/mozilla/javascript/NativeDate.java
@@ -24,7 +24,7 @@
* https://dxr.mozilla.org/mozilla-central/source/js/src/jsdate.cpp
*/
@SuppressWarnings("AndroidJdkLibsChecker")
-final class NativeDate extends IdScriptableObject {
+public final class NativeDate extends IdScriptableObject {
private static final long serialVersionUID = -8307438915861678966L;
private static final Object DATE_TAG = "Date";
@@ -50,7 +50,7 @@ public Object getDefaultValue(Class> typeHint) {
return super.getDefaultValue(typeHint);
}
- double getJSTimeValue() {
+ public double getJSTimeValue() {
return date;
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeFunction.java b/rhino/src/main/java/org/mozilla/javascript/NativeFunction.java
index 4b61c9439c..1bb5031b8f 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeFunction.java
+++ b/rhino/src/main/java/org/mozilla/javascript/NativeFunction.java
@@ -32,7 +32,7 @@ public final void initScriptFunction(
* @param flags Flags specifying format of decompilation output
*/
@Override
- final String decompile(int indent, EnumSet flags) {
+ protected final String decompile(int indent, EnumSet flags) {
String rawSource = getRawSource();
if (rawSource != null) {
return rawSource;
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeGlobal.java b/rhino/src/main/java/org/mozilla/javascript/NativeGlobal.java
index 38996775b7..ce4ad43f64 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeGlobal.java
+++ b/rhino/src/main/java/org/mozilla/javascript/NativeGlobal.java
@@ -20,7 +20,7 @@
*
* @author Mike Shaver
*/
-public class NativeGlobal implements Serializable, IdFunctionCall {
+public class NativeGlobal implements Serializable, IdFunctionCall, EqualObjectGraphs.Stateless {
static final long serialVersionUID = 6080442165748707530L;
public static void init(Context cx, Scriptable scope, boolean sealed) {
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJSON.java b/rhino/src/main/java/org/mozilla/javascript/NativeJSON.java
index c136ad3f2d..91ecc7f0de 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJSON.java
+++ b/rhino/src/main/java/org/mozilla/javascript/NativeJSON.java
@@ -305,8 +305,8 @@ private static Object str(Object key, Scriptable holder, StringifyState state) {
} else if (state.cx.getLanguageVersion() >= Context.VERSION_ES6
&& value instanceof NativeBigInt) {
value = ((NativeBigInt) value).getDefaultValue(ScriptRuntime.BigIntegerClass);
- } else if (value instanceof NativeJavaObject) {
- unwrappedJavaValue = ((NativeJavaObject) value).unwrap();
+ } else if (value instanceof Wrapper) {
+ unwrappedJavaValue = ((Wrapper) value).unwrap();
if (!(unwrappedJavaValue instanceof Map
|| unwrappedJavaValue instanceof Collection
|| unwrappedJavaValue.getClass().isArray())) {
@@ -594,8 +594,8 @@ private static boolean isObjectArrayLike(Object o) {
if (o instanceof NativeArray) {
return true;
}
- if (o instanceof NativeJavaObject) {
- Object unwrapped = ((NativeJavaObject) o).unwrap();
+ if (o instanceof Wrapper) {
+ Object unwrapped = ((Wrapper) o).unwrap();
return (unwrapped instanceof Collection) || unwrapped.getClass().isArray();
}
return false;
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeScript.java b/rhino/src/main/java/org/mozilla/javascript/NativeScript.java
index 1084010290..5b719f76c7 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeScript.java
+++ b/rhino/src/main/java/org/mozilla/javascript/NativeScript.java
@@ -72,7 +72,7 @@ public int getArity() {
}
@Override
- String decompile(int indent, EnumSet flags) {
+ protected String decompile(int indent, EnumSet flags) {
if (script instanceof NativeFunction) {
return ((NativeFunction) script).decompile(indent, flags);
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java b/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
index 4e3a97c7d4..cee6261a0d 100644
--- a/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
+++ b/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
@@ -7,7 +7,6 @@
package org.mozilla.javascript;
import java.io.Serializable;
-import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
@@ -19,6 +18,7 @@
import java.util.ResourceBundle;
import java.util.function.BiConsumer;
import org.mozilla.javascript.ast.FunctionNode;
+import org.mozilla.javascript.lc.JavaWrapFactory;
import org.mozilla.javascript.v8dtoa.DoubleConversion;
import org.mozilla.javascript.v8dtoa.FastDtoa;
import org.mozilla.javascript.xml.XMLLib;
@@ -148,7 +148,6 @@ public static ScriptableObject initSafeStandardObjects(
}
scope.associateValue(LIBRARY_SCOPE_KEY, scope);
- new ClassCache().associate(scope);
BaseFunction.init(cx, scope, sealed);
NativeObject.init(scope, sealed);
@@ -189,8 +188,8 @@ public static ScriptableObject initSafeStandardObjects(
NativeArrayIterator.init(scope, sealed);
NativeStringIterator.init(scope, sealed);
- NativeJavaObject.init(scope, sealed);
- NativeJavaMap.init(scope, sealed);
+ // NativeJavaObject.init(scope, sealed);
+ // NativeJavaMap.init(scope, sealed);
boolean withXml =
cx.hasFeature(Context.FEATURE_E4X) && cx.getE4xImplementationFactory() != null;
@@ -303,29 +302,15 @@ public static ScriptableObject initStandardObjects(
Context cx, ScriptableObject scope, boolean sealed) {
ScriptableObject s = initSafeStandardObjects(cx, scope, sealed);
- new LazilyLoadedCtor(
- s, "Packages", "org.mozilla.javascript.NativeJavaTopPackage", sealed, true);
- new LazilyLoadedCtor(
- s, "getClass", "org.mozilla.javascript.NativeJavaTopPackage", sealed, true);
- new LazilyLoadedCtor(s, "JavaAdapter", "org.mozilla.javascript.JavaAdapter", sealed, true);
- new LazilyLoadedCtor(
- s, "JavaImporter", "org.mozilla.javascript.ImporterTopLevel", sealed, true);
-
- for (String packageName : getTopPackageNames()) {
- new LazilyLoadedCtor(
- s, packageName, "org.mozilla.javascript.NativeJavaTopPackage", sealed, true);
+ if (cx.getWrapFactory() == null) {
+ System.err.println("No wrapfactory present");
+ } else {
+ cx.getWrapFactory().initStandardObjects(cx, s, sealed);
}
return s;
}
- static String[] getTopPackageNames() {
- // Include "android" top package if running on Android
- return "Dalvik".equals(System.getProperty("java.vm.name"))
- ? new String[] {"java", "javax", "org", "com", "edu", "net", "android"}
- : new String[] {"java", "javax", "org", "com", "edu", "net"};
- }
-
public static ScriptableObject getLibraryScopeOrNull(Scriptable scope) {
ScriptableObject libScope;
libScope = (ScriptableObject) ScriptableObject.getTopScopeValue(scope, LIBRARY_SCOPE_KEY);
@@ -1303,6 +1288,9 @@ public static Scriptable toObject(Context cx, Scriptable scope, Object val) {
return result;
}
+ if (cx.getWrapFactory() == null) {
+ throw new UnsupportedOperationException("Cannot convert java value " + val + "(" + (val == null ? "null" : val.getClass()) + ") to javascript");
+ }
// Extension: Wrap as a LiveConnect object.
Object wrapped = cx.getWrapFactory().wrap(cx, scope, val, null);
if (wrapped instanceof Scriptable) return (Scriptable) wrapped;
@@ -4098,24 +4086,6 @@ private static boolean compareTo(double d1, double d2, int op) {
// Statements
// ------------------
- public static ScriptableObject getGlobal(Context cx) {
- final String GLOBAL_CLASS = "org.mozilla.javascript.tools.shell.Global";
- Class> globalClass = Kit.classOrNull(GLOBAL_CLASS);
- if (globalClass != null) {
- try {
- Class>[] parm = {ScriptRuntime.ContextClass};
- Constructor> globalClassCtor = globalClass.getConstructor(parm);
- Object[] arg = {cx};
- return (ScriptableObject) globalClassCtor.newInstance(arg);
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- // fall through...
- }
- }
- return new ImporterTopLevel(cx);
- }
-
public static boolean hasTopCall(Context cx) {
return (cx.topCallScope != null);
}
@@ -4403,25 +4373,27 @@ public static Scriptable newCatchScope(
((NativeError) errorObject).setStackProvider(re);
}
- if (javaException != null && isVisible(cx, javaException)) {
- Object wrap = cx.getWrapFactory().wrap(cx, scope, javaException, null);
- ScriptableObject.defineProperty(
- errorObject,
- "javaException",
- wrap,
- ScriptableObject.PERMANENT
- | ScriptableObject.READONLY
- | ScriptableObject.DONTENUM);
- }
- if (isVisible(cx, re)) {
- Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null);
- ScriptableObject.defineProperty(
- errorObject,
- "rhinoException",
- wrap,
- ScriptableObject.PERMANENT
- | ScriptableObject.READONLY
- | ScriptableObject.DONTENUM);
+ if (cx.getWrapFactory() != null) {
+ if (javaException != null && isVisible(cx, javaException)) {
+ Object wrap = cx.getWrapFactory().wrap(cx, scope, javaException, null);
+ ScriptableObject.defineProperty(
+ errorObject,
+ "javaException",
+ wrap,
+ ScriptableObject.PERMANENT
+ | ScriptableObject.READONLY
+ | ScriptableObject.DONTENUM);
+ }
+ if (isVisible(cx, re)) {
+ Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null);
+ ScriptableObject.defineProperty(
+ errorObject,
+ "rhinoException",
+ wrap,
+ ScriptableObject.PERMANENT
+ | ScriptableObject.READONLY
+ | ScriptableObject.DONTENUM);
+ }
}
obj = errorObject;
}
@@ -4501,26 +4473,27 @@ public static Scriptable wrapException(Throwable t, Scriptable scope, Context cx
if (errorObject instanceof NativeError) {
((NativeError) errorObject).setStackProvider(re);
}
-
- if (javaException != null && isVisible(cx, javaException)) {
- Object wrap = cx.getWrapFactory().wrap(cx, scope, javaException, null);
- ScriptableObject.defineProperty(
- errorObject,
- "javaException",
- wrap,
- ScriptableObject.PERMANENT
- | ScriptableObject.READONLY
- | ScriptableObject.DONTENUM);
- }
- if (isVisible(cx, re)) {
- Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null);
- ScriptableObject.defineProperty(
- errorObject,
- "rhinoException",
- wrap,
- ScriptableObject.PERMANENT
- | ScriptableObject.READONLY
- | ScriptableObject.DONTENUM);
+ if (cx.getWrapFactory() != null) {
+ if (javaException != null && isVisible(cx, javaException)) {
+ Object wrap = cx.getWrapFactory().wrap(cx, scope, javaException, null);
+ ScriptableObject.defineProperty(
+ errorObject,
+ "javaException",
+ wrap,
+ ScriptableObject.PERMANENT
+ | ScriptableObject.READONLY
+ | ScriptableObject.DONTENUM);
+ }
+ if (isVisible(cx, re)) {
+ Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null);
+ ScriptableObject.defineProperty(
+ errorObject,
+ "rhinoException",
+ wrap,
+ ScriptableObject.PERMANENT
+ | ScriptableObject.READONLY
+ | ScriptableObject.DONTENUM);
+ }
}
return errorObject;
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java b/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java
index 8fc9cd5eca..d0529e1796 100644
--- a/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java
+++ b/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java
@@ -945,7 +945,7 @@ protected Object equivalentValues(Object value) {
* @exception InvocationTargetException if an exception is thrown during execution of methods of
* the named class
* @see org.mozilla.javascript.Function
- * @see org.mozilla.javascript.FunctionObject
+ * @see FunctionObject
* @see org.mozilla.javascript.ScriptableObject#READONLY
* @see org.mozilla.javascript.ScriptableObject #defineProperty(String, Class, int)
*/
@@ -1913,7 +1913,7 @@ protected boolean isGenericDescriptor(ScriptableObject desc) {
return !isDataDescriptor(desc) && !isAccessorDescriptor(desc);
}
- protected static Scriptable ensureScriptable(Object arg) {
+ public static Scriptable ensureScriptable(Object arg) {
if (!(arg instanceof Scriptable))
throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(arg));
return (Scriptable) arg;
@@ -1926,7 +1926,7 @@ protected static SymbolScriptable ensureSymbolScriptable(Object arg) {
return (SymbolScriptable) arg;
}
- protected static ScriptableObject ensureScriptableObject(Object arg) {
+ public static ScriptableObject ensureScriptableObject(Object arg) {
if (arg instanceof ScriptableObject) {
return (ScriptableObject) arg;
}
@@ -1946,7 +1946,7 @@ protected static ScriptableObject ensureScriptableObject(Object arg) {
* @param names the names of the Methods to add as function properties
* @param clazz the class to search for the Methods
* @param attributes the attributes of the new properties
- * @see org.mozilla.javascript.FunctionObject
+ * @see FunctionObject
*/
public void defineFunctionProperties(String[] names, Class> clazz, int attributes) {
Method[] methods = FunctionObject.getMethodList(clazz);
diff --git a/rhino/src/main/java/org/mozilla/javascript/WrapFactory.java b/rhino/src/main/java/org/mozilla/javascript/WrapFactory.java
index 09f2859995..e162bd2ae6 100644
--- a/rhino/src/main/java/org/mozilla/javascript/WrapFactory.java
+++ b/rhino/src/main/java/org/mozilla/javascript/WrapFactory.java
@@ -1,17 +1,5 @@
-/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-// API class
-
package org.mozilla.javascript;
-import java.math.BigInteger;
-import java.util.List;
-import java.util.Map;
-
/**
* Embeddings that wish to provide their own custom wrappings for Java objects may extend this class
* and call {@link Context#setWrapFactory(WrapFactory)} Once an instance of this class or an
@@ -22,7 +10,10 @@
* @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory)
* @since 1.5 Release 4
*/
-public class WrapFactory {
+public interface WrapFactory {
+
+ void initStandardObjects(Context cx, ScriptableObject scope, boolean sealed);
+
/**
* Wrap the object.
*
@@ -44,36 +35,7 @@ public class WrapFactory {
* class, staticType will be used instead.
* @return the wrapped value.
*/
- public Object wrap(Context cx, Scriptable scope, Object obj, Class> staticType) {
- if (obj == null || obj == Undefined.instance || obj instanceof Scriptable) {
- return obj;
- }
- if (staticType != null && staticType.isPrimitive()) {
- if (staticType == Void.TYPE) return Undefined.instance;
- if (staticType == Character.TYPE) return Integer.valueOf(((Character) obj).charValue());
- return obj;
- }
- if (!isJavaPrimitiveWrap()) {
- if (obj instanceof String
- || obj instanceof Boolean
- || obj instanceof Integer
- || obj instanceof Byte
- || obj instanceof Short
- || obj instanceof Long
- || obj instanceof Float
- || obj instanceof Double
- || obj instanceof BigInteger) {
- return obj;
- } else if (obj instanceof Character) {
- return String.valueOf(((Character) obj).charValue());
- }
- }
- Class> cls = obj.getClass();
- if (cls.isArray()) {
- return NativeJavaArray.wrap(scope, obj);
- }
- return wrapAsJavaObject(cx, scope, obj, staticType);
- }
+ Object wrap(Context cx, Scriptable scope, Object obj, Class> staticType);
/**
* Wrap an object newly created by a constructor call.
@@ -83,16 +45,7 @@ public Object wrap(Context cx, Scriptable scope, Object obj, Class> staticType
* @param obj the object to be wrapped
* @return the wrapped value.
*/
- public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) {
- if (obj instanceof Scriptable) {
- return (Scriptable) obj;
- }
- Class> cls = obj.getClass();
- if (cls.isArray()) {
- return NativeJavaArray.wrap(scope, obj);
- }
- return wrapAsJavaObject(cx, scope, obj, null);
- }
+ Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj);
/**
* Wrap Java object as Scriptable instance to allow full access to its methods and fields from
@@ -111,15 +64,8 @@ public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) {
* class, staticType will be used instead.
* @return the wrapped value which shall not be null
*/
- public Scriptable wrapAsJavaObject(
- Context cx, Scriptable scope, Object javaObject, Class> staticType) {
- if (List.class.isAssignableFrom(javaObject.getClass())) {
- return new NativeJavaList(scope, javaObject);
- } else if (Map.class.isAssignableFrom(javaObject.getClass())) {
- return new NativeJavaMap(scope, javaObject);
- }
- return new NativeJavaObject(scope, javaObject, staticType);
- }
+ Scriptable wrapAsJavaObject(
+ Context cx, Scriptable scope, Object javaObject, Class> staticType);
/**
* Wrap a Java class as Scriptable instance to allow access to its static members and fields and
@@ -133,32 +79,5 @@ public Scriptable wrapAsJavaObject(
* @return the wrapped value which shall not be null
* @since 1.7R3
*/
- public Scriptable wrapJavaClass(Context cx, Scriptable scope, Class> javaClass) {
- return new NativeJavaClass(scope, javaClass);
- }
-
- /**
- * Return false
if result of Java method, which is instance of String
,
- * Number
, Boolean
and Character
, should be used directly
- * as JavaScript primitive type. By default the method returns true to indicate that instances
- * of String
, Number
, Boolean
and Character
- * should be wrapped as any other Java object and scripts can access any Java method available
- * in these objects. Use {@link #setJavaPrimitiveWrap(boolean)} to change this.
- */
- public final boolean isJavaPrimitiveWrap() {
- return javaPrimitiveWrap;
- }
-
- /**
- * @see #isJavaPrimitiveWrap()
- */
- public final void setJavaPrimitiveWrap(boolean value) {
- Context cx = Context.getCurrentContext();
- if (cx != null && cx.isSealed()) {
- Context.onSealedMutation();
- }
- javaPrimitiveWrap = value;
- }
-
- private boolean javaPrimitiveWrap = true;
+ Scriptable wrapJavaClass(Context cx, Scriptable scope, Class> javaClass);
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/ClassCache.java b/rhino/src/main/java/org/mozilla/javascript/lc/ClassCache.java
similarity index 96%
rename from rhino/src/main/java/org/mozilla/javascript/ClassCache.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/ClassCache.java
index c9e326d61e..ae032e1142 100644
--- a/rhino/src/main/java/org/mozilla/javascript/ClassCache.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/ClassCache.java
@@ -4,12 +4,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
/**
* Cache of generated classes and data structures to access Java runtime from JavaScript.
@@ -177,11 +179,11 @@ public final synchronized int newClassSerialNumber() {
return ++generatedClassSerial;
}
- Object getInterfaceAdapter(Class> cl) {
+ public Object getInterfaceAdapter(Class> cl) {
return interfaceAdapterCache == null ? null : interfaceAdapterCache.get(cl);
}
- synchronized void cacheInterfaceAdapter(Class> cl, Object iadapter) {
+ public synchronized void cacheInterfaceAdapter(Class> cl, Object iadapter) {
if (cachingIsEnabled) {
if (interfaceAdapterCache == null) {
interfaceAdapterCache = new ConcurrentHashMap<>(16, 0.75f, 1);
diff --git a/rhino/src/main/java/org/mozilla/javascript/ImporterTopLevel.java b/rhino/src/main/java/org/mozilla/javascript/lc/ImporterTopLevel.java
similarity index 96%
rename from rhino/src/main/java/org/mozilla/javascript/ImporterTopLevel.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/ImporterTopLevel.java
index 2a28f116cb..38315a12cf 100644
--- a/rhino/src/main/java/org/mozilla/javascript/ImporterTopLevel.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/ImporterTopLevel.java
@@ -6,9 +6,16 @@
// API class
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.util.ArrayList;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.IdFunctionObject;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.TopLevel;
+import org.mozilla.javascript.Undefined;
/**
* Class ImporterTopLevel
diff --git a/rhino/src/main/java/org/mozilla/javascript/JavaAdapter.java b/rhino/src/main/java/org/mozilla/javascript/lc/JavaAdapter.java
similarity index 97%
rename from rhino/src/main/java/org/mozilla/javascript/JavaAdapter.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/JavaAdapter.java
index 168300fec7..d549f458ac 100644
--- a/rhino/src/main/java/org/mozilla/javascript/JavaAdapter.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/JavaAdapter.java
@@ -4,7 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -25,8 +25,25 @@
import java.util.Set;
import org.mozilla.classfile.ByteCode;
import org.mozilla.classfile.ClassFileWriter;
-
-public final class JavaAdapter implements IdFunctionCall {
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
+import org.mozilla.javascript.EqualObjectGraphs;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.GeneratedClassLoader;
+import org.mozilla.javascript.IdFunctionCall;
+import org.mozilla.javascript.IdFunctionObject;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.NativeObject;
+import org.mozilla.javascript.Script;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.SecurityController;
+import org.mozilla.javascript.SecurityUtilities;
+import org.mozilla.javascript.Undefined;
+import org.mozilla.javascript.Wrapper;
+
+public final class JavaAdapter implements IdFunctionCall, EqualObjectGraphs.Stateless {
/**
* Provides a key with which to distinguish previously generated adapter classes stored in a
* hash table.
@@ -576,7 +593,7 @@ public static Scriptable runScript(final Script script) {
return ContextFactory.getGlobal()
.call(
cx -> {
- ScriptableObject global = ScriptRuntime.getGlobal(cx);
+ ScriptableObject global = LiveConnectRuntime.getGlobal(cx);
script.exec(cx, global);
return global;
});
@@ -642,7 +659,7 @@ private static void generateCtor(
cfw.add(ByteCode.ALOAD_0); // this
cfw.addInvoke(
ByteCode.INVOKESTATIC,
- "org/mozilla/javascript/JavaAdapter",
+ "org/mozilla/javascript/lc/JavaAdapter",
"createAdapterWrapper",
"(Lorg/mozilla/javascript/Scriptable;"
+ "Ljava/lang/Object;"
@@ -714,7 +731,7 @@ private static void generateEmptyCtor(
// Run script and save resulting scope
cfw.addInvoke(
ByteCode.INVOKESTATIC,
- "org/mozilla/javascript/JavaAdapter",
+ "org/mozilla/javascript/lc/JavaAdapter",
"runScript",
"(Lorg/mozilla/javascript/Script;" + ")Lorg/mozilla/javascript/Scriptable;");
cfw.add(ByteCode.ASTORE_1);
@@ -730,7 +747,7 @@ private static void generateEmptyCtor(
cfw.add(ByteCode.ALOAD_0); // this
cfw.addInvoke(
ByteCode.INVOKESTATIC,
- "org/mozilla/javascript/JavaAdapter",
+ "org/mozilla/javascript/lc/JavaAdapter",
"createAdapterWrapper",
"(Lorg/mozilla/javascript/Scriptable;"
+ "Ljava/lang/Object;"
@@ -889,7 +906,7 @@ static void generateReturnResult(
cfw.addInvoke(
ByteCode.INVOKESTATIC,
- "org/mozilla/javascript/JavaAdapter",
+ "org/mozilla/javascript/lc/JavaAdapter",
"convertResult",
"(Ljava/lang/Object;" + "Ljava/lang/Class;" + ")Ljava/lang/Object;");
}
@@ -927,7 +944,7 @@ private static void generateMethod(
cfw.addPush(methodName);
cfw.addInvoke(
ByteCode.INVOKESTATIC,
- "org/mozilla/javascript/JavaAdapter",
+ "org/mozilla/javascript/lc/JavaAdapter",
"getFunction",
"(Lorg/mozilla/javascript/Scriptable;"
+ "Ljava/lang/String;"
@@ -955,7 +972,7 @@ private static void generateMethod(
// method in.
cfw.addInvoke(
ByteCode.INVOKESTATIC,
- "org/mozilla/javascript/JavaAdapter",
+ "org/mozilla/javascript/lc/JavaAdapter",
"callMethod",
"(Lorg/mozilla/javascript/ContextFactory;"
+ "Lorg/mozilla/javascript/Scriptable;"
diff --git a/rhino/src/main/java/org/mozilla/javascript/JavaMembers.java b/rhino/src/main/java/org/mozilla/javascript/lc/JavaMembers.java
similarity index 98%
rename from rhino/src/main/java/org/mozilla/javascript/JavaMembers.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/JavaMembers.java
index 8539710351..9ed0a87d95 100644
--- a/rhino/src/main/java/org/mozilla/javascript/JavaMembers.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/JavaMembers.java
@@ -4,7 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import static java.lang.reflect.Modifier.isProtected;
import static java.lang.reflect.Modifier.isPublic;
@@ -24,6 +24,14 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import org.mozilla.javascript.ClassShutter;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.MemberBox;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
/**
* @author Mike Shaver
@@ -139,7 +147,7 @@ void put(Scriptable scope, String name, Object javaObject, Object value, boolean
// main setter. Otherwise, let the NativeJavaMethod decide which
// setter to use:
if (bp.setters == null || value == null) {
- Class> setType = bp.setter.argTypes[0];
+ Class> setType = bp.setter.getArgTypes()[0];
Object[] args = {Context.jsToJava(value, setType)};
try {
bp.setter.invoke(javaObject, args);
@@ -255,7 +263,7 @@ private MemberBox findExplicitFunction(String name, boolean isStatic) {
if (methodsOrCtors != null) {
for (MemberBox methodsOrCtor : methodsOrCtors) {
- Class>[] type = methodsOrCtor.argTypes;
+ Class>[] type = methodsOrCtor.getArgTypes();
String sig = liveConnectSignature(type);
if (sigStart + sig.length() == name.length()
&& name.regionMatches(sigStart, sig, 0, sig.length())) {
@@ -708,7 +716,7 @@ private static MemberBox extractGetMethod(MemberBox[] methods, boolean isStatic)
for (MemberBox method : methods) {
// Does getter method have an empty parameter list with a return
// value (eg. a getSomething() or isSomething())?
- if (method.argTypes.length == 0 && (!isStatic || method.isStatic())) {
+ if (method.getArgTypes().length == 0 && (!isStatic || method.isStatic())) {
Class> type = method.method().getReturnType();
if (type != Void.TYPE) {
return method;
@@ -732,7 +740,7 @@ private static MemberBox extractSetMethod(
for (int pass = 1; pass <= 2; ++pass) {
for (MemberBox method : methods) {
if (!isStatic || method.isStatic()) {
- Class>[] params = method.argTypes;
+ Class>[] params = method.getArgTypes();
if (params.length == 1) {
if (pass == 1) {
if (params[0] == type) {
@@ -756,7 +764,7 @@ private static MemberBox extractSetMethod(MemberBox[] methods, boolean isStatic)
for (MemberBox method : methods) {
if (!isStatic || method.isStatic()) {
if (method.method().getReturnType() == Void.TYPE) {
- if (method.argTypes.length == 1) {
+ if (method.getArgTypes().length == 1) {
return method;
}
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/JavaMembers_jdk11.java b/rhino/src/main/java/org/mozilla/javascript/lc/JavaMembers_jdk11.java
similarity index 98%
rename from rhino/src/main/java/org/mozilla/javascript/JavaMembers_jdk11.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/JavaMembers_jdk11.java
index b7ed6d5d0e..8ca4d282a7 100644
--- a/rhino/src/main/java/org/mozilla/javascript/JavaMembers_jdk11.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/JavaMembers_jdk11.java
@@ -4,12 +4,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
+import org.mozilla.javascript.Scriptable;
/** Version of {@link JavaMembers} for modular JDKs. */
class JavaMembers_jdk11 extends JavaMembers {
diff --git a/rhino/src/main/java/org/mozilla/javascript/lc/JavaWrapFactory.java b/rhino/src/main/java/org/mozilla/javascript/lc/JavaWrapFactory.java
new file mode 100644
index 0000000000..c5d08ac9f7
--- /dev/null
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/JavaWrapFactory.java
@@ -0,0 +1,132 @@
+/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// API class
+
+package org.mozilla.javascript.lc;
+
+import java.math.BigInteger;
+import java.util.List;
+import java.util.Map;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.LazilyLoadedCtor;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Undefined;
+import org.mozilla.javascript.WrapFactory;
+
+/** WrapFactory for LiveConnect */
+public class JavaWrapFactory implements WrapFactory {
+
+ @Override
+ public Object wrap(Context cx, Scriptable scope, Object obj, Class> staticType) {
+ if (obj == null || obj == Undefined.instance || obj instanceof Scriptable) {
+ return obj;
+ }
+ if (staticType != null && staticType.isPrimitive()) {
+ if (staticType == Void.TYPE) return Undefined.instance;
+ if (staticType == Character.TYPE) return Integer.valueOf(((Character) obj).charValue());
+ return obj;
+ }
+ if (!isJavaPrimitiveWrap()) {
+ if (obj instanceof String
+ || obj instanceof Boolean
+ || obj instanceof Integer
+ || obj instanceof Byte
+ || obj instanceof Short
+ || obj instanceof Long
+ || obj instanceof Float
+ || obj instanceof Double
+ || obj instanceof BigInteger) {
+ return obj;
+ } else if (obj instanceof Character) {
+ return String.valueOf(((Character) obj).charValue());
+ }
+ }
+ Class> cls = obj.getClass();
+ if (cls.isArray()) {
+ return NativeJavaArray.wrap(scope, obj);
+ }
+ return wrapAsJavaObject(cx, scope, obj, staticType);
+ }
+
+ @Override
+ public void initStandardObjects(Context cx, ScriptableObject scope, boolean sealed) {
+ NativeJavaObject.init(scope, sealed);
+ NativeJavaMap.init(scope, sealed);
+ new ClassCache().associate(scope);
+ new LazilyLoadedCtor(
+ scope, "Packages", "org.mozilla.javascript.lc.NativeJavaTopPackage", sealed, true);
+ new LazilyLoadedCtor(
+ scope, "getClass", "org.mozilla.javascript.lc.NativeJavaTopPackage", sealed, true);
+ new LazilyLoadedCtor(
+ scope, "JavaAdapter", "org.mozilla.javascript.lc.JavaAdapter", sealed, true);
+ new LazilyLoadedCtor(
+ scope, "JavaImporter", "org.mozilla.javascript.lc.ImporterTopLevel", sealed, true);
+
+ for (String packageName : NativeJavaTopPackage.getTopPackageNames()) {
+ new LazilyLoadedCtor(
+ scope,
+ packageName,
+ "org.mozilla.javascript.lc.NativeJavaTopPackage",
+ sealed,
+ true);
+ }
+ }
+
+ @Override
+ public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) {
+ if (obj instanceof Scriptable) {
+ return (Scriptable) obj;
+ }
+ Class> cls = obj.getClass();
+ if (cls.isArray()) {
+ return NativeJavaArray.wrap(scope, obj);
+ }
+ return wrapAsJavaObject(cx, scope, obj, null);
+ }
+
+ @Override
+ public Scriptable wrapAsJavaObject(
+ Context cx, Scriptable scope, Object javaObject, Class> staticType) {
+ if (List.class.isAssignableFrom(javaObject.getClass())) {
+ return new NativeJavaList(scope, javaObject);
+ } else if (Map.class.isAssignableFrom(javaObject.getClass())) {
+ return new NativeJavaMap(scope, javaObject);
+ }
+ return new NativeJavaObject(scope, javaObject, staticType);
+ }
+
+ @Override
+ public Scriptable wrapJavaClass(Context cx, Scriptable scope, Class> javaClass) {
+ return new NativeJavaClass(scope, javaClass);
+ }
+
+ /**
+ * Return false
if result of Java method, which is instance of String
,
+ * Number
, Boolean
and Character
, should be used directly
+ * as JavaScript primitive type. By default the method returns true to indicate that instances
+ * of String
, Number
, Boolean
and Character
+ * should be wrapped as any other Java object and scripts can access any Java method available
+ * in these objects. Use {@link #setJavaPrimitiveWrap(boolean)} to change this.
+ */
+ public final boolean isJavaPrimitiveWrap() {
+ return javaPrimitiveWrap;
+ }
+
+ /**
+ * @see #isJavaPrimitiveWrap()
+ */
+ public final void setJavaPrimitiveWrap(boolean value) {
+ Context cx = Context.getCurrentContext();
+ if (cx != null && cx.isSealed()) {
+ Context.onSealedMutation();
+ }
+ javaPrimitiveWrap = value;
+ }
+
+ private boolean javaPrimitiveWrap = true;
+}
diff --git a/rhino/src/main/java/org/mozilla/javascript/lc/LiveConnectRuntime.java b/rhino/src/main/java/org/mozilla/javascript/lc/LiveConnectRuntime.java
new file mode 100644
index 0000000000..6530608f79
--- /dev/null
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/LiveConnectRuntime.java
@@ -0,0 +1,50 @@
+package org.mozilla.javascript.lc;
+
+import java.lang.reflect.Constructor;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.Script;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+
+/**
+ * @author Roland Praml, Foconis Analytics GmbH
+ */
+public class LiveConnectRuntime {
+ public static ScriptableObject getGlobal(Context cx) {
+ final String GLOBAL_CLASS = "org.mozilla.javascript.tools.shell.Global";
+ Class> globalClass = Kit.classOrNull(GLOBAL_CLASS);
+ if (globalClass != null) {
+ try {
+ Class>[] parm = {ScriptRuntime.ContextClass};
+ Constructor> globalClassCtor = globalClass.getConstructor(parm);
+ Object[] arg = {cx};
+ return (ScriptableObject) globalClassCtor.newInstance(arg);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ // fall through...
+ }
+ }
+ return new ImporterTopLevel(cx);
+ }
+
+ public static void main(final Script script, final String[] args) {
+ ContextFactory.getGlobal()
+ .call(
+ cx -> {
+ ScriptableObject global = getGlobal(cx);
+
+ // get the command line arguments and define "arguments"
+ // array in the top-level object
+ Object[] argsCopy = new Object[args.length];
+ System.arraycopy(args, 0, argsCopy, 0, args.length);
+ Scriptable argsObj = cx.newArray(global, argsCopy);
+ global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
+ script.exec(cx, global);
+ return null;
+ });
+ }
+}
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaArray.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaArray.java
similarity index 90%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaArray.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaArray.java
index 2253ba9dc6..c43e15bd6c 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaArray.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaArray.java
@@ -4,10 +4,19 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.lang.reflect.Array;
import java.util.Objects;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Symbol;
+import org.mozilla.javascript.SymbolKey;
+import org.mozilla.javascript.SymbolScriptable;
+import org.mozilla.javascript.Undefined;
+import org.mozilla.javascript.Wrapper;
/**
* This class reflects Java arrays into the JavaScript environment.
@@ -64,7 +73,7 @@ public boolean has(Symbol key, Scriptable start) {
public Object get(String id, Scriptable start) {
if (id.equals("length")) return Integer.valueOf(length);
Object result = super.get(id, start);
- if (result == NOT_FOUND && !ScriptableObject.hasProperty(getPrototype(), id)) {
+ if (result == Scriptable.NOT_FOUND && !ScriptableObject.hasProperty(getPrototype(), id)) {
throw Context.reportRuntimeErrorById(
"msg.java.member.not.found", array.getClass().getName(), id);
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaClass.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaClass.java
similarity index 95%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaClass.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaClass.java
index 0e19ac384f..832641e362 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaClass.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaClass.java
@@ -4,11 +4,22 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.Map;
+import org.mozilla.javascript.Constructable;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.MemberBox;
+import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.WrapFactory;
+import org.mozilla.javascript.Wrapper;
/**
* This class reflects Java classes into the JavaScript environment, mainly for constructors and
@@ -189,9 +200,9 @@ static Scriptable constructSpecific(
}
static Object constructInternal(Object[] args, MemberBox ctor) {
- Class>[] argTypes = ctor.argTypes;
+ Class>[] argTypes = ctor.getArgTypes();
- if (ctor.vararg) {
+ if (ctor.isVararg()) {
// marshall the explicit parameter
Object[] newArgs = new Object[argTypes.length];
for (int i = 0; i < argTypes.length - 1; i++) {
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaConstructor.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaConstructor.java
similarity index 83%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaConstructor.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaConstructor.java
index e4f94202fa..528081c7ef 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaConstructor.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaConstructor.java
@@ -4,7 +4,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
+
+import org.mozilla.javascript.BaseFunction;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.MemberBox;
+import org.mozilla.javascript.Scriptable;
/**
* This class reflects a single Java constructor into the JavaScript environment. It satisfies a
@@ -33,7 +38,7 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
@Override
public String getFunctionName() {
- String sig = JavaMembers.liveConnectSignature(ctor.argTypes);
+ String sig = JavaMembers.liveConnectSignature(ctor.getArgTypes());
return "".concat(sig);
}
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaList.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaList.java
similarity index 95%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaList.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaList.java
index f292a6f8d7..9a59d1bf7d 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaList.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaList.java
@@ -3,10 +3,16 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.util.ArrayList;
import java.util.List;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.Symbol;
+import org.mozilla.javascript.SymbolKey;
+import org.mozilla.javascript.Undefined;
/**
* NativeJavaList
is a wrapper for java objects implementing java.util.List
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaMap.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaMap.java
similarity index 94%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaMap.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaMap.java
index 752f2c0edf..2156b4ad3d 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaMap.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaMap.java
@@ -3,12 +3,22 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import org.mozilla.javascript.Callable;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ES6Iterator;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Symbol;
+import org.mozilla.javascript.SymbolKey;
+import org.mozilla.javascript.Undefined;
+import org.mozilla.javascript.WrapFactory;
/**
* NativeJavaMap
is a wrapper for java objects implementing java.util.Map
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaMethod.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaMethod.java
similarity index 91%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaMethod.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaMethod.java
index 4c9112e9d8..a3846bc147 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaMethod.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaMethod.java
@@ -4,13 +4,25 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.concurrent.CopyOnWriteArrayList;
+import org.mozilla.javascript.BaseFunction;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.DecompilerFlag;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.MemberBox;
+import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.Undefined;
+import org.mozilla.javascript.Wrapper;
/**
* This class reflects Java methods into the JavaScript environment and handles overloading of
@@ -87,7 +99,7 @@ static String scriptSignature(Object[] values) {
}
@Override
- String decompile(int indent, EnumSet flags) {
+ protected String decompile(int indent, EnumSet flags) {
StringBuilder sb = new StringBuilder();
boolean justbody = flags.contains(DecompilerFlag.ONLY_BODY);
if (!justbody) {
@@ -114,7 +126,7 @@ public String toString() {
} else {
sb.append(methods[i].getName());
}
- sb.append(JavaMembers.liveConnectSignature(methods[i].argTypes));
+ sb.append(JavaMembers.liveConnectSignature(methods[i].getArgTypes()));
sb.append('\n');
}
return sb.toString();
@@ -135,9 +147,9 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
}
MemberBox meth = methods[index];
- Class>[] argTypes = meth.argTypes;
+ Class>[] argTypes = meth.getArgTypes();
- if (meth.vararg) {
+ if (meth.isVararg()) {
// marshall the explicit parameters
Object[] newArgs = new Object[argTypes.length];
for (int i = 0; i < argTypes.length - 1; i++) {
@@ -267,10 +279,10 @@ static int findFunction(Context cx, MemberBox[] methodsOrCtors, Object[] args) {
return -1;
} else if (methodsOrCtors.length == 1) {
MemberBox member = methodsOrCtors[0];
- Class>[] argTypes = member.argTypes;
+ Class>[] argTypes = member.getArgTypes();
int alength = argTypes.length;
- if (member.vararg) {
+ if (member.isVararg()) {
alength--;
if (alength > args.length) {
return -1;
@@ -297,9 +309,9 @@ static int findFunction(Context cx, MemberBox[] methodsOrCtors, Object[] args) {
search:
for (int i = 0; i < methodsOrCtors.length; i++) {
MemberBox member = methodsOrCtors[i];
- Class>[] argTypes = member.argTypes;
+ Class>[] argTypes = member.getArgTypes();
int alength = argTypes.length;
- if (member.vararg) {
+ if (member.isVararg()) {
alength--;
if (alength > args.length) {
continue search;
@@ -347,9 +359,9 @@ static int findFunction(Context cx, MemberBox[] methodsOrCtors, Object[] args) {
preferSignature(
args,
argTypes,
- member.vararg,
- bestFit.argTypes,
- bestFit.vararg);
+ member.isVararg(),
+ bestFit.getArgTypes(),
+ bestFit.isVararg());
if (preference == PREFERENCE_AMBIGUOUS) {
break;
} else if (preference == PREFERENCE_FIRST_ARG) {
@@ -424,7 +436,7 @@ static int findFunction(Context cx, MemberBox[] methodsOrCtors, Object[] args) {
bestFitIndex = extraBestFits[j];
}
buf.append("\n ");
- buf.append(methodsOrCtors[bestFitIndex].toJavaDeclaration());
+ buf.append(toJavaDeclaration(methodsOrCtors[bestFitIndex]));
}
MemberBox firstFitMember = methodsOrCtors[firstBestFit];
@@ -443,6 +455,26 @@ static int findFunction(Context cx, MemberBox[] methodsOrCtors, Object[] args) {
buf.toString());
}
+ static String toJavaDeclaration(MemberBox memberBox) {
+ StringBuilder sb = new StringBuilder();
+ if (memberBox.isMethod()) {
+ Method method = memberBox.method();
+ sb.append(method.getReturnType());
+ sb.append(' ');
+ sb.append(method.getName());
+ } else {
+ Constructor> ctor = memberBox.ctor();
+ String name = ctor.getDeclaringClass().getName();
+ int lastDot = name.lastIndexOf('.');
+ if (lastDot >= 0) {
+ name = name.substring(lastDot + 1);
+ }
+ sb.append(name);
+ }
+ sb.append(JavaMembers.liveConnectSignature(memberBox.getArgTypes()));
+ return sb.toString();
+ }
+
/** Types are equal */
private static final int PREFERENCE_EQUAL = 0;
@@ -513,7 +545,7 @@ private static void printDebug(String msg, MemberBox member, Object[] args) {
if (member.isMethod()) {
sb.append(member.getName());
}
- sb.append(JavaMembers.liveConnectSignature(member.argTypes));
+ sb.append(JavaMembers.liveConnectSignature(member.getArgTypes()));
sb.append(" for arguments (");
sb.append(scriptSignature(args));
sb.append(')');
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaObject.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaObject.java
similarity index 97%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaObject.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaObject.java
index d0b29162a6..1ba21850ef 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaObject.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaObject.java
@@ -4,7 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -18,6 +18,26 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
+import org.mozilla.javascript.Callable;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ES6Iterator;
+import org.mozilla.javascript.EvaluatorException;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.InterfaceAdapter;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.NativeDate;
+import org.mozilla.javascript.NativeFunction;
+import org.mozilla.javascript.NativeObject;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Symbol;
+import org.mozilla.javascript.SymbolKey;
+import org.mozilla.javascript.SymbolScriptable;
+import org.mozilla.javascript.TopLevel;
+import org.mozilla.javascript.Undefined;
+import org.mozilla.javascript.Wrapper;
/**
* This class reflects non-Array Java objects into the JavaScript environment. It reflect fields
@@ -187,7 +207,7 @@ public Object[] getIds() {
/**
* @deprecated Use {@link Context#getWrapFactory()} together with calling {@link
- * WrapFactory#wrap(Context, Scriptable, Object, Class)}
+ * JavaWrapFactory#wrap(Context, Scriptable, Object, Class)}
*/
@Deprecated
public static Object wrap(Scriptable scope, Object obj, Class> staticType) {
@@ -483,7 +503,7 @@ public static Object coerceType(Class> type, Object value) {
}
/** Type-munging for field setting and method invocation. Conforms to LC3 specification */
- static Object coerceTypeImpl(Class> type, Object value) {
+ public static Object coerceTypeImpl(Class> type, Object value) {
if (value != null && value.getClass() == type) {
return value;
}
@@ -971,7 +991,7 @@ protected String getTag() {
static {
// Reflection in java is verbose
Class>[] sig2 = new Class[2];
- Class> cl = Kit.classOrNull("org.mozilla.javascript.JavaAdapter");
+ Class> cl = Kit.classOrNull("org.mozilla.javascript.lc.JavaAdapter");
if (cl != null) {
try {
sig2[0] = ScriptRuntime.ObjectClass;
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaPackage.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaPackage.java
similarity index 90%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaPackage.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaPackage.java
index 6921e18187..45f8810359 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaPackage.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaPackage.java
@@ -4,12 +4,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashSet;
import java.util.Set;
+import org.mozilla.javascript.ClassShutter;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.EqualObjectGraphs;
+import org.mozilla.javascript.Kit;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.WrapFactory;
/**
* This class reflects Java packages into the JavaScript environment. We lazily reflect classes and
@@ -22,7 +30,7 @@
* @see NativeJavaObject
* @see NativeJavaClass
*/
-public class NativeJavaPackage extends ScriptableObject {
+public class NativeJavaPackage extends ScriptableObject implements EqualObjectGraphs.JavaEquals {
private static final long serialVersionUID = 7445054382212031523L;
NativeJavaPackage(boolean internalUsage, String packageName, ClassLoader classLoader) {
@@ -95,7 +103,7 @@ NativeJavaPackage forcePackage(String name, Scriptable scope) {
return pkg;
}
- synchronized Object getPkgProperty(String name, Scriptable start, boolean createPkg) {
+ public synchronized Object getPkgProperty(String name, Scriptable start, boolean createPkg) {
Object cached = super.get(name, start);
if (cached != NOT_FOUND) return cached;
if (negativeCache != null && negativeCache.contains(name)) {
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeJavaTopPackage.java b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaTopPackage.java
similarity index 87%
rename from rhino/src/main/java/org/mozilla/javascript/NativeJavaTopPackage.java
rename to rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaTopPackage.java
index 24be4e8994..64739423b1 100644
--- a/rhino/src/main/java/org/mozilla/javascript/NativeJavaTopPackage.java
+++ b/rhino/src/main/java/org/mozilla/javascript/lc/NativeJavaTopPackage.java
@@ -4,7 +4,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-package org.mozilla.javascript;
+package org.mozilla.javascript.lc;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.IdFunctionCall;
+import org.mozilla.javascript.IdFunctionObject;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Wrapper;
/**
* This class reflects Java packages into the JavaScript environment. We lazily reflect classes and
@@ -83,7 +92,7 @@ public static void init(Context cx, Scriptable scope, boolean sealed) {
// We want to get a real alias, and not a distinct JavaPackage
// with the same packageName, so that we share classes and top
// that are underneath.
- String[] topNames = ScriptRuntime.getTopPackageNames();
+ String[] topNames = getTopPackageNames();
NativeJavaPackage[] topPackages = new NativeJavaPackage[topNames.length];
for (int i = 0; i < topNames.length; i++) {
topPackages[i] = (NativeJavaPackage) top.get(topNames[i], top);
@@ -103,6 +112,13 @@ public static void init(Context cx, Scriptable scope, boolean sealed) {
}
}
+ static String[] getTopPackageNames() {
+ // Include "android" top package if running on Android
+ return "Dalvik".equals(System.getProperty("java.vm.name"))
+ ? new String[] {"java", "javax", "org", "com", "edu", "net", "android"}
+ : new String[] {"java", "javax", "org", "com", "edu", "net"};
+ }
+
@Override
public Object execIdCall(
IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
diff --git a/rhino/src/main/java/org/mozilla/javascript/optimizer/ClassCompiler.java b/rhino/src/main/java/org/mozilla/javascript/optimizer/ClassCompiler.java
index 5abe7e8841..5c9f5bf092 100644
--- a/rhino/src/main/java/org/mozilla/javascript/optimizer/ClassCompiler.java
+++ b/rhino/src/main/java/org/mozilla/javascript/optimizer/ClassCompiler.java
@@ -9,12 +9,12 @@
import java.util.HashMap;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.IRFactory;
-import org.mozilla.javascript.JavaAdapter;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.ScriptNode;
+import org.mozilla.javascript.lc.JavaAdapter;
/**
* Generates class files from script sources.
diff --git a/rhino/src/main/java/org/mozilla/javascript/optimizer/OptRuntime.java b/rhino/src/main/java/org/mozilla/javascript/optimizer/OptRuntime.java
index 7d5688cbf1..452a62f3e4 100644
--- a/rhino/src/main/java/org/mozilla/javascript/optimizer/OptRuntime.java
+++ b/rhino/src/main/java/org/mozilla/javascript/optimizer/OptRuntime.java
@@ -7,17 +7,14 @@
import org.mozilla.javascript.ArrowFunction;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context;
-import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ES6Generator;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.NativeFunction;
import org.mozilla.javascript.NativeGenerator;
import org.mozilla.javascript.NativeIterator;
-import org.mozilla.javascript.Script;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
-import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
public final class OptRuntime extends ScriptRuntime {
@@ -201,23 +198,6 @@ public static Scriptable newArrayLiteral(
return newArrayLiteral(objects, skipIndexces, cx, scope);
}
- public static void main(final Script script, final String[] args) {
- ContextFactory.getGlobal()
- .call(
- cx -> {
- ScriptableObject global = getGlobal(cx);
-
- // get the command line arguments and define "arguments"
- // array in the top-level object
- Object[] argsCopy = new Object[args.length];
- System.arraycopy(args, 0, argsCopy, 0, args.length);
- Scriptable argsObj = cx.newArray(global, argsCopy);
- global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
- script.exec(cx, global);
- return null;
- });
- }
-
public static void throwStopIteration(Object scope, Object genState) {
Object value = getGeneratorReturnValue(genState);
Object si =
diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/Bug409702Test.java b/rhino/src/test/java/org/mozilla/javascript/tests/Bug409702Test.java
index 649fead8e3..a3c0e95c7e 100644
--- a/rhino/src/test/java/org/mozilla/javascript/tests/Bug409702Test.java
+++ b/rhino/src/test/java/org/mozilla/javascript/tests/Bug409702Test.java
@@ -9,6 +9,7 @@
import org.junit.Test;
import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.lc.JavaWrapFactory;
/**
* See https://bugzilla.mozilla.org/show_bug.cgi?id=409702
@@ -46,6 +47,7 @@ public void adapter() {
Utils.runWithAllOptimizationLevels(
cx -> {
+ cx.setWrapFactory(new JavaWrapFactory());
final Scriptable scope = cx.initStandardObjects();
Object result = cx.evaluateString(scope, source, "source", 1, null);
diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/Bug419940Test.java b/rhino/src/test/java/org/mozilla/javascript/tests/Bug419940Test.java
index 1989db8b39..354510c5cd 100644
--- a/rhino/src/test/java/org/mozilla/javascript/tests/Bug419940Test.java
+++ b/rhino/src/test/java/org/mozilla/javascript/tests/Bug419940Test.java
@@ -8,6 +8,7 @@
import org.junit.Test;
import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.lc.JavaWrapFactory;
/**
* See https://bugzilla.mozilla.org/show_bug.cgi?id=419940
@@ -34,6 +35,7 @@ public void adapter() {
Utils.runWithAllOptimizationLevels(
cx -> {
+ cx.setWrapFactory(new JavaWrapFactory());
Scriptable scope = cx.initStandardObjects();
Object result = cx.evaluateString(scope, source, "source", 1, null);
assertEquals(Integer.valueOf(value), result);
diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/Bug421071Test.java b/rhino/src/test/java/org/mozilla/javascript/tests/Bug421071Test.java
index 50a66b7a13..cbeaf0107e 100644
--- a/rhino/src/test/java/org/mozilla/javascript/tests/Bug421071Test.java
+++ b/rhino/src/test/java/org/mozilla/javascript/tests/Bug421071Test.java
@@ -13,9 +13,9 @@
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
-import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.lc.ImporterTopLevel;
public class Bug421071Test {
private ContextFactory factory;
diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/Bug448816Test.java b/rhino/src/test/java/org/mozilla/javascript/tests/Bug448816Test.java
index 93d87fc977..017d912df4 100644
--- a/rhino/src/test/java/org/mozilla/javascript/tests/Bug448816Test.java
+++ b/rhino/src/test/java/org/mozilla/javascript/tests/Bug448816Test.java
@@ -16,6 +16,7 @@
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.lc.JavaWrapFactory;
/**
* See https://bugzilla.mozilla.org/show_bug.cgi?id=448816
@@ -37,6 +38,7 @@ public void setUp() {
reference.put(Integer.valueOf(1), Integer.valueOf(42));
// get a js object as map
try (Context context = Context.enter()) {
+ context.setWrapFactory(new JavaWrapFactory());
ScriptableObject scope = context.initStandardObjects();
map =
(Map