The ScriptLoader is a convienence container for instantiating and managing ScriptInstances.
To load a script from a file and run it:
ScriptLoader loader = new ScriptLoader();
ScriptInstance script = loader.loadScript("script.sl");
script.runScript();
The above will load the file script.sl and then execute it immediately.
Installation of loadable bridges you create can also be managed by the ScriptLoader.
A loadable bridge is installed into the language by adding it to a script loader class. There are two types of
bridges. The two types are specific and global bridges.
The load and unload methods for a
specific bridge are executed for every script load and unload, no matter
what.
A
global bridge is installed once for each script environment. If scripts are sharing an environment there is
no sense in loading stuff into the environment more than once. This is why global bridges exist.
An example of adding a loadable bridge to a script loader:
ScriptLoader loader = new ScriptLoader()
loader.addSpecificBridge(new MyLoadableBridge());
There is a difference between "loading" and "compiling" a script:
This class contains several methods to either load or compile a script. Loading a script instantiates a script environment,
registers the script with the script loader, and registers all of the appropriate bridges with the script on top of compiling
the script.
To compile a script means to produce a runnable Block of code. On its own a Block is not really runnable as a script
environment is needed. For functions like eval(), include(), etc.. it makes sense to compile a script as one may want to run
the block of code within the environment of the calling script. Using the compile method saves on the overhead of unnecessary
script environment creation and bridge registration.
Hopefully this helped to clarify things. :)
BLOCK_CACHE
protected static HashMap BLOCK_CACHE
cache for parsed scripts mantained (optionally) by the script loader.
bridgesg
protected LinkedList bridgesg
global bridges
bridgess
protected LinkedList bridgess
specific bridges
disableConversions
protected boolean disableConversions
loadedScripts
protected LinkedList loadedScripts
loaded scripts
paths
protected LinkedList paths
path to search for jar files imported using [import * from: *] syntax
scripts
protected HashMap scripts
loaded scripts except referable by key
addGlobalBridge
public void addGlobalBridge(Loadable l)
A global bridge is loaded into an environment once and only once. This way if the environment is shared among multiple
script instances this will save on both memory and script load time
addSpecificBridge
public void addSpecificBridge(Loadable l)
A specific bridge is loaded into *every* script regardless of wether or not the environment is shared. Useful for
modifying the script instance while it is being in processed. Specific bridges are the first thing that happens after
the script code is parsed
compileScript
public Block compileScript(String name,
InputStream stream)
throws YourCodeSucksException,
IOException
compiles a script using the specified stream as a source
compileScript
public Block compileScript(String name,
String code)
throws YourCodeSucksException
compiles the specified script into a runnable block
getCharset
public String getCharset()
getFirstScriptEnvironment
public ScriptEnvironment getFirstScriptEnvironment()
Convienence method to return the script environment of the first script tht was loaded, returns null if no scripts are loaded
getScripts
public LinkedList getScripts()
Returns a linked list of all loaded ScriptInstance objects
getScriptsByKey
public HashMap getScriptsByKey()
Returns a HashMap with all loaded scripts, the key is a string which is just the filename, the value is a ScriptInstance
object
getScriptsToLoad
public Set getScriptsToLoad(Set configured)
A convienence method to determine the set of scripts to "load" based on a passed in set of scripts that are currently
configured. The configured scripts are compared to the loaded scripts. Scripts that are configured but not loaded
are determined to be in need of loading. The return Set contains String objects of the script names. The passed in
Set is expected to be the same thing (a bunch of Strings).
getScriptsToUnload
public Set getScriptsToUnload(Set configured)
A convienence method to determine the set of scripts to "unload" based on a passed in set of scripts that are currently
configured. The configured scripts are compared to the loaded scripts. Scripts that are loaded but not configured are
determined to be in need of unloading. The return Set contains String objects of the script names. The passed in Set is
expected to be the same thing (a bunch of Strings).
initDefaultBridges
protected void initDefaultBridges()
method call to initialize the default bridges, if you want to change the default bridges subclass this class and
override this method
isCharsetConversions
public boolean isCharsetConversions()
isLoaded
public boolean isLoaded(String name)
Determines wether or not the script is loaded by checking if the specified key exists in the script db.
loadScript
public ScriptInstance loadScript(File file,
Hashtable env)
throws IOException,
YourCodeSucksException
Loads the specified script file, uses the specified hashtable for the environment
loadScript
public ScriptInstance loadScript(String fileName,
Hashtable env)
throws IOException,
YourCodeSucksException
Loads the specified script file, uses the specified hashtable for the environment
loadScript
public ScriptInstance loadScript(String name,
InputStream stream,
Hashtable env)
throws YourCodeSucksException,
IOException
loads a script from the specified input stream using the specified hashtable as a shared environment
loadSerialized
public ScriptInstance loadSerialized(File script,
Hashtable env)
throws IOException,
ClassNotFoundException
Load a serialized version of the script iff a serialized version exists, and its modification time is greater than the
modification time of the script. Also handles the muss and fuss of reserializing the script if it has to reload the
script. Personally I didn't find much of a startup time decrease when loading the scripts serialized versus parsing them
each time. Theres a command 'bload' in the console to benchmark loading a script normally versus serialized. Try it.
script
- a file object pointing to the script file...
loadSerialized
public ScriptInstance loadSerialized(String name,
InputStream stream,
Hashtable env)
throws IOException,
ClassNotFoundException
Loads a serialized script from the specified input stream with the specified name
saveSerialized
public static void saveSerialized(ScriptInstance si)
throws IOException
Saves a serialized version of the compiled script to scriptname.bin.
saveSerialized
public static void saveSerialized(ScriptInstance si,
OutputStream stream)
throws IOException
Saves a serialized version of the ScriptInstance si to the specified output stream
setCharset
public void setCharset(String charset)
If charset conversion is enabled and charset is set, then the stream will be read using specified charset.
charset
- The name of a supported
charset
setCharsetConversion
public void setCharsetConversion(boolean b)
Java by default maps characters from an 8bit ascii file to an internal 32bit unicode representation. How this mapping is done is called a character set encoding. Sometimes this conversion can frustrate scripters making them say "hey, I didn't put that character in my script". You can use this option to ensure sleep disables charset conversions for scripts loaded with this script loader
setGlobalCache
public HashMap setGlobalCache(boolean setting)
The Sleep script loader can optionally cache parsed script files once they are loaded. This is useful if you will have
several script loader instances loading the same script files in isolated objects.
unloadScript
public void unloadScript(String filename)
unload a script
unloadScript
public void unloadScript(ScriptInstance script)
unload a script