GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/util.cc Lines: 43 59 72.9 %
Date: 2019-02-01 22:03:38 Branches: 13 28 46.4 %

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
242379
static void MakeUtf8String(Isolate* isolate,
39
                           Local<Value> value,
40
                           T* target) {
41
  Local<String> string;
42

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

484758
  if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
46
242379
  storage += 1;
47
242379
  target->AllocateSufficientStorage(storage);
48
  const int flags =
49
242379
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
50
  const int length =
51
484758
      string->WriteUtf8(isolate, target->out(), storage, 0, flags);
52
242379
  target->SetLengthAndZeroTerminate(length);
53
}
54
55
209207
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
56
209207
  if (value.IsEmpty())
57
209535
    return;
58
59
208879
  MakeUtf8String(isolate, value, this);
60
}
61
62
63
489
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
64
489
  if (value.IsEmpty()) {
65
    return;
66
  }
67
68
  Local<String> string;
69
1467
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
70
71
  // Allocate enough space to include the null terminator
72
489
  const size_t storage = string->Length() + 1;
73
489
  AllocateSufficientStorage(storage);
74
75
489
  const int flags = String::NO_NULL_TERMINATION;
76
978
  const int length = string->Write(isolate, out(), 0, storage, flags);
77
489
  SetLengthAndZeroTerminate(length);
78
}
79
80
33500
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
33500
  if (value.IsEmpty()) {
85
    // Dereferencing this object will return nullptr.
86
    Invalidate();
87
33500
    return;
88
  }
89
90
67000
  if (value->IsString()) {
91
33500
    MakeUtf8String(isolate, value, this);
92
  } else if (Buffer::HasInstance(value)) {
93
    const size_t len = Buffer::Length(value);
94
    // Leave place for the terminating '\0' byte.
95
    AllocateSufficientStorage(len + 1);
96
    memcpy(out(), Buffer::Data(value), len);
97
    SetLengthAndZeroTerminate(len);
98
  } else {
99
    Invalidate();
100
  }
101
}
102
103
void LowMemoryNotification() {
104
  if (per_process::v8_initialized) {
105
    auto isolate = Isolate::GetCurrent();
106
    if (isolate != nullptr) {
107
      isolate->LowMemoryNotification();
108
    }
109
  }
110
}
111
112
164
std::string GetHumanReadableProcessName() {
113
  char name[1024];
114
164
  GetHumanReadableProcessName(&name);
115
164
  return name;
116
}
117
118
164
void GetHumanReadableProcessName(char (*name)[1024]) {
119
  // Leave room after title for pid, which can be up to 20 digits for 64 bit.
120
164
  char title[1000] = "Node.js";
121
164
  uv_get_process_title(title, sizeof(title));
122
164
  snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
123
164
}
124
125
66
std::vector<std::string> SplitString(const std::string& in, char delim) {
126
66
  std::vector<std::string> out;
127
66
  if (in.empty())
128
    return out;
129
132
  std::istringstream in_stream(in);
130
264
  while (in_stream.good()) {
131
132
    std::string item;
132
132
    std::getline(in_stream, item, delim);
133
132
    out.emplace_back(std::move(item));
134
132
  }
135
66
  return out;
136
}
137
138
void ThrowErrStringTooLong(Isolate* isolate) {
139
  isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
140
}
141
142
}  // namespace node