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: 41 54 75.9 %
Date: 2019-02-23 22:23:05 Branches: 12 28 42.9 %

Line Branch Exec Source
1
#include "sharedarraybuffer_metadata.h"
2
3
#include <utility>
4
#include "base_object.h"
5
#include "base_object-inl.h"
6
#include "node_errors.h"
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
24
Local<Function> GetSABLifetimePartnerConstructor(
28
    Environment* env, Local<Context> context) {
29
  Local<FunctionTemplate> templ;
30
24
  templ = env->sab_lifetimepartner_constructor_template();
31
24
  if (!templ.IsEmpty())
32
28
    return templ->GetFunction(context).ToLocalChecked();
33
34
10
  templ = BaseObject::MakeLazilyInitializedJSTemplate(env);
35
  templ->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(),
36
20
                                            "SABLifetimePartner"));
37
10
  env->set_sab_lifetimepartner_constructor_template(templ);
38
39
10
  return GetSABLifetimePartnerConstructor(env, context);
40
}
41
42
28
class SABLifetimePartner : public BaseObject {
43
 public:
44
14
  SABLifetimePartner(Environment* env,
45
                     Local<Object> obj,
46
                     SharedArrayBufferMetadataReference r)
47
    : BaseObject(env, obj),
48
14
      reference(std::move(r)) {
49
14
    MakeWeak();
50
14
  }
51
52
  SET_NO_MEMORY_INFO()
53
  SET_MEMORY_INFO_NAME(SABLifetimePartner)
54
  SET_SELF_SIZE(SABLifetimePartner)
55
56
  SharedArrayBufferMetadataReference reference;
57
};
58
59
}  // anonymous namespace
60
61
SharedArrayBufferMetadataReference
62
7
SharedArrayBufferMetadata::ForSharedArrayBuffer(
63
    Environment* env,
64
    Local<Context> context,
65
    Local<SharedArrayBuffer> source) {
66
  Local<Value> lifetime_partner;
67
68
21
  if (!source->GetPrivate(context,
69
7
                          env->sab_lifetimepartner_symbol())
70
21
                              .ToLocal(&lifetime_partner)) {
71
    return nullptr;
72
  }
73
74

28
  if (lifetime_partner->IsObject() &&
75
      env->sab_lifetimepartner_constructor_template()
76
7
         ->HasInstance(lifetime_partner)) {
77
    CHECK(source->IsExternal());
78
    SABLifetimePartner* partner =
79
        Unwrap<SABLifetimePartner>(lifetime_partner.As<Object>());
80
    CHECK_NOT_NULL(partner);
81
    return partner->reference;
82
  }
83
84
7
  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
7
  SharedArrayBuffer::Contents contents = source->Externalize();
94
7
  SharedArrayBufferMetadataReference r(new SharedArrayBufferMetadata(contents));
95
14
  if (r->AssignToSharedArrayBuffer(env, context, source).IsNothing())
96
    return nullptr;
97
7
  return r;
98
}
99
100
14
Maybe<bool> SharedArrayBufferMetadata::AssignToSharedArrayBuffer(
101
    Environment* env, Local<Context> context,
102
    Local<SharedArrayBuffer> target) {
103
14
  CHECK(target->IsExternal());
104
14
  Local<Function> ctor = GetSABLifetimePartnerConstructor(env, context);
105
  Local<Object> obj;
106
28
  if (!ctor->NewInstance(context).ToLocal(&obj))
107
    return Nothing<bool>();
108
109
14
  new SABLifetimePartner(env, obj, shared_from_this());
110
14
  return target->SetPrivate(context,
111
                            env->sab_lifetimepartner_symbol(),
112
28
                            obj);
113
}
114
115
7
SharedArrayBufferMetadata::SharedArrayBufferMetadata(
116
    const SharedArrayBuffer::Contents& contents)
117
7
  : contents_(contents) { }
118
119
14
SharedArrayBufferMetadata::~SharedArrayBufferMetadata() {
120
7
  contents_.Deleter()(contents_.Data(),
121
                      contents_.ByteLength(),
122
7
                      contents_.DeleterData());
123
7
}
124
125
7
MaybeLocal<SharedArrayBuffer> SharedArrayBufferMetadata::GetSharedArrayBuffer(
126
    Environment* env, Local<Context> context) {
127
  Local<SharedArrayBuffer> obj =
128
      SharedArrayBuffer::New(env->isolate(),
129
                             contents_.Data(),
130
7
                             contents_.ByteLength());
131
132
14
  if (AssignToSharedArrayBuffer(env, context, obj).IsNothing())
133
    return MaybeLocal<SharedArrayBuffer>();
134
135
7
  return obj;
136
}
137
138
}  // namespace worker
139
}  // namespace node