Skip to content

Commit 30c7a09

Browse files
authored
stop kebab-casing and validate component names (#1294)
* stop kebab-casing and validate component names * tests * validate for wasm-compose * don't validate , change name to composition-root * new tests in * lose import map cow and shorten compostition root name
1 parent fe363f0 commit 30c7a09

File tree

30 files changed

+186
-43
lines changed

30 files changed

+186
-43
lines changed

crates/wasm-compose/CONFIG.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ To specify the configuration file to use, pass the `--config` option to
77

88
The configuration file has the following top-level fields:
99

10-
* `search-paths` : `list<string>` (optional) - a list of paths to search for
11-
dependencies.
12-
* `skip-validation` : `bool` (optional) - a boolean indicating whether to skip
13-
validation of the resulting composed component.
14-
* `dependencies` : `map<string, dependency>` (optional) - a map specifying the
15-
explicit locations of transitive dependencies.
16-
* `instantiations` : `map<string, instantiation>` (optional) - a map specifying
17-
the explicit instantiations of transitive dependencies.
18-
* `definitions` : `list<string>` (optional) - a list of paths to _definition_
19-
components.
10+
- `search-paths` : `list<string>` (optional) - a list of paths to search for
11+
dependencies.
12+
- `skip-validation` : `bool` (optional) - a boolean indicating whether to skip
13+
validation of the resulting composed component.
14+
- `dependencies` : `map<string, dependency>` (optional) - a map specifying the
15+
explicit locations of transitive dependencies.
16+
- `instantiations` : `map<string, instantiation>` (optional) - a map specifying
17+
the explicit instantiations of transitive dependencies.
18+
- `definitions` : `list<string>` (optional) - a list of paths to _definition_
19+
components.
2020

2121
## Dependencies
2222

@@ -25,10 +25,10 @@ dependency, rather than searching the configured search paths.
2525

2626
A dependency has the following fields:
2727

28-
* `path` : `string` - the path to the WebAssembly component file; the path is
28+
- `path` : `string` - the path to the WebAssembly component file; the path is
2929
relative to the configuration file.
3030

31-
* `import` : `string` (optional) - the name to use for importing the component.
31+
- `import` : `string` (optional) - the name to use for importing the component.
3232
If not present, the component at the given path will be defined directly in
3333
the composed component.
3434

@@ -49,11 +49,11 @@ dependencies:
4949
5050
In the above example, two dependencies are defined in the configuration:
5151
52-
* `a` - the contents of `a.wasm` will be defined directly in the composed
53-
component.
54-
* `b` - a component of the same type as `b.wasm` will be imported in the
55-
composed component with name `b`; the original file will not be embedded in the
56-
composed component.
52+
- `a` - the contents of `a.wasm` will be defined directly in the composed
53+
component.
54+
- `b` - a component of the same type as `b.wasm` will be imported in the
55+
composed component with name `b`; the original file will not be embedded in the
56+
composed component.
5757

5858
_Note: importing components from a composed component is not currently
5959
supported in [Wasmtime](https://github.com/bytecodealliance/wasmtime)._
@@ -68,19 +68,19 @@ instantiated, even allowing a dependency to be instantiated multiple times.
6868

6969
An instantiation has the following fields:
7070

71-
* `dependency` : `string` (optional) - the name of the dependency to
71+
- `dependency` : `string` (optional) - the name of the dependency to
7272
instantiate.
7373

7474
If unspecified, the instantiation will be for a dependency of the same name
7575
as the instantiation itself.
7676

77-
* `arguments` : `map<string, argument>` (optional) - a mapping of argument
77+
- `arguments` : `map<string, argument>` (optional) - a mapping of argument
7878
names to arguments.
7979

8080
Argument names match the names of the imports of the dependency being
8181
instantiated.
8282

83-
Note that the instantiation name `$input` is special and signifies how the input
83+
Note that the instantiation name `root` is special and signifies how the input
8484
component is to be instantiated.
8585

8686
### Instantiation arguments
@@ -90,9 +90,9 @@ to the instantiation of a dependency.
9090

9191
An argument has the following fields:
9292

93-
* `instance` : `string` - the name of the instance to pass as the argument.
93+
- `instance` : `string` - the name of the instance to pass as the argument.
9494

95-
* `export` : `string` (optional) - the name of the exported instance on
95+
- `export` : `string` (optional) - the name of the exported instance on
9696
`instance` to use as the argument.
9797

9898
If not present, the instance specified by `instance` will be passed directly.
@@ -106,7 +106,7 @@ A slightly complex example of configuring instantiations:
106106

107107
```yaml
108108
instantiations:
109-
$input:
109+
root:
110110
arguments:
111111
a: b
112112
b:
@@ -118,7 +118,7 @@ instantiations:
118118
dependency: f
119119
```
120120

121-
In the above example, the `$input` instantiation (i.e. the root instantiation)
121+
In the above example, the `root` instantiation (i.e. the root instantiation)
122122
has explicitly specified that the argument named `a` is to be provided instance
123123
`b`.
124124

crates/wasm-compose/example/server/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ search-paths:
22
- ../service/target/wasm32-wasi/release
33

44
instantiations:
5-
$input:
5+
root:
66
arguments:
77
example:service/[email protected]:
88
instance: svc

crates/wasm-compose/src/composer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use wasmparser::{
1717
};
1818

1919
/// The root component name used in configuration.
20-
pub const ROOT_COMPONENT_NAME: &str = "$input";
20+
pub const ROOT_COMPONENT_NAME: &str = "root";
2121

2222
/// A reference to an instance import on a component.
2323
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]

crates/wasm-compose/src/encoding.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::graph::{
22
type_desc, CompositionGraph, EncodeOptions, ExportIndex, ImportIndex, InstanceId,
33
};
44
use anyhow::{anyhow, bail, Result};
5-
use heck::ToKebabCase;
65
use indexmap::{IndexMap, IndexSet};
76
use petgraph::EdgeDirection;
87
use smallvec::SmallVec;
@@ -1038,7 +1037,7 @@ enum ImportMapEntry<'a> {
10381037
/// Represents the import map built during the encoding
10391038
/// of a composition graph.
10401039
#[derive(Default)]
1041-
struct ImportMap<'a>(IndexMap<Cow<'a, str>, ImportMapEntry<'a>>);
1040+
struct ImportMap<'a>(IndexMap<&'a str, ImportMapEntry<'a>>);
10421041

10431042
impl<'a> ImportMap<'a> {
10441043
fn new(import_components: bool, graph: &'a CompositionGraph) -> Result<Self> {
@@ -1062,7 +1061,7 @@ impl<'a> ImportMap<'a> {
10621061
assert!(self
10631062
.0
10641063
.insert(
1065-
entry.component.name.to_kebab_case().into(),
1064+
&entry.component.name,
10661065
ImportMapEntry::Component(&entry.component),
10671066
)
10681067
.is_none());

crates/wasm-compose/src/graph.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{
1111
sync::atomic::{AtomicUsize, Ordering},
1212
};
1313
use wasmparser::{
14+
names::ComponentName,
1415
types::{
1516
ComponentAnyTypeId, ComponentEntityType, ComponentInstanceTypeId, Remap, Remapping,
1617
ResourceId, SubtypeCx, Types, TypesRef,
@@ -48,12 +49,11 @@ pub struct Component<'a> {
4849

4950
impl<'a> Component<'a> {
5051
/// Constructs a new component from reading the given file.
51-
pub fn from_file(name: impl Into<String>, path: impl AsRef<Path>) -> Result<Self> {
52+
pub fn from_file(name: &str, path: impl AsRef<Path>) -> Result<Self> {
5253
let path = path.as_ref();
5354
log::info!("parsing WebAssembly component file `{}`", path.display());
54-
5555
let component = Self::parse(
56-
name.into(),
56+
ComponentName::new(name, 0)?.to_string(),
5757
Some(path.to_owned()),
5858
wat::parse_file(path)
5959
.with_context(|| {
@@ -83,8 +83,12 @@ impl<'a> Component<'a> {
8383
}
8484

8585
log::info!("parsing WebAssembly component from bytes");
86-
let component =
87-
Self::parse(name.into(), None, bytes).context("failed to parse component")?;
86+
let component = Self::parse(
87+
ComponentName::new(&name.into(), 0)?.to_string(),
88+
None,
89+
bytes,
90+
)
91+
.context("failed to parse component")?;
8892

8993
log::debug!("WebAssembly component parsed:\n{component:#?}",);
9094

crates/wasm-compose/tests/compositions/complex-import/composed.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(export (;1;) "m2" (func (type 1)))
1515
)
1616
)
17-
(import "input" (component (;0;) (type 0)))
17+
(import "root" (component (;0;) (type 0)))
1818
(type (;1;)
1919
(component
2020
(type (;0;)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cannot import instance with name `a` for an instantiation argument of component `b` because it conflicts with an imported instantiation argument of component `$input`
1+
cannot import instance with name `a` for an instantiation argument of component `b` because it conflicts with an imported instantiation argument of component `root`

crates/wasm-compose/tests/compositions/connect-resources/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
instantiations:
2-
$input:
2+
root:
33
arguments:
44
example:service/[email protected]:
55
instance: logger
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
failed to connect instance `$input` to definition component `tests/compositions/def-mismatch/definitions.wat`
1+
failed to connect instance `root` to definition component `tests/compositions/def-mismatch/definitions.wat`
22

33
Caused by:
44
source instance export `foo` is not compatible with target instance import `foo`

crates/wasm-compose/tests/compositions/export-on-import/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
instantiations:
2-
$input:
2+
root:
33
arguments:
44
a:
55
instance: a

0 commit comments

Comments
 (0)