// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ #define EXTENSIONS_RENDERER_V8_HELPERS_H_ #include #include "v8/include/v8.h" namespace extensions { namespace v8_helpers { // Helper functions for V8 APIs. // Converts |str| to a V8 string. Returns true on success. inline bool ToV8String(v8::Isolate* isolate, const char* str, v8::Local* out) { return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) .ToLocal(out); } inline bool ToV8String(v8::Isolate* isolate, const std::string& str, v8::Local* out) { return ToV8String(isolate, str.c_str(), out); } // Converts |str| to a V8 string. // This crashes when strlen(str) > v8::String::kMaxLength. inline v8::Local ToV8StringUnsafe( v8::Isolate* isolate, const char* str, v8::NewStringType string_type = v8::NewStringType::kNormal) { DCHECK(strlen(str) <= v8::String::kMaxLength); return v8::String::NewFromUtf8(isolate, str, string_type) .ToLocalChecked(); } inline v8::Local ToV8StringUnsafe( v8::Isolate* isolate, const std::string& str, v8::NewStringType string_type = v8::NewStringType::kNormal) { return ToV8StringUnsafe(isolate, str.c_str(), string_type); } // Returns true if |maybe| is both a value, and that value is true. inline bool IsTrue(v8::Maybe maybe) { return maybe.IsJust() && maybe.FromJust(); } // Returns true if |value| is empty or undefined. inline bool IsEmptyOrUndefied(v8::Local value) { return value.IsEmpty() || value->IsUndefined(); } // SetProperty() family wraps V8::Object::Set(). Returns true on success. inline bool SetProperty(v8::Local context, v8::Local object, v8::Local key, v8::Local value) { return IsTrue(object->Set(context, key, value)); } inline bool SetProperty(v8::Local context, v8::Local object, uint32_t index, v8::Local value) { return IsTrue(object->Set(context, index, value)); } inline bool SetProperty(v8::Local context, v8::Local object, const char* key, v8::Local value) { v8::Local v8_key; if (!ToV8String(context->GetIsolate(), key, &v8_key)) return false; return SetProperty(context, object, v8_key, value); } // GetProperty() family calls V8::Object::Get() and extracts a value from // returned MaybeLocal. Returns true on success. template inline bool GetProperty(v8::Local context, v8::Local object, Key key, v8::Local* out) { return object->Get(context, key).ToLocal(out); } inline bool GetProperty(v8::Local context, v8::Local object, const char* key, v8::Local* out) { v8::Local v8_key; if (!ToV8String(context->GetIsolate(), key, &v8_key)) return false; return GetProperty(context, object, v8_key, out); } // GetPropertyUnsafe() family wraps v8::Object::Get(). They crash when an // exception is thrown. inline v8::Local GetPropertyUnsafe(v8::Local context, v8::Local object, v8::Local key) { return object->Get(context, key).ToLocalChecked(); } inline v8::Local GetPropertyUnsafe( v8::Local context, v8::Local object, const char* key, v8::NewStringType string_type = v8::NewStringType::kNormal) { return object->Get(context, ToV8StringUnsafe(context->GetIsolate(), key, string_type)) .ToLocalChecked(); } // Wraps v8::Function::Call(). Returns true on success. inline bool CallFunction(v8::Local context, v8::Local function, v8::Local recv, int argc, v8::Local argv[], v8::Local* out) { return function->Call(context, recv, argc, argv).ToLocal(out); } } // namespace v8_helpers } // namespace extensions #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_