Skip to content

Commit 940de62

Browse files
committed
rename error to error-context per latest spec (part 2)
Also, parse string encoding and realloc from encoded `error-context.new` and `error-context.debug-string` names. Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent d5a6c1b commit 940de62

10 files changed

Lines changed: 174 additions & 120 deletions

File tree

crates/wasm-encoder/src/component/builder.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -520,29 +520,30 @@ impl ComponentBuilder {
520520
inc(&mut self.core_funcs)
521521
}
522522

523-
/// Declares a new `error.new` intrinsic.
524-
pub fn error_new<O>(&mut self, options: O) -> u32
523+
/// Declares a new `error-context.new` intrinsic.
524+
pub fn error_context_new<O>(&mut self, options: O) -> u32
525525
where
526526
O: IntoIterator<Item = CanonicalOption>,
527527
O::IntoIter: ExactSizeIterator,
528528
{
529-
self.canonical_functions().error_new(options);
529+
self.canonical_functions().error_context_new(options);
530530
inc(&mut self.core_funcs)
531531
}
532532

533-
/// Declares a new `error.debug-message` intrinsic.
534-
pub fn error_debug_message<O>(&mut self, options: O) -> u32
533+
/// Declares a new `error-context.debug-message` intrinsic.
534+
pub fn error_context_debug_message<O>(&mut self, options: O) -> u32
535535
where
536536
O: IntoIterator<Item = CanonicalOption>,
537537
O::IntoIter: ExactSizeIterator,
538538
{
539-
self.canonical_functions().error_debug_message(options);
539+
self.canonical_functions()
540+
.error_context_debug_message(options);
540541
inc(&mut self.core_funcs)
541542
}
542543

543-
/// Declares a new `error.drop` intrinsic.
544-
pub fn error_drop(&mut self) -> u32 {
545-
self.canonical_functions().error_drop();
544+
/// Declares a new `error-context.drop` intrinsic.
545+
pub fn error_context_drop(&mut self) -> u32 {
546+
self.canonical_functions().error_context_drop();
546547
inc(&mut self.core_funcs)
547548
}
548549

crates/wasm-encoder/src/component/canonicals.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ impl CanonicalFunctionSection {
399399
self
400400
}
401401

402-
/// Defines a function to create a new `error` with a specified debug
403-
/// message.
404-
pub fn error_new<O>(&mut self, options: O) -> &mut Self
402+
/// Defines a function to create a new `error-context` with a specified
403+
/// debug message.
404+
pub fn error_context_new<O>(&mut self, options: O) -> &mut Self
405405
where
406406
O: IntoIterator<Item = CanonicalOption>,
407407
O::IntoIter: ExactSizeIterator,
@@ -416,11 +416,12 @@ impl CanonicalFunctionSection {
416416
self
417417
}
418418

419-
/// Defines a function to get the debug message for a specified `error`.
419+
/// Defines a function to get the debug message for a specified
420+
/// `error-context`.
420421
///
421422
/// Note that the debug message might not necessarily match what was passed
422-
/// to `error.new`.
423-
pub fn error_debug_message<O>(&mut self, options: O) -> &mut Self
423+
/// to `error-context.new`.
424+
pub fn error_context_debug_message<O>(&mut self, options: O) -> &mut Self
424425
where
425426
O: IntoIterator<Item = CanonicalOption>,
426427
O::IntoIter: ExactSizeIterator,
@@ -435,8 +436,8 @@ impl CanonicalFunctionSection {
435436
self
436437
}
437438

438-
/// Defines a function to drop a specified `error`.
439-
pub fn error_drop(&mut self) -> &mut Self {
439+
/// Defines a function to drop a specified `error-context`.
440+
pub fn error_context_drop(&mut self) -> &mut Self {
440441
self.bytes.push(0x1e);
441442
self.num_added += 1;
442443
self

crates/wasm-encoder/src/reencode/component.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,16 @@ pub mod component_utils {
10361036
wasmparser::CanonicalFunction::FutureCloseWritable { ty } => {
10371037
section.future_close_writable(reencoder.component_type_index(ty));
10381038
}
1039-
wasmparser::CanonicalFunction::ErrorNew { options } => {
1040-
section.error_new(options.iter().map(|o| reencoder.canonical_option(*o)));
1039+
wasmparser::CanonicalFunction::ErrorContextNew { options } => {
1040+
section.error_context_new(options.iter().map(|o| reencoder.canonical_option(*o)));
10411041
}
1042-
wasmparser::CanonicalFunction::ErrorDebugMessage { options } => {
1043-
section.error_debug_message(options.iter().map(|o| reencoder.canonical_option(*o)));
1042+
wasmparser::CanonicalFunction::ErrorContextDebugMessage { options } => {
1043+
section.error_context_debug_message(
1044+
options.iter().map(|o| reencoder.canonical_option(*o)),
1045+
);
10441046
}
1045-
wasmparser::CanonicalFunction::ErrorDrop => {
1046-
section.error_drop();
1047+
wasmparser::CanonicalFunction::ErrorContextDrop => {
1048+
section.error_context_drop();
10471049
}
10481050
}
10491051
Ok(())

crates/wasmparser/src/readers/component/canonicals.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,22 @@ pub enum CanonicalFunction {
212212
/// The `future` type to expect.
213213
ty: u32,
214214
},
215-
/// A function to create a new `error` with a specified debug
215+
/// A function to create a new `error-context` with a specified debug
216216
/// message.
217-
ErrorNew {
217+
ErrorContextNew {
218218
/// String encoding, memory, etc. to use when loading debug message.
219219
options: Box<[CanonicalOption]>,
220220
},
221-
/// A function to get the debug message for a specified `error`.
221+
/// A function to get the debug message for a specified `error-context`.
222222
///
223223
/// Note that the debug message might not necessarily match what was passed
224224
/// to `error.new`.
225-
ErrorDebugMessage {
225+
ErrorContextDebugMessage {
226226
/// String encoding, memory, etc. to use when storing debug message.
227227
options: Box<[CanonicalOption]>,
228228
},
229-
/// A function to drop a specified `error`.
230-
ErrorDrop,
229+
/// A function to drop a specified `error-context`.
230+
ErrorContextDrop,
231231
}
232232

233233
/// A reader for the canonical section of a WebAssembly component.
@@ -335,17 +335,17 @@ impl<'a> FromReader<'a> for CanonicalFunction {
335335
},
336336
0x1a => CanonicalFunction::FutureCloseReadable { ty: reader.read()? },
337337
0x1b => CanonicalFunction::FutureCloseWritable { ty: reader.read()? },
338-
0x1c => CanonicalFunction::ErrorNew {
338+
0x1c => CanonicalFunction::ErrorContextNew {
339339
options: reader
340340
.read_iter(MAX_WASM_CANONICAL_OPTIONS, "canonical options")?
341341
.collect::<Result<_>>()?,
342342
},
343-
0x1d => CanonicalFunction::ErrorDebugMessage {
343+
0x1d => CanonicalFunction::ErrorContextDebugMessage {
344344
options: reader
345345
.read_iter(MAX_WASM_CANONICAL_OPTIONS, "canonical options")?
346346
.collect::<Result<_>>()?,
347347
},
348-
0x1e => CanonicalFunction::ErrorDrop,
348+
0x1e => CanonicalFunction::ErrorContextDrop,
349349
x => return reader.invalid_leading_byte(x, "canonical function"),
350350
})
351351
}

crates/wasmparser/src/validator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,13 +1370,13 @@ impl Validator {
13701370
crate::CanonicalFunction::FutureCloseWritable { ty } => {
13711371
current.future_close_writable(ty, types, offset, features)
13721372
}
1373-
crate::CanonicalFunction::ErrorNew { options } => {
1373+
crate::CanonicalFunction::ErrorContextNew { options } => {
13741374
current.error_new(options.into_vec(), types, offset, features)
13751375
}
1376-
crate::CanonicalFunction::ErrorDebugMessage { options } => {
1376+
crate::CanonicalFunction::ErrorContextDebugMessage { options } => {
13771377
current.error_debug_message(options.into_vec(), types, offset, features)
13781378
}
1379-
crate::CanonicalFunction::ErrorDrop => {
1379+
crate::CanonicalFunction::ErrorContextDrop => {
13801380
current.error_drop(types, offset, features)
13811381
}
13821382
}

crates/wasmprinter/src/component.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,31 +1142,31 @@ impl Printer<'_, '_> {
11421142
self.end_group()?;
11431143
state.core.funcs += 1;
11441144
}
1145-
CanonicalFunction::ErrorNew { options } => {
1145+
CanonicalFunction::ErrorContextNew { options } => {
11461146
self.start_group("core func ")?;
11471147
self.print_name(&state.core.func_names, state.core.funcs)?;
11481148
self.result.write_str(" ")?;
1149-
self.start_group("canon error.new ")?;
1149+
self.start_group("canon error-context.new ")?;
11501150
self.print_canonical_options(state, &options)?;
11511151
self.end_group()?;
11521152
self.end_group()?;
11531153
state.core.funcs += 1;
11541154
}
1155-
CanonicalFunction::ErrorDebugMessage { options } => {
1155+
CanonicalFunction::ErrorContextDebugMessage { options } => {
11561156
self.start_group("core func ")?;
11571157
self.print_name(&state.core.func_names, state.core.funcs)?;
11581158
self.result.write_str(" ")?;
1159-
self.start_group("canon error.debug-message ")?;
1159+
self.start_group("canon error-context.debug-message ")?;
11601160
self.print_canonical_options(state, &options)?;
11611161
self.end_group()?;
11621162
self.end_group()?;
11631163
state.core.funcs += 1;
11641164
}
1165-
CanonicalFunction::ErrorDrop => {
1165+
CanonicalFunction::ErrorContextDrop => {
11661166
self.start_group("core func ")?;
11671167
self.print_name(&state.core.func_names, state.core.funcs)?;
11681168
self.result.write_str(" ")?;
1169-
self.start_group("canon error.drop")?;
1169+
self.start_group("canon error-context.drop")?;
11701170
self.end_group()?;
11711171
self.end_group()?;
11721172
state.core.funcs += 1;

crates/wit-component/src/encoding.rs

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,31 +1380,35 @@ impl<'a> EncodingState<'a> {
13801380
ShimKind::TaskPoll { async_ } => self
13811381
.component
13821382
.task_poll(*async_, self.memory_index.unwrap()),
1383-
ShimKind::ErrorNew { for_module } | ShimKind::ErrorDebugMessage { for_module } => {
1384-
let metadata = self.info.module_metadata_for(*for_module);
1385-
let exports = self.info.exports_for(*for_module);
1386-
let instance_index = self.instance_for(*for_module);
1387-
let encoding = metadata.general_purpose_encoding();
1388-
let realloc = exports.general_purpose_realloc();
1389-
let realloc_index = realloc
1390-
.map(|name| self.core_alias_export(instance_index, name, ExportKind::Func));
1391-
1392-
match &shim.kind {
1393-
ShimKind::ErrorNew { .. } => self.component.error_new(
1394-
(RequiredOptions::MEMORY | RequiredOptions::STRING_ENCODING)
1395-
.into_iter(encoding, self.memory_index, realloc_index)?
1396-
.collect::<Vec<_>>(),
1397-
),
1398-
ShimKind::ErrorDebugMessage { .. } => self.component.error_debug_message(
1383+
ShimKind::ErrorContextNew {
1384+
for_module,
1385+
encoding,
1386+
}
1387+
| ShimKind::ErrorContextDebugMessage {
1388+
for_module,
1389+
encoding,
1390+
..
1391+
} => match &shim.kind {
1392+
ShimKind::ErrorContextNew { .. } => self.component.error_context_new(
1393+
(RequiredOptions::MEMORY | RequiredOptions::STRING_ENCODING)
1394+
.into_iter(*encoding, self.memory_index, None)?
1395+
.collect::<Vec<_>>(),
1396+
),
1397+
ShimKind::ErrorContextDebugMessage { realloc, .. } => {
1398+
let instance_index = self.instance_for(*for_module);
1399+
let realloc_index =
1400+
Some(self.core_alias_export(instance_index, realloc, ExportKind::Func));
1401+
1402+
self.component.error_context_debug_message(
13991403
(RequiredOptions::MEMORY
14001404
| RequiredOptions::STRING_ENCODING
14011405
| RequiredOptions::REALLOC)
1402-
.into_iter(encoding, self.memory_index, realloc_index)?
1406+
.into_iter(*encoding, self.memory_index, realloc_index)?
14031407
.collect::<Vec<_>>(),
1404-
),
1405-
_ => unreachable!(),
1408+
)
14061409
}
1407-
}
1410+
_ => unreachable!(),
1411+
},
14081412
};
14091413

14101414
exports.push((shim.name.as_str(), ExportKind::Func, core_func_index));
@@ -1802,26 +1806,35 @@ impl<'a> EncodingState<'a> {
18021806
let index = self.component.future_close_writable(type_index);
18031807
return Ok((ExportKind::Func, index));
18041808
}
1805-
Import::ErrorNew => {
1809+
Import::ErrorContextNew { encoding } => {
18061810
let index = self.component.core_alias_export(
18071811
self.shim_instance_index
18081812
.expect("shim should be instantiated"),
1809-
&shims.shims[&ShimKind::ErrorNew { for_module }].name,
1813+
&shims.shims[&ShimKind::ErrorContextNew {
1814+
for_module,
1815+
encoding: *encoding,
1816+
}]
1817+
.name,
18101818
ExportKind::Func,
18111819
);
18121820
return Ok((ExportKind::Func, index));
18131821
}
1814-
Import::ErrorDebugMessage => {
1822+
Import::ErrorContextDebugMessage { encoding, realloc } => {
18151823
let index = self.component.core_alias_export(
18161824
self.shim_instance_index
18171825
.expect("shim should be instantiated"),
1818-
&shims.shims[&ShimKind::ErrorDebugMessage { for_module }].name,
1826+
&shims.shims[&ShimKind::ErrorContextDebugMessage {
1827+
for_module,
1828+
encoding: *encoding,
1829+
realloc,
1830+
}]
1831+
.name,
18191832
ExportKind::Func,
18201833
);
18211834
return Ok((ExportKind::Func, index));
18221835
}
1823-
Import::ErrorDrop => {
1824-
let index = self.component.error_drop();
1836+
Import::ErrorContextDrop => {
1837+
let index = self.component.error_context_drop();
18251838
return Ok((ExportKind::Func, index));
18261839
}
18271840
Import::WorldFunc(key, name, abi) => (key, name, None, *abi),
@@ -2059,11 +2072,14 @@ enum ShimKind<'a> {
20592072
TaskPoll {
20602073
async_: bool,
20612074
},
2062-
ErrorNew {
2075+
ErrorContextNew {
20632076
for_module: CustomModule<'a>,
2077+
encoding: StringEncoding,
20642078
},
2065-
ErrorDebugMessage {
2079+
ErrorContextDebugMessage {
20662080
for_module: CustomModule<'a>,
2081+
encoding: StringEncoding,
2082+
realloc: &'a str,
20672083
},
20682084
}
20692085

@@ -2135,7 +2151,7 @@ impl<'a> Shims<'a> {
21352151
| Import::ExportedResourceDrop(..)
21362152
| Import::ExportedResourceRep(..)
21372153
| Import::ExportedResourceNew(..)
2138-
| Import::ErrorDrop
2154+
| Import::ErrorContextDrop
21392155
| Import::TaskBackpressure
21402156
| Import::TaskYield { .. }
21412157
| Import::SubtaskDrop
@@ -2234,13 +2250,16 @@ impl<'a> Shims<'a> {
22342250
continue;
22352251
}
22362252

2237-
Import::ErrorNew => {
2253+
Import::ErrorContextNew { encoding } => {
22382254
let name = self.shims.len().to_string();
22392255
self.push(Shim {
22402256
name,
22412257
debug_name: "error-new".to_string(),
22422258
options: RequiredOptions::empty(),
2243-
kind: ShimKind::ErrorNew { for_module },
2259+
kind: ShimKind::ErrorContextNew {
2260+
for_module,
2261+
encoding: *encoding,
2262+
},
22442263
sig: WasmSignature {
22452264
params: vec![WasmType::I32; 2],
22462265
results: vec![WasmType::I32],
@@ -2251,13 +2270,17 @@ impl<'a> Shims<'a> {
22512270
continue;
22522271
}
22532272

2254-
Import::ErrorDebugMessage => {
2273+
Import::ErrorContextDebugMessage { encoding, realloc } => {
22552274
let name = self.shims.len().to_string();
22562275
self.push(Shim {
22572276
name,
22582277
debug_name: "error-debug-message".to_string(),
22592278
options: RequiredOptions::empty(),
2260-
kind: ShimKind::ErrorDebugMessage { for_module },
2279+
kind: ShimKind::ErrorContextDebugMessage {
2280+
for_module,
2281+
encoding: *encoding,
2282+
realloc,
2283+
},
22612284
sig: WasmSignature {
22622285
params: vec![WasmType::I32; 2],
22632286
results: vec![],

0 commit comments

Comments
 (0)