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 "v8/include/v8.h" | 10 #include "v8/include/v8.h" |
11 | 11 |
12 namespace extensions { | 12 namespace extensions { |
13 namespace v8_helpers { | 13 namespace v8_helpers { |
14 | 14 |
15 // Helper functions for V8 APIs. | 15 // Helper functions for V8 APIs. |
16 | 16 |
17 // Converts |str| to a V8 string. Returns true on success. | 17 // Converts |str| to a V8 string. Returns true on success. |
18 inline bool ToV8String(v8::Isolate* isolate, | 18 inline bool ToV8String(v8::Isolate* isolate, |
19 const char* str, | 19 const char* str, |
20 v8::Local<v8::String>* out) { | 20 v8::Local<v8::String>* out) { |
21 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) | 21 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) |
22 .ToLocal(out); | 22 .ToLocal(out); |
23 } | 23 } |
24 | 24 |
| 25 inline bool ToV8String(v8::Isolate* isolate, |
| 26 const std::string& str, |
| 27 v8::Local<v8::String>* out) { |
| 28 return ToV8String(isolate, str.c_str(), out); |
| 29 } |
| 30 |
25 // Converts |str| to a V8 string. | 31 // Converts |str| to a V8 string. |
26 // This crashes when strlen(str) > v8::String::kMaxLength. | 32 // This crashes when strlen(str) > v8::String::kMaxLength. |
27 inline v8::Local<v8::String> ToV8StringUnsafe( | 33 inline v8::Local<v8::String> ToV8StringUnsafe( |
28 v8::Isolate* isolate, | 34 v8::Isolate* isolate, |
29 const char* str, | 35 const char* str, |
30 v8::NewStringType string_type = v8::NewStringType::kNormal) { | 36 v8::NewStringType string_type = v8::NewStringType::kNormal) { |
31 DCHECK(strlen(str) <= v8::String::kMaxLength); | 37 DCHECK(strlen(str) <= v8::String::kMaxLength); |
32 return v8::String::NewFromUtf8(isolate, str, string_type) | 38 return v8::String::NewFromUtf8(isolate, str, string_type) |
33 .ToLocalChecked(); | 39 .ToLocalChecked(); |
34 } | 40 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 inline v8::Local<v8::Value> GetPropertyUnsafe( | 99 inline v8::Local<v8::Value> GetPropertyUnsafe( |
94 v8::Local<v8::Context> context, | 100 v8::Local<v8::Context> context, |
95 v8::Local<v8::Object> object, | 101 v8::Local<v8::Object> object, |
96 const char* key, | 102 const char* key, |
97 v8::NewStringType string_type = v8::NewStringType::kNormal) { | 103 v8::NewStringType string_type = v8::NewStringType::kNormal) { |
98 return object->Get(context, | 104 return object->Get(context, |
99 ToV8StringUnsafe(context->GetIsolate(), key, string_type)) | 105 ToV8StringUnsafe(context->GetIsolate(), key, string_type)) |
100 .ToLocalChecked(); | 106 .ToLocalChecked(); |
101 } | 107 } |
102 | 108 |
| 109 // Wraps v8::Function::Call(). Returns true on success. |
| 110 inline bool CallFunction(v8::Local<v8::Context> context, |
| 111 v8::Local<v8::Function> function, |
| 112 v8::Local<v8::Value> recv, |
| 113 int argc, |
| 114 v8::Local<v8::Value> argv[], |
| 115 v8::Local<v8::Value>* out) { |
| 116 return function->Call(context, recv, argc, argv).ToLocal(out); |
| 117 } |
| 118 |
103 } // namespace v8_helpers | 119 } // namespace v8_helpers |
104 } // namespace extensions | 120 } // namespace extensions |
105 | 121 |
106 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ | 122 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ |
OLD | NEW |