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

Side by Side Diff: gin/wrappable.cc

Issue 79203004: [Gin] Add a mechanism for wrapping C++ object (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Disallow copy and assign Created 7 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 | « gin/wrappable.h ('k') | gin/wrappable_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gin/wrappable.h"
6
7 #include "base/logging.h"
8 #include "gin/per_isolate_data.h"
9
10 namespace gin {
11
12 Wrappable::Wrappable() {
13 }
14
15 Wrappable::~Wrappable() {
16 wrapper_.Reset();
17 }
18
19 void Wrappable::WeakCallback(
20 const v8::WeakCallbackData<v8::Object, Wrappable>& data) {
21 Wrappable* wrappable = data.GetParameter();
22 wrappable->wrapper_.Reset();
23 wrappable->Release(); // Balanced in Wrappable::ConfigureWrapper.
24 }
25
26 v8::Handle<v8::Object> Wrappable::CreateWrapper(v8::Isolate* isolate) {
27 WrapperInfo* info = GetWrapperInfo();
28 PerIsolateData* data = PerIsolateData::From(isolate);
29 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info);
30 CHECK(!templ.IsEmpty()); // Don't forget to register an object template.
31 CHECK(templ->InternalFieldCount() == kNumberOfInternalFields);
32 v8::Handle<v8::Object> wrapper = templ->NewInstance();
33 wrapper->SetAlignedPointerInInternalField(kWrapperInfoIndex, info);
34 wrapper->SetAlignedPointerInInternalField(kEncodedValueIndex, this);
35 wrapper_.Reset(isolate, wrapper);
36 AddRef(); // Balanced in Wrappable::WeakCallback.
37 wrapper_.SetWeak(this, WeakCallback);
38 return wrapper;
39 }
40
41 v8::Handle<v8::Value> Converter<Wrappable*>::ToV8(v8::Isolate* isolate,
42 Wrappable* val) {
43 if (val->wrapper_.IsEmpty())
44 return val->CreateWrapper(isolate);
45 return v8::Local<v8::Object>::New(isolate, val->wrapper_);
46 }
47
48 bool Converter<Wrappable*>::FromV8(v8::Handle<v8::Value> val,
49 Wrappable** out) {
50 if (!val->IsObject())
51 return false;
52 v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val);
53 WrapperInfo* info = WrapperInfo::From(obj);
54 if (!info)
55 return false;
56 void* pointer = obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
57 Wrappable* wrappable = static_cast<Wrappable*>(pointer);
58 CHECK(wrappable->GetWrapperInfo() == info); // Security check for cast above.
59 *out = wrappable;
60 return true;
61 }
62
63 } // namespace gin
OLDNEW
« no previous file with comments | « gin/wrappable.h ('k') | gin/wrappable_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698