oscript.OscriptInterpreter
class provides static eval methods that
provide your primary interface to the interpreter. Also useful will be
oscript.data.Value
.
In general all the methods you will need to access a script value are
in Value
, and they are overloaded by various
subclasses as needed. You shouldn't really have any need to cast a
Value to any other type.
import oscript.data.Scope; import oscript.data.BasicScope; import oscript.data.JavaBridge; import oscript.OscriptInterpreter; ... Scope a = new BasicScope( OscriptInterpreter.getGlobalScope() ); Scope b = new BasicScope( OscriptInterpreter.getGlobalScope() ); OscriptInterpreter.eval( "var foo = 1;", a ); OscriptInterpreter.eval( "foo;", b ). opAssign( JavaBridge.convertToScriptObject(1) );
foo;
in the different scope will cause a NoSuchMember
exception (packaged in a PackagedScriptObjectException
) to be thrown,
because that variable has not been defined in scope b
.1
oscript.Shell
.
oscript.data.Value
provide a mechanism to access the value of a script object as various types of java objects.
Of course not all script objects will be accessible as any given java object, for example
the string "foo"
cannot be accessed as an integer number, and attempts to
access it as such would result in a script object (wrapped in a PackagedScriptObjectException
)
being thrown.
castToBoolean()
returns boolean
castToInexactNumber()
returns double
castToExactNumber()
returns long
castToString()
returns java.lang.String
castToJavaObject()
returns java.lang.Object
elementAt(oscript.data.Value)
returns oscript.data.Value
callAsFunction(oscript.data.Value[])
returns the value returned from the function, oscript.data.Value
callAsConstructor(oscript.data.Value[])
returns the newly constructed object, oscript.data.Value
oscript.data.JavaBridge
contains various static convertToScriptObject
methods to convert various types of
java objects to script objects:
convertToScriptObject(boolean)
returns oscript.data.Value
convertToScriptObject(double)
returns oscript.data.Value
convertToScriptObject(long)
returns oscript.data.Value
convertToScriptObject(java.lang.String)
returns oscript.data.Value
convertToScriptObject(java.lang.Object)
returns oscript.data.Value
PackagedScriptObjectException
is a subclass of java.lang.RuntimeException
, so it is not necessarily
explicitly declared to be thrown by methods. Since all script exceptions get wrapped by
PackagedScriptObjectException
, any time any method gets called
that results in script code being evaluated, PackagedScriptObjectException
may be thrown. This doesn't mean that it should be necessarily caught (for example
java code called by script code may not want to catch the exception, because that
would prevent the script code calling the java code from catching it), just that
care should be taken to use finally
where necessary.