-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathglobal.ts
executable file
·170 lines (146 loc) · 5.77 KB
/
global.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { LuaLibraries, LuaType } from './types'
import LuaTypeExtension from './type-extension'
import Thread from './thread'
import type LuaWasm from './luawasm'
interface LuaMemoryStats {
memoryUsed: number
memoryMax?: number
}
export default class Global extends Thread {
private memoryStats: LuaMemoryStats | undefined
private allocatorFunctionPointer: number | undefined
public constructor(cmodule: LuaWasm, shouldTraceAllocations: boolean) {
if (shouldTraceAllocations) {
const memoryStats: LuaMemoryStats = { memoryUsed: 0 }
const allocatorFunctionPointer = cmodule.module.addFunction(
(_userData: number, pointer: number, oldSize: number, newSize: number): number => {
if (newSize === 0) {
if (pointer) {
memoryStats.memoryUsed -= oldSize
cmodule.module._free(pointer)
}
return 0
}
const endMemoryDelta = pointer ? newSize - oldSize : newSize
const endMemory = memoryStats.memoryUsed + endMemoryDelta
if (newSize > oldSize && memoryStats.memoryMax && endMemory > memoryStats.memoryMax) {
return 0
}
const reallocated = cmodule.module._realloc(pointer, newSize)
if (reallocated) {
memoryStats.memoryUsed = endMemory
}
return reallocated
},
'iiiii',
)
super(cmodule, [], cmodule.lua_newstate(allocatorFunctionPointer, null))
this.memoryStats = memoryStats
this.allocatorFunctionPointer = allocatorFunctionPointer
} else {
super(cmodule, [], cmodule.luaL_newstate())
}
if (this.isClosed()) {
throw new Error('Global state could not be created (probably due to lack of memory)')
}
}
public close(): void {
if (this.isClosed()) {
return
}
super.close()
// Do this before removing the gc to force.
// Here rather than in the threads because you don't
// actually close threads, just pop them. Only the top-level
// lua state needs closing.
this.lua.lua_close(this.address)
if (this.allocatorFunctionPointer) {
this.lua.module.removeFunction(this.allocatorFunctionPointer)
}
for (const wrapper of this.typeExtensions) {
wrapper.extension.close()
}
}
// To allow library users to specify custom types
// Higher is more important and will be evaluated first.
public registerTypeExtension(priority: number, extension: LuaTypeExtension<unknown>): void {
this.typeExtensions.push({ extension, priority })
this.typeExtensions.sort((a, b) => b.priority - a.priority)
}
public loadLibrary(library: LuaLibraries): void {
switch (library) {
case LuaLibraries.Base:
this.lua.luaopen_base(this.address)
break
case LuaLibraries.Coroutine:
this.lua.luaopen_coroutine(this.address)
break
case LuaLibraries.Table:
this.lua.luaopen_table(this.address)
break
case LuaLibraries.IO:
this.lua.luaopen_io(this.address)
break
case LuaLibraries.OS:
this.lua.luaopen_os(this.address)
break
case LuaLibraries.String:
this.lua.luaopen_string(this.address)
break
case LuaLibraries.UTF8:
this.lua.luaopen_string(this.address)
break
case LuaLibraries.Math:
this.lua.luaopen_math(this.address)
break
case LuaLibraries.Debug:
this.lua.luaopen_debug(this.address)
break
case LuaLibraries.Package:
this.lua.luaopen_package(this.address)
break
}
this.lua.lua_setglobal(this.address, library)
}
public get(name: string): any {
const type = this.lua.lua_getglobal(this.address, name)
const value = this.getValue(-1, type)
this.pop()
return value
}
public set(name: string, value: unknown): void {
this.pushValue(value)
this.lua.lua_setglobal(this.address, name)
}
public getTable(name: string, callback: (index: number) => void): void {
const startStackTop = this.getTop()
const type = this.lua.lua_getglobal(this.address, name)
try {
if (type !== LuaType.Table) {
throw new TypeError(`Unexpected type in ${name}. Expected ${LuaType[LuaType.Table]}. Got ${LuaType[type]}.`)
}
callback(startStackTop + 1)
} finally {
// +1 for the table
if (this.getTop() !== startStackTop + 1) {
console.warn(`getTable: expected stack size ${startStackTop} got ${this.getTop()}`)
}
this.setTop(startStackTop)
}
}
public getMemoryUsed(): number {
return this.getMemoryStatsRef().memoryUsed
}
public getMemoryMax(): number | undefined {
return this.getMemoryStatsRef().memoryMax
}
public setMemoryMax(max: number | undefined): void {
this.getMemoryStatsRef().memoryMax = max
}
private getMemoryStatsRef(): LuaMemoryStats {
if (!this.memoryStats) {
throw new Error('Memory allocations is not being traced, please build engine with { traceAllocations: true }')
}
return this.memoryStats
}
}