Skip to content

Commit

Permalink
Shared buffer fixes (denoland#1644)
Browse files Browse the repository at this point in the history
* makes `libdeno.shared` a `SharedArrayBuffer` instead of a regular `ArrayBuffer`.
* fixes `libdeno.shared` becoming undefined after accessing it once.
  • Loading branch information
piscisaureus committed Feb 9, 2019
1 parent 7380b19 commit bbe2004
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
9 changes: 5 additions & 4 deletions libdeno/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,15 @@ void Shared(v8::Local<v8::Name> property,
if (d->shared_.data_ptr == nullptr) {
return;
}
v8::Local<v8::ArrayBuffer> ab;
v8::Local<v8::SharedArrayBuffer> ab;
if (d->shared_ab_.IsEmpty()) {
// Lazily initialize the persistent external ArrayBuffer.
ab = v8::ArrayBuffer::New(isolate, d->shared_.data_ptr, d->shared_.data_len,
v8::ArrayBufferCreationMode::kExternalized);
ab = v8::SharedArrayBuffer::New(isolate, d->shared_.data_ptr,
d->shared_.data_len,
v8::ArrayBufferCreationMode::kExternalized);
d->shared_ab_.Reset(isolate, ab);
}
info.GetReturnValue().Set(ab);
info.GetReturnValue().Set(d->shared_ab_);
}

void DenoIsolate::ClearModules() {
Expand Down
3 changes: 2 additions & 1 deletion libdeno/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DenoIsolate {
}

~DenoIsolate() {
shared_ab_.Reset();
if (snapshot_creator_) {
delete snapshot_creator_;
} else {
Expand Down Expand Up @@ -98,7 +99,7 @@ class DenoIsolate {
v8::Persistent<v8::Function> recv_;
v8::StartupData snapshot_;
v8::Persistent<v8::ArrayBuffer> global_import_buf_;
v8::Persistent<v8::ArrayBuffer> shared_ab_;
v8::Persistent<v8::SharedArrayBuffer> shared_ab_;
};

class UserDataScope {
Expand Down
13 changes: 13 additions & 0 deletions libdeno/libdeno_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,16 @@ TEST(LibDenoTest, Utf8Bug) {
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_delete(d);
}

TEST(LibDenoTest, SharedAtomics) {
int32_t s[] = {0, 1, 2};
deno_buf shared = {nullptr, 0, reinterpret_cast<uint8_t*>(s), sizeof s};
Deno* d = deno_new(deno_config{0, empty, shared, nullptr});
deno_execute(d, nullptr, "a.js",
"Atomics.add(new Int32Array(libdeno.shared), 0, 1)");
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(s[0], 1);
EXPECT_EQ(s[1], 1);
EXPECT_EQ(s[2], 2);
deno_delete(d);
}
3 changes: 2 additions & 1 deletion libdeno/libdeno_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ global.CheckPromiseErrors = () => {

global.Shared = () => {
const ab = libdeno.shared;
assert(ab instanceof ArrayBuffer);
assert(ab instanceof SharedArrayBuffer);
assert(libdeno.shared != undefined);
assert(ab.byteLength === 3);
const ui8 = new Uint8Array(ab);
assert(ui8[0] === 0);
Expand Down

0 comments on commit bbe2004

Please sign in to comment.