GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/util.cc Lines: 30 36 83.3 %
Date: 2016-12-18 Branches: 12 20 60.0 %

Line Branch Exec Source
1
#include "util.h"
2
#include "string_bytes.h"
3
#include "node_buffer.h"
4
#include "node_internals.h"
5
#include <stdio.h>
6
7
namespace node {
8
9
using v8::Isolate;
10
using v8::Local;
11
using v8::String;
12
using v8::Value;
13
14
template <typename T>
15
352068
static void MakeUtf8String(Isolate* isolate,
16
                           Local<Value> value,
17
                           T* target) {
18
352068
  Local<String> string = value->ToString(isolate);
19

352068
  if (string.IsEmpty())
20
    return;
21
22
352065
  const size_t storage = StringBytes::StorageSize(isolate, string, UTF8) + 1;
23
704130
  target->AllocateSufficientStorage(storage);
24
  const int flags =
25
352065
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
26
352065
  const int length = string->WriteUtf8(target->out(), storage, 0, flags);
27
352065
  target->SetLengthAndZeroTerminate(length);
28
}
29
30
496370
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
31
248185
  if (value.IsEmpty())
32
    return;
33
34
247494
  MakeUtf8String(isolate, value, this);
35
}
36
37
38
146
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
39
73
  if (value.IsEmpty()) {
40
    return;
41
  }
42
43
73
  Local<String> string = value->ToString(isolate);
44
73
  if (string.IsEmpty())
45
    return;
46
47
  // Allocate enough space to include the null terminator
48
73
  const size_t storage = string->Length() + 1;
49
146
  AllocateSufficientStorage(storage);
50
51
  const int flags =
52
73
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
53
73
  const int length = string->Write(out(), 0, storage, flags);
54
73
  SetLengthAndZeroTerminate(length);
55
}
56
57
210382
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
58
  // Slightly different take on Utf8Value. If value is a String,
59
  // it will return a Utf8 encoded string. If value is a Buffer,
60
  // it will copy the data out of the Buffer as is.
61
105191
  if (value.IsEmpty()) {
62
    // Dereferencing this object will return nullptr.
63
    Invalidate();
64
    return;
65
  }
66
67
210382
  if (value->IsString()) {
68
104574
    MakeUtf8String(isolate, value, this);
69
617
  } else if (Buffer::HasInstance(value)) {
70
613
    const size_t len = Buffer::Length(value);
71
    // Leave place for the terminating '\0' byte.
72
1226
    AllocateSufficientStorage(len + 1);
73
1226
    memcpy(out(), Buffer::Data(value), len);
74
613
    SetLengthAndZeroTerminate(len);
75
  } else {
76
4
    Invalidate();
77
  }
78
}
79
80
void LowMemoryNotification() {
81
  if (v8_initialized) {
82
    auto isolate = v8::Isolate::GetCurrent();
83
    if (isolate != nullptr) {
84
      isolate->LowMemoryNotification();
85
    }
86
  }
87
}
88
89
}  // namespace node