Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions crates/wasm-encoder/src/component/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ impl ComponentBuilder {
inc(&mut self.core_funcs)
}

/// Declares a new `resource.drop` intrinsic.
pub fn resource_drop_async(&mut self, ty: u32) -> u32 {
self.canonical_functions().resource_drop_async(ty);
inc(&mut self.core_funcs)
}

/// Declares a new `resource.new` intrinsic.
pub fn resource_new(&mut self, ty: u32) -> u32 {
self.canonical_functions().resource_new(ty);
Expand All @@ -385,15 +391,19 @@ impl ComponentBuilder {
inc(&mut self.core_funcs)
}

/// Declares a new `task.backpressure` intrinsic.
pub fn task_backpressure(&mut self) -> u32 {
self.canonical_functions().task_backpressure();
/// Declares a new `backpressure.set` intrinsic.
pub fn backpressure_set(&mut self) -> u32 {
self.canonical_functions().backpressure_set();
inc(&mut self.core_funcs)
}

/// Declares a new `task.return` intrinsic.
pub fn task_return(&mut self, ty: Option<impl Into<ComponentValType>>) -> u32 {
self.canonical_functions().task_return(ty);
pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> u32
where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{
self.canonical_functions().task_return(ty, options);
inc(&mut self.core_funcs)
}

Expand Down
86 changes: 37 additions & 49 deletions crates/wasm-encoder/src/component/canonicals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,10 @@ impl CanonicalFunctionSection {
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{
let options = options.into_iter();
self.bytes.push(0x00);
self.bytes.push(0x00);
core_func_index.encode(&mut self.bytes);
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
type_index.encode(&mut self.bytes);
self.num_added += 1;
self
Expand All @@ -120,14 +116,10 @@ impl CanonicalFunctionSection {
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{
let options = options.into_iter();
self.bytes.push(0x01);
self.bytes.push(0x00);
func_index.encode(&mut self.bytes);
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand All @@ -149,6 +141,14 @@ impl CanonicalFunctionSection {
self
}

/// Defines a function which will drop the specified type of handle.
pub fn resource_drop_async(&mut self, ty_index: u32) -> &mut Self {
self.bytes.push(0x07);
ty_index.encode(&mut self.bytes);
self.num_added += 1;
self
}

/// Defines a function which will return the representation of the specified
/// resource type.
pub fn resource_rep(&mut self, ty_index: u32) -> &mut Self {
Expand Down Expand Up @@ -179,7 +179,7 @@ impl CanonicalFunctionSection {
/// backpressure for the caller's instance. When backpressure is enabled,
/// the host must not start any new calls to that instance until
/// backpressure is disabled.
pub fn task_backpressure(&mut self) -> &mut Self {
pub fn backpressure_set(&mut self) -> &mut Self {
self.bytes.push(0x08);
self.num_added += 1;
self
Expand All @@ -188,15 +188,14 @@ impl CanonicalFunctionSection {
/// Defines a function which returns a result to the caller of a lifted
/// export function. This allows the callee to continue executing after
/// returning a result.
pub fn task_return(&mut self, ty: Option<impl Into<ComponentValType>>) -> &mut Self {
pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{
self.bytes.push(0x09);
if let Some(ty) = ty {
self.bytes.push(0x00);
ty.into().encode(&mut self.bytes);
} else {
self.bytes.push(0x01);
0_usize.encode(&mut self.bytes);
}
crate::encode_resultlist(&mut self.bytes, ty);
self.encode_options(options);
self.num_added += 1;
self
}
Expand Down Expand Up @@ -261,11 +260,7 @@ impl CanonicalFunctionSection {
{
self.bytes.push(0x0f);
ty.encode(&mut self.bytes);
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand All @@ -278,11 +273,7 @@ impl CanonicalFunctionSection {
{
self.bytes.push(0x10);
ty.encode(&mut self.bytes);
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand Down Expand Up @@ -342,11 +333,7 @@ impl CanonicalFunctionSection {
{
self.bytes.push(0x16);
ty.encode(&mut self.bytes);
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand All @@ -359,11 +346,7 @@ impl CanonicalFunctionSection {
{
self.bytes.push(0x17);
ty.encode(&mut self.bytes);
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand Down Expand Up @@ -414,11 +397,7 @@ impl CanonicalFunctionSection {
O::IntoIter: ExactSizeIterator,
{
self.bytes.push(0x1c);
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand All @@ -434,11 +413,7 @@ impl CanonicalFunctionSection {
O::IntoIter: ExactSizeIterator,
{
self.bytes.push(0x1d);
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self.encode_options(options);
self.num_added += 1;
self
}
Expand All @@ -449,6 +424,19 @@ impl CanonicalFunctionSection {
self.num_added += 1;
self
}

fn encode_options<O>(&mut self, options: O) -> &mut Self
where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{
let options = options.into_iter();
options.len().encode(&mut self.bytes);
for option in options {
option.encode(&mut self.bytes);
}
self
}
}

impl Encode for CanonicalFunctionSection {
Expand Down
24 changes: 14 additions & 10 deletions crates/wasm-encoder/src/component/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,20 +405,24 @@ impl<'a> ComponentFuncTypeEncoder<'a> {
assert!(self.params_encoded);
assert!(!self.results_encoded);
self.results_encoded = true;
match ty {
Some(ty) => {
self.sink.push(0x00);
ty.encode(self.sink);
}
None => {
self.sink.push(0x01);
self.sink.push(0x00);
}
}
encode_resultlist(self.sink, ty);
self
}
}

pub(crate) fn encode_resultlist(sink: &mut Vec<u8>, ty: Option<ComponentValType>) {
match ty {
Some(ty) => {
sink.push(0x00);
ty.encode(sink);
}
None => {
sink.push(0x01);
sink.push(0x00);
}
}
}

/// Used to encode component and instance types.
#[derive(Debug)]
pub struct ComponentTypeEncoder<'a>(&'a mut Vec<u8>);
Expand Down
15 changes: 11 additions & 4 deletions crates/wasm-encoder/src/reencode/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,10 @@ pub mod component_utils {
let resource = reencoder.component_type_index(resource);
section.resource_drop(resource);
}
wasmparser::CanonicalFunction::ResourceDropAsync { resource } => {
let resource = reencoder.component_type_index(resource);
section.resource_drop_async(resource);
}
wasmparser::CanonicalFunction::ResourceRep { resource } => {
let resource = reencoder.component_type_index(resource);
section.resource_rep(resource);
Expand All @@ -956,11 +960,14 @@ pub mod component_utils {
wasmparser::CanonicalFunction::ThreadAvailableParallelism => {
section.thread_available_parallelism();
}
wasmparser::CanonicalFunction::TaskBackpressure => {
section.task_backpressure();
wasmparser::CanonicalFunction::BackpressureSet => {
section.backpressure_set();
}
wasmparser::CanonicalFunction::TaskReturn { result } => {
section.task_return(result.map(|ty| reencoder.component_val_type(ty)));
wasmparser::CanonicalFunction::TaskReturn { result, options } => {
section.task_return(
result.map(|ty| reencoder.component_val_type(ty)),
options.iter().map(|o| reencoder.canonical_option(*o)),
);
}
wasmparser::CanonicalFunction::TaskWait { async_, memory } => {
section.task_wait(async_, reencoder.memory_index(memory));
Expand Down
Loading