@@ -13,7 +13,7 @@ use alloc::vec::Vec;
1313///
1414/// ```
1515/// use wasm_encoder::{
16- /// CodeSection, Function, FunctionSection, Instruction, Module,
16+ /// CodeSection, Function, FunctionSection, Module,
1717/// TypeSection, ValType
1818/// };
1919///
@@ -26,7 +26,7 @@ use alloc::vec::Vec;
2626///
2727/// let locals = vec![];
2828/// let mut func = Function::new(locals);
29- /// func.instruction(&Instruction::I32Const (42) );
29+ /// func.instructions().i32_const (42);
3030/// let mut code = CodeSection::new();
3131/// code.function(&func);
3232///
@@ -126,7 +126,7 @@ impl Section for CodeSection {
126126/// # Example
127127///
128128/// ```
129- /// use wasm_encoder::{CodeSection, Function, Instruction };
129+ /// use wasm_encoder::{CodeSection, Function, LocalIdx };
130130///
131131/// // Define the function body for:
132132/// //
@@ -136,9 +136,10 @@ impl Section for CodeSection {
136136/// // i32.add)
137137/// let locals = vec![];
138138/// let mut func = Function::new(locals);
139- /// func.instruction(&Instruction::LocalGet(0));
140- /// func.instruction(&Instruction::LocalGet(1));
141- /// func.instruction(&Instruction::I32Add);
139+ /// func.instructions()
140+ /// .local_get(LocalIdx(0))
141+ /// .local_get(LocalIdx(1))
142+ /// .i32_add();
142143///
143144/// // Add our function to the code section.
144145/// let mut code = CodeSection::new();
@@ -271,9 +272,9 @@ impl Function {
271272 /// For example:
272273 ///
273274 /// ```
274- /// # use wasm_encoder::{CodeSection, Function, Instruction };
275+ /// # use wasm_encoder::{CodeSection, Function};
275276 /// let mut f = Function::new([]);
276- /// f.instruction(&Instruction::End );
277+ /// f.instructions().end( );
277278 /// let bytes = f.into_raw_body();
278279 /// // (save `bytes` somewhere for later use)
279280 /// let mut code = CodeSection::new();
@@ -2320,125 +2321,169 @@ impl ConstExpr {
23202321 Self { bytes }
23212322 }
23222323
2323- fn new_insn ( insn : Instruction ) -> Self {
2324+ fn new ( f : impl FnOnce ( & mut InstructionSink ) ) -> Self {
23242325 let mut bytes = vec ! [ ] ;
2325- insn . encode ( & mut bytes) ;
2326+ f ( & mut InstructionSink :: new ( & mut bytes) ) ;
23262327 Self { bytes }
23272328 }
23282329
2329- fn with_insn ( mut self , insn : Instruction ) -> Self {
2330- insn . encode ( & mut self . bytes ) ;
2330+ fn with ( mut self , f : impl FnOnce ( & mut InstructionSink ) ) -> Self {
2331+ f ( & mut InstructionSink :: new ( & mut self . bytes ) ) ;
23312332 self
23322333 }
23332334
23342335 /// Create a constant expression containing a single `global.get` instruction.
23352336 pub fn global_get ( index : u32 ) -> Self {
2336- Self :: new_insn ( Instruction :: GlobalGet ( index) )
2337+ Self :: new ( |insn| {
2338+ insn. global_get ( GlobalIdx ( index) ) ;
2339+ } )
23372340 }
23382341
23392342 /// Create a constant expression containing a single `ref.null` instruction.
23402343 pub fn ref_null ( ty : HeapType ) -> Self {
2341- Self :: new_insn ( Instruction :: RefNull ( ty) )
2344+ Self :: new ( |insn| {
2345+ insn. ref_null ( ty) ;
2346+ } )
23422347 }
23432348
23442349 /// Create a constant expression containing a single `ref.func` instruction.
23452350 pub fn ref_func ( func : u32 ) -> Self {
2346- Self :: new_insn ( Instruction :: RefFunc ( func) )
2351+ Self :: new ( |insn| {
2352+ insn. ref_func ( FuncIdx ( func) ) ;
2353+ } )
23472354 }
23482355
23492356 /// Create a constant expression containing a single `i32.const` instruction.
23502357 pub fn i32_const ( value : i32 ) -> Self {
2351- Self :: new_insn ( Instruction :: I32Const ( value) )
2358+ Self :: new ( |insn| {
2359+ insn. i32_const ( value) ;
2360+ } )
23522361 }
23532362
23542363 /// Create a constant expression containing a single `i64.const` instruction.
23552364 pub fn i64_const ( value : i64 ) -> Self {
2356- Self :: new_insn ( Instruction :: I64Const ( value) )
2365+ Self :: new ( |insn| {
2366+ insn. i64_const ( value) ;
2367+ } )
23572368 }
23582369
23592370 /// Create a constant expression containing a single `f32.const` instruction.
23602371 pub fn f32_const ( value : f32 ) -> Self {
2361- Self :: new_insn ( Instruction :: F32Const ( value) )
2372+ Self :: new ( |insn| {
2373+ insn. f32_const ( value) ;
2374+ } )
23622375 }
23632376
23642377 /// Create a constant expression containing a single `f64.const` instruction.
23652378 pub fn f64_const ( value : f64 ) -> Self {
2366- Self :: new_insn ( Instruction :: F64Const ( value) )
2379+ Self :: new ( |insn| {
2380+ insn. f64_const ( value) ;
2381+ } )
23672382 }
23682383
23692384 /// Create a constant expression containing a single `v128.const` instruction.
23702385 pub fn v128_const ( value : i128 ) -> Self {
2371- Self :: new_insn ( Instruction :: V128Const ( value) )
2386+ Self :: new ( |insn| {
2387+ insn. v128_const ( value) ;
2388+ } )
23722389 }
23732390
23742391 /// Add a `global.get` instruction to this constant expression.
23752392 pub fn with_global_get ( self , index : u32 ) -> Self {
2376- self . with_insn ( Instruction :: GlobalGet ( index) )
2393+ self . with ( |insn| {
2394+ insn. global_get ( GlobalIdx ( index) ) ;
2395+ } )
23772396 }
23782397
23792398 /// Add a `ref.null` instruction to this constant expression.
23802399 pub fn with_ref_null ( self , ty : HeapType ) -> Self {
2381- self . with_insn ( Instruction :: RefNull ( ty) )
2400+ self . with ( |insn| {
2401+ insn. ref_null ( ty) ;
2402+ } )
23822403 }
23832404
23842405 /// Add a `ref.func` instruction to this constant expression.
23852406 pub fn with_ref_func ( self , func : u32 ) -> Self {
2386- self . with_insn ( Instruction :: RefFunc ( func) )
2407+ self . with ( |insn| {
2408+ insn. ref_func ( FuncIdx ( func) ) ;
2409+ } )
23872410 }
23882411
23892412 /// Add an `i32.const` instruction to this constant expression.
23902413 pub fn with_i32_const ( self , value : i32 ) -> Self {
2391- self . with_insn ( Instruction :: I32Const ( value) )
2414+ self . with ( |insn| {
2415+ insn. i32_const ( value) ;
2416+ } )
23922417 }
23932418
23942419 /// Add an `i64.const` instruction to this constant expression.
23952420 pub fn with_i64_const ( self , value : i64 ) -> Self {
2396- self . with_insn ( Instruction :: I64Const ( value) )
2421+ self . with ( |insn| {
2422+ insn. i64_const ( value) ;
2423+ } )
23972424 }
23982425
23992426 /// Add a `f32.const` instruction to this constant expression.
24002427 pub fn with_f32_const ( self , value : f32 ) -> Self {
2401- self . with_insn ( Instruction :: F32Const ( value) )
2428+ self . with ( |insn| {
2429+ insn. f32_const ( value) ;
2430+ } )
24022431 }
24032432
24042433 /// Add a `f64.const` instruction to this constant expression.
24052434 pub fn with_f64_const ( self , value : f64 ) -> Self {
2406- self . with_insn ( Instruction :: F64Const ( value) )
2435+ self . with ( |insn| {
2436+ insn. f64_const ( value) ;
2437+ } )
24072438 }
24082439
24092440 /// Add a `v128.const` instruction to this constant expression.
24102441 pub fn with_v128_const ( self , value : i128 ) -> Self {
2411- self . with_insn ( Instruction :: V128Const ( value) )
2442+ self . with ( |insn| {
2443+ insn. v128_const ( value) ;
2444+ } )
24122445 }
24132446
24142447 /// Add an `i32.add` instruction to this constant expression.
24152448 pub fn with_i32_add ( self ) -> Self {
2416- self . with_insn ( Instruction :: I32Add )
2449+ self . with ( |insn| {
2450+ insn. i32_add ( ) ;
2451+ } )
24172452 }
24182453
24192454 /// Add an `i32.sub` instruction to this constant expression.
24202455 pub fn with_i32_sub ( self ) -> Self {
2421- self . with_insn ( Instruction :: I32Sub )
2456+ self . with ( |insn| {
2457+ insn. i32_sub ( ) ;
2458+ } )
24222459 }
24232460
24242461 /// Add an `i32.mul` instruction to this constant expression.
24252462 pub fn with_i32_mul ( self ) -> Self {
2426- self . with_insn ( Instruction :: I32Mul )
2463+ self . with ( |insn| {
2464+ insn. i32_mul ( ) ;
2465+ } )
24272466 }
24282467
24292468 /// Add an `i64.add` instruction to this constant expression.
24302469 pub fn with_i64_add ( self ) -> Self {
2431- self . with_insn ( Instruction :: I64Add )
2470+ self . with ( |insn| {
2471+ insn. i64_add ( ) ;
2472+ } )
24322473 }
24332474
24342475 /// Add an `i64.sub` instruction to this constant expression.
24352476 pub fn with_i64_sub ( self ) -> Self {
2436- self . with_insn ( Instruction :: I64Sub )
2477+ self . with ( |insn| {
2478+ insn. i64_sub ( ) ;
2479+ } )
24372480 }
24382481
24392482 /// Add an `i64.mul` instruction to this constant expression.
24402483 pub fn with_i64_mul ( self ) -> Self {
2441- self . with_insn ( Instruction :: I64Mul )
2484+ self . with ( |insn| {
2485+ insn. i64_mul ( ) ;
2486+ } )
24422487 }
24432488
24442489 /// Returns the function, if any, referenced by this global.
@@ -2456,7 +2501,7 @@ impl ConstExpr {
24562501impl Encode for ConstExpr {
24572502 fn encode ( & self , sink : & mut Vec < u8 > ) {
24582503 sink. extend ( & self . bytes ) ;
2459- Instruction :: End . encode ( sink) ;
2504+ InstructionSink :: new ( sink) . end ( ) ;
24602505 }
24612506}
24622507
0 commit comments