Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: runtime/vm/dart_api_state.h

Issue 8446009: Add Dart_Null, Dart_True, and Dart_False. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/dart_api_state.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef VM_DART_API_STATE_H_ 5 #ifndef VM_DART_API_STATE_H_
6 #define VM_DART_API_STATE_H_ 6 #define VM_DART_API_STATE_H_
7 7
8 #include "include/dart_api.h" 8 #include "include/dart_api.h"
9 9
10 #include "vm/dart_api_impl.h" 10 #include "vm/dart_api_impl.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 private: 65 private:
66 LocalHandle() { } 66 LocalHandle() { }
67 ~LocalHandle() { } 67 ~LocalHandle() { }
68 68
69 RawObject* raw_; 69 RawObject* raw_;
70 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 70 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
71 DISALLOW_COPY_AND_ASSIGN(LocalHandle); 71 DISALLOW_COPY_AND_ASSIGN(LocalHandle);
72 }; 72 };
73 73
74 74
75 // A distinguished callback which indicates that a persistent handle
76 // should not be deleted from the dart api.
77 void ProtectedHandleCallback();
78
79
75 // Implementation of persistent handles which are handed out through the 80 // Implementation of persistent handles which are handed out through the
76 // dart API. 81 // dart API.
77 class PersistentHandle { 82 class PersistentHandle {
78 public: 83 public:
79 enum { 84 enum {
80 StrongReference = 0, 85 StrongReference = 0,
81 WeakReference, 86 WeakReference,
82 }; 87 };
83 88
84 // Accessors. 89 // Accessors.
85 RawObject* raw() const { return raw_; } 90 RawObject* raw() const { return raw_; }
86 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } 91 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); }
87 void set_raw(const Object& object) { raw_ = object.raw(); } 92 void set_raw(const Object& object) { raw_ = object.raw(); }
88 static intptr_t raw_offset() { return OFFSET_OF(PersistentHandle, raw_); } 93 static intptr_t raw_offset() { return OFFSET_OF(PersistentHandle, raw_); }
89 void* callback() const { return callback_; } 94 void* callback() const { return callback_; }
90 void set_callback(void* value) { callback_ = value; } 95 void set_callback(void* value) { callback_ = value; }
91 intptr_t type() const { return type_; } 96 intptr_t type() const { return type_; }
92 void set_type(intptr_t value) { type_ = value; } 97 void set_type(intptr_t value) { type_ = value; }
93 98
99 // Some handles are protected from being freed via the external dart api.
100 bool IsProtected() {
101 return callback() == &ProtectedHandleCallback;
102 }
103
94 private: 104 private:
95 friend class PersistentHandles; 105 friend class PersistentHandles;
96 106
97 PersistentHandle() { } 107 PersistentHandle() { }
98 ~PersistentHandle() { } 108 ~PersistentHandle() { }
99 109
100 // Overload the callback_ field as a next pointer when adding freed 110 // Overload the callback_ field as a next pointer when adding freed
101 // handles to the free list. 111 // handles to the free list.
102 PersistentHandle* Next() { 112 PersistentHandle* Next() {
103 return reinterpret_cast<PersistentHandle*>(callback_); 113 return reinterpret_cast<PersistentHandle*>(callback_);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 260
251 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope); 261 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope);
252 }; 262 };
253 263
254 264
255 // Implementation of the API State used in dart api for maintaining 265 // Implementation of the API State used in dart api for maintaining
256 // local scopes, persistent handles etc. These are setup on a per isolate 266 // local scopes, persistent handles etc. These are setup on a per isolate
257 // basis and destroyed when the isolate is shutdown. 267 // basis and destroyed when the isolate is shutdown.
258 class ApiState { 268 class ApiState {
259 public: 269 public:
260 ApiState() : top_scope_(NULL), true_(NULL) { } 270 ApiState() : top_scope_(NULL), null_(NULL), true_(NULL), false_(NULL) { }
261 ~ApiState() { 271 ~ApiState() {
262 while (top_scope_ != NULL) { 272 while (top_scope_ != NULL) {
263 ApiLocalScope* scope = top_scope_; 273 ApiLocalScope* scope = top_scope_;
264 top_scope_ = top_scope_->previous(); 274 top_scope_ = top_scope_->previous();
265 delete scope; 275 delete scope;
266 } 276 }
277 if (null_ != NULL) {
278 persistent_handles().FreeHandle(null_);
279 null_ = NULL;
280 }
267 if (true_ != NULL) { 281 if (true_ != NULL) {
268 persistent_handles().FreeHandle(true_); 282 persistent_handles().FreeHandle(true_);
269 true_ = NULL; 283 true_ = NULL;
270 } 284 }
285 if (false_ != NULL) {
286 persistent_handles().FreeHandle(false_);
287 false_ = NULL;
288 }
271 } 289 }
272 290
273 // Accessors. 291 // Accessors.
274 ApiLocalScope* top_scope() const { return top_scope_; } 292 ApiLocalScope* top_scope() const { return top_scope_; }
275 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } 293 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; }
276 PersistentHandles& persistent_handles() { return persistent_handles_; } 294 PersistentHandles& persistent_handles() { return persistent_handles_; }
277 295
278 void UnwindScopes(uword sp) { 296 void UnwindScopes(uword sp) {
279 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { 297 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) {
280 ApiLocalScope* scope = top_scope_; 298 ApiLocalScope* scope = top_scope_;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 } 338 }
321 int ZoneSizeInBytes() const { 339 int ZoneSizeInBytes() const {
322 int total = 0; 340 int total = 0;
323 ApiLocalScope* scope = top_scope_; 341 ApiLocalScope* scope = top_scope_;
324 while (scope != NULL) { 342 while (scope != NULL) {
325 total += scope->zone().SizeInBytes(); 343 total += scope->zone().SizeInBytes();
326 scope = scope->previous(); 344 scope = scope->previous();
327 } 345 }
328 return total; 346 return total;
329 } 347 }
348 PersistentHandle* Null() {
349 if (null_ == NULL) {
350 Zone zone; // Setup a VM zone as we are creating some handles.
351 HandleScope scope; // Setup a VM handle scope.
352
353 Object& null_object = Object::Handle();
354 null_ = persistent_handles().AllocateHandle();
355 null_->set_raw(null_object);
356 null_->set_callback(reinterpret_cast<void*>(&ProtectedHandleCallback));
357 }
358 return null_;
359 }
330 PersistentHandle* True() { 360 PersistentHandle* True() {
331 if (true_ == NULL) { 361 if (true_ == NULL) {
332 Zone zone; // Setup a VM zone as we are creating some handles. 362 Zone zone; // Setup a VM zone as we are creating some handles.
333 HandleScope scope; // Setup a VM handle scope. 363 HandleScope scope; // Setup a VM handle scope.
334 364
335 const Object& true_object = Object::Handle(Bool::True()); 365 const Object& true_object = Object::Handle(Bool::True());
336 true_ = persistent_handles().AllocateHandle(); 366 true_ = persistent_handles().AllocateHandle();
337 true_->set_raw(true_object); 367 true_->set_raw(true_object);
368 true_->set_callback(reinterpret_cast<void*>(&ProtectedHandleCallback));
338 } 369 }
339 return true_; 370 return true_;
340 } 371 }
372 PersistentHandle* False() {
373 if (false_ == NULL) {
374 Zone zone; // Setup a VM zone as we are creating some handles.
375 HandleScope scope; // Setup a VM handle scope.
376
377 const Object& false_object = Object::Handle(Bool::False());
378 false_ = persistent_handles().AllocateHandle();
379 false_->set_raw(false_object);
380 false_->set_callback(reinterpret_cast<void*>(&ProtectedHandleCallback));
381 }
382 return false_;
383 }
341 384
342 private: 385 private:
343 PersistentHandles persistent_handles_; 386 PersistentHandles persistent_handles_;
344 ApiLocalScope* top_scope_; 387 ApiLocalScope* top_scope_;
345 388
346 // A persistent handle to the "True" object. 389 // Persistent handles to important objects.
390 PersistentHandle* null_;
347 PersistentHandle* true_; 391 PersistentHandle* true_;
392 PersistentHandle* false_;
348 393
349 DISALLOW_COPY_AND_ASSIGN(ApiState); 394 DISALLOW_COPY_AND_ASSIGN(ApiState);
350 }; 395 };
351 396
352 } // namespace dart 397 } // namespace dart
353 398
354 #endif // VM_DART_API_STATE_H_ 399 #endif // VM_DART_API_STATE_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/dart_api_state.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698