porffor 0.2.0-08a272e → 0.2.0-1afe9b8

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/README.md CHANGED
@@ -121,7 +121,7 @@ No particular order and no guarentees, just what could happen soon™
121
121
  - *Basic* Wasm engine (interpreter) in JS
122
122
  - More math operators (`**`, etc)
123
123
  - `do { ... } while (...)`
124
- - Rewrite `console.log` to work with strings/arrays
124
+ - Typed export inputs (array)
125
125
  - Exceptions
126
126
  - Rewrite to use actual strings (optional?)
127
127
  - `try { } finally { }`
@@ -130,7 +130,10 @@ No particular order and no guarentees, just what could happen soon™
130
130
  - Rewrite local indexes per func for smallest local header and remove unused idxs
131
131
  - Smarter inline selection (snapshots?)
132
132
  - Remove const ifs (`if (true)`, etc)
133
- - Use type(script) information to remove unneeded typechecker code
133
+ - Memory alignment
134
+ - Runtime
135
+ - WASI target
136
+ - Run precompiled Wasm file if given
134
137
  - Cool proposals
135
138
  - [Optional Chaining Assignment](https://github.com/tc39/proposal-optional-chaining-assignment)
136
139
  - [Modulus and Additional Integer Math](https://github.com/tc39/proposal-integer-and-modulus-math)
@@ -139,9 +142,12 @@ No particular order and no guarentees, just what could happen soon™
139
142
  - [Seeded Pseudo-Random Numbers](https://github.com/tc39/proposal-seeded-random)
140
143
  - [`do` expressions](https://github.com/tc39/proposal-do-expressions)
141
144
  - [String Trim Characters](https://github.com/Kingwl/proposal-string-trim-characters)
145
+ - Posts
146
+ - Inlining investigation
147
+ - Self hosted testing?
142
148
 
143
149
  ## Performance
144
- *For the things it supports most of the time*, Porffor is blazingly fast compared to most interpreters, and common engines running without JIT. For those with JIT, it is not that much slower like a traditional interpreter would be; mostly the same or a bit faster/slower depending on what.
150
+ *For the things it supports most of the time*, Porffor is *blazingly fast* compared to most interpreters, and common engines running without JIT. For those with JIT, it is usually slower by default, but can catch up with compiler arguments and typed input.
145
151
 
146
152
  ![Screenshot of comparison chart](https://github.com/CanadaHonk/porffor/assets/19228318/76c75264-cc68-4be1-8891-c06dc389d97a)
147
153
 
@@ -165,10 +171,12 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
165
171
  - Remove unneeded blocks (no `br`s inside)
166
172
  - Remove unused imports
167
173
  - Use data segments for initing arrays/strings
174
+ - (Likely more not documented yet, todo)
168
175
 
169
176
  ### Wasm module
170
177
  - Type cache/index (no repeated types)
171
178
  - No main func if empty (and other exports)
179
+ - No tags if unused/optimized out
172
180
 
173
181
  ## Test262
174
182
  Porffor can run Test262 via some hacks/transforms which remove unsupported features whilst still doing the same asserts (eg simpler error messages using literals only). It currently passes >10% (see latest commit desc for latest and details). Use `node test262` to test, it will also show a difference of overall results between the last commit and current results.
@@ -201,10 +209,13 @@ Porffor can run Test262 via some hacks/transforms which remove unsupported featu
201
209
  - `test262`: test262 runner and utils
202
210
 
203
211
  ## Usecases
204
- Basically none (other than giving people headaches). Potential ideas to come?
212
+ Basically none right now (other than giving people headaches). Potential ideas:
213
+ - Safety. As Porffor is written in JS, a memory-safe language\*, and compiles JS to Wasm, a fully sandboxed environment\*, it is quite safe. (\* These rely on the underlying implementations being secure. You could also run Wasm, or even Porffor itself, with an interpreter instead of a JIT for bonus security points too.)
214
+ - Compiling JS to native binaries. This is still very early, [`2c`](#2c) is not that good yet :(
215
+ - More in future probably?
205
216
 
206
217
  ## Usage
207
- Basically nothing will work :). See files in `test` for examples.
218
+ Basically nothing will work :). See files in `test` and `bench` for examples.
208
219
 
209
220
  1. Clone repo
210
221
  2. `npm install`
@@ -1,4 +1,4 @@
1
- import { Blocktype, Opcodes, Valtype } from "./wasmSpec.js";
1
+ import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
2
2
  import { number, i32x4 } from "./embedding.js";
3
3
 
4
4
  export const importedFuncs = [
@@ -29,6 +29,21 @@ for (let i = 0; i < importedFuncs.length; i++) {
29
29
 
30
30
  const char = c => number(c.charCodeAt(0));
31
31
 
32
+ const printStaticStr = str => {
33
+ const out = [];
34
+
35
+ for (let i = 0; i < str.length; i++) {
36
+ out.push(
37
+ // ...number(str.charCodeAt(i)),
38
+ ...number(str.charCodeAt(i), Valtype.i32),
39
+ Opcodes.i32_from_u,
40
+ [ Opcodes.call, importedFuncs.printChar ]
41
+ );
42
+ }
43
+
44
+ return out;
45
+ };
46
+
32
47
  // todo: somehow diff between these (undefined != null) while remaining falsey in wasm as a number value
33
48
  export const UNDEFINED = 0;
34
49
  export const NULL = 0;
@@ -155,25 +170,6 @@ export const BuiltinFuncs = function() {
155
170
  ]
156
171
  };
157
172
 
158
- // todo: return false for NaN
159
- this.Boolean = {
160
- params: [ valtypeBinary ],
161
- locals: [],
162
- returns: [ valtypeBinary ],
163
- returnType: 'boolean',
164
- wasm: [
165
- [ Opcodes.local_get, 0 ],
166
- ...(valtype === 'f64' ? [
167
- ...number(0),
168
- [ Opcodes.f64_ne ]
169
- ] : [
170
- ...Opcodes.eqz,
171
- [ Opcodes.i32_eqz ]
172
- ]),
173
- Opcodes.i32_from
174
- ]
175
- };
176
-
177
173
  // just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
178
174
  this.Object = {
179
175
  params: [ valtypeBinary ],
@@ -187,12 +183,125 @@ export const BuiltinFuncs = function() {
187
183
 
188
184
 
189
185
  this.__console_log = {
190
- params: [ valtypeBinary ],
191
- locals: [],
186
+ params: [ valtypeBinary, Valtype.i32 ],
187
+ typedParams: true,
188
+ locals: [ Valtype.i32, Valtype.i32 ],
192
189
  returns: [],
193
- wasm: [
194
- [ Opcodes.local_get, 0 ],
195
- [ Opcodes.call, importedFuncs.print ],
190
+ wasm: (scope, { TYPES, typeSwitch }) => [
191
+ ...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
192
+ [TYPES.number]: [
193
+ [ Opcodes.local_get, 0 ],
194
+ [ Opcodes.call, importedFuncs.print ],
195
+ ],
196
+ [TYPES.boolean]: [
197
+ [ Opcodes.local_get, 0 ],
198
+ Opcodes.i32_to_u,
199
+ [ Opcodes.if, Blocktype.void ],
200
+ ...printStaticStr('true'),
201
+ [ Opcodes.else ],
202
+ ...printStaticStr('false'),
203
+ [ Opcodes.end ]
204
+ ],
205
+ [TYPES.string]: [
206
+ // simply print a string :))
207
+ // cache input pointer as i32
208
+ [ Opcodes.local_get, 0 ],
209
+ Opcodes.i32_to_u,
210
+ [ Opcodes.local_tee, 2 ],
211
+
212
+ // make end pointer
213
+ [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
214
+ ...number(ValtypeSize.i16, Valtype.i32),
215
+ [ Opcodes.i32_mul ],
216
+
217
+ [ Opcodes.local_get, 2 ],
218
+ [ Opcodes.i32_add ],
219
+ [ Opcodes.local_set, 3 ],
220
+
221
+ [ Opcodes.loop, Blocktype.void ],
222
+
223
+ // print current char
224
+ [ Opcodes.local_get, 2 ],
225
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
226
+ Opcodes.i32_from_u,
227
+ [ Opcodes.call, importedFuncs.printChar ],
228
+
229
+ // increment pointer by sizeof i16
230
+ [ Opcodes.local_get, 2 ],
231
+ ...number(ValtypeSize.i16, Valtype.i32),
232
+ [ Opcodes.i32_add ],
233
+ [ Opcodes.local_tee, 2 ],
234
+
235
+ // if pointer != end pointer, loop
236
+ [ Opcodes.local_get, 3 ],
237
+ [ Opcodes.i32_ne ],
238
+ [ Opcodes.br_if, 0 ],
239
+
240
+ [ Opcodes.end ]
241
+ ],
242
+ [TYPES._array]: [
243
+ ...printStaticStr('[ '),
244
+
245
+ // cache input pointer as i32
246
+ [ Opcodes.local_get, 0 ],
247
+ Opcodes.i32_to_u,
248
+ [ Opcodes.local_tee, 2 ],
249
+
250
+ // make end pointer
251
+ [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
252
+ ...number(ValtypeSize[valtype], Valtype.i32),
253
+ [ Opcodes.i32_mul ],
254
+
255
+ [ Opcodes.local_get, 2 ],
256
+ [ Opcodes.i32_add ],
257
+ [ Opcodes.local_set, 3 ],
258
+
259
+ [ Opcodes.loop, Blocktype.void ],
260
+
261
+ // print current char
262
+ [ Opcodes.local_get, 2 ],
263
+ [ Opcodes.load, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
264
+ [ Opcodes.call, importedFuncs.print ],
265
+
266
+ // increment pointer by sizeof valtype
267
+ [ Opcodes.local_get, 2 ],
268
+ ...number(ValtypeSize[valtype], Valtype.i32),
269
+ [ Opcodes.i32_add ],
270
+ [ Opcodes.local_tee, 2 ],
271
+
272
+ // if pointer != end pointer, print separator and loop
273
+ [ Opcodes.local_get, 3 ],
274
+ [ Opcodes.i32_ne ],
275
+ [ Opcodes.if, Blocktype.void ],
276
+ ...printStaticStr(', '),
277
+ [ Opcodes.br, 1 ],
278
+ [ Opcodes.end ],
279
+
280
+ [ Opcodes.end ],
281
+
282
+ ...printStaticStr(' ]'),
283
+ ],
284
+ [TYPES.undefined]: [
285
+ ...printStaticStr('undefined')
286
+ ],
287
+ [TYPES.function]: [
288
+ ...printStaticStr('function () {}')
289
+ ],
290
+ [TYPES.object]: [
291
+ [ Opcodes.local_get, 0 ],
292
+ Opcodes.i32_to_u,
293
+ [ Opcodes.if, Blocktype.void ],
294
+ ...printStaticStr('{}'),
295
+ [ Opcodes.else ],
296
+ ...printStaticStr('null'),
297
+ [ Opcodes.end ]
298
+ ],
299
+ default: [
300
+ [ Opcodes.local_get, 0 ],
301
+ [ Opcodes.call, importedFuncs.print ],
302
+ ]
303
+ }, Blocktype.void),
304
+
196
305
  ...char('\n'),
197
306
  [ Opcodes.call, importedFuncs.printChar ]
198
307
  ]
@@ -571,6 +680,23 @@ export const BuiltinFuncs = function() {
571
680
  };
572
681
 
573
682
 
683
+ this.__Porffor_type = {
684
+ params: [ valtypeBinary, Valtype.i32 ],
685
+ typedParams: true,
686
+ locals: [ Valtype.i32, Valtype.i32 ],
687
+ returns: [ valtypeBinary ],
688
+ returnType: process.argv.includes('-bytestring') ? '_bytestring' : 'string',
689
+ wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
690
+ const bc = {};
691
+ for (const x in TYPE_NAMES) {
692
+ bc[x] = makeString(scope, TYPE_NAMES[x], false, '#Porffor_type_result');
693
+ }
694
+
695
+ return typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], bc);
696
+ }
697
+ };
698
+
699
+
574
700
  this.__SIMD_i32x4_load = {
575
701
  params: [ Valtype.i32 ],
576
702
  locals: [],