-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathClassInfo.h
More file actions
595 lines (478 loc) · 19.9 KB
/
ClassInfo.h
File metadata and controls
595 lines (478 loc) · 19.9 KB
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/***************************************************************************
* Copyright (C) 2012 by Leo Hoo *
* *
***************************************************************************/
#pragma once
/**
@author Leo Hoo <[email protected]>
*/
#include <v8.h>
#include <string>
namespace fibjs {
extern bool g_track_native_object;
class ClassInfo;
struct ClassData {
enum AsyncType {
ASYNC_SYNC = 0,
ASYNC_ASYNC = 1,
ASYNC_PROMISE = 2
};
struct ClassProperty {
const char* name;
v8::FunctionCallback getter;
v8::FunctionCallback setter;
bool is_static;
};
enum ConstType {
CONST_Integer = 0,
CONST_Boolean = 1,
CONST_String = 2
};
struct ClassConst {
const char* name;
ConstType type;
union {
int32_t intValue;
bool boolValue;
const char* stringValue;
};
};
struct ClassMethod {
const char* name;
v8::FunctionCallback invoker;
bool is_static;
AsyncType async_type;
};
struct ClassObject {
const char* name;
ClassInfo& (*invoker)();
};
struct ClassIndexed {
v8::IndexedPropertyGetterCallbackV2 getter;
v8::IndexedPropertySetterCallbackV2 setter;
};
struct ClassNamed {
v8::NamedPropertyGetterCallback getter;
v8::NamedPropertySetterCallback setter;
v8::NamedPropertyDeleterCallback remover;
v8::NamedPropertyEnumeratorCallback enumerator;
};
const char* name;
bool module;
v8::FunctionCallback cor;
v8::FunctionCallback caf;
int32_t mc;
const ClassMethod* cms;
int32_t oc;
const ClassObject* cos;
int32_t pc;
const ClassProperty* cps;
int32_t cc;
const ClassConst* ccs;
const ClassIndexed* cis;
const ClassNamed* cns;
ClassInfo* base;
bool has_async;
};
class ClassInfo {
public:
class cache {
public:
cache()
: m_init_isolate(0)
{
}
public:
int32_t m_init_isolate;
v8::Global<v8::FunctionTemplate> m_class;
v8::Global<v8::Object> m_cache;
v8::Global<v8::Function> m_function;
v8::Global<v8::FunctionTemplate> m_pclass;
v8::Global<v8::Object> m_pcache;
};
public:
ClassInfo(ClassData& cd)
: m_cd(cd)
, refs_(0)
, m_next(NULL)
, m_Inherit(NULL)
, m_id(-1)
{
if (m_cd.base) {
if (m_cd.base->m_Inherit)
m_next = m_cd.base->m_Inherit;
m_cd.base->m_Inherit = this;
}
}
v8::Local<v8::Function> getFunction(Isolate* isolate)
{
ex_assert(!m_cd.module);
return _init(isolate)->m_function.Get(isolate->m_isolate);
}
v8::Local<v8::Object> getModule(Isolate* isolate)
{
if (m_cd.module)
return _init(isolate)->m_cache.Get(isolate->m_isolate);
else
return _init(isolate)->m_function.Get(isolate->m_isolate);
}
v8::Local<v8::Object> CreateInstance(Isolate* isolate)
{
ex_assert(!m_cd.module);
return _init(isolate)->m_cache.Get(isolate->m_isolate)->Clone();
}
v8::Local<v8::Value> GetAsyncPrototype(Isolate* isolate)
{
ex_assert(!m_cd.module);
return _init(isolate)->m_pcache.Get(isolate->m_isolate)->GetPrototype();
}
v8::Local<v8::Value> GetPrototype(Isolate* isolate)
{
ex_assert(!m_cd.module);
return _init(isolate)->m_cache.Get(isolate->m_isolate)->GetPrototype();
}
bool hasAsync()
{
return m_cd.has_async;
}
bool isInstance(ClassInfo& ci)
{
ClassInfo* _ci = &ci;
while (_ci) {
if (_ci == this)
return true;
_ci = _ci->m_cd.base;
}
return false;
}
bool init_isolate(Isolate* isolate)
{
cache* _cache = _init(isolate);
if (_cache->m_init_isolate > (m_cd.has_async ? 1 : 0))
return false;
_cache->m_init_isolate++;
return true;
}
v8::Local<v8::Name> get_prop_name(Isolate* isolate, const char* name)
{
if (name[0] != '@')
return isolate->NewString(name);
if (!qstrcmp("iterator", name + 1))
return v8::Symbol::GetIterator(isolate->m_isolate);
if (!qstrcmp("toStringTag", name + 1))
return v8::Symbol::GetToStringTag(isolate->m_isolate);
return isolate->NewString(name);
}
bool has(const char* name)
{
int32_t i;
for (i = 0; i < m_cd.mc; i++)
if (!qstrcmp(name, m_cd.cms[i].name))
return true;
for (i = 0; i < m_cd.oc; i++)
if (!qstrcmp(name, m_cd.cos[i].name))
return true;
for (i = 0; i < m_cd.pc; i++)
if (!qstrcmp(name, m_cd.cps[i].name))
return true;
for (i = 0; i < m_cd.cc; i++)
if (!qstrcmp(name, m_cd.ccs[i].name))
return true;
if (m_cd.base)
return m_cd.base->has(name);
return false;
}
const char* name()
{
return m_cd.name;
}
void Attach(Isolate* isolate, v8::Local<v8::Object> o)
{
int32_t i;
v8::Local<v8::Context> _context = isolate->context();
v8::Local<v8::Object> op;
if (m_cd.has_async)
op = v8::Object::New(isolate->m_isolate);
for (i = 0; i < m_cd.mc; i++) {
if (m_cd.cms[i].is_static) {
v8::Local<v8::Function> func = isolate->NewFunction(m_cd.cms[i].name, m_cd.cms[i].invoker);
v8::Local<v8::Name> name = get_prop_name(isolate, m_cd.cms[i].name);
if (m_cd.cms[i].async_type == ClassData::ASYNC_SYNC) {
o->Set(_context, name, func).IsJust();
if (m_cd.has_async)
op->Set(_context, name, func).IsJust();
} else {
func->SetPrivate(_context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_async")), func);
func->SetPrivate(_context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_sync")), func);
v8::Local<v8::Function> pfunc = isolate->NewFunction(m_cd.cms[i].name, m_cd.cms[i].invoker, v8::True(isolate->m_isolate));
setAsyncFunctoin(pfunc);
func->SetPrivate(_context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_promise")), pfunc);
pfunc->SetPrivate(_context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_async")), func);
pfunc->SetPrivate(_context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_sync")), func);
exlib::string name_sync(m_cd.cms[i].name);
name_sync.append("Sync");
v8::Local<v8::Name> _name_sync = get_prop_name(isolate, name_sync.c_str());
exlib::string name_async(m_cd.cms[i].name);
name_async.append("Async");
v8::Local<v8::Name> _name_async = get_prop_name(isolate, name_async.c_str());
if (m_cd.cms[i].async_type == ClassData::ASYNC_ASYNC)
o->Set(_context, name, func).IsJust();
else
o->Set(_context, name, pfunc).IsJust();
o->Set(_context, _name_sync, func).IsJust();
o->Set(_context, _name_async, pfunc).IsJust();
if (m_cd.has_async) {
op->Set(_context, name, pfunc).IsJust();
op->Set(_context, _name_sync, func).IsJust();
op->Set(_context, _name_async, pfunc).IsJust();
}
}
}
}
for (i = 0; i < m_cd.oc; i++) {
o->Set(_context, isolate->NewString(m_cd.cos[i].name),
m_cd.cos[i].invoker().getModule(isolate))
.IsJust();
}
for (i = 0; i < m_cd.pc; i++)
if (m_cd.cps[i].is_static) {
o->SetAccessorProperty(get_prop_name(isolate, m_cd.cps[i].name),
isolate->NewFunction(m_cd.cps[i].name, m_cd.cps[i].getter),
isolate->NewFunction(m_cd.cps[i].name, m_cd.cps[i].setter));
}
for (i = 0; i < m_cd.cc; i++) {
v8::Local<v8::Value> constVal;
switch (m_cd.ccs[i].type) {
case ClassData::CONST_Integer:
constVal = v8::Integer::New(isolate->m_isolate, m_cd.ccs[i].intValue);
break;
case ClassData::CONST_Boolean:
constVal = v8::Boolean::New(isolate->m_isolate, m_cd.ccs[i].boolValue);
break;
case ClassData::CONST_String:
constVal = isolate->NewString(m_cd.ccs[i].stringValue);
break;
}
o->Set(_context, isolate->NewString(m_cd.ccs[i].name), constVal).IsJust();
}
if (m_cd.base)
m_cd.base->Attach(isolate, o);
if (!op.IsEmpty())
o->Set(_context, isolate->NewString("promises"), op).IsJust();
}
public:
void RefClass()
{
refs_.inc();
}
void UnrefClass()
{
refs_.dec();
}
ClassData& data()
{
return m_cd;
}
intptr_t dump(v8::Local<v8::Object>& o)
{
Isolate* isolate = Isolate::current();
v8::Local<v8::Context> context = isolate->context();
intptr_t cnt = refs_;
if (cnt) {
o = v8::Object::New(isolate->m_isolate);
o->Set(context, isolate->NewString("class"),
isolate->NewString(m_cd.name))
.IsJust();
o->Set(context, isolate->NewString("objects"),
v8::Integer::New(isolate->m_isolate, (int32_t)cnt))
.IsJust();
v8::Local<v8::Array> inherits = v8::Array::New(isolate->m_isolate);
ClassInfo* p = m_Inherit;
intptr_t icnt = 0;
while (p) {
v8::Local<v8::Object> o1;
intptr_t cnt1 = p->dump(o1);
if (cnt1)
inherits->Set(context, (int32_t)(icnt++), o1).IsJust();
p = p->m_next;
}
if (icnt)
o->Set(context, isolate->NewString("inherits"), inherits).IsJust();
}
return cnt;
}
private:
cache* _init(Isolate* isolate)
{
static exlib::atomic m_gid;
if (m_id < 0)
m_id = (int32_t)m_gid.inc();
void* p = NULL;
while ((int32_t)isolate->m_classInfo.size() < m_id)
isolate->m_classInfo.append(p);
cache* _cache = (cache*)isolate->m_classInfo[m_id];
if (_cache)
return _cache;
isolate->m_classInfo[m_id] = _cache = new cache();
v8::Local<v8::Context> context = isolate->context();
v8::Local<v8::String> class_name = isolate->NewString(m_cd.name);
if (!m_cd.module) {
v8::Local<v8::FunctionTemplate> _pclass;
v8::Local<v8::FunctionTemplate> _class = v8::FunctionTemplate::New(isolate->m_isolate, m_cd.cor);
_class->SetClassName(class_name);
_cache->m_class.Reset(isolate->m_isolate, _class);
if (m_cd.has_async) {
_pclass = v8::FunctionTemplate::New(isolate->m_isolate, m_cd.cor);
_pclass->SetClassName(class_name);
_cache->m_pclass.Reset(isolate->m_isolate, _pclass);
} else
_cache->m_pclass.Reset(isolate->m_isolate, _class);
if (m_cd.base) {
cache* _cache1 = m_cd.base->_init(isolate);
_class->Inherit(_cache1->m_class.Get(isolate->m_isolate));
if (m_cd.has_async)
_pclass->Inherit(_cache1->m_pclass.Get(isolate->m_isolate));
}
v8::Local<v8::ObjectTemplate> pt = _class->PrototypeTemplate();
pt->Set(get_prop_name(isolate, "@toStringTag"), class_name,
v8::PropertyAttribute::DontEnum);
v8::Local<v8::ObjectTemplate> ppt;
if (m_cd.has_async) {
ppt = _pclass->PrototypeTemplate();
ppt->Set(get_prop_name(isolate, "@toStringTag"), class_name,
v8::PropertyAttribute::DontEnum);
}
int32_t i;
for (i = 0; i < m_cd.mc; i++)
if (!m_cd.cms[i].is_static) {
v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New(isolate->m_isolate, m_cd.cms[i].invoker);
v8::Local<v8::Name> name = get_prop_name(isolate, m_cd.cms[i].name);
if (m_cd.cms[i].async_type == ClassData::ASYNC_SYNC) {
pt->Set(name, ft);
if (m_cd.has_async)
ppt->Set(name, ft);
} else {
v8::Local<v8::Function> func = ft->GetFunction(context).FromMaybe(v8::Local<v8::Function>());
func->SetPrivate(context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_async")), func);
func->SetPrivate(context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_sync")), func);
v8::Local<v8::FunctionTemplate> pft = v8::FunctionTemplate::New(isolate->m_isolate, m_cd.cms[i].invoker, v8::True(isolate->m_isolate));
v8::Local<v8::Function> pfunc = pft->GetFunction(context).FromMaybe(v8::Local<v8::Function>());
setAsyncFunctoin(pfunc);
func->SetPrivate(context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_promise")), pfunc);
pfunc->SetPrivate(context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_async")), func);
pfunc->SetPrivate(context, v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_sync")), func);
exlib::string name_sync(m_cd.cms[i].name);
name_sync.append("Sync");
v8::Local<v8::Name> _name_sync = get_prop_name(isolate, name_sync.c_str());
exlib::string name_async(m_cd.cms[i].name);
name_async.append("Async");
v8::Local<v8::Name> _name_async = get_prop_name(isolate, name_async.c_str());
if (m_cd.cms[i].async_type == ClassData::ASYNC_ASYNC)
pt->Set(name, ft);
else
pt->Set(name, pft);
pt->Set(_name_sync, ft);
pt->Set(_name_async, pft);
if (m_cd.has_async) {
ppt->Set(name, pft);
ppt->Set(_name_sync, ft);
ppt->Set(_name_async, pft);
}
}
}
for (i = 0; i < m_cd.pc; i++)
if (!m_cd.cps[i].is_static) {
v8::Local<v8::Name> name = get_prop_name(isolate, m_cd.cps[i].name);
v8::Local<v8::FunctionTemplate> ft_getter = v8::FunctionTemplate::New(isolate->m_isolate, m_cd.cps[i].getter);
v8::Local<v8::FunctionTemplate> ft_setter = v8::FunctionTemplate::New(isolate->m_isolate, m_cd.cps[i].setter);
pt->SetAccessorProperty(name, ft_getter, ft_setter, v8::DontDelete);
if (m_cd.has_async)
ppt->SetAccessorProperty(name, ft_getter, ft_setter, v8::DontDelete);
}
for (i = 0; i < m_cd.cc; i++) {
v8::Local<v8::String> name = isolate->NewString(m_cd.ccs[i].name);
v8::Local<v8::Value> constVal;
switch (m_cd.ccs[i].type) {
case ClassData::CONST_Integer:
constVal = v8::Integer::New(isolate->m_isolate, m_cd.ccs[i].intValue);
break;
case ClassData::CONST_Boolean:
constVal = v8::Boolean::New(isolate->m_isolate, m_cd.ccs[i].boolValue);
break;
case ClassData::CONST_String:
constVal = isolate->NewString(m_cd.ccs[i].stringValue);
break;
}
pt->Set(name, constVal);
if (m_cd.has_async)
ppt->Set(name, constVal);
}
v8::Local<v8::ObjectTemplate> ot = _class->InstanceTemplate();
ot->SetInternalFieldCount(1);
v8::Local<v8::ObjectTemplate> pot;
if (m_cd.has_async) {
pot = _pclass->InstanceTemplate();
pot->SetInternalFieldCount(1);
}
ClassData* pcd;
pcd = &m_cd;
while (pcd && !pcd->cis)
pcd = pcd->base ? &pcd->base->m_cd : NULL;
if (pcd) {
ot->SetHandler(v8::IndexedPropertyHandlerConfiguration(
pcd->cis->getter, pcd->cis->setter));
if (m_cd.has_async)
pot->SetHandler(v8::IndexedPropertyHandlerConfiguration(
pcd->cis->getter, pcd->cis->setter));
}
pcd = &m_cd;
while (pcd && !pcd->cns)
pcd = pcd->base ? &pcd->base->m_cd : NULL;
if (pcd) {
ot->SetHandler(v8::NamedPropertyHandlerConfiguration(
pcd->cns->getter, pcd->cns->setter, nullptr, pcd->cns->remover, pcd->cns->enumerator, v8::Local<v8::Value>(),
v8::PropertyHandlerFlags::kOnlyInterceptStrings));
if (m_cd.has_async)
pot->SetHandler(v8::NamedPropertyHandlerConfiguration(
pcd->cns->getter, pcd->cns->setter, nullptr, pcd->cns->remover, pcd->cns->enumerator, v8::Local<v8::Value>(),
v8::PropertyHandlerFlags::kOnlyInterceptStrings));
}
if (m_cd.caf) {
ot->SetCallAsFunctionHandler(m_cd.caf);
if (m_cd.has_async)
pot->SetCallAsFunctionHandler(m_cd.caf);
}
v8::Local<v8::Function> _function = _class->GetFunction(context).FromMaybe(v8::Local<v8::Function>());
_cache->m_function.Reset(isolate->m_isolate, _function);
v8::Local<v8::Object> o = _function->NewInstance(isolate->context()).FromMaybe(v8::Local<v8::Object>());
o->SetAlignedPointerInInternalField(0, 0);
_cache->m_cache.Reset(isolate->m_isolate, o);
Attach(isolate, _function);
if (m_cd.has_async) {
v8::Local<v8::Function> _pfunction = _pclass->GetFunction(context).FromMaybe(v8::Local<v8::Function>());
v8::Local<v8::Object> po = _pfunction->NewInstance(isolate->context()).FromMaybe(v8::Local<v8::Object>());
po->SetAlignedPointerInInternalField(0, 0);
_cache->m_pcache.Reset(isolate->m_isolate, po);
} else
_cache->m_pcache.Reset(isolate->m_isolate, o);
} else {
v8::Local<v8::Object> o;
if (m_cd.caf)
o = v8::Function::New(context, m_cd.caf).FromMaybe(v8::Local<v8::Function>());
else
o = v8::Object::New(isolate->m_isolate);
o->Set(context, get_prop_name(isolate, "@toStringTag"), class_name).IsJust();
_cache->m_cache.Reset(isolate->m_isolate, o);
Attach(isolate, o);
}
return _cache;
}
private:
ClassData& m_cd;
exlib::atomic refs_;
ClassInfo* m_next;
ClassInfo* m_Inherit;
int32_t m_id;
};
}