Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
util.cc
Go to the documentation of this file.
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 "util.h"
23 #include "string_bytes.h"
24 #include "node_buffer.h"
25 #include "node_internals.h"
26 #include <stdio.h>
27 
28 namespace node {
29 
30 using v8::Isolate;
31 using v8::Local;
32 using v8::String;
33 using v8::Value;
34 
35 template <typename T>
36 static void MakeUtf8String(Isolate* isolate,
37  Local<Value> value,
38  T* target) {
39  Local<String> string = value->ToString(isolate);
40  if (string.IsEmpty())
41  return;
42 
43  const size_t storage = StringBytes::StorageSize(isolate, string, UTF8) + 1;
44  target->AllocateSufficientStorage(storage);
45  const int flags =
46  String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
47  const int length = string->WriteUtf8(target->out(), storage, 0, flags);
48  target->SetLengthAndZeroTerminate(length);
49 }
50 
51 Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
52  if (value.IsEmpty())
53  return;
54 
55  MakeUtf8String(isolate, value, this);
56 }
57 
58 
59 TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
60  if (value.IsEmpty()) {
61  return;
62  }
63 
64  Local<String> string = value->ToString(isolate);
65  if (string.IsEmpty())
66  return;
67 
68  // Allocate enough space to include the null terminator
69  const size_t storage = string->Length() + 1;
70  AllocateSufficientStorage(storage);
71 
72  const int flags = String::NO_NULL_TERMINATION;
73  const int length = string->Write(out(), 0, storage, flags);
74  SetLengthAndZeroTerminate(length);
75 }
76 
77 BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
78  // Slightly different take on Utf8Value. If value is a String,
79  // it will return a Utf8 encoded string. If value is a Buffer,
80  // it will copy the data out of the Buffer as is.
81  if (value.IsEmpty()) {
82  // Dereferencing this object will return nullptr.
83  Invalidate();
84  return;
85  }
86 
87  if (value->IsString()) {
88  MakeUtf8String(isolate, value, this);
89  } else if (Buffer::HasInstance(value)) {
90  const size_t len = Buffer::Length(value);
91  // Leave place for the terminating '\0' byte.
92  AllocateSufficientStorage(len + 1);
93  memcpy(out(), Buffer::Data(value), len);
94  SetLengthAndZeroTerminate(len);
95  } else {
96  Invalidate();
97  }
98 }
99 
101  if (v8_initialized) {
102  auto isolate = v8::Isolate::GetCurrent();
103  if (isolate != nullptr) {
104  isolate->LowMemoryNotification();
105  }
106  }
107 }
108 
109 } // namespace node
bool HasInstance(Local< Value > val)
Definition: node_buffer.cc:201
int len
Definition: cares_wrap.cc:485
size_t Length(Local< Value > val)
Definition: node_buffer.cc:227
bool v8_initialized
Definition: node.cc:249
char * Data(Local< Value > val)
Definition: node_buffer.cc:211
void LowMemoryNotification()
Definition: util.cc:100