GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/util.cc Lines: 48 59 81.4 %
Date: 2019-01-07 12:15:22 Branches: 16 28 57.1 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include <stdio.h>
23
#include <sstream>
24
#include "node_buffer.h"
25
#include "node_errors.h"
26
#include "node_internals.h"
27
#include "string_bytes.h"
28
#include "uv.h"
29
30
namespace node {
31
32
using v8::Isolate;
33
using v8::Local;
34
using v8::String;
35
using v8::Value;
36
37
template <typename T>
38
1408337
static void MakeUtf8String(Isolate* isolate,
39
                           Local<Value> value,
40
                           T* target) {
41
  Local<String> string;
42

4225015
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
43
44
  size_t storage;
45

2816674
  if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
46
1408337
  storage += 1;
47
1408337
  target->AllocateSufficientStorage(storage);
48
  const int flags =
49
1408335
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
50
  const int length =
51
2816670
      string->WriteUtf8(isolate, target->out(), storage, 0, flags);
52
1408333
  target->SetLengthAndZeroTerminate(length);
53
}
54
55
1210403
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
56
1210401
  if (value.IsEmpty())
57
1211668
    return;
58
59
1209137
  MakeUtf8String(isolate, value, this);
60
}
61
62
63
535
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
64
535
  if (value.IsEmpty()) {
65
    return;
66
  }
67
68
  Local<String> string;
69
1605
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
70
71
  // Allocate enough space to include the null terminator
72
535
  const size_t storage = string->Length() + 1;
73
535
  AllocateSufficientStorage(storage);
74
75
535
  const int flags = String::NO_NULL_TERMINATION;
76
1070
  const int length = string->Write(isolate, out(), 0, storage, flags);
77
535
  SetLengthAndZeroTerminate(length);
78
}
79
80
200364
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
81
  // Slightly different take on Utf8Value. If value is a String,
82
  // it will return a Utf8 encoded string. If value is a Buffer,
83
  // it will copy the data out of the Buffer as is.
84
200366
  if (value.IsEmpty()) {
85
    // Dereferencing this object will return nullptr.
86
    Invalidate();
87
200366
    return;
88
  }
89
90
400732
  if (value->IsString()) {
91
199200
    MakeUtf8String(isolate, value, this);
92
1166
  } else if (Buffer::HasInstance(value)) {
93
1166
    const size_t len = Buffer::Length(value);
94
    // Leave place for the terminating '\0' byte.
95
1166
    AllocateSufficientStorage(len + 1);
96
1166
    memcpy(out(), Buffer::Data(value), len);
97
1166
    SetLengthAndZeroTerminate(len);
98
  } else {
99
    Invalidate();
100
  }
101
}
102
103
void LowMemoryNotification() {
104
  if (v8_initialized) {
105
    auto isolate = Isolate::GetCurrent();
106
    if (isolate != nullptr) {
107
      isolate->LowMemoryNotification();
108
    }
109
  }
110
}
111
112
3563
std::string GetHumanReadableProcessName() {
113
  char name[1024];
114
3563
  GetHumanReadableProcessName(&name);
115
3563
  return name;
116
}
117
118
3563
void GetHumanReadableProcessName(char (*name)[1024]) {
119
  // Leave room after title for pid, which can be up to 20 digits for 64 bit.
120
3563
  char title[1000] = "Node.js";
121
3563
  uv_get_process_title(title, sizeof(title));
122
3563
  snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
123
3563
}
124
125
65
std::set<std::string> ParseCommaSeparatedSet(const std::string& in) {
126
65
  std::set<std::string> out;
127
65
  if (in.empty())
128
    return out;
129
130
  std::istringstream in_stream(in);
130
203
  while (in_stream.good()) {
131
73
    std::string item;
132
73
    getline(in_stream, item, ',');
133
73
    out.emplace(std::move(item));
134
73
  }
135
65
  return out;
136
}
137
138
void ThrowErrStringTooLong(Isolate* isolate) {
139
  isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
140
}
141
142
}  // namespace node