assert-scope.h 5.86 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
#include "src/globals.h"
11 12 13 14

namespace v8 {
namespace internal {

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

19 20 21 22 23 24

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

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

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

43
  V8_EXPORT_PRIVATE static bool IsAllowed();
44

45 46
  void Release();

47
 private:
48
  PerThreadAssertData* data_;
49
  bool old_state_;
50 51 52 53 54 55

  DISALLOW_COPY_AND_ASSIGN(PerThreadAssertScope);
};


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

61
  static bool IsAllowed(Isolate* isolate);
62 63

 private:
64
  class DataBit;
65 66

  Isolate* isolate_;
67
  uint32_t old_data_;
68 69 70 71 72 73 74 75 76 77 78 79 80

  DISALLOW_COPY_AND_ASSIGN(PerIsolateAssertScope);
};


template <PerThreadAssertType type, bool allow>
#ifdef DEBUG
class PerThreadAssertScopeDebugOnly : public
    PerThreadAssertScope<type, allow> {
#else
class PerThreadAssertScopeDebugOnly {
 public:
  PerThreadAssertScopeDebugOnly() { }
81
  void Release() {}
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#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) { }
97 98 99
#endif
};

100 101
// Per-thread assert scopes.

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

// Scope to introduce an exception to DisallowHandleAllocation.
107
typedef PerThreadAssertScopeDebugOnly<HANDLE_ALLOCATION_ASSERT, true>
108 109 110
    AllowHandleAllocation;

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

// Scope to introduce an exception to DisallowHeapAllocation.
115
typedef PerThreadAssertScopeDebugOnly<HEAP_ALLOCATION_ASSERT, true>
116 117 118
    AllowHeapAllocation;

// Scope to document where we do not expect any handle dereferences.
119
typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, false>
120 121 122
    DisallowHandleDereference;

// Scope to introduce an exception to DisallowHandleDereference.
123
typedef PerThreadAssertScopeDebugOnly<HANDLE_DEREFERENCE_ASSERT, true>
124 125 126
    AllowHandleDereference;

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

// Scope to introduce an exception to DisallowDeferredHandleDereference.
131
typedef PerThreadAssertScopeDebugOnly<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>
132 133
    AllowDeferredHandleDereference;

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

// Scope to introduce an exception to DisallowDeferredHandleDereference.
139
typedef PerThreadAssertScopeDebugOnly<CODE_DEPENDENCY_CHANGE_ASSERT, true>
140 141
    AllowCodeDependencyChange;

142 143 144 145 146 147 148 149 150 151 152

// 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;

153 154 155 156 157 158 159 160
// Scope to document where we do not expect javascript execution (debug only)
typedef PerIsolateAssertScopeDebugOnly<JAVASCRIPT_EXECUTION_ASSERT, false>
    DisallowJavascriptExecutionDebugOnly;

// Scope to introduce an exception to DisallowJavascriptExecutionDebugOnly.
typedef PerIsolateAssertScopeDebugOnly<JAVASCRIPT_EXECUTION_ASSERT, true>
    AllowJavascriptExecutionDebugOnly;

161 162 163 164 165 166 167 168
// 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;

169 170 171 172 173 174 175 176
// 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;

177 178 179 180 181 182 183
// 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;
184 185 186 187 188 189 190 191

// Scope to document where we do not expect exceptions.
typedef PerIsolateAssertScopeDebugOnly<NO_EXCEPTION_ASSERT, false>
    DisallowExceptions;

// Scope to introduce an exception to DisallowExceptions.
typedef PerIsolateAssertScopeDebugOnly<NO_EXCEPTION_ASSERT, true>
    AllowExceptions;
192 193
}  // namespace internal
}  // namespace v8
194 195

#endif  // V8_ASSERT_SCOPE_H_