-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasmState.js
110 lines (87 loc) · 2.48 KB
/
WasmState.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
"use strict";
// WasmState
// experimental code to store and load WASM state
if (!getGlobalThis().assert) {
getGlobalThis().assert = function (v) {
if (!v) {
throw new Exception("failed assertation")
}
};
};
(class WasmState extends Base {
initPrototype () {
this.newSlot("wasmModule", null)
this.newSlot("memory", null)
this.newSlot("db", null)
this.newSlot("stateKey", "1")
this.newSlot("delegate", null)
}
init () {
super.init()
this.setWasmModule(WasmLoader.shared().module())
this.setDb(PersistentAsyncMap.clone().setName("WasmStore"))
this.openStore()
}
openStore () {
this.db().asyncOpen(() => {
this.onOpenStoreSuccess()
}, (error) => {
this.onOpenStoreError(error)
})
}
onOpenStoreSuccess () {
console.log(this.type() + ".onOpenStoreSuccess()")
}
onOpenStoreError (error) {
console.log(this.type() + ".onOpenStoreError(" + error + ")")
}
isOpen () {
const db = this.db()
return db && db.isOpen()
}
// store
store () {
assert(this.isOpen())
const wasmModule = this.wasmModule()
const memory = new Uint8Array(wasmModule.HEAP8);
this.setMemory(memory)
this.storeMemory()
return this
}
storeMemory () {
this.db().asyncAtPut(this.stateKey(), this.memory(), () => { this.onStoreMemory() }, (error) => { this.onStoreMemoryError(error) })
}
onStoreMemory () {
console.log(this.type() + ".onStoreMemory()")
}
onStoreMemoryError (error) {
console.log(this.type() + ".onStoreMemoryError(" + error + ")")
}
// load
load () {
assert(this.isOpen())
this.db().asyncAt(this.stateKey(), (data) => { this.onLoadMemory(data) }, (error) => { this.onLoadMemoryError(error) })
}
onLoadMemory (data) {
//console.log(this.type() + ".onLoadMemory ", data)
const wasmModule = this.wasmModule()
//assert(data.length === wasmModule.HEAP8.length)
//wasmModule.HEAP8.length = data.length
wasmModule.HEAP8.set(data)
}
onLoadMemoryError(error) {
console.log(this.type() + " onLoadMemoryError(" + error + ")")
}
}.initThisClass());
WasmState.shared() // start opening db
/*
compile () {
assert(this.wasmModule() === null)
assert(this.memory() !== null)
// wasmModule should be null, and memory should be sett
// First, create a WebAssembly module using the WebAssembly.compile function
WebAssembly.compile(wasmCode).then((wasmModule) => {
this.afterCompile(wasmModule)
});
}
*/