-
Notifications
You must be signed in to change notification settings - Fork 4
/
ctypeEnumImpl.ts
212 lines (176 loc) · 7.38 KB
/
ctypeEnumImpl.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import isLittleEndian from 'common/function/isLittleEndian'
import { CTypeEnum } from './typedef'
import { override as readoverride} from './ctypeEnumRead'
import { override as writeoverride} from './ctypeEnumWrite'
import AllocatorInterface from './allocator/Allocator'
let getAllocator: () => AllocatorInterface
let getView: () => DataView
const littleEndian = isLittleEndian()
function writeU8(pointer: pointer<void>, value: uint8) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setUint8(pointer, value)
}
function readU8(pointer: pointer<void>): uint8 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getUint8(pointer)
}
function writeU16(pointer: pointer<void>, value: uint16) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setUint16(pointer, value, littleEndian)
}
function readU16(pointer: pointer<void>): uint16 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getUint16(pointer, littleEndian)
}
function writeU32(pointer: pointer<void>, value: uint32) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setUint32(pointer, value, littleEndian)
}
function readU32(pointer: pointer<void>): uint32 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getUint32(pointer, littleEndian)
}
function writeU64(pointer: pointer<void>, value: uint64) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setBigUint64(pointer, value, littleEndian)
}
function readU64(pointer: pointer<void>): uint64 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getBigUint64(pointer, littleEndian)
}
function write8(pointer: pointer<void>, value: int8) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setInt8(pointer, value)
}
function read8(pointer: pointer<void>): int8 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getInt8(pointer)
}
function write16(pointer: pointer<void>, value: int16) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setInt16(pointer, value, littleEndian)
}
function read16(pointer: pointer<void>): int16 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getInt16(pointer, littleEndian)
}
function write32(pointer: pointer<void>, value: int32) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setInt32(pointer, value, littleEndian)
}
function read32(pointer: pointer<void>): int32 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getInt32(pointer, littleEndian)
}
function write64(pointer: pointer<void>, value: int64) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setBigInt64(pointer, value, littleEndian)
}
function read64(pointer: pointer<void>): int64 {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getBigInt64(pointer, littleEndian)
}
function writef32(pointer: pointer<void>, value: float) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setFloat32(pointer, value, littleEndian)
}
function readf32(pointer: pointer<void>): float {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getFloat32(pointer, littleEndian)
}
function writef64(pointer: pointer<void>, value: float64) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
getView().setFloat64(pointer, value, littleEndian)
}
function readf64(pointer: pointer<void>): float64 | double {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getFloat64(pointer, littleEndian)
}
function readPointer<T>(pointer: pointer<void>): pointer<T> {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().getUint32(pointer, littleEndian) as pointer<T>
}
function writePointer<T>(pointer: pointer<void>, value: pointer<T>) {
assert(pointer, 'Out Of Bounds, address: 0')
assert(getAllocator().isAlloc(pointer), `address ${pointer} is not alloc`)
return getView().setUint32(pointer, value, littleEndian)
}
export default function init(getAllocator_: () => AllocatorInterface, getView_: () => DataView) {
getAllocator = getAllocator_
getView = getView_
readoverride({
[CTypeEnum.char]: readU8 as any,
[CTypeEnum.atomic_char]: readU8 as any,
[CTypeEnum.uint8]: readU8,
[CTypeEnum.atomic_uint8]: readU8 as any,
[CTypeEnum.uint16]: readU16,
[CTypeEnum.atomic_uint16]: readU16 as any,
[CTypeEnum.uint32]: readU32,
[CTypeEnum.atomic_uint32]: readU32 as any,
[CTypeEnum.uint64]: readU64,
[CTypeEnum.int8]: read8,
[CTypeEnum.atomic_int8]: read8 as any,
[CTypeEnum.int16]: read16,
[CTypeEnum.atomic_int16]: read16 as any,
[CTypeEnum.int32]: read32,
[CTypeEnum.atomic_int32]: read32 as any,
[CTypeEnum.int64]: read64,
[CTypeEnum.float]: readf32,
[CTypeEnum.double]: readf64,
[CTypeEnum.pointer]: readPointer,
[CTypeEnum.bool]: (pointer: pointer<void>) => {
return !!read8(pointer)
},
[CTypeEnum.atomic_bool]: (pointer: pointer<void>) => {
return !!read8(pointer)
}
})
writeoverride({
[CTypeEnum.char]: writeU8 as any,
[CTypeEnum.atomic_char]: writeU8,
[CTypeEnum.uint8]: writeU8,
[CTypeEnum.atomic_uint8]: writeU8,
[CTypeEnum.uint16]: writeU16,
[CTypeEnum.atomic_uint16]: writeU16 as any,
[CTypeEnum.uint32]: writeU32,
[CTypeEnum.atomic_uint32]: writeU32 as any,
[CTypeEnum.uint64]: writeU64,
[CTypeEnum.int8]: write8,
[CTypeEnum.atomic_int8]: write8 as any,
[CTypeEnum.int16]: write16,
[CTypeEnum.atomic_int16]: write16 as any,
[CTypeEnum.int32]: write32,
[CTypeEnum.atomic_int32]: write32 as any,
[CTypeEnum.int64]: write64,
[CTypeEnum.float]: writef32,
[CTypeEnum.double]: writef64,
[CTypeEnum.pointer]: writePointer,
[CTypeEnum.bool]: (pointer: pointer<void>, value: bool) => {
write8(pointer, value ? 1 : 0)
},
[CTypeEnum.atomic_bool]: ((pointer: pointer<void>, value: bool) => {
write8(pointer, value ? 1 : 0)
}) as any
})
}