/
runtime.mjs
284 lines (230 loc) · 7.19 KB
/
runtime.mjs
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
export const __init__$ = Symbol.for('#__init__')
export const __initor__$ = Symbol.for('#__initor__')
export const __inited__$ = Symbol.for('#__inited__')
export const __hooks__$ = Symbol.for('#__hooks__')
export const __patch__$ = Symbol.for('#__patch__')
export const __has__$ = Symbol.for('#has')
export const __meta__$ = Symbol.for('#meta')
export const __imba__$ = Symbol.for('imba')
export const __mixin__$ = Symbol.for('#__mixin__')
export const matcher = Symbol.for('#matcher')
const L = Symbol.for('#L');
export const appendChild$ = Symbol.for('#appendChild')
export const afterVisit$ = Symbol.for('#afterVisit')
export const beforeReconcile$ = Symbol.for('#beforeReconcile')
export const afterReconcile$ = Symbol.for('#afterReconcile')
export const up$ = Symbol.for('##up')
export const HAS = {
SUPERCALLS: 1 << 3,
CONSTRUCTOR: 1 << 4
}
export const ClassFlags = {
IsExtension: 1 << 0,
IsTag: 1 << 1,
HasDescriptors: 1 << 2,
HasSuperCalls: 1 << 3,
HasConstructor: 1 << 4,
HasFields: 1 << 5,
HasMixins: 1 << 6,
HasInitor: 1 << 7,
HasDecorators: 1 << 8,
IsObjectExtension: 1 << 9
}
const mmap = new Map;
const state = globalThis[__imba__$] ||= {
counter: 0,
classes: mmap
};
function meta$(klass,defaults = {}){
mmap.has(klass) || mmap.set(klass,{
symbol: Symbol(),
parent: Object.getPrototypeOf(klass.prototype)?.constructor,
for:klass,
uses:[],
inits:[],
id: state.counter++,
...defaults
});
return mmap.get(klass)
}
const statics = new WeakMap;
export function is$(a,b){
return a === b || b?.[matcher]?.(a);
};
export function isa$(a,b){
return (typeof b === 'string') ? (typeof a === b) : b?.[Symbol.hasInstance]?.(a);
};
export function has$(a,b){
return ((b?.[__has__$]?.(a) ?? b?.includes?.(a) ?? b?.has?.(a)) ?? false);
}
export function idx$(a,b){
return b?.indexOf ? b.indexOf(a) : Array.prototype.indexOf.call(a,b);
}
export function devlog$(args,self,...rest){
if(self && self[L] instanceof Function)
args = self[L](args,self,...rest)
return args;
}
export function statics$(scope){
return statics.get(scope) || statics.set(scope,{}).get(scope);
}
// $1 extends {$accessor: (...args: any[]) => infer X} ? X : $1
export function iterable$(a){
return a?.toIterable?.() || a;
}
export function decorate$(ds,t,k,desc){
let d,c = arguments.length,i = ds.length;
let r = (c < 3) ? t : (((desc === null) ? (desc = Object.getOwnPropertyDescriptor(t,k)) : desc));
while (i > 0){
if (d = ds[--i]) { r = (c < 3 ? d(r) : (c > 3 ? d(t,k,r) : d(t,k))) || r };
};
c > 3 && r && Object.defineProperty(t,k,r);
return r;
}
function isSameDesc(a,b){
if(!a || !b) return false;
if(a.get) return b.get === a.get;
if(a.set) return b.set === a.set;
if(a.value) return a.value === b.value;
}
export function extend$(target,ext,descs){
const klass = target.constructor;
descs ??= Object.getOwnPropertyDescriptors(ext);
const originals = Object.getOwnPropertyDescriptors(target);
delete descs.constructor;
if (descs[__init__$]) {
// console.warn(`Cannot define plain fields when extending class ${klass.name}`);
delete descs[__init__$];
};
Object.defineProperties(target,descs);
let meta = meta$(klass);
if(meta && meta.augments){
for(let target of meta.augments){
let current = Object.getOwnPropertyDescriptors(target.prototype);
let defines = {}
for(let key of Object.keys(descs)){
let prop = descs[key];
if(current[key] && !isSameDesc(originals[key],current[key])) console.warn('wont extend',key);
else defines[key] = prop;
}
if(Object.keys(defines).length) extend$(target.prototype,null,defines)
}
}
return target;
}
export function augment$(klass,mixin){
let meta = meta$(klass);
let mix = meta$(mixin);
if(mix.parent){
if(!(klass.prototype instanceof mix.parent)){
// For better error reports we could delay this message...
throw new Error(`Mixin ${mixin.name} has superclass not present in target class`);
}
}
if(!mix.augments){
mix.augments = new Set;
const ref = mix.ref = Symbol();
const native = Object[Symbol.hasInstance];
mixin.prototype[ref] = true;
Object.defineProperty(mixin,Symbol.hasInstance,{
value: function(rel) { return (this === mixin) ? (rel && !!rel[ref]) : native.call(this,rel) }
})
}
// klass already contains mixin somewhere in the chain
if(klass.prototype[mix.ref]){
return klass;
}
for(let v of mix.uses) augment$(klass,v,meta);
mix.augments.add(klass);
meta.uses.push(mixin);
let descs = Object.getOwnPropertyDescriptors(mixin.prototype);
delete descs.constructor
if(descs[__init__$]){
meta.inits.push(mixin.prototype[__init__$]);
delete descs[__init__$];
}
Object.defineProperties(klass.prototype,descs);
// TODO Should also run a method / trigger a hook
try { meta.top.version++; } catch(e) { }
if(mixin?.mixed instanceof Function) mixin.mixed(klass)
return klass;
};
export function multi$(symbol,sup,...mixins){
let Mixins = sup ? (class extends sup {}) : (class {});
let meta = meta$(Mixins,{symbol});
for(let mixin of mixins) augment$(Mixins,mixin);
Mixins.prototype[symbol] = function(o,deep,fields) {
if(meta.inits)
for(let init of meta.inits){ init.call(this,o,false,fields) }
return
};
return Mixins
};
let sup = {
cache: {},
self: null,
target: null,
proxy: new Proxy({},{
apply: (_, key, ...params) => {
return sup.target[key].apply(sup.self, params)
},
get: (_, key) => {
return Reflect.get(sup.target, key, sup.self);
},
set: (_, key, value, receiver) => {
return Reflect.set(sup.target, key, value, sup.self);
}
})
}
export function sup$(self,symbol) {
sup.self = self;
sup.target = sup.cache[symbol];
return sup.proxy;
}
export function register$(klass,symbol,name,flags,into = null) {
// Look for the actual superclass excluding mixins
let proto = Object.getPrototypeOf(klass.prototype)
let mixed = flags & ClassFlags.HasMixins
if(mixed) {
mmap.set(klass,mmap.get(proto.constructor))
proto = Object.getPrototypeOf(proto)
}
if(into){
let target = flags & ClassFlags.IsObjectExtension ? into : into.prototype;
let meta = meta$(klass);
if(meta.uses?.length){
if(into === target) console.warn("Cannot extend object with mixins");
for(let mixin of meta.uses) augment$(into,mixin);
}
// Create fake super now
if(flags & ClassFlags.HasSuperCalls){
sup.cache[symbol] = Object.create(
Object.getPrototypeOf(target),
Object.getOwnPropertyDescriptors(target)
)
}
extend$(target,klass.prototype);
return into;
}
let supr = proto?.constructor;
let meta = meta$(klass,{symbol})
// All classes defined in imba get the Class.meta getter to access metadata for class
Object.defineProperty(klass,__meta__$,{value: meta, enumerable: false, configurable: true})
// Override name of class
if(name && klass.name !== name){
Object.defineProperty(klass,"name",{value: name,configurable: true})
}
meta.flags = flags;
if(flags & ClassFlags.HasConstructor)
klass.prototype[__initor__$] = symbol;
if(meta.uses) for(let mixin of meta.uses) mixin.mixes?.(klass)
if(supr?.inherited instanceof Function) supr.inherited(klass)
return klass;
}
export function inited$(obj,symbol){
if(obj[__initor__$]===symbol){
// init potential hooks
obj[__inited__$]?.();
obj[__hooks__$]&&obj[__hooks__$].inited(obj);
}
}