The HertzScript virtual machine context executes code that was compiled by the HertzScript Compiler.
See the HertzScript Specification repository for more information.
Other Module Docs are in the works soon.
-
Context module
The context module can be run in multiple different execution modes:
- Run-To-Completion
- Synchronous quantum/time-slicing
- Asynchronous quantum/time-slicing
The context itself is the main module, so you can import it simply like this:
const Context = require("hertzscript-context");
// Instantiate the Class
const context = new Context();
Imports a pre-compiled HertzScript Module into the context, spawning it as a new coroutine. The module is not immediately executed with this method and is only enqueued for later execution.
context.import( hzModule );
hzModule
Function
- A
Function
which was previously compiled withhertzscript-compiler
inmodule
mode.
This method immediately executes the given pre-compiled function or HzModule in run-to-completion mode.
The returned value is the last value returned by the top-most function.
context.exec( functor [, thisArg = null [, args = null ]] );
functor
Function
- A function or HzModule which was previously compiled with
hertzscript-compiler
.
thisArg
(Optional) Any
- An optional value to set the
this
variable to when callingfunctor
.
args
(Optional) Array
- An optional array to supply as arguments when calling
functor
.
Enqueues a new coroutine. The coroutine is not immediately executed with this method and is only enqueued for later execution.
context.spawn( functor [, thisArg = null [, args = null ]] );
functor
Function
- A function which was previously compiled with
hertzscript-compiler
.
thisArg
(Optional) Any
- An optional value to set the
this
variable to when callingfunctor
.
args
(Optional) Array
- An optional array to supply as arguments when calling
functor
.
Adds a pre-compiled function to the end of the active coroutine's call stack, or creates a new coroutine for it if there is no active coroutine. The coroutine is not immediately executed with this method and is only enqueued for later execution.
context.enqueue( functor [, thisArg = null [, args = null ]] );
functor
Function
- A function which was previously compiled with
hertzscript-compiler
.
thisArg
(Optional) Any
- An optional value to set the
this
variable to when callingfunctor
.
args
(Optional) Array
- An optional array to supply as arguments when calling
functor
.
If the context is running, then this method cycles the context for a given duration.
The returned value is the last value returned in the most recent cycle.
context.cycle( [ quantum = null [, throwUp = false ]] );
quantum
(Optional) Number
- A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the context should cycle.
throwUp
(Optional) Boolean
- This argument affects how the context handles uncaught errors. If set to
true
then the context will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.
Sets the context to running state and cycles it for a given duration.
The returned value is the last value returned in the most recent cycle.
context.dispatch( [ quantum = null [, throwUp = false ]] );
quantum
(Optional) Number or Boolean
- A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the context should cycle. If set to
false
then the context will continue cycling until all coroutines have finished executing.
throwUp
(Optional) Boolean
- This argument affects how the context handles uncaught errors. If set to
true
then the context will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.
Sets the context to running state and cycles it in run-to-completion mode, only returning when all coroutines have finished executing.
The returned value is the last value returned by the top-most function.
context.dispatchAll( [ throwUp = false ] );
throwUp
(Optional) Boolean
- This argument affects how the context handles uncaught errors. If set to
true
then the context will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.