blob: 6c3402f9868dc9f8fa2ce7a1a97331c6813db323 [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:011// Copyright 2013 The Chromium Authors
[email protected]e87f3122013-11-12 00:41:272// 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 Hattori0e45c022021-11-27 09:25:528#include "base/memory/raw_ptr.h"
[email protected]e87f3122013-11-12 00:41:279#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:3410#include "gin/gin_export.h"
[email protected]e87f3122013-11-12 00:41:2711
12namespace gin {
13
[email protected]60531d52013-11-27 02:10:1514// 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 Costa7b0b3a72021-10-18 13:05:4319// https://webidl.spec.whatwg.org/#idl-dictionaries
[email protected]60531d52013-11-27 02:10:1520//
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]48c21632013-12-12 21:32:3426class GIN_EXPORT Dictionary {
[email protected]e87f3122013-11-12 00:41:2727 public:
28 explicit Dictionary(v8::Isolate* isolate);
deepak.sfaaa1b62015-04-30 07:30:4829 Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object);
vmpstr8ed6f862016-02-25 20:25:1430 Dictionary(const Dictionary& other);
[email protected]e87f3122013-11-12 00:41:2731 ~Dictionary();
32
33 static Dictionary CreateEmpty(v8::Isolate* isolate);
34
35 template<typename T>
36 bool Get(const std::string& key, T* out) {
bashidbd2ef9bb2015-06-02 01:39:3237 v8::Local<v8::Value> val;
38 if (!object_->Get(isolate_->GetCurrentContext(), StringToV8(isolate_, key))
39 .ToLocal(&val)) {
40 return false;
41 }
[email protected]7618ebbb2013-11-27 03:38:2642 return ConvertFromV8(isolate_, val, out);
[email protected]e87f3122013-11-12 00:41:2743 }
44
Jeremy Apthorp6a3480292018-04-26 18:53:5945 template <typename T>
46 bool Set(const std::string& key, const T& val) {
bashidbd2ef9bb2015-06-02 01:39:3247 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]e87f3122013-11-12 00:41:2754 }
55
56 v8::Isolate* isolate() const { return isolate_; }
57
58 private:
59 friend struct Converter<Dictionary>;
60
[email protected]7618ebbb2013-11-27 03:38:2661 // TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get().
Keishi Hattori0e45c022021-11-27 09:25:5262 raw_ptr<v8::Isolate> isolate_;
deepak.sfaaa1b62015-04-30 07:30:4863 v8::Local<v8::Object> object_;
[email protected]e87f3122013-11-12 00:41:2764};
65
66template<>
[email protected]48c21632013-12-12 21:32:3467struct GIN_EXPORT Converter<Dictionary> {
deepak.sfaaa1b62015-04-30 07:30:4868 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
[email protected]e87f3122013-11-12 00:41:2769 Dictionary val);
[email protected]7618ebbb2013-11-27 03:38:2670 static bool FromV8(v8::Isolate* isolate,
deepak.sfaaa1b62015-04-30 07:30:4871 v8::Local<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2772 Dictionary* out);
73};
74
75} // namespace gin
76
77#endif // GIN_DICTIONARY_H_