Skip to content

Commit b17693f

Browse files
committedOct 4, 2016
Do not use a data slot in V8 isolate by default (issue #26)
Storing a shared list of class singletons only when a preprocessor macro `V8PP_ISOLATE_DATA_SLOT` is defined. Otherwise using the singletons list from a static variable inside of `class_instances()` function. It works this way in most cases for Node.js addons, since a native addon depends on another native addon very rarely. In opposite case, developer should define the `V8PP_ISOLATE_DATA_SLOT` macro with the same value in several projects, which all register and use shared classes in v8pp.
1 parent 21ad7e4 commit b17693f

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed
 

‎v8pp/class.hpp

+28-30
Original file line numberDiff line numberDiff line change
@@ -254,57 +254,55 @@ class class_singleton : public class_info
254254
return scope.Escape(obj);
255255
}
256256

257-
using class_instances = std::vector<void*>;
258-
public:
259-
static class_singleton& create(v8::Isolate* isolate)
257+
using class_singletons = std::vector<void*>;
258+
static class_singletons& class_instances(v8::Isolate* isolate)
260259
{
261-
// Get pointer to singleton instances from v8::Isolate
262-
class_instances* singletons =
263-
static_cast<class_instances*>(isolate->GetData(V8PP_ISOLATE_DATA_SLOT));
264-
if (!singletons)
260+
#if defined(V8PP_ISOLATE_DATA_SLOT)
261+
class_singletons* instances =
262+
static_cast<class_singletons*>(isolate->GetData(V8PP_ISOLATE_DATA_SLOT));
263+
if (!instances)
265264
{
266-
// No singletons map yet, create and store it
267-
singletons = new class_instances;
268-
isolate->SetData(V8PP_ISOLATE_DATA_SLOT, singletons);
265+
instances = new class_singletons;
266+
isolate->SetData(V8PP_ISOLATE_DATA_SLOT, instances);
269267
}
268+
return *instances;
269+
#else
270+
static std::unordered_map<v8::Isolate*, class_singletons> instances;
271+
return instances[isolate];
272+
#endif
273+
}
270274

271-
class_type = static_cast<type_index>(singletons->size());
275+
public:
276+
static class_singleton& create(v8::Isolate* isolate)
277+
{
278+
class_singletons& singletons = class_instances(isolate);
279+
class_type = static_cast<type_index>(singletons.size());
272280
class_singleton* result = new class_singleton(isolate);
273-
singletons->emplace_back(result);
274-
281+
singletons.emplace_back(result);
275282
return *result;
276283
}
277284

278285
static void destroy(v8::Isolate* isolate)
279286
{
280-
class_instances* singletons =
281-
static_cast<class_instances*>(isolate->GetData(V8PP_ISOLATE_DATA_SLOT));
282-
if (singletons)
287+
class_singletons& singletons = class_instances(isolate);
288+
if (class_type < singletons.size())
283289
{
284-
if (class_type < singletons->size())
290+
class_singleton* instance = static_cast<class_singleton*>(singletons[class_type]);
291+
if (instance)
285292
{
286-
class_singleton* instance = static_cast<class_singleton*>((*singletons)[class_type]);
287293
instance->destroy_objects();
288294
delete instance;
289-
(*singletons)[class_type] = nullptr;
290-
size_t const null_count = std::count(singletons->begin(), singletons->end(), nullptr);
291-
if (null_count == singletons->size())
292-
{
293-
delete singletons;
294-
isolate->SetData(V8PP_ISOLATE_DATA_SLOT, nullptr);
295-
}
295+
singletons[class_type] = nullptr;
296296
}
297297
}
298298
}
299299

300300
static class_singleton& instance(v8::Isolate* isolate)
301301
{
302-
// Get pointer to singleton instances from v8::Isolate
303-
class_instances* singletons =
304-
static_cast<class_instances*>(isolate->GetData(V8PP_ISOLATE_DATA_SLOT));
305-
if (singletons && class_type < singletons->size())
302+
class_singletons& singletons = class_instances(isolate);
303+
if (class_type < singletons.size())
306304
{
307-
class_singleton* result = static_cast<class_singleton*>((*singletons)[class_type]);
305+
class_singleton* result = static_cast<class_singleton*>(singletons[class_type]);
308306
if (result)
309307
{
310308
return *result;

‎v8pp/config.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#ifndef V8PP_CONFIG_HPP_INCLUDED
1010
#define V8PP_CONFIG_HPP_INCLUDED
1111

12-
/// v8::Isolate data slot number, used in v8pp
13-
#if !defined(V8PP_ISOLATE_DATA_SLOT)
14-
#define V8PP_ISOLATE_DATA_SLOT 0
15-
#endif
12+
/// v8::Isolate data slot number, used in v8pp for shared data
13+
//#if !defined(V8PP_ISOLATE_DATA_SLOT)
14+
//#define V8PP_ISOLATE_DATA_SLOT 0
15+
//#endif
1616

1717
/// v8pp plugin initialization procedure name
1818
#if !defined(V8PP_PLUGIN_INIT_PROC_NAME)

0 commit comments

Comments
 (0)