OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ | 5 #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ |
6 #define EXTENSIONS_RENDERER_V8_HELPERS_H_ | 6 #define EXTENSIONS_RENDERER_V8_HELPERS_H_ |
7 | 7 |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
| 10 #include "base/strings/string_number_conversions.h" |
10 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
11 | 12 |
12 namespace extensions { | 13 namespace extensions { |
13 namespace v8_helpers { | 14 namespace v8_helpers { |
14 | 15 |
15 // Helper functions for V8 APIs. | 16 // Helper functions for V8 APIs. |
16 | 17 |
17 // Converts |str| to a V8 string. Returns true on success. | 18 // Converts |str| to a V8 string. Returns true on success. |
18 inline bool ToV8String(v8::Isolate* isolate, | 19 inline bool ToV8String(v8::Isolate* isolate, |
19 const char* str, | 20 const char* str, |
(...skipping 29 matching lines...) Expand all Loading... |
49 // Returns true if |maybe| is both a value, and that value is true. | 50 // Returns true if |maybe| is both a value, and that value is true. |
50 inline bool IsTrue(v8::Maybe<bool> maybe) { | 51 inline bool IsTrue(v8::Maybe<bool> maybe) { |
51 return maybe.IsJust() && maybe.FromJust(); | 52 return maybe.IsJust() && maybe.FromJust(); |
52 } | 53 } |
53 | 54 |
54 // Returns true if |value| is empty or undefined. | 55 // Returns true if |value| is empty or undefined. |
55 inline bool IsEmptyOrUndefied(v8::Local<v8::Value> value) { | 56 inline bool IsEmptyOrUndefied(v8::Local<v8::Value> value) { |
56 return value.IsEmpty() || value->IsUndefined(); | 57 return value.IsEmpty() || value->IsUndefined(); |
57 } | 58 } |
58 | 59 |
59 // SetProperty() family wraps V8::Object::Set(). Returns true on success. | 60 // SetProperty() family wraps V8::Object::DefineOwnProperty(). |
| 61 // Returns true on success. |
60 inline bool SetProperty(v8::Local<v8::Context> context, | 62 inline bool SetProperty(v8::Local<v8::Context> context, |
61 v8::Local<v8::Object> object, | 63 v8::Local<v8::Object> object, |
62 v8::Local<v8::Value> key, | 64 v8::Local<v8::String> key, |
63 v8::Local<v8::Value> value) { | 65 v8::Local<v8::Value> value) { |
64 return IsTrue(object->Set(context, key, value)); | 66 return IsTrue(object->DefineOwnProperty(context, key, value)); |
65 } | 67 } |
66 | 68 |
67 inline bool SetProperty(v8::Local<v8::Context> context, | 69 inline bool SetProperty(v8::Local<v8::Context> context, |
68 v8::Local<v8::Object> object, | |
69 uint32_t index, | |
70 v8::Local<v8::Value> value) { | |
71 return IsTrue(object->Set(context, index, value)); | |
72 } | |
73 | |
74 inline bool SetProperty(v8::Local<v8::Context> context, | |
75 v8::Local<v8::Object> object, | 70 v8::Local<v8::Object> object, |
76 const char* key, | 71 const char* key, |
77 v8::Local<v8::Value> value) { | 72 v8::Local<v8::Value> value) { |
78 v8::Local<v8::String> v8_key; | 73 v8::Local<v8::String> v8_key; |
79 if (!ToV8String(context->GetIsolate(), key, &v8_key)) | 74 if (!ToV8String(context->GetIsolate(), key, &v8_key)) |
80 return false; | 75 return false; |
81 return SetProperty(context, object, v8_key, value); | 76 return SetProperty(context, object, v8_key, value); |
82 } | 77 } |
83 | 78 |
| 79 inline bool SetProperty(v8::Local<v8::Context> context, |
| 80 v8::Local<v8::Object> object, |
| 81 uint32_t index, |
| 82 v8::Local<v8::Value> value) { |
| 83 return SetProperty(context, object, base::UintToString(index).c_str(), value); |
| 84 } |
| 85 |
84 // GetProperty() family calls V8::Object::Get() and extracts a value from | 86 // GetProperty() family calls V8::Object::Get() and extracts a value from |
85 // returned MaybeLocal. Returns true on success. | 87 // returned MaybeLocal. Returns true on success. |
86 template <typename Key> | 88 template <typename Key> |
87 inline bool GetProperty(v8::Local<v8::Context> context, | 89 inline bool GetProperty(v8::Local<v8::Context> context, |
88 v8::Local<v8::Object> object, | 90 v8::Local<v8::Object> object, |
89 Key key, | 91 Key key, |
90 v8::Local<v8::Value>* out) { | 92 v8::Local<v8::Value>* out) { |
91 return object->Get(context, key).ToLocal(out); | 93 return object->Get(context, key).ToLocal(out); |
92 } | 94 } |
93 | 95 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 int argc, | 128 int argc, |
127 v8::Local<v8::Value> argv[], | 129 v8::Local<v8::Value> argv[], |
128 v8::Local<v8::Value>* out) { | 130 v8::Local<v8::Value>* out) { |
129 return function->Call(context, recv, argc, argv).ToLocal(out); | 131 return function->Call(context, recv, argc, argv).ToLocal(out); |
130 } | 132 } |
131 | 133 |
132 } // namespace v8_helpers | 134 } // namespace v8_helpers |
133 } // namespace extensions | 135 } // namespace extensions |
134 | 136 |
135 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ | 137 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ |
OLD | NEW |