porffor 0.47.6 → 0.47.8

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,130 +0,0 @@
1
- import { Opcodes, PageSize, Valtype } from './wasmSpec.js';
2
- import { number } from './embedding.js';
3
- import {} 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 16;
34
- return ind * PageSize;
35
- }
36
-
37
- alloc({ scope, pages }, name, { itemType }) {
38
- let scopeName = scope.name;
39
- if (globalThis.precompile && scopeName === 'main') scopeName = globalThis.precompile;
40
- const reason = `${this.allocType(itemType)}: ${Prefs.scopedPageNames ? (scopeName + '/') : ''}${name}`;
41
-
42
- this.lastName = reason;
43
- if (pages.has(reason)) {
44
- const ptr = this.lastPtr = this.ptr(pages.get(reason).ind);
45
- return number(ptr, Valtype.i32);
46
- }
47
-
48
- let ind = pages.size;
49
- pages.set(reason, { ind, type: itemType });
50
-
51
- scope.pages ??= new Map();
52
- scope.pages.set(reason, { ind, type: itemType });
53
-
54
- const ptr = this.lastPtr = this.ptr(ind);
55
- return number(ptr, Valtype.i32);
56
- }
57
- }
58
-
59
- export class GrowAllocator {
60
- constructor() {
61
- Prefs.rmUnusedTypes = false;
62
- }
63
-
64
- alloc() {
65
- return [
66
- // grow by 1 page
67
- [ Opcodes.i32_const, 1 ],
68
- [ Opcodes.memory_grow, 0 ], // returns old page count
69
-
70
- // get ptr (page count * page size)
71
- number(65536, Valtype.i32)[0],
72
- [ Opcodes.i32_mul ]
73
- ];
74
- }
75
- }
76
-
77
- export class ChunkAllocator {
78
- constructor(chunkSize) {
79
- Prefs.rmUnusedTypes = false;
80
-
81
- // 64KiB * chunk size each growth
82
- // 16: 1MiB chunks
83
- this.chunkSize = chunkSize ?? Prefs.chunkAllocatorSize ?? 16;
84
- }
85
-
86
- alloc({ asmFunc, funcIndex }) {
87
- const func = funcIndex['#chunkallocator_alloc'] ?? asmFunc('#chunkallocator_alloc', {
88
- wasm: [
89
- [ Opcodes.global_get, 0 ],
90
- [ Opcodes.global_get, 1 ],
91
- [ Opcodes.i32_ge_s ],
92
- [ Opcodes.if, Valtype.i32 ], // ptr >= next
93
- // grow by chunk size pages
94
- [ Opcodes.i32_const, this.chunkSize ],
95
- [ Opcodes.memory_grow, 0 ],
96
-
97
- // ptr = prev memory size * PageSize
98
- number(65536, Valtype.i32)[0],
99
- [ Opcodes.i32_mul ],
100
- [ Opcodes.global_set, 0 ],
101
-
102
- // next = ptr + ((chunkSize - 1) * PageSize)
103
- [ Opcodes.global_get, 0 ],
104
- number(65536 * (this.chunkSize - 1), Valtype.i32)[0],
105
- [ Opcodes.i32_add ],
106
- [ Opcodes.global_set, 1 ],
107
-
108
- // return ptr
109
- [ Opcodes.global_get, 0 ],
110
- [ Opcodes.else ],
111
- // return ptr = ptr + PageSize
112
- [ Opcodes.global_get, 0 ],
113
- number(65536, Valtype.i32)[0],
114
- [ Opcodes.i32_add ],
115
- [ Opcodes.global_set, 0 ],
116
- [ Opcodes.global_get, 0 ],
117
- [ Opcodes.end ],
118
- ],
119
- params: [],
120
- locals: [],
121
- globals: [ Valtype.i32, Valtype.i32 ],
122
- globalNames: ['#chunkallocator_ptr', '#chunkallocator_next'],
123
- returns: [ Valtype.i32 ],
124
- }).index;
125
-
126
- return [
127
- [ Opcodes.call, func ]
128
- ];
129
- }
130
- }