-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathobject.h
More file actions
569 lines (456 loc) · 14.2 KB
/
object.h
File metadata and controls
569 lines (456 loc) · 14.2 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
/***************************************************************************
* Copyright (C) 2012 by Leo Hoo *
* *
***************************************************************************/
#pragma once
/**
@author Leo Hoo <[email protected]>
*/
#include "utils.h"
#include "ClassInfo.h"
#include "Runtime.h"
#include "AsyncCall.h"
#undef min
#undef max
namespace fibjs {
#include "object_async.inl"
#define JSOBJECT_JSVALUE 1
#define JSOBJECT_JSHANDLE 2
class object_base : public obj_base {
public:
object_base()
: m_isolate(NULL)
, m_isJSObject(0)
, m_nExtMemory(sizeof(object_base) * 2)
, m_nExtMemoryDelay(0)
, m_holding(false)
{
if (g_track_native_object) {
m_in_trace = true;
object_base::class_info().RefClass();
}
}
virtual ~object_base()
{
clear_handle();
if (m_in_trace)
object_base::class_info().UnrefClass();
}
public:
bool m_in_trace = false;
public:
virtual void Unref()
{
if (internalUnref() == 0) {
if (m_isJSObject) {
ex_assert(!m_weak.m_inlist);
Isolate* isolate = m_isolate;
isolate->m_weakLock.lock();
isolate->m_weak.putTail(&m_weak);
isolate->m_weakLock.unlock();
} else
Delete();
}
}
virtual void Delete()
{
delete this;
}
private:
exlib::linkitem m_weak;
public:
virtual bool enterTask(exlib::Task_base* current)
{
return m_lock.lock(current);
}
virtual void enter()
{
if (!m_lock.trylock()) {
Isolate::LeaveJsScope _rt(holder());
m_lock.lock();
}
}
virtual void leave(exlib::Task_base* current = NULL)
{
m_lock.unlock(current);
}
exlib::Locker m_lock;
private:
v8::Global<v8::Object> handle_;
public:
static void gc_delete(exlib::linkitem* node)
{
object_base* pThis = NULL;
pThis = (object_base*)((char*)node
- ((char*)&pThis->m_weak - (char*)0));
pThis->Delete();
}
private:
Isolate* m_isolate;
int32_t m_isJSObject;
public:
Isolate* holder(Isolate* _isolate = NULL)
{
Isolate* isolate = m_isolate;
if (isolate)
return isolate;
return m_isolate = _isolate ? _isolate : Isolate::current();
}
Isolate* get_holder()
{
return m_isolate;
}
const char* typeName()
{
return Classinfo().name();
}
void setJSObject()
{
m_isJSObject |= JSOBJECT_JSVALUE;
holder();
ex_assert(m_isolate != 0);
}
private:
void clear_handle()
{
if (m_isJSObject & JSOBJECT_JSHANDLE) {
m_isJSObject &= ~JSOBJECT_JSHANDLE;
Isolate* isolate = holder();
v8::Isolate* v8_isolate = isolate->m_isolate;
handle_.ClearWeak();
handle_.Reset();
v8_isolate->AdjustAmountOfExternalAllocatedMemory(-m_nExtMemory);
if (internalUnref() == 0)
Delete();
}
}
static void WeakCallback(const v8::WeakCallbackInfo<object_base>& data)
{
ex_assert(!data.GetParameter()->handle_.IsEmpty());
data.GetParameter()->clear_handle();
}
public:
virtual v8::Local<v8::Object> wrap(Isolate* isolate, v8::Local<v8::Object> o = v8::Local<v8::Object>());
v8::Local<v8::Object> wrap(v8::Local<v8::Object> o = v8::Local<v8::Object>())
{
return wrap(o.IsEmpty() ? Isolate::current() : Isolate::current(o), o);
}
static void* unwrap(v8::Local<v8::Value> o);
bool hasJSHandle() const
{
return (m_isJSObject & JSOBJECT_JSHANDLE) != 0;
}
void safe_release()
{
if (m_isolate)
m_isolate->sync([this]() {
delete this;
return 0;
});
else
delete this;
}
public:
class scope {
public:
scope(object_base* pInst)
: m_pInst(pInst)
{
m_pInst->enter();
}
~scope()
{
m_pInst->leave();
}
private:
object_base* m_pInst;
};
public:
// Event
result_t on(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object>& retVal);
result_t on(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal);
result_t addEventListener(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object> options, v8::Local<v8::Object>& retVal);
result_t prependListener(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object>& retVal);
result_t prependListener(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal);
result_t once(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object>& retVal);
result_t once(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal);
result_t prependOnceListener(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object>& retVal);
result_t prependOnceListener(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal);
result_t off(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object>& retVal);
result_t off(exlib::string ev, v8::Local<v8::Object>& retVal);
result_t off(v8::Local<v8::Object> map, v8::Local<v8::Object>& retVal);
result_t removeEventListener(exlib::string ev, v8::Local<v8::Function> func, v8::Local<v8::Object> options, v8::Local<v8::Object>& retVal);
result_t removeAllListeners(exlib::string ev, v8::Local<v8::Object>& retVal);
result_t removeAllListeners(v8::Local<v8::Array> evs, v8::Local<v8::Object>& retVal);
result_t setMaxListeners(int32_t n);
result_t getMaxListeners(int32_t& retVal);
result_t setListener(exlib::string ev, v8::Local<v8::Function> func);
result_t getListener(exlib::string ev, v8::Local<v8::Function>& func);
result_t listeners(exlib::string ev, v8::Local<v8::Array>& retVal);
result_t rawListeners(exlib::string ev, v8::Local<v8::Array>& retVal);
result_t listenerCount(exlib::string ev, int32_t& retVal);
result_t listenerCount(v8::Local<v8::Value> o, exlib::string ev, int32_t& retVal);
result_t emit(exlib::string ev, OptArgs args, bool& retVal);
result_t eventNames(v8::Local<v8::Array>& retVal);
result_t _emit(exlib::string ev, v8::Local<v8::Value>* args, int32_t argCount, bool& retVal);
result_t _emit(exlib::string ev, Variant* args = NULL, int32_t argCount = 0);
result_t _emit(exlib::string ev, Variant arg);
virtual result_t onEventChange(exlib::string type, exlib::string ev, v8::Local<v8::Function> func)
{
return 0;
}
virtual result_t onEventEmit(exlib::string ev)
{
return 0;
}
void extMemory(int32_t ext)
{
if (handle_.IsEmpty())
m_nExtMemory += ext;
else {
ext += m_nExtMemoryDelay;
m_nExtMemoryDelay = 0;
if (ext != 0) {
Isolate* isolate = m_isolate;
if (isolate && Runtime::is_current(isolate)) {
isolate->m_isolate->AdjustAmountOfExternalAllocatedMemory(ext);
m_nExtMemory += ext;
} else
m_nExtMemoryDelay = ext;
}
}
}
void isolate_ref()
{
bool expected = false;
if (m_holding.compare_exchange_strong(expected, true))
holder()->Ref();
}
void isolate_unref()
{
bool expected = true;
if (m_holding.compare_exchange_strong(expected, false))
holder()->Unref();
}
private:
v8::Local<v8::Array> GetHiddenList(const char* k, bool create = false,
bool autoDelete = false);
private:
int32_t m_nExtMemory;
int32_t m_nExtMemoryDelay;
std::atomic_bool m_holding;
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CONSTRUCT_INIT();
ThrowTypeError("not a constructor");
}
static result_t load(Isolate* isolate, v8::Local<v8::Value> v, obj_ptr<object_base>& retVal)
{
return CALL_E_TYPEMISMATCH;
}
public:
v8::Local<v8::Object> GetPrivateObject()
{
Isolate* isolate = holder();
v8::Local<v8::Object> o = wrap(isolate);
v8::Local<v8::Context> context = isolate->context();
v8::Local<v8::Private> k = v8::Private::ForApi(isolate->m_isolate, isolate->NewString("_private_object"));
JSValue v = o->GetPrivate(context, k);
if (v->IsObject())
return v8::Local<v8::Object>::Cast(v);
v8::Local<v8::Object> po = v8::Object::New(isolate->m_isolate);
o->SetPrivate(context, k, po);
return po;
}
v8::Local<v8::Value> GetPrivate(exlib::string key)
{
v8::Local<v8::Context> context = holder()->context();
return JSValue(GetPrivateObject()->Get(context, holder()->NewString(key)));
}
void SetPrivate(exlib::string key, v8::Local<v8::Value> value)
{
v8::Local<v8::Context> context = holder()->context();
GetPrivateObject()->Set(context, holder()->NewString(key), value).IsJust();
}
void DeletePrivate(exlib::string key)
{
v8::Local<v8::Context> context = holder()->context();
GetPrivateObject()->Delete(context, holder()->NewString(key)).IsJust();
}
public:
virtual result_t equals(object_base* expected, bool& retVal)
{
retVal = expected == this;
return 0;
}
virtual result_t toString(exlib::string& retVal)
{
retVal = "[object ";
retVal.append(Classinfo().name());
retVal.append("]");
return 0;
}
virtual result_t toJSON(exlib::string key, v8::Local<v8::Value>& retVal)
{
v8::Local<v8::Object> o = wrap(holder());
v8::Local<v8::Object> o1 = v8::Object::New(holder()->m_isolate);
extend(o, o1);
retVal = o1;
return 0;
}
virtual result_t valueOf(v8::Local<v8::Value>& retVal)
{
retVal = wrap(holder());
return 0;
}
virtual result_t unbind(obj_ptr<object_base>& retVal)
{
return CHECK_ERROR(CALL_E_INVALID_CALL);
}
result_t unbind_dispose(obj_ptr<object_base>& retVal)
{
Isolate* isolate = holder();
v8::Isolate* v8_isolate = isolate->m_isolate;
if (m_isJSObject & JSOBJECT_JSHANDLE)
v8::Local<v8::Object>::New(v8_isolate, handle_)->SetAlignedPointerInInternalField(0, 0);
clear_handle();
isolate->m_weakLock.lock();
#ifdef DEBUG
if (m_weak.m_inlist)
#endif
isolate->m_weak.remove(&m_weak);
isolate->m_weakLock.unlock();
m_isolate = NULL;
retVal = this;
return 0;
}
public:
static void block_set(const v8::FunctionCallbackInfo<v8::Value>& args)
{
ThrowTypeError("Property is read-only.");
}
static v8::Intercepted i_IndexedSetter(uint32_t index,
v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
{
ThrowTypeError("Indexed Property is read-only.");
return v8::Intercepted::kNo;
}
static v8::Intercepted i_NamedSetter(v8::Local<v8::Name> property,
v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
{
ThrowTypeError("Named Property is read-only.");
return v8::Intercepted::kNo;
}
static v8::Intercepted i_NamedDeleter(
v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean>& info)
{
ThrowTypeError("Named Property is read-only.");
return v8::Intercepted::kNo;
}
//------------------------------------------------------------------
public:
static ClassInfo& class_info();
virtual ClassInfo& Classinfo()
{
return class_info();
}
static object_base* getInstance(object_base* o)
{
return o;
}
static object_base* getInstance(v8::Local<v8::Value> o)
{
return getInstance((object_base*)unwrap(o));
}
private:
static void s_toString(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_toJSON(const v8::FunctionCallbackInfo<v8::Value>& args);
};
class ValueHolder : public obj_base {
public:
ValueHolder(v8::Local<v8::Object> v)
{
m_isolate = Isolate::current(v);
m_v.Reset(m_isolate->m_isolate, v);
}
public:
virtual void Unref()
{
if (internalUnref() == 0)
m_isolate->sync([this]() -> int {
delete this;
return 0;
});
}
private:
v8::Global<v8::Object> m_v;
Isolate* m_isolate;
};
class RootModule {
public:
RootModule()
: m_next(NULL)
{
}
public:
void install()
{
if (!g_root)
g_root = this;
else
g_last->m_next = this;
g_last = this;
}
public:
virtual ClassInfo& class_info() = 0;
virtual v8::Local<v8::Object> getModule(Isolate* isolate)
{
return class_info().getModule(isolate);
}
virtual const char* name()
{
return class_info().name();
}
public:
RootModule* m_next;
static RootModule* g_root;
static RootModule* g_last;
};
inline ClassInfo& object_base::class_info()
{
static ClassData::ClassMethod s_method[] = {
{ "toString", s_toString },
{ "toJSON", s_toJSON }
};
static ClassData s_cd = {
"object", false, NULL, NULL, ARRAYSIZE(s_method), s_method
};
static ClassInfo s_ci(s_cd);
return s_ci;
}
inline void object_base::s_toString(const v8::FunctionCallbackInfo<v8::Value>& args)
{
exlib::string vr;
METHOD_INSTANCE(object_base);
METHOD_ENTER();
METHOD_OVER(0, 0);
hr = pInst->toString(vr);
METHOD_RETURN();
}
inline void object_base::s_toJSON(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Local<v8::Value> vr;
METHOD_INSTANCE(object_base);
METHOD_ENTER();
METHOD_OVER(1, 0);
OPT_ARG(exlib::string, 0, "");
hr = pInst->toJSON(v0, vr);
METHOD_RETURN();
}
}
#ifdef _assert
#undef _assert
#endif
#include "SimpleObject.h"