GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/sharedarraybuffer_metadata.cc Lines: 48 54 88.9 %
Date: 2019-08-17 22:35:23 Branches: 18 28 64.3 %

Line Branch Exec Source
1
#include "sharedarraybuffer_metadata.h"
2
3
#include "base_object-inl.h"
4
#include "node_errors.h"
5
#include "util-inl.h"
6
7
#include <utility>
8
9
using v8::Context;
10
using v8::Function;
11
using v8::FunctionTemplate;
12
using v8::Local;
13
using v8::Maybe;
14
using v8::MaybeLocal;
15
using v8::Nothing;
16
using v8::Object;
17
using v8::SharedArrayBuffer;
18
using v8::Value;
19
20
namespace node {
21
namespace worker {
22
23
namespace {
24
25
// Yield a JS constructor for SABLifetimePartner objects in the form of a
26
// standard API object, that has a single field for containing the raw
27
// SABLifetimePartner* pointer.
28
587
Local<Function> GetSABLifetimePartnerConstructor(
29
    Environment* env, Local<Context> context) {
30
  Local<FunctionTemplate> templ;
31
587
  templ = env->sab_lifetimepartner_constructor_template();
32
587
  if (!templ.IsEmpty())
33
608
    return templ->GetFunction(context).ToLocalChecked();
34
35
283
  templ = BaseObject::MakeLazilyInitializedJSTemplate(env);
36
  templ->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(),
37
566
                                            "SABLifetimePartner"));
38
283
  env->set_sab_lifetimepartner_constructor_template(templ);
39
40
283
  return GetSABLifetimePartnerConstructor(env, context);
41
}
42
43
596
class SABLifetimePartner : public BaseObject {
44
 public:
45
304
  SABLifetimePartner(Environment* env,
46
                     Local<Object> obj,
47
                     SharedArrayBufferMetadataReference r)
48
    : BaseObject(env, obj),
49
304
      reference(std::move(r)) {
50
304
    MakeWeak();
51
304
  }
52
53
4
  SET_NO_MEMORY_INFO()
54
4
  SET_MEMORY_INFO_NAME(SABLifetimePartner)
55
4
  SET_SELF_SIZE(SABLifetimePartner)
56
57
  SharedArrayBufferMetadataReference reference;
58
};
59
60
}  // anonymous namespace
61
62
SharedArrayBufferMetadataReference
63
222
SharedArrayBufferMetadata::ForSharedArrayBuffer(
64
    Environment* env,
65
    Local<Context> context,
66
    Local<SharedArrayBuffer> source) {
67
  Local<Value> lifetime_partner;
68
69
666
  if (!source->GetPrivate(context,
70
222
                          env->sab_lifetimepartner_symbol())
71
666
                              .ToLocal(&lifetime_partner)) {
72
    return nullptr;
73
  }
74
75

1012
  if (lifetime_partner->IsObject() &&
76
      env->sab_lifetimepartner_constructor_template()
77
594
         ->HasInstance(lifetime_partner)) {
78
124
    CHECK(source->IsExternal());
79
    SABLifetimePartner* partner =
80
124
        Unwrap<SABLifetimePartner>(lifetime_partner.As<Object>());
81
124
    CHECK_NOT_NULL(partner);
82
124
    return partner->reference;
83
  }
84
85
98
  if (source->IsExternal()) {
86
    // If this is an external SharedArrayBuffer but we do not see a lifetime
87
    // partner object, it was not us who externalized it. In that case, there
88
    // is no way to serialize it, because it's unclear how the memory
89
    // is actually owned.
90
    THROW_ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER(env);
91
    return nullptr;
92
  }
93
94
98
  SharedArrayBuffer::Contents contents = source->Externalize();
95
98
  SharedArrayBufferMetadataReference r(new SharedArrayBufferMetadata(contents));
96
196
  if (r->AssignToSharedArrayBuffer(env, context, source).IsNothing())
97
    return nullptr;
98
98
  return r;
99
}
100
101
304
Maybe<bool> SharedArrayBufferMetadata::AssignToSharedArrayBuffer(
102
    Environment* env, Local<Context> context,
103
    Local<SharedArrayBuffer> target) {
104
304
  CHECK(target->IsExternal());
105
304
  Local<Function> ctor = GetSABLifetimePartnerConstructor(env, context);
106
  Local<Object> obj;
107
608
  if (!ctor->NewInstance(context).ToLocal(&obj))
108
    return Nothing<bool>();
109
110
304
  new SABLifetimePartner(env, obj, shared_from_this());
111
304
  return target->SetPrivate(context,
112
                            env->sab_lifetimepartner_symbol(),
113
608
                            obj);
114
}
115
116
98
SharedArrayBufferMetadata::SharedArrayBufferMetadata(
117
    const SharedArrayBuffer::Contents& contents)
118
98
  : contents_(contents) { }
119
120
184
SharedArrayBufferMetadata::~SharedArrayBufferMetadata() {
121
92
  contents_.Deleter()(contents_.Data(),
122
                      contents_.ByteLength(),
123
92
                      contents_.DeleterData());
124
92
}
125
126
206
MaybeLocal<SharedArrayBuffer> SharedArrayBufferMetadata::GetSharedArrayBuffer(
127
    Environment* env, Local<Context> context) {
128
  Local<SharedArrayBuffer> obj =
129
      SharedArrayBuffer::New(env->isolate(),
130
                             contents_.Data(),
131
206
                             contents_.ByteLength());
132
133
412
  if (AssignToSharedArrayBuffer(env, context, obj).IsNothing())
134
    return MaybeLocal<SharedArrayBuffer>();
135
136
206
  return obj;
137
}
138
139
}  // namespace worker
140
}  // namespace node