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.
5 #ifndef GIN_DICTIONARY_H_
6 #define GIN_DICTIONARY_H_
8 #include "gin/converter.h"
9 #include "gin/gin_export.h"
13 // Dictionary is useful when writing bindings for a function that either
14 // receives an arbitrary JavaScript object as an argument or returns an
15 // arbitrary JavaScript object as a result. For example, Dictionary is useful
16 // when you might use the |dictionary| type in WebIDL:
18 // http://heycam.github.io/webidl/#idl-dictionaries
20 // WARNING: You cannot retain a Dictionary object in the heap. The underlying
21 // storage for Dictionary is tied to the closest enclosing
22 // v8::HandleScope. Generally speaking, you should store a Dictionary
25 class GIN_EXPORT Dictionary
{
27 explicit Dictionary(v8::Isolate
* isolate
);
28 Dictionary(v8::Isolate
* isolate
, v8::Local
<v8::Object
> object
);
31 static Dictionary
CreateEmpty(v8::Isolate
* isolate
);
34 bool Get(const std::string
& key
, T
* out
) {
35 v8::Local
<v8::Value
> val
;
36 if (!object_
->Get(isolate_
->GetCurrentContext(), StringToV8(isolate_
, key
))
40 return ConvertFromV8(isolate_
, val
, out
);
44 bool Set(const std::string
& key
, T val
) {
45 v8::Local
<v8::Value
> v8_value
;
46 if (!TryConvertToV8(isolate_
, val
, &v8_value
))
48 v8::Maybe
<bool> result
=
49 object_
->Set(isolate_
->GetCurrentContext(), StringToV8(isolate_
, key
),
51 return !result
.IsNothing() && result
.FromJust();
54 v8::Isolate
* isolate() const { return isolate_
; }
57 friend struct Converter
<Dictionary
>;
59 // TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get().
60 v8::Isolate
* isolate_
;
61 v8::Local
<v8::Object
> object_
;
65 struct GIN_EXPORT Converter
<Dictionary
> {
66 static v8::Local
<v8::Value
> ToV8(v8::Isolate
* isolate
,
68 static bool FromV8(v8::Isolate
* isolate
,
69 v8::Local
<v8::Value
> val
,
75 #endif // GIN_DICTIONARY_H_