Skip to content

Commit 8dc75f0

Browse files
wasm-encoder: Support extended constant expressions proposal (#1398)
1 parent 56d0d94 commit 8dc75f0

File tree

1 file changed

+75
-0
lines changed
  • crates/wasm-encoder/src/core

1 file changed

+75
-0
lines changed

crates/wasm-encoder/src/core/code.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,6 +3188,11 @@ impl ConstExpr {
31883188
Self { bytes }
31893189
}
31903190

3191+
fn with_insn(mut self, insn: Instruction) -> Self {
3192+
insn.encode(&mut self.bytes);
3193+
self
3194+
}
3195+
31913196
/// Create a constant expression containing a single `global.get` instruction.
31923197
pub fn global_get(index: u32) -> Self {
31933198
Self::new_insn(Instruction::GlobalGet(index))
@@ -3227,6 +3232,76 @@ impl ConstExpr {
32273232
pub fn v128_const(value: i128) -> Self {
32283233
Self::new_insn(Instruction::V128Const(value))
32293234
}
3235+
3236+
/// Add a `global.get` instruction to this constant expression.
3237+
pub fn with_global_get(self, index: u32) -> Self {
3238+
self.with_insn(Instruction::GlobalGet(index))
3239+
}
3240+
3241+
/// Add a `ref.null` instruction to this constant expression.
3242+
pub fn with_ref_null(self, ty: HeapType) -> Self {
3243+
self.with_insn(Instruction::RefNull(ty))
3244+
}
3245+
3246+
/// Add a `ref.func` instruction to this constant expression.
3247+
pub fn with_ref_func(self, func: u32) -> Self {
3248+
self.with_insn(Instruction::RefFunc(func))
3249+
}
3250+
3251+
/// Add an `i32.const` instruction to this constant expression.
3252+
pub fn with_i32_const(self, value: i32) -> Self {
3253+
self.with_insn(Instruction::I32Const(value))
3254+
}
3255+
3256+
/// Add an `i64.const` instruction to this constant expression.
3257+
pub fn with_i64_const(self, value: i64) -> Self {
3258+
self.with_insn(Instruction::I64Const(value))
3259+
}
3260+
3261+
/// Add a `f32.const` instruction to this constant expression.
3262+
pub fn with_f32_const(self, value: f32) -> Self {
3263+
self.with_insn(Instruction::F32Const(value))
3264+
}
3265+
3266+
/// Add a `f64.const` instruction to this constant expression.
3267+
pub fn with_f64_const(self, value: f64) -> Self {
3268+
self.with_insn(Instruction::F64Const(value))
3269+
}
3270+
3271+
/// Add a `v128.const` instruction to this constant expression.
3272+
pub fn with_v128_const(self, value: i128) -> Self {
3273+
self.with_insn(Instruction::V128Const(value))
3274+
}
3275+
3276+
/// Add an `i32.add` instruction to this constant expression.
3277+
pub fn with_i32_add(self) -> Self {
3278+
self.with_insn(Instruction::I32Add)
3279+
}
3280+
3281+
/// Add an `i32.sub` instruction to this constant expression.
3282+
pub fn with_i32_sub(self) -> Self {
3283+
self.with_insn(Instruction::I32Sub)
3284+
}
3285+
3286+
/// Add an `i32.mul` instruction to this constant expression.
3287+
pub fn with_i32_mul(self) -> Self {
3288+
self.with_insn(Instruction::I32Mul)
3289+
}
3290+
3291+
/// Add an `i64.add` instruction to this constant expression.
3292+
pub fn with_i64_add(self) -> Self {
3293+
self.with_insn(Instruction::I64Add)
3294+
}
3295+
3296+
/// Add an `i64.sub` instruction to this constant expression.
3297+
pub fn with_i64_sub(self) -> Self {
3298+
self.with_insn(Instruction::I64Sub)
3299+
}
3300+
3301+
/// Add an `i64.mul` instruction to this constant expression.
3302+
pub fn with_i64_mul(self) -> Self {
3303+
self.with_insn(Instruction::I64Mul)
3304+
}
32303305
}
32313306

32323307
impl Encode for ConstExpr {

0 commit comments

Comments
 (0)