aboutsummaryrefslogtreecommitdiff
path: root/src/inspector/v8-internal-value-type.cc
blob: 46f5dac1ac19132ba43fe2ae12b48f8e5fda1da8 (plain)
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
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/inspector/v8-internal-value-type.h"

#include "src/inspector/string-util.h"

namespace v8_inspector {

namespace {

v8::Local<v8::Private> internalSubtypePrivate(v8::Isolate* isolate) {
  return v8::Private::ForApi(
      isolate,
      toV8StringInternalized(isolate, "V8InternalType#internalSubtype"));
}

v8::Local<v8::String> subtypeForInternalType(v8::Isolate* isolate,
                                             V8InternalValueType type) {
  switch (type) {
    case V8InternalValueType::kEntry:
      return toV8StringInternalized(isolate, "internal#entry");
    case V8InternalValueType::kLocation:
      return toV8StringInternalized(isolate, "internal#location");
    case V8InternalValueType::kScope:
      return toV8StringInternalized(isolate, "internal#scope");
    case V8InternalValueType::kScopeList:
      return toV8StringInternalized(isolate, "internal#scopeList");
  }
  UNREACHABLE();
  return v8::Local<v8::String>();
}

}  // namespace

bool markAsInternal(v8::Local<v8::Context> context,
                    v8::Local<v8::Object> object, V8InternalValueType type) {
  v8::Isolate* isolate = context->GetIsolate();
  v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
  v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
  return object->SetPrivate(context, privateValue, subtype).FromMaybe(false);
}

bool markArrayEntriesAsInternal(v8::Local<v8::Context> context,
                                v8::Local<v8::Array> array,
                                V8InternalValueType type) {
  v8::Isolate* isolate = context->GetIsolate();
  v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
  v8::Local<v8::String> subtype = subtypeForInternalType(isolate, type);
  for (uint32_t i = 0; i < array->Length(); ++i) {
    v8::Local<v8::Value> entry;
    if (!array->Get(context, i).ToLocal(&entry) || !entry->IsObject())
      return false;
    if (!entry.As<v8::Object>()
             ->SetPrivate(context, privateValue, subtype)
             .FromMaybe(false))
      return false;
  }
  return true;
}

v8::Local<v8::Value> v8InternalValueTypeFrom(v8::Local<v8::Context> context,
                                             v8::Local<v8::Object> object) {
  v8::Isolate* isolate = context->GetIsolate();
  v8::Local<v8::Private> privateValue = internalSubtypePrivate(isolate);
  if (!object->HasPrivate(context, privateValue).FromMaybe(false))
    return v8::Null(isolate);
  v8::Local<v8::Value> subtypeValue;
  if (!object->GetPrivate(context, privateValue).ToLocal(&subtypeValue) ||
      !subtypeValue->IsString())
    return v8::Null(isolate);
  return subtypeValue;
}

}  // namespace v8_inspector