-
Notifications
You must be signed in to change notification settings - Fork 1
/
PopDllWebApi.js
88 lines (75 loc) · 2.32 KB
/
PopDllWebApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* gr: this was my WIP "easy-load wasm as a dll" class */
Pop.Wasm = {};
Pop.Wasm.Module = class
{
constructor()
{
this.Module = null;
this.Instance = null;
this.UsedBytes = 0;
}
GetMemory()
{
return this.Instance.exports.memory;
}
ResetHeap()
{
this.UsedBytes = 0;
}
HeapAlloc(Size)
{
const Offset = this.UsedBytes;
this.UsedBytes += Size;
// realloc if we need to
const NewBytesUsed = this.UsedBytes;
const Memory = this.GetMemory();
const MaxBytes = Memory.buffer.byteLength;
if (NewBytesUsed > MaxBytes)
{
const PageSize = 64 * 1024;
const NewPages = Math.ceil((NewBytesUsed - MaxBytes) / PageSize);
Pop.Debug(`Reallocating heap in WASM module ${NewBytesUsed} > ${MaxBytes}. New Pages x${NewPages}`);
Memory.grow(NewPages);
Pop.Debug(`New WASM module heap size ${Memory.buffer.byteLength}`);
}
return Offset;
}
HeapAllocArray(ArrayType,Length)
{
const ElementSize = ArrayType.BYTES_PER_ELEMENT;
const ByteOffset = this.HeapAlloc(Length * ElementSize);
const Memory = this.GetMemory();
return new ArrayType(Memory.buffer,ByteOffset,Length);
}
}
async function LoadWasmModule(WasmCode)
{
const PageSize = 64 * 1024;
function BytesToPages(Bytes)
{
return Math.ceil(Bytes / PageSize);
}
let WasmImports = {};
WasmImports.a = {};
WasmImports.a.a = function(){};
WasmImports.a.b = function(){};
WasmImports.a.memory = function(){};
/* gr: not sure this is having any effect, can't get constructor right?
const MaxPages = BytesToPages(64 * 1024 * 1024);
const InitialPages = MaxPages;
Pop.Debug(`Allocating ${MaxSizeBytes / 1024 / 1024}mb`);
const Memory = new WebAssembly.Memory({ initial: InitialPages,maximum: MaxPages });
Pop.Debug("WASM instance memory buffer:",Memory.buffer.byteLength);
Pop.Debug("WASM instance memory buffer maximum:",Memory.maximum);
WasmImports.env = {};
WasmImports.env.memory = Memory;
*/
const WasmCodeArrayBuffer = WasmCode.buffer;
const Wasm = new Pop.Wasm.Module();
Wasm.Module = await WebAssembly.compile(WasmCode);
//Wasm.Instance = await WebAssembly.instantiate(Wasm.Module,WasmImports);
const InstPromise = await WebAssembly.instantiate(Wasm.Module,WasmImports);
Wasm.Instance = await InstPromise;
//Pop.Debug("REAL WASM instance memory buffer:",Wasm.Instance.exports.memory.buffer.byteLength);
return Wasm;
}