Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef GIN_DICTIONARY_H_ |
| 6 | #define GIN_DICTIONARY_H_ |
| 7 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 9 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 10 | #include "gin/gin_export.h" |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 11 | |
| 12 | namespace gin { |
| 13 | |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 14 | // Dictionary is useful when writing bindings for a function that either |
| 15 | // receives an arbitrary JavaScript object as an argument or returns an |
| 16 | // arbitrary JavaScript object as a result. For example, Dictionary is useful |
| 17 | // when you might use the |dictionary| type in WebIDL: |
| 18 | // |
Raphael Kubo da Costa | 7b0b3a7 | 2021-10-18 13:05:43 | [diff] [blame] | 19 | // https://webidl.spec.whatwg.org/#idl-dictionaries |
[email protected] | 60531d5 | 2013-11-27 02:10:15 | [diff] [blame] | 20 | // |
| 21 | // WARNING: You cannot retain a Dictionary object in the heap. The underlying |
| 22 | // storage for Dictionary is tied to the closest enclosing |
| 23 | // v8::HandleScope. Generally speaking, you should store a Dictionary |
| 24 | // on the stack. |
| 25 | // |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 26 | class GIN_EXPORT Dictionary { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 27 | public: |
| 28 | explicit Dictionary(v8::Isolate* isolate); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 29 | Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object); |
vmpstr | 8ed6f86 | 2016-02-25 20:25:14 | [diff] [blame] | 30 | Dictionary(const Dictionary& other); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 31 | ~Dictionary(); |
| 32 | |
| 33 | static Dictionary CreateEmpty(v8::Isolate* isolate); |
| 34 | |
| 35 | template<typename T> |
| 36 | bool Get(const std::string& key, T* out) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 37 | v8::Local<v8::Value> val; |
| 38 | if (!object_->Get(isolate_->GetCurrentContext(), StringToV8(isolate_, key)) |
| 39 | .ToLocal(&val)) { |
| 40 | return false; |
| 41 | } |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 42 | return ConvertFromV8(isolate_, val, out); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 43 | } |
| 44 | |
Jeremy Apthorp | 6a348029 | 2018-04-26 18:53:59 | [diff] [blame] | 45 | template <typename T> |
| 46 | bool Set(const std::string& key, const T& val) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 47 | v8::Local<v8::Value> v8_value; |
| 48 | if (!TryConvertToV8(isolate_, val, &v8_value)) |
| 49 | return false; |
| 50 | v8::Maybe<bool> result = |
| 51 | object_->Set(isolate_->GetCurrentContext(), StringToV8(isolate_, key), |
| 52 | v8_value); |
| 53 | return !result.IsNothing() && result.FromJust(); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | v8::Isolate* isolate() const { return isolate_; } |
| 57 | |
| 58 | private: |
| 59 | friend struct Converter<Dictionary>; |
| 60 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 61 | // TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get(). |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 62 | raw_ptr<v8::Isolate> isolate_; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 63 | v8::Local<v8::Object> object_; |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 67 | struct GIN_EXPORT Converter<Dictionary> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 68 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 69 | Dictionary val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 70 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 71 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 72 | Dictionary* out); |
| 73 | }; |
| 74 | |
| 75 | } // namespace gin |
| 76 | |
| 77 | #endif // GIN_DICTIONARY_H_ |