GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/util.cc Lines: 30 31 96.8 %
Date: 2016-07-23 Branches: 11 16 68.8 %

Line Exec Source
1
#include "util.h"
2
#include "string_bytes.h"
3
#include "node_buffer.h"
4
#include <stdio.h>
5
6
namespace node {
7
8
using v8::Isolate;
9
using v8::Local;
10
using v8::String;
11
using v8::Value;
12
13
template <typename T>
14
235712
static void MakeUtf8String(Isolate* isolate,
15
                           Local<Value> value,
16
                           T* target) {
17
235712
  Local<String> string = value->ToString(isolate);
18
235712
  if (string.IsEmpty())
19
    return;
20
21
235712
  const size_t storage = StringBytes::StorageSize(isolate, string, UTF8) + 1;
22
235712
  target->AllocateSufficientStorage(storage);
23
  const int flags =
24
235712
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
25
235712
  const int length = string->WriteUtf8(target->out(), storage, 0, flags);
26
235712
  target->SetLengthAndZeroTerminate(length);
27
}
28
29
396668
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
30
198334
  if (value.IsEmpty())
31
    return;
32
33
197660
  MakeUtf8String(isolate, value, this);
34
}
35
36
37
66
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
38
33
  if (value.IsEmpty()) {
39
    return;
40
  }
41
42
33
  Local<String> string = value->ToString(isolate);
43
33
  if (string.IsEmpty())
44
    return;
45
46
  // Allocate enough space to include the null terminator
47
33
  const size_t storage = string->Length() + 1;
48
33
  AllocateSufficientStorage(storage);
49
50
  const int flags =
51
33
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
52
33
  const int length = string->Write(out(), 0, storage, flags);
53
33
  SetLengthAndZeroTerminate(length);
54
}
55
56
77102
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
57
  // Slightly different take on Utf8Value. If value is a String,
58
  // it will return a Utf8 encoded string. If value is a Buffer,
59
  // it will copy the data out of the Buffer as is.
60
38551
  if (value.IsEmpty()) {
61
    // Dereferencing this object will return nullptr.
62
    Invalidate();
63
    return;
64
  }
65
66
77102
  if (value->IsString()) {
67
38052
    MakeUtf8String(isolate, value, this);
68
499
  } else if (Buffer::HasInstance(value)) {
69
495
    const size_t len = Buffer::Length(value);
70
    // Leave place for the terminating '\0' byte.
71
495
    AllocateSufficientStorage(len + 1);
72
990
    memcpy(out(), Buffer::Data(value), len);
73
495
    SetLengthAndZeroTerminate(len);
74
  } else {
75
4
    Invalidate();
76
  }
77
}
78
79
}  // namespace node