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: 33 41 80.5 %
Date: 2017-10-21 Branches: 12 20 60.0 %

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 "string_bytes.h"
23
#include "node_buffer.h"
24
#include "node_internals.h"
25
#include <stdio.h>
26
27
namespace node {
28
29
using v8::Isolate;
30
using v8::Local;
31
using v8::String;
32
using v8::Value;
33
34
template <typename T>
35
1159376
static void MakeUtf8String(Isolate* isolate,
36
                           Local<Value> value,
37
                           T* target) {
38
1159376
  Local<String> string = value->ToString(isolate);
39

1159376
  if (string.IsEmpty())
40
1159378
    return;
41
42
1159374
  const size_t storage = StringBytes::StorageSize(isolate, string, UTF8) + 1;
43
1159374
  target->AllocateSufficientStorage(storage);
44
  const int flags =
45
1159374
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
46
2318748
  const int length = string->WriteUtf8(target->out(), storage, 0, flags);
47
1159374
  target->SetLengthAndZeroTerminate(length);
48
}
49
50
930720
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
51
930720
  if (value.IsEmpty())
52
932117
    return;
53
54
929323
  MakeUtf8String(isolate, value, this);
55
}
56
57
58
408
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
59
408
  if (value.IsEmpty()) {
60
    return;
61
  }
62
63
408
  Local<String> string = value->ToString(isolate);
64
408
  if (string.IsEmpty())
65
    return;
66
67
  // Allocate enough space to include the null terminator
68
408
  const size_t storage = string->Length() + 1;
69
408
  AllocateSufficientStorage(storage);
70
71
408
  const int flags = String::NO_NULL_TERMINATION;
72
816
  const int length = string->Write(out(), 0, storage, flags);
73
408
  SetLengthAndZeroTerminate(length);
74
}
75
76
230850
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
77
  // Slightly different take on Utf8Value. If value is a String,
78
  // it will return a Utf8 encoded string. If value is a Buffer,
79
  // it will copy the data out of the Buffer as is.
80
230850
  if (value.IsEmpty()) {
81
    // Dereferencing this object will return nullptr.
82
    Invalidate();
83
230850
    return;
84
  }
85
86
461700
  if (value->IsString()) {
87
230053
    MakeUtf8String(isolate, value, this);
88
797
  } else if (Buffer::HasInstance(value)) {
89
793
    const size_t len = Buffer::Length(value);
90
    // Leave place for the terminating '\0' byte.
91
793
    AllocateSufficientStorage(len + 1);
92
793
    memcpy(out(), Buffer::Data(value), len);
93
793
    SetLengthAndZeroTerminate(len);
94
  } else {
95
4
    Invalidate();
96
  }
97
}
98
99
void LowMemoryNotification() {
100
  if (v8_initialized) {
101
    auto isolate = v8::Isolate::GetCurrent();
102
    if (isolate != nullptr) {
103
      isolate->LowMemoryNotification();
104
    }
105
  }
106
}
107
108
}  // namespace node