-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.macro.js
234 lines (195 loc) · 7.61 KB
/
entity.macro.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
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
const fs = require("fs")
const path = require("path")
const t = require("@babel/types")
const generate = require("@babel/generator")
const { createMacro, MacroError } = require("babel-plugin-macros")
const EntitySystem = require("./src/EntitySystem")
module.exports = createMacro(entityMacro, {
configName: "entityMacro"
})
function getKey(variable, arrayIndex)
{
return variable + "/" + arrayIndex
}
function getConfig(entitySystem, ref)
{
const entities = ref.parentPath.node.arguments[0].params.map(p => p.name)
const props = new Map()
const usedRows = new Map()
const usedArrays = new Set()
const VisitEntityPropReferences = {
MemberExpression(path)
{
const {object, property} = path.node
if (object.type === "Identifier" && entities.indexOf(object.name) >= 0)
{
if (property.type !== "Identifier")
{
throw new MacroError("Only Identifier props allowed for entities.")
}
const cfg = entitySystem.getPropConfig(property.name, MacroError)
const k = getKey(object.name, cfg.array)
props.set(
getKey(object.name,property.name),
cfg
)
if (!usedRows.has(k))
{
usedRows.set(k, {
key: k,
arrayIndex: cfg.array,
entity: object.name,
component: cfg.component,
sizeOf: cfg.sizeOf
})
}
usedArrays.add(cfg.array)
}
}
}
ref.parentPath.get("arguments.0.body").traverse(VisitEntityPropReferences)
return {props, usedRows, usedArrays, entities}
}
function entityMacro({references,config, state}) {
const entitySystemName = config.entitySystemName || "entitySystem"
const json = fs.readFileSync(
path.join( state.file.opts.root, config && config.config ? config.config : "entity-config.json"),
"utf-8"
);
const raw = JSON.parse(json)
const entitySystem = new EntitySystem(raw)
const { maskSize } = entitySystem
references.default.forEach(ref => {
const { body } = ref.parentPath.node.arguments[0]
const { props, usedRows, usedArrays, entities } = getConfig(entitySystem, ref)
const varNames = {}
for (let key of usedRows.keys())
{
const { entity, arrayIndex } = usedRows.get(key)
varNames[key] = ref.scope.generateUidIdentifier( entity + "_T" + arrayIndex + "_" ).name
}
for (let arrayIndex of usedArrays)
{
varNames[arrayIndex] = ref.scope.generateUidIdentifier("array").name
}
const ReplaceEntityPropReferences = {
"AssignmentExpression|UpdateExpression|AssignmentExpression" : function (path)
{
const assignmentTarget = path.node.left || path.node.argument
if (t.isIdentifier(assignmentTarget) && entities.indexOf(assignmentTarget.name) >= 0)
{
const rowsForEntity = Array.from(usedRows.values())
.filter(ur => ur.entity === assignmentTarget.name)
.reverse()
rowsForEntity.forEach(
r => path.insertAfter(
t.assignmentExpression(
"=",
t.identifier(varNames[r.key]),
t.memberExpression(
t.memberExpression(
t.identifier(entitySystemName),
t.identifier("e"),
false
),
t.binaryExpression(
"+",
t.binaryExpression(
"*",
t.identifier(r.entity),
t.numericLiteral(entitySystem.s.sizeOf)
),
t.numericLiteral(maskSize + r.arrayIndex),
),
true
)
)
))
}
},
MemberExpression(path)
{
const {object, property} = path.node
if (object.type === "Identifier" && entities.indexOf(object.name) >= 0)
{
if (property.type !== "Identifier")
{
throw new MacroError("Only Identifier props allowed for entities.")
}
const { array, sizeOf, offset } = props.get(getKey(object.name,property.name))
path.replaceWith(
t.memberExpression(
t.identifier(varNames[array]),
t.binaryExpression(
"+",
t.identifier(varNames[getKey(object.name, array)]),
t.numericLiteral(offset)
),
true
)
)
}
}
}
ref.parentPath.get("arguments.0.body").traverse(ReplaceEntityPropReferences)
const componentRowDecls = Array.from(usedRows.keys(), key => {
const { arrayIndex, entity, sizeOf }= usedRows.get(key)
return (
t.variableDeclarator(
t.identifier(varNames[key]),
t.memberExpression(
t.memberExpression(
t.identifier(entitySystemName),
t.identifier("e"),
false
),
t.binaryExpression(
"+",
t.binaryExpression(
"*",
t.identifier(entity),
t.numericLiteral(entitySystem.s.sizeOf)
),
t.numericLiteral(maskSize + arrayIndex),
),
true
)
)
)
})
const arrayDecls = Array.from(usedArrays, arrayIndex => {
return (
t.variableDeclarator(
t.identifier(varNames[arrayIndex]),
t.memberExpression(
t.identifier(entitySystemName),
t.identifier("c" + arrayIndex),
false
)
)
)
})
let decls = []
if (componentRowDecls.length)
{
decls = decls.concat(componentRowDecls)
}
if (arrayDecls.length)
{
decls = decls.concat(arrayDecls)
}
if (decls.length)
{
body.body.unshift(
t.variableDeclaration("let",
decls
)
)
}
ref.parentPath.replaceWithMultiple(body.body)
if (config.debug)
{
console.log("ENTITY MACRO RESULT", generate.default(body).code)
}
})
}