Skip to content

Commit 696d528

Browse files
authored
fix(ext/web): make TextDecoderResource use cppgc (#24888)
Fixes #24878
1 parent ba40347 commit 696d528

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

ext/web/08_text_encoding.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class TextDecoder {
5656
/** @type {boolean} */
5757
#utf8SinglePass;
5858

59-
/** @type {number | null} */
60-
#rid = null;
59+
/** @type {object | null} */
60+
#handle = null;
6161

6262
/**
6363
* @param {string} label
@@ -159,7 +159,7 @@ class TextDecoder {
159159
}
160160

161161
// Fast path for single pass encoding.
162-
if (!stream && this.#rid === null) {
162+
if (!stream && this.#handle === null) {
163163
// Fast path for utf8 single pass encoding.
164164
if (this.#utf8SinglePass) {
165165
return op_encoding_decode_utf8(input, this.#ignoreBOM);
@@ -173,18 +173,17 @@ class TextDecoder {
173173
);
174174
}
175175

176-
if (this.#rid === null) {
177-
this.#rid = op_encoding_new_decoder(
176+
if (this.#handle === null) {
177+
this.#handle = op_encoding_new_decoder(
178178
this.#encoding,
179179
this.#fatal,
180180
this.#ignoreBOM,
181181
);
182182
}
183-
return op_encoding_decode(input, this.#rid, stream);
183+
return op_encoding_decode(input, this.#handle, stream);
184184
} finally {
185-
if (!stream && this.#rid !== null) {
186-
core.close(this.#rid);
187-
this.#rid = null;
185+
if (!stream && this.#handle !== null) {
186+
this.#handle = null;
188187
}
189188
}
190189
}

ext/web/lib.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ use deno_core::op2;
1313
use deno_core::url::Url;
1414
use deno_core::v8;
1515
use deno_core::ByteString;
16-
use deno_core::OpState;
17-
use deno_core::Resource;
18-
use deno_core::ResourceId;
1916
use deno_core::ToJsBuffer;
2017
use deno_core::U16String;
2118

@@ -277,14 +274,13 @@ fn op_encoding_decode_single(
277274
}
278275
}
279276

280-
#[op2(fast)]
281-
#[smi]
277+
#[op2]
278+
#[cppgc]
282279
fn op_encoding_new_decoder(
283-
state: &mut OpState,
284280
#[string] label: &str,
285281
fatal: bool,
286282
ignore_bom: bool,
287-
) -> Result<ResourceId, AnyError> {
283+
) -> Result<TextDecoderResource, AnyError> {
288284
let encoding = Encoding::for_label(label.as_bytes()).ok_or_else(|| {
289285
range_error(format!(
290286
"The encoding label provided ('{label}') is invalid."
@@ -297,24 +293,19 @@ fn op_encoding_new_decoder(
297293
encoding.new_decoder_with_bom_removal()
298294
};
299295

300-
let rid = state.resource_table.add(TextDecoderResource {
296+
Ok(TextDecoderResource {
301297
decoder: RefCell::new(decoder),
302298
fatal,
303-
});
304-
305-
Ok(rid)
299+
})
306300
}
307301

308302
#[op2]
309303
#[serde]
310304
fn op_encoding_decode(
311-
state: &mut OpState,
312305
#[anybuffer] data: &[u8],
313-
#[smi] rid: ResourceId,
306+
#[cppgc] resource: &TextDecoderResource,
314307
stream: bool,
315308
) -> Result<U16String, AnyError> {
316-
let resource = state.resource_table.get::<TextDecoderResource>(rid)?;
317-
318309
let mut decoder = resource.decoder.borrow_mut();
319310
let fatal = resource.fatal;
320311

@@ -357,11 +348,7 @@ struct TextDecoderResource {
357348
fatal: bool,
358349
}
359350

360-
impl Resource for TextDecoderResource {
361-
fn name(&self) -> Cow<str> {
362-
"textDecoder".into()
363-
}
364-
}
351+
impl deno_core::GarbageCollected for TextDecoderResource {}
365352

366353
#[op2(fast(op_encoding_encode_into_fast))]
367354
fn op_encoding_encode_into(

0 commit comments

Comments
 (0)