@lightvm/core-browser-wasm 0.1.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +203 -0
- package/README.md +561 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2025 SoTeen Studio
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://git.io/typing-svg">
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=700&size=40&pause=1000&color=FFFFFF¢er=true&vCenter=true&width=435&lines=LightVM">
|
|
5
|
+
<source media="(prefers-color-scheme: light)" srcset="https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=700&size=40&pause=1000&color=000000¢er=true&vCenter=true&width=435&lines=LightVM">
|
|
6
|
+
<img alt="LightVM" src="https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=700&size=40&pause=1000&color=000000¢er=true&vCenter=true&width=435&lines=LightVM">
|
|
7
|
+
</picture>
|
|
8
|
+
</a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<picture>
|
|
13
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://readme-typing-svg.herokuapp.com?font=Fira+Code&italic=true&weight=400&size=18&pause=1000&color=A9A9A9¢er=true&vCenter=true&width=435&lines=Minimalist+Execution.+Maximal+Security.">
|
|
14
|
+
<source media="(prefers-color-scheme: light)" srcset="https://readme-typing-svg.herokuapp.com?font=Fira+Code&italic=true&weight=400&size=18&pause=1000&color=4A4A4A¢er=true&vCenter=true&width=435&lines=Minimalist+Execution.+Maximal+Security.">
|
|
15
|
+
<img alt="Tagline" src="https://readme-typing-svg.herokuapp.com?font=Fira+Code&italic=true&weight=400&size=18&pause=1000&color=4A4A4A¢er=true&vCenter=true&width=435&lines=Minimalist+Execution.+Maximal+Security.">
|
|
16
|
+
</picture>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
<a href="https://github.com/soteenstudio/lightvm/actions">
|
|
21
|
+
<img src="https://img.shields.io/github/actions/workflow/status/soteenstudio/lightvm/ci.yml?branch=main&style=flat-square&color=black&label=build&logo=github" alt="build status">
|
|
22
|
+
</a>
|
|
23
|
+
<a href="https://www.npmjs.com/package/lightvm">
|
|
24
|
+
<img src="https://img.shields.io/npm/v/lightvm?style=flat-square&color=black&logo=npm&logoColor=white" alt="npm version">
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://www.npmjs.com/package/lightvm">
|
|
27
|
+
<img src="https://img.shields.io/npm/v/lightvm/next?style=flat-square&color=orange&label=nightly&logo=npm&logoColor=white" alt="nightly version">
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://www.npmjs.com/package/lightvm">
|
|
30
|
+
<img src="https://img.shields.io/npm/dt/lightvm?style=flat-square&color=black&logo=target&logoColor=white" alt="total downloads">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://github.com/soteenstudio/lightvm/stargazers">
|
|
33
|
+
<img src="https://img.shields.io/github/stars/soteenstudio/lightvm?style=flat-square&color=black&logo=github&logoColor=white" alt="github stars">
|
|
34
|
+
</a>
|
|
35
|
+
<a href="https://github.com/soteenstudio/lightvm">
|
|
36
|
+
<img src="https://img.shields.io/github/repo-size/soteenstudio/lightvm?style=flat-square&color=black&logo=database&logoColor=white" alt="repo size">
|
|
37
|
+
</a>
|
|
38
|
+
<a href="LICENSE">
|
|
39
|
+
<img src="https://img.shields.io/badge/license-Apache--2.0-black?style=flat-square&logo=apache&logoColor=white" alt="license">
|
|
40
|
+
</a>
|
|
41
|
+
</p>
|
|
42
|
+
|
|
43
|
+
A capability-based virtual machine designed for __secure__, __predictable__, and __optimized bytecode execution__.
|
|
44
|
+
## The Philosophy: Deterministic & Lean
|
|
45
|
+
LightVM is built with a focus on execution transparency and resource efficiency:
|
|
46
|
+
* __Zero Magic (Deterministic)__: Instruction execution is linear and completely predictable. The VM operates explicitly, executing instructions exactly as they are defined.
|
|
47
|
+
* __Resource Conscious__: Designed with a minimal memory footprint through the use of optimized data structures such as SmolStr and ahash for fast metadata management.
|
|
48
|
+
* __Explicit Security__: Security is managed through a strict Capability system. Every VM access and operation must have permissions explicitly defined by the host from the outset.
|
|
49
|
+
|
|
50
|
+
## AOT Optimization Process
|
|
51
|
+
LightVM isn't just a simple interpreter. Before execution, your bytecode undergoes a multi-pass optimization to ensure maximum performance and minimal footprint:
|
|
52
|
+
* __Constant Folding__: Pre-calculates math and logic operations (e.g., Add, Sub, Xor, Concat) if the values are known at compile-time.
|
|
53
|
+
* __Conversion & Metadata Folding__: Pre-evaluates type casting (e.g., ToInteger, ToString) and metadata checks like TypeOf to eliminate redundant runtime work.
|
|
54
|
+
* __Strength Reduction__: Replaces "heavy" operations with lighter ones, such as converting multiplication by powers of two into bitwise Shl (Shift Left).
|
|
55
|
+
* __Dead Store Elimination__: Analyzes variable usage and automatically removes Push, Set, or Inc operations that don't contribute to the final program state.
|
|
56
|
+
* __Dead Loop Elimination__: Identifies and prunes "pure" loops that have no side effects (no I/O, calls, or returns), preventing unnecessary CPU cycles.
|
|
57
|
+
* __Redundant Load Elimination__: Detects consecutive attempts to load identical values or variables onto the stack and replaces redundant operations with a high-performance Dup instruction to minimize memory access overhead.
|
|
58
|
+
* __Jump Optimization__: Detects and removes redundant Jump instructions that point to the very next line of code.
|
|
59
|
+
* __Jump Threading__: Optimizes control flow by collapsing chains of redirection, where a jump leads directly to another jump, ensuring the instruction pointer bypasses intermediate hops to reach the final destination immediately.
|
|
60
|
+
|
|
61
|
+
## Getting Started
|
|
62
|
+
### Installation
|
|
63
|
+
|
|
64
|
+
<details>
|
|
65
|
+
<summary>Installation with NPM</summary>
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm install lightvm
|
|
69
|
+
|
|
70
|
+
# or
|
|
71
|
+
npm install lightvm@next
|
|
72
|
+
```
|
|
73
|
+
</details>
|
|
74
|
+
|
|
75
|
+
<details>
|
|
76
|
+
<summary>Installation with Yarn</summary>
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
yarn add lightvm
|
|
80
|
+
|
|
81
|
+
# or
|
|
82
|
+
yarn add lightvm@next
|
|
83
|
+
```
|
|
84
|
+
</details>
|
|
85
|
+
|
|
86
|
+
<details>
|
|
87
|
+
<summary>Installation with Cargo</summary>
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
cargo add lightvm
|
|
91
|
+
|
|
92
|
+
# or specific version
|
|
93
|
+
cargo add lightvm@0.1.0-alpha.6
|
|
94
|
+
```
|
|
95
|
+
</details>
|
|
96
|
+
|
|
97
|
+
### Quick Usage
|
|
98
|
+
|
|
99
|
+
<details>
|
|
100
|
+
<summary>Using TypeScript:</summary>
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { LightVM, Capability } from 'lightvm';
|
|
104
|
+
|
|
105
|
+
const caps = [Capability.Control, Capability.Observe];
|
|
106
|
+
|
|
107
|
+
const vm = new LightVM(caps);
|
|
108
|
+
```
|
|
109
|
+
</details>
|
|
110
|
+
|
|
111
|
+
<details>
|
|
112
|
+
<summary>Using Rust:</summary>
|
|
113
|
+
|
|
114
|
+
```rust
|
|
115
|
+
use lightvm::LightVM;
|
|
116
|
+
use lightvm::types::capability::Capability;
|
|
117
|
+
|
|
118
|
+
fn main() {
|
|
119
|
+
let caps = vec![Capability::Control, Capability::Observe];
|
|
120
|
+
|
|
121
|
+
let mut vm = LightVM::new(caps);
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
</details>
|
|
125
|
+
|
|
126
|
+
### Virtual Machine Capabilities
|
|
127
|
+
LightVM uses a strict capability-based security model. You must explicitly grant permissions when instantiating the VM.
|
|
128
|
+
| Capability | Level | Description |
|
|
129
|
+
|------------|-------|-------------|
|
|
130
|
+
| Control | Low | Grants permission to start/stop execution and export functions. |
|
|
131
|
+
| Observe | Medium | Allows the host to inspect internal states, variable stacks, and metrics. |
|
|
132
|
+
| Debug | High | Opens access to verbose internal logs and hidden states for troubleshooting. |
|
|
133
|
+
| Unsafe | Critical | Removes safety guards, allowing manual halts and raw memory/process access. |
|
|
134
|
+
|
|
135
|
+
### How to use
|
|
136
|
+
1. ``run()`` __method__:
|
|
137
|
+
Function to start bytecode execution.
|
|
138
|
+
|
|
139
|
+
<details>
|
|
140
|
+
<summary>TypeScript:</summary>
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
const raw = [
|
|
144
|
+
["push", 5],
|
|
145
|
+
["val", "x"],
|
|
146
|
+
["set", "x"]
|
|
147
|
+
];
|
|
148
|
+
vm.load(vm.tools().optimizeBytecode(raw))
|
|
149
|
+
.run();
|
|
150
|
+
```
|
|
151
|
+
</details>
|
|
152
|
+
|
|
153
|
+
<details>
|
|
154
|
+
<summary>Rust:</summary>
|
|
155
|
+
|
|
156
|
+
```rust
|
|
157
|
+
let raw = r#"[
|
|
158
|
+
["push", 5],
|
|
159
|
+
["val", "x"],
|
|
160
|
+
["set", "x"]
|
|
161
|
+
]"#;
|
|
162
|
+
vm.load(LightVM::tools().optimize_bytecode(raw).clone())
|
|
163
|
+
.run(None);
|
|
164
|
+
```
|
|
165
|
+
</details>
|
|
166
|
+
|
|
167
|
+
> [!NOTE]
|
|
168
|
+
> __Capability Required__: control
|
|
169
|
+
> __Info__: parameters of ``load()`` can change bytecode directly or file path to .ltc
|
|
170
|
+
|
|
171
|
+
2. ``provide()`` method:
|
|
172
|
+
Function to inject data/variables into the VM.
|
|
173
|
+
|
|
174
|
+
<details>
|
|
175
|
+
<summary>TypeScript:</summary>
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
vm.provide({
|
|
179
|
+
name: "John Doe",
|
|
180
|
+
force: 2021,
|
|
181
|
+
});
|
|
182
|
+
let raw = [
|
|
183
|
+
["get", "name"],
|
|
184
|
+
["println"],
|
|
185
|
+
["get", "force"],
|
|
186
|
+
["println"]
|
|
187
|
+
];
|
|
188
|
+
```
|
|
189
|
+
</details>
|
|
190
|
+
|
|
191
|
+
<details>
|
|
192
|
+
<summary>Rust:</summary>
|
|
193
|
+
|
|
194
|
+
```rust
|
|
195
|
+
vm.provide(serde_json::json!({
|
|
196
|
+
"name": "John Doe",
|
|
197
|
+
"force": 2021
|
|
198
|
+
}));
|
|
199
|
+
let raw = r#"[
|
|
200
|
+
["get", "name"],
|
|
201
|
+
["println"],
|
|
202
|
+
["get", "force"],
|
|
203
|
+
["println"]
|
|
204
|
+
]"#;
|
|
205
|
+
```
|
|
206
|
+
</details>
|
|
207
|
+
|
|
208
|
+
> [!NOTE]
|
|
209
|
+
> __Capability Required__: no spesific capability
|
|
210
|
+
|
|
211
|
+
3. ``inspect()`` method:
|
|
212
|
+
Function to view state, number of instructions, and capability.
|
|
213
|
+
|
|
214
|
+
<details>
|
|
215
|
+
<summary>TypeScript:</summary>
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
const report = vm.inspect();
|
|
219
|
+
console.log(report);
|
|
220
|
+
```
|
|
221
|
+
</details>
|
|
222
|
+
|
|
223
|
+
<details>
|
|
224
|
+
<summary>Rust:</summary>
|
|
225
|
+
|
|
226
|
+
```rust
|
|
227
|
+
let report = vm.inspect();
|
|
228
|
+
println!("{}", serde_json::to_string_pretty(&report).unwrap());
|
|
229
|
+
```
|
|
230
|
+
</details>
|
|
231
|
+
|
|
232
|
+
> [!NOTE]
|
|
233
|
+
> __Capability Required__: observe
|
|
234
|
+
|
|
235
|
+
4. ``halt()`` method:
|
|
236
|
+
Function to force/manually stop VM.
|
|
237
|
+
|
|
238
|
+
<details>
|
|
239
|
+
<summary>TypeScript:</summary>
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
vm.halt();
|
|
243
|
+
vm.run(); // will not be executed
|
|
244
|
+
console.log("The VM has been terminated.")
|
|
245
|
+
```
|
|
246
|
+
</details>
|
|
247
|
+
|
|
248
|
+
<details>
|
|
249
|
+
<summary>Rust:</summary>
|
|
250
|
+
|
|
251
|
+
```rust
|
|
252
|
+
vm.halt();
|
|
253
|
+
vm.run(None); // will not be executed
|
|
254
|
+
println!("The VM has been terminated.");
|
|
255
|
+
```
|
|
256
|
+
</details>
|
|
257
|
+
|
|
258
|
+
> [!NOTE]
|
|
259
|
+
> __Capability Required__: unsafe
|
|
260
|
+
|
|
261
|
+
5. ``on()`` method:
|
|
262
|
+
Function to register listener to VM.
|
|
263
|
+
|
|
264
|
+
<details>
|
|
265
|
+
<summary>TypeScript:</summary>
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
vm.on("halt", (payload) => {
|
|
269
|
+
console.log("Halted: ", payload);
|
|
270
|
+
});
|
|
271
|
+
```
|
|
272
|
+
</details>
|
|
273
|
+
|
|
274
|
+
<details>
|
|
275
|
+
<summary>Rust:</summary>
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
vm.on("halt", |payload| {
|
|
279
|
+
println!("Halted: ", payload);
|
|
280
|
+
});
|
|
281
|
+
```
|
|
282
|
+
</details>
|
|
283
|
+
|
|
284
|
+
> [!NOTE]
|
|
285
|
+
> __Capability Required__: no spesific capability
|
|
286
|
+
> __Event__: tick, halt, and panic
|
|
287
|
+
|
|
288
|
+
6. ``export()`` method:
|
|
289
|
+
Function to export functions in the VM out.
|
|
290
|
+
|
|
291
|
+
<details>
|
|
292
|
+
<summary>TypeScript:</summary>
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
const add = vm.export("add");
|
|
296
|
+
console.log(add(5, 6));
|
|
297
|
+
```
|
|
298
|
+
</details>
|
|
299
|
+
|
|
300
|
+
<details>
|
|
301
|
+
<summary>Rust:</summary>
|
|
302
|
+
|
|
303
|
+
```rust
|
|
304
|
+
let mut add = vm.export("add".to_string());
|
|
305
|
+
let args = vec![serde_json::json!(5), serde_json::json!(6)];
|
|
306
|
+
if let Some(hasil) = add_func(args) {
|
|
307
|
+
println!("Hasil dari VM: {}", hasil);
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
</details>
|
|
311
|
+
|
|
312
|
+
> [!NOTE]
|
|
313
|
+
> __Capability Required__: control
|
|
314
|
+
|
|
315
|
+
7. ``tools()`` method:
|
|
316
|
+
Functions used to call utilities
|
|
317
|
+
|
|
318
|
+
<ol type="a">
|
|
319
|
+
<li>Optimizes raw JSON bytecode and serializes it to a string</li>
|
|
320
|
+
<details>
|
|
321
|
+
<summary>TypeScript:</summary>
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
const tools = vm.tools();
|
|
325
|
+
const optimized = tools.optimizeBytecode(raw);
|
|
326
|
+
console.log(optimized);
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
</details>
|
|
330
|
+
<details>
|
|
331
|
+
<summary>Rust:</summary>
|
|
332
|
+
|
|
333
|
+
```rust
|
|
334
|
+
let tools = LightVM::tools();
|
|
335
|
+
let optimized = tools.optimize_bytecode(raw);
|
|
336
|
+
println!(optimized.clone());
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
</details>
|
|
340
|
+
|
|
341
|
+
<li>Converts raw JSON bytecode into a readable LTC assembly string</li>
|
|
342
|
+
<details>
|
|
343
|
+
<summary>TypeScript:</summary>
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
const tools = vm.tools().tools;
|
|
347
|
+
const stringify = tools.stringifyLTC(raw);
|
|
348
|
+
console.log(stringify);
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
</details>
|
|
352
|
+
<details>
|
|
353
|
+
<summary>Rust:</summary>
|
|
354
|
+
|
|
355
|
+
```rust
|
|
356
|
+
let tools = LightVM::tools();
|
|
357
|
+
let stringify = tools.stringify_ltc(raw);
|
|
358
|
+
println!("{:#}", stringify.clone());
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
</details>
|
|
362
|
+
|
|
363
|
+
<li>Parses LTC code and serializes the instructions to a JSON string</li>
|
|
364
|
+
<details>
|
|
365
|
+
<summary>TypeScript:</summary>
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
const tools = vm.tools();
|
|
369
|
+
const parsed = tools.parseLTC(raw);
|
|
370
|
+
console.log(parsed.clone());
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
</details>
|
|
374
|
+
<details>
|
|
375
|
+
<summary>Rust:</summary>
|
|
376
|
+
|
|
377
|
+
```rust
|
|
378
|
+
let tools = LightVM::tools();
|
|
379
|
+
let parsed = tools.parse_ltc(raw);
|
|
380
|
+
println!("{:#}", parsed.clone());
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
</details>
|
|
384
|
+
|
|
385
|
+
<li>Parses an LTC string into a JSON array</li>
|
|
386
|
+
<details>
|
|
387
|
+
<summary>TypeScript:</summary>
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
const tools = vm.tools();
|
|
391
|
+
const json = tools.parseLTCArray(raw);
|
|
392
|
+
console.log(json.clone());
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
</details>
|
|
396
|
+
<details>
|
|
397
|
+
<summary>Rust:</summary>
|
|
398
|
+
|
|
399
|
+
```rust
|
|
400
|
+
let tools = LightVM::tools();
|
|
401
|
+
let json = tools.parse_ltc_array(raw);
|
|
402
|
+
println!("{:#}", json.clone());
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
</details>
|
|
406
|
+
|
|
407
|
+
</ol>
|
|
408
|
+
|
|
409
|
+
> [!NOTE]
|
|
410
|
+
> __Capability Required__: no spesific capability
|
|
411
|
+
|
|
412
|
+
## References
|
|
413
|
+
### Supported Primitive Types
|
|
414
|
+
LightVM requires explicit type definitions for certain instructions to maintain deterministic execution and peak performance.
|
|
415
|
+
| Type | Aliases | Reference | Target Value Type |
|
|
416
|
+
|------|---------|-----------|-------------------|
|
|
417
|
+
| sht | i16 | Short | 16-bit Integer (Int16) |
|
|
418
|
+
| int | i32 | Integer | 32-bit Integer (Int32) |
|
|
419
|
+
| lng | i64 | Long | 64-bit Integer (Int64) |
|
|
420
|
+
| oct | i128 | Octa | 128-bit Integer (Int128) |
|
|
421
|
+
| hlf | f16 | Half | 16-bit Floating Point (Float16) |
|
|
422
|
+
| flt | f32 | Float | 32-bit Floating Point (Float32) |
|
|
423
|
+
| dbl | f64 | Double | 64-bit Floating Point (Float64) |
|
|
424
|
+
| str | - | String | String / Text data |
|
|
425
|
+
|
|
426
|
+
### Bytecode Instructions
|
|
427
|
+
LightVM has a total of 40+ instructions for bytecode.
|
|
428
|
+
1. Stack & Variable Management
|
|
429
|
+
A group of instructions for basic data manipulation and memory (variable) allocation.
|
|
430
|
+
|
|
431
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
432
|
+
|--------|-----------|------------------|-------------|
|
|
433
|
+
| push | value | - |Inserting data into the stack |
|
|
434
|
+
| val | name | - | Declaring a new variable |
|
|
435
|
+
| set | name | val | Take the top stack and then save it to the variable ``name`` |
|
|
436
|
+
| get | name | - | Take the contents of the ``name`` variable and push it onto the stack |
|
|
437
|
+
| dup | - | val | Duplicate the top value in the stack |
|
|
438
|
+
| swap | - | val1, val2 | Swap the top stack with the bottom stack |
|
|
439
|
+
2. Arithmetic & Logic
|
|
440
|
+
Instructions for calculations. Note that for optimization, these instructions require a ``PrimitiveTypes`` (``sht``, ``hlf``, ``int``, ``flt``, ``lng``, ``dbl``, ``oct``) to prevent the VM from guessing the data type during execution.
|
|
441
|
+
|
|
442
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
443
|
+
|-----------|------------|------------------|-------------|
|
|
444
|
+
| add / sub | type | val1, val2 | Addition or Subtraction |
|
|
445
|
+
| mul / div | type | val1, val2 | Multiplication or Division |
|
|
446
|
+
| mod | type | val1, val2 | Modulo (Remainder) |
|
|
447
|
+
| neg | type | val | Negation (-5 to 5, or vice versa) |
|
|
448
|
+
| inc / dec | name, type | - | Directly add/remove variable contents (without going through the stack) |
|
|
449
|
+
| gt / lt | type | val1, val2 |Greater Than or Less Than |
|
|
450
|
+
| ge / le | type | val1, val2 | Greater/Less Than or Equal |
|
|
451
|
+
| eq / neq | type | val1, val2 | Equal or Not Equal |
|
|
452
|
+
| shl / shr | type | val1, val2 | Shift Left or Shift Right bitwise operation based on data type |
|
|
453
|
+
| rol / ror | type | val1, val2 | __Circular__ Shift Left or Right (Rotate) bitwise operation based on data type |
|
|
454
|
+
| and / or | - | val1, val2 | Boolean logic operations (``&&`` / ``\|\|``) |
|
|
455
|
+
| xor | - | val1, val2 | Bitwise __Exclusive OR__ operation between two values |
|
|
456
|
+
| not | - | val1, val2 | Bitwise __NOT__ (Inversion) operation on a single value |
|
|
457
|
+
| pow | type | val1, val2 | General power operation (x^y) |
|
|
458
|
+
| powi | type | val1, val2 | Power with integer exponent |
|
|
459
|
+
| powf | type | val1, val2 | Power with floating point exponent |
|
|
460
|
+
| sin / cos | type | val1, val2 | Sine or Cosine trigonometric operation |
|
|
461
|
+
| tan | type | val1, val2 | Tangent trigonometric operation |
|
|
462
|
+
|
|
463
|
+
3. Control Flow & Function
|
|
464
|
+
Instructions for managing program flow, looping, and function calls.
|
|
465
|
+
|
|
466
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
467
|
+
|----------|-----------|------------------|-------------|
|
|
468
|
+
| jump | target_ip | - | Jump to a specific instruction line (Instruction Pointer) |
|
|
469
|
+
| if_false | target_ip | cond | Jump if the value on the stack is false |
|
|
470
|
+
| func | name, argc, start, end, [params] | - | Function block definition (scope) |
|
|
471
|
+
| call | name, argc | - | Call a function with a specified number of arguments |
|
|
472
|
+
| return | - | val | Exit the function and return to the caller |
|
|
473
|
+
| stop | - | - | Kill all VM processes (Halt) |
|
|
474
|
+
4. Data Structures & Metadata
|
|
475
|
+
Create complex data handles like JS Objects or Arrays, plus data type matters.
|
|
476
|
+
|
|
477
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
478
|
+
|------------|-----------|------------------|-------------|
|
|
479
|
+
| make_obj | count | k1, v1, ... kn, vn | Create Object from n key-value pairs in stack |
|
|
480
|
+
| make_array | count | v1, v2, ... vn | Create an Array of n elements in a stack |
|
|
481
|
+
| access | prop_name | target_obj | Access Object's properties |
|
|
482
|
+
| access_index | - | target_arr, index | Access Array elements by index on the stack |
|
|
483
|
+
| length | - | val | Check the length of a string or the number of items in an array/object |
|
|
484
|
+
| typeof | - | val | Get the data type from the top value of the stack |
|
|
485
|
+
| concat | - | val1, val2 | Combine two values (usually strings) |
|
|
486
|
+
|
|
487
|
+
5. Type Casting (Conversion)
|
|
488
|
+
For those of you who want to force a certain data type to ensure consistent performance.
|
|
489
|
+
|
|
490
|
+
| Opcode | Operands (stack) | Description |
|
|
491
|
+
|------------|------------------|-----------------------------|
|
|
492
|
+
| to_short | val | Change value to Short (16-bit) |
|
|
493
|
+
| to_integer | val | Change value to Integer (32-bit) |
|
|
494
|
+
| to_long | val | Change the value to Long (64-bit) |
|
|
495
|
+
| to_octa | val | Change value to Octa (128-bit Integer) |
|
|
496
|
+
| to_half | val | Change value to Half-precision (16-bit Float) |
|
|
497
|
+
| to_float | val | Change value to Float (32-bit) |
|
|
498
|
+
| to_double | val | Change the value to Double (64-bit) |
|
|
499
|
+
| to_string | val | Change the value to String |
|
|
500
|
+
6. Objects & OOP
|
|
501
|
+
Instructions for handling class instances and modifying object properties dynamically.
|
|
502
|
+
|
|
503
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
504
|
+
|--------|-----------|------------------|-------------|
|
|
505
|
+
| set_prop | prop_name | val, obj | Set the value of an object property (retrieve ``value`` and ``target_obj`` from the stack) |
|
|
506
|
+
| instantiate | class_name, argc | arg1, ... argN | Creates a new instance of a class with a specified number of constructor arguments |
|
|
507
|
+
| inspect_obj | - | obj | Prints the internal structure of an Object to the console |
|
|
508
|
+
| inspect_array | - | arr | Print the internal contents of an Array to the console |
|
|
509
|
+
> [!WARNING]
|
|
510
|
+
> __Nightly Opcode__: The `instantiate` instruction is still experimental. The API may change without notice in the `@next` version.
|
|
511
|
+
|
|
512
|
+
7. Module & Export System
|
|
513
|
+
Instructions for communication between modules or with external runtimes.
|
|
514
|
+
|
|
515
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
516
|
+
|--------|-----------|------------------|-------------|
|
|
517
|
+
| import | module_name, alias_idx | target, length | Importing external libraries/modules into a specific variable index |
|
|
518
|
+
| export | name | - | Mark a function or variable to be accessible from outside the VM |
|
|
519
|
+
> [!WARNING]
|
|
520
|
+
> __Nightly Opcode__: The `export` and `import` instructions are still experimental. The API may change without notice in the `@next` version.
|
|
521
|
+
|
|
522
|
+
8. Basic I/O & Loop Control
|
|
523
|
+
Instructions for standard output and more specific iteration control.
|
|
524
|
+
|
|
525
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
526
|
+
|--------|-----------|------------------|-------------|
|
|
527
|
+
| print | - | val | Prints the top value of the stack to the console without a newline |
|
|
528
|
+
| println | - | val | Prints the top value of the stack to the console with a newline |
|
|
529
|
+
| stdin | - | val | Reads a line from standard input, trims the trailing newline characters, and pushes the resulting string onto the stack |
|
|
530
|
+
| stdout | - | val | Pops the top value from the stack (must be a String) and prints it to the console without a newline |
|
|
531
|
+
| stdoutln | - | val | Pops the top value from the stack (must be a String) and prints it to the console followed by a newline |
|
|
532
|
+
| clear_screen | - | - | Clears the terminal screen and resets the cursor position to the top-left corner using ANSI escape codes (\x1B[2J\x1B[1H) |
|
|
533
|
+
| break | - | target_ip | Stops the loop and jumps to the specified target_ip |
|
|
534
|
+
| nop | - | - | Empty instructions (usually for placeholders or alignment) |
|
|
535
|
+
|
|
536
|
+
9. Advanced Stack & Memory Management
|
|
537
|
+
Special instructions for low-level control of the VM memory footprint in real-time.
|
|
538
|
+
|
|
539
|
+
| Opcode | Arguments | Operands (stack) | Description |
|
|
540
|
+
|--------|-----------|------------------|-------------|
|
|
541
|
+
| init_stack | size | - | Initialize the evaluation stack memory capacity at the beginning of VM execution to prevent reallocation |
|
|
542
|
+
| shrink | - | target, length | Reduces the capacity of the stack to fit its current length |
|
|
543
|
+
| truncate | - | target_size | Clear/reset the stack elements efficiently |
|
|
544
|
+
|
|
545
|
+
## Supported Architectures
|
|
546
|
+
LightVM supports a wide range of platforms and architectures to ensure maximum operational flexibility. Here's the current compatibility list:
|
|
547
|
+
| OS / Runtime | Architecture | Toolchain | Rust | Node.js | Web |
|
|
548
|
+
|--------------|--------------|-----------|-------|---------|-----|
|
|
549
|
+
| Windows | x64, ia32 | MSVC | ✓ | ✓ | - |
|
|
550
|
+
| Linux | x64, ia32, arm64 | GNU (glibc) | ✓ | ✓ | - |
|
|
551
|
+
| Linux (musl) | x64, ia32, arm64 | musl | ✓ | ✓ | - |
|
|
552
|
+
| macOS (Darwin) | x64 | Apple Clang | ✓ | ✓ | - |
|
|
553
|
+
| Android | arm64, arm | NDK | ✓ | ✓ | - |
|
|
554
|
+
| FreeBSD | x64 | Clang | ✓ | ✓ | - |
|
|
555
|
+
| Web / Browser | wasm32 | wasm-pack / wasm-bindgen | - | - | ✓ |
|
|
556
|
+
> [!WARNING]
|
|
557
|
+
> __Nightly Support__: Support for `wasm32` for browsers is still in experimental stage.
|
|
558
|
+
|
|
559
|
+
## 📜 License & Changelog
|
|
560
|
+
- This project is distributed using the [Apache-2.0 license](LICENSE).
|
|
561
|
+
- See [changelogs](./docs/CHANGELOG.md) for the latest updates and release history.
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lightvm/core-browser-wasm",
|
|
3
|
+
"version": "0.1.0-alpha.7",
|
|
4
|
+
"os": [
|
|
5
|
+
"browser"
|
|
6
|
+
],
|
|
7
|
+
"cpu": [
|
|
8
|
+
"wasm"
|
|
9
|
+
],
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"*.wasm",
|
|
13
|
+
"*.js",
|
|
14
|
+
"*.d.ts",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0"
|
|
22
|
+
}
|