1 // Copyright 2017 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_DATA_OBJECT_BUILDER_H_
6 #define GIN_DATA_OBJECT_BUILDER_H_
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/strings/string_piece.h"
13 #include "gin/converter.h"
14 #include "gin/gin_export.h"
15 #include "v8/include/v8.h"
19 // Constructs a JavaScript object with a series of data properties.
20 // (As with default data properties in JavaScript, these properties are
21 // configurable, writable and enumerable.)
23 // Values are automatically converted using gin::Converter, though if
24 // using a type where the conversion may fail, callers must convert ahead of
27 // This class avoids the pitfall of using v8::Object::Set, which may invoke
28 // setters on the object prototype.
31 // v8::Local<v8::Object> object = gin::DataObjectBuilder(isolate)
32 // .Set("boolean", true)
33 // .Set("integer", 42)
36 // Because this builder class contains local handles, callers must ensure it
37 // does not outlive the scope in which it is created.
38 class GIN_EXPORT DataObjectBuilder {
40 explicit DataObjectBuilder(v8::Isolate* isolate);
43 DataObjectBuilder& Set(base::StringPiece key, T&& value) {
44 DCHECK(!object_.IsEmpty());
45 v8::Local<v8::String> v8_key = StringToSymbol(isolate_, key);
46 v8::Local<v8::Value> v8_value =
47 ConvertToV8(isolate_, std::forward<T>(value));
48 CHECK(object_->CreateDataProperty(context_, v8_key, v8_value).ToChecked());
53 DataObjectBuilder& Set(uint32_t index, T&& value) {
54 DCHECK(!object_.IsEmpty());
55 v8::Local<v8::Value> v8_value =
56 ConvertToV8(isolate_, std::forward<T>(value));
57 CHECK(object_->CreateDataProperty(context_, index, v8_value).ToChecked());
61 v8::Local<v8::Object> Build() {
62 DCHECK(!object_.IsEmpty());
63 v8::Local<v8::Object> result = object_;
69 v8::Isolate* isolate_;
70 v8::Local<v8::Context> context_;
71 v8::Local<v8::Object> object_;
73 DISALLOW_COPY_AND_ASSIGN(DataObjectBuilder);
78 #endif // GIN_DATA_OBJECT_BUILDER_H_