优化
This commit is contained in:
@@ -5,6 +5,7 @@ import android.content.pm.PackageManager
|
|||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import androidx.annotation.Keep
|
import androidx.annotation.Keep
|
||||||
import com.script.rhino.RhinoScriptEngine
|
import com.script.rhino.RhinoScriptEngine
|
||||||
|
|
||||||
import io.legado.app.BuildConfig
|
import io.legado.app.BuildConfig
|
||||||
import io.legado.app.utils.channel
|
import io.legado.app.utils.channel
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'com.android.library'
|
id 'com.android.library'
|
||||||
|
id 'org.jetbrains.kotlin.android'
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
|
|||||||
+3
-2
@@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'com.android.library'
|
id 'com.android.library'
|
||||||
|
id 'org.jetbrains.kotlin.android'
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
@@ -22,8 +23,8 @@ android {
|
|||||||
|
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility JavaVersion.VERSION_1_8
|
sourceCompatibility JavaVersion.VERSION_11
|
||||||
targetCompatibility JavaVersion.VERSION_1_8
|
targetCompatibility JavaVersion.VERSION_11
|
||||||
}
|
}
|
||||||
lint {
|
lint {
|
||||||
checkDependencies true
|
checkDependencies true
|
||||||
|
|||||||
@@ -22,24 +22,11 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import com.script.ScriptContext
|
||||||
|
import org.mozilla.javascript.*
|
||||||
import com.script.Bindings;
|
import org.mozilla.javascript.Function
|
||||||
import com.script.ScriptContext;
|
|
||||||
|
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Function;
|
|
||||||
import org.mozilla.javascript.NativeJavaClass;
|
|
||||||
import org.mozilla.javascript.ScriptRuntime;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
|
||||||
import org.mozilla.javascript.Wrapper;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ExternalScriptable is an implementation of Scriptable
|
* ExternalScriptable is an implementation of Scriptable
|
||||||
@@ -49,430 +36,252 @@ import java.util.Map;
|
|||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
|
internal class ExternalScriptable @JvmOverloads constructor(
|
||||||
|
context: ScriptContext?,
|
||||||
|
indexedProps: MutableMap<Any, Any> = HashMap()
|
||||||
|
) : Scriptable {
|
||||||
|
val context: ScriptContext
|
||||||
|
private val indexedProps: MutableMap<Any, Any>
|
||||||
|
private var prototype: Scriptable? = null
|
||||||
|
private var parent: Scriptable? = null
|
||||||
|
|
||||||
final class ExternalScriptable implements Scriptable {
|
init {
|
||||||
/* Underlying ScriptContext that we use to store
|
|
||||||
* named variables of this scope.
|
|
||||||
*/
|
|
||||||
private ScriptContext context;
|
|
||||||
|
|
||||||
/* JavaScript allows variables to be named as numbers (indexed
|
|
||||||
* properties). This way arrays, objects (scopes) are treated uniformly.
|
|
||||||
* Note that JSR 223 API supports only String named variables and
|
|
||||||
* so we can't store these in Bindings. Also, JavaScript allows name
|
|
||||||
* of the property name to be even empty String! Again, JSR 223 API
|
|
||||||
* does not support empty name. So, we use the following fallback map
|
|
||||||
* to store such variables of this scope. This map is not exposed to
|
|
||||||
* JSR 223 API. We can just script objects "as is" and need not convert.
|
|
||||||
*/
|
|
||||||
private Map<Object, Object> indexedProps;
|
|
||||||
|
|
||||||
// my prototype
|
|
||||||
private Scriptable prototype;
|
|
||||||
// my parent scope, if any
|
|
||||||
private Scriptable parent;
|
|
||||||
|
|
||||||
ExternalScriptable(ScriptContext context) {
|
|
||||||
this(context, new HashMap<Object, Object>());
|
|
||||||
}
|
|
||||||
|
|
||||||
ExternalScriptable(ScriptContext context, Map<Object, Object> indexedProps) {
|
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
throw new NullPointerException("context is null");
|
throw NullPointerException("context is null")
|
||||||
}
|
|
||||||
this.context = context;
|
|
||||||
this.indexedProps = indexedProps;
|
|
||||||
}
|
|
||||||
|
|
||||||
ScriptContext getContext() {
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isEmpty(String name) {
|
|
||||||
return name.equals("");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the name of the class.
|
|
||||||
*/
|
|
||||||
public String getClassName() {
|
|
||||||
return "Global";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the value of the named property or NOT_FOUND.
|
|
||||||
*
|
|
||||||
* If the property was created using defineProperty, the
|
|
||||||
* appropriate getter method is called.
|
|
||||||
*
|
|
||||||
* @param name the name of the property
|
|
||||||
* @param start the object in which the lookup began
|
|
||||||
* @return the value of the property (may be null), or NOT_FOUND
|
|
||||||
*/
|
|
||||||
public synchronized Object get(String name, Scriptable start) {
|
|
||||||
if (isEmpty(name)) {
|
|
||||||
if (indexedProps.containsKey(name)) {
|
|
||||||
return indexedProps.get(name);
|
|
||||||
} else {
|
} else {
|
||||||
return NOT_FOUND;
|
this.context = context
|
||||||
|
this.indexedProps = indexedProps
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isEmpty(name: String): Boolean {
|
||||||
|
return name == ""
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getClassName(): String {
|
||||||
|
return "Global"
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
override fun get(name: String, start: Scriptable): Any {
|
||||||
|
return if (this.isEmpty(name)) {
|
||||||
|
indexedProps.getOrElse(name) { Scriptable.NOT_FOUND }
|
||||||
} else {
|
} else {
|
||||||
synchronized (context) {
|
synchronized(context) {
|
||||||
int scope = context.getAttributesScope(name);
|
val scope = context.getAttributesScope(name)
|
||||||
if (scope != -1) {
|
return if (scope != -1) {
|
||||||
Object value = context.getAttribute(name, scope);
|
val value = context.getAttribute(name, scope)
|
||||||
return Context.javaToJS(value, this);
|
Context.javaToJS(value, this)
|
||||||
} else {
|
} else {
|
||||||
return NOT_FOUND;
|
Scriptable.NOT_FOUND
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Synchronized
|
||||||
* Returns the value of the indexed property or NOT_FOUND.
|
override fun get(index: Int, start: Scriptable): Any {
|
||||||
*
|
return indexedProps.getOrElse(index) { Scriptable.NOT_FOUND }
|
||||||
* @param index the numeric index for the property
|
}
|
||||||
* @param start the object in which the lookup began
|
|
||||||
* @return the value of the property (may be null), or NOT_FOUND
|
@Synchronized
|
||||||
*/
|
override fun has(name: String, start: Scriptable): Boolean {
|
||||||
public synchronized Object get(int index, Scriptable start) {
|
return if (this.isEmpty(name)) {
|
||||||
if (indexedProps.containsKey(index)) {
|
indexedProps.containsKey(name)
|
||||||
return indexedProps.get(index);
|
|
||||||
} else {
|
} else {
|
||||||
return NOT_FOUND;
|
synchronized(context) { return context.getAttributesScope(name) != -1 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Synchronized
|
||||||
* Returns true if the named property is defined.
|
override fun has(index: Int, start: Scriptable): Boolean {
|
||||||
*
|
return indexedProps.containsKey(index)
|
||||||
* @param name the name of the property
|
|
||||||
* @param start the object in which the lookup began
|
|
||||||
* @return true if and only if the property was found in the object
|
|
||||||
*/
|
|
||||||
public synchronized boolean has(String name, Scriptable start) {
|
|
||||||
if (isEmpty(name)) {
|
|
||||||
return indexedProps.containsKey(name);
|
|
||||||
} else {
|
|
||||||
synchronized (context) {
|
|
||||||
return context.getAttributesScope(name) != -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun put(name: String, start: Scriptable, value: Any) {
|
||||||
* Returns true if the property index is defined.
|
if (start === this) {
|
||||||
*
|
synchronized(this) {
|
||||||
* @param index the numeric index for the property
|
if (this.isEmpty(name)) {
|
||||||
* @param start the object in which the lookup began
|
indexedProps.put(name, value)
|
||||||
* @return true if and only if the property was found in the object
|
|
||||||
*/
|
|
||||||
public synchronized boolean has(int index, Scriptable start) {
|
|
||||||
return indexedProps.containsKey(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the value of the named property, creating it if need be.
|
|
||||||
*
|
|
||||||
* @param name the name of the property
|
|
||||||
* @param start the object whose property is being set
|
|
||||||
* @param value value to set the property to
|
|
||||||
*/
|
|
||||||
public void put(String name, Scriptable start, Object value) {
|
|
||||||
if (start == this) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (isEmpty(name)) {
|
|
||||||
indexedProps.put(name, value);
|
|
||||||
} else {
|
} else {
|
||||||
synchronized (context) {
|
synchronized(context) {
|
||||||
int scope = context.getAttributesScope(name);
|
var scope = context.getAttributesScope(name)
|
||||||
if (scope == -1) {
|
if (scope == -1) {
|
||||||
scope = ScriptContext.ENGINE_SCOPE;
|
scope = 100
|
||||||
}
|
}
|
||||||
context.setAttribute(name, jsToJava(value), scope);
|
context.setAttribute(name, jsToJava(value), scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
start.put(name, start, value);
|
start.put(name, start, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun put(index: Int, start: Scriptable, value: Any) {
|
||||||
* Sets the value of the indexed property, creating it if need be.
|
if (start === this) {
|
||||||
*
|
synchronized(this) { indexedProps.put(index, value) }
|
||||||
* @param index the numeric index for the property
|
|
||||||
* @param start the object whose property is being set
|
|
||||||
* @param value value to set the property to
|
|
||||||
*/
|
|
||||||
public void put(int index, Scriptable start, Object value) {
|
|
||||||
if (start == this) {
|
|
||||||
synchronized (this) {
|
|
||||||
indexedProps.put(index, value);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
start.put(index, start, value);
|
start.put(index, start, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Synchronized
|
||||||
* Removes a named property from the object.
|
override fun delete(name: String) {
|
||||||
*
|
if (this.isEmpty(name)) {
|
||||||
* If the property is not found, no action is taken.
|
indexedProps.remove(name)
|
||||||
*
|
|
||||||
* @param name the name of the property
|
|
||||||
*/
|
|
||||||
public synchronized void delete(String name) {
|
|
||||||
if (isEmpty(name)) {
|
|
||||||
indexedProps.remove(name);
|
|
||||||
} else {
|
} else {
|
||||||
synchronized (context) {
|
synchronized(context) {
|
||||||
int scope = context.getAttributesScope(name);
|
val scope = context.getAttributesScope(name)
|
||||||
if (scope != -1) {
|
if (scope != -1) {
|
||||||
context.removeAttribute(name, scope);
|
context.removeAttribute(name, scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun delete(index: Int) {
|
||||||
* Removes the indexed property from the object.
|
indexedProps.remove(index)
|
||||||
*
|
|
||||||
* If the property is not found, no action is taken.
|
|
||||||
*
|
|
||||||
* @param index the numeric index for the property
|
|
||||||
*/
|
|
||||||
public void delete(int index) {
|
|
||||||
indexedProps.remove(index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun getPrototype(): Scriptable? {
|
||||||
* Get the prototype of the object.
|
return prototype
|
||||||
* @return the prototype
|
|
||||||
*/
|
|
||||||
public Scriptable getPrototype() {
|
|
||||||
return prototype;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun setPrototype(prototype: Scriptable?) {
|
||||||
* Set the prototype of the object.
|
this.prototype = prototype
|
||||||
* @param prototype the prototype to set
|
|
||||||
*/
|
|
||||||
public void setPrototype(Scriptable prototype) {
|
|
||||||
this.prototype = prototype;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun getParentScope(): Scriptable? {
|
||||||
* Get the parent scope of the object.
|
return parent
|
||||||
* @return the parent scope
|
|
||||||
*/
|
|
||||||
public Scriptable getParentScope() {
|
|
||||||
return parent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun setParentScope(parent: Scriptable?) {
|
||||||
* Set the parent scope of the object.
|
this.parent = parent
|
||||||
* @param parent the parent scope to set
|
|
||||||
*/
|
|
||||||
public void setParentScope(Scriptable parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Synchronized
|
||||||
* Get an array of property ids.
|
override fun getIds(): Array<Any> {
|
||||||
*
|
val keys = allKeys
|
||||||
* Not all property ids need be returned. Those properties
|
val size = keys.size + indexedProps.size
|
||||||
* whose ids are not returned are considered non-enumerable.
|
val res = arrayOfNulls<Any>(size)
|
||||||
*
|
System.arraycopy(keys, 0, res, 0, keys.size)
|
||||||
* @return an array of Objects. Each entry in the array is either
|
var i = keys.size
|
||||||
* a java.lang.String or a java.lang.Number
|
var index: Any
|
||||||
*/
|
val var5: Iterator<*> = indexedProps.keys.iterator()
|
||||||
public synchronized Object[] getIds() {
|
while (var5.hasNext()) {
|
||||||
String[] keys = getAllKeys();
|
index = var5.next()!!
|
||||||
int size = keys.length + indexedProps.size();
|
res[i++] = index
|
||||||
Object[] res = new Object[size];
|
|
||||||
System.arraycopy(keys, 0, res, 0, keys.length);
|
|
||||||
int i = keys.length;
|
|
||||||
// now add all indexed properties
|
|
||||||
for (Object index : indexedProps.keySet()) {
|
|
||||||
res[i++] = index;
|
|
||||||
}
|
}
|
||||||
return res;
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
return res as Array<Any>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun getDefaultValue(typeHint: Class<*>?): Any {
|
||||||
* Get the default value of the object with a given hint.
|
for (i in 0..1) {
|
||||||
* The hints are String.class for type String, Number.class for type
|
val tryToString: Boolean =
|
||||||
* Number, Scriptable.class for type Object, and Boolean.class for
|
|
||||||
* type Boolean. <p>
|
|
||||||
*
|
|
||||||
* A <code>hint</code> of null means "no hint".
|
|
||||||
*
|
|
||||||
* See ECMA 8.6.2.6.
|
|
||||||
*
|
|
||||||
* @param typeHint the type hint
|
|
||||||
* @return the default value
|
|
||||||
*/
|
|
||||||
public Object getDefaultValue(Class typeHint) {
|
|
||||||
for (int i=0; i < 2; i++) {
|
|
||||||
boolean tryToString;
|
|
||||||
if (typeHint == ScriptRuntime.StringClass) {
|
if (typeHint == ScriptRuntime.StringClass) {
|
||||||
tryToString = (i == 0);
|
i == 0
|
||||||
} else {
|
} else {
|
||||||
tryToString = (i == 1);
|
i == 1
|
||||||
}
|
}
|
||||||
|
var methodName: String
|
||||||
String methodName;
|
var args: Array<Any?>
|
||||||
Object[] args;
|
|
||||||
if (tryToString) {
|
if (tryToString) {
|
||||||
methodName = "toString";
|
methodName = "toString"
|
||||||
args = ScriptRuntime.emptyArgs;
|
args = ScriptRuntime.emptyArgs
|
||||||
} else {
|
} else {
|
||||||
methodName = "valueOf";
|
methodName = "valueOf"
|
||||||
args = new Object[1];
|
args = arrayOfNulls(1)
|
||||||
String hint;
|
val hint: String = if (typeHint == null) {
|
||||||
if (typeHint == null) {
|
"undefined"
|
||||||
hint = "undefined";
|
|
||||||
} else if (typeHint == ScriptRuntime.StringClass) {
|
} else if (typeHint == ScriptRuntime.StringClass) {
|
||||||
hint = "string";
|
"string"
|
||||||
} else if (typeHint == ScriptRuntime.ScriptableClass) {
|
} else if (typeHint == ScriptRuntime.ScriptableClass) {
|
||||||
hint = "object";
|
"object"
|
||||||
} else if (typeHint == ScriptRuntime.FunctionClass) {
|
} else if (typeHint == ScriptRuntime.FunctionClass) {
|
||||||
hint = "function";
|
"function"
|
||||||
} else if (typeHint == ScriptRuntime.BooleanClass
|
} else if (typeHint != ScriptRuntime.BooleanClass && typeHint != java.lang.Boolean.TYPE) {
|
||||||
|| typeHint == Boolean.TYPE)
|
if (typeHint != ScriptRuntime.NumberClass && typeHint != ScriptRuntime.ByteClass && typeHint != java.lang.Byte.TYPE && typeHint != ScriptRuntime.ShortClass && typeHint != java.lang.Short.TYPE && typeHint != ScriptRuntime.IntegerClass && typeHint != Integer.TYPE && typeHint != ScriptRuntime.FloatClass && typeHint != java.lang.Float.TYPE && typeHint != ScriptRuntime.DoubleClass && typeHint != java.lang.Double.TYPE) {
|
||||||
{
|
throw Context.reportRuntimeError("Invalid JavaScript value of type $typeHint")
|
||||||
hint = "boolean";
|
}
|
||||||
} else if (typeHint == ScriptRuntime.NumberClass ||
|
"number"
|
||||||
typeHint == ScriptRuntime.ByteClass ||
|
|
||||||
typeHint == Byte.TYPE ||
|
|
||||||
typeHint == ScriptRuntime.ShortClass ||
|
|
||||||
typeHint == Short.TYPE ||
|
|
||||||
typeHint == ScriptRuntime.IntegerClass ||
|
|
||||||
typeHint == Integer.TYPE ||
|
|
||||||
typeHint == ScriptRuntime.FloatClass ||
|
|
||||||
typeHint == Float.TYPE ||
|
|
||||||
typeHint == ScriptRuntime.DoubleClass ||
|
|
||||||
typeHint == Double.TYPE)
|
|
||||||
{
|
|
||||||
hint = "number";
|
|
||||||
} else {
|
} else {
|
||||||
throw Context.reportRuntimeError(
|
"boolean"
|
||||||
"Invalid JavaScript value of type " +
|
|
||||||
typeHint);
|
|
||||||
}
|
}
|
||||||
args[0] = hint;
|
args[0] = hint
|
||||||
}
|
}
|
||||||
Object v = ScriptableObject.getProperty(this, methodName);
|
var v = ScriptableObject.getProperty(this, methodName)
|
||||||
if (!(v instanceof Function))
|
if (v is Function) {
|
||||||
continue;
|
val `fun` = v
|
||||||
Function fun = (Function) v;
|
val cx = Context.enter()
|
||||||
Context cx = RhinoScriptEngine.enterContext();
|
v = try {
|
||||||
try {
|
`fun`.call(cx, `fun`.parentScope, this, args)
|
||||||
v = fun.call(cx, fun.getParentScope(), this, args);
|
|
||||||
} finally {
|
} finally {
|
||||||
cx.exit();
|
Context.exit()
|
||||||
}
|
}
|
||||||
if (v != null) {
|
if (v != null) {
|
||||||
if (!(v instanceof Scriptable)) {
|
if (v !is Scriptable) {
|
||||||
return v;
|
return v
|
||||||
}
|
}
|
||||||
if (typeHint == ScriptRuntime.ScriptableClass
|
if (typeHint == ScriptRuntime.ScriptableClass || typeHint == ScriptRuntime.FunctionClass) {
|
||||||
|| typeHint == ScriptRuntime.FunctionClass)
|
return v
|
||||||
{
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
if (tryToString && v instanceof Wrapper) {
|
if (tryToString && v is Wrapper) {
|
||||||
// Let a wrapped java.lang.String pass for a primitive
|
val u = (v as Wrapper).unwrap()
|
||||||
// string.
|
if (u is String) {
|
||||||
Object u = ((Wrapper)v).unwrap();
|
return u
|
||||||
if (u instanceof String)
|
|
||||||
return u;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fall through to error
|
}
|
||||||
String arg = (typeHint == null) ? "undefined" : typeHint.getName();
|
}
|
||||||
throw Context.reportRuntimeError(
|
val arg = if (typeHint == null) "undefined" else typeHint.name
|
||||||
"Cannot find default value for object " + arg);
|
throw Context.reportRuntimeError("找不到对象的默认值 $arg")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun hasInstance(instance: Scriptable): Boolean {
|
||||||
* Implements the instanceof operator.
|
var proto = instance.prototype
|
||||||
*
|
|
||||||
* @param instance The value that appeared on the LHS of the instanceof
|
|
||||||
* operator
|
|
||||||
* @return true if "this" appears in value's prototype chain
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public boolean hasInstance(Scriptable instance) {
|
|
||||||
// Default for JS objects (other than Function) is to do prototype
|
|
||||||
// chasing.
|
|
||||||
Scriptable proto = instance.getPrototype();
|
|
||||||
while (proto != null) {
|
while (proto != null) {
|
||||||
if (proto.equals(this)) return true;
|
if (proto == this) {
|
||||||
proto = proto.getPrototype();
|
return true
|
||||||
}
|
}
|
||||||
return false;
|
proto = proto.prototype
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] getAllKeys() {
|
private val allKeys: Array<String>
|
||||||
ArrayList<String> list = new ArrayList<String>();
|
get() {
|
||||||
synchronized (context) {
|
val list = ArrayList<String>()
|
||||||
for (int scope : context.getScopes()) {
|
synchronized(context) {
|
||||||
Bindings bindings = context.getBindings(scope);
|
val var3: Iterator<*> = context.scopes.iterator()
|
||||||
|
while (var3.hasNext()) {
|
||||||
|
val scope = var3.next() as Int
|
||||||
|
val bindings = context.getBindings(scope)
|
||||||
if (bindings != null) {
|
if (bindings != null) {
|
||||||
list.ensureCapacity(bindings.size());
|
list.ensureCapacity(bindings.size)
|
||||||
for (String key : bindings.keySet()) {
|
val var6: Iterator<*> = bindings.keys.iterator()
|
||||||
list.add(key);
|
while (var6.hasNext()) {
|
||||||
|
val key = var6.next() as String
|
||||||
|
list.add(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String[] res = new String[list.size()];
|
return list.toTypedArray()
|
||||||
list.toArray(res);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private fun jsToJava(jsObj: Any): Any {
|
||||||
* We convert script values to the nearest Java value.
|
return if (jsObj is Wrapper) {
|
||||||
* We unwrap wrapped Java objects so that access from
|
if (jsObj is NativeJavaClass) {
|
||||||
* Bindings.get() would return "workable" value for Java.
|
jsObj
|
||||||
* But, at the same time, we need to make few special cases
|
|
||||||
* and hence the following function is used.
|
|
||||||
*/
|
|
||||||
private Object jsToJava(Object jsObj) {
|
|
||||||
if (jsObj instanceof Wrapper) {
|
|
||||||
Wrapper njb = (Wrapper) jsObj;
|
|
||||||
/* importClass feature of ImporterTopLevel puts
|
|
||||||
* NativeJavaClass in global scope. If we unwrap
|
|
||||||
* it, importClass won't work.
|
|
||||||
*/
|
|
||||||
if (njb instanceof NativeJavaClass) {
|
|
||||||
return njb;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* script may use Java primitive wrapper type objects
|
|
||||||
* (such as java.lang.Integer, java.lang.Boolean etc)
|
|
||||||
* explicitly. If we unwrap, then these script objects
|
|
||||||
* will become script primitive types. For example,
|
|
||||||
*
|
|
||||||
* var x = new java.lang.Double(3.0); print(typeof x);
|
|
||||||
*
|
|
||||||
* will print 'number'. We don't want that to happen.
|
|
||||||
*/
|
|
||||||
Object obj = njb.unwrap();
|
|
||||||
if (obj instanceof Number || obj instanceof String ||
|
|
||||||
obj instanceof Boolean || obj instanceof Character) {
|
|
||||||
// special type wrapped -- we just leave it as is.
|
|
||||||
return njb;
|
|
||||||
} else {
|
} else {
|
||||||
// return unwrapped object for any other object.
|
val obj = jsObj.unwrap()
|
||||||
return obj;
|
if (obj !is Number && obj !is String && obj !is Boolean && obj !is Char) obj else jsObj
|
||||||
}
|
}
|
||||||
} else { // not-a-Java-wrapper
|
} else {
|
||||||
return jsObj;
|
jsObj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22,97 +22,75 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import com.script.Invocable
|
||||||
|
import com.script.ScriptException
|
||||||
|
import java.lang.reflect.InvocationHandler
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
import java.lang.reflect.Proxy
|
||||||
|
import java.security.AccessControlContext
|
||||||
|
import java.security.AccessController
|
||||||
|
import java.security.PrivilegedExceptionAction
|
||||||
|
|
||||||
import com.script.Invocable;
|
/**
|
||||||
import com.script.ScriptException;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Proxy;
|
|
||||||
import java.security.AccessControlContext;
|
|
||||||
import java.security.AccessController;
|
|
||||||
import java.security.PrivilegedExceptionAction;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* java.lang.reflect.Proxy based interface implementor. This is meant
|
* java.lang.reflect.Proxy based interface implementor. This is meant
|
||||||
* to be used to implement Invocable.getInterface.
|
* to be used to implement Invocable.getInterface.
|
||||||
*
|
*
|
||||||
* @author Mike Grogan
|
* @author Mike Grogan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
public class InterfaceImplementor {
|
open class InterfaceImplementor(private val engine: Invocable) {
|
||||||
|
@Throws(ScriptException::class)
|
||||||
private Invocable engine;
|
fun <T> getInterface(thiz: Any?, iface: Class<T>?): T? {
|
||||||
|
return if (iface != null && iface.isInterface) {
|
||||||
/** Creates a new instance of Invocable */
|
if (!isImplemented(thiz, iface)) {
|
||||||
public InterfaceImplementor(Invocable engine) {
|
null
|
||||||
this.engine = engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class InterfaceImplementorInvocationHandler
|
|
||||||
implements InvocationHandler {
|
|
||||||
private Object thiz;
|
|
||||||
private AccessControlContext accCtxt;
|
|
||||||
|
|
||||||
public InterfaceImplementorInvocationHandler(Object thiz,
|
|
||||||
AccessControlContext accCtxt) {
|
|
||||||
this.thiz = thiz;
|
|
||||||
this.accCtxt = accCtxt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object invoke(Object proxy , Method method, Object[] args)
|
|
||||||
throws java.lang.Throwable {
|
|
||||||
// give chance to convert input args
|
|
||||||
args = convertArguments(method, args);
|
|
||||||
Object result;
|
|
||||||
final Method m = method;
|
|
||||||
final Object[] a = args;
|
|
||||||
result = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
|
|
||||||
public Object run() throws Exception {
|
|
||||||
if (thiz == null) {
|
|
||||||
return engine.invokeFunction(m.getName(), a);
|
|
||||||
} else {
|
} else {
|
||||||
return engine.invokeMethod(thiz, m.getName(), a);
|
val accCtxt = AccessController.getContext()
|
||||||
|
iface.cast(
|
||||||
|
Proxy.newProxyInstance(
|
||||||
|
iface.classLoader,
|
||||||
|
arrayOf<Class<*>>(iface),
|
||||||
|
InterfaceImplementorInvocationHandler(thiz, accCtxt)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
}, accCtxt);
|
throw IllegalArgumentException("interface Class expected")
|
||||||
// give chance to convert the method result
|
|
||||||
return convertResult(method, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getInterface(Object thiz, Class<T> iface)
|
protected open fun isImplemented(thiz: Any?, iface: Class<*>): Boolean {
|
||||||
throws ScriptException {
|
return true
|
||||||
if (iface == null || !iface.isInterface()) {
|
|
||||||
throw new IllegalArgumentException("interface Class expected");
|
|
||||||
}
|
|
||||||
if (! isImplemented(thiz, iface)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
AccessControlContext accCtxt = AccessController.getContext();
|
|
||||||
return iface.cast(Proxy.newProxyInstance(iface.getClassLoader(),
|
|
||||||
new Class[]{iface},
|
|
||||||
new InterfaceImplementorInvocationHandler(thiz, accCtxt)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isImplemented(Object thiz, Class<?> iface) {
|
@Throws(ScriptException::class)
|
||||||
return true;
|
protected open fun convertResult(method: Method?, res: Any): Any {
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// called to convert method result after invoke
|
@Throws(ScriptException::class)
|
||||||
protected Object convertResult(Method method, Object res)
|
protected fun convertArguments(method: Method?, args: Array<Any>): Array<Any> {
|
||||||
throws ScriptException {
|
return args
|
||||||
// default is identity conversion
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// called to convert method arguments before invoke
|
private inner class InterfaceImplementorInvocationHandler(
|
||||||
protected Object[] convertArguments(Method method, Object[] args)
|
private val thiz: Any?,
|
||||||
throws ScriptException {
|
private val accCtxt: AccessControlContext
|
||||||
// default is identity conversion
|
) : InvocationHandler {
|
||||||
return args;
|
@Throws(Throwable::class)
|
||||||
|
override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any {
|
||||||
|
val finalArgs = convertArguments(method, args)
|
||||||
|
val result = AccessController.doPrivileged(PrivilegedExceptionAction {
|
||||||
|
if (thiz == null) engine.invokeFunction(
|
||||||
|
method.name,
|
||||||
|
*finalArgs
|
||||||
|
) else engine.invokeMethod(
|
||||||
|
thiz, method.name, *finalArgs
|
||||||
|
)
|
||||||
|
} as PrivilegedExceptionAction<Any>, accCtxt)
|
||||||
|
return convertResult(method, result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22,16 +22,10 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import org.mozilla.javascript.*
|
||||||
|
import org.mozilla.javascript.Function
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Function;
|
|
||||||
import org.mozilla.javascript.NativeArray;
|
|
||||||
import org.mozilla.javascript.NativeJavaArray;
|
|
||||||
import org.mozilla.javascript.RhinoException;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSAdapter is java.lang.reflect.Proxy equivalent for JavaScript. JSAdapter
|
* JSAdapter is java.lang.reflect.Proxy equivalent for JavaScript. JSAdapter
|
||||||
@@ -74,270 +68,239 @@ import org.mozilla.javascript.ScriptableObject;
|
|||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
public final class JSAdapter implements Scriptable, Function {
|
class JSAdapter private constructor(val adaptee: Scriptable) : Scriptable, Function {
|
||||||
private JSAdapter(Scriptable obj) {
|
private var prototype: Scriptable? = null
|
||||||
setAdaptee(obj);
|
private var parent: Scriptable? = null
|
||||||
|
private var isPrototype = false
|
||||||
|
|
||||||
|
override fun getClassName(): String {
|
||||||
|
return "JSAdapter"
|
||||||
}
|
}
|
||||||
|
|
||||||
// initializer to setup JSAdapter prototype in the given scope
|
override fun get(name: String, start: Scriptable): Any {
|
||||||
public static void init(Context cx, Scriptable scope, boolean sealed)
|
val func = getAdapteeFunction(GET_PROP)
|
||||||
throws RhinoException {
|
return if (func != null) {
|
||||||
JSAdapter obj = new JSAdapter(cx.newObject(scope));
|
this.call(func, arrayOf(name))
|
||||||
obj.setParentScope(scope);
|
} else {
|
||||||
obj.setPrototype(getFunctionPrototype(scope));
|
adaptee[name, adaptee]
|
||||||
obj.isPrototype = true;
|
}
|
||||||
ScriptableObject.defineProperty(scope, "JSAdapter", obj,
|
|
||||||
ScriptableObject.DONTENUM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClassName() {
|
override fun get(index: Int, start: Scriptable): Any {
|
||||||
return "JSAdapter";
|
val func = getAdapteeFunction(GET_PROP)
|
||||||
|
return if (func != null) {
|
||||||
|
this.call(func, arrayOf(index))
|
||||||
|
} else {
|
||||||
|
adaptee[index, adaptee]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object get(String name, Scriptable start) {
|
override fun has(name: String, start: Scriptable): Boolean {
|
||||||
Function func = getAdapteeFunction(GET_PROP);
|
val func = getAdapteeFunction(HAS_PROP)
|
||||||
|
return if (func != null) {
|
||||||
|
val res = this.call(func, arrayOf(name))
|
||||||
|
Context.toBoolean(res)
|
||||||
|
} else {
|
||||||
|
adaptee.has(name, adaptee)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun has(index: Int, start: Scriptable): Boolean {
|
||||||
|
val func = getAdapteeFunction(HAS_PROP)
|
||||||
|
return if (func != null) {
|
||||||
|
val res = this.call(func, arrayOf(index))
|
||||||
|
Context.toBoolean(res)
|
||||||
|
} else {
|
||||||
|
adaptee.has(index, adaptee)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun put(name: String, start: Scriptable, value: Any) {
|
||||||
|
if (start === this) {
|
||||||
|
val func = getAdapteeFunction(PUT_PROP)
|
||||||
if (func != null) {
|
if (func != null) {
|
||||||
return call(func, new Object[] { name });
|
this.call(func, arrayOf(name, value))
|
||||||
} else {
|
} else {
|
||||||
start = getAdaptee();
|
adaptee.put(name, adaptee, value)
|
||||||
return start.get(name, start);
|
}
|
||||||
|
} else {
|
||||||
|
start.put(name, start, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object get(int index, Scriptable start) {
|
override fun put(index: Int, start: Scriptable, value: Any) {
|
||||||
Function func = getAdapteeFunction(GET_PROP);
|
if (start === this) {
|
||||||
|
val func = getAdapteeFunction(PUT_PROP)
|
||||||
if (func != null) {
|
if (func != null) {
|
||||||
return call(func, new Object[] {index});
|
this.call(func, arrayOf(index, value))
|
||||||
} else {
|
} else {
|
||||||
start = getAdaptee();
|
adaptee.put(index, adaptee, value)
|
||||||
return start.get(index, start);
|
}
|
||||||
|
} else {
|
||||||
|
start.put(index, start, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean has(String name, Scriptable start) {
|
override fun delete(name: String) {
|
||||||
Function func = getAdapteeFunction(HAS_PROP);
|
val func = getAdapteeFunction(DEL_PROP)
|
||||||
if (func != null) {
|
if (func != null) {
|
||||||
Object res = call(func, new Object[] { name });
|
this.call(func, arrayOf(name))
|
||||||
return Context.toBoolean(res);
|
|
||||||
} else {
|
} else {
|
||||||
start = getAdaptee();
|
adaptee.delete(name)
|
||||||
return start.has(name, start);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean has(int index, Scriptable start) {
|
override fun delete(index: Int) {
|
||||||
Function func = getAdapteeFunction(HAS_PROP);
|
val func = getAdapteeFunction(DEL_PROP)
|
||||||
if (func != null) {
|
if (func != null) {
|
||||||
Object res = call(func, new Object[] {index});
|
this.call(func, arrayOf(index))
|
||||||
return Context.toBoolean(res);
|
|
||||||
} else {
|
} else {
|
||||||
start = getAdaptee();
|
adaptee.delete(index)
|
||||||
return start.has(index, start);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String name, Scriptable start, Object value) {
|
override fun getPrototype(): Scriptable? {
|
||||||
if (start == this) {
|
return prototype
|
||||||
Function func = getAdapteeFunction(PUT_PROP);
|
}
|
||||||
if (func != null) {
|
|
||||||
call(func, new Object[] { name, value });
|
override fun setPrototype(prototype: Scriptable?) {
|
||||||
|
this.prototype = prototype
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getParentScope(): Scriptable? {
|
||||||
|
return parent
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setParentScope(parent: Scriptable?) {
|
||||||
|
this.parent = parent
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getIds(): Array<Any?> {
|
||||||
|
val func = getAdapteeFunction(GET_PROPIDS)
|
||||||
|
return if (func == null) {
|
||||||
|
adaptee.ids
|
||||||
} else {
|
} else {
|
||||||
start = getAdaptee();
|
val val1 = this.call(func, arrayOfNulls(0))
|
||||||
start.put(name, start, value);
|
val res: Array<Any?>
|
||||||
|
when (val1) {
|
||||||
|
is NativeArray -> {
|
||||||
|
res = arrayOfNulls(val1.length.toInt())
|
||||||
|
for (index in res.indices) {
|
||||||
|
res[index] = mapToId(val1[index, val1])
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
!is NativeJavaArray -> {
|
||||||
|
Context.emptyArgs
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val tmp = val1.unwrap()
|
||||||
|
if (tmp.javaClass == Array<Any>::class.java) {
|
||||||
|
val array = tmp as Array<*>
|
||||||
|
res = arrayOfNulls(array.size)
|
||||||
|
for (index in array.indices) {
|
||||||
|
res[index] = mapToId(array[index])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
start.put(name, start, value);
|
res = Context.emptyArgs
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(int index, Scriptable start, Object value) {
|
override fun hasInstance(scriptable: Scriptable): Boolean {
|
||||||
if (start == this) {
|
return if (scriptable is JSAdapter) {
|
||||||
Function func = getAdapteeFunction(PUT_PROP);
|
true
|
||||||
if( func != null) {
|
|
||||||
call(func, new Object[] {index, value });
|
|
||||||
} else {
|
} else {
|
||||||
start = getAdaptee();
|
var proto = scriptable.prototype
|
||||||
start.put(index, start, value);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
start.put(index, start, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void delete(String name) {
|
|
||||||
Function func = getAdapteeFunction(DEL_PROP);
|
|
||||||
if (func != null) {
|
|
||||||
call(func, new Object[] { name });
|
|
||||||
} else {
|
|
||||||
getAdaptee().delete(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void delete(int index) {
|
|
||||||
Function func = getAdapteeFunction(DEL_PROP);
|
|
||||||
if (func != null) {
|
|
||||||
call(func, new Object[] {index});
|
|
||||||
} else {
|
|
||||||
getAdaptee().delete(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Scriptable getPrototype() {
|
|
||||||
return prototype;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPrototype(Scriptable prototype) {
|
|
||||||
this.prototype = prototype;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Scriptable getParentScope() {
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setParentScope(Scriptable parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object[] getIds() {
|
|
||||||
Function func = getAdapteeFunction(GET_PROPIDS);
|
|
||||||
if (func != null) {
|
|
||||||
Object val = call(func, new Object[0]);
|
|
||||||
// in most cases, adaptee would return native JS array
|
|
||||||
if (val instanceof NativeArray) {
|
|
||||||
NativeArray array = (NativeArray) val;
|
|
||||||
Object[] res = new Object[(int)array.getLength()];
|
|
||||||
for (int index = 0; index < res.length; index++) {
|
|
||||||
res[index] = mapToId(array.get(index, array));
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
} else if (val instanceof NativeJavaArray) {
|
|
||||||
// may be attempt wrapped Java array
|
|
||||||
Object tmp = ((NativeJavaArray)val).unwrap();
|
|
||||||
Object[] res;
|
|
||||||
if (tmp.getClass() == Object[].class) {
|
|
||||||
Object[] array = (Object[]) tmp;
|
|
||||||
res = new Object[array.length];
|
|
||||||
for (int index = 0; index < array.length; index++) {
|
|
||||||
res[index] = mapToId(array[index]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// just return an empty array
|
|
||||||
res = Context.emptyArgs;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
} else {
|
|
||||||
// some other return type, just return empty array
|
|
||||||
return Context.emptyArgs;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return getAdaptee().getIds();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasInstance(Scriptable scriptable) {
|
|
||||||
if (scriptable instanceof JSAdapter) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
Scriptable proto = scriptable.getPrototype();
|
|
||||||
while (proto != null) {
|
while (proto != null) {
|
||||||
if (proto.equals(this)) return true;
|
if (proto == this) {
|
||||||
proto = proto.getPrototype();
|
return true
|
||||||
}
|
}
|
||||||
return false;
|
proto = proto.prototype
|
||||||
|
}
|
||||||
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getDefaultValue(Class hint) {
|
override fun getDefaultValue(hint: Class<*>?): Any {
|
||||||
return getAdaptee().getDefaultValue(hint);
|
return adaptee.getDefaultValue(hint)
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
|
@Throws(RhinoException::class)
|
||||||
Object[] args)
|
override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable, args: Array<Any>): Any {
|
||||||
throws RhinoException {
|
return if (isPrototype) {
|
||||||
if (isPrototype) {
|
construct(cx, scope, args)
|
||||||
return construct(cx, scope, args);
|
|
||||||
} else {
|
} else {
|
||||||
Scriptable tmp = getAdaptee();
|
val tmp = adaptee
|
||||||
if (tmp instanceof Function) {
|
if (tmp is Function) {
|
||||||
return ((Function)tmp).call(cx, scope, tmp, args);
|
tmp.call(cx, scope, tmp, args)
|
||||||
} else {
|
} else {
|
||||||
throw Context.reportRuntimeError("TypeError: not a function");
|
throw Context.reportRuntimeError("TypeError: not a function")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Scriptable construct(Context cx, Scriptable scope, Object[] args)
|
@Throws(RhinoException::class)
|
||||||
throws RhinoException {
|
override fun construct(cx: Context, scope: Scriptable, args: Array<Any>): Scriptable {
|
||||||
if (isPrototype) {
|
val tmp: Scriptable?
|
||||||
Scriptable topLevel = ScriptableObject.getTopLevelScope(scope);
|
return if (isPrototype) {
|
||||||
JSAdapter newObj;
|
tmp = ScriptableObject.getTopLevelScope(scope)
|
||||||
if (args.length > 0) {
|
if (args.size > 0) {
|
||||||
newObj = new JSAdapter(Context.toObject(args[0], topLevel));
|
JSAdapter(Context.toObject(args[0], tmp))
|
||||||
} else {
|
} else {
|
||||||
throw Context.reportRuntimeError("JSAdapter requires adaptee");
|
throw Context.reportRuntimeError("JSAdapter requires adaptee")
|
||||||
}
|
}
|
||||||
return newObj;
|
|
||||||
} else {
|
} else {
|
||||||
Scriptable tmp = getAdaptee();
|
tmp = adaptee
|
||||||
if (tmp instanceof Function) {
|
if (tmp is Function) {
|
||||||
return ((Function)tmp).construct(cx, scope, args);
|
tmp.construct(cx, scope, args)
|
||||||
} else {
|
} else {
|
||||||
throw Context.reportRuntimeError("TypeError: not a constructor");
|
throw Context.reportRuntimeError("TypeError: not a constructor")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Scriptable getAdaptee() {
|
private fun mapToId(tmp: Any?): Any {
|
||||||
return adaptee;
|
return if (tmp is Double) tmp.toInt() else Context.toString(tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAdaptee(Scriptable adaptee) {
|
private fun getAdapteeFunction(name: String): Function? {
|
||||||
if (adaptee == null) {
|
val o = ScriptableObject.getProperty(adaptee, name)
|
||||||
throw new NullPointerException("adaptee can not be null");
|
return if (o is Function) o else null
|
||||||
}
|
|
||||||
this.adaptee = adaptee;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- internals only below this point
|
private fun call(func: Function, args: Array<Any?>): Any {
|
||||||
|
val cx = Context.getCurrentContext()
|
||||||
// map a property id. Property id can only be an Integer or String
|
val thisObj = adaptee
|
||||||
private Object mapToId(Object tmp) {
|
val scope = func.parentScope
|
||||||
if (tmp instanceof Double) {
|
return try {
|
||||||
return ((Double) tmp).intValue();
|
func.call(cx, scope, thisObj, args)
|
||||||
} else {
|
} catch (var7: RhinoException) {
|
||||||
return Context.toString(tmp);
|
throw Context.reportRuntimeError(var7.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Scriptable getFunctionPrototype(Scriptable scope) {
|
companion object {
|
||||||
return ScriptableObject.getFunctionPrototype(scope);
|
private const val GET_PROP = "__get__"
|
||||||
|
private const val HAS_PROP = "__has__"
|
||||||
|
private const val PUT_PROP = "__put__"
|
||||||
|
private const val DEL_PROP = "__delete__"
|
||||||
|
private const val GET_PROPIDS = "__getIds__"
|
||||||
|
|
||||||
|
@Throws(RhinoException::class)
|
||||||
|
fun init(cx: Context, scope: Scriptable, sealed: Boolean) {
|
||||||
|
val obj = JSAdapter(cx.newObject(scope))
|
||||||
|
obj.parentScope = scope
|
||||||
|
obj.setPrototype(getFunctionPrototype(scope))
|
||||||
|
obj.isPrototype = true
|
||||||
|
ScriptableObject.defineProperty(scope, "JSAdapter", obj, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
private Function getAdapteeFunction(String name) {
|
private fun getFunctionPrototype(scope: Scriptable): Scriptable {
|
||||||
Object o = ScriptableObject.getProperty(getAdaptee(), name);
|
return ScriptableObject.getFunctionPrototype(scope)
|
||||||
return (o instanceof Function)? (Function)o : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object call(Function func, Object[] args) {
|
|
||||||
Context cx = Context.getCurrentContext();
|
|
||||||
Scriptable thisObj = getAdaptee();
|
|
||||||
Scriptable scope = func.getParentScope();
|
|
||||||
try {
|
|
||||||
return func.call(cx, scope, thisObj, args);
|
|
||||||
} catch (RhinoException re) {
|
|
||||||
throw Context.reportRuntimeError(re.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Scriptable prototype;
|
|
||||||
private Scriptable parent;
|
|
||||||
private Scriptable adaptee;
|
|
||||||
private boolean isPrototype;
|
|
||||||
|
|
||||||
// names of adaptee JavaScript functions
|
|
||||||
private static final String GET_PROP = "__get__";
|
|
||||||
private static final String HAS_PROP = "__has__";
|
|
||||||
private static final String PUT_PROP = "__put__";
|
|
||||||
private static final String DEL_PROP = "__delete__";
|
|
||||||
private static final String GET_PROPIDS = "__getIds__";
|
|
||||||
}
|
}
|
||||||
@@ -22,32 +22,27 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import com.script.Invocable
|
||||||
|
import org.mozilla.javascript.*
|
||||||
import com.script.Invocable;
|
import org.mozilla.javascript.Function
|
||||||
|
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Function;
|
|
||||||
import org.mozilla.javascript.RhinoException;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
|
||||||
import org.mozilla.javascript.Wrapper;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements Rhino-like JavaAdapter to help implement a Java
|
* This class implements Rhino-like JavaAdapter to help implement a Java
|
||||||
* interface in JavaScript. We support this using Invocable.getInterface.
|
* interface in JavaScript. We support this using Invocable.getInterface.
|
||||||
* Using this JavaAdapter, script author could write:
|
* Using this JavaAdapter, script author could write:
|
||||||
*
|
*
|
||||||
|
*
|
||||||
* var r = new java.lang.Runnable() {
|
* var r = new java.lang.Runnable() {
|
||||||
* run: function() { script... }
|
* run: function() { script... }
|
||||||
* };
|
* };
|
||||||
*
|
*
|
||||||
|
*
|
||||||
* r.run();
|
* r.run();
|
||||||
* new java.lang.Thread(r).start();
|
* new java.lang.Thread(r).start();
|
||||||
*
|
*
|
||||||
|
*
|
||||||
* Note that Rhino's JavaAdapter support allows extending a Java class and/or
|
* Note that Rhino's JavaAdapter support allows extending a Java class and/or
|
||||||
* implementing one or more interfaces. This JavaAdapter implementation does
|
* implementing one or more interfaces. This JavaAdapter implementation does
|
||||||
* not support these.
|
* not support these.
|
||||||
@@ -55,60 +50,54 @@ import org.mozilla.javascript.Wrapper;
|
|||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
final class JavaAdapter extends ScriptableObject implements Function {
|
internal class JavaAdapter private constructor(private val engine: Invocable) : ScriptableObject(),
|
||||||
private JavaAdapter(Invocable engine) {
|
Function {
|
||||||
this.engine = engine;
|
override fun getClassName(): String {
|
||||||
|
return "JavaAdapter"
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init(Context cx, Scriptable scope, boolean sealed)
|
@Throws(RhinoException::class)
|
||||||
throws RhinoException {
|
override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable, args: Array<Any>): Any {
|
||||||
RhinoTopLevel topLevel = (RhinoTopLevel) scope;
|
return construct(cx, scope, args)
|
||||||
Invocable engine = topLevel.getScriptEngine();
|
|
||||||
JavaAdapter obj = new JavaAdapter(engine);
|
|
||||||
obj.setParentScope(scope);
|
|
||||||
obj.setPrototype(getFunctionPrototype(scope));
|
|
||||||
/*
|
|
||||||
* Note that we can't use defineProperty. A property of this
|
|
||||||
* name is already defined in Context.initStandardObjects. We
|
|
||||||
* simply overwrite the property value!
|
|
||||||
*/
|
|
||||||
ScriptableObject.putProperty(topLevel, "JavaAdapter", obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClassName() {
|
@Throws(RhinoException::class)
|
||||||
return "JavaAdapter";
|
override fun construct(cx: Context, scope: Scriptable, args: Array<Any>): Scriptable {
|
||||||
}
|
return if (args.size == 2) {
|
||||||
|
var clazz: Class<*>? = null
|
||||||
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
|
val obj1 = args[0]
|
||||||
Object[] args) throws RhinoException {
|
if (obj1 is Wrapper) {
|
||||||
return construct(cx, scope, args);
|
val o = obj1.unwrap()
|
||||||
}
|
if (o is Class<*> && o.isInterface) {
|
||||||
|
clazz = o
|
||||||
public Scriptable construct(Context cx, Scriptable scope, Object[] args)
|
|
||||||
throws RhinoException {
|
|
||||||
if (args.length == 2) {
|
|
||||||
Class<?> clazz = null;
|
|
||||||
Object obj1 = args[0];
|
|
||||||
if (obj1 instanceof Wrapper) {
|
|
||||||
Object o = ((Wrapper)obj1).unwrap();
|
|
||||||
if (o instanceof Class && ((Class)o).isInterface()) {
|
|
||||||
clazz = (Class) o;
|
|
||||||
}
|
|
||||||
} else if (obj1 instanceof Class) {
|
|
||||||
if (((Class)obj1).isInterface()) {
|
|
||||||
clazz = (Class) obj1;
|
|
||||||
}
|
}
|
||||||
|
} else if (obj1 is Class<*> && obj1.isInterface) {
|
||||||
|
clazz = obj1
|
||||||
}
|
}
|
||||||
if (clazz == null) {
|
if (clazz == null) {
|
||||||
throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class");
|
throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class")
|
||||||
}
|
|
||||||
|
|
||||||
Scriptable topLevel = ScriptableObject.getTopLevelScope(scope);
|
|
||||||
return cx.toObject(engine.getInterface(args[1], clazz), topLevel);
|
|
||||||
} else {
|
} else {
|
||||||
throw Context.reportRuntimeError("JavaAdapter requires two arguments");
|
val topLevel = getTopLevelScope(scope)
|
||||||
|
Context.toObject(
|
||||||
|
engine.getInterface(args[1], clazz),
|
||||||
|
topLevel
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw Context.reportRuntimeError("JavaAdapter requires two arguments")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Invocable engine;
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
@Throws(RhinoException::class)
|
||||||
|
fun init(cx: Context?, scope: Scriptable, sealed: Boolean) {
|
||||||
|
val topLevel = scope as RhinoTopLevel
|
||||||
|
val engine: Invocable = topLevel.scriptEngine
|
||||||
|
val obj = JavaAdapter(engine)
|
||||||
|
obj.parentScope = scope
|
||||||
|
obj.prototype = getFunctionPrototype(scope)
|
||||||
|
putProperty(topLevel, "JavaAdapter", obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,13 +22,9 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import org.mozilla.javascript.ClassShutter
|
||||||
|
|
||||||
import org.mozilla.javascript.ClassShutter;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class prevents script access to certain sensitive classes.
|
* This class prevents script access to certain sensitive classes.
|
||||||
@@ -38,43 +34,38 @@ import java.util.Map;
|
|||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
final class RhinoClassShutter implements ClassShutter {
|
internal class RhinoClassShutter private constructor() : ClassShutter {
|
||||||
private static Map<String, Boolean> protectedClasses;
|
override fun visibleToScripts(fullClassName: String): Boolean {
|
||||||
private static RhinoClassShutter theInstance;
|
val sm = System.getSecurityManager()
|
||||||
|
|
||||||
private RhinoClassShutter() {
|
|
||||||
}
|
|
||||||
|
|
||||||
static synchronized ClassShutter getInstance() {
|
|
||||||
if (theInstance == null) {
|
|
||||||
theInstance = new RhinoClassShutter();
|
|
||||||
protectedClasses = new HashMap<String, Boolean>();
|
|
||||||
|
|
||||||
// For now, we just have AccessController. Allowing scripts
|
|
||||||
// to this class will allow it to execute doPrivileged in
|
|
||||||
// bootstrap context. We can add more classes for other reasons.
|
|
||||||
protectedClasses.put("java.lang.Class", Boolean.TRUE);
|
|
||||||
protectedClasses.put("java.lang.Runtime", Boolean.TRUE);
|
|
||||||
protectedClasses.put("java.io.File", Boolean.TRUE);
|
|
||||||
protectedClasses.put("java.security.AccessController", Boolean.TRUE);
|
|
||||||
}
|
|
||||||
return theInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean visibleToScripts(String fullClassName) {
|
|
||||||
// first do the security check.
|
|
||||||
SecurityManager sm = System.getSecurityManager();
|
|
||||||
if (sm != null) {
|
if (sm != null) {
|
||||||
int i = fullClassName.lastIndexOf(".");
|
val i = fullClassName.lastIndexOf(".")
|
||||||
if (i != -1) {
|
if (i != -1) {
|
||||||
try {
|
try {
|
||||||
sm.checkPackageAccess(fullClassName.substring(0, i));
|
sm.checkPackageAccess(fullClassName.substring(0, i))
|
||||||
} catch (SecurityException se) {
|
} catch (var5: SecurityException) {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// now, check is it a protected class.
|
return protectedClasses[fullClassName] == null
|
||||||
return protectedClasses.get(fullClassName) == null;
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
val protectedClasses by lazy {
|
||||||
|
val protectedClasses = HashMap<Any, Any>()
|
||||||
|
protectedClasses["java.lang.Runtime"] = java.lang.Boolean.TRUE
|
||||||
|
protectedClasses["java.io.File"] = java.lang.Boolean.TRUE
|
||||||
|
protectedClasses["java.security.AccessController"] = java.lang.Boolean.TRUE
|
||||||
|
protectedClasses
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
val instance: ClassShutter by lazy {
|
||||||
|
val theInstance = RhinoClassShutter()
|
||||||
|
theInstance
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22,20 +22,13 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import com.script.CompiledScript
|
||||||
|
import com.script.ScriptContext
|
||||||
import com.script.CompiledScript;
|
import com.script.ScriptEngine
|
||||||
import com.script.ScriptContext;
|
import com.script.ScriptException
|
||||||
import com.script.ScriptEngine;
|
import org.mozilla.javascript.*
|
||||||
import com.script.ScriptException;
|
|
||||||
|
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.JavaScriptException;
|
|
||||||
import org.mozilla.javascript.RhinoException;
|
|
||||||
import org.mozilla.javascript.Script;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents compiled JavaScript code.
|
* Represents compiled JavaScript code.
|
||||||
@@ -43,46 +36,36 @@ import org.mozilla.javascript.Scriptable;
|
|||||||
* @author Mike Grogan
|
* @author Mike Grogan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
final class RhinoCompiledScript extends CompiledScript {
|
internal class RhinoCompiledScript(
|
||||||
|
private val engine: RhinoScriptEngine,
|
||||||
|
private val script: Script
|
||||||
|
) : CompiledScript() {
|
||||||
|
|
||||||
private com.script.rhino.RhinoScriptEngine engine;
|
@Throws(ScriptException::class)
|
||||||
private Script script;
|
override fun eval(context: ScriptContext): Any? {
|
||||||
|
val cx = Context.enter()
|
||||||
|
val result: Any?
|
||||||
RhinoCompiledScript(com.script.rhino.RhinoScriptEngine engine, Script script) {
|
|
||||||
this.engine = engine;
|
|
||||||
this.script = script;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object eval(ScriptContext context) throws ScriptException {
|
|
||||||
|
|
||||||
Object result = null;
|
|
||||||
Context cx = RhinoScriptEngine.enterContext();
|
|
||||||
try {
|
try {
|
||||||
|
val scope = engine.getRuntimeScope(context)
|
||||||
Scriptable scope = engine.getRuntimeScope(context);
|
val ret = script.exec(cx, scope)
|
||||||
Object ret = script.exec(cx, scope);
|
result = engine.unwrapReturnValue(ret)
|
||||||
result = engine.unwrapReturnValue(ret);
|
} catch (re: RhinoException) {
|
||||||
} catch (RhinoException re) {
|
val line = if (re.lineNumber() == 0) -1 else re.lineNumber()
|
||||||
int line = (line = re.lineNumber()) == 0 ? -1 : line;
|
val msg: String = if (re is JavaScriptException) {
|
||||||
String msg;
|
re.value.toString()
|
||||||
if (re instanceof JavaScriptException) {
|
|
||||||
msg = String.valueOf(((JavaScriptException)re).getValue());
|
|
||||||
} else {
|
} else {
|
||||||
msg = re.toString();
|
re.toString()
|
||||||
}
|
}
|
||||||
ScriptException se = new ScriptException(msg, re.sourceName(), line);
|
val se = ScriptException(msg, re.sourceName(), line)
|
||||||
se.initCause(re);
|
se.initCause(re)
|
||||||
throw se;
|
throw se
|
||||||
} finally {
|
} finally {
|
||||||
Context.exit();
|
Context.exit()
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
override fun getEngine(): ScriptEngine {
|
||||||
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptEngine getEngine() {
|
|
||||||
return engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -22,440 +22,358 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import com.script.*
|
||||||
|
import com.script.rhino.RhinoClassShutter.Companion.instance
|
||||||
|
import org.intellij.lang.annotations.Language
|
||||||
import com.script.AbstractScriptEngine;
|
import org.mozilla.javascript.*
|
||||||
import com.script.Bindings;
|
import org.mozilla.javascript.Function
|
||||||
import com.script.Compilable;
|
import java.io.IOException
|
||||||
import com.script.CompiledScript;
|
import java.io.Reader
|
||||||
import com.script.Invocable;
|
import java.io.StringReader
|
||||||
import com.script.ScriptContext;
|
import java.lang.reflect.Method
|
||||||
import com.script.ScriptEngine;
|
import java.security.*
|
||||||
import com.script.ScriptException;
|
|
||||||
import com.script.SimpleBindings;
|
|
||||||
|
|
||||||
import org.mozilla.javascript.Callable;
|
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.ContextFactory;
|
|
||||||
import org.mozilla.javascript.Function;
|
|
||||||
import org.mozilla.javascript.JavaScriptException;
|
|
||||||
import org.mozilla.javascript.RhinoException;
|
|
||||||
import org.mozilla.javascript.Script;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
|
||||||
import org.mozilla.javascript.Undefined;
|
|
||||||
import org.mozilla.javascript.Wrapper;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.security.AccessControlContext;
|
|
||||||
import java.security.AccessControlException;
|
|
||||||
import java.security.AccessController;
|
|
||||||
import java.security.AllPermission;
|
|
||||||
import java.security.PrivilegedAction;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of <code>ScriptEngine</code> using the Mozilla Rhino
|
* Implementation of `ScriptEngine` using the Mozilla Rhino
|
||||||
* interpreter.
|
* interpreter.
|
||||||
*
|
*
|
||||||
* @author Mike Grogan
|
* @author Mike Grogan
|
||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
public final class RhinoScriptEngine extends AbstractScriptEngine
|
class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||||
implements Invocable, Compilable {
|
var accessContext: AccessControlContext? = null
|
||||||
|
private var topLevel: RhinoTopLevel? = null
|
||||||
|
private val indexedProps: MutableMap<Any, Any>
|
||||||
|
private val implementor: InterfaceImplementor
|
||||||
|
|
||||||
private static final boolean DEBUG = false;
|
@Throws(ScriptException::class)
|
||||||
|
override fun eval(reader: Reader, ctxt: ScriptContext): Any {
|
||||||
private AccessControlContext accCtxt;
|
val cx = Context.enter()
|
||||||
|
val ret: Any
|
||||||
/* Scope where standard JavaScript objects and our
|
try {
|
||||||
* extensions to it are stored. Note that these are not
|
val scope = getRuntimeScope(ctxt)
|
||||||
* user defined engine level global variables. These are
|
var filename = this["javax.script.filename"] as? String
|
||||||
* variables have to be there on all compliant ECMAScript
|
filename = filename ?: "<Unknown source>"
|
||||||
* scopes. We put these standard objects in this top level.
|
ret = cx.evaluateReader(scope, reader, filename, 1, null as Any?)
|
||||||
*/
|
} catch (re: RhinoException) {
|
||||||
private com.script.rhino.RhinoTopLevel topLevel;
|
val line = if (re.lineNumber() == 0) -1 else re.lineNumber()
|
||||||
|
val msg: String = if (re is JavaScriptException) {
|
||||||
/* map used to store indexed properties in engine scope
|
re.value.toString()
|
||||||
* refer to comment on 'indexedProps' in ExternalScriptable.java.
|
|
||||||
*/
|
|
||||||
private Map<Object, Object> indexedProps;
|
|
||||||
|
|
||||||
private InterfaceImplementor implementor;
|
|
||||||
|
|
||||||
private static final int languageVersion = getLanguageVersion();
|
|
||||||
private static final int optimizationLevel = getOptimizationLevel();
|
|
||||||
|
|
||||||
static {
|
|
||||||
ContextFactory.initGlobal(new ContextFactory() {
|
|
||||||
/**
|
|
||||||
* Create new Context instance to be associated with the current thread.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected Context makeContext() {
|
|
||||||
Context cx = super.makeContext();
|
|
||||||
cx.setLanguageVersion(languageVersion);
|
|
||||||
cx.setOptimizationLevel(optimizationLevel);
|
|
||||||
cx.setClassShutter(com.script.rhino.RhinoClassShutter.getInstance());
|
|
||||||
cx.setWrapFactory(com.script.rhino.RhinoWrapFactory.getInstance());
|
|
||||||
return cx;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute top call to script or function. When the runtime is about to
|
|
||||||
* execute a script or function that will create the first stack frame
|
|
||||||
* with scriptable code, it calls this method to perform the real call.
|
|
||||||
* In this way execution of any script happens inside this function.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected Object doTopCall(final Callable callable,
|
|
||||||
final Context cx, final Scriptable scope,
|
|
||||||
final Scriptable thisObj, final Object[] args) {
|
|
||||||
AccessControlContext accCtxt = null;
|
|
||||||
Scriptable global = ScriptableObject.getTopLevelScope(scope);
|
|
||||||
Scriptable globalProto = global.getPrototype();
|
|
||||||
if (globalProto instanceof com.script.rhino.RhinoTopLevel) {
|
|
||||||
accCtxt = ((com.script.rhino.RhinoTopLevel) globalProto).getAccessContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (accCtxt != null) {
|
|
||||||
return AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
|
||||||
public Object run() {
|
|
||||||
return superDoTopCall(callable, cx, scope, thisObj, args);
|
|
||||||
}
|
|
||||||
}, accCtxt);
|
|
||||||
} else {
|
} else {
|
||||||
return superDoTopCall(callable, cx, scope, thisObj, args);
|
re.toString()
|
||||||
}
|
}
|
||||||
}
|
val se = ScriptException(msg, re.sourceName(), line)
|
||||||
|
se.initCause(re)
|
||||||
private Object superDoTopCall(Callable callable,
|
throw se
|
||||||
Context cx, Scriptable scope,
|
} catch (var14: IOException) {
|
||||||
Scriptable thisObj, Object[] args) {
|
throw ScriptException(var14)
|
||||||
return super.doTopCall(callable, cx, scope, thisObj, args);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String RHINO_JS_VERSION = "rhino.js.version";
|
|
||||||
|
|
||||||
private static int getLanguageVersion() {
|
|
||||||
return Context.VERSION_1_8;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String RHINO_OPT_LEVEL = "rhino.opt.level";
|
|
||||||
|
|
||||||
private static int getOptimizationLevel() {
|
|
||||||
int optLevel = -1;
|
|
||||||
// disable optimizer under security manager, for now.
|
|
||||||
if (System.getSecurityManager() == null) {
|
|
||||||
optLevel = Integer.getInteger(RHINO_OPT_LEVEL, -1);
|
|
||||||
}
|
|
||||||
return optLevel;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new instance of RhinoScriptEngine
|
|
||||||
*/
|
|
||||||
public RhinoScriptEngine() {
|
|
||||||
if (System.getSecurityManager() != null) {
|
|
||||||
try {
|
|
||||||
AccessController.checkPermission(new AllPermission());
|
|
||||||
} catch (AccessControlException ace) {
|
|
||||||
accCtxt = AccessController.getContext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Context cx = enterContext();
|
|
||||||
try {
|
|
||||||
topLevel = new RhinoTopLevel(cx, this);
|
|
||||||
} finally {
|
} finally {
|
||||||
cx.exit();
|
Context.exit()
|
||||||
|
}
|
||||||
|
return unwrapReturnValue(ret)!!
|
||||||
}
|
}
|
||||||
|
|
||||||
indexedProps = new HashMap<Object, Object>();
|
@Throws(ScriptException::class)
|
||||||
|
override fun eval(script: String?, ctxt: ScriptContext): Any {
|
||||||
//construct object used to implement getInterface
|
return if (script == null) {
|
||||||
implementor = new InterfaceImplementor(this) {
|
throw NullPointerException("null script")
|
||||||
protected boolean isImplemented(Object thiz, Class<?> iface) {
|
|
||||||
Context cx = enterContext();
|
|
||||||
try {
|
|
||||||
if (thiz != null && !(thiz instanceof Scriptable)) {
|
|
||||||
thiz = cx.toObject(thiz, topLevel);
|
|
||||||
}
|
|
||||||
Scriptable engineScope = getRuntimeScope(context);
|
|
||||||
Scriptable localScope = (thiz != null) ? (Scriptable) thiz :
|
|
||||||
engineScope;
|
|
||||||
for (Method method : iface.getMethods()) {
|
|
||||||
// ignore methods of java.lang.Object class
|
|
||||||
if (method.getDeclaringClass() == Object.class) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Object obj = ScriptableObject.getProperty(localScope, method.getName());
|
|
||||||
if (!(obj instanceof Function)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} finally {
|
|
||||||
cx.exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Object convertResult(Method method, Object res)
|
|
||||||
throws ScriptException {
|
|
||||||
Class desiredType = method.getReturnType();
|
|
||||||
if (desiredType == Void.TYPE) {
|
|
||||||
return null;
|
|
||||||
} else {
|
} else {
|
||||||
return Context.jsToJava(res, desiredType);
|
this.eval(StringReader(script) as Reader, ctxt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
override fun createBindings(): Bindings {
|
||||||
|
return SimpleBindings()
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object eval(Reader reader, ScriptContext ctxt)
|
@Throws(ScriptException::class, NoSuchMethodException::class)
|
||||||
throws ScriptException {
|
override fun invokeFunction(name: String, vararg args: Any): Any {
|
||||||
Object ret;
|
return this.invoke(null as Any?, name, *args)
|
||||||
|
}
|
||||||
|
|
||||||
Context cx = enterContext();
|
@Throws(ScriptException::class, NoSuchMethodException::class)
|
||||||
try {
|
override fun invokeMethod(thiz: Any?, name: String, vararg args: Any): Any {
|
||||||
Scriptable scope = getRuntimeScope(ctxt);
|
return if (thiz == null) {
|
||||||
String filename = (String) get(ScriptEngine.FILENAME);
|
throw IllegalArgumentException("脚本对象不能为空")
|
||||||
filename = filename == null ? "<Unknown source>" : filename;
|
|
||||||
|
|
||||||
ret = cx.evaluateReader(scope, reader, filename, 1, null);
|
|
||||||
} catch (RhinoException re) {
|
|
||||||
if (DEBUG) re.printStackTrace();
|
|
||||||
int line = (line = re.lineNumber()) == 0 ? -1 : line;
|
|
||||||
String msg;
|
|
||||||
if (re instanceof JavaScriptException) {
|
|
||||||
msg = String.valueOf(((JavaScriptException) re).getValue());
|
|
||||||
} else {
|
} else {
|
||||||
msg = re.toString();
|
this.invoke(thiz, name, *args)
|
||||||
}
|
}
|
||||||
ScriptException se = new ScriptException(msg, re.sourceName(), line);
|
|
||||||
se.initCause(re);
|
|
||||||
throw se;
|
|
||||||
} catch (IOException ee) {
|
|
||||||
throw new ScriptException(ee);
|
|
||||||
} finally {
|
|
||||||
cx.exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return unwrapReturnValue(ret);
|
@Suppress("UNCHECKED_CAST")
|
||||||
}
|
@Throws(ScriptException::class, NoSuchMethodException::class)
|
||||||
|
private operator fun invoke(thiz: Any?, name: String?, vararg args: Any?): Any {
|
||||||
public Object eval(String script, ScriptContext ctxt) throws ScriptException {
|
var thiz1 = thiz
|
||||||
if (script == null) {
|
val cx = Context.enter()
|
||||||
throw new NullPointerException("null script");
|
val var11: Any
|
||||||
}
|
|
||||||
return eval(new StringReader(script), ctxt);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Bindings createBindings() {
|
|
||||||
return new SimpleBindings();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Invocable methods
|
|
||||||
public Object invokeFunction(String name, Object... args)
|
|
||||||
throws ScriptException, NoSuchMethodException {
|
|
||||||
return invoke(null, name, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object invokeMethod(Object thiz, String name, Object... args)
|
|
||||||
throws ScriptException, NoSuchMethodException {
|
|
||||||
if (thiz == null) {
|
|
||||||
throw new IllegalArgumentException("script object can not be null");
|
|
||||||
}
|
|
||||||
return invoke(thiz, name, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object invoke(Object thiz, String name, Object... args)
|
|
||||||
throws ScriptException, NoSuchMethodException {
|
|
||||||
Context cx = enterContext();
|
|
||||||
try {
|
try {
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
throw new NullPointerException("method name is null");
|
throw NullPointerException("方法名为空")
|
||||||
}
|
}
|
||||||
|
if (thiz1 != null && thiz1 !is Scriptable) {
|
||||||
if (thiz != null && !(thiz instanceof Scriptable)) {
|
thiz1 = Context.toObject(thiz1, topLevel)
|
||||||
thiz = cx.toObject(thiz, topLevel);
|
|
||||||
}
|
}
|
||||||
|
val engineScope = getRuntimeScope(context)
|
||||||
Scriptable engineScope = getRuntimeScope(context);
|
val localScope = if (thiz1 != null) thiz1 as Scriptable else engineScope
|
||||||
Scriptable localScope = (thiz != null) ? (Scriptable) thiz :
|
val obj = ScriptableObject.getProperty(localScope, name) as? Function
|
||||||
engineScope;
|
?: throw NoSuchMethodException("no such method: $name")
|
||||||
Object obj = ScriptableObject.getProperty(localScope, name);
|
val func = obj
|
||||||
if (!(obj instanceof Function)) {
|
var scope = func.parentScope
|
||||||
throw new NoSuchMethodException("no such method: " + name);
|
|
||||||
}
|
|
||||||
|
|
||||||
Function func = (Function) obj;
|
|
||||||
Scriptable scope = func.getParentScope();
|
|
||||||
if (scope == null) {
|
if (scope == null) {
|
||||||
scope = engineScope;
|
scope = engineScope
|
||||||
}
|
}
|
||||||
Object result = func.call(cx, scope, localScope,
|
val result = func.call(cx, scope, localScope, wrapArguments(args as? Array<Any?>))
|
||||||
wrapArguments(args));
|
var11 = unwrapReturnValue(result)!!
|
||||||
return unwrapReturnValue(result);
|
} catch (re: RhinoException) {
|
||||||
} catch (RhinoException re) {
|
val line = if (re.lineNumber() == 0) -1 else re.lineNumber()
|
||||||
if (DEBUG) re.printStackTrace();
|
val se = ScriptException(re.toString(), re.sourceName(), line)
|
||||||
int line = (line = re.lineNumber()) == 0 ? -1 : line;
|
se.initCause(re)
|
||||||
ScriptException se = new ScriptException(re.toString(), re.sourceName(), line);
|
throw se
|
||||||
se.initCause(re);
|
|
||||||
throw se;
|
|
||||||
} finally {
|
} finally {
|
||||||
cx.exit();
|
Context.exit()
|
||||||
|
}
|
||||||
|
return var11
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun <T> getInterface(clasz: Class<T>): T? {
|
||||||
|
return try {
|
||||||
|
implementor.getInterface(null as Any?, clasz)
|
||||||
|
} catch (var3: ScriptException) {
|
||||||
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getInterface(Class<T> clasz) {
|
override fun <T> getInterface(thiz: Any?, clasz: Class<T>): T? {
|
||||||
|
return if (thiz == null) {
|
||||||
|
throw IllegalArgumentException("脚本对象不能为空")
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
return implementor.getInterface(null, clasz);
|
implementor.getInterface(thiz, clasz)
|
||||||
} catch (ScriptException e) {
|
} catch (var4: ScriptException) {
|
||||||
return null;
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getInterface(Object thiz, Class<T> clasz) {
|
fun getRuntimeScope(ctxt: ScriptContext?): Scriptable {
|
||||||
if (thiz == null) {
|
return if (ctxt == null) {
|
||||||
throw new IllegalArgumentException("script object can not be null");
|
throw NullPointerException("脚本context为空")
|
||||||
}
|
} else {
|
||||||
|
val newScope: Scriptable = ExternalScriptable(ctxt, indexedProps)
|
||||||
|
newScope.prototype = topLevel
|
||||||
|
newScope.put("context", newScope, ctxt)
|
||||||
|
val cx = Context.enter()
|
||||||
try {
|
try {
|
||||||
return implementor.getInterface(thiz, clasz);
|
@Language("js")
|
||||||
} catch (ScriptException e) {
|
val js = """
|
||||||
return null;
|
function print(str, newline) {
|
||||||
|
if (typeof(str) == 'undefined') {
|
||||||
|
str = 'undefined';
|
||||||
|
} else if (str == null) {
|
||||||
|
str = 'null';
|
||||||
}
|
}
|
||||||
|
var out = context.getWriter();
|
||||||
|
if (!(out instanceof java.io.PrintWriter))
|
||||||
|
out = new java.io.PrintWriter(out);
|
||||||
|
out.print(String(str));
|
||||||
|
if (newline) out.print('\\n');
|
||||||
|
out.flush();
|
||||||
}
|
}
|
||||||
|
function println(str) {
|
||||||
private static final String printSource =
|
print(str, true);
|
||||||
"function print(str, newline) { \n" +
|
|
||||||
" if (typeof(str) == 'undefined') { \n" +
|
|
||||||
" str = 'undefined'; \n" +
|
|
||||||
" } else if (str == null) { \n" +
|
|
||||||
" str = 'null'; \n" +
|
|
||||||
" } \n" +
|
|
||||||
" var out = context.getWriter(); \n" +
|
|
||||||
" if (!(out instanceof java.io.PrintWriter))\n" +
|
|
||||||
" out = new java.io.PrintWriter(out); \n" +
|
|
||||||
" out.print(String(str)); \n" +
|
|
||||||
" if (newline) out.print('\\n'); \n" +
|
|
||||||
" out.flush(); \n" +
|
|
||||||
"}\n" +
|
|
||||||
"function println(str) { \n" +
|
|
||||||
" print(str, true); \n" +
|
|
||||||
"}";
|
|
||||||
|
|
||||||
Scriptable getRuntimeScope(ScriptContext ctxt) {
|
|
||||||
if (ctxt == null) {
|
|
||||||
throw new NullPointerException("null script context");
|
|
||||||
}
|
}
|
||||||
|
""".trimIndent()
|
||||||
// we create a scope for the given ScriptContext
|
cx.evaluateString(
|
||||||
Scriptable newScope = new com.script.rhino.ExternalScriptable(ctxt, indexedProps);
|
newScope,
|
||||||
|
js,
|
||||||
// Set the prototype of newScope to be 'topLevel' so that
|
"print",
|
||||||
// JavaScript standard objects are visible from the scope.
|
1,
|
||||||
newScope.setPrototype(topLevel);
|
null as Any?
|
||||||
|
)
|
||||||
// define "context" variable in the new scope
|
|
||||||
newScope.put("context", newScope, ctxt);
|
|
||||||
|
|
||||||
// define "print", "println" functions in the new scope
|
|
||||||
Context cx = enterContext();
|
|
||||||
try {
|
|
||||||
cx.evaluateString(newScope, printSource, "print", 1, null);
|
|
||||||
} finally {
|
} finally {
|
||||||
cx.exit();
|
Context.exit()
|
||||||
|
}
|
||||||
|
newScope
|
||||||
}
|
}
|
||||||
return newScope;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Throws(ScriptException::class)
|
||||||
//Compilable methods
|
override fun compile(script: String): CompiledScript {
|
||||||
public CompiledScript compile(String script) throws ScriptException {
|
return this.compile(StringReader(script) as Reader)
|
||||||
return compile(new StringReader(script));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompiledScript compile(java.io.Reader script) throws ScriptException {
|
@Throws(ScriptException::class)
|
||||||
CompiledScript ret = null;
|
override fun compile(script: Reader): CompiledScript {
|
||||||
Context cx = enterContext();
|
val cx = Context.enter()
|
||||||
|
val ret: RhinoCompiledScript
|
||||||
try {
|
try {
|
||||||
String fileName = (String) get(ScriptEngine.FILENAME);
|
var fileName = this["javax.script.filename"] as? String
|
||||||
if (fileName == null) {
|
if (fileName == null) {
|
||||||
fileName = "<Unknown Source>";
|
fileName = "<Unknown Source>"
|
||||||
}
|
}
|
||||||
|
val scr = cx.compileReader(script, fileName, 1, null as Any?)
|
||||||
Scriptable scope = getRuntimeScope(context);
|
ret = RhinoCompiledScript(this, scr)
|
||||||
@SuppressWarnings("deprecation")
|
} catch (var9: Exception) {
|
||||||
Script scr = cx.compileReader(scope, script, fileName, 1, null);
|
throw ScriptException(var9)
|
||||||
ret = new RhinoCompiledScript(this, scr);
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (DEBUG) e.printStackTrace();
|
|
||||||
throw new ScriptException(e);
|
|
||||||
} finally {
|
} finally {
|
||||||
cx.exit();
|
Context.exit()
|
||||||
}
|
}
|
||||||
return ret;
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun wrapArguments(args: Array<Any?>?): Array<Any?> {
|
||||||
//package-private helpers
|
return if (args == null) {
|
||||||
|
Context.emptyArgs
|
||||||
static Context enterContext() {
|
} else {
|
||||||
// call this always so that initializer of this class runs
|
val res = arrayOfNulls<Any>(args.size)
|
||||||
// and initializes custom wrap factory and class shutter.
|
for (i in res.indices) {
|
||||||
return Context.enter();
|
res[i] = Context.javaToJS(args[i], topLevel)
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AccessControlContext getAccessContext() {
|
fun unwrapReturnValue(result: Any?): Any? {
|
||||||
return accCtxt;
|
var result1 = result
|
||||||
|
if (result1 is Wrapper) {
|
||||||
|
result1 = result1.unwrap()
|
||||||
|
}
|
||||||
|
return if (result1 is Undefined) null else result1
|
||||||
}
|
}
|
||||||
|
|
||||||
Object[] wrapArguments(Object[] args) {
|
init {
|
||||||
if (args == null) {
|
if (System.getSecurityManager() != null) {
|
||||||
return Context.emptyArgs;
|
try {
|
||||||
|
AccessController.checkPermission(AllPermission())
|
||||||
|
} catch (var6: AccessControlException) {
|
||||||
|
accessContext = AccessController.getContext()
|
||||||
}
|
}
|
||||||
Object[] res = new Object[args.length];
|
|
||||||
for (int i = 0; i < res.length; i++) {
|
|
||||||
res[i] = Context.javaToJS(args[i], topLevel);
|
|
||||||
}
|
}
|
||||||
return res;
|
val cx = Context.enter()
|
||||||
|
try {
|
||||||
|
topLevel = RhinoTopLevel(cx, this)
|
||||||
|
} finally {
|
||||||
|
Context.exit()
|
||||||
|
}
|
||||||
|
indexedProps = HashMap()
|
||||||
|
implementor = object : InterfaceImplementor(this) {
|
||||||
|
override fun isImplemented(thiz: Any?, iface: Class<*>): Boolean {
|
||||||
|
var thiz1 = thiz
|
||||||
|
return try {
|
||||||
|
if (thiz1 != null && thiz1 !is Scriptable) {
|
||||||
|
thiz1 = Context.toObject(
|
||||||
|
thiz1,
|
||||||
|
topLevel
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val engineScope =
|
||||||
|
getRuntimeScope(context)
|
||||||
|
val localScope =
|
||||||
|
if (thiz1 != null) thiz1 as Scriptable else engineScope
|
||||||
|
val var5 = iface.methods
|
||||||
|
val var6 = var5.size
|
||||||
|
for (var7 in 0 until var6) {
|
||||||
|
val method = var5[var7]
|
||||||
|
if (method.declaringClass != Any::class.java) {
|
||||||
|
val obj =
|
||||||
|
ScriptableObject.getProperty(localScope, method.name) as? Function
|
||||||
|
obj ?: return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
} finally {
|
||||||
|
Context.exit()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object unwrapReturnValue(Object result) {
|
override fun convertResult(method: Method?, res: Any): Any {
|
||||||
if (result instanceof Wrapper) {
|
val desiredType = method!!.returnType
|
||||||
result = ((Wrapper) result).unwrap();
|
return (if (desiredType == Void.TYPE) null else Context.jsToJava(
|
||||||
|
res,
|
||||||
|
desiredType
|
||||||
|
))!!
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result instanceof Undefined ? null : result;
|
companion object {
|
||||||
|
private const val DEBUG = false
|
||||||
|
|
||||||
|
@Language("js")
|
||||||
|
private val printSource = """
|
||||||
|
function print(str, newline) {
|
||||||
|
if (typeof str == "undefined") {
|
||||||
|
str = "undefined";
|
||||||
|
} else if (str == null) {
|
||||||
|
str = "null";
|
||||||
|
}
|
||||||
|
var out = context.getWriter();
|
||||||
|
if (!(out instanceof java.io.PrintWriter))
|
||||||
|
out = new java.io.PrintWriter(out);
|
||||||
|
out.print(String(str));
|
||||||
|
if (newline) out.print("\\n");
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
function println(str) {
|
||||||
|
print(str, true);
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
init {
|
||||||
|
ContextFactory.initGlobal(object : ContextFactory() {
|
||||||
|
override fun makeContext(): Context {
|
||||||
|
val cx = super.makeContext()
|
||||||
|
cx.languageVersion = 200
|
||||||
|
cx.optimizationLevel = -1
|
||||||
|
cx.setClassShutter(instance)
|
||||||
|
cx.wrapFactory = RhinoWrapFactory.instance
|
||||||
|
return cx
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
override fun doTopCall(
|
||||||
public static void main(String[] args) throws Exception {
|
callable: Callable,
|
||||||
if (args.length == 0) {
|
cx: Context,
|
||||||
System.out.println("No file specified");
|
scope: Scriptable,
|
||||||
return;
|
thisObj: Scriptable,
|
||||||
|
args: Array<Any>
|
||||||
|
): Any {
|
||||||
|
var accCtxt: AccessControlContext? = null
|
||||||
|
val global = ScriptableObject.getTopLevelScope(scope)
|
||||||
|
val globalProto = global.prototype
|
||||||
|
if (globalProto is RhinoTopLevel) {
|
||||||
|
accCtxt = globalProto.accessContext
|
||||||
|
}
|
||||||
|
return if (accCtxt != null) AccessController.doPrivileged(
|
||||||
|
PrivilegedAction {
|
||||||
|
superDoTopCall(
|
||||||
|
callable,
|
||||||
|
cx,
|
||||||
|
scope,
|
||||||
|
thisObj,
|
||||||
|
args
|
||||||
|
)
|
||||||
|
} as PrivilegedAction<Any>, accCtxt) else superDoTopCall(
|
||||||
|
callable,
|
||||||
|
cx,
|
||||||
|
scope,
|
||||||
|
thisObj,
|
||||||
|
args
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStreamReader r = new InputStreamReader(new FileInputStream(args[0]));
|
private fun superDoTopCall(
|
||||||
ScriptEngine engine = new RhinoScriptEngine();
|
callable: Callable,
|
||||||
|
cx: Context,
|
||||||
engine.put("x", "y");
|
scope: Scriptable,
|
||||||
engine.put(ScriptEngine.FILENAME, args[0]);
|
thisObj: Scriptable,
|
||||||
engine.eval(r);
|
args: Array<Any>
|
||||||
System.out.println(engine.get("x"));
|
): Any {
|
||||||
|
return super.doTopCall(callable, cx, scope, thisObj, args)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
@@ -22,24 +22,14 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import com.script.Bindings
|
||||||
|
import com.script.ScriptContext
|
||||||
import com.script.Bindings;
|
import com.script.SimpleScriptContext
|
||||||
import com.script.ScriptContext;
|
import org.mozilla.javascript.*
|
||||||
import com.script.SimpleScriptContext;
|
import org.mozilla.javascript.Function
|
||||||
|
import java.security.AccessControlContext
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Function;
|
|
||||||
import org.mozilla.javascript.ImporterTopLevel;
|
|
||||||
import org.mozilla.javascript.LazilyLoadedCtor;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
|
||||||
import org.mozilla.javascript.Synchronizer;
|
|
||||||
import org.mozilla.javascript.Wrapper;
|
|
||||||
|
|
||||||
import java.security.AccessControlContext;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class serves as top level scope for Rhino. This class adds
|
* This class serves as top level scope for Rhino. This class adds
|
||||||
@@ -49,129 +39,69 @@ import java.security.AccessControlContext;
|
|||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
public final class RhinoTopLevel extends ImporterTopLevel {
|
@Suppress("UNUSED_PARAMETER")
|
||||||
RhinoTopLevel(Context cx, RhinoScriptEngine engine) {
|
class RhinoTopLevel internal constructor(cx: Context?, val scriptEngine: RhinoScriptEngine) :
|
||||||
// second boolean parameter to super constructor tells whether
|
ImporterTopLevel(cx, System.getSecurityManager() != null) {
|
||||||
// to seal standard JavaScript objects or not. If security manager
|
|
||||||
// is present, we seal the standard objects.
|
|
||||||
super(cx, System.getSecurityManager() != null);
|
|
||||||
this.engine = engine;
|
|
||||||
|
|
||||||
// initialize JSAdapter lazily. Reduces footprint & startup time.
|
init {
|
||||||
new LazilyLoadedCtor(this, "JSAdapter",
|
LazilyLoadedCtor(this, "JSAdapter", "com.sun.script.javascript.JSAdapter", false)
|
||||||
"com.sun.script.javascript.JSAdapter",
|
JavaAdapter.init(cx, this, false)
|
||||||
false);
|
val names = arrayOf("bindings", "scope", "sync")
|
||||||
|
defineFunctionProperties(names, RhinoTopLevel::class.java, 2)
|
||||||
/*
|
|
||||||
* initialize JavaAdapter. We can't lazy initialize this because
|
|
||||||
* lazy initializer attempts to define a new property. But, JavaAdapter
|
|
||||||
* is an exisiting property that we overwrite.
|
|
||||||
*/
|
|
||||||
com.script.rhino.JavaAdapter.init(cx, this, false);
|
|
||||||
|
|
||||||
// add top level functions
|
|
||||||
String names[] = { "bindings", "scope", "sync" };
|
|
||||||
defineFunctionProperties(names, RhinoTopLevel.class,
|
|
||||||
ScriptableObject.DONTENUM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
val accessContext: AccessControlContext?
|
||||||
* The bindings function takes a JavaScript scope object
|
get() = scriptEngine.accessContext
|
||||||
* of type ExternalScriptable and returns the underlying Bindings
|
|
||||||
* instance.
|
companion object {
|
||||||
*
|
|
||||||
* var page = scope(pageBindings);
|
@JvmStatic
|
||||||
* with (page) {
|
fun bindings(
|
||||||
* // code that uses page scope
|
cx: Context?,
|
||||||
* }
|
thisObj: Scriptable?,
|
||||||
* var b = bindings(page);
|
args: Array<Any?>,
|
||||||
* // operate on bindings here.
|
funObj: Function?
|
||||||
*/
|
): Any {
|
||||||
public static Object bindings(Context cx, Scriptable thisObj, Object[] args,
|
if (args.size == 1) {
|
||||||
Function funObj) {
|
var arg = args[0]
|
||||||
if (args.length == 1) {
|
if (arg is Wrapper) {
|
||||||
Object arg = args[0];
|
arg = arg.unwrap()
|
||||||
if (arg instanceof Wrapper) {
|
|
||||||
arg = ((Wrapper)arg).unwrap();
|
|
||||||
}
|
}
|
||||||
if (arg instanceof com.script.rhino.ExternalScriptable) {
|
if (arg is ExternalScriptable) {
|
||||||
ScriptContext ctx = ((com.script.rhino.ExternalScriptable)arg).getContext();
|
val ctx = arg.context
|
||||||
Bindings bind = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
|
val bind = ctx.getBindings(100)
|
||||||
return Context.javaToJS(bind,
|
return Context.javaToJS(bind, getTopLevelScope(thisObj))
|
||||||
ScriptableObject.getTopLevelScope(thisObj));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cx.getUndefinedValue();
|
return Context.getUndefinedValue()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@JvmStatic
|
||||||
* The scope function creates a new JavaScript scope object
|
fun scope(cx: Context?, thisObj: Scriptable?, args: Array<Any?>, funObj: Function?): Any {
|
||||||
* with given Bindings object as backing store. This can be used
|
if (args.size == 1) {
|
||||||
* to create a script scope based on arbitrary Bindings instance.
|
var arg = args[0]
|
||||||
* For example, in webapp scenario, a 'page' level Bindings instance
|
if (arg is Wrapper) {
|
||||||
* may be wrapped as a scope and code can be run in JavaScripe 'with'
|
arg = arg.unwrap()
|
||||||
* statement:
|
|
||||||
*
|
|
||||||
* var page = scope(pageBindings);
|
|
||||||
* with (page) {
|
|
||||||
* // code that uses page scope
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
public static Object scope(Context cx, Scriptable thisObj, Object[] args,
|
|
||||||
Function funObj) {
|
|
||||||
if (args.length == 1) {
|
|
||||||
Object arg = args[0];
|
|
||||||
if (arg instanceof Wrapper) {
|
|
||||||
arg = ((Wrapper)arg).unwrap();
|
|
||||||
}
|
}
|
||||||
if (arg instanceof Bindings) {
|
if (arg is Bindings) {
|
||||||
ScriptContext ctx = new SimpleScriptContext();
|
val ctx: ScriptContext = SimpleScriptContext()
|
||||||
ctx.setBindings((Bindings)arg, ScriptContext.ENGINE_SCOPE);
|
ctx.setBindings(arg as Bindings?, 100)
|
||||||
Scriptable res = new com.script.rhino.ExternalScriptable(ctx);
|
val res: Scriptable = ExternalScriptable(ctx)
|
||||||
res.setPrototype(ScriptableObject.getObjectPrototype(thisObj));
|
res.prototype = getObjectPrototype(thisObj)
|
||||||
res.setParentScope(ScriptableObject.getTopLevelScope(thisObj));
|
res.parentScope = getTopLevelScope(thisObj)
|
||||||
return res;
|
return res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cx.getUndefinedValue();
|
return Context.getUndefinedValue()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@JvmStatic
|
||||||
* The sync function creates a synchronized function (in the sense
|
fun sync(cx: Context?, thisObj: Scriptable?, args: Array<Any?>, funObj: Function?): Any {
|
||||||
* of a Java synchronized method) from an existing function. The
|
return if (args.size == 1 && args[0] is Function) {
|
||||||
* new function synchronizes on the <code>this</code> object of
|
Synchronizer(args[0] as Function?)
|
||||||
* its invocation.
|
|
||||||
* {@code
|
|
||||||
* js> var o = { f : sync(function(x) {
|
|
||||||
* print("entry");
|
|
||||||
* Packages.java.lang.Thread.sleep(x*1000);
|
|
||||||
* print("exit");
|
|
||||||
* })};
|
|
||||||
* js> thread(function() {o.f(5);});
|
|
||||||
* entry
|
|
||||||
* js> thread(function() {o.f(5);});
|
|
||||||
* js>
|
|
||||||
* exit
|
|
||||||
* entry
|
|
||||||
* exit
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
public static Object sync(Context cx, Scriptable thisObj, Object[] args,
|
|
||||||
Function funObj) {
|
|
||||||
if (args.length == 1 && args[0] instanceof Function) {
|
|
||||||
return new Synchronizer((Function)args[0]);
|
|
||||||
} else {
|
} else {
|
||||||
throw Context.reportRuntimeError("wrong argument(s) for sync");
|
throw Context.reportRuntimeError("wrong argument(s) for sync")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RhinoScriptEngine getScriptEngine() {
|
|
||||||
return engine;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AccessControlContext getAccessContext() {
|
|
||||||
return engine.getAccessContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
private RhinoScriptEngine engine;
|
|
||||||
}
|
}
|
||||||
@@ -22,18 +22,14 @@
|
|||||||
* or visit www.oracle.com if you need additional information or have any
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
package com.script.rhino
|
||||||
|
|
||||||
package com.script.rhino;
|
import org.mozilla.javascript.Context
|
||||||
|
import org.mozilla.javascript.NativeJavaObject
|
||||||
import org.mozilla.javascript.ClassShutter;
|
import org.mozilla.javascript.Scriptable
|
||||||
import org.mozilla.javascript.Context;
|
import org.mozilla.javascript.WrapFactory
|
||||||
import org.mozilla.javascript.NativeJavaObject;
|
import java.lang.reflect.Member
|
||||||
import org.mozilla.javascript.Scriptable;
|
import java.lang.reflect.Modifier
|
||||||
import org.mozilla.javascript.WrapFactory;
|
|
||||||
|
|
||||||
import java.lang.reflect.Member;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This wrap factory is used for security reasons. JSR 223 script
|
* This wrap factory is used for security reasons. JSR 223 script
|
||||||
@@ -48,118 +44,88 @@ import java.lang.reflect.Modifier;
|
|||||||
* @author A. Sundararajan
|
* @author A. Sundararajan
|
||||||
* @since 1.6
|
* @since 1.6
|
||||||
*/
|
*/
|
||||||
final class RhinoWrapFactory extends WrapFactory {
|
internal class RhinoWrapFactory private constructor() : WrapFactory() {
|
||||||
private RhinoWrapFactory() {}
|
|
||||||
private static RhinoWrapFactory theInstance;
|
|
||||||
|
|
||||||
static synchronized WrapFactory getInstance() {
|
override fun wrapAsJavaObject(
|
||||||
if (theInstance == null) {
|
cx: Context,
|
||||||
theInstance = new RhinoWrapFactory();
|
scope: Scriptable?,
|
||||||
}
|
javaObject: Any,
|
||||||
return theInstance;
|
staticType: Class<*>?
|
||||||
}
|
): Scriptable? {
|
||||||
|
scope?.delete("Packages")
|
||||||
// We use instance of this class to wrap security sensitive
|
val sm = System.getSecurityManager()
|
||||||
// Java object. Please refer below.
|
val classShutter = RhinoClassShutter.instance
|
||||||
private static class RhinoJavaObject extends NativeJavaObject {
|
return if (javaObject is ClassLoader) {
|
||||||
RhinoJavaObject(Scriptable scope, Object obj, Class type) {
|
sm?.checkPermission(RuntimePermission("getClassLoader"))
|
||||||
// we pass 'null' to object. NativeJavaObject uses
|
super.wrapAsJavaObject(cx, scope, javaObject, staticType)
|
||||||
// passed 'type' to reflect fields and methods when
|
|
||||||
// object is null.
|
|
||||||
super(scope, null, type);
|
|
||||||
|
|
||||||
// Now, we set actual object. 'javaObject' is protected
|
|
||||||
// field of NativeJavaObject.
|
|
||||||
javaObject = obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object get(String name, Scriptable start) {
|
|
||||||
if (name.equals("getClass") || name.equals("exec")) {
|
|
||||||
return NOT_FOUND;
|
|
||||||
}
|
|
||||||
return super.get(name, start);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
|
|
||||||
Object javaObject, Class staticType) {
|
|
||||||
if (scope != null) {
|
|
||||||
scope.delete("Packages");
|
|
||||||
}
|
|
||||||
SecurityManager sm = System.getSecurityManager();
|
|
||||||
ClassShutter classShutter = RhinoClassShutter.getInstance();
|
|
||||||
if (javaObject instanceof ClassLoader) {
|
|
||||||
// Check with Security Manager whether we can expose a
|
|
||||||
// ClassLoader...
|
|
||||||
if (sm != null) {
|
|
||||||
sm.checkPermission(new RuntimePermission("getClassLoader"));
|
|
||||||
}
|
|
||||||
// if we fall through here, check permission succeeded.
|
|
||||||
return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
|
|
||||||
} else {
|
} else {
|
||||||
String name = null;
|
var name: String? = null
|
||||||
if (javaObject instanceof Class) {
|
if (javaObject is Class<*>) {
|
||||||
name = ((Class)javaObject).getName();
|
name = javaObject.name
|
||||||
} else if (javaObject instanceof Member) {
|
} else if (javaObject is Member) {
|
||||||
Member member = (Member) javaObject;
|
if (sm != null && !Modifier.isPublic(javaObject.modifiers)) {
|
||||||
// Check member access. Don't allow reflective access to
|
return null
|
||||||
// non-public members. Note that we can't call checkMemberAccess
|
|
||||||
// because that expects exact stack depth!
|
|
||||||
if (sm != null && !Modifier.isPublic(member.getModifiers())) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
name = member.getDeclaringClass().getName();
|
name = javaObject.declaringClass.name
|
||||||
}
|
}
|
||||||
// Now, make sure that no ClassShutter prevented Class or Member
|
|
||||||
// of it is accessed reflectively. Note that ClassShutter may
|
|
||||||
// prevent access to a class, even though SecurityManager permit.
|
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
if (!classShutter.visibleToScripts(name)) {
|
if (!classShutter.visibleToScripts(name)) null else super.wrapAsJavaObject(
|
||||||
return null;
|
cx,
|
||||||
|
scope,
|
||||||
|
javaObject,
|
||||||
|
staticType
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
|
var dynamicType: Class<*>? = javaObject.javaClass
|
||||||
}
|
name = dynamicType!!.name
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// we have got some non-reflective object.
|
|
||||||
Class dynamicType = javaObject.getClass();
|
|
||||||
String name = dynamicType.getName();
|
|
||||||
if (!classShutter.visibleToScripts(name)) {
|
|
||||||
// Object of some sensitive class (such as sun.net.www.*
|
|
||||||
// objects returned from public method of java.net.URL class.
|
|
||||||
// We expose this object as though it is an object of some
|
|
||||||
// super class that is safe for access.
|
|
||||||
|
|
||||||
Class type = null;
|
|
||||||
|
|
||||||
// Whenever a Java Object is wrapped, we are passed with a
|
|
||||||
// staticType which is the type found from environment. For
|
|
||||||
// example, method return type known from signature. The dynamic
|
|
||||||
// type would be the actual Class of the actual returned object.
|
|
||||||
// If the staticType is an interface, we just use that type.
|
|
||||||
if (staticType != null && staticType.isInterface()) {
|
|
||||||
type = staticType;
|
|
||||||
} else {
|
|
||||||
// dynamicType is always a class type and never an interface.
|
|
||||||
// find an accessible super class of the dynamic type.
|
|
||||||
while (dynamicType != null) {
|
|
||||||
dynamicType = dynamicType.getSuperclass();
|
|
||||||
name = dynamicType.getName();
|
|
||||||
if (classShutter.visibleToScripts(name)) {
|
if (classShutter.visibleToScripts(name)) {
|
||||||
type = dynamicType; break;
|
super.wrapAsJavaObject(cx, scope, javaObject, staticType)
|
||||||
}
|
|
||||||
}
|
|
||||||
// atleast java.lang.Object has to be accessible. So, when
|
|
||||||
// we reach here, type variable should not be null.
|
|
||||||
assert type != null:
|
|
||||||
"even java.lang.Object is not accessible?";
|
|
||||||
}
|
|
||||||
// create custom wrapper with the 'safe' type.
|
|
||||||
return new RhinoJavaObject(scope, javaObject, type);
|
|
||||||
} else {
|
} else {
|
||||||
return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
|
var type: Class<*>? = null
|
||||||
|
if (staticType != null && staticType.isInterface) {
|
||||||
|
type = staticType
|
||||||
|
} else {
|
||||||
|
while (dynamicType != null) {
|
||||||
|
dynamicType = dynamicType.superclass
|
||||||
|
name = dynamicType.name
|
||||||
|
if (classShutter.visibleToScripts(name)) {
|
||||||
|
type = dynamicType
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(type != null) { "java.lang.Object 不可访问" }
|
||||||
|
}
|
||||||
|
RhinoJavaObject(scope, javaObject, type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class RhinoJavaObject(
|
||||||
|
scope: Scriptable?,
|
||||||
|
obj: Any?,
|
||||||
|
type: Class<*>?
|
||||||
|
) : NativeJavaObject(scope, null as Any?, type) {
|
||||||
|
init {
|
||||||
|
javaObject = obj
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun get(name: String, start: Scriptable): Any {
|
||||||
|
return if (name != "getClass" && name != "exec") super.get(name, start) else NOT_FOUND
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private var theInstance: RhinoWrapFactory? = null
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@get:Synchronized
|
||||||
|
val instance: WrapFactory?
|
||||||
|
get() {
|
||||||
|
if (theInstance == null) {
|
||||||
|
theInstance = RhinoWrapFactory()
|
||||||
|
}
|
||||||
|
return theInstance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user