porffor 0.16.0-79cd8c0c8 → 0.16.0-8107e135a

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
 
package/bf ADDED
Binary file
@@ -0,0 +1,25 @@
1
+ import { Opcodes, Valtype } from '../wasmSpec.js';
2
+ import { number } from '../embedding.js';
3
+ import Prefs from '../prefs.js';
4
+
5
+ export default class GrowAllocator {
6
+ constructor() {
7
+ Prefs.rmUnusedTypes = false;
8
+ }
9
+
10
+ alloc() {
11
+ return [
12
+ // get current page count
13
+ [ Opcodes.memory_size, 0 ],
14
+
15
+ // grow by 1 page
16
+ [ Opcodes.i32_const, 1 ],
17
+ [ Opcodes.memory_grow, 0 ],
18
+ [ Opcodes.drop ],
19
+
20
+ // get ptr (page count * page size)
21
+ number(65536, Valtype.i32)[0],
22
+ [ Opcodes.i32_mul ]
23
+ ];
24
+ }
25
+ }
@@ -0,0 +1,10 @@
1
+ import StaticAllocator from './static.js';
2
+ import GrowAllocator from './grow.js';
3
+
4
+ export default name => {
5
+ switch (name) {
6
+ case 'static': return new StaticAllocator();
7
+ case 'grow': return new GrowAllocator();
8
+ default: throw new Error(`unknown allocator: ${name}`);
9
+ }
10
+ };
@@ -0,0 +1,42 @@
1
+ import { Valtype } from '../wasmSpec.js';
2
+ import { number } from '../embedding.js';
3
+ import Prefs from '../prefs.js';
4
+
5
+ const allocType = itemType => {
6
+ switch (itemType) {
7
+ case 'i8': return 'bytestring';
8
+ case 'i16': return 'string';
9
+
10
+ default: return 'array';
11
+ }
12
+ };
13
+
14
+ const ptr = ind => {
15
+ if (ind === 0) return 1;
16
+ return ind * pageSize;
17
+ };
18
+
19
+
20
+ export default class StaticAllocator {
21
+ constructor() {
22
+ }
23
+
24
+ alloc({ scope, pages }, name, { itemType }) {
25
+ const reason = `${allocType(itemType)}: ${Prefs.scopedPageNames ? (scope.name + '/') : ''}${name}`;
26
+
27
+ if (pages.has(reason)) return number(ptr(pages.get(reason).ind), Valtype.i32);
28
+
29
+ if (reason.startsWith('array:')) pages.hasArray = true;
30
+ if (reason.startsWith('string:')) pages.hasString = true;
31
+ if (reason.startsWith('bytestring:')) pages.hasByteString = true;
32
+ if (reason.includes('string:')) pages.hasAnyString = true;
33
+
34
+ let ind = pages.size;
35
+ pages.set(reason, { ind, type: itemType });
36
+
37
+ scope.pages ??= new Map();
38
+ scope.pages.set(reason, { ind, type: itemType });
39
+
40
+ return number(ptr(ind), Valtype.i32);
41
+ }
42
+ }
@@ -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(
@@ -722,12 +722,10 @@ 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
725
+ hack.length = Porffor.wasm`memory.size 0
726
+ i32.const 1
726
727
  memory.grow 0
727
728
  drop
728
- memory.size 0
729
- i32.const 1
730
- i32.sub
731
729
  i32.const 65536
732
730
  i32.mul
733
731
  i32.from_u`;
@@ -2,12 +2,10 @@ 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
5
+ Porffor.wasm`memory.size 0
6
+ i32.const 1
6
7
  memory.grow 0
7
8
  drop
8
- memory.size 0
9
- i32.const 1
10
- i32.sub
11
9
  i32.const 65536
12
10
  i32.mul
13
11
  i32.from_u