Skip to content

Commit

Permalink
Merge with upstream (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhil authored Aug 16, 2024
2 parents 4d522a3 + 878eedb commit 2cc5da3
Show file tree
Hide file tree
Showing 922 changed files with 99,555 additions and 1,611 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ that can be use programmatically as well:
| `wasm-tools component embed` | | | Embed a `component-type` custom section in a core wasm binary |
| `wasm-tools metadata show` | [wasm-metadata] | | Show name and producer metadata in a component or module |
| `wasm-tools metadata add` | | | Add name or producer metadata to a component or module |
| `wasm-tools metadata unbundle` | | | Extract core wasm modules from a component |
| `wasm-tools addr2line` | | | Translate wasm offsets to filename/line numbers with DWARF |
| `wasm-tools completion` | | | Generate shell completion scripts for `wasm-tools` |
| `wasm-tools json-from-wast` | | | Convert a `*.wast` file into JSON commands |
Expand Down
6 changes: 6 additions & 0 deletions crates/wasm-encoder/src/component/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ impl ComponentNameSection {
self.component_decls(INSTANCE_SORT, names)
}

/// Appends a raw subsection with the given id and data.
pub fn raw(&mut self, id: u8, data: &[u8]) {
self.bytes.push(id);
data.encode(&mut self.bytes);
}

fn component_decls(&mut self, kind: u8, names: &NameMap) {
self.subsection_header(Subsection::Decls, 1 + names.size());
self.bytes.push(kind);
Expand Down
64 changes: 45 additions & 19 deletions crates/wasm-encoder/src/core/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,12 @@ pub enum Instruction<'a> {
Suspend(u32),
Resume {
type_index: u32,
resumetable: Cow<'a, [(u32, u32)]>,
resumetable: ResumeTable,
},
ResumeThrow {
type_index: u32,
tag_index: u32,
resumetable: Cow<'a, [(u32, u32)]>,
resumetable: ResumeTable,
},
Barrier(BlockType),

Expand Down Expand Up @@ -3370,33 +3370,43 @@ impl Encode for Instruction<'_> {
sink.push(0x4E);
memarg.encode(sink);
}
Instruction::ContNew(_type_index) => {
todo!()
Instruction::ContNew(type_index) => {
sink.push(0xE0);
type_index.encode(sink);
}
Instruction::ContBind {
src_index: _,
dst_index: _,
src_index,
dst_index,
} => {
todo!()
sink.push(0xE1);
src_index.encode(sink);
dst_index.encode(sink);
}
Instruction::Suspend(_tag_index) => {
todo!()
Instruction::Suspend(tag_index) => {
sink.push(0xE2);
tag_index.encode(sink);
}
Instruction::Resume {
type_index: _,
resumetable: _,
type_index,
ref resumetable,
} => {
todo!()
sink.push(0xE3);
type_index.encode(sink);
resumetable.encode(sink);
}
Instruction::ResumeThrow {
type_index: _,
tag_index: _,
resumetable: _,
type_index,
tag_index,
ref resumetable,
} => {
todo!()
sink.push(0xE4);
type_index.encode(sink);
tag_index.encode(sink);
resumetable.encode(sink);
}
Instruction::Barrier(_blockty) => {
todo!()
Instruction::Barrier(blockty) => {
sink.push(0xE5);
blockty.encode(sink);
}

// Atomic instructions from the shared-everything-threads proposal
Expand Down Expand Up @@ -3739,7 +3749,7 @@ impl Encode for Instruction<'_> {
}
Instruction::RefI31Shared => {
sink.push(0xFE);
sink.push(0x1F);
sink.push(0x72);
}
}
}
Expand Down Expand Up @@ -3779,6 +3789,22 @@ impl Encode for Catch {
}
}

#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct ResumeTable {
pub targets: Vec<(u32, u32)>,
}

impl Encode for ResumeTable {
fn encode(&self, sink: &mut Vec<u8>) {
self.targets.len().encode(sink);
for (tag, lbl) in &self.targets {
tag.encode(sink);
lbl.encode(sink);
}
}
}

/// A constant expression.
///
/// Usable in contexts such as offsets or initializers.
Expand Down
6 changes: 6 additions & 0 deletions crates/wasm-encoder/src/core/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ impl NameSection {
names.encode(&mut self.bytes);
}

/// Appends a raw subsection with the given id and data.
pub fn raw(&mut self, id: u8, data: &[u8]) {
self.bytes.push(id);
data.encode(&mut self.bytes);
}

fn subsection_header(&mut self, id: Subsection, len: usize) {
self.bytes.push(id as u8);
len.encode(&mut self.bytes);
Expand Down
64 changes: 44 additions & 20 deletions crates/wasm-encoder/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Encode for CompositeType {
CompositeInnerType::Struct(ty) => {
TypeSection::encode_struct(sink, ty.fields.iter().cloned())
}
CompositeInnerType::Cont(ty) => TypeSection::encode_continuation(sink, ty.0),
}
}
}
Expand All @@ -63,6 +64,8 @@ pub enum CompositeInnerType {
Array(ArrayType),
/// The type is for a struct.
Struct(StructType),
/// The type is for a continuation.
Cont(ContType),
}

/// Represents a type of a function in a WebAssembly module.
Expand All @@ -74,6 +77,10 @@ pub struct FuncType {
len_params: usize,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[allow(missing_docs)]
pub struct ContType(pub u32);

/// Represents a type of an array in a WebAssembly module.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ArrayType(pub FieldType);
Expand Down Expand Up @@ -328,29 +335,34 @@ impl RefType {

impl Encode for RefType {
fn encode(&self, sink: &mut Vec<u8>) {
if self.nullable {
// Favor the original encodings of `funcref` and `externref` where
// possible.
use AbstractHeapType::*;
match self.heap_type {
HeapType::Abstract {
shared: false,
ty: Func,
} => return sink.push(0x70),
HeapType::Abstract {
shared: false,
ty: Extern,
} => return sink.push(0x6f),
_ => {}
match self {
// Binary abbreviations (i.e., short form), for when the ref is
// nullable.
RefType {
nullable: true,
heap_type: heap @ HeapType::Abstract { .. },
} => {
heap.encode(sink);
}

// Generic 'ref null <heaptype>' encoding (i.e., long form).
RefType {
nullable: true,
heap_type,
} => {
sink.push(0x63);
heap_type.encode(sink);
}
}

if self.nullable {
sink.push(0x63);
} else {
sink.push(0x64);
// Generic 'ref <heaptype>' encoding.
RefType {
nullable: false,
heap_type,
} => {
sink.push(0x64);
heap_type.encode(sink);
}
}
self.heap_type.encode(sink);
}
}

Expand Down Expand Up @@ -612,6 +624,18 @@ impl TypeSection {
}
}

/// Define a continuation type in this type section.
pub fn cont_type(&mut self, ty: &ContType) -> &mut Self {
Self::encode_continuation(&mut self.bytes, ty.0);
self.num_added += 1;
self
}

fn encode_continuation(sink: &mut Vec<u8>, index: u32) {
sink.push(0x5d);
i64::from(index).encode(sink)
}

/// Define an explicit subtype in this type section.
pub fn subtype(&mut self, ty: &SubType) -> &mut Self {
ty.encode(&mut self.bytes);
Expand Down
Loading

0 comments on commit 2cc5da3

Please sign in to comment.