~marcustomlinson/v8-cpp/test-gc

« back to all changes in this revision

Viewing changes to src/locker.h

  • Committer: Marcus Tomlinson
  • Date: 2015-07-29 10:55:02 UTC
  • mfrom: (22.1.8 trunk)
  • Revision ID: marcus.tomlinson@canonical.com-20150729105502-cci3rsnmqua22d8m
Added v8cpp::Locker class

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Marcus Tomlinson <marcus.tomlinson@canonical.com>
 
17
 */
 
18
 
 
19
#pragma once
 
20
 
 
21
#include <memory>
 
22
 
 
23
namespace v8cpp
 
24
{
 
25
 
 
26
class Locker
 
27
{
 
28
public:
 
29
    explicit Locker(v8::Isolate* isolate)
 
30
    {
 
31
        if (v8::Locker::IsActive() && !v8::Locker::IsLocked(isolate))
 
32
        {
 
33
            locker_ = std::make_shared<v8::Locker>(isolate);
 
34
            isolate_scope_ = std::make_shared<v8::Isolate::Scope>(isolate);
 
35
            handle_scope_ = std::make_shared<v8::HandleScope>(isolate);
 
36
            context_scope_ = std::make_shared<v8::Context::Scope>(v8::Context::New(isolate));
 
37
        }
 
38
    }
 
39
 
 
40
private:
 
41
    std::shared_ptr<v8::Locker> locker_;
 
42
    std::shared_ptr<v8::Isolate::Scope> isolate_scope_;
 
43
    std::shared_ptr<v8::HandleScope> handle_scope_;
 
44
    std::shared_ptr<v8::Context::Scope> context_scope_;
 
45
};
 
46
 
 
47
class Unlocker
 
48
{
 
49
public:
 
50
    explicit Unlocker(v8::Isolate* isolate)
 
51
    {
 
52
        if (v8::Locker::IsActive() && v8::Locker::IsLocked(isolate))
 
53
        {
 
54
            unlocker_ = std::make_shared<v8::Unlocker>(isolate);
 
55
        }
 
56
    }
 
57
 
 
58
private:
 
59
    std::shared_ptr<v8::Unlocker> unlocker_;
 
60
};
 
61
 
 
62
}  // namespace v8cpp