1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/bindings/v8_private_property.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding_macros.h"
namespace blink {
// As SymbolKey is widely used, numerous instances of SymbolKey are created,
// plus all the instances have static storage duration (defined as static
// variables). Thus, it's important to make SymbolKey
// trivially-constructible/destructible so that compilers can remove all the
// constructor/destructor calls and reduce the code size.
static_assert(
std::is_trivially_constructible<V8PrivateProperty::SymbolKey>::value,
"SymbolKey is not trivially constructible");
static_assert(
std::is_trivially_destructible<V8PrivateProperty::SymbolKey>::value,
"SymbolKey is not trivially destructible");
V8PrivateProperty::Symbol V8PrivateProperty::GetWindowDocumentCachedAccessor(
v8::Isolate* isolate) {
V8PrivateProperty* private_prop =
V8PerIsolateData::From(isolate)->PrivateProperty();
if (private_prop->symbol_window_document_cached_accessor_.IsEmpty())
[[unlikely]] {
// This private property is used in Window, and Window and Document are
// stored in the V8 context snapshot. So, this private property needs to
// be restorable from the snapshot, and only v8::Private::ForApi supports
// it so far.
//
// TODO(peria): Explore a better way to connect a Document to a Window.
v8::Local<v8::Private> private_symbol = v8::Private::ForApi(
isolate, V8String(isolate, "Window#DocumentCachedAccessor"));
private_prop->symbol_window_document_cached_accessor_.Set(isolate,
private_symbol);
}
return Symbol(
isolate,
private_prop->symbol_window_document_cached_accessor_.NewLocal(isolate));
}
V8PrivateProperty::Symbol V8PrivateProperty::GetCachedAccessor(
v8::Isolate* isolate,
CachedAccessor symbol_id) {
switch (symbol_id) {
case CachedAccessor::kNone:
break;
case CachedAccessor::kWindowProxy:
return Symbol(
isolate,
v8::Private::ForApi(
isolate,
V8String(isolate,
"V8PrivateProperty::CachedAccessor::kWindowProxy")));
case CachedAccessor::kWindowDocument:
return GetWindowDocumentCachedAccessor(isolate);
}
NOTREACHED();
}
V8PrivateProperty::Symbol V8PrivateProperty::GetSymbol(
v8::Isolate* isolate,
const V8PrivateProperty::SymbolKey& key) {
V8PrivateProperty* private_prop =
V8PerIsolateData::From(isolate)->PrivateProperty();
auto& symbol_map = private_prop->symbol_map_;
auto iter = symbol_map.find(&key);
v8::Local<v8::Private> v8_private;
if (iter == symbol_map.end()) [[unlikely]] {
v8_private = CreateV8Private(isolate, nullptr);
symbol_map.insert(&key, v8::Eternal<v8::Private>(isolate, v8_private));
} else {
v8_private = iter->value.Get(isolate);
}
return Symbol(isolate, v8_private);
}
v8::Local<v8::Private> V8PrivateProperty::CreateV8Private(v8::Isolate* isolate,
const char* symbol) {
return v8::Private::New(isolate, V8String(isolate, symbol));
}
} // namespace blink
|