-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraybuffer.cc
More file actions
134 lines (111 loc) · 3.75 KB
/
arraybuffer.cc
File metadata and controls
134 lines (111 loc) · 3.75 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "_cgo_export.h"
#include "deps/include/v8-array-buffer.h"
#include "deps/include/v8-context.h"
#include "deps/include/v8-isolate.h"
#include "deps/include/v8-locker.h"
#include "arraybuffer.h"
#include "context.h"
#include "context-macros.h"
#include "value.h"
using namespace v8;
static void externalBackingStoreDeleter(void* data, size_t length,
void* deleter_data) {
int ref = static_cast<int>(reinterpret_cast<intptr_t>(deleter_data));
goReleaseExternalArrayBuffer(ref);
}
extern "C" {
ValuePtr NewArrayBufferFromBytes(ContextPtr ctx_ptr,
const void* data,
size_t byte_length) {
LOCAL_CONTEXT(ctx_ptr);
auto backing = ArrayBuffer::NewBackingStore(iso, byte_length);
if (data != nullptr && byte_length > 0) {
memcpy(backing->Data(), data, byte_length);
}
Local<ArrayBuffer> ab = ArrayBuffer::New(iso, std::move(backing));
m_value* val = new m_value;
val->id = 0;
val->iso = iso;
val->ctx = ctx_ptr;
val->ptr.Reset(iso, ab);
return tracked_value(ctx_ptr, val);
}
ValuePtr NewArrayBufferAlloc(ContextPtr ctx_ptr,
size_t byte_length) {
LOCAL_CONTEXT(ctx_ptr);
auto backing = ArrayBuffer::NewBackingStore(iso, byte_length);
Local<ArrayBuffer> ab = ArrayBuffer::New(iso, std::move(backing));
m_value* val = new m_value;
val->id = 0;
val->iso = iso;
val->ctx = ctx_ptr;
val->ptr.Reset(iso, ab);
return tracked_value(ctx_ptr, val);
}
ValuePtr NewArrayBufferExternal(ContextPtr ctx_ptr,
void* data,
size_t byte_length,
int deleter_ref) {
LOCAL_CONTEXT(ctx_ptr);
#ifdef V8_ENABLE_SANDBOX
// With the V8 sandbox enabled, backing stores must live inside the sandbox
// address space. Fall back to alloc + memcpy, then immediately release the
// Go-side pin since we no longer reference the external memory.
auto backing = ArrayBuffer::NewBackingStore(iso, byte_length);
if (data != nullptr && byte_length > 0) {
memcpy(backing->Data(), data, byte_length);
}
goReleaseExternalArrayBuffer(deleter_ref);
#else
auto backing = ArrayBuffer::NewBackingStore(
data, byte_length, externalBackingStoreDeleter,
reinterpret_cast<void*>(static_cast<intptr_t>(deleter_ref)));
#endif
Local<ArrayBuffer> ab = ArrayBuffer::New(iso, std::move(backing));
m_value* val = new m_value;
val->id = 0;
val->iso = iso;
val->ctx = ctx_ptr;
val->ptr.Reset(iso, ab);
return tracked_value(ctx_ptr, val);
}
int V8SandboxIsEnabled() {
#ifdef V8_ENABLE_SANDBOX
return 1;
#else
return 0;
#endif
}
void* ArrayBufferGetData(ValuePtr ptr) {
m_value* val = static_cast<m_value*>(ptr);
Isolate* iso = val->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
Local<Value> value = val->ptr.Get(iso);
Local<ArrayBuffer> ab = Local<ArrayBuffer>::Cast(value);
return ab->Data();
}
size_t ArrayBufferGetByteLength(ValuePtr ptr) {
m_value* val = static_cast<m_value*>(ptr);
Isolate* iso = val->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
Local<Value> value = val->ptr.Get(iso);
Local<ArrayBuffer> ab = Local<ArrayBuffer>::Cast(value);
return ab->ByteLength();
}
BackingStorePtr ArrayBufferGetBackingStore(ValuePtr ptr) {
m_value* val = static_cast<m_value*>(ptr);
Isolate* iso = val->iso;
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
Local<Value> value = val->ptr.Get(iso);
Local<ArrayBuffer> ab = Local<ArrayBuffer>::Cast(value);
auto backing_store = ab->GetBackingStore();
auto proxy = new v8BackingStore(std::move(backing_store));
return proxy;
}
}