|
| 1 | +#include "node_diagnostics_channel.h" |
| 2 | + |
| 3 | +#include "env-inl.h" |
| 4 | +#include "node_external_reference.h" |
| 5 | +#include "util-inl.h" |
| 6 | +#include "v8.h" |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | + |
| 10 | +namespace node { |
| 11 | +namespace diagnostics_channel { |
| 12 | + |
| 13 | +using v8::Context; |
| 14 | +using v8::FunctionCallbackInfo; |
| 15 | +using v8::HandleScope; |
| 16 | +using v8::Isolate; |
| 17 | +using v8::Local; |
| 18 | +using v8::Object; |
| 19 | +using v8::ObjectTemplate; |
| 20 | +using v8::SnapshotCreator; |
| 21 | +using v8::String; |
| 22 | +using v8::Value; |
| 23 | + |
| 24 | +BindingData::BindingData(Realm* realm, |
| 25 | + Local<Object> wrap, |
| 26 | + InternalFieldInfo* info) |
| 27 | + : SnapshotableObject(realm, wrap, type_int), |
| 28 | + subscribers_(realm->isolate(), |
| 29 | + kMaxChannels, |
| 30 | + MAYBE_FIELD_PTR(info, subscribers)) { |
| 31 | + if (info == nullptr) { |
| 32 | + wrap->Set(realm->context(), |
| 33 | + FIXED_ONE_BYTE_STRING(realm->isolate(), "subscribers"), |
| 34 | + subscribers_.GetJSArray()) |
| 35 | + .Check(); |
| 36 | + } else { |
| 37 | + subscribers_.Deserialize(realm->context()); |
| 38 | + } |
| 39 | + subscribers_.MakeWeak(); |
| 40 | +} |
| 41 | + |
| 42 | +void BindingData::MemoryInfo(MemoryTracker* tracker) const { |
| 43 | + tracker->TrackField("subscribers", subscribers_); |
| 44 | +} |
| 45 | + |
| 46 | +uint32_t BindingData::GetOrCreateChannelIndex(const std::string& name) { |
| 47 | + auto it = channel_indices_.find(name); |
| 48 | + if (it != channel_indices_.end()) { |
| 49 | + return it->second; |
| 50 | + } |
| 51 | + CHECK_LT(next_channel_index_, kMaxChannels); |
| 52 | + uint32_t index = next_channel_index_++; |
| 53 | + channel_indices_.emplace(name, index); |
| 54 | + return index; |
| 55 | +} |
| 56 | + |
| 57 | +void BindingData::GetOrCreateChannelIndex( |
| 58 | + const FunctionCallbackInfo<Value>& args) { |
| 59 | + Realm* realm = Realm::GetCurrent(args); |
| 60 | + BindingData* binding = realm->GetBindingData<BindingData>(); |
| 61 | + CHECK_NOT_NULL(binding); |
| 62 | + |
| 63 | + CHECK(args[0]->IsString()); |
| 64 | + Utf8Value name(realm->isolate(), args[0]); |
| 65 | + |
| 66 | + uint32_t index = binding->GetOrCreateChannelIndex(*name); |
| 67 | + args.GetReturnValue().Set(index); |
| 68 | +} |
| 69 | + |
| 70 | +void BindingData::SetPublishCallback( |
| 71 | + const FunctionCallbackInfo<Value>& args) { |
| 72 | + Realm* realm = Realm::GetCurrent(args); |
| 73 | + BindingData* binding = realm->GetBindingData<BindingData>(); |
| 74 | + CHECK_NOT_NULL(binding); |
| 75 | + |
| 76 | + CHECK(args[0]->IsFunction()); |
| 77 | + binding->publish_callback_.Reset(realm->isolate(), |
| 78 | + args[0].As<v8::Function>()); |
| 79 | +} |
| 80 | + |
| 81 | +bool BindingData::PrepareForSerialization(Local<Context> context, |
| 82 | + SnapshotCreator* creator) { |
| 83 | + DCHECK_NULL(internal_field_info_); |
| 84 | + internal_field_info_ = InternalFieldInfoBase::New<InternalFieldInfo>(type()); |
| 85 | + internal_field_info_->subscribers = |
| 86 | + subscribers_.Serialize(context, creator); |
| 87 | + publish_callback_.Reset(); |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +InternalFieldInfoBase* BindingData::Serialize(int index) { |
| 92 | + DCHECK_IS_SNAPSHOT_SLOT(index); |
| 93 | + InternalFieldInfo* info = internal_field_info_; |
| 94 | + internal_field_info_ = nullptr; |
| 95 | + return info; |
| 96 | +} |
| 97 | + |
| 98 | +void BindingData::Deserialize(Local<Context> context, |
| 99 | + Local<Object> holder, |
| 100 | + int index, |
| 101 | + InternalFieldInfoBase* info) { |
| 102 | + DCHECK_IS_SNAPSHOT_SLOT(index); |
| 103 | + HandleScope scope(Isolate::GetCurrent()); |
| 104 | + Realm* realm = Realm::GetCurrent(context); |
| 105 | + BindingData* binding = realm->AddBindingData<BindingData>( |
| 106 | + holder, static_cast<InternalFieldInfo*>(info)); |
| 107 | + CHECK_NOT_NULL(binding); |
| 108 | +} |
| 109 | + |
| 110 | +void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, |
| 111 | + Local<ObjectTemplate> target) { |
| 112 | + Isolate* isolate = isolate_data->isolate(); |
| 113 | + SetMethod( |
| 114 | + isolate, target, "getOrCreateChannelIndex", GetOrCreateChannelIndex); |
| 115 | + SetMethod(isolate, target, "setPublishCallback", SetPublishCallback); |
| 116 | +} |
| 117 | + |
| 118 | +void BindingData::CreatePerContextProperties(Local<Object> target, |
| 119 | + Local<Value> unused, |
| 120 | + Local<Context> context, |
| 121 | + void* priv) { |
| 122 | + Realm* realm = Realm::GetCurrent(context); |
| 123 | + BindingData* const binding = realm->AddBindingData<BindingData>(target); |
| 124 | + if (binding == nullptr) return; |
| 125 | +} |
| 126 | + |
| 127 | +void BindingData::RegisterExternalReferences( |
| 128 | + ExternalReferenceRegistry* registry) { |
| 129 | + registry->Register(GetOrCreateChannelIndex); |
| 130 | + registry->Register(SetPublishCallback); |
| 131 | +} |
| 132 | + |
| 133 | +Channel::Channel(BindingData* binding_data, uint32_t index, const char* name) |
| 134 | + : binding_data_(binding_data), index_(index), name_(name) {} |
| 135 | + |
| 136 | +Channel Channel::Get(Environment* env, const char* name) { |
| 137 | + Realm* realm = env->principal_realm(); |
| 138 | + BindingData* binding = realm->GetBindingData<BindingData>(); |
| 139 | + if (binding == nullptr) { |
| 140 | + return Channel(nullptr, 0, name); |
| 141 | + } |
| 142 | + uint32_t index = binding->GetOrCreateChannelIndex(std::string(name)); |
| 143 | + return Channel(binding, index, name); |
| 144 | +} |
| 145 | + |
| 146 | +void Channel::Publish(Environment* env, Local<Value> message) const { |
| 147 | + if (!HasSubscribers()) return; |
| 148 | + |
| 149 | + Isolate* isolate = env->isolate(); |
| 150 | + HandleScope handle_scope(isolate); |
| 151 | + Local<Context> context = env->context(); |
| 152 | + Context::Scope context_scope(context); |
| 153 | + |
| 154 | + if (binding_data_->publish_callback_.IsEmpty()) return; |
| 155 | + |
| 156 | + Local<v8::Function> callback = |
| 157 | + binding_data_->publish_callback_.Get(isolate); |
| 158 | + Local<String> channel_name = |
| 159 | + String::NewFromUtf8(isolate, name_).ToLocalChecked(); |
| 160 | + |
| 161 | + Local<Value> argv[] = {channel_name, message}; |
| 162 | + USE(callback->Call(context, v8::Undefined(isolate), 2, argv)); |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace diagnostics_channel |
| 166 | +} // namespace node |
| 167 | + |
| 168 | +NODE_BINDING_CONTEXT_AWARE_INTERNAL( |
| 169 | + diagnostics_channel, |
| 170 | + node::diagnostics_channel::BindingData::CreatePerContextProperties) |
| 171 | +NODE_BINDING_PER_ISOLATE_INIT( |
| 172 | + diagnostics_channel, |
| 173 | + node::diagnostics_channel::BindingData::CreatePerIsolateProperties) |
| 174 | +NODE_BINDING_EXTERNAL_REFERENCE( |
| 175 | + diagnostics_channel, |
| 176 | + node::diagnostics_channel::BindingData::RegisterExternalReferences) |
0 commit comments