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

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