This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathallocator-wrap.cpp
71 lines (54 loc) · 3.2 KB
/
allocator-wrap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "allocator-wrap.h"
using namespace napa::module;
NAPA_DEFINE_PERSISTENT_CONSTRUCTOR(AllocatorWrap);
void AllocatorWrap::Init() {
auto isolate = v8::Isolate::GetCurrent();
auto constructorTemplate = v8::FunctionTemplate::New(isolate, DefaultConstructorCallback<AllocatorWrap>);
constructorTemplate->SetClassName(v8_helpers::MakeV8String(isolate, exportName));
constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
InitConstructorTemplate<AllocatorWrap>(constructorTemplate);
NAPA_SET_PROTOTYPE_METHOD(constructorTemplate, "allocate", AllocateCallback);
NAPA_SET_PROTOTYPE_METHOD(constructorTemplate, "deallocate", DeallocateCallback);
NAPA_SET_ACCESSOR(constructorTemplate, "type", GetTypeCallback, nullptr);
auto constructor = constructorTemplate->GetFunction();
InitConstructor("<AllocatorWrap>", constructor);
NAPA_SET_PERSISTENT_CONSTRUCTOR(exportName, constructor);
}
std::shared_ptr<napa::memory::Allocator> AllocatorWrap::Get() {
return ShareableWrap::Get<napa::memory::Allocator>();
}
void AllocatorWrap::AllocateCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
CHECK_ARG(isolate, args.Length() == 1, "1 argument of 'size' is required for \"allocate\".");
CHECK_ARG(isolate, args[0]->IsUint32(), "Argument \"size\" must be a unsigned integer.");
auto thisObject = NAPA_OBJECTWRAP::Unwrap<AllocatorWrap>(args.Holder());
auto allocator = thisObject->Get();
JS_ENSURE(isolate, allocator != nullptr, "AllocatorWrap is not attached with any C++ allocator.");
auto handle = v8_helpers::PtrToV8Uint32Array(isolate, allocator->Allocate(args[0]->Uint32Value()));
args.GetReturnValue().Set(handle);
}
void AllocatorWrap::DeallocateCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
CHECK_ARG(isolate, args.Length() == 2, "2 arguments is required for \"deallocate\".");
auto result = v8_helpers::V8ValueToUintptr(isolate, args[0]);
JS_ENSURE(isolate, result.second, "Unable to cast \"handle\" to pointer. Please check if it's in valid handle format.");
CHECK_ARG(isolate, args[1]->IsUint32(), "Argument \"sizeHint\" must be a 32-bit unsigned integer.");
auto thisObject = NAPA_OBJECTWRAP::Unwrap<AllocatorWrap>(args.Holder());
auto allocator = thisObject->Get();
JS_ENSURE(isolate, allocator != nullptr, "AllocatorWrap is not attached with any C++ allocator.");
allocator->Deallocate(reinterpret_cast<void*>(result.first), args[1]->Uint32Value());
}
void AllocatorWrap::GetTypeCallback(v8::Local<v8::String> /*propertyName*/, const v8::PropertyCallbackInfo<v8::Value>& args){
auto isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
auto thisObject = NAPA_OBJECTWRAP::Unwrap<AllocatorWrap>(args.Holder());
auto allocator = thisObject->Get();
args.GetReturnValue().Set(
v8_helpers::MakeV8String(
isolate,
allocator != nullptr ? allocator->GetType() : "<unloaded>"));
}