Package delight.rhinosandox.internal
Class RhinoSandboxImpl
java.lang.Object
delight.rhinosandox.internal.RhinoSandboxImpl
- All Implemented Interfaces:
RhinoSandbox
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interfaceFunctional interface for configuring the execution scope. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected SafeClassShutterprotected SafeContextprotected static final Objectprotected org.mozilla.javascript.ScriptableObjectprotected intprotected longprotected org.mozilla.javascript.ScriptableObjectprotected booleanprotected boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionWill allow access to this class in Rhino scripts.voidsee https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scopes_and_ContextsvoidassertSafeScope(org.mozilla.javascript.Context context) callFunction(org.mozilla.javascript.NativeFunction function, Object[] args) executes a javascript functionEvaluate a script with its own scope.Evaluate a script with its own scope.Evaluate a script with its own scope.Evaluate a script with its own scope.Evaluates JavaScript code with a custom Scriptable object as the execution scope.evalWithGlobalScope(String sourceName, Reader js) Evaluate the given script with the global scope.evalWithGlobalScope(String sourceName, String js) Evaluate the given script with the global scope.private ObjectevalWithScope(String sourceName, Reader js, RhinoSandboxImpl.ScopeConfigurer scopeConfigurer) Common evaluation logic for Reader input that sets up the execution context and delegates scope-specific configuration to a callback function.private ObjectevalWithScope(String sourceName, String js, RhinoSandboxImpl.ScopeConfigurer scopeConfigurer) Common evaluation logic that sets up the execution context and delegates scope-specific configuration to a callback function.protected SafeContextCreates a new SafeContext instance.Will make this class available to instantiate in Rhino scripts.Will add a global variable available to all scripts executed with this sandbox.protected voidsetInstructionLimit(int limit) Sets the maximum instructions allowed for script execution.setMaxDuration(int limitInMs) Sets the maximum allowed duration for scripts.setUseSafeStandardObjects(boolean useSafeStandardObjects) If .initSafeStandardObjects should be used.setUseSealedScope(boolean value) If the global scope should be sealed (default: true).
-
Field Details
-
contextFactory
-
globalScope
protected org.mozilla.javascript.ScriptableObject globalScope -
safeScope
protected org.mozilla.javascript.ScriptableObject safeScope -
instructionLimit
protected int instructionLimit -
maxDuration
protected long maxDuration -
useSafeStandardObjects
protected boolean useSafeStandardObjects -
sealScope
protected boolean sealScope -
inScope
-
classShutter
-
ctxFactoryLock
-
-
Constructor Details
-
RhinoSandboxImpl
public RhinoSandboxImpl()
-
-
Method Details
-
getSafeContext
Creates a new SafeContext instance. Subclasses can override this method to provide custom SafeContext implementations while preserving all configuration logic.- Returns:
- a new SafeContext instance
-
assertContextFactory
public void assertContextFactory()see https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scopes_and_Contexts -
assertSafeScope
public void assertSafeScope(org.mozilla.javascript.Context context) -
evalWithGlobalScope
Description copied from interface:RhinoSandboxEvaluate the given script with the global scope. That is all new global variables written will be available to all other scripts.- Specified by:
evalWithGlobalScopein interfaceRhinoSandbox
-
evalWithGlobalScope
Description copied from interface:RhinoSandboxEvaluate the given script with the global scope. That is all new global variables written will be available to all other scripts.- Specified by:
evalWithGlobalScopein interfaceRhinoSandbox- Throws:
IOException
-
eval
Description copied from interface:RhinoSandboxEvaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.
variablesdefines variables with Java objects which will be available for the execution of this script.- Specified by:
evalin interfaceRhinoSandbox
-
eval
public Object eval(String sourceName, String js, org.mozilla.javascript.Scriptable scriptableObject) Evaluates JavaScript code with a custom Scriptable object as the execution scope. This method provides more flexibility by allowing custom Scriptable objects while maintaining all security features of the sandbox.- Specified by:
evalin interfaceRhinoSandbox- Parameters:
sourceName- the name of the source file or contextjs- the JavaScript code to evaluatescriptableObject- the Scriptable object to use as the execution scope- Returns:
- the result of the JavaScript evaluation
-
evalWithScope
private Object evalWithScope(String sourceName, String js, RhinoSandboxImpl.ScopeConfigurer scopeConfigurer) Common evaluation logic that sets up the execution context and delegates scope-specific configuration to a callback function.- Parameters:
sourceName- the name of the source file or contextjs- the JavaScript code to evaluatescopeConfigurer- callback that configures the execution scope- Returns:
- the result of the JavaScript evaluation
-
evalWithScope
private Object evalWithScope(String sourceName, Reader js, RhinoSandboxImpl.ScopeConfigurer scopeConfigurer) throws IOException Common evaluation logic for Reader input that sets up the execution context and delegates scope-specific configuration to a callback function.- Parameters:
sourceName- the name of the source file or contextjs- the JavaScript reader to evaluatescopeConfigurer- callback that configures the execution scope- Returns:
- the result of the JavaScript evaluation
- Throws:
IOException- if there's an error reading from the Reader
-
eval
Description copied from interface:RhinoSandboxEvaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.
variablesdefines variables with Java objects which will be available for the execution of this script.- Specified by:
evalin interfaceRhinoSandbox- Throws:
IOException
-
eval
Description copied from interface:RhinoSandboxEvaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.- Specified by:
evalin interfaceRhinoSandbox
-
callFunction
Description copied from interface:RhinoSandboxexecutes a javascript function- Specified by:
callFunctionin interfaceRhinoSandbox- Parameters:
function- a Native function you may got from jsargs- parameters you need to call the function- Returns:
- the result of the javascript function
-
eval
Description copied from interface:RhinoSandboxEvaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.- Specified by:
evalin interfaceRhinoSandbox- Throws:
IOException
-
setInstructionLimit
Description copied from interface:RhinoSandboxSets the maximum instructions allowed for script execution.- Specified by:
setInstructionLimitin interfaceRhinoSandbox
-
setMaxDuration
Sets the maximum allowed duration for scripts.- Specified by:
setMaxDurationin interfaceRhinoSandbox
-
setUseSafeStandardObjects
Description copied from interface:RhinoSandboxIf .initSafeStandardObjects should be used.- Specified by:
setUseSafeStandardObjectsin interfaceRhinoSandbox
-
allow
Description copied from interface:RhinoSandboxWill allow access to this class in Rhino scripts.
Note that for classes in packages which don't start with java., com., net. etc. the class name needs to be prefixed with Packages.
e.g. mypackage.Myclass will be Packages.mypackage.MyClass
- Specified by:
allowin interfaceRhinoSandbox
-
inject
Description copied from interface:RhinoSandboxWill make this class available to instantiate in Rhino scripts.- Specified by:
injectin interfaceRhinoSandbox
-
inject
Description copied from interface:RhinoSandboxWill add a global variable available to all scripts executed with this sandbox.- Specified by:
injectin interfaceRhinoSandbox
-
injectInt
-
setUseSealedScope
Description copied from interface:RhinoSandboxIf the global scope should be sealed (default: true).- Specified by:
setUseSealedScopein interfaceRhinoSandbox
-