8000 core, refactor: use External to keep netive object pointer. · fibjs/fibjs@e12db17 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit e12db17

Browse files
committed
core, r 8000 efactor: use External to keep netive object pointer.
1 parent d305a44 commit e12db17

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

fibjs/include/ClassInfo.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ class ClassInfo {
133133
o = _cache->m_function.Get(isolate->m_isolate)
134134
->NewInstance(isolate->context())
135135
.FromMaybe(v8::Local<v8::Object>());
136-
o->SetAlignedPointerInInternalField(0, 0);
137136
_cache->m_cache.Reset(isolate->m_isolate, o);
138137

139138
o = o->Clone();
@@ -375,8 +374,6 @@ class ClassInfo {
375374
}
376375

377376
v8::Local<v8::ObjectTemplate> ot = _class->InstanceTemplate();
378-
ot->SetInternalFieldCount(1);
379-
380377
ClassData* pcd;
381378

382379
pcd = &m_cd;
@@ -403,7 +400,6 @@ class ClassInfo {
403400

404401
if (m_cd.cor) {
405402
v8::Local<v8::Object> o = _function->NewInstance(isolate->context()).FromMaybe(v8::Local<v8::Object>());
406-
o->SetAlignedPointerInInternalField(0, 0);
407403
_cache->m_cache.Reset(isolate->m_isolate, o);
408404
}
409405

fibjs/include/object.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ class object_base : public obj_base {
170170
if (o.IsEmpty())
171171
o = Classinfo().CreateInstance(isolate);
172172
handle_.Reset(v8_isolate, o);
173-
o->SetAlignedPointerInInternalField(0, this);
173+
v8::Local<v8::Private> buf_key = v8::Private::ForApi(isolate->m_isolate, isolate->NewString("internal"));
174+
o->SetPrivate(isolate->context(), buf_key, v8::External::New(isolate->m_isolate, this));
174175

175176
v8_isolate->AdjustAmountOfExternalAllocatedMemory(m_nExtMemory);
176177

@@ -374,7 +375,10 @@ class object_base : public obj_base {
374375
v8::Isolate* v8_isolate = isolate->m_isolate;
375376

376377
if (m_isJSObject & JSOBJECT_JSHANDLE)
377-
v8::Local<v8::Object>::New(v8_isolate, handle_)->SetAlignedPointerInInternalField(0, 0);
378+
{
8000
379+
v8::Local<v8::Private> buf_key = v8::Private::ForApi(isolate->m_isolate, isolate->NewString("internal"));
380+
v8::Local<v8::Object>::New(v8_isolate, handle_)->DeletePrivate(isolate->context(), buf_key).IsJust();
381+
}
378382

379383
clear_handle();
380384

fibjs/include/utils.h

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -362,26 +362,33 @@ typedef int32_t result_t;
362362

363363
#define ARG_LIST(n) OptArgs v##n(args, n, argc1);
364364

365-
#define DECLARE_CLASSINFO(c) \
366-
public: \
367-
static ClassInfo& class_info(); \
368-
virtual ClassInfo& Classinfo() \
369-
{ \
370-
return class_info(); \
371-
} \
372-
static c* getInstance(void* o) \
373-
{ \
374-
return dynamic_cast<c*>((object_base*)o); \
375-
} \
376-
static c* getInstance(v8::Local<v8::Value> o) \
377-
{ \
378-
if (o.IsEmpty() || !o->IsObject()) \
379-
return NULL; \
380-
\
381-
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(o); \
382-
if (obj->InternalFieldCount() != 1) \
383-
return NULL; \
384-
return getInstance(obj->GetAlignedPointerFromInternalField(0)); \
365+
#define DECLARE_CLASSINFO(c) \
366+
public: \
367+
static ClassInfo& class_info(); \
368+
virtual ClassInfo& Classinfo() \
369+
{ \
370+
return class_info(); \
371+
} \
372+
static c* getInstance(void* o) \
373+
{ \
374+
return dynamic_cast<c*>((object_base*)o); \
375+
} \
376+
static c* getInstance(v8::Local<v8::Value> o) \
377+
{ \
378+
if (o.IsEmpty() || !o->IsObject()) \
379+
return NULL; \
380+
\
381+
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(o); \
382+
Isolate* isolate = Isolate::current(); \
383+
v8::Local<v8::Private> buf_key = v8::Private::ForApi(isolate->m_isolate, isolate->NewString("internal")); \
384+
if (!obj->HasPrivate(isolate->context(), buf_key).FromMaybe(false)) \
385+
return NULL; \
386+
v8::Local<v8::Value> v; \
387+
obj->GetPrivate(isolate->context(), buf_key).ToLocal(&v); \
388+
if (v.IsEmpty()) \
389+
return NULL; \
390+
v8::Local<v8::External> ext = v8::Local<v8::External>::Cast(v); \
391+
return getInstance(ext->Value()); \
385392
}
386393

387394
#define DECLARE_CLASS(c) \

0 commit comments

Comments
 (0)
0