@@ -25,6 +25,7 @@ const {
2525 op_v8_read_value,
2626 op_v8_release_buffer,
2727 op_v8_set_treat_array_buffer_views_as_host_objects,
28+ op_v8_query_objects_count,
2829 op_v8_take_heap_snapshot,
2930 op_v8_transfer_array_buffer,
3031 op_v8_transfer_array_buffer_de,
@@ -48,9 +49,10 @@ const { isArrayBufferView } = core.loadExtScript(
4849const lazyFsUtils = core . createLazyLoader (
4950 "ext:deno_node/internal/fs/utils.mjs" ,
5051) ;
51- const { validateObject } = core . loadExtScript (
52- "ext:deno_node/internal/validators.mjs" ,
53- ) ;
52+ const { validateFunction, validateObject, validateOneOf } = core
53+ . loadExtScript (
54+ "ext:deno_node/internal/validators.mjs" ,
55+ ) ;
5456
5557function cachedDataVersionTag ( ) {
5658 return op_v8_cached_data_version_tag ( ) ;
@@ -167,6 +169,44 @@ function writeHeapSnapshot(
167169 return filename ;
168170}
169171
172+ // https://nodejs.org/api/v8.html#v8queryobjectsctor-options
173+ //
174+ // Deno currently only supports `{ format: 'count' }`. Returning live instances
175+ // would require V8's `HeapProfiler::QueryObjects`, which isn't exposed in the
176+ // rusty_v8 bindings; the count form is what Node's leak tests rely on.
177+ function queryObjects (
178+ ctor : { name ?: string ; prototype ?: unknown } ,
179+ options :
180+ | { format ?: "count" | "summary" }
181+ | undefined = undefined ,
182+ ) {
183+ validateFunction ( ctor , "constructor" ) ;
184+ if ( options !== undefined ) {
185+ validateObject ( options , "options" ) ;
186+ if ( options . format !== undefined ) {
187+ validateOneOf ( options . format , "options.format" , [ "count" , "summary" ] ) ;
188+ }
189+ }
190+ const format = options ?. format ;
191+
192+ const name = typeof ctor . name === "string" ? ctor . name : "" ;
193+ if ( name === "" ) {
194+ return format === "count" ? 0 : [ ] ;
195+ }
196+ const count = op_v8_query_objects_count ( name ) ;
197+ if ( format === "count" ) {
198+ return count ;
199+ }
200+ if ( format === "summary" ) {
201+ if ( count === 0 ) return [ ] ;
202+ return [ `${ count } instance(s) of ${ name } ` ] ;
203+ }
204+ // Default format returns live object handles, which would require V8's
205+ // `HeapProfiler::QueryObjects` (not exposed in rusty_v8). Returning an
206+ // empty array keeps the signature sensible.
207+ return [ ] ;
208+ }
209+
170210// deno-lint-ignore no-explicit-any
171211function serialize ( value : any ) {
172212 const ser = new DefaultSerializer ( ) ;
@@ -396,6 +436,7 @@ return {
396436 getHeapSnapshot,
397437 getHeapSpaceStatistics,
398438 getHeapStatistics,
439+ queryObjects,
399440 setFlagsFromString,
400441 stopCoverage,
401442 takeCoverage,
0 commit comments