Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions ext/web/08_text_encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class TextDecoder {
/** @type {boolean} */
#utf8SinglePass;

/** @type {number | null} */
#rid = null;
/** @type {object | null} */
#handle = null;

/**
* @param {string} label
Expand Down Expand Up @@ -159,7 +159,7 @@ class TextDecoder {
}

// Fast path for single pass encoding.
if (!stream && this.#rid === null) {
if (!stream && this.#handle === null) {
// Fast path for utf8 single pass encoding.
if (this.#utf8SinglePass) {
return op_encoding_decode_utf8(input, this.#ignoreBOM);
Expand All @@ -173,18 +173,17 @@ class TextDecoder {
);
}

if (this.#rid === null) {
this.#rid = op_encoding_new_decoder(
if (this.#handle === null) {
this.#handle = op_encoding_new_decoder(
this.#encoding,
this.#fatal,
this.#ignoreBOM,
);
}
return op_encoding_decode(input, this.#rid, stream);
return op_encoding_decode(input, this.#handle, stream);
} finally {
if (!stream && this.#rid !== null) {
core.close(this.#rid);
this.#rid = null;
if (!stream && this.#handle !== null) {
this.#handle = null;
}
}
}
Expand Down
27 changes: 7 additions & 20 deletions ext/web/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ use deno_core::op2;
use deno_core::url::Url;
use deno_core::v8;
use deno_core::ByteString;
use deno_core::OpState;
use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::ToJsBuffer;
use deno_core::U16String;

Expand Down Expand Up @@ -277,14 +274,13 @@ fn op_encoding_decode_single(
}
}

#[op2(fast)]
#[smi]
#[op2]
#[cppgc]
fn op_encoding_new_decoder(
state: &mut OpState,
#[string] label: &str,
fatal: bool,
ignore_bom: bool,
) -> Result<ResourceId, AnyError> {
) -> Result<TextDecoderResource, AnyError> {
let encoding = Encoding::for_label(label.as_bytes()).ok_or_else(|| {
range_error(format!(
"The encoding label provided ('{label}') is invalid."
Expand All @@ -297,24 +293,19 @@ fn op_encoding_new_decoder(
encoding.new_decoder_with_bom_removal()
};

let rid = state.resource_table.add(TextDecoderResource {
Ok(TextDecoderResource {
decoder: RefCell::new(decoder),
fatal,
});

Ok(rid)
})
}

#[op2]
#[serde]
fn op_encoding_decode(
state: &mut OpState,
#[anybuffer] data: &[u8],
#[smi] rid: ResourceId,
#[cppgc] resource: &TextDecoderResource,
stream: bool,
) -> Result<U16String, AnyError> {
let resource = state.resource_table.get::<TextDecoderResource>(rid)?;

let mut decoder = resource.decoder.borrow_mut();
let fatal = resource.fatal;

Expand Down Expand Up @@ -357,11 +348,7 @@ struct TextDecoderResource {
fatal: bool,
}

impl Resource for TextDecoderResource {
fn name(&self) -> Cow<str> {
"textDecoder".into()
}
}
impl deno_core::GarbageCollected for TextDecoderResource {}

#[op2(fast(op_encoding_encode_into_fast))]
fn op_encoding_encode_into(
Expand Down