Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #ifndef GIN_DATA_OBJECT_BUILDER_H_ | |
| 6 #define GIN_DATA_OBJECT_BUILDER_H_ | |
| 7 | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/strings/string_piece.h" | |
| 11 #include "gin/converter.h" | |
| 12 #include "gin/gin_export.h" | |
| 13 #include "v8/include/v8.h" | |
| 14 | |
| 15 namespace gin { | |
| 16 | |
| 17 // Constructs a JavaScript object with a series of data properties. | |
|
Devlin
2017/04/26 15:50:31
Optional: Maybe worth describing that the new prop
jbroman
2017/04/26 17:47:31
Done.
| |
| 18 // Values are automatically converted using gin::Converter. | |
| 19 // This class avoids the pitfall of using Set, which may invoke setters on the | |
|
Devlin
2017/04/26 15:50:31
nit: maybe specify v8::Object::Set()?
jbroman
2017/04/26 17:47:31
Done.
| |
| 20 // object prototype. | |
| 21 // | |
| 22 // Expected usage: | |
| 23 // v8::Local<v8::Object> object = gin::DataObjectBuilder(isolate) | |
| 24 // .Set("boolean", true) | |
| 25 // .Set("integer", 42) | |
| 26 // .Finish(); | |
| 27 // | |
| 28 // Because this builder class contains local handles, callers must ensure it | |
| 29 // does not outlive the scope in which it is created. | |
| 30 class GIN_EXPORT DataObjectBuilder { | |
| 31 public: | |
| 32 explicit DataObjectBuilder(v8::Isolate* isolate); | |
| 33 | |
| 34 template <typename T> | |
| 35 DataObjectBuilder& Set(base::StringPiece key, T&& value) { | |
|
Devlin
2017/04/26 15:50:31
Note: this won't work for std::vectors or potentia
jbroman
2017/04/26 17:47:31
Yeah, I waffled on this a little. It is easy enoug
Devlin
2017/04/26 18:30:23
Punting to callers sgtm.
| |
| 36 v8::Local<v8::String> v8_key = StringToSymbol(isolate_, key); | |
|
Devlin
2017/04/26 15:50:31
DCHECK(!object_.IsEmpty()) (enforcing single-use)?
jbroman
2017/04/26 17:47:31
Could do. It will crash anyways when it's derefere
Devlin
2017/04/26 18:30:23
DCHECK gives a nice stack trace. nullptr access g
jbroman
2017/04/27 15:26:30
Done.
| |
| 37 v8::Local<v8::Value> v8_value = | |
| 38 ConvertToV8(isolate_, std::forward<T>(value)); | |
| 39 CHECK(object_->CreateDataProperty(context_, v8_key, v8_value).ToChecked()); | |
| 40 return *this; | |
|
Devlin
2017/04/26 15:50:31
over the course of <n> template specializations, t
jbroman
2017/04/26 17:47:31
As you note the ConvertToV8 call still is inline,
| |
| 41 } | |
| 42 | |
| 43 v8::Local<v8::Object> Finish() { | |
|
Devlin
2017/04/26 15:50:31
is it worth defining this inline?
jbroman
2017/04/26 17:47:31
Not needed. My Blink-side habits tend to favour in
| |
| 44 v8::Local<v8::Object> result = object_; | |
|
Devlin
2017/04/26 15:50:31
here, too, maybe DCHECK(!object_.IsEmpty())?
jbroman
2017/04/26 17:47:31
I could. It takes away my ability to test it (beca
Devlin
2017/04/26 18:30:23
IMO, DCHECK is better than safely handling a resul
| |
| 45 object_.Clear(); | |
| 46 return result; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 v8::Isolate* isolate_; | |
| 51 v8::Local<v8::Context> context_; | |
| 52 v8::Local<v8::Object> object_; | |
|
Devlin
2017/04/26 15:50:32
DISALLOW_COPY_AND_ASSIGN()
jbroman
2017/04/26 17:47:31
Done.
| |
| 53 }; | |
| 54 | |
| 55 } // namespace gin | |
| 56 | |
| 57 #endif // GIN_DATA_OBJECT_BUILDER_H_ | |
| OLD | NEW |