porffor 0.16.0-6572d1c74 → 0.16.0-688a50c13

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.
@@ -1,6 +1,5 @@
1
1
  import { Opcodes, Blocktype, Valtype, ValtypeSize } from './wasmSpec.js';
2
2
  import { number } from './embedding.js';
3
- import { unsignedLEB128 } from './encoding.js';
4
3
  import { UNDEFINED } from './builtins.js';
5
4
  import { TYPES } from './types.js';
6
5
  import Prefs from './prefs.js';
@@ -55,10 +54,10 @@ export const PrototypeFuncs = function() {
55
54
 
56
55
  // read from memory
57
56
  [ Opcodes.local_get, iTmp ],
58
- [ Opcodes.load, 0, ...unsignedLEB128(ValtypeSize.i32) ],
57
+ [ Opcodes.load, 0, ValtypeSize.i32 ],
59
58
 
60
59
  [ Opcodes.local_get, iTmp ],
61
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ]
60
+ [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
62
61
  ],
63
62
 
64
63
  // todo: only for 1 argument
@@ -74,12 +73,12 @@ export const PrototypeFuncs = function() {
74
73
  // store value
75
74
  [ Opcodes.local_get, iTmp ],
76
75
  ...wNewMember,
77
- [ Opcodes.store, 0, ...unsignedLEB128(ValtypeSize.i32) ],
76
+ [ Opcodes.store, 0, ValtypeSize.i32 ],
78
77
 
79
78
  // store type
80
79
  [ Opcodes.local_get, iTmp ],
81
80
  ...wType,
82
- [ Opcodes.i32_store8, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ],
81
+ [ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
83
82
 
84
83
  // bump array length by 1 and return it
85
84
  ...length.setI32([
@@ -137,10 +136,10 @@ export const PrototypeFuncs = function() {
137
136
  [ Opcodes.local_set, iTmp ],
138
137
 
139
138
  [ Opcodes.local_get, iTmp ],
140
- [ Opcodes.load, 0, ...unsignedLEB128(ValtypeSize.i32) ],
139
+ [ Opcodes.load, 0, ValtypeSize.i32 ],
141
140
 
142
141
  [ Opcodes.local_get, iTmp ],
143
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ]
142
+ [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
144
143
  ])
145
144
  ],
146
145
 
@@ -168,10 +167,10 @@ export const PrototypeFuncs = function() {
168
167
  // load first element
169
168
  // todo/perf: unusedValue opt
170
169
  ...pointer,
171
- [ Opcodes.load, 0, ...unsignedLEB128(ValtypeSize.i32) ],
170
+ [ Opcodes.load, 0, ValtypeSize.i32 ],
172
171
 
173
172
  ...pointer,
174
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ],
173
+ [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
175
174
 
176
175
  // offset page by -1 ind
177
176
  // ...number(pointer + ValtypeSize.i32, Valtype.i32), // dst = base array index + length size
@@ -250,12 +249,12 @@ export const PrototypeFuncs = function() {
250
249
  // store value
251
250
  [ Opcodes.local_get, iTmp2 ],
252
251
  [ Opcodes.local_get, iTmp ],
253
- [ Opcodes.store, 0, ...unsignedLEB128(ValtypeSize.i32) ],
252
+ [ Opcodes.store, 0, ValtypeSize.i32 ],
254
253
 
255
254
  // store type
256
255
  [ Opcodes.local_get, iTmp2 ],
257
256
  ...wType,
258
- [ Opcodes.i32_store8, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ],
257
+ [ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
259
258
 
260
259
  // pointer - sizeof value
261
260
  ...length.getCachedI32(),
@@ -292,11 +291,8 @@ export const PrototypeFuncs = function() {
292
291
  const [ newOut, newPointer ] = arrayShell(1, 'i16');
293
292
 
294
293
  return [
295
- // setup new/out array
294
+ // setup new/out array and use pointer for store
296
295
  ...newOut,
297
- [ Opcodes.drop ],
298
-
299
- ...number(0, Valtype.i32), // base 0 for store later
300
296
 
301
297
  ...wIndex,
302
298
  Opcodes.i32_to_u,
@@ -335,13 +331,14 @@ export const PrototypeFuncs = function() {
335
331
  [ Opcodes.i32_add ],
336
332
 
337
333
  // load current string ind {arg}
338
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
334
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
339
335
 
340
336
  // store to new string ind 0
341
- [ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
337
+ [ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
342
338
 
343
339
  // return new string (pointer)
344
- ...number(newPointer)
340
+ ...newPointer,
341
+ Opcodes.i32_from_u
345
342
  ];
346
343
  },
347
344
 
@@ -350,11 +347,8 @@ export const PrototypeFuncs = function() {
350
347
  const [ newOut, newPointer ] = arrayShell(1, 'i16');
351
348
 
352
349
  return [
353
- // setup new/out array
350
+ // setup new/out array and use as pointer for store
354
351
  ...newOut,
355
- [ Opcodes.drop ],
356
-
357
- ...number(0, Valtype.i32), // base 0 for store later
358
352
 
359
353
  ...wIndex,
360
354
  Opcodes.i32_to,
@@ -366,13 +360,14 @@ export const PrototypeFuncs = function() {
366
360
  [ Opcodes.i32_add ],
367
361
 
368
362
  // load current string ind {arg}
369
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
363
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
370
364
 
371
365
  // store to new string ind 0
372
- [ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
366
+ [ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
373
367
 
374
368
  // return new string (page)
375
- ...number(newPointer)
369
+ ...newPointer,
370
+ Opcodes.i32_from_u
376
371
  ];
377
372
  },
378
373
 
@@ -411,7 +406,7 @@ export const PrototypeFuncs = function() {
411
406
  [ Opcodes.i32_add ],
412
407
 
413
408
  // load current string ind {arg}
414
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
409
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
415
410
  Opcodes.i32_from_u
416
411
  ],
417
412
 
@@ -433,7 +428,7 @@ export const PrototypeFuncs = function() {
433
428
  [ Opcodes.block, Blocktype.void ],
434
429
 
435
430
  [ Opcodes.local_get, iTmp ],
436
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
431
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
437
432
  [ Opcodes.local_set, iTmp2 ],
438
433
 
439
434
  // if not surrogate, continue
@@ -455,7 +450,7 @@ export const PrototypeFuncs = function() {
455
450
 
456
451
  // if not followed by trailing surrogate, return false
457
452
  [ Opcodes.local_get, iTmp ],
458
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize.i16) ],
453
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 + ValtypeSize.i16 ],
459
454
  ...number(0xFC00, Valtype.i32),
460
455
  [ Opcodes.i32_and ],
461
456
  ...number(0xDC00, Valtype.i32),
@@ -507,11 +502,8 @@ export const PrototypeFuncs = function() {
507
502
  const [ newOut, newPointer ] = arrayShell(1, 'i8');
508
503
 
509
504
  return [
510
- // setup new/out array
505
+ // setup new/out array and use pointer for store later
511
506
  ...newOut,
512
- [ Opcodes.drop ],
513
-
514
- ...number(0, Valtype.i32), // base 0 for store later
515
507
 
516
508
  ...wIndex,
517
509
  Opcodes.i32_to_u,
@@ -548,13 +540,14 @@ export const PrototypeFuncs = function() {
548
540
  [ Opcodes.i32_add ],
549
541
 
550
542
  // load current string ind {arg}
551
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
543
+ [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
552
544
 
553
545
  // store to new string ind 0
554
- [ Opcodes.i32_store8, 0, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
546
+ [ Opcodes.i32_store8, 0, ValtypeSize.i32 ],
555
547
 
556
548
  // return new string (pointer)
557
- ...number(newPointer)
549
+ ...newPointer,
550
+ Opcodes.i32_from_u
558
551
  ];
559
552
  },
560
553
 
@@ -563,11 +556,8 @@ export const PrototypeFuncs = function() {
563
556
  const [ newOut, newPointer ] = arrayShell(1, 'i8');
564
557
 
565
558
  return [
566
- // setup new/out array
559
+ // setup new/out array and use pointer for later
567
560
  ...newOut,
568
- [ Opcodes.drop ],
569
-
570
- ...number(0, Valtype.i32), // base 0 for store later
571
561
 
572
562
  ...wIndex,
573
563
  Opcodes.i32_to,
@@ -576,13 +566,14 @@ export const PrototypeFuncs = function() {
576
566
  [ Opcodes.i32_add ],
577
567
 
578
568
  // load current string ind {arg}
579
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
569
+ [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
580
570
 
581
571
  // store to new string ind 0
582
- [ Opcodes.i32_store8, 0, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
572
+ [ Opcodes.i32_store8, 0, ValtypeSize.i32 ],
583
573
 
584
574
  // return new string (page)
585
- ...number(newPointer)
575
+ ...newPointer,
576
+ Opcodes.i32_from_u
586
577
  ];
587
578
  },
588
579
 
@@ -618,7 +609,7 @@ export const PrototypeFuncs = function() {
618
609
  [ Opcodes.i32_add ],
619
610
 
620
611
  // load current string ind {arg}
621
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
612
+ [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
622
613
  Opcodes.i32_from_u
623
614
  ],
624
615
 
package/compiler/wrap.js CHANGED
@@ -11,13 +11,13 @@ const fs = (typeof process?.version !== 'undefined' ? (await import('node:fs'))
11
11
  const bold = x => `\u001b[1m${x}\u001b[0m`;
12
12
 
13
13
  export const readByteStr = (memory, ptr) => {
14
- const length = (new Int32Array(memory.buffer, ptr, 1))[0];
14
+ const length = (new Int32Array(memory.buffer.slice(ptr, ptr + 4), 0, 1))[0];
15
15
  return Array.from(new Uint8Array(memory.buffer, ptr + 4, length)).map(x => String.fromCharCode(x)).join('');
16
16
  };
17
17
 
18
18
  export const writeByteStr = (memory, ptr, str) => {
19
19
  const length = str.length;
20
- (new Int32Array(memory.buffer, ptr, 1))[0] = length;
20
+ (new Int32Array(memory.buffer.slice(ptr, ptr + 4), 0, 1))[0] = length;
21
21
 
22
22
  const arr = new Uint8Array(memory.buffer, ptr + 4, length);
23
23
  for (let i = 0; i < length; i++) {
@@ -46,17 +46,17 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
46
46
  }
47
47
 
48
48
  case TYPES.string: {
49
- const length = (new Int32Array(memory.buffer, value, 1))[0];
49
+ const length = (new Int32Array(memory.buffer.slice(value, value + 4), 0, 1))[0];
50
50
  return Array.from(new Uint16Array(memory.buffer, value + 4, length)).map(x => String.fromCharCode(x)).join('');
51
51
  }
52
52
 
53
53
  case TYPES.bytestring: {
54
- const length = (new Int32Array(memory.buffer, value, 1))[0];
54
+ const length = (new Int32Array(memory.buffer.slice(value, value + 4), 0, 1))[0];
55
55
  return Array.from(new Uint8Array(memory.buffer, value + 4, length)).map(x => String.fromCharCode(x)).join('');
56
56
  }
57
57
 
58
58
  case TYPES.array: {
59
- const length = (new Int32Array(memory.buffer, value, 1))[0];
59
+ const length = (new Int32Array(memory.buffer.slice(value, value + 4), 0, 1))[0];
60
60
 
61
61
  const out = [];
62
62
  for (let i = 0; i < length; i++) {
@@ -77,12 +77,12 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
77
77
  }
78
78
 
79
79
  case TYPES.date: {
80
- const t = (new Float64Array(memory.buffer, value, 1))[0];
80
+ const t = (new Float64Array(memory.buffer.slice(value, value + 8), 0, 1))[0];
81
81
  return new Date(t);
82
82
  }
83
83
 
84
84
  case TYPES.set: {
85
- const size = (new Int32Array(memory.buffer, value, 1))[0];
85
+ const size = (new Int32Array(memory.buffer.slice(value, value + 4), 0, 1))[0];
86
86
 
87
87
  const out = new Set();
88
88
  for (let i = 0; i < size; i++) {
@@ -102,17 +102,17 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
102
102
  return out;
103
103
  }
104
104
 
105
- case TYPES.symbol: {
106
- const descStore = pages.get('bytestring: __Porffor_symbol_descStore/ptr').ind * pageSize;
107
- const offset = descStore + 4 + ((value - 1) * 9);
105
+ // case TYPES.symbol: {
106
+ // const descStore = pages.get('bytestring: __Porffor_symbol_descStore/ptr').ind * pageSize;
107
+ // const offset = descStore + 4 + ((value - 1) * 9);
108
108
 
109
- const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
110
- const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
109
+ // const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
110
+ // const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
111
111
 
112
- const desc = porfToJSValue({ memory, funcs, pages }, v, t);
112
+ // const desc = porfToJSValue({ memory, funcs, pages }, v, t);
113
113
 
114
- return Symbol(desc);
115
- }
114
+ // return Symbol(desc);
115
+ // }
116
116
 
117
117
  default: return value;
118
118
  }
@@ -128,7 +128,7 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
128
128
 
129
129
  if (source.includes?.('export ')) flags.push('module');
130
130
 
131
- fs.writeFileSync('out.wasm', Buffer.from(wasm));
131
+ // fs.writeFileSync('out.wasm', Buffer.from(wasm));
132
132
 
133
133
  times.push(performance.now() - t1);
134
134
  if (Prefs.profileCompiler) console.log(bold(`compiled in ${times[0].toFixed(2)}ms`));
package/package.json CHANGED
@@ -1,12 +1,10 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.16.0-6572d1c74",
4
+ "version": "0.16.0-688a50c13",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
- "scripts": {
8
- "precompile": "node ./compiler/precompile.js"
9
- },
7
+ "scripts": {},
10
8
  "dependencies": {
11
9
  "acorn": "^8.11.3",
12
10
  "node-repl-polyfill": "^0.1.1"
@@ -28,5 +26,5 @@
28
26
  "bugs": {
29
27
  "url": "https://github.com/CanadaHonk/porffor/issues"
30
28
  },
31
- "homepage": "https://porffor.goose.icu"
29
+ "homepage": "https://porffor.dev"
32
30
  }
package/runner/index.js CHANGED
@@ -1,6 +1,4 @@
1
1
  #!/usr/bin/env node
2
-
3
- import compile from '../compiler/wrap.js';
4
2
  import fs from 'node:fs';
5
3
 
6
4
  const start = performance.now();
@@ -59,10 +57,15 @@ if (process.argv.includes('--help')) {
59
57
  }
60
58
 
61
59
  let file = process.argv.slice(2).find(x => x[0] !== '-');
62
- if (['run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm'].includes(file)) {
60
+ if (['precompile', 'run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm'].includes(file)) {
63
61
  // remove this arg
64
62
  process.argv.splice(process.argv.indexOf(file), 1);
65
63
 
64
+ if (file === 'precompile') {
65
+ await import('../compiler/precompile.js');
66
+ await new Promise(() => {}); // do nothing for the rest of this file
67
+ }
68
+
66
69
  if (file === 'profile') {
67
70
  await import('./profile.js');
68
71
  await new Promise(() => {}); // do nothing for the rest of this file
@@ -105,6 +108,8 @@ if (!file) {
105
108
 
106
109
  const source = fs.readFileSync(file, 'utf8');
107
110
 
111
+ const compile = (await import('../compiler/wrap.js')).default;
112
+
108
113
  let cache = '';
109
114
  const print = str => {
110
115
  /* cache += str;