assert-scope.h 5.06 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_ASSERT_SCOPE_H_
#define V8_ASSERT_SCOPE_H_

8
#include <stdint.h>
9
#include "src/base/macros.h"
10 11 12 13

namespace v8 {
namespace internal {

14
// Forward declarations.
15
class Isolate;
16 17
class PerThreadAssertData;

18 19 20 21 22 23

enum PerThreadAssertType {
  HEAP_ALLOCATION_ASSERT,
  HANDLE_ALLOCATION_ASSERT,
  HANDLE_DEREFERENCE_ASSERT,
  DEFERRED_HANDLE_DEREFERENCE_ASSERT,
24
  CODE_DEPENDENCY_CHANGE_ASSERT,
25 26 27 28
  LAST_PER_THREAD_ASSERT_TYPE
};


29 30
enum PerIsolateAssertType {
  JAVASCRIPT_EXECUTION_ASSERT,
31
  JAVASCRIPT_EXECUTION_THROWS,
32 33
  DEOPTIMIZATION_ASSERT,
  COMPILATION_ASSERT
34 35 36
};


37 38
template <PerThreadAssertType kType, bool kAllow>
class PerThreadAssertScope {
39
 public:
40 41
  PerThreadAssertScope();
  ~PerThreadAssertScope();
42

43
  static bool IsAllowed();
44

45
 private:
46
  PerThreadAssertData* data_;
47
  bool old_state_;
48 49 50 51 52 53

  DISALLOW_COPY_AND_ASSIGN(PerThreadAssertScope);
};


template <PerIsolateAssertType type, bool allow>
54
class PerIsolateAssertScope {
55
 public:
56 57
  explicit PerIsolateAssertScope(Isolate* isolate);
  ~PerIsolateAssertScope();
58

59
  static bool IsAllowed(Isolate* isolate);
60 61

 private:
62
  class DataBit;
63 64

  Isolate* isolate_;
65
  uint32_t old_data_;
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

  DISALLOW_COPY_AND_ASSIGN(PerIsolateAssertScope);
};


template <PerThreadAssertType type, bool allow>
#ifdef DEBUG
class PerThreadAssertScopeDebugOnly : public
    PerThreadAssertScope<type, allow> {
#else
class PerThreadAssertScopeDebugOnly {
 public:
  PerThreadAssertScopeDebugOnly() { }
#endif
};


template <PerIsolateAssertType type, bool allow>
#ifdef DEBUG
class PerIsolateAssertScopeDebugOnly : public
    PerIsolateAssertScope<type, allow> {
 public:
  explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate)
      : PerIsolateAssertScope<type, allow>(isolate) { }
#else
class PerIsolateAssertScopeDebugOnly {
 public:
  explicit PerIsolateAssertScopeDebugOnly(Isolate* isolate) { }
94 95 96
#endif
};

97 98
// Per-thread assert scopes.

99
// Scope to document where we do not expect handles to be created.
100
typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, false>
101 102 103
    DisallowHandleAllocation;

// Scope to introduce an exception to DisallowHandleAllocation.
104
typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, true>
105 106 107
    AllowHandleAllocation;

// Scope to document where we do not expect any allocation and GC.
108
typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, false>
109 110 111
    DisallowHeapAllocation;

// Scope to introduce an exception to DisallowHeapAllocation.
112
typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, true>
113 114 115
    AllowHeapAllocation;

// Scope to document where we do not expect any handle dereferences.
116
typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, false>
117 118 119
    DisallowHandleDereference;

// Scope to introduce an exception to DisallowHandleDereference.
120
typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, true>
121 122 123
    AllowHandleDereference;

// Scope to document where we do not expect deferred handles to be dereferenced.
124
typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false>
125 126 127
    DisallowDeferredHandleDereference;

// Scope to introduce an exception to DisallowDeferredHandleDereference.
128
typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>
129 130
    AllowDeferredHandleDereference;

131
// Scope to document where we do not expect deferred handles to be dereferenced.
132
typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, false>
133 134 135
    DisallowCodeDependencyChange;

// Scope to introduce an exception to DisallowDeferredHandleDereference.
136
typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, true>
137 138
    AllowCodeDependencyChange;

139 140 141 142 143 144 145 146 147 148 149

// Per-isolate assert scopes.

// Scope to document where we do not expect javascript execution.
typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false>
    DisallowJavascriptExecution;

// Scope to introduce an exception to DisallowJavascriptExecution.
typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true>
    AllowJavascriptExecution;

150 151 152 153 154 155 156 157
// Scope in which javascript execution leads to exception being thrown.
typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, false>
    ThrowOnJavascriptExecution;

// Scope to introduce an exception to ThrowOnJavascriptExecution.
typedef PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, true>
    NoThrowOnJavascriptExecution;

158 159 160 161 162 163 164 165
// Scope to document where we do not expect deoptimization.
typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, false>
    DisallowDeoptimization;

// Scope to introduce an exception to DisallowDeoptimization.
typedef PerIsolateAssertScopeDebugOnly<DEOPTIMIZATION_ASSERT, true>
    AllowDeoptimization;

166 167 168 169 170 171 172
// Scope to document where we do not expect deoptimization.
typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, false>
    DisallowCompilation;

// Scope to introduce an exception to DisallowDeoptimization.
typedef PerIsolateAssertScopeDebugOnly<COMPILATION_ASSERT, true>
    AllowCompilation;
173 174
}  // namespace internal
}  // namespace v8
175 176

#endif  // V8_ASSERT_SCOPE_H_