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-13 22:28:58 Branches: 12 28 42.9 %

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

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