Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9df5ef7

Browse filesBrowse files
andreubotellaV8 LUCI CQ
authored and
V8 LUCI CQ
committed
Add an v8::ArrayBuffer::WasDetached method to the C++ API
V8's C++ API does not give a way to tell whether an ArrayBuffer has been detached from the `v8::ArrayBuffer` class. In fact, as far as can be told from the C++ API without running JS code, detached ArrayBuffers behave the same as zero-sized ArrayBuffers and there is no way to observe the difference. However, this difference can be observed in JS because constructing a TypedArray from a detached ArrayBuffer will throw. This change adds a `WasDetached` method to the `v8::ArrayBuffer` class to give embedders access to this information without having to run JS code. Bug: v8:13159 Change-Id: I2bb1e380cee1cecd31f6d48ec3d9f28c03a8a673 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3810345 Commit-Queue: Toon Verwaest <verwaest@chromium.org> Reviewed-by: Toon Verwaest <verwaest@chromium.org> Cr-Commit-Position: refs/heads/main@{#83963}
1 parent b799750 commit 9df5ef7
Copy full SHA for 9df5ef7

File tree

Expand file treeCollapse file tree

5 files changed

+45
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+45
-0
lines changed

‎AUTHORS

Copy file name to clipboardExpand all lines: AUTHORS
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Allan Sandfeld Jensen <allan.jensen@qt.io>
6060
Amos Lim <eui-sang.lim@samsung.com>
6161
Andreas Anyuru <andreas.anyuru@gmail.com>
6262
Andrei Kashcha <anvaka@gmail.com>
63+
Andreu Botella <andreu@andreubotella.com>
6364
Andrew Paprocki <andrew@ishiboo.com>
6465
Anna Henningsen <anna@addaleax.net>
6566
Antoine du Hamel <duhamelantoine1995@gmail.com>

‎include/v8-array-buffer.h

Copy file name to clipboardExpand all lines: include/v8-array-buffer.h
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ class V8_EXPORT ArrayBuffer : public Object {
240240
*/
241241
bool IsDetachable() const;
242242

243+
/**
244+
* Returns true if this ArrayBuffer has been detached.
245+
*/
246+
bool WasDetached() const;
247+
243248
/**
244249
* Detaches this ArrayBuffer and all its views (typed arrays).
245250
* Detaching sets the byte length of the buffer and all typed arrays to zero,
@@ -271,6 +276,9 @@ class V8_EXPORT ArrayBuffer : public Object {
271276
* pointer coordinates the lifetime management of the internal storage
272277
* with any live ArrayBuffers on the heap, even across isolates. The embedder
273278
* should not attempt to manage lifetime of the storage through other means.
279+
*
280+
* The returned shared pointer will not be empty, even if the ArrayBuffer has
281+
* been detached. Use |WasDetached| to tell if it has been detached instead.
274282
*/
275283
std::shared_ptr<BackingStore> GetBackingStore();
276284

‎src/api/api.cc

Copy file name to clipboardExpand all lines: src/api/api.cc
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8100,6 +8100,10 @@ bool v8::ArrayBuffer::IsDetachable() const {
81008100
return Utils::OpenHandle(this)->is_detachable();
81018101
}
81028102

8103+
bool v8::ArrayBuffer::WasDetached() const {
8104+
return Utils::OpenHandle(this)->was_detached();
8105+
}
8106+
81038107
namespace {
81048108
std::shared_ptr<i::BackingStore> ToInternal(
81058109
std::shared_ptr<i::BackingStoreBase> backing_store) {

‎test/cctest/cctest.status

Copy file name to clipboardExpand all lines: test/cctest/cctest.status
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@
513513
'test-api/WasmI32AtomicWaitCallback': [SKIP],
514514
'test-api/WasmI64AtomicWaitCallback': [SKIP],
515515
'test-api/WasmSetJitCodeEventHandler': [SKIP],
516+
'test-api-array-buffer/ArrayBuffer_NonDetachableWasDetached': [SKIP],
516517
'test-backing-store/Run_WasmModule_Buffer_Externalized_Regression_UseAfterFree': [SKIP],
517518
'test-c-wasm-entry/*': [SKIP],
518519
'test-compilation-cache/*': [SKIP],

‎test/cctest/test-api-array-buffer.cc

Copy file name to clipboardExpand all lines: test/cctest/test-api-array-buffer.cc
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,37 @@ THREADED_TEST(ArrayBuffer_DetachingScript) {
245245
CheckDataViewIsDetached(dv);
246246
}
247247

248+
THREADED_TEST(ArrayBuffer_WasDetached) {
249+
LocalContext env;
250+
v8::Isolate* isolate = env->GetIsolate();
251+
v8::HandleScope handle_scope(isolate);
252+
253+
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 0);
254+
CHECK(!ab->WasDetached());
255+
256+
ab->Detach(v8::Local<v8::Value>()).Check();
257+
CHECK(ab->WasDetached());
258+
}
259+
260+
THREADED_TEST(ArrayBuffer_NonDetachableWasDetached) {
261+
LocalContext env;
262+
v8::Isolate* isolate = env->GetIsolate();
263+
v8::HandleScope handle_scope(isolate);
264+
265+
CompileRun(R"JS(
266+
var wasmMemory = new WebAssembly.Memory({initial: 1, maximum: 2});
267+
)JS");
268+
269+
Local<v8::ArrayBuffer> non_detachable =
270+
CompileRun("wasmMemory.buffer").As<v8::ArrayBuffer>();
271+
CHECK(!non_detachable->IsDetachable());
272+
CHECK(!non_detachable->WasDetached());
273+
274+
CompileRun("wasmMemory.grow(1)");
275+
CHECK(!non_detachable->IsDetachable());
276+
CHECK(non_detachable->WasDetached());
277+
}
278+
248279
THREADED_TEST(ArrayBuffer_ExternalizeEmpty) {
249280
LocalContext env;
250281
v8::Isolate* isolate = env->GetIsolate();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.