-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstronger.lua
577 lines (487 loc) · 13.6 KB
/
stronger.lua
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
local ObjectFactory = require "factory.ffi"
local parser = require "parser"
local TypeFactory = require "type"
local Util = require "util"
local POINTER_SIZE = 4
local stronger = { }
local settings = {
exposed = false,
cPrefix = ""
}
local initialized = false
local systemTypes = {}
local classTypes = {}
local templateParamTypes = {}
local intialize
local function findTemplate(_type, name)
for i, v in ipairs(_type.templates) do
if v.name == name then
return v
end
end
end
local function getType(name)
local t = classTypes[name]
if t ~= nil then
return t
end
t = systemTypes[name]
if t ~= nil then
return t
end
end
local function validateParent(p)
if p == nil then
error("Parent class undefined")
end
if p.resolved == false then
error("Unable to inherit from an unresolved class type")
end
end
local function validateTemplate(t)
end
local function validateTemplateLookup(v, name)
assert(v ~= nil, "Template argument '" .. name .. "' could not be found in the lookup")
end
local function validateTemplateArg(arg)
if arg == nil then
error("Template argument undefined")
elseif type(arg) == "string" then
-- TODO: Make sure this isn't a reserved word
elseif type(arg) == "number" then
-- Do some checking here
elseif type(arg) == "table" then
if arg.primitiveType == nil then
error("Template argument must be a valid type")
end
else
error("Template parameters of type '" .. type(arg) .. "' are not supported")
end
end
local function updateTypeSize(t)
t.size = 0
for _, v in ipairs(t.members) do
assert(v.type.resolved == nil or v.type.resolved == true)
if v.type.pointer == 0 then
t.size = t.size + v.type.size
else
t.size = t.size + POINTER_SIZE
end
end
end
local function generateCTypeName(name)
return settings.cPrefix .. name:gsub("<", "_"):gsub(",", "_"):gsub(">", ""):gsub(" ", "")
end
local function cloneType(t)
local ct = Util.shallowCloneTable(t)
ct.members = Util.shallowCloneTable(t.members)
ct.templates = Util.shallowCloneTable(t.templates)
for i, v in ipairs(ct.members) do
if v.type.primitiveType == "template" then
ct.members[i] = Util.shallowCloneTable(v)
end
end
ct.origin = t.origin or t
ct.cType = ""
ct.cTypePtr = ""
return ct
end
local function bundleTemplateArgs(args, templates)
local templateArgs = {}
for i, arg in ipairs(args) do
validateTemplateArg(arg)
table.insert(templateArgs, arg)
end
for i = #args + 1, #templates do
assert(templates[i].default ~= nil)
table.insert(templateArgs, templates[i].default)
end
return templateArgs
end
-- Resolve member variables that are templated types
local function resolveTemplateMember(member, lookup)
local templateParams = {}
for i, template in ipairs(member.type.templates) do
local lookupName = template.name
if template.value ~= nil then
lookupName = template.value.name
end
assert(lookup[lookupName] ~= nil, "Template argument '" .. lookupName .. "' could not be found in the lookup")
table.insert(templateParams, lookup[lookupName])
end
local memberType = member.type
if memberType.origin ~= nil then
memberType = memberType.origin
end
return { name = member.name, type = memberType(unpack(templateParams)) }
end
local function generateTemplateClassName(name, templates, lookup)
name = name .. "<"
for i, v in ipairs(templates) do
local item = lookup[v.name]
local tname = item
if type(item) == "table" then
tname = item.name
end
name = name .. (i > 1 and ", " or "") .. tname
end
return name .. ">"
end
local function setTemplateValue(template, value)
-- This function can probably be tidied up a bit
local resolved = true
if type(value) == "string" then
value = TypeFactory.TemplateType({ name = value })
template = TypeFactory.TemplateType(template, { value = value })
resolved = false
elseif type(value) == "table" then
assert(value.primitiveType ~= "pointer")
if value.primitiveType == "template" then
template = TypeFactory.TemplateType(template, { value = value })
resolved = false
else
-- Check if this codepath is called
template = TypeFactory.TemplateType(template, { value = value })
end
else
-- Check if this codepath is called
template = TypeFactory.TemplateType(template, { value = value })
end
return template, resolved
end
local function resolveMembers(_type, lookup)
for i, v in ipairs(_type.members) do
if v.type.primitiveType == "template" then
local temp = lookup[v.type.name]
validateTemplateLookup(temp, v.type.name)
_type.members[i] = { name = v.name, type = temp }
elseif v.type.primitiveType == "pointer" and v.type.resolved == false then
local temp = lookup[v.type.origin.name]
validateTemplateLookup(temp, v.type.origin.name)
local pt = TypeFactory.PointerType(temp, v.type.indirection)
_type.members[i] = { name = v.name, type = pt }
elseif v.type.primitiveType == "class" and v.type.resolved == false then
_type.members[i] = resolveTemplateMember(v, lookup)
end
end
end
local function resolveTemplateArgs(t, ...)
if t.resolved == false then
local args = {...}
if #args >= (#t.templates - t.templateDefaults) and #args <= #t.templates then
local templateArgs = bundleTemplateArgs(args, t.templates)
local ct = cloneType(t)
ct.resolved = true
local lookup = {}
for i, arg in ipairs(templateArgs) do
local template, resolved = setTemplateValue(ct.templates[i], arg)
ct.templates[i] = template
lookup[template.name] = template.value
if resolved == false then
ct.resolved = false
end
end
resolveMembers(ct, lookup)
if ct.resolved == true then
ct.name = generateTemplateClassName(ct.name, ct.templates, lookup)
ct.cType = generateCTypeName(ct.name)
ct.cTypePtr = ct.cType .. "*"
updateTypeSize(ct)
else
ct.cType = nil
end
return ct
else
error("Wrong number of template arguments supplied to class '" .. t.name .. "' (" .. #t.templates .. " expected, " .. #args .. " supplied)")
end
else
error("Type '" .. t.name .. "' does not support template arguments")
end
end
local function applyPointerMetatable(_type)
setmetatable(_type, {
__index = {
new = function(self, ...)
if self.origin.resolved == true then
return ObjectFactory.create(self.origin, ...)
end
error("Unable to instantiate class type '" .. self.name .. "' as it is unresolved");
end,
newArray = function(self, size)
return ObjectFactory.createArray(_type, size)
end,
ptr = function(self, level)
level = (level or 1) + _type.indirection
local pointerType = TypeFactory.PointerType(_type.origin, level)
applyPointerMetatable(pointerType)
return pointerType
end
}
})
end
local function applySystemMetatable(_type)
setmetatable(_type, {
__call = function(t, default)
local ct = Util.shallowCloneTable(t)
ct.default = default
return ct
end,
__index = {
newArray = function(t, size)
return ObjectFactory.createArray(_type, size)
end,
ptr = function(self, level)
local pointerType = TypeFactory.PointerType(_type, level)
applyPointerMetatable(pointerType)
return pointerType
end
}
})
end
local function applyClassMetatable(_type)
setmetatable(_type, {
__call = function(t, ...)
local ct = resolveTemplateArgs(t, ...)
applyClassMetatable(ct)
return ct
end,
__index = {
commit = function(self)
if self.resolved == true then
return ObjectFactory.commit(self)
end
error("Unable to commit class type '" .. self.name .. "' as it is unresolved");
end,
alloc = function(self)
if self.resolved == true then
return ObjectFactory.alloc(self)
end
error("Unable to instantiate class type '" .. self.name .. "' as it is unresolved");
end,
new = function(self, ...)
if self.resolved == true then
return ObjectFactory.create(self, ...)
end
error("Unable to instantiate class type '" .. self.name .. "' as it is unresolved");
end,
newArray = function(self, size)
if self.resolved == true then
return ObjectFactory.createArray(self, size)
end
error("Unable to instantiate array of class type '" .. self.name .. "' as it is unresolved");
end,
findMember = function(self, name)
for i,v in ipairs(self.members) do
if v.name == name then
return v
end
end
end,
findTemplate = function(self, name)
for i,v in ipairs(self.templates) do
if v.name == name then
return v
end
end
end,
ptr = function(self, level)
local pointerType = TypeFactory.PointerType(_type, level)
applyPointerMetatable(pointerType)
return pointerType
end
},
__newindex = function(t, k, v)
_type.methods[k] = v
if #k > 4 then
local s = k:sub(1, 4)
local n = k:sub(5)
local prop = _type.properties[n]
if s == "get_" then
prop = prop or {}
prop.getter = v
elseif s == "set_" then
prop = prop or {}
prop.setter = v
end
_type.properties[n] = prop
end
end
})
end
local function addSystemType(name, size, cType)
local _type = TypeFactory.SystemType(name, size, cType)
applySystemMetatable(_type)
systemTypes[name] = _type
stronger[name] = _type
if settings.exposed == true then
_G[name] = _type
end
ObjectFactory.registerSystemType(_type)
end
local function class(name, super)
if initialized == false then
initialize()
end
local parsed = parser.parse(name)
if systemTypes[parsed.name] ~= nil then
error("The class name '" .. parsed.name .. "' is invalid as it is a reserved system type name")
end
if classTypes[parsed.name] ~= nil then
error("The class name '" .. parsed.name .. "' is invalid as it is already in use")
end
if super ~= nil then
if type(super) == "string" then
local parsedSuper = parser.parse(super)
assert(systemTypes[parsedSuper.name] == nil)
super = classTypes[parsedSuper.name]
if #super.templates > 0 then
end
end
validateParent(super)
end
local _type = TypeFactory.ClassType({
name = parsed.name,
cType = generateCTypeName(parsed.name),
super = super or stronger["object"],
templates = parsed.templates
})
applyClassMetatable(_type)
classTypes[parsed.name] = _type
stronger[parsed.name] = _type
if settings.exposed == true then
_G[parsed.name] = _type
end
local modifier
modifier = setmetatable({}, {
__call = function(t, members)
--TODO: Validate members!
for k, v in pairs(members) do
if type(v) == "string" then
local parsed = parser.parse(v)
local t = getType(parsed.name)
if t == nil then
t = findTemplate(_type, parsed.name)
if t == nil then
error("The type or template argument '" .. parsed.name .. "' does not exist")
end
end
if parsed.pointer ~= nil then
t = TypeFactory.PointerType(t, parsed.pointer)
end
table.insert(_type.members, { name = k, type = t })
elseif type(v) == "table" then
table.insert(_type.members, { name = k, type = v })
end
end
if _type.resolved == true then
updateTypeSize(_type)
else
_type.cType = nil
end
return _type
end,
__index = {
inherits = function(parent)
validateParent(parent)
_type.parent = parent
return modifier
end,
templates = function(...)
for i,v in ipairs({...}) do
validateTemplate(v, _type.templateDefaults > 0)
local template
if type(v) == "string" then
template = TypeFactory.TemplateType({ name = v })
else
template = TypeFactory.TemplateType(v)
if template.default ~= nil then
_type.templateDefaults = _type.templateDefaults + 1
end
end
table.insert(_type.templates, template)
end
_type.resolved = false
return modifier
end
}
})
return modifier
end
initialize = function(_settings)
if _settings == nil then
_settings = settings
end
if _settings.exposed == true then
_G["class"] = class
settings.exposed = true
end
if _settings.cPrefix ~= nil then
settings.cPrefix = _settings.cPrefix
end
addSystemType("char", 1)
addSystemType("bool", 4)
addSystemType("float", 4)
addSystemType("double", 4)
addSystemType("int8", 1, "int8_t")
addSystemType("uint8", 1, "uint8_t")
addSystemType("int16", 2, "int16_t")
addSystemType("uint16", 2, "uint16_t")
addSystemType("int32", 4, "int32_t")
addSystemType("uint32", 4, "uint32_t")
addSystemType("int64", 8, "int64_t")
addSystemType("uint64", 8, "uint64_t")
addSystemType("intptr", 4, "intptr_t")
addSystemType("uintptr", 4, "uintptr_t")
addSystemType("f32", 4, "float")
addSystemType("f64", 8, "double")
initialized = true
end
local function p(_type, level)
local t = _type
if type(_type) == "string" then
local parsed = parser.parse(_type)
t = getType(parsed.name)
if t == nil then
t = parsed.name
end
if parsed.pointer ~= nil then
level = (level or 1) + parsed.pointer
end
end
local pointerType = TypeFactory.PointerType(t, level)
applyPointerMetatable(pointerType)
return pointerType
end
local function typeOf(_type)
if type(_type) == "string" then
local t = systemTypes[_type]
if t ~= nil then return t end
t = classTypes[_type]
if t ~= nil then return t end
elseif type(_type) == "table" then
if _type.primitiveType ~= nil then
return _type
end
elseif type(_type) == "cdata" then
if _type.__type ~= nil then
return _type.__type()
end
end
end
local function typeDef(from, to)
end
local function parseClass(name, fields)
end
local function setAllocator(_type, allocator)
_type = typeOf(_type)
ObjectFactory.setAllocator(_type, allocator)
end
stronger.setup = initialize
stronger.class = class
stronger.typeOf = typeOf
stronger.templateOf = templateOf
stronger.p = p
stronger.typeDef = typeDef
stronger.parseClass = parseClass
stronger.setAllocator = setAllocator
return stronger