~marcustomlinson/v8-cpp/test-gc

« back to all changes in this revision

Viewing changes to src/internal/require.h

  • Committer: Marcus Tomlinson
  • Date: 2015-07-08 09:29:21 UTC
  • Revision ID: marcus.tomlinson@canonical.com-20150708092921-i8sjb82198rc95fr
Cleaned up some memoery leaks and added valgrind.supp

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include <dlfcn.h>
22
22
#include <iostream>
 
23
#include <memory>
23
24
 
24
25
namespace v8cpp
25
26
{
28
29
 
29
30
using ModuleInitFunc = void(v8::Handle<v8::Object> exports);
30
31
ModuleInitFunc* node_init_func_;
31
 
std::string v8cpp_script_path_;
 
32
std::shared_ptr<std::string> v8cpp_script_path_;
32
33
 
33
34
struct NodeModule
34
35
{
92
93
inline v8::Local<v8::Object> require(std::string const& module_path)
93
94
{
94
95
    node_init_func_ = nullptr;
95
 
    v8::Local<v8::Object> exports = v8::Object::New(v8::Isolate::GetCurrent());
 
96
    std::string script_path = v8cpp_script_path_ ? *v8cpp_script_path_ : "";
96
97
 
97
98
    // Try append ".node" to module_path
98
 
    std::string suffixed_module_path = v8cpp_script_path_ + module_path + ".node";
 
99
    std::string suffixed_module_path = script_path + module_path + ".node";
99
100
    auto module = dlopen(suffixed_module_path.c_str(), RTLD_LAZY);
100
101
    if (!module)
101
102
    {
102
103
        // Didn't work, now try append ".so" to module_path
103
 
        suffixed_module_path = v8cpp_script_path_ + module_path + ".so";
 
104
        suffixed_module_path = script_path + module_path + ".so";
104
105
        module = dlopen(suffixed_module_path.c_str(), RTLD_LAZY);
105
106
        if (!module)
106
107
        {
107
108
            // Still didn't work, just try module_path as is
108
 
            suffixed_module_path = v8cpp_script_path_ + module_path;
 
109
            suffixed_module_path = script_path + module_path;
109
110
            module = dlopen(suffixed_module_path.c_str(), RTLD_LAZY);
110
111
            if (!module)
111
112
            {
114
115
        }
115
116
    }
116
117
 
 
118
    v8::Local<v8::Object> exports = v8::Object::New(v8::Isolate::GetCurrent());
 
119
 
117
120
    if (node_init_func_)
118
121
    {
119
122
        node_init_func_(exports);