Skip to content

Commit 3a2828d

Browse files
committed
Major refactor, deleting Dispatcher
1 parent 61c26e2 commit 3a2828d

11 files changed

+436
-268
lines changed

README.md

+40-104
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,56 @@
1-
# HertzScript Coroutine Dispatcher
1+
# HertzScript Virtual Machine
22

3-
[![NPM](https://nodei.co/npm/hertzscript-dispatcher.png)](https://nodei.co/npm/hertzscript-dispatcher/)
3+
[![NPM](https://nodei.co/npm/hertzscript-vm.png)](https://nodei.co/npm/hertzscript-vm/)
44

5-
The HertzScript coroutine dispatcher executes code that was compiled by the [HertzScript Compiler](https://github.com/hertzscript/Compiler).
5+
The HertzScript virtual machine context executes code that was compiled by the [HertzScript Compiler](https://github.com/hertzscript/Compiler).
66

77
See the [HertzScript Specification](https://github.com/hertzscript/Specification) repository for more information.
88

9-
- [Command-Line Interfaces](#command-line-interfaces)
10-
- [`hzs` / `hzscript`](#hzs--hzscript)
11-
- [Dispatcher Module](#dispatcher-module)
12-
- [`import`](#dispatcherprototypeimport)
13-
- [`exec`](#dispatcherprototypeexec)
14-
- [`spawn`](#dispatcherprototypespawn)
15-
- [`enqueue`](#dispatcherprototypeenqueue)
16-
- [`cycle`](#dispatcherprototypecycle)
17-
- [`runSync`](#dispatcherprototyperunsync)
18-
- [`runAsync`](#dispatcherprototyperunasync)
19-
- [`runComplete`](#dispatcherprototyperuncomplete)
9+
Other Module Docs are in the works soon.
2010

11+
- [Context Module](#context-module)
12+
- [`import`](#contextprototypeimport)
13+
- [`exec`](#contextprototypeexec)
14+
- [`enqueue`](#contextprototypeenqueue)
15+
- [`cycle`](#contextprototypecycle)
16+
- [`dispatch`](#contextprototypedispatch)
17+
- [`dispatchAll`](#contextprototypedispatchall)
2118

22-
There are multiple ways to use this dispatcher.
19+
- Context module
2320

24-
- Command-line executor
25-
- Dispatcher module
26-
27-
The dispatcher module can be run in multiple different execution modes:
21+
The context module can be run in multiple different execution modes:
2822

2923
- Run-To-Completion
3024
- Synchronous quantum/time-slicing
3125
- Asynchronous quantum/time-slicing
3226

33-
## Command-Line Interfaces
34-
35-
### `hzs` / `hzscript`
36-
37-
Execute source code with optional hot-compiling via `stdin`, argument, or file input.
38-
39-
#### Command-Line Options
40-
41-
`-i` *path* (`--input`)
42-
43-
- This option supplies the text from the given filepath as the input JavaScript source code you would like to execute. If this option is set to nothing, or is not set at all, then the source code is consumed via the standard input of the terminal.
44-
45-
`-s` *code* (`--source`)
46-
47-
- If set, this option specifies the tring which follows as the input JavaScript source code which you would like to execute. If this option is set to nothing, or is not set at all, then the source code is consumed via the standard input of the terminal.
48-
49-
`-a` *quantum* (`--async`)
50-
51-
- If set, this option specifies the time slice quantum of the dispatcher so that it cooperates with the built-in asynchronous event loop. The dispatcher will pause the running code when the duration of its execution reaches or exceeds the time slice, allowing the event loop to continue operating. 5 milliseconds after pausing, the dispatcher will resume execution until the time slice quantum is reached again.
52-
53-
`-c` (`--compile`)
54-
55-
- If set, then `hertzscript-compiler` will pre-process the input source code before it is executed. Useful for quick testing without having to manually compile to another file every time you want to execute the script.
56-
57-
`--spawn`
58-
59-
- If set in addition to the `--compile` option, then `hertzscript-compiler` will be used to detect and compile the `spawn` keyword. If the `--compile` option is not present, then this option will do nothing.
60-
6127
---
6228

63-
## Dispatcher Module
29+
## Context Module
6430

65-
The dispatcher itself is the main module, so you can import it simply like this:
31+
The context itself is the main module, so you can import it simply like this:
6632

6733
```JavaScript
68-
const Dispatcher = require("hertzscript-dispatcher");
34+
const Context = require("hertzscript-context");
6935
// Instantiate the Class
70-
const dispatcher = new Dispatcher();
36+
const context = new Context();
7137
```
7238

73-
### `Dispatcher.prototype.import`
39+
### `Context.prototype.import`
7440

75-
Imports a pre-compiled HertzScript Module into the dispatcher, spawning it as a new coroutine. The module is not immediately executed with this method and is only enqueued for later execution.
41+
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.
7642

7743
#### Function Parameters
7844

7945
```JavaScript
80-
dispatcher.import( hzModule );
46+
context.import( hzModule );
8147
```
8248

8349
`hzModule` Function
8450

8551
- A `Function` which was previously compiled with `hertzscript-compiler` in `module` mode.
8652

87-
### `Dispatcher.prototype.exec`
53+
### `Context.prototype.exec`
8854

8955
This method immediately executes the given pre-compiled function or HzModule in run-to-completion mode.
9056

@@ -95,7 +61,7 @@ The returned value is the last value returned by the top-most function.
9561
#### Function Parameters
9662

9763
```JavaScript
98-
dispatcher.exec( functor [, thisArg = null [, args = null ]] );
64+
context.exec( functor [, thisArg = null [, args = null ]] );
9965
```
10066

10167
`functor` Function
@@ -110,14 +76,14 @@ dispatcher.exec( functor [, thisArg = null [, args = null ]] );
11076

11177
- An optional array to supply as arguments when calling `functor`.
11278

113-
### `Dispatcher.prototype.spawn`
79+
### `Context.prototype.spawn`
11480

11581
Enqueues a new coroutine. The coroutine is not immediately executed with this method and is only enqueued for later execution.
11682

11783
#### Function Parameters
11884

11985
```JavaScript
120-
dispatcher.spawn( functor [, thisArg = null [, args = null ]] );
86+
context.spawn( functor [, thisArg = null [, args = null ]] );
12187
```
12288

12389
`functor` Function
@@ -132,14 +98,14 @@ dispatcher.spawn( functor [, thisArg = null [, args = null ]] );
13298

13399
- An optional array to supply as arguments when calling `functor`.
134100

135-
### `Dispatcher.prototype.enqueue`
101+
### `Context.prototype.enqueue`
136102

137103
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.
138104

139105
#### Function Parameters
140106

141107
```JavaScript
142-
dispatcher.enqueue( functor [, thisArg = null [, args = null ]] );
108+
context.enqueue( functor [, thisArg = null [, args = null ]] );
143109
```
144110

145111
`functor` Function
@@ -154,9 +120,9 @@ dispatcher.enqueue( functor [, thisArg = null [, args = null ]] );
154120

155121
- An optional array to supply as arguments when calling `functor`.
156122

157-
### `Dispatcher.prototype.cycle`
123+
### `Context.prototype.cycle`
158124

159-
If the dispatcher is running, then this method cycles the dispatcher for a given duration.
125+
If the context is running, then this method cycles the context for a given duration.
160126

161127
#### Return Value
162128

@@ -165,20 +131,20 @@ The returned value is the last value returned in the most recent cycle.
165131
#### Function Parameters
166132

167133
```JavaScript
168-
dispatcher.cycle( [ quantum = null [, throwUp = false ]] );
134+
context.cycle( [ quantum = null [, throwUp = false ]] );
169135
```
170136

171137
`quantum` (*Optional*) Number
172138

173-
- A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the dispatcher should cycle.
139+
- A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the context should cycle.
174140

175141
`throwUp` (*Optional*) Boolean
176142

177-
- This argument affects how the dispatcher handles uncaught errors. If set to `true` then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.
143+
- 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.
178144

179-
### `Dispatcher.prototype.runSync`
145+
### `Context.prototype.runSync`
180146

181-
Sets the dispatcher to running state and cycles it for a given duration.
147+
Sets the context to running state and cycles it for a given duration.
182148

183149
#### Return Value
184150

@@ -187,46 +153,20 @@ The returned value is the last value returned in the most recent cycle.
187153
#### Function Parameters
188154

189155
```JavaScript
190-
dispatcher.runSync( [ quantum = null [, throwUp = false ]] );
156+
context.dispatch( [ quantum = null [, throwUp = false ]] );
191157
```
192158

193159
`quantum` (*Optional*) Number or Boolean
194160

195-
- A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the dispatcher should cycle. If set to `false` then the dispatcher will continue cycling until all coroutines have finished executing.
161+
- 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.
196162

197163
`throwUp` (*Optional*) Boolean
198164

199-
- This argument affects how the dispatcher handles uncaught errors. If set to `true` then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.
165+
- 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.
200166

201-
### `Dispatcher.prototype.runAsync`
167+
### `Context.prototype.dispatchAll`
202168

203-
Sets the dispatcher to running state and cycles it for a given duration in a given interval, scheduling cycles in the acynchronous event loop using `setTimeout`. Can be stopped via `Dispatcher.prototype.stop`.
204-
205-
#### Return Value
206-
207-
Either a `Promise` which resolves with the returned value is the last value returned by the top-most function, or `undefined` if this method is already running.
208-
209-
#### Function Parameters
210-
211-
```JavaScript
212-
dispatcher.runAsync( [ interval = 30 [,quantum = null [, throwUp = false ]]] );
213-
```
214-
215-
`interval` (*Optional*) Number
216-
217-
- A cycle delay, in milliseconds, which specifies the length of time to wait in between cycles. During this time, other functions in the asynchronous event loop may execute.
218-
219-
`quantum` (*Optional*) Number
220-
221-
- A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the dispatcher should cycle per-interval.
222-
223-
`throwUp` (*Optional*) Boolean
224-
225-
- This argument affects how the dispatcher handles uncaught errors. If set to `true` then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.
226-
227-
### `Dispatcher.prototype.runComplete`
228-
229-
Sets the dispatcher to running state and cycles it in run-to-completion mode, only returning when all coroutines have finished executing.
169+
Sets the context to running state and cycles it in run-to-completion mode, only returning when all coroutines have finished executing.
230170

231171
#### Return Value
232172

@@ -235,13 +175,9 @@ The returned value is the last value returned by the top-most function.
235175
#### Function Parameters
236176

237177
```JavaScript
238-
dispatcher.runComplete( [ throwUp = false ] );
178+
context.dispatchAll( [ throwUp = false ] );
239179
```
240180

241181
`throwUp` (*Optional*) Boolean
242182

243-
- This argument affects how the dispatcher handles uncaught errors. If set to `true` then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.
244-
245-
### `Dispatcher.prototype.stop`
246-
247-
Stops the dispatcher from cycling, then resets the runqueue's `blockIndex` and `activeBlock`. Can be used to stop `runAsync`.
183+
- 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.

package.json

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,36 @@
11
{
2-
"name": "hertzscript-dispatcher",
3-
"version": "0.0.19",
2+
"name": "hertzscript-vm",
3+
"version": "0.0.1",
44
"description": "Executes preemptible JavaScript coroutines which conform to the HertzScript specification.",
55
"keywords": [
6+
"virtual-machine",
7+
"hertzscript",
8+
"reflection",
69
"javascript",
710
"preemption",
811
"preemptive",
912
"preemptible",
10-
"protothreads",
11-
"green-threads",
1213
"coroutines",
1314
"coroutine",
14-
"coroutine-dispatcher",
1515
"concurrency",
16-
"automatic-concurrency",
17-
"concurrent",
1816
"concurrent-programming",
1917
"concurrent-javascript",
2018
"stackless-coroutines",
21-
"multitasking",
22-
"cooperative-multitasking",
23-
"preemptive-multitasking",
24-
"voluntary-preemtive-multitasking"
19+
"detour",
20+
"hook"
2521
],
26-
"homepage": "https://github.com/hertzscript/Dispatcher#readme",
22+
"homepage": "https://github.com/hertzscript/VirtualMachine#readme",
2723
"repository": {
2824
"type": "git",
29-
"url": "https://github.com/hertzscript/Dispatcher.git"
25+
"url": "https://github.com/hertzscript/VirtualMachine.git"
3026
},
3127
"bugs": {
32-
"url": "https://github.com/hertzscript/Dispatcher/issues"
33-
},
34-
"main": "src/Dispatcher.js",
35-
"bin": {
36-
"hzs": "./bin/executeCLI.js",
37-
"hzscript": "./bin/executeCLI.js"
28+
"url": "https://github.com/hertzscript/VirtualMachine/issues"
3829
},
30+
"main": "src/index.js",
3931
"author": "Dani Glore (https://github.com/Floofies)",
4032
"contributors": [
4133
"Thomas G. (https://github.com/trgwii)"
4234
],
43-
"license": "MIT",
44-
"dependencies": {
45-
"command-line-args": "^5.0.2",
46-
"hertzscript-compiler": ""
47-
}
35+
"license": "MIT"
4836
}

0 commit comments

Comments
 (0)