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 }
33
34 } // namespace
35
markAsInternal(v8::Local<v8::Context> context,v8::Local<v8::Object> object,V8InternalValueType type)36 bool markAsInternal(v8::Local<v8::Context> context,
37 v8::Local<v8::Object> object, V8InternalValueType type) {
38 v8::Isolate* isolate = context->GetIsolate();
39 v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
40 v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
41 return object->SetPrivate(context, privateValue, subtype).FromMaybe(false);
42 }
43
markArrayEntriesAsInternal(v8::Local<v8::Context> context,v8::Local<v8::Array> array,V8InternalValueType type)44 bool markArrayEntriesAsInternal(v8::Local<v8::Context> context,
45 v8::Local<v8::Array> array,
46 V8InternalValueType type) {
47 v8::Isolate* isolate = context->GetIsolate();
48 v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
49 v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
50 for (uint32_t i = 0; i < array->Length(); ++i) {
51 v8::Local<v8::Value> entry;
52 if (!array->Get(context, i).ToLocal(&entry) || !entry->IsObject())
53 return false;
54 if (!entry.As<v8::Object>()
55 ->SetPrivate(context, privateValue, subtype)
56 .FromMaybe(false))
57 return false;
58 }
59 return true;
60 }
61
v8InternalValueTypeFrom(v8::Local<v8::Context> context,v8::Local<v8::Object> object)62 v8::Local<v8::Value> v8InternalValueTypeFrom(v8::Local<v8::Context> context,
63 v8::Local<v8::Object> object) {
64 v8::Isolate* isolate = context->GetIsolate();
65 v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
66 if (!object->HasPrivate(context, privateValue).FromMaybe(false))
67 return v8::Null(isolate);
68 v8::Local<v8::Value> subtypeValue;
69 if (!object->GetPrivate(context, privateValue).ToLocal(&subtypeValue) ||
70 !subtypeValue->IsString())
71 return v8::Null(isolate);
72 return subtypeValue;
73 }
74
75 } // namespace v8_inspector
76