loader: Dynamic loading of Alore code (deprecated)

Note: This module is deprecated and will be removed in the future. The compiler module will replace it.

This module enables dynamic compilation of Alore source code files. Dynamically loaded source files must follow the same conventions as ordinary Alore programs. They may import any modules imported by the program that loads the file and additional modules, potentially located in different directories.

Note: It is not safe to load source code from untrusted sources. Malicious code may read or delete your personal data files.

Functions

Load(path as Str[, modulePath as Str]) as Module
Load an Alore source file. Return a Module object that allows accessing public variables, functions and classes defined in the file. If compilation fails, a CompileError exception will be raised.

By default, the constant ModuleSearchPath specifies the module search path. The optional argument can be provided to override the default module search path. The format of the argument is the same as the format of the ModuleSearchPath constant.

Constants

ModuleSearchPath as Str
The default module search path. It is a string that contains module search directories separated by os::PathSeparator. The directories are searched from first to last to find imported modules.

Note: This constant is empty ("") if the program currently being executed is standalone, i.e. it was compiled using alorec.

Class Module

The Module class provides these members and operations:

contents() as Array<Str>
Return an array of the names of public global definitions in the source file. This includes variable, constant, function and class definitions. The order of the result is unspecified.
module[name] (Module[Str] ⇒ dynamic)
Return the value of public global definition. The name argument is the name of the definition as a string. Raise an KeyError exception if there is no public global definition with the specified name.

Exception CompileError

class CompileError(errorMessages as Array<Str>)
Construct a CompileError instance. Messages should be an array of error message strings. CompileError inherits from std::Exception.

CompileError members

errorMessages as Array<Str>
Constant member that contains compilation error messages as an array of strings.

Shared modules and garbage collection

Dynamically loaded code may not compile new copies of any already imported modules. Therefore any global variables in modules are shared between all source files in a program, and module initialization is performed only once per module.

As an exception to this rule, if only a dynamically loaded source file or module refers to a module, and all references to code that may access this module (including Module objects, function objects and class instances) are removed, the garbage collector of the Alore runtime may free and destroy the module. In this case, a new copy of the module can be imported later.