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/protocol-platform.h"
8#include "src/inspector/string-util.h"
9
10namespace v8_inspector {
11
12namespace {
13
14v8::Local<v8::Private> internalSubtypePrivate(v8::Isolate* isolate) {
15  return v8::Private::ForApi(
16      isolate,
17      toV8StringInternalized(isolate, "V8InternalType#internalSubtype"));
18}
19
20v8::Local<v8::String> subtypeForInternalType(v8::Isolate* isolate,
21                                             V8InternalValueType type) {
22  switch (type) {
23    case V8InternalValueType::kEntry:
24      return toV8StringInternalized(isolate, "internal#entry");
25    case V8InternalValueType::kLocation:
26      return toV8StringInternalized(isolate, "internal#location");
27    case V8InternalValueType::kScope:
28      return toV8StringInternalized(isolate, "internal#scope");
29    case V8InternalValueType::kScopeList:
30      return toV8StringInternalized(isolate, "internal#scopeList");
31  }
32  UNREACHABLE();
33  return v8::Local<v8::String>();
34}
35
36}  // namespace
37
38bool markAsInternal(v8::Local<v8::Context> context,
39                    v8::Local<v8::Object> object, V8InternalValueType type) {
40  v8::Isolate* isolate = context->GetIsolate();
41  v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
42  v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
43  return object->SetPrivate(context, privateValue, subtype).FromMaybe(false);
44}
45
46bool markArrayEntriesAsInternal(v8::Local<v8::Context> context,
47                                v8::Local<v8::Array> array,
48                                V8InternalValueType type) {
49  v8::Isolate* isolate = context->GetIsolate();
50  v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
51  v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
52  for (uint32_t i = 0; i < array->Length(); ++i) {
53    v8::Local<v8::Value> entry;
54    if (!array->Get(context, i).ToLocal(&entry) || !entry->IsObject())
55      return false;
56    if (!entry.As<v8::Object>()
57             ->SetPrivate(context, privateValue, subtype)
58             .FromMaybe(false))
59      return false;
60  }
61  return true;
62}
63
64v8::Local<v8::Value> v8InternalValueTypeFrom(v8::Local<v8::Context> context,
65                                             v8::Local<v8::Object> object) {
66  v8::Isolate* isolate = context->GetIsolate();
67  v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
68  if (!object->HasPrivate(context, privateValue).FromMaybe(false))
69    return v8::Null(isolate);
70  v8::Local<v8::Value> subtypeValue;
71  if (!object->GetPrivate(context, privateValue).ToLocal(&subtypeValue) ||
72      !subtypeValue->IsString())
73    return v8::Null(isolate);
74  return subtypeValue;
75}
76
77}  // namespace v8_inspector
78