GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com // Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node_buffer.h"
#include "node.h"
#include "node_errors.h"
#include "node_internals.h"
#include "env-inl.h"
#include "string_bytes.h"
#include "string_search.h"
#include "util-inl.h"
#include "v8.h"
#include
#include
#define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument") \
#define THROW_AND_RETURN_IF_OOB(r) \
do { \
if ((r).IsNothing()) return; \
if (!(r).FromJust()) \
return node::THROW_ERR_OUT_OF_RANGE(env, "Index out of range"); \
} while (0) \
namespace node {
namespace Buffer {
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::Context;
using v8::EscapableHandleScope;
using v8::FunctionCallbackInfo;
using v8::Global;
using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Isolate;
using v8::Just;
using v8::Local;
using v8::Maybe;
using v8::MaybeLocal;
using v8::Nothing;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Uint32;
using v8::Uint32Array;
using v8::Uint8Array;
using v8::Value;
using v8::WeakCallbackInfo;
namespace {
class CallbackInfo {
public:
~CallbackInfo();
static inline void Free(char* data, void* hint);
static inline CallbackInfo* New(Environment* env,
Local object,
FreeCallback callback,
char* data,
void* hint = nullptr);
CallbackInfo(const CallbackInfo&) = delete;
CallbackInfo& operator=(const CallbackInfo&) = delete;
private:
static void CleanupHook(void* data);
static void WeakCallback(const WeakCallbackInfo&);
inline void WeakCallback(Isolate* isolate);
inline CallbackInfo(Environment* env,
Local object,
FreeCallback callback,
char* data,
void* hint);
Global persistent_;
FreeCallback const callback_;
char* const data_;
void* const hint_;
Environment* const env_;
};
void CallbackInfo::Free(char* data, void*) {
::free(data);
}
CallbackInfo* CallbackInfo::New(Environment* env,
Local object,
FreeCallback callback,
char* data,
void* hint) {
return new CallbackInfo(env, object, callback, data, hint);
}
CallbackInfo::CallbackInfo(Environment* env,
Local object,
FreeCallback callback,
char* data,
void* hint)
: persistent_(env->isolate(), object),
callback_(callback),
data_(data),
hint_(hint),
env_(env) {
std::shared_ptr obj_backing = object->GetBackingStore();
CHECK_EQ(data_, static_cast(obj_backing->Data()));
if (object->ByteLength() != 0)
CHECK_NOT_NULL(data_);
persistent_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
env->AddCleanupHook(CleanupHook, this);
env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
}
CallbackInfo::~CallbackInfo() {
persistent_.Reset();
env_->RemoveCleanupHook(CleanupHook, this);
}
void CallbackInfo::CleanupHook(void* data) {
CallbackInfo* self = static_cast(data);
{
HandleScope handle_scope(self->env_->isolate());
Local ab = self->persistent_.Get(self->env_->isolate());
CHECK(!ab.IsEmpty());
ab->Detach();
}
self->WeakCallback(self->env_->isolate());
}
void CallbackInfo::WeakCallback(
const WeakCallbackInfo& data) {
CallbackInfo* self = data.GetParameter();
self->WeakCallback(data.GetIsolate());
delete self;
}
void CallbackInfo::WeakCallback(Isolate* isolate) {
callback_(data_, hint_);
int64_t change_in_bytes = -static_cast(sizeof(*this));
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
}
// Parse index for external array data. An empty Maybe indicates
// a pending exception. `false` indicates that the index is out-of-bounds.
inline MUST_USE_RESULT Maybe ParseArrayIndex(Environment* env,
Local arg,
size_t def,
size_t* ret) {
if (arg->IsUndefined()) {
*ret = def;
return Just(true);
}
int64_t tmp_i;
if (!arg->IntegerValue(env->context()).To(&tmp_i))
return Nothing();
if (tmp_i < 0)
return Just(false);
// Check that the result fits in a size_t.
const uint64_t kSizeMax = static_cast(static_cast(-1));
// coverity[pointless_expression]
if (static_cast(tmp_i) > kSizeMax)
return Just(false);
*ret = static_cast(tmp_i);
return Just(true);
}
} // anonymous namespace
// Buffer methods
bool HasInstance(Local val) {
return val->IsArrayBufferView();
}
bool HasInstance(Local |