1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/web): make TextDecoderResource use cppgc (#24888)

Fixes https://github.com/denoland/deno/issues/24878
This commit is contained in:
Divy Srivastava 2024-08-06 00:40:17 -07:00 committed by GitHub
parent ba40347a35
commit 696d528641
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 29 deletions

View file

@ -56,8 +56,8 @@ class TextDecoder {
/** @type {boolean} */ /** @type {boolean} */
#utf8SinglePass; #utf8SinglePass;
/** @type {number | null} */ /** @type {object | null} */
#rid = null; #handle = null;
/** /**
* @param {string} label * @param {string} label
@ -159,7 +159,7 @@ class TextDecoder {
} }
// Fast path for single pass encoding. // Fast path for single pass encoding.
if (!stream && this.#rid === null) { if (!stream && this.#handle === null) {
// Fast path for utf8 single pass encoding. // Fast path for utf8 single pass encoding.
if (this.#utf8SinglePass) { if (this.#utf8SinglePass) {
return op_encoding_decode_utf8(input, this.#ignoreBOM); return op_encoding_decode_utf8(input, this.#ignoreBOM);
@ -173,18 +173,17 @@ class TextDecoder {
); );
} }
if (this.#rid === null) { if (this.#handle === null) {
this.#rid = op_encoding_new_decoder( this.#handle = op_encoding_new_decoder(
this.#encoding, this.#encoding,
this.#fatal, this.#fatal,
this.#ignoreBOM, this.#ignoreBOM,
); );
} }
return op_encoding_decode(input, this.#rid, stream); return op_encoding_decode(input, this.#handle, stream);
} finally { } finally {
if (!stream && this.#rid !== null) { if (!stream && this.#handle !== null) {
core.close(this.#rid); this.#handle = null;
this.#rid = null;
} }
} }
} }

View file

@ -13,9 +13,6 @@ use deno_core::op2;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::v8; use deno_core::v8;
use deno_core::ByteString; use deno_core::ByteString;
use deno_core::OpState;
use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::ToJsBuffer; use deno_core::ToJsBuffer;
use deno_core::U16String; use deno_core::U16String;
@ -277,14 +274,13 @@ fn op_encoding_decode_single(
} }
} }
#[op2(fast)] #[op2]
#[smi] #[cppgc]
fn op_encoding_new_decoder( fn op_encoding_new_decoder(
state: &mut OpState,
#[string] label: &str, #[string] label: &str,
fatal: bool, fatal: bool,
ignore_bom: bool, ignore_bom: bool,
) -> Result<ResourceId, AnyError> { ) -> Result<TextDecoderResource, AnyError> {
let encoding = Encoding::for_label(label.as_bytes()).ok_or_else(|| { let encoding = Encoding::for_label(label.as_bytes()).ok_or_else(|| {
range_error(format!( range_error(format!(
"The encoding label provided ('{label}') is invalid." "The encoding label provided ('{label}') is invalid."
@ -297,24 +293,19 @@ fn op_encoding_new_decoder(
encoding.new_decoder_with_bom_removal() encoding.new_decoder_with_bom_removal()
}; };
let rid = state.resource_table.add(TextDecoderResource { Ok(TextDecoderResource {
decoder: RefCell::new(decoder), decoder: RefCell::new(decoder),
fatal, fatal,
}); })
Ok(rid)
} }
#[op2] #[op2]
#[serde] #[serde]
fn op_encoding_decode( fn op_encoding_decode(
state: &mut OpState,
#[anybuffer] data: &[u8], #[anybuffer] data: &[u8],
#[smi] rid: ResourceId, #[cppgc] resource: &TextDecoderResource,
stream: bool, stream: bool,
) -> Result<U16String, AnyError> { ) -> Result<U16String, AnyError> {
let resource = state.resource_table.get::<TextDecoderResource>(rid)?;
let mut decoder = resource.decoder.borrow_mut(); let mut decoder = resource.decoder.borrow_mut();
let fatal = resource.fatal; let fatal = resource.fatal;
@ -357,11 +348,7 @@ struct TextDecoderResource {
fatal: bool, fatal: bool,
} }
impl Resource for TextDecoderResource { impl deno_core::GarbageCollected for TextDecoderResource {}
fn name(&self) -> Cow<str> {
"textDecoder".into()
}
}
#[op2(fast(op_encoding_encode_into_fast))] #[op2(fast(op_encoding_encode_into_fast))]
fn op_encoding_encode_into( fn op_encoding_encode_into(