1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/inspector/v8-internal-value-type.h"
6
7 #include "src/inspector/string-util.h"
8
9 namespace v8_inspector {
10
11 namespace {
12
internalSubtypePrivate(v8::Isolate * isolate)13 v8::Local<v8::Private> internalSubtypePrivate(v8::Isolate* isolate) {
14 return v8::Private::ForApi(
15 isolate,
16 toV8StringInternalized(isolate, "V8InternalType#internalSubtype"));
17 }
18
subtypeForInternalType(v8::Isolate * isolate,V8InternalValueType type)19 v8::Local<v8::String> subtypeForInternalType(v8::Isolate* isolate,
20 V8InternalValueType type) {
21 switch (type) {
22 case V8InternalValueType::kEntry:
23 return toV8StringInternalized(isolate, "internal#entry");
24 case V8InternalValueType::kLocation:
25 return toV8StringInternalized(isolate, "internal#location");
26 case V8InternalValueType::kScope:
27 return toV8StringInternalized(isolate, "internal#scope");
28 case V8InternalValueType::kScopeList:
29 return toV8StringInternalized(isolate, "internal#scopeList");
30 }
31 UNREACHABLE();
32 return v8::Local<v8::String>();
33 }
34
35 } // namespace
36
markAsInternal(v8::Local<v8::Context> context,v8::Local<v8::Object> object,V8InternalValueType type)37 bool markAsInternal(v8::Local<v8::Context> context,
38 v8::Local<v8::Object> object, V8InternalValueType type) {
39 v8::Isolate* isolate = context->GetIsolate();
40 v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
41 v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
42 return object->SetPrivate(context, privateValue, subtype).FromMaybe(false);
43 }
44
markArrayEntriesAsInternal(v8::Local<v8::Context> context,v8::Local<v8::Array> array,V8InternalValueType type)45 bool markArrayEntriesAsInternal(v8::Local<v8::Context> context,
46 v8::Local<v8::Array> array,
47 V8InternalValueType type) {
48 v8::Isolate* isolate = context->GetIsolate();
49 v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
50 v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
51 for (uint32_t i = 0; i < array->Length(); ++i) {
52 v8::Local<v8::Value> entry;
53 if (!array->Get(context, i).ToLocal(&entry) || !entry->IsObject())
54 return false;
55 if (!entry.As<v8::Object>()
56 ->SetPrivate(context, privateValue, subtype)
57 .FromMaybe(false))
58 return false;
59 }
60 return true;
61 }
62
v8InternalValueTypeFrom(v8::Local<v8::Context> context,v8::Local<v8::Object> object)63 v8::Local<v8::Value> v8InternalValueTypeFrom(v8::Local<v8::Context> context,
64 v8::Local<v8::Object> object) {
65 v8::Isolate* isolate = context->GetIsolate();
66 v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
67 if (!object->HasPrivate(context, privateValue).FromMaybe(false))
68 return v8::Null(isolate);
69 v8::Local<v8::Value> subtypeValue;
70 if (!object->GetPrivate(context, privateValue).ToLocal(&subtypeValue) ||
71 !subtypeValue->IsString())
72 return v8::Null(isolate);
73 return subtypeValue;
74 }
75
76 } // namespace v8_inspector
77