-
Notifications
You must be signed in to change notification settings - Fork 0
Loading scripts
For standard Hscript scripts...
This is a tiny wrapper for the Hscript script interpreter and parser, and allows you to easily execute code from a string!
The following example will execute script code from a file...
var path:String = 'test/scripts/testScript.hxs';
var script:Script = new Script(File.getContent(path), path); // note Scripts are parsed immediately on creation.
script.start(); // this starts up the script program!Scripts have the methods onParsingError and onProgramError that are called when the script fails to parse and to initialize, respectively. These methods only log to the console by default, but are dynamic and can be overridden for custom behavior.
To call a method defined in a Script, use [Script].call('functionName', [argument1, argument2, ...]); and to get it's variables, you can use the [Script].variables map.
Unlike script files, if you want to load modules automatically (like Haxe's source folder), you must implement this yourself; however, this library offers a few classes to handle the more heavy work of reading and initializing them...
The Environment is a base that allows all modules and scripts within it to know and be able to import each other!
var env:Environment = new Environment([module1, module2, ...]);The first (and only) argument of an Environment expects an array of Modules, so you can create your Modules before the Environment - although that is the recommended (and more performant) way of creating an Environment, this argument can be omitted and [Environment].addModule(module) can be used to manually add modules instead.
This class is similar to a Script, but is used to load module code instead.
The following example will parse module code from a file...
var path:String = 'test/source/TestModule.hxs';
var module:Module = new Module(File.getContent(path), 'TestModule', ['my', 'package'], path);The second argument is the name of the Module, and the third argument serves to assert the package path of the module (ex. my.package -> ['my', 'package']); an error will be thrown should there be a mismatch.
A subModules array, which contains more Modules, can be set to import this module into other modules and to use import modules, similarly to Haxe.
Like Script, this class has the methods onParsingError and onProgramError, with the addition of onTypeError, that is called when a type within this Module fails to initialize.
insanity.ImportModule [extends Module]
It's functionality is identical to Haxe's import modules.
var path:String = 'test/source/import.hxs';
var importModule:ImportModule = new ImportModule(File.getContent(path), path);When in a Module's sub-modules array, all import and using statements will apply on said Module.
Once all modules are added to an environment, simply call [Environment].start() to finish initializing all types!
If you want to make all of your types importable in your Scripts, include the respective Environment in the third argument of their constructors!
At last, the following (relatively...) small example will load script files located in a folder named "test/scripts", and module files in "test/source", with the ".hxs" format (to distinguish them from ".hx")!
import sys.io.File;
import sys.FileSystem;
import insanity.Environment;
import insanity.ImportModule;
import insanity.Module;
import insanity.Script;
using StringTools;
public var environment:Environment;
public var scripts:Array<Script> = [];
final scriptsPath:String = 'test/scripts';
final sourcePath:String = 'test/source';
final fileType:String = '.hxs';
var modules:Array<Module> = [];
if (FileSystem.exists(sourcePath) && FileSystem.isDirectory(sourcePath)) {
function readModules(dir:String, ?subModules:Array<Module>) {
subModules = (subModules?.copy() ?? []);
var subDirectories:Array<String> = [];
for (file in FileSystem.readDirectory(dir)) {
final path:String = '$dir/$file';
if (FileSystem.isDirectory(path)) {
subDirectories.push(path);
} else if (file == 'import$fileType') {
subModules.push(new ImportModule(File.getContent(path), path));
} else if (file.endsWith(fileType)) {
var pack:Array<String> = dir.replace('$sourcePath/', '').replace(sourcePath, '').split('/');
if (pack[0].length == 0) pack.shift();
var module:Module = new Module(File.getContent(path), file.replace(fileType, ''), pack, path);
module.subModules = subModules;
subModules.push(module);
modules.push(module);
}
}
// this is done last to ensure all types from the current package can be imported into the sub-packages
for (sub in subDirectories)
readModules(sub, subModules);
}
readModules(sourcePath);
}
environment = new Environment(modules);
environment.start();
if (FileSystem.exists(scriptsPath) && FileSystem.isDirectory(scriptsPath)) {
for (file in FileSystem.readDirectory(scriptsPath)) {
if (!file.endsWith(fileType)) continue;
final script:Script = new Script(File.getContent('$scriptsPath/$file'), '$scriptsPath/$file', environment);
if (script.interp != null) script.start();
scripts.push(script);
}
}HscriptInsanity by emi3