Skip to content

Commit 90545d4

Browse files
authored
Refactor how error-context.* intrinsics are imported (bytecodealliance#2067)
This commit refactors the `wit-component` conventions used to import `error-context.*` intrinsics. The first change is to drop the `realloc` option on the `error-context.debug-message` intrinsic in favor of inferring that from the "general purpose import realloc" option. This avoids the need to name a symbol and enables generally relying on there being a single option for this. The second change is to change how `encoding=` is specified and bake it directly into the name instead of having a url-scheme-like encoding with multiple options. This is in theory the only option necessary for these intrinsics so it's possible to have a static set of a few names that might be chosen.
1 parent 53d1e22 commit 90545d4

File tree

4 files changed

+137
-113
lines changed

4 files changed

+137
-113
lines changed

crates/wit-component/src/encoding.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,13 +1324,13 @@ impl<'a> EncodingState<'a> {
13241324
shim.options.into_iter(*encoding, self.memory_index, None)?,
13251325
),
13261326
ShimKind::ErrorContextDebugMessage {
1327-
encoding,
13281327
for_module,
1329-
realloc,
1328+
encoding,
13301329
} => {
13311330
let instance_index = self.instance_for(*for_module);
1332-
let realloc_index =
1333-
Some(self.core_alias_export(instance_index, realloc, ExportKind::Func));
1331+
let realloc = self.info.exports_for(*for_module).import_realloc_fallback();
1332+
let realloc_index = realloc
1333+
.map(|r| self.core_alias_export(instance_index, r, ExportKind::Func));
13341334

13351335
self.component
13361336
.error_context_debug_message(shim.options.into_iter(
@@ -1740,15 +1740,13 @@ impl<'a> EncodingState<'a> {
17401740
encoding: *encoding,
17411741
},
17421742
)),
1743-
Import::ErrorContextDebugMessage { encoding, realloc } => Ok(self
1744-
.materialize_shim_import(
1745-
shims,
1746-
&ShimKind::ErrorContextDebugMessage {
1747-
for_module,
1748-
encoding: *encoding,
1749-
realloc,
1750-
},
1751-
)),
1743+
Import::ErrorContextDebugMessage { encoding } => Ok(self.materialize_shim_import(
1744+
shims,
1745+
&ShimKind::ErrorContextDebugMessage {
1746+
for_module,
1747+
encoding: *encoding,
1748+
},
1749+
)),
17521750
Import::ErrorContextDrop => {
17531751
let index = self.component.error_context_drop();
17541752
Ok((ExportKind::Func, index))
@@ -2092,9 +2090,6 @@ enum ShimKind<'a> {
20922090
for_module: CustomModule<'a>,
20932091
/// The string encoding to use when lowering the debug message.
20942092
encoding: StringEncoding,
2095-
/// The realloc function to use when allocating linear memory for the
2096-
/// debug message.
2097-
realloc: &'a str,
20982093
},
20992094
}
21002095

@@ -2293,7 +2288,7 @@ impl<'a> Shims<'a> {
22932288
});
22942289
}
22952290

2296-
Import::ErrorContextDebugMessage { encoding, realloc } => {
2291+
Import::ErrorContextDebugMessage { encoding } => {
22972292
let name = self.shims.len().to_string();
22982293
self.push(Shim {
22992294
name,
@@ -2304,7 +2299,6 @@ impl<'a> Shims<'a> {
23042299
kind: ShimKind::ErrorContextDebugMessage {
23052300
for_module,
23062301
encoding: *encoding,
2307-
realloc,
23082302
},
23092303
sig: WasmSignature {
23102304
params: vec![WasmType::I32; 2],

crates/wit-component/src/validation.rs

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,7 @@ pub enum Import {
383383
/// This allows the guest to retrieve the debug message from a
384384
/// `error-context` instance. Note that the content of this message might
385385
/// not be identical to what was passed in to `error-context.new`.
386-
ErrorContextDebugMessage {
387-
encoding: StringEncoding,
388-
realloc: String,
389-
},
386+
ErrorContextDebugMessage { encoding: StringEncoding },
390387

391388
/// A `canon error-context.drop` intrinsic.
392389
///
@@ -633,14 +630,11 @@ impl ImportMap {
633630
return Ok(Import::ErrorContextNew { encoding });
634631
}
635632

636-
if let Some((encoding, realloc)) = names.error_context_debug_message(name) {
633+
if let Some(encoding) = names.error_context_debug_message(name) {
637634
validate_not_async()?;
638635
let expected = FuncType::new([ValType::I32; 2], []);
639636
validate_func_sig(name, &expected, ty)?;
640-
return Ok(Import::ErrorContextDebugMessage {
641-
encoding,
642-
realloc: realloc.to_owned(),
643-
});
637+
return Ok(Import::ErrorContextDebugMessage { encoding });
644638
}
645639

646640
let key = WorldKey::Name(name.to_string());
@@ -1271,6 +1265,13 @@ impl ExportMap {
12711265
// `cabi_realloc_{name}` or something like that.
12721266
let _ = (interface, func);
12731267

1268+
self.import_realloc_fallback()
1269+
}
1270+
1271+
/// Returns the general-purpose realloc function to use for imports.
1272+
///
1273+
/// Note that `import_realloc_for` should be used instead where possible.
1274+
pub fn import_realloc_fallback(&self) -> Option<&str> {
12741275
if let Some(name) = self.find(|m| matches!(m, Export::GeneralPurposeImportRealloc)) {
12751276
return Some(name);
12761277
}
@@ -1420,7 +1421,7 @@ trait NameMangling {
14201421
fn async_name<'a>(&self, s: &'a str) -> Option<&'a str>;
14211422
fn async_stackful_name<'a>(&self, s: &'a str) -> Option<&'a str>;
14221423
fn error_context_new(&self, s: &str) -> Option<StringEncoding>;
1423-
fn error_context_debug_message<'a>(&self, s: &'a str) -> Option<(StringEncoding, &'a str)>;
1424+
fn error_context_debug_message(&self, s: &str) -> Option<StringEncoding>;
14241425
fn error_context_drop(&self) -> Option<&str>;
14251426
fn module_to_interface(
14261427
&self,
@@ -1519,12 +1520,10 @@ impl NameMangling for Standard {
15191520
_ = s;
15201521
None
15211522
}
1522-
fn error_context_new(&self, s: &str) -> Option<StringEncoding> {
1523-
_ = s;
1523+
fn error_context_new(&self, _: &str) -> Option<StringEncoding> {
15241524
None
15251525
}
1526-
fn error_context_debug_message<'a>(&self, s: &'a str) -> Option<(StringEncoding, &'a str)> {
1527-
_ = s;
1526+
fn error_context_debug_message(&self, _: &str) -> Option<StringEncoding> {
15281527
None
15291528
}
15301529
fn error_context_drop(&self) -> Option<&str> {
@@ -1701,37 +1700,21 @@ impl NameMangling for Legacy {
17011700
fn async_stackful_name<'a>(&self, s: &'a str) -> Option<&'a str> {
17021701
s.strip_prefix("[async-stackful]")
17031702
}
1704-
fn error_context_new(&self, s: &str) -> Option<StringEncoding> {
1705-
parse_encoding(
1706-
s.strip_prefix("[error-context-new;encoding=")?
1707-
.strip_suffix("]")?,
1708-
)
1703+
fn error_context_new(&self, name: &str) -> Option<StringEncoding> {
1704+
match name {
1705+
"[error-context-new-utf8]" => Some(StringEncoding::UTF8),
1706+
"[error-context-new-utf16]" => Some(StringEncoding::UTF16),
1707+
"[error-context-new-latin1+utf16]" => Some(StringEncoding::CompactUTF16),
1708+
_ => None,
1709+
}
17091710
}
1710-
fn error_context_debug_message<'a>(&self, s: &'a str) -> Option<(StringEncoding, &'a str)> {
1711-
let mut suffix = s.strip_prefix("[error-context-debug-message;")?;
1712-
let mut encoding = None;
1713-
let mut realloc = None;
1714-
loop {
1715-
if let Some(index) = suffix.find(';').or_else(|| suffix.find(']')) {
1716-
if let Some(suffix) = suffix[..index].strip_prefix("encoding=") {
1717-
if encoding.is_some() {
1718-
return None;
1719-
}
1720-
encoding = parse_encoding(suffix)
1721-
} else if let Some(suffix) = suffix[..index].strip_prefix("realloc=") {
1722-
if realloc.is_some() {
1723-
return None;
1724-
}
1725-
realloc = Some(suffix);
1726-
} else {
1727-
return None;
1728-
}
1729-
suffix = &suffix[index + 1..];
1730-
} else {
1731-
break;
1732-
}
1711+
fn error_context_debug_message(&self, name: &str) -> Option<StringEncoding> {
1712+
match name {
1713+
"[error-context-debug-message-utf8]" => Some(StringEncoding::UTF8),
1714+
"[error-context-debug-message-utf16]" => Some(StringEncoding::UTF16),
1715+
"[error-context-debug-message-latin1+utf16]" => Some(StringEncoding::CompactUTF16),
1716+
_ => None,
17331717
}
1734-
Some((encoding?, realloc?))
17351718
}
17361719
fn error_context_drop(&self) -> Option<&str> {
17371720
Some("[error-context-drop]")
@@ -2043,12 +2026,3 @@ fn get_function<'a>(
20432026
};
20442027
Ok(function)
20452028
}
2046-
2047-
fn parse_encoding(s: &str) -> Option<StringEncoding> {
2048-
match s {
2049-
"utf8" => Some(StringEncoding::UTF8),
2050-
"utf16" => Some(StringEncoding::UTF16),
2051-
"compact-utf16" => Some(StringEncoding::CompactUTF16),
2052-
_ => None,
2053-
}
2054-
}

0 commit comments

Comments
 (0)