-
Notifications
You must be signed in to change notification settings - Fork 5
/
mem.js
140 lines (115 loc) · 4.25 KB
/
mem.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class GameAllocator {
constructor(totalMemoryBytes, heapSize) {
this.WebAssemblyMemory = null;
this.memory = null // Alias for WebAssemblyMemory
this.AllocatorPtr = null;
this.WasmExports = null;
this.RequestedBytes = totalMemoryBytes;
this.HeapSizeBytes = 0;
this.RequestedHeapSize = heapSize;
this.GlobalDumpState = "";
let self = this;
// WASM is 64 KiB / page (our allocator is 4 KiB);
let wasmPageSize = 64 * 1024; // 64 KiB
let wasmNumPages = Math.ceil(totalMemoryBytes / wasmPageSize);
self.WebAssemblyMemory = new WebAssembly.Memory( {
initial: wasmNumPages,
maximum: wasmNumPages
});
self.memory = this.WebAssemblyMemory;
}
logError(str) {
console.logError(str);
}
logInfo(str) {
console.log(str);
}
InjectWebAssemblyImportObject(importObject) {
if (!importObject.hasOwnProperty("env")) {
importObject.env = {};
}
importObject.env.memory = this.WebAssemblyMemory;
let self = this;
importObject.env["GameAllocator_jsBuildMemState"] = function(ptr, len) {
const array = new Uint8Array(self.WebAssemblyMemory.buffer, ptr, len);
const decoder = new TextDecoder()
const string = decoder.decode(array);
self.GlobalDumpState += string;
};
}
InitializeWebAssembly(wasmExports) {
if (!wasmExports) {
this.logError("invalid exports object");
}
this.WasmExports = wasmExports;
this.AllocatorPtr = this.WasmExports.GameAllocator_wasmInitialize(this.RequestedHeapSize);
this.HeapSizeBytes = this.WasmExports.GameAllocator_wasmHeapSize(this.RequestedBytes);
this.logInfo("Requested heap: " + this.RequestedHeapSize + ", actual heap: " + this.HeapSizeBytes);
if (this.HeapSizeBytes < this.RequestedHeapSize) {
console.logError("Actual heap size is less than requested heap size");
}
}
ShutdownWebAssembly() {
this.WasmExports.GameAllocator_wasmShutdown(this.AllocatorPtr);
this.AllocatorPtr = 0;
}
Allocte(bytes, alignment) {
if (!alignment) {
alignment = 0;
}
if (!bytes || bytes <= 0) {
this.logError("Can't allocate <=0 bytes!");
bytes = 1;
}
return this.WasmExports.GameAllocator_wasmAllocate(this.AllocatorPtr, bytes, alignment);
}
Release(ptr) {
this.WasmExports.GameAllocator_wasmRelease(this.AllocatorPtr, ptr);
}
Set(ptr, value, size) {
this.WasmExports.GameAllocator_wasmSet(ptr, value, size);
}
Copy(dest_ptr, src_ptr, size) {
this.WasmExports.GameAllocator_wasmCopy(dest_ptr, src_ptr, size);
}
GetNumPages() {
return this.WasmExports.GameAllocator_wasmGetNumPages(this.AllocatorPtr);
}
GetNumPagesInUse() {
return this.WasmExports.GameAllocator_wasmGetNumPagesInUse(this.AllocatorPtr);
}
GetPeekPagesUsed() {
return this.WasmExports.GameAllocator_wasmGetPeekPagesUsed(this.AllocatorPtr);
}
GetRequestedBytes() {
return this.WasmExports.GameAllocator_wasmGetRequestedBytes(this.AllocatorPtr);
}
GetServedBytes() {
return this.WasmExports.GameAllocator_wasmGetServedBytes(this.AllocatorPtr);
}
IsPageInUse(page) {
if (page < 0) {
this.logError("invalid page");
page = 0;
}
let result = this.WasmExports.GameAllocator_wasmIsPageInUse(this.AllocatorPtr, page);
return result != 0;
}
Size() {
return this.WasmExports.GameAllocator_wasmGetSize(this.AllocatorPtr);
}
GetNumOverheadPages() {
return this.WasmExports.GameAllocator_wasmGetNumOverheadPages(this.AllocatorPtr);
}
StrLen(str_ptr) {
return this.WasmExports.GameAllocator_wasmStrLen(str_ptr);
}
GetAllocationDebugInfo(alloc_ptr) {
return this.WasmExports.GameAllocator_wasmGetAllocationDebugName(this.AllocatorPtr, alloc_ptr);
}
DebugDumpState() {
this.GlobalDumpState = "";
this.WasmExports.GameAllocator_wasmDumpState(this.AllocatorPtr);
return this.GlobalDumpState;
}
}