blob: cfaf4fb6eb68084ccad4051c9627ffd2da91473a [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_HANDLES_INL_H_
6#define V8_HANDLES_INL_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/api.h"
9#include "src/handles.h"
10#include "src/heap/heap.h"
11#include "src/isolate.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000012
13namespace v8 {
14namespace internal {
15
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016HandleBase::HandleBase(Object* object, Isolate* isolate)
17 : location_(HandleScope::GetHandle(isolate, object)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018
19
20template <typename T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021// Allocate a new handle for the object, do not canonicalize.
22Handle<T> Handle<T>::New(T* object, Isolate* isolate) {
23 return Handle(
24 reinterpret_cast<T**>(HandleScope::CreateHandle(isolate, object)));
Steve Blocka7e24c12009-10-30 11:49:00 +000025}
26
27
Steve Block44f0eee2011-05-26 01:26:41 +010028HandleScope::HandleScope(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029 HandleScopeData* data = isolate->handle_scope_data();
Steve Block44f0eee2011-05-26 01:26:41 +010030 isolate_ = isolate;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 prev_next_ = data->next;
32 prev_limit_ = data->limit;
33 data->level++;
34}
35
36
37template <typename T>
38inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) {
39 return os << Brief(*handle);
Steve Block44f0eee2011-05-26 01:26:41 +010040}
41
42
43HandleScope::~HandleScope() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044#ifdef DEBUG
45 if (FLAG_check_handle_count) {
46 int before = NumberOfHandles(isolate_);
47 CloseScope(isolate_, prev_next_, prev_limit_);
48 int after = NumberOfHandles(isolate_);
49 DCHECK(after - before < kCheckHandleThreshold);
50 DCHECK(before < kCheckHandleThreshold);
51 } else {
52#endif // DEBUG
53 CloseScope(isolate_, prev_next_, prev_limit_);
54#ifdef DEBUG
55 }
56#endif // DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +010057}
58
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059
60void HandleScope::CloseScope(Isolate* isolate,
61 Object** prev_next,
62 Object** prev_limit) {
63 HandleScopeData* current = isolate->handle_scope_data();
64
65 std::swap(current->next, prev_next);
Steve Block44f0eee2011-05-26 01:26:41 +010066 current->level--;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 if (current->limit != prev_limit) {
68 current->limit = prev_limit;
69 DeleteExtensions(isolate);
70#ifdef ENABLE_HANDLE_ZAPPING
71 ZapRange(current->next, prev_limit);
72 } else {
73 ZapRange(current->next, prev_next);
Steve Block44f0eee2011-05-26 01:26:41 +010074#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 }
Steve Block44f0eee2011-05-26 01:26:41 +010076}
77
78
79template <typename T>
80Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 HandleScopeData* current = isolate_->handle_scope_data();
82
Steve Block44f0eee2011-05-26 01:26:41 +010083 T* value = *handle_value;
84 // Throw away all handles in the current scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 CloseScope(isolate_, prev_next_, prev_limit_);
Steve Block44f0eee2011-05-26 01:26:41 +010086 // Allocate one handle in the parent scope.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 DCHECK(current->level > current->sealed_level);
88 Handle<T> result(value, isolate_);
Steve Block44f0eee2011-05-26 01:26:41 +010089 // Reinitialize the current scope (so that it's ready
90 // to be used or closed again).
91 prev_next_ = current->next;
92 prev_limit_ = current->limit;
93 current->level++;
94 return result;
95}
96
97
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 DCHECK(AllowHandleAllocation::IsAllowed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100 HandleScopeData* data = isolate->handle_scope_data();
Steve Block44f0eee2011-05-26 01:26:41 +0100101
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 Object** result = data->next;
103 if (result == data->limit) result = Extend(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100104 // Update the current next field, set the value in the created
105 // handle, and return the result.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 DCHECK(result < data->limit);
107 data->next = result + 1;
Steve Block44f0eee2011-05-26 01:26:41 +0100108
Steve Block44f0eee2011-05-26 01:26:41 +0100109 *result = value;
110 return result;
111}
112
113
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114Object** HandleScope::GetHandle(Isolate* isolate, Object* value) {
115 DCHECK(AllowHandleAllocation::IsAllowed());
116 HandleScopeData* data = isolate->handle_scope_data();
117 CanonicalHandleScope* canonical = data->canonical_scope;
118 return canonical ? canonical->Lookup(value) : CreateHandle(isolate, value);
119}
120
121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) {
124 // Make sure the current thread is allowed to create handles to begin with.
125 CHECK(AllowHandleAllocation::IsAllowed());
126 HandleScopeData* current = isolate_->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 // Shrink the current handle scope to make it impossible to do
128 // handle allocations without an explicit handle scope.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 prev_limit_ = current->limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 current->limit = current->next;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131 prev_sealed_level_ = current->sealed_level;
132 current->sealed_level = current->level;
Steve Blocka7e24c12009-10-30 11:49:00 +0000133}
134
135
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136inline SealHandleScope::~SealHandleScope() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 // Restore state in current handle scope to re-enable handle
138 // allocations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 HandleScopeData* current = isolate_->handle_scope_data();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 DCHECK_EQ(current->next, current->limit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 current->limit = prev_limit_;
142 DCHECK_EQ(current->level, current->sealed_level);
143 current->sealed_level = prev_sealed_level_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000144}
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148} // namespace internal
149} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000150
151#endif // V8_HANDLES_INL_H_