Skip to content

Commit

Permalink
src: let's clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
gengjiawen committed Feb 6, 2019
1 parent 12b3cfc commit 425befd
Show file tree
Hide file tree
Showing 196 changed files with 14,443 additions and 15,533 deletions.
65 changes: 22 additions & 43 deletions src/aliased_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "v8.h"
#include "util-inl.h"
#include "v8.h"

namespace node {

Expand All @@ -22,16 +22,14 @@ namespace node {
* The encapsulation herein provides a placeholder where such writes can be
* observed. Any notification APIs will be left as a future exercise.
*/
template <class NativeT, class V8T,
template <class NativeT,
class V8T,
// SFINAE NativeT to be scalar
typename = std::enable_if_t<std::is_scalar<NativeT>::value>>
class AliasedBuffer {
public:
AliasedBuffer(v8::Isolate* isolate, const size_t count)
: isolate_(isolate),
count_(count),
byte_offset_(0),
free_buffer_(true) {
: isolate_(isolate), count_(count), byte_offset_(0), free_buffer_(true) {
CHECK_GT(count, 0);
const v8::HandleScope handle_scope(isolate_);

Expand All @@ -41,8 +39,8 @@ class AliasedBuffer {
buffer_ = Calloc<NativeT>(count);

// allocate v8 ArrayBuffer
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
isolate_, buffer_, size_in_bytes);
v8::Local<v8::ArrayBuffer> ab =
v8::ArrayBuffer::New(isolate_, buffer_, size_in_bytes);

// allocate v8 TypedArray
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count);
Expand All @@ -61,8 +59,7 @@ class AliasedBuffer {
AliasedBuffer(v8::Isolate* isolate,
const size_t byte_offset,
const size_t count,
const AliasedBuffer<uint8_t,
v8::Uint8Array>& backing_buffer)
const AliasedBuffer<uint8_t, v8::Uint8Array>& backing_buffer)
: isolate_(isolate),
count_(count),
byte_offset_(byte_offset),
Expand All @@ -74,7 +71,7 @@ class AliasedBuffer {
// validate that the byte_offset is aligned with sizeof(NativeT)
CHECK_EQ(byte_offset & (sizeof(NativeT) - 1), 0);
// validate this fits inside the backing buffer
CHECK_LE(sizeof(NativeT) * count, ab->ByteLength() - byte_offset);
CHECK_LE(sizeof(NativeT) * count, ab->ByteLength() - byte_offset);

buffer_ = reinterpret_cast<NativeT*>(
const_cast<uint8_t*>(backing_buffer.GetNativeBuffer() + byte_offset));
Expand Down Expand Up @@ -121,14 +118,10 @@ class AliasedBuffer {
class Reference {
public:
Reference(AliasedBuffer<NativeT, V8T>* aliased_buffer, size_t index)
: aliased_buffer_(aliased_buffer),
index_(index) {
}
: aliased_buffer_(aliased_buffer), index_(index) {}

Reference(const Reference& that)
: aliased_buffer_(that.aliased_buffer_),
index_(that.index_) {
}
: aliased_buffer_(that.aliased_buffer_), index_(that.index_) {}

inline Reference& operator=(const NativeT& val) {
aliased_buffer_->SetValue(index_, val);
Expand All @@ -139,9 +132,7 @@ class AliasedBuffer {
return *this = static_cast<NativeT>(val);
}

operator NativeT() const {
return aliased_buffer_->GetValue(index_);
}
operator NativeT() const { return aliased_buffer_->GetValue(index_); }

inline Reference& operator+=(const NativeT& val) {
const NativeT current = aliased_buffer_->GetValue(index_);
Expand All @@ -167,14 +158,12 @@ class AliasedBuffer {
/**
* Get the underlying v8 TypedArray overlayed on top of the native buffer
*/
v8::Local<V8T> GetJSArray() const {
return js_array_.Get(isolate_);
}
v8::Local<V8T> GetJSArray() const { return js_array_.Get(isolate_); }

/**
* Get the underlying v8::ArrayBuffer underlying the TypedArray and
* overlaying the native buffer
*/
* Get the underlying v8::ArrayBuffer underlying the TypedArray and
* overlaying the native buffer
*/
v8::Local<v8::ArrayBuffer> GetArrayBuffer() const {
return GetJSArray()->Buffer();
}
Expand All @@ -183,16 +172,12 @@ class AliasedBuffer {
* Get the underlying native buffer. Note that all reads/writes should occur
* through the GetValue/SetValue/operator[] methods
*/
inline const NativeT* GetNativeBuffer() const {
return buffer_;
}
inline const NativeT* GetNativeBuffer() const { return buffer_; }

/**
* Synonym for GetBuffer()
*/
inline const NativeT* operator * () const {
return GetNativeBuffer();
}
inline const NativeT* operator*() const { return GetNativeBuffer(); }

/**
* Set position index to given value.
Expand All @@ -213,17 +198,11 @@ class AliasedBuffer {
/**
* Effectively, a synonym for GetValue/SetValue
*/
Reference operator[](size_t index) {
return Reference(this, index);
}
Reference operator[](size_t index) { return Reference(this, index); }

NativeT operator[](size_t index) const {
return GetValue(index);
}
NativeT operator[](size_t index) const { return GetValue(index); }

size_t Length() const {
return count_;
}
size_t Length() const { return count_; }

// Should only be used to extend the array.
// Should only be used on an owning array, not one created as a sub array of
Expand All @@ -243,8 +222,8 @@ class AliasedBuffer {
memcpy(new_buffer, buffer_, old_size_in_bytes);

// allocate v8 new ArrayBuffer
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
isolate_, new_buffer, new_size_in_bytes);
v8::Local<v8::ArrayBuffer> ab =
v8::ArrayBuffer::New(isolate_, new_buffer, new_size_in_bytes);

// allocate v8 TypedArray
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, new_capacity);
Expand Down
33 changes: 15 additions & 18 deletions src/api/callback.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "node.h"
#include "async_wrap-inl.h"
#include "env-inl.h"
#include "node.h"
#include "v8.h"

namespace node {
Expand All @@ -22,33 +22,31 @@ using AsyncHooks = Environment::AsyncHooks;
CallbackScope::CallbackScope(Isolate* isolate,
Local<Object> object,
async_context asyncContext)
: private_(new InternalCallbackScope(Environment::GetCurrent(isolate),
object,
asyncContext)),
try_catch_(isolate) {
: private_(new InternalCallbackScope(
Environment::GetCurrent(isolate), object, asyncContext)),
try_catch_(isolate) {
try_catch_.SetVerbose(true);
}

CallbackScope::~CallbackScope() {
if (try_catch_.HasCaught())
private_->MarkAsFailed();
if (try_catch_.HasCaught()) private_->MarkAsFailed();
delete private_;
}

InternalCallbackScope::InternalCallbackScope(AsyncWrap* async_wrap)
: InternalCallbackScope(async_wrap->env(),
async_wrap->object(),
{ async_wrap->get_async_id(),
async_wrap->get_trigger_async_id() }) {}
: InternalCallbackScope(
async_wrap->env(),
async_wrap->object(),
{async_wrap->get_async_id(), async_wrap->get_trigger_async_id()}) {}

InternalCallbackScope::InternalCallbackScope(Environment* env,
Local<Object> object,
const async_context& asyncContext,
ResourceExpectation expect)
: env_(env),
async_context_(asyncContext),
object_(object),
callback_scope_(env) {
: env_(env),
async_context_(asyncContext),
object_(object),
callback_scope_(env) {
CHECK_IMPLIES(expect == kRequireResource, !object.IsEmpty());
CHECK_NOT_NULL(env);

Expand All @@ -68,7 +66,7 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
}

env->async_hooks()->push_async_ids(async_context_.async_id,
async_context_.trigger_async_id);
async_context_.trigger_async_id);
pushed_ids_ = true;
}

Expand All @@ -86,8 +84,7 @@ void InternalCallbackScope::Close() {
env_->async_hooks()->clear_async_id_stack();
}

if (pushed_ids_)
env_->async_hooks()->pop_async_id(async_context_.async_id);
if (pushed_ids_) env_->async_hooks()->pop_async_id(async_context_.async_id);

if (failed_) return;

Expand Down
31 changes: 11 additions & 20 deletions src/api/encoding.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "node.h"
#include "env-inl.h"
#include "node.h"
#include "string_bytes.h"
#include "util-inl.h"
#include "v8.h"
Expand All @@ -19,35 +19,29 @@ enum encoding ParseEncoding(const char* encoding,
if (encoding[1] == 't' && encoding[2] == 'f') {
// Skip `-`
encoding += encoding[3] == '-' ? 4 : 3;
if (encoding[0] == '8' && encoding[1] == '\0')
return UTF8;
if (strncmp(encoding, "16le", 4) == 0)
return UCS2;
if (encoding[0] == '8' && encoding[1] == '\0') return UTF8;
if (strncmp(encoding, "16le", 4) == 0) return UCS2;

// ucs2
// ucs2
} else if (encoding[1] == 'c' && encoding[2] == 's') {
encoding += encoding[3] == '-' ? 4 : 3;
if (encoding[0] == '2' && encoding[1] == '\0')
return UCS2;
if (encoding[0] == '2' && encoding[1] == '\0') return UCS2;
}
break;
case 'l':
// latin1
if (encoding[1] == 'a') {
if (strncmp(encoding + 2, "tin1", 4) == 0)
return LATIN1;
if (strncmp(encoding + 2, "tin1", 4) == 0) return LATIN1;
}
break;
case 'b':
// binary
if (encoding[1] == 'i') {
if (strncmp(encoding + 2, "nary", 4) == 0)
return LATIN1;
if (strncmp(encoding + 2, "nary", 4) == 0) return LATIN1;

// buffer
// buffer
} else if (encoding[1] == 'u') {
if (strncmp(encoding + 2, "ffer", 4) == 0)
return BUFFER;
if (strncmp(encoding + 2, "ffer", 4) == 0) return BUFFER;
}
break;
case '\0':
Expand Down Expand Up @@ -85,14 +79,12 @@ enum encoding ParseEncoding(const char* encoding,
}
}


enum encoding ParseEncoding(Isolate* isolate,
Local<Value> encoding_v,
enum encoding default_encoding) {
CHECK(!encoding_v.IsEmpty());

if (!encoding_v->IsString())
return default_encoding;
if (!encoding_v->IsString()) return default_encoding;

Utf8Value encoding(isolate, encoding_v);

Expand All @@ -111,8 +103,7 @@ Local<Value> Encode(Isolate* isolate,

Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) {
Local<Value> error;
return StringBytes::Encode(isolate, buf, len, &error)
.ToLocalChecked();
return StringBytes::Encode(isolate, buf, len, &error).ToLocalChecked();
}

// Returns -1 if the handle was not valid for decoding
Expand Down
Loading

0 comments on commit 425befd

Please sign in to comment.