Skip to content

Commit 1723e9d

Browse files
committed
More simplifications
1 parent 1e4cac9 commit 1723e9d

File tree

1 file changed

+19
-40
lines changed

1 file changed

+19
-40
lines changed

crates/cli-support/src/webidl/outgoing.rs

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ pub enum NonstandardOutgoing {
6060
kind: VectorKind,
6161
},
6262

63+
/// A Rust String (or &str) which might be cached, and might be `None`.
6364
///
65+
/// If `offset` is 0 then it is cached, and the cached JsValue's index is in `length`.
66+
///
67+
/// If `offset` and `length` are both 0, then it is `None`.
6468
CachedString {
6569
offset: u32,
6670
length: u32,
@@ -247,16 +251,7 @@ impl OutgoingBuilder<'_> {
247251
Descriptor::Ref(d) => self.process_ref(false, d)?,
248252
Descriptor::RefMut(d) => self.process_ref(true, d)?,
249253

250-
Descriptor::CachedString => {
251-
let offset = self.push_wasm(ValType::I32);
252-
let length = self.push_wasm(ValType::I32);
253-
self.webidl.push(ast::WebidlScalarType::Any);
254-
self.bindings.push(NonstandardOutgoing::CachedString {
255-
offset,
256-
length,
257-
owned: true,
258-
})
259-
}
254+
Descriptor::CachedString => self.cached_string(true),
260255

261256
Descriptor::Vector(_) | Descriptor::String => {
262257
let kind = arg.vector_kind().ok_or_else(|| {
@@ -299,16 +294,7 @@ impl OutgoingBuilder<'_> {
299294
self.bindings
300295
.push(NonstandardOutgoing::BorrowedAnyref { idx });
301296
}
302-
Descriptor::CachedString => {
303-
let offset = self.push_wasm(ValType::I32);
304-
let length = self.push_wasm(ValType::I32);
305-
self.webidl.push(ast::WebidlScalarType::DomString);
306-
self.bindings.push(NonstandardOutgoing::CachedString {
307-
offset,
308-
length,
309-
owned: false,
310-
})
311-
}
297+
Descriptor::CachedString => self.cached_string(false),
312298
Descriptor::Slice(_) | Descriptor::String => {
313299
use wasm_webidl_bindings::ast::WebidlScalarType::*;
314300

@@ -451,16 +437,7 @@ impl OutgoingBuilder<'_> {
451437
Descriptor::Ref(d) => self.process_option_ref(false, d)?,
452438
Descriptor::RefMut(d) => self.process_option_ref(true, d)?,
453439

454-
Descriptor::CachedString => {
455-
let offset = self.push_wasm(ValType::I32);
456-
let length = self.push_wasm(ValType::I32);
457-
self.webidl.push(ast::WebidlScalarType::DomString);
458-
self.bindings.push(NonstandardOutgoing::CachedString {
459-
offset,
460-
length,
461-
owned: true,
462-
})
463-
}
440+
Descriptor::CachedString => self.cached_string(true),
464441

465442
Descriptor::String | Descriptor::Vector(_) => {
466443
let kind = arg.vector_kind().ok_or_else(|| {
@@ -495,16 +472,7 @@ impl OutgoingBuilder<'_> {
495472
self.bindings
496473
.push(NonstandardOutgoing::BorrowedAnyref { idx });
497474
}
498-
Descriptor::CachedString => {
499-
let offset = self.push_wasm(ValType::I32);
500-
let length = self.push_wasm(ValType::I32);
501-
self.webidl.push(ast::WebidlScalarType::DomString);
502-
self.bindings.push(NonstandardOutgoing::CachedString {
503-
offset,
504-
length,
505-
owned: false,
506-
})
507-
}
475+
Descriptor::CachedString => self.cached_string(false),
508476
Descriptor::String | Descriptor::Slice(_) => {
509477
let kind = arg.vector_kind().ok_or_else(|| {
510478
format_err!(
@@ -555,6 +523,17 @@ impl OutgoingBuilder<'_> {
555523
.push(NonstandardOutgoing::Standard(binding.into()));
556524
}
557525

526+
fn cached_string(&mut self, owned: bool) {
527+
let offset = self.push_wasm(ValType::I32);
528+
let length = self.push_wasm(ValType::I32);
529+
self.webidl.push(ast::WebidlScalarType::DomString);
530+
self.bindings.push(NonstandardOutgoing::CachedString {
531+
offset,
532+
length,
533+
owned,
534+
})
535+
}
536+
558537
fn option_native(&mut self, signed: bool, ty: ValType) {
559538
let present = self.push_wasm(ValType::I32);
560539
let val = self.push_wasm(ty);

0 commit comments

Comments
 (0)