mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 00:21:05 -05:00
Shared buffer fixes (#1644)
* makes `libdeno.shared` a `SharedArrayBuffer` instead of a regular `ArrayBuffer`. * fixes `libdeno.shared` becoming undefined after accessing it once.
This commit is contained in:
parent
7380b19459
commit
bbe2004f5d
4 changed files with 22 additions and 6 deletions
|
@ -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() {
|
||||
|
|
|
@ -46,6 +46,7 @@ class DenoIsolate {
|
|||
}
|
||||
|
||||
~DenoIsolate() {
|
||||
shared_ab_.Reset();
|
||||
if (snapshot_creator_) {
|
||||
delete snapshot_creator_;
|
||||
} else {
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue