porffor 0.58.19 → 0.59.1

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.
@@ -5,8 +5,11 @@ import { TYPES, TYPE_NAMES } from './types.js';
5
5
  import { number, unsignedLEB128 } from './encoding.js';
6
6
  import './prefs.js';
7
7
 
8
- export const importedFuncs = {};
9
- Object.defineProperty(importedFuncs, 'length', { configurable: true, writable: true, value: 0 });
8
+ export let importedFuncs;
9
+ export const setImports = (v = null) => {
10
+ importedFuncs = v ?? { length: 0 };
11
+ };
12
+ setImports();
10
13
 
11
14
  /**
12
15
  * Create an import function for the Porffor world to use.
@@ -18,51 +21,41 @@ Object.defineProperty(importedFuncs, 'length', { configurable: true, writable: t
18
21
  * @param {string} c - C source code to compile as import implementation
19
22
  */
20
23
  export const createImport = (name, params, returns, js = null, c = null) => {
21
- const lazy = () => {
22
- if (typeof params === 'function') params = params();
23
- if (typeof returns === 'function') returns = returns();
24
- if (typeof params === 'number') params = new Array(params).fill(valtypeBinary);
25
- if (typeof returns === 'number') returns = new Array(returns).fill(valtypeBinary);
26
- };
24
+ if (!globalThis.valtypeBinary) {
25
+ globalThis.valtype ??= Prefs.valtype ?? 'f64';
26
+ globalThis.valtypeBinary = Valtype[valtype];
27
+ }
28
+
29
+ if (typeof params === 'number') params = new Array(params).fill(valtypeBinary);
30
+ if (typeof returns === 'number') returns = new Array(returns).fill(valtypeBinary);
27
31
 
28
32
  if (name in importedFuncs) {
29
33
  // overwrite existing import
30
34
  const existing = importedFuncs[name];
31
- lazy();
32
-
33
- existing.params = params;
34
- existing.returns = returns;
35
- existing.js = js;
36
- existing.c = c;
35
+ const call = +existing;
36
+ const replacement = new Number(call);
37
+ replacement.name = name;
38
+ replacement.import = existing.import;
39
+ replacement.params = params;
40
+ replacement.returns = returns;
41
+ replacement.js = js;
42
+ replacement.c = c;
43
+
44
+ importedFuncs[name] = replacement;
37
45
  return;
38
46
  }
39
47
 
40
48
  const call = importedFuncs.length;
41
49
  const ident = String.fromCharCode(97 + importedFuncs.length);
42
- let obj;
43
- const get = () => {
44
- if (obj) return obj;
45
- lazy();
46
-
47
- obj = new Number(call);
48
- obj.name = name;
49
- obj.import = ident;
50
- obj.params = params;
51
- obj.returns = returns;
52
- obj.js = js;
53
- obj.c = c;
54
- return obj;
55
- };
56
50
 
57
- Object.defineProperty(importedFuncs, name, {
58
- get,
59
- configurable: true,
60
- enumerable: true
61
- });
62
- Object.defineProperty(importedFuncs, call, {
63
- get,
64
- configurable: true
65
- });
51
+ const obj = importedFuncs[name] = importedFuncs[call] = new Number(call);
52
+ obj.name = name;
53
+ obj.import = ident;
54
+ obj.params = params;
55
+ obj.returns = returns;
56
+ obj.js = js;
57
+ obj.c = c;
58
+
66
59
  importedFuncs.length = call + 1;
67
60
  };
68
61