porffor 0.2.0-ef043de → 0.2.0-f435128

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,181 @@
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
+ <br>
8
+
9
+ ## Types
10
+
11
+ Porffor has usual JS types (or at least the ones it supports), but also internal types for various reasons.
12
+
13
+ ### ByteString
14
+
15
+ 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.
16
+
17
+ ### i32
18
+
19
+ This is complicated internally but essentially, only use it for pointers.
20
+
21
+ <br>
22
+
23
+ ## Pointers
24
+
25
+ Pointers are the main (and most difficult) unique feature you ~need to understand when dealing with objects (arrays, strings, ...).
26
+
27
+ We'll explain things per common usage you will likely need to know:
28
+
29
+ ## Commonly used Wasm code
30
+
31
+ ### Get a pointer
32
+
33
+ ```js
34
+ Porffor.wasm`local.get ${foobar}`
35
+ ```
36
+
37
+ 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.
38
+
39
+ ### Store a character in a ByteString
40
+
41
+ ```js
42
+ Porffor.wasm.i32.store8(pointer, characterCode, 0, 4)
43
+ ```
44
+
45
+ Stores the character code `characterCode` at the pointer `pointer` **for a ByteString**.[^1]
46
+
47
+ ### Store a character in a String
48
+
49
+ ```js
50
+ Porffor.wasm.i32.store16(pointer, characterCode, 0, 4)
51
+ ```
52
+
53
+ Stores the character code `characterCode` at the pointer `pointer` **for a String**.[^1]
54
+
55
+ ### Load a character from a ByteString
56
+
57
+ ```js
58
+ Porffor.wasm.i32.load8_u(pointer, 0, 4)
59
+ ```
60
+
61
+ Loads the character code at the pointer `pointer` **for a ByteString**.[^1]
62
+
63
+ ### Load a character from a String
64
+
65
+ ```js
66
+ Porffor.wasm.i32.load16_u(pointer, 0, 4)
67
+ ```
68
+
69
+ Loads the character code at the pointer `pointer` **for a String**.[^1]
70
+
71
+ ### Manually store the length of an object
72
+
73
+ ```js
74
+ Porffor.wasm.i32.store(pointer, length, 0, 0)
75
+ ```
76
+
77
+ 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]
78
+
79
+ <br>
80
+
81
+ ## Example
82
+
83
+ Here is the code for `ByteString.prototype.toUpperCase()`:
84
+
85
+ ```ts
86
+ export const ___bytestring_prototype_toUpperCase = (_this: bytestring) => {
87
+ const len: i32 = _this.length;
88
+
89
+ let out: bytestring = '';
90
+ Porffor.wasm.i32.store(out, len, 0, 0);
91
+
92
+ let i: i32 = Porffor.wasm`local.get ${_this}`,
93
+ j: i32 = Porffor.wasm`local.get ${out}`;
94
+
95
+ const endPtr: i32 = i + len;
96
+ while (i < endPtr) {
97
+ let chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
98
+
99
+ if (chr >= 97) if (chr <= 122) chr -= 32;
100
+
101
+ Porffor.wasm.i32.store8(j++, chr, 0, 4);
102
+ }
103
+
104
+ return out;
105
+ };
106
+ ```
107
+
108
+ Now let's go through it section by section:
109
+
110
+ ```ts
111
+ export const ___bytestring_prototype_toUpperCase = (_this: bytestring) => {
112
+ ```
113
+
114
+ Here we define a built-in for Porffor. Notably:
115
+ - We do not use `a.b.c`, instead we use `__a_b_c`
116
+ - The ByteString type is actually `_bytestring`, as internal types have an extra `_` at the beginning (this is due to be fixed/simplified soon(tm))
117
+ - We use a `_this` argument, as `this` does not exist in Porffor yet
118
+ - We use an arrow function
119
+
120
+ ---
121
+
122
+ ```ts
123
+ const len: i32 = _this.length;
124
+
125
+ let out: bytestring = '';
126
+ Porffor.wasm.i32.store(out, len, 0, 0);
127
+ ```
128
+
129
+ 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.
130
+
131
+ ---
132
+
133
+ ```ts
134
+ let i: i32 = Porffor.wasm`local.get ${_this}`,
135
+ j: i32 = Porffor.wasm`local.get ${out}`;
136
+ ```
137
+
138
+ Get the pointers for `_this` and `out` as `i32`s (~`number`s).
139
+
140
+ ---
141
+
142
+ ```ts
143
+ const endPtr: i32 = i + len;
144
+ while (i < endPtr) {
145
+ ```
146
+
147
+ 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.
148
+
149
+ ---
150
+
151
+ ```ts
152
+ let chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
153
+ ```
154
+
155
+ Read the character (code) from the current `_this` pointer variable, and increment it so next iteration it reads the next character, etc.
156
+
157
+ ---
158
+
159
+ ```ts
160
+ if (chr >= 97) if (chr <= 122) chr -= 32;
161
+ ```
162
+
163
+ 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`).
164
+
165
+ ---
166
+
167
+ ```ts
168
+ Porffor.wasm.i32.store8(j++, chr, 0, 4);
169
+ ```
170
+
171
+ Store the character code into the `out` pointer variable, and increment it.
172
+
173
+ <br>
174
+
175
+ ## Formatting/linting
176
+
177
+ 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 :^)
178
+
179
+ [^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).
180
+
181
+ [^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
@@ -23,13 +23,13 @@ Expect nothing to work! Only very limited JS is currently supported. See files i
23
23
  **`porf path/to/script.js`**
24
24
 
25
25
  ### Compiling to Wasm
26
- **`porf compile path/to/script.js out.wasm`**. Currently it does not use an import standard like WASI, so it is mostly unusable on its own.
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.
27
27
 
28
28
  ### Compiling to native binaries
29
29
  > [!WARNING]
30
30
  > Compiling to native binaries uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
31
31
 
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.
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.
33
33
 
34
34
  ### Compiling to C
35
35
  > [!WARNING]
@@ -57,31 +57,25 @@ Expect nothing to work! Only very limited JS is currently supported. See files i
57
57
 
58
58
 
59
59
  ### Options
60
- - `-target=wasm|c|native` (default: `wasm`) to set target output (native compiles c output to binary, see args below)
61
- - `-target=c|native` only:
62
- - `-o=out.c|out.exe|out` to set file to output c or binary
63
- - `-target=native` only:
64
- - `-compiler=clang` to set compiler binary (path/name) to use to compile
65
- - `-cO=O3` to set compiler opt argument
66
- - `-parser=acorn|@babel/parser|meriyah|hermes-parser` (default: `acorn`) to set which parser to use
67
- - `-parse-types` to enable parsing type annotations/typescript. if `-parser` is unset, changes default to `@babel/parser`. does not type check
68
- - `-opt-types` to perform optimizations using type annotations as compiler hints. does not type check
69
- - `-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
70
64
  - `-O0` to disable opt
71
65
  - `-O1` (default) to enable basic opt (simplify insts, treeshake wasm imports)
72
66
  - `-O2` to enable advanced opt (inlining). unstable
73
67
  - `-O3` to enable advanceder opt (precompute const math). unstable
74
- - `-no-run` to not run wasm output, just compile
75
- - `-opt-log` to log some opts
76
- - `-code-log` to log some codegen (you probably want `-funcs`)
77
- - `-regex-log` to log some regex
78
- - `-funcs` to log funcs
79
- - `-ast-log` to log AST
80
- - `-opt-funcs` to log funcs after opt
81
- - `-sections` to log sections as hex
82
- - `-opt-no-inline` to not inline any funcs
83
- - `-tail-call` to enable tail calls (experimental + not widely implemented)
84
- - `-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?)
85
79
 
86
80
  ### Running in the repo
87
81
  The repo comes with easy alias files for Unix and Windows, which you can use like so:
@@ -211,7 +205,7 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
211
205
  ### Traditional opts
212
206
  - Inlining functions (WIP, limited)
213
207
  - Inline const math ops
214
- - Tail calls (behind flag `-tail-call`)
208
+ - Tail calls (behind flag `--tail-call`)
215
209
 
216
210
  ### Wasm transforms
217
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 = '................';