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: 49 59 83.1 %
Date: 2019-03-02 22:23:06 Branches: 17 28 60.7 %

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 <cstdio>
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::ArrayBufferView;
33
using v8::Isolate;
34
using v8::Local;
35
using v8::String;
36
using v8::Value;
37
38
template <typename T>
39
2573414
static void MakeUtf8String(Isolate* isolate,
40
                           Local<Value> value,
41
                           T* target) {
42
  Local<String> string;
43

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

5146830
  if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
47
2573414
  storage += 1;
48
2573414
  target->AllocateSufficientStorage(storage);
49
  const int flags =
50
2573414
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
51
  const int length =
52
5146824
      string->WriteUtf8(isolate, target->out(), storage, nullptr, flags);
53
2573416
  target->SetLengthAndZeroTerminate(length);
54
}
55
56
2350198
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
57
2350199
  if (value.IsEmpty())
58
2352180
    return;
59
60
2348222
  MakeUtf8String(isolate, value, this);
61
}
62
63
64
14022
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
65
14022
  if (value.IsEmpty()) {
66
    return;
67
  }
68
69
  Local<String> string;
70
42066
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
71
72
  // Allocate enough space to include the null terminator
73
14022
  const size_t storage = string->Length() + 1;
74
14022
  AllocateSufficientStorage(storage);
75
76
14022
  const int flags = String::NO_NULL_TERMINATION;
77
28044
  const int length = string->Write(isolate, out(), 0, storage, flags);
78
14022
  SetLengthAndZeroTerminate(length);
79
}
80
81
226475
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
82
  // Slightly different take on Utf8Value. If value is a String,
83
  // it will return a Utf8 encoded string. If value is a Buffer,
84
  // it will copy the data out of the Buffer as is.
85
226475
  if (value.IsEmpty()) {
86
    // Dereferencing this object will return nullptr.
87
    Invalidate();
88
226475
    return;
89
  }
90
91
452950
  if (value->IsString()) {
92
225193
    MakeUtf8String(isolate, value, this);
93
1282
  } else if (value->IsArrayBufferView()) {
94
2564
    const size_t len = value.As<ArrayBufferView>()->ByteLength();
95
    // Leave place for the terminating '\0' byte.
96
1282
    AllocateSufficientStorage(len + 1);
97
3846
    value.As<ArrayBufferView>()->CopyContents(out(), len);
98
1282
    SetLengthAndZeroTerminate(len);
99
  } else {
100
    Invalidate();
101
  }
102
}
103
104
void LowMemoryNotification() {
105
  if (per_process::v8_initialized) {
106
    auto isolate = Isolate::GetCurrent();
107
    if (isolate != nullptr) {
108
      isolate->LowMemoryNotification();
109
    }
110
  }
111
}
112
113
4253
std::string GetHumanReadableProcessName() {
114
  char name[1024];
115
4253
  GetHumanReadableProcessName(&name);
116
4253
  return name;
117
}
118
119
4253
void GetHumanReadableProcessName(char (*name)[1024]) {
120
  // Leave room after title for pid, which can be up to 20 digits for 64 bit.
121
4253
  char title[1000] = "Node.js";
122
4253
  uv_get_process_title(title, sizeof(title));
123
4253
  snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
124
4253
}
125
126
4215
std::vector<std::string> SplitString(const std::string& in, char delim) {
127
4215
  std::vector<std::string> out;
128
4215
  if (in.empty())
129
3
    return out;
130
8424
  std::istringstream in_stream(in);
131
16798
  while (in_stream.good()) {
132
8374
    std::string item;
133
8374
    std::getline(in_stream, item, delim);
134
8374
    out.emplace_back(std::move(item));
135
8374
  }
136
4212
  return out;
137
}
138
139
void ThrowErrStringTooLong(Isolate* isolate) {
140
  isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
141
}
142
143
}  // namespace node