porffor 0.16.0-30af62694 → 0.16.0-5c5338783

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/CONTRIBUTING.md CHANGED
@@ -26,7 +26,7 @@ You can also swap out `node` in the alias to use another runtime like Deno (`den
26
26
 
27
27
  ### Precompile
28
28
 
29
- **If you update any file inside `compiler/builtins` you will need to do this for it to update inside Porffor otherwise your changes will have no effect.** Run `node compiler/precompile.js` to precompile. It may error during this, if so, you might have an error in your code or there could be a compiler error with Porffor (feel free to ask for help as soon as you encounter any errors with it).
29
+ **If you update any file inside `compiler/builtins` you will need to do this for it to update inside Porffor otherwise your changes will have no effect.** Run `./porf precompile` to precompile. It may error during this, if so, you might have an error in your code or there could be a compiler error with Porffor (feel free to ask for help as soon as you encounter any errors with it).
30
30
 
31
31
  <br>
32
32
 
@@ -0,0 +1,129 @@
1
+ import { Blocktype, Opcodes, PageSize, Valtype } from './wasmSpec.js';
2
+ import { number } from './embedding.js';
3
+ import Prefs from './prefs.js';
4
+
5
+ // we currently have 3 allocators:
6
+ // - static (default): a static/compile-time allocator. fast (no grow/run-time alloc needed) but can break some code
7
+ // - grow: perform a memory.grow every allocation. simple but maybe slow?
8
+ // - chunk: perform large memory.grow's in chunks when needed. needs investigation
9
+
10
+ export default name => {
11
+ switch (name) {
12
+ case 'static': return new StaticAllocator();
13
+ case 'grow': return new GrowAllocator();
14
+ case 'chunk': return new ChunkAllocator();
15
+ default: throw new Error(`unknown allocator: ${name}`);
16
+ }
17
+ };
18
+
19
+ export class StaticAllocator {
20
+ constructor() {
21
+ }
22
+
23
+ allocType(itemType) {
24
+ switch (itemType) {
25
+ case 'i8': return 'bytestring';
26
+ case 'i16': return 'string';
27
+
28
+ default: return 'array';
29
+ }
30
+ }
31
+
32
+ ptr(ind) {
33
+ if (ind === 0) return 4;
34
+ return ind * PageSize;
35
+ }
36
+
37
+ alloc({ scope, pages }, name, { itemType }) {
38
+ const reason = `${this.allocType(itemType)}: ${Prefs.scopedPageNames ? (scope.name + '/') : ''}${name}`;
39
+
40
+ if (pages.has(reason)) return number(this.ptr(pages.get(reason).ind), Valtype.i32);
41
+
42
+ if (reason.startsWith('array:')) pages.hasArray = true;
43
+ if (reason.startsWith('string:')) pages.hasString = true;
44
+ if (reason.startsWith('bytestring:')) pages.hasByteString = true;
45
+ if (reason.includes('string:')) pages.hasAnyString = true;
46
+
47
+ let ind = pages.size;
48
+ pages.set(reason, { ind, type: itemType });
49
+
50
+ scope.pages ??= new Map();
51
+ scope.pages.set(reason, { ind, type: itemType });
52
+
53
+ return number(this.ptr(ind), Valtype.i32);
54
+ }
55
+ }
56
+
57
+ export class GrowAllocator {
58
+ constructor() {
59
+ Prefs.rmUnusedTypes = false;
60
+ }
61
+
62
+ alloc() {
63
+ return [
64
+ // grow by 1 page
65
+ [ Opcodes.i32_const, 1 ],
66
+ [ Opcodes.memory_grow, 0 ], // returns old page count
67
+
68
+ // get ptr (page count * page size)
69
+ number(65536, Valtype.i32)[0],
70
+ [ Opcodes.i32_mul ]
71
+ ];
72
+ }
73
+ }
74
+
75
+ export class ChunkAllocator {
76
+ constructor(chunkSize) {
77
+ Prefs.rmUnusedTypes = false;
78
+
79
+ // todo: what should be the default
80
+ // 16: 1MiB chunks
81
+ // 64KiB * chunk size each growth
82
+ this.chunkSize = chunkSize ?? Prefs.chunkAllocatorSize ?? 16;
83
+ }
84
+
85
+ alloc({ asmFunc, funcIndex, globals }) {
86
+ const func = funcIndex['#chunkallocator_alloc'] ?? asmFunc('#chunkallocator_alloc', {
87
+ wasm: [
88
+ [ Opcodes.global_get, 0 ],
89
+ [ Opcodes.global_get, 1 ],
90
+ [ Opcodes.i32_ge_s ],
91
+ [ Opcodes.if, Valtype.i32 ], // ptr >= next
92
+ // grow by chunk size pages
93
+ [ Opcodes.i32_const, this.chunkSize ],
94
+ [ Opcodes.memory_grow, 0 ],
95
+
96
+ // ptr = prev memory size * PageSize
97
+ number(65536, Valtype.i32)[0],
98
+ [ Opcodes.i32_mul ],
99
+ [ Opcodes.global_set, 0 ],
100
+
101
+ // next = ptr + ((chunkSize - 1) * PageSize)
102
+ [ Opcodes.global_get, 0 ],
103
+ number(65536 * (this.chunkSize - 1), Valtype.i32)[0],
104
+ [ Opcodes.i32_add ],
105
+ [ Opcodes.global_set, 1 ],
106
+
107
+ // return ptr
108
+ [ Opcodes.global_get, 0 ],
109
+ [ Opcodes.else ],
110
+ // return ptr = ptr + PageSize
111
+ [ Opcodes.global_get, 0 ],
112
+ number(65536, Valtype.i32)[0],
113
+ [ Opcodes.i32_add ],
114
+ [ Opcodes.global_set, 0 ],
115
+ [ Opcodes.global_get, 0 ],
116
+ [ Opcodes.end ],
117
+ ],
118
+ params: [],
119
+ locals: [],
120
+ globals: [ Valtype.i32, Valtype.i32 ],
121
+ globalNames: ['#chunkallocator_ptr', '#chunkallocator_next'],
122
+ returns: [ Valtype.i32 ],
123
+ }).index;
124
+
125
+ return [
126
+ [ Opcodes.call, func ]
127
+ ];
128
+ }
129
+ }
@@ -240,7 +240,13 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
240
240
 
241
241
  const dataSection = data.length === 0 ? [] : createSection(
242
242
  Section.data,
243
- encodeVector(data.map(x => [ 0x00, Opcodes.i32_const, ...signedLEB128(x.offset), Opcodes.end, ...encodeVector(x.bytes) ]))
243
+ encodeVector(data.map(x => {
244
+ // type: active
245
+ if (x.offset != null) return [ 0x00, Opcodes.i32_const, ...signedLEB128(x.offset), Opcodes.end, ...encodeVector(x.bytes) ];
246
+
247
+ // type: passive
248
+ return [ 0x01, ...encodeVector(x.bytes) ];
249
+ }))
244
250
  );
245
251
 
246
252
  const dataCountSection = data.length === 0 ? [] : createSection(
@@ -171,12 +171,11 @@ export const __Array_prototype_filter = (_this: any[], callbackFn: any) => {
171
171
  };
172
172
 
173
173
  export const __Array_prototype_map = (_this: any[], callbackFn: any) => {
174
- const out: any[] = [];
175
-
176
- const len: i32 = _this.length;
177
174
  let i: i32 = 0;
175
+ const len: i32 = _this.length;
176
+ const out: any[] = new Array(len);
178
177
  while (i < len) {
179
- out.push(callbackFn(_this[i], i++, _this));
178
+ out[i] = callbackFn(_this[i], i++, _this);
180
179
  }
181
180
 
182
181
  return out;
@@ -722,12 +722,9 @@ export const __Porffor_date_allocate = (): Date => {
722
722
  const hack: bytestring = '';
723
723
 
724
724
  if (hack.length == 0) {
725
- hack.length = Porffor.wasm`i32.const 1
726
- memory.grow 0
727
- drop
728
- memory.size 0
725
+ hack.length = Porffor.wasm`
729
726
  i32.const 1
730
- i32.sub
727
+ memory.grow 0
731
728
  i32.const 65536
732
729
  i32.mul
733
730
  i32.from_u`;
@@ -2,12 +2,9 @@ import type {} from './porffor.d.ts';
2
2
 
3
3
  // dark wasm magic for dealing with memory, sorry.
4
4
  export const __Porffor_allocate = (): number => {
5
- Porffor.wasm`i32.const 1
6
- memory.grow 0
7
- drop
8
- memory.size 0
5
+ Porffor.wasm`
9
6
  i32.const 1
10
- i32.sub
7
+ memory.grow 0
11
8
  i32.const 65536
12
9
  i32.mul
13
10
  i32.from_u