OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ | |
6 #define EXTENSIONS_RENDERER_V8_HELPERS_H_ | |
7 | |
8 #include <string.h> | |
9 | |
10 #include "v8/include/v8.h" | |
11 | |
12 namespace extensions { | |
13 namespace v8_helpers { | |
14 | |
15 // Helper functions for V8 APIs. | |
16 | |
17 // Converts |str| to a V8 string. Returns true on success. | |
18 inline bool ToV8String(v8::Isolate* isolate, | |
19 const char* str, | |
20 v8::Local<v8::String>* out) { | |
21 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) | |
22 .ToLocal(out); | |
23 } | |
24 | |
25 // Converts |str| to a V8 string. | |
26 // This crashes when strlen(str) > v8::String::kMaxLength. | |
27 inline v8::Local<v8::String> ToV8StringUnsafe( | |
28 v8::Isolate* isolate, | |
29 const char* str, | |
30 v8::NewStringType string_type = v8::NewStringType::kNormal) { | |
31 DCHECK(strlen(str) <= v8::String::kMaxLength); | |
32 return v8::String::NewFromUtf8(isolate, str, string_type) | |
33 .ToLocalChecked(); | |
34 } | |
35 | |
36 // Returns true if |maybe| is both a value, and that value is true. | |
37 inline bool IsTrue(v8::Maybe<bool> maybe) { | |
38 return maybe.IsJust() && maybe.FromJust(); | |
39 } | |
40 | |
41 // SetProperty() family wraps V8::Object::Set(). Returns true on success. | |
42 inline bool SetProperty(v8::Local<v8::Context> context, | |
43 v8::Local<v8::Object> object, | |
44 v8::Local<v8::Value> key, | |
45 v8::Local<v8::Value> value) { | |
46 return IsTrue(object->Set(context, key, value)); | |
47 } | |
48 | |
49 inline bool SetProperty(v8::Local<v8::Context> context, | |
50 v8::Local<v8::Object> object, | |
51 uint32_t index, | |
52 v8::Local<v8::Value> value) { | |
53 return IsTrue(object->Set(context, index, value)); | |
54 } | |
55 | |
56 inline bool SetProperty(v8::Local<v8::Context> context, | |
57 v8::Local<v8::Object> object, | |
58 const char* key, | |
59 v8::Local<v8::Value> value) { | |
60 v8::Local<v8::String> v8_key; | |
61 if (!ToV8String(context->GetIsolate(), key, &v8_key)) | |
62 return false; | |
63 return SetProperty(context, object, v8_key, value); | |
64 } | |
65 | |
66 // GetProperty() family calls V8::Object::Get() and extracts a value from | |
67 // returned MaybeLocal. Returns true on success. | |
68 inline bool GetProperty(v8::Local<v8::Context> context, | |
69 v8::Local<v8::Object> object, | |
70 v8::Local<v8::Value> key, | |
71 v8::Local<v8::Value>* out) { | |
72 return object->Get(context, key).ToLocal(out); | |
73 } | |
74 | |
75 inline bool GetProperty(v8::Local<v8::Context> context, | |
76 v8::Local<v8::Object> object, | |
77 const char* key, | |
78 v8::Local<v8::Value>* out) { | |
79 v8::Local<v8::String> v8_key; | |
80 if (!ToV8String(context->GetIsolate(), key, &v8_key)) | |
81 return false; | |
82 return GetProperty(context, object, v8_key, out); | |
83 } | |
84 | |
85 // GetPropertyUnsafe() family Wraps v8::Object::Get(). Thye crash when an | |
not at google - send to devlin
2015/06/17 18:25:15
They
bashi
2015/06/18 22:58:33
Done.
| |
86 // exception is thrown. | |
87 inline v8::Local<v8::Value> GetPropertyUnsafe(v8::Local<v8::Context> context, | |
88 v8::Local<v8::Object> object, | |
89 v8::Local<v8::Value> key) { | |
90 return object->Get(context, key).ToLocalChecked(); | |
91 } | |
92 | |
93 inline v8::Local<v8::Value> GetPropertyUnsafe( | |
94 v8::Local<v8::Context> context, | |
95 v8::Local<v8::Object> object, | |
96 const char* key, | |
97 v8::NewStringType string_type = v8::NewStringType::kNormal) { | |
98 return object->Get(context, | |
99 ToV8StringUnsafe(context->GetIsolate(), key, string_type)) | |
100 .ToLocalChecked(); | |
101 } | |
102 | |
103 } // namespace v8_helpers | |
104 } // namespace extensions | |
105 | |
106 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ | |
OLD | NEW |