Skip to content

Commit 6b840f0

Browse files
authored
Fix Generated TypeScript Types (#888)
Apparently `Uint8Array` is ambiguous in the latest TypeScript versions, as it can be backed by either an `ArrayBuffer` or a `SharedArrayBuffer` now. If you are working with an ambiguous `Uint8Array`, you can't necessarily use it everywhere, such as when creating a `Blob` out of it. We should be precise about which ones we are returning out of our C API functions. Additionally, this implements a slight improvement in the web renderer around empty strings. Previously, there were various places where we had to create an empty string. Now, we create it once and reuse it.
1 parent ab8ee24 commit 6b840f0

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

capi/bind_gen/src/wasm_bindgen.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{typescript, Class, Function, Type, TypeKind};
1+
use crate::{Class, Function, Type, TypeKind, typescript};
22
use heck::ToLowerCamelCase;
33
use std::{
44
collections::BTreeMap,
@@ -420,13 +420,13 @@ function allocString(str: string): Slice {
420420
return { ptr, len: stats.written, cap };
421421
}
422422
423-
function decodeSlice(ptr: number): Uint8Array {
423+
function decodeSlice(ptr: number): Uint8Array<ArrayBuffer> {
424424
const memory = new Uint8Array(wasm.memory.buffer);
425425
const len = wasm.get_buf_len();
426426
return memory.slice(ptr, ptr + len);
427427
}
428428
429-
function decodePtrLen(ptr: number, len: number): Uint8Array {
429+
function decodePtrLen(ptr: number, len: number): Uint8Array<ArrayBuffer> {
430430
const memory = new Uint8Array(wasm.memory.buffer);
431431
return memory.slice(ptr, ptr + len);
432432
}
@@ -574,7 +574,7 @@ export class {class_name_ref} {{"#,
574574
writer,
575575
"{}",
576576
r#"
577-
saveAsLssBytes(): Uint8Array {
577+
saveAsLssBytes(): Uint8Array<ArrayBuffer> {
578578
if (this.ptr == 0) {
579579
throw "this is disposed";
580580
}
@@ -588,7 +588,7 @@ export class {class_name_ref} {{"#,
588588
"{}",
589589
r#"
590590
/**
591-
* @return {Uint8Array}
591+
* @return {Uint8Array<ArrayBuffer>}
592592
*/
593593
saveAsLssBytes() {
594594
if (this.ptr == 0) {
@@ -605,7 +605,7 @@ export class {class_name_ref} {{"#,
605605
writer,
606606
"{}",
607607
r#"
608-
saveAsLssBytes(): Uint8Array {
608+
saveAsLssBytes(): Uint8Array<ArrayBuffer> {
609609
if (this.ptr == 0) {
610610
throw "this is disposed";
611611
}
@@ -619,7 +619,7 @@ export class {class_name_ref} {{"#,
619619
"{}",
620620
r#"
621621
/**
622-
* @return {Uint8Array}
622+
* @return {Uint8Array<ArrayBuffer>}
623623
*/
624624
saveAsLssBytes() {
625625
if (this.ptr == 0) {
@@ -642,7 +642,7 @@ export class {class_name_ref} {{"#,
642642
* data may not even represent a valid image at all. If the image is not in the
643643
* cache, null is returned. This does not mark the image as visited.
644644
*/
645-
lookupData(key: string): Uint8Array | undefined {
645+
lookupData(key: string): Uint8Array<ArrayBuffer> | undefined {
646646
if (this.ptr == 0) {
647647
throw "this is disposed";
648648
}
@@ -668,7 +668,7 @@ export class {class_name_ref} {{"#,
668668
* cache, null is returned. This does not mark the image as visited.
669669
*
670670
* @param {string} key
671-
* @return {Uint8Array | undefined}
671+
* @return {Uint8Array<ArrayBuffer> | undefined}
672672
*/
673673
lookupData(key) {
674674
if (this.ptr == 0) {

src/rendering/web/mod.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,25 @@ struct CanvasLabelInner {
165165
width_without_max_width: f32,
166166
}
167167

168-
impl Default for CanvasLabelInner {
169-
fn default() -> Self {
170-
Self {
168+
struct CanvasLabel(Rc<RefCell<CanvasLabelInner>>);
169+
170+
impl CanvasLabel {
171+
fn new(empty_string: &JsString) -> Self {
172+
Self(Rc::new(RefCell::new(CanvasLabelInner {
171173
font: Rc::new(CanvasFont {
172-
descriptor: "".into(),
174+
descriptor: empty_string.clone(),
173175
font_handling: FontHandling::Normal,
174-
font_kerning: "".into(),
176+
font_kerning: empty_string.clone(),
175177
top: 0.0,
176178
bottom: 0.0,
177179
}),
178-
shape: LabelShape::Normal("".into()),
180+
shape: LabelShape::Normal(empty_string.clone()),
179181
width: 0.0,
180182
width_without_max_width: 0.0,
181-
}
183+
})))
182184
}
183185
}
184186

185-
#[derive(Default)]
186-
struct CanvasLabel(Rc<RefCell<CanvasLabelInner>>);
187-
188187
impl SharedOwnership for CanvasLabel {
189188
fn share(&self) -> Self {
190189
Self(self.0.share())
@@ -310,7 +309,7 @@ impl ResourceAllocator for CanvasAllocator {
310309
// FIXME: We query this to position a gradient from the top to the
311310
// bottom of the font. Is the ascent and descent what we want here?
312311
// That's not what we do in our default text engine.
313-
let metrics = self.ctx_top.measure_text(&JsString::from("")).unwrap();
312+
let metrics = self.ctx_top.measure_text(&self.cache.empty_string).unwrap();
314313
let top = -metrics.font_bounding_box_ascent() as f32;
315314
let bottom = metrics.font_bounding_box_descent() as f32;
316315

@@ -362,7 +361,7 @@ impl ResourceAllocator for CanvasAllocator {
362361
font: &mut Self::Font,
363362
max_width: Option<f32>,
364363
) -> Self::Label {
365-
let mut label = CanvasLabel::default();
364+
let mut label = CanvasLabel::new(&self.cache.empty_string);
366365
self.update_label(&mut label, text, font, max_width);
367366
label
368367
}
@@ -491,6 +490,7 @@ struct JsValueCache {
491490
colors: HashMap<[u8; 16], JsString>,
492491
filters: HashMap<HashFilter, JsString>,
493492
digits: [JsString; 10],
493+
empty_string: JsString,
494494
transparent: JsString,
495495
none: JsString,
496496
auto: JsString,
@@ -673,6 +673,7 @@ impl Renderer {
673673
colors: HashMap::new(),
674674
filters: HashMap::new(),
675675
digits: array::from_fn(|digit| JsString::from((digit as u8 + b'0') as char)),
676+
empty_string: JsString::from(""),
676677
transparent: JsString::from("transparent"),
677678
none: JsString::from("none"),
678679
auto: JsString::from("auto"),

0 commit comments

Comments
 (0)