diff options
author | bashi <bashi@chromium.org> | 2015-06-18 17:51:51 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-19 00:52:19 +0000 |
commit | 6a4854fa7f9c3156a6621f22e10567c7d7c7c0d1 (patch) | |
tree | 5ddb7b776ec207d9866a67884d7b751a7b4bf5f1 /extensions/renderer/v8_helpers.h | |
parent | 76de16b533909638d2f0eec8f8d632cd0f7ec6b6 (diff) | |
download | chromium_src-6a4854fa7f9c3156a6621f22e10567c7d7c7c0d1.zip chromium_src-6a4854fa7f9c3156a6621f22e10567c7d7c7c0d1.tar.gz chromium_src-6a4854fa7f9c3156a6621f22e10567c7d7c7c0d1.tar.bz2 |
extensions: Use V8 Maybe APIs in ModuleSystem
Replacing DEPRECATE_SOON APIs.
BUG=479065
Review URL: https://codereview.chromium.org/1185443004
Cr-Commit-Position: refs/heads/master@{#335181}
Diffstat (limited to 'extensions/renderer/v8_helpers.h')
-rw-r--r-- | extensions/renderer/v8_helpers.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/extensions/renderer/v8_helpers.h b/extensions/renderer/v8_helpers.h new file mode 100644 index 0000000..385490f --- /dev/null +++ b/extensions/renderer/v8_helpers.h @@ -0,0 +1,106 @@ +// 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 <string.h> + +#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<v8::String>* out) { + return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) + .ToLocal(out); +} + +// Converts |str| to a V8 string. +// This crashes when strlen(str) > v8::String::kMaxLength. +inline v8::Local<v8::String> 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(); +} + +// Returns true if |maybe| is both a value, and that value is true. +inline bool IsTrue(v8::Maybe<bool> maybe) { + return maybe.IsJust() && maybe.FromJust(); +} + +// SetProperty() family wraps V8::Object::Set(). Returns true on success. +inline bool SetProperty(v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + v8::Local<v8::Value> key, + v8::Local<v8::Value> value) { + return IsTrue(object->Set(context, key, value)); +} + +inline bool SetProperty(v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + uint32_t index, + v8::Local<v8::Value> value) { + return IsTrue(object->Set(context, index, value)); +} + +inline bool SetProperty(v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + const char* key, + v8::Local<v8::Value> value) { + v8::Local<v8::String> 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. +inline bool GetProperty(v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + v8::Local<v8::Value> key, + v8::Local<v8::Value>* out) { + return object->Get(context, key).ToLocal(out); +} + +inline bool GetProperty(v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + const char* key, + v8::Local<v8::Value>* out) { + v8::Local<v8::String> 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<v8::Value> GetPropertyUnsafe(v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + v8::Local<v8::Value> key) { + return object->Get(context, key).ToLocalChecked(); +} + +inline v8::Local<v8::Value> GetPropertyUnsafe( + v8::Local<v8::Context> context, + v8::Local<v8::Object> object, + const char* key, + v8::NewStringType string_type = v8::NewStringType::kNormal) { + return object->Get(context, + ToV8StringUnsafe(context->GetIsolate(), key, string_type)) + .ToLocalChecked(); +} + +} // namespace v8_helpers +} // namespace extensions + +#endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ |