porffor 0.2.0-3272f21 → 0.2.0-3aaa9b4

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.
@@ -0,0 +1,183 @@
1
+ # Contributing to Porffor
2
+
3
+ Hello! Thanks for your potential interest in contributing to Porffor :)
4
+
5
+ This document hopes to help you understand Porffor-specific TS, specifically for writing built-ins (inside `compiler/builtins/` eg `btoa`, `String.prototype.trim`, ...). This guide isn't really meant for modifying the compiler itself yet (eg `compiler/codegen.js`), as built-ins are ~easier to implement and more useful at the moment.
6
+
7
+ I mostly presume decent JS knowledge, with some basic TS too but nothing complicated. Knowing low-level stuff generally (pointers, etc) and/or Wasm (bytecode) is also a plus but hopefully not required.
8
+
9
+ <br>
10
+
11
+ ## Types
12
+
13
+ Porffor has usual JS types (or at least the ones it supports), but also internal types for various reasons.
14
+
15
+ ### ByteString
16
+
17
+ The most important and widely used internal type is ByteString (also called `bytestring` or `_bytestring` in code). Regular strings in Porffor are UTF-16 encoded, so each character uses 2 bytes. ByteStrings are special strings which are used when the characters in a string only use ASCII/LATIN-1 characters, so the lower byte of the UTF-16 characters are unused. Instead of wasting memory with all the unused memory, ByteStrings instead use 1 byte per character. This halves memory usage of such strings and also makes operating on them faster. The downside is that many Porffor built-ins have to be written twice, slightly different, for both `String` and `ByteString` types.
18
+
19
+ ### i32
20
+
21
+ This is complicated internally but essentially, only use it for pointers. (This is not signed or unsigned, instead it is the Wasm valtype `i32` so the signage is ~instruction dependant).
22
+
23
+ <br>
24
+
25
+ ## Pointers
26
+
27
+ Pointers are the main (and most difficult) unique feature you ~need to understand when dealing with objects (arrays, strings, ...).
28
+
29
+ We'll explain things per common usage you will likely need to know:
30
+
31
+ ## Commonly used Wasm code
32
+
33
+ ### Get a pointer
34
+
35
+ ```js
36
+ Porffor.wasm`local.get ${foobar}`
37
+ ```
38
+
39
+ Gets the pointer to the variable `foobar`. You don't really need to worry about how it works in detail, but essentially it gets the pointer as a number (type) instead of as the object it is.
40
+
41
+ ### Store a character in a ByteString
42
+
43
+ ```js
44
+ Porffor.wasm.i32.store8(pointer, characterCode, 0, 4)
45
+ ```
46
+
47
+ Stores the character code `characterCode` at the pointer `pointer` **for a ByteString**.[^1]
48
+
49
+ ### Store a character in a String
50
+
51
+ ```js
52
+ Porffor.wasm.i32.store16(pointer, characterCode, 0, 4)
53
+ ```
54
+
55
+ Stores the character code `characterCode` at the pointer `pointer` **for a String**.[^1]
56
+
57
+ ### Load a character from a ByteString
58
+
59
+ ```js
60
+ Porffor.wasm.i32.load8_u(pointer, 0, 4)
61
+ ```
62
+
63
+ Loads the character code at the pointer `pointer` **for a ByteString**.[^1]
64
+
65
+ ### Load a character from a String
66
+
67
+ ```js
68
+ Porffor.wasm.i32.load16_u(pointer, 0, 4)
69
+ ```
70
+
71
+ Loads the character code at the pointer `pointer` **for a String**.[^1]
72
+
73
+ ### Manually store the length of an object
74
+
75
+ ```js
76
+ Porffor.wasm.i32.store(pointer, length, 0, 0)
77
+ ```
78
+
79
+ Stores the length `length` at pointer `pointer`, setting the length of an object. This is mostly unneeded today as you can just do `obj.length = length`. [^2]
80
+
81
+ <br>
82
+
83
+ ## Example
84
+
85
+ Here is the code for `ByteString.prototype.toUpperCase()`:
86
+
87
+ ```ts
88
+ export const ___bytestring_prototype_toUpperCase = (_this: bytestring) => {
89
+ const len: i32 = _this.length;
90
+
91
+ let out: bytestring = '';
92
+ Porffor.wasm.i32.store(out, len, 0, 0);
93
+
94
+ let i: i32 = Porffor.wasm`local.get ${_this}`,
95
+ j: i32 = Porffor.wasm`local.get ${out}`;
96
+
97
+ const endPtr: i32 = i + len;
98
+ while (i < endPtr) {
99
+ let chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
100
+
101
+ if (chr >= 97) if (chr <= 122) chr -= 32;
102
+
103
+ Porffor.wasm.i32.store8(j++, chr, 0, 4);
104
+ }
105
+
106
+ return out;
107
+ };
108
+ ```
109
+
110
+ Now let's go through it section by section:
111
+
112
+ ```ts
113
+ export const ___bytestring_prototype_toUpperCase = (_this: bytestring) => {
114
+ ```
115
+
116
+ Here we define a built-in for Porffor. Notably:
117
+ - We do not use `a.b.c`, instead we use `__a_b_c`
118
+ - The ByteString type is actually `_bytestring`, as internal types have an extra `_` at the beginning (this is due to be fixed/simplified soon(tm))
119
+ - We use a `_this` argument, as `this` does not exist in Porffor yet
120
+ - We use an arrow function
121
+
122
+ ---
123
+
124
+ ```ts
125
+ const len: i32 = _this.length;
126
+
127
+ let out: bytestring = '';
128
+ Porffor.wasm.i32.store(out, len, 0, 0);
129
+ ```
130
+
131
+ This sets up the `out` variable we are going to write to for the output of this function. We set the length in advance to be the same as `_this`, as `foo.length == foo.toLowerCase().length`, because we will later be manually writing to it using Wasm instrinsics, which will not update the length themselves.
132
+
133
+ ---
134
+
135
+ ```ts
136
+ let i: i32 = Porffor.wasm`local.get ${_this}`,
137
+ j: i32 = Porffor.wasm`local.get ${out}`;
138
+ ```
139
+
140
+ Get the pointers for `_this` and `out` as `i32`s (~`number`s).
141
+
142
+ ---
143
+
144
+ ```ts
145
+ const endPtr: i32 = i + len;
146
+ while (i < endPtr) {
147
+ ```
148
+
149
+ Set up an end target pointer as the pointer variable for `_this` plus the length of it. Loop below until that pointer reaches the end target, so we iterate through the entire string.
150
+
151
+ ---
152
+
153
+ ```ts
154
+ let chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
155
+ ```
156
+
157
+ Read the character (code) from the current `_this` pointer variable, and increment it so next iteration it reads the next character, etc.
158
+
159
+ ---
160
+
161
+ ```ts
162
+ if (chr >= 97) if (chr <= 122) chr -= 32;
163
+ ```
164
+
165
+ If the character code is >= 97 (`a`) and <= 122 (`z`), decrease it by 32, making it an upper case character. eg: 97 (`a`) - 32 = 65 (`A`).
166
+
167
+ ---
168
+
169
+ ```ts
170
+ Porffor.wasm.i32.store8(j++, chr, 0, 4);
171
+ ```
172
+
173
+ Store the character code into the `out` pointer variable, and increment it.
174
+
175
+ <br>
176
+
177
+ ## Formatting/linting
178
+
179
+ There is 0 setup for this (right now). You can try looking through the other built-ins files but do not worry about it a lot, I honestly do not mind going through and cleaning up after a PR as long as the code itself is good :^)
180
+
181
+ [^1]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `4` byte offset for length).
182
+
183
+ [^2]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `0` byte offset).
package/README.md CHANGED
@@ -14,60 +14,75 @@ Porffor is primarily built from scratch, the only thing that is not is the parse
14
14
  Expect nothing to work! Only very limited JS is currently supported. See files in `bench` for examples.
15
15
 
16
16
  ### Setup
17
- 1. Clone this repo (`git clone https://github.com/CanadaHonk/porffor.git`)
18
- 2. `npm install` - for parser(s)
17
+ **`npm install -g porffor`**. It's that easy (hopefully) :)
19
18
 
20
- ### Running a file
21
- The repos comes with easy alias files for Unix and Windows, which you can use like so:
22
- - Unix: `./porf path/to/script.js`
23
- - Windows: `.\porf path/to/script.js`
19
+ ### Trying a REPL
20
+ **`porf`**. Just run it with no script file argument.
24
21
 
25
- Please note that further examples below will just use `./porf`, you need to use `.\porf` on Windows. You can also swap out `node` in the alias to use another runtime like Deno (`deno run -A`) or Bun (`bun ...`), or just use it yourself (eg `node runner/index.js ...`, `bun runner/index.js ...`). Node and Bun should work great, Deno support is WIP.
22
+ ### Running a JS file
23
+ **`porf path/to/script.js`**
26
24
 
27
- ### Trying a REPL
28
- **`./porf`**. Just run it with no script file argument.
25
+ ### Compiling to Wasm
26
+ **`porf wasm path/to/script.js out.wasm`**. Currently it does not use an import standard like WASI, so it is mostly unusable on its own.
29
27
 
30
28
  ### Compiling to native binaries
31
29
  > [!WARNING]
32
30
  > Compiling to native binaries uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
33
31
 
34
- **`./porf native path/to/script.js out(.exe)`**. You can specify the compiler with `-compiler=clang/zig/gcc`, and which opt level to use with `-cO=O3` (`Ofast` by default). Output binaries are also stripped by default.
32
+ **`porf native path/to/script.js out(.exe)`**. You can specify the compiler with `--compiler=clang/zig/gcc`, and which opt level to use with `--cO=O3` (`Ofast` by default). Output binaries are also stripped by default.
35
33
 
36
34
  ### Compiling to C
37
35
  > [!WARNING]
38
36
  > Compiling to C uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
39
37
 
40
- **`./porf c path/to/script.js (out.c)`**. When not including an output file, it will be printed to stdout instead.
38
+ **`porf c path/to/script.js (out.c)`**. When not including an output file, it will be printed to stdout instead.
39
+
40
+ ### Profiling a JS file
41
+ > [!WARNING]
42
+ > Very experimental WIP feature!
43
+
44
+ **`porf profile path/to/script.js`**
45
+
46
+ ### Debugging a JS file
47
+ > [!WARNING]
48
+ > Very experimental WIP feature!
49
+
50
+ **`porf debug path/to/script.js`**
51
+
52
+ ### Profiling the generated Wasm of a JS file
53
+ > [!WARNING]
54
+ > Very experimental WIP feature!
55
+
56
+ **`porf debug-wasm path/to/script.js`**
41
57
 
42
- ### Compiling to a Wasm binary
43
- **`./porf compile path/to/script.js out.wasm`**. Currently it does not use an import standard like WASI, so it is mostly unusable.
44
58
 
45
59
  ### Options
46
- - `-target=wasm|c|native` (default: `wasm`) to set target output (native compiles c output to binary, see args below)
47
- - `-target=c|native` only:
48
- - `-o=out.c|out.exe|out` to set file to output c or binary
49
- - `-target=native` only:
50
- - `-compiler=clang` to set compiler binary (path/name) to use to compile
51
- - `-cO=O3` to set compiler opt argument
52
- - `-parser=acorn|@babel/parser|meriyah|hermes-parser` (default: `acorn`) to set which parser to use
53
- - `-parse-types` to enable parsing type annotations/typescript. if `-parser` is unset, changes default to `@babel/parser`. does not type check
54
- - `-opt-types` to perform optimizations using type annotations as compiler hints. does not type check
55
- - `-valtype=i32|i64|f64` (default: `f64`) to set valtype
60
+ - `--parser=acorn|@babel/parser|meriyah|hermes-parser` (default: `acorn`) to set which parser to use
61
+ - `--parse-types` to enable parsing type annotations/typescript. if `-parser` is unset, changes default to `@babel/parser`. does not type check
62
+ - `--opt-types` to perform optimizations using type annotations as compiler hints. does not type check
63
+ - `--valtype=i32|i64|f64` (default: `f64`) to set valtype
56
64
  - `-O0` to disable opt
57
65
  - `-O1` (default) to enable basic opt (simplify insts, treeshake wasm imports)
58
66
  - `-O2` to enable advanced opt (inlining). unstable
59
67
  - `-O3` to enable advanceder opt (precompute const math). unstable
60
- - `-no-run` to not run wasm output, just compile
61
- - `-opt-log` to log some opts
62
- - `-code-log` to log some codegen (you probably want `-funcs`)
63
- - `-regex-log` to log some regex
64
- - `-funcs` to log funcs
65
- - `-ast-log` to log AST
66
- - `-opt-funcs` to log funcs after opt
67
- - `-sections` to log sections as hex
68
- - `-opt-no-inline` to not inline any funcs
69
- - `-tail-call` to enable tail calls (experimental + not widely implemented)
70
- - `-compile-hints` to enable V8 compilation hints (experimental + doesn't seem to do much?)
68
+ - `--no-run` to not run wasm output, just compile
69
+ - `--opt-log` to log some opts
70
+ - `--code-log` to log some codegen (you probably want `-funcs`)
71
+ - `--regex-log` to log some regex
72
+ - `--funcs` to log funcs
73
+ - `--ast-log` to log AST
74
+ - `--opt-funcs` to log funcs after opt
75
+ - `--sections` to log sections as hex
76
+ - `--opt-no-inline` to not inline any funcs
77
+ - `--tail-call` to enable tail calls (experimental + not widely implemented)
78
+ - `--compile-hints` to enable V8 compilation hints (experimental + doesn't seem to do much?)
79
+
80
+ ### Running in the repo
81
+ The repo comes with easy alias files for Unix and Windows, which you can use like so:
82
+ - Unix: `./porf path/to/script.js`
83
+ - Windows: `.\porf path/to/script.js`
84
+
85
+ Please note that further examples below will just use `./porf`, you need to use `.\porf` on Windows. You can also swap out `node` in the alias to use another runtime like Deno (`deno run -A`) or Bun (`bun ...`), or just use it yourself (eg `node runner/index.js ...`, `bun runner/index.js ...`). Node and Bun should work great, Deno support is WIP.
71
86
 
72
87
  ## Limitations
73
88
  - No full object support yet
@@ -190,7 +205,7 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
190
205
  ### Traditional opts
191
206
  - Inlining functions (WIP, limited)
192
207
  - Inline const math ops
193
- - Tail calls (behind flag `-tail-call`)
208
+ - Tail calls (behind flag `--tail-call`)
194
209
 
195
210
  ### Wasm transforms
196
211
  - `local.set`, `local.get` -> `local.tee`
package/asur/index.js CHANGED
@@ -1244,7 +1244,7 @@ paused = _paused;`);
1244
1244
  });
1245
1245
 
1246
1246
  export const instantiate = async (binary, importImpls) => {
1247
- const _vm = process?.argv?.includes('-wasm-debug') ? await wasmDebugVm() : vm;
1247
+ const _vm = process?.argv?.includes('--wasm-debug') ? await wasmDebugVm() : vm;
1248
1248
 
1249
1249
  const parsed = parse(binary);
1250
1250
  const exports = {};
@@ -154,7 +154,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
154
154
 
155
155
  const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === 'main' ? 'm' : x.name), ExportDesc.func, x.index ]);
156
156
 
157
- if (Prefs.alwaysMemory && pages.size === 0) pages.set('-always-memory', 0);
157
+ if (Prefs.alwaysMemory && pages.size === 0) pages.set('--always-memory', 0);
158
158
  if (optLevel === 0) pages.set('O0 precaution', 0);
159
159
 
160
160
  const usesMemory = pages.size > 0;
@@ -1,5 +1,5 @@
1
1
  export default () => {
2
- let out = `// @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
+ let out = `// @porf --funsafe-no-unlikely-proto-checks --valtype=i32
3
3
  `;
4
4
 
5
5
  const annexB_noArgs = (a0, a1) => out += `
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
1
+ // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
2
 
3
3
  // todo: trimLeft, trimRight
4
4
  export const __String_prototype_trimLeft = (_this: string) => {
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks
1
+ // @porf --funsafe-no-unlikely-proto-checks
2
2
 
3
3
  export const __Array_isArray = (x: unknown): boolean =>
4
4
  // Porffor.wasm`local.get ${x+1}` == Porffor.TYPES._array;
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
1
+ // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
2
 
3
3
  // while (len >= 8) {
4
4
  // Porffor.wasm`
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
1
+ // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
2
 
3
3
  export const __crypto_randomUUID = (): bytestring => {
4
4
  let bytes: bytestring = '................';