Skip to content

Commit 5dfdbd1

Browse files
committed
src: use DataView instead of Buffer for code cache
Refs: nodejs#48191 (comment) Signed-off-by: Darshan Sen <[email protected]>
1 parent cf1bdc2 commit 5dfdbd1

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/node_sea.cc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
#if !defined(DISABLE_SINGLE_EXECUTABLE_APPLICATION)
3030

3131
using node::ExitCode;
32+
using v8::ArrayBuffer;
33+
using v8::BackingStore;
3234
using v8::Context;
35+
using v8::DataView;
3336
using v8::Function;
3437
using v8::FunctionCallbackInfo;
3538
using v8::HandleScope;
@@ -202,21 +205,20 @@ void GetCodeCache(const FunctionCallbackInfo<Value>& args) {
202205

203206
SeaResource sea_resource = FindSingleExecutableResource();
204207

205-
Local<Object> buf;
206-
if (!Buffer::New(
207-
env,
208-
const_cast<char*>(sea_resource.code_cache.data()),
209-
sea_resource.code_cache.length(),
210-
[](char* /* data */, void* /* hint */) {
211-
// We don't free the code cache data string because it is not owned
212-
// by us.
213-
},
214-
nullptr)
215-
.ToLocal(&buf)) {
216-
return;
217-
}
218-
219-
args.GetReturnValue().Set(buf);
208+
std::shared_ptr<BackingStore> backing_store = ArrayBuffer::NewBackingStore(
209+
const_cast<void*>(
210+
static_cast<const void*>(sea_resource.code_cache.data())),
211+
sea_resource.code_cache.length(),
212+
[](void* /* data */, size_t /* length */, void* /* deleter_data */) {
213+
// The code cache data string is not freed here because it is a static
214+
// string which is not allocated by the BackingStore allocator.
215+
},
216+
nullptr);
217+
Local<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, backing_store);
218+
Local<DataView> data_view =
219+
DataView::New(array_buffer, 0, array_buffer->ByteLength());
220+
221+
args.GetReturnValue().Set(data_view);
220222
}
221223

222224
void GetCodePath(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)