assert-scope.h 5.41 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
  ALLOCATION_FAILURE_ASSERT,
33 34
  DEOPTIMIZATION_ASSERT,
  COMPILATION_ASSERT
35 36 37
};


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

44
  static bool IsAllowed();
45

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

  DISALLOW_COPY_AND_ASSIGN(PerThreadAssertScope);
};


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

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

 private:
63
  class DataBit;
64 65

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

  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) { }
95 96 97
#endif
};

98 99
// Per-thread assert scopes.

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

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

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

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

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

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

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

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

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

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

140 141 142 143 144 145 146 147 148 149 150

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

151 152 153 154 155 156 157 158
// 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;

159 160 161 162 163 164 165 166
// Scope to document where we do not expect an allocation failure.
typedef PerIsolateAssertScopeDebugOnly<ALLOCATION_FAILURE_ASSERT, false>
    DisallowAllocationFailure;

// Scope to introduce an exception to DisallowAllocationFailure.
typedef PerIsolateAssertScopeDebugOnly<ALLOCATION_FAILURE_ASSERT, true>
    AllowAllocationFailure;

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

175 176 177 178 179 180 181
// 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;
182 183 184
} }  // namespace v8::internal

#endif  // V8_ASSERT_SCOPE_H_