Skip to main content

js_component_bindgen/
function_bindgen.rs

1use std::collections::{BTreeMap, BTreeSet};
2use std::fmt::Write;
3use std::mem;
4
5use heck::{ToLowerCamelCase, ToUpperCamelCase};
6use wasmtime_environ::component::{
7    InterfaceType, ResourceIndex, RuntimeCallbackIndex, RuntimeComponentInstanceIndex,
8    RuntimeMemoryIndex, RuntimeReallocIndex, TypeComponentLocalErrorContextTableIndex,
9    TypeFutureTableIndex, TypeResourceTableIndex, TypeStreamTableIndex,
10};
11use wit_bindgen_core::abi::{Bindgen, Bitcast, Instruction};
12use wit_component::StringEncoding;
13use wit_parser::abi::WasmType;
14use wit_parser::{
15    Alignment, ArchitectureSize, Handle, Resolve, SizeAlign, Type, TypeDef, TypeDefKind, TypeId,
16};
17
18use crate::intrinsics::Intrinsic;
19use crate::intrinsics::component::ComponentIntrinsic;
20use crate::intrinsics::conversion::ConversionIntrinsic;
21use crate::intrinsics::js_helper::JsHelperIntrinsic;
22use crate::intrinsics::p3::async_future::AsyncFutureIntrinsic;
23use crate::intrinsics::p3::async_stream::AsyncStreamIntrinsic;
24use crate::intrinsics::p3::async_task::AsyncTaskIntrinsic;
25use crate::intrinsics::resource::ResourceIntrinsic;
26use crate::intrinsics::string::StringIntrinsic;
27use crate::{ManagesIntrinsics, get_thrown_type, source};
28use crate::{uwrite, uwriteln};
29
30/// Method of error handling
31#[derive(Debug, Clone, PartialEq)]
32pub enum ErrHandling {
33    /// Do no special handling of errors, requiring users to return objects that represent
34    /// errors as represented in WIT
35    None,
36    /// Require throwing of result error objects
37    ThrowResultErr,
38    /// Catch thrown errors and convert them into result<t,e> error variants
39    ResultCatchHandler,
40}
41
42impl ErrHandling {
43    fn to_js_string(&self) -> String {
44        match self {
45            ErrHandling::None => "none".into(),
46            ErrHandling::ThrowResultErr => "throw-result-err".into(),
47            ErrHandling::ResultCatchHandler => "result-catch-handler".into(),
48        }
49    }
50}
51
52/// Data related to a given resource
53#[derive(Clone, Debug, PartialEq)]
54pub enum ResourceData {
55    Host {
56        tid: TypeResourceTableIndex,
57        rid: ResourceIndex,
58        local_name: String,
59        dtor_name: Option<String>,
60    },
61    Guest {
62        resource_name: String,
63        prefix: Option<String>,
64        extra: Option<ResourceExtraData>,
65    },
66}
67
68#[derive(Clone, Debug, PartialEq)]
69pub struct PayloadTypeMetadata {
70    pub(crate) ty: Type,
71    pub(crate) iface_ty: InterfaceType,
72    /// JS expression that serves as a function that lifts a given type
73    pub(crate) lift_js_expr: String,
74    /// JS expression that serves as a function that lowers a given type
75    pub(crate) lower_js_expr: String,
76    pub(crate) size32: u32,
77    pub(crate) align32: u32,
78    pub(crate) flat_count: Option<u8>,
79}
80
81/// Supplemental data kept along with [`ResourceData`]
82#[derive(Clone, Debug, PartialEq)]
83pub enum ResourceExtraData {
84    Stream {
85        table_idx: TypeStreamTableIndex,
86        elem_ty: Option<PayloadTypeMetadata>,
87    },
88    Future {
89        table_idx: TypeFutureTableIndex,
90        elem_ty: Option<PayloadTypeMetadata>,
91        nesting_level: u32,
92    },
93    ErrorContext {
94        table_idx: TypeComponentLocalErrorContextTableIndex,
95    },
96}
97
98/// Map used for resource function bindgen within a given component
99///
100/// Mapping from the instance + resource index in that component (internal or external)
101/// to the unique global resource id used to key the resource tables for this resource.
102///
103/// The id value uniquely identifies the resource table so that if a resource is used
104/// by n components, there should be n different indices and spaces in use. The map is
105/// therefore entirely unique and fully distinct for each instance's function bindgen.
106///
107/// The second bool is true if it is an imported resource.
108///
109/// For a given resource table id {x}, with resource index {y} the local variables are assumed:
110/// - handleTable{x}
111/// - captureTable{y} (rep to instance map for captured imported tables, only for JS import bindgen, not hybrid)
112/// - captureCnt{y} for assigning capture rep
113///
114/// For component-defined resources:
115/// - finalizationRegistry{x}
116///
117/// handleTable internally will be allocated with { rep: i32, own: bool } entries
118///
119/// In the case of an imported resource tables, in place of "rep" we just store
120/// the direct JS object being referenced, since in JS the object is its own handle.
121///
122#[derive(Clone, Debug, PartialEq)]
123pub struct ResourceTable {
124    /// Whether a resource was imported
125    ///
126    /// This should be tracked because imported types cannot be re-exported uniquely (?)
127    pub imported: bool,
128
129    /// Data related to the actual resource
130    pub data: ResourceData,
131}
132
133/// A mapping of type IDs to the resources that they represent
134pub type ResourceMap = BTreeMap<TypeId, ResourceTable>;
135
136#[derive(bon::Builder)]
137#[non_exhaustive]
138pub struct FunctionBindgen<'a> {
139    /// Mapping of resources for types that have corresponding definitions locally
140    pub resource_map: &'a ResourceMap,
141
142    /// Whether current resource borrows need to be deactivated
143    pub clear_resource_borrows: bool,
144
145    /// Set of intrinsics
146    pub intrinsics: &'a mut BTreeSet<Intrinsic>,
147
148    /// Whether to perform valid lifting optimization
149    pub valid_lifting_optimization: bool,
150
151    /// Sizes and alignments for sub elements
152    pub sizes: &'a SizeAlign,
153
154    /// Method of error handling
155    pub err: ErrHandling,
156
157    /// Temporary values
158    pub tmp: usize,
159
160    /// Source code of the function
161    pub src: source::Source,
162
163    /// Block storage
164    pub block_storage: Vec<source::Source>,
165
166    /// Blocks of the function
167    pub blocks: Vec<(String, Vec<String>)>,
168
169    /// Parameters of the function
170    pub params: Vec<String>,
171
172    /// Memory variable
173    pub memory: Option<&'a String>,
174
175    /// Realloc function name
176    pub realloc: Option<&'a String>,
177
178    /// Post return function name
179    pub post_return: Option<&'a String>,
180
181    /// Prefix to use when printing tracing information
182    pub tracing_prefix: &'a String,
183
184    /// Whether tracing is enabled
185    pub tracing_enabled: bool,
186
187    /// Method if string encoding
188    pub encoding: StringEncoding,
189
190    /// Callee of the function
191    pub callee: &'a str,
192
193    /// Whether the callee is dynamic (i.e. has multiple operands)
194    pub callee_resource_dynamic: bool,
195
196    /// The [`wit_bindgen::Resolve`] containing extracted WIT information
197    pub resolve: &'a Resolve,
198
199    /// Whether the function requires async porcelain
200    ///
201    /// In the case of an import this likely implies the use of JSPI
202    /// and in the case of an export this is simply code generation metadata.
203    pub requires_async_porcelain: bool,
204
205    /// Whether the function is guest async lifted (i.e. WASI P3)
206    pub is_async: bool,
207
208    /// Interface name
209    pub iface_name: Option<&'a str>,
210
211    /// Whether the callee was transpiled from Wasm to JS (asm.js) and thus needs shimming for i64
212    pub asmjs: bool,
213
214    /// Component state generated from processing a component.
215    ///
216    /// This information is normally accessible via producing/having
217    /// access to a [`wasmtime_environ::component::Component`]), and
218    /// is required for *some* bindgen instructions.
219    ///
220    /// If you are performing bindgen aganist a dummy module, you may omit this field, but if
221    /// processing has been performed on the component, and this state is available at time of generation for
222    /// this function, provide this information (normally found in [`CanonicalOptions`]s)
223    ///
224    pub component_state: Option<FunctionBindgenComponentState>,
225
226    /// Whether the bindgen is being performed for an import
227    /// (false implies generation is being performed for an export)
228    pub(crate) for_import: Option<bool>,
229}
230
231/// Metadata that is derived from processing a component.
232///
233/// This information is often required to perform bindgen completely, *but*
234/// requires component processing which not all downstream bindgen consumers may
235/// perform.
236///
237/// For bindgen consumers that perform generation which requires this information,
238/// the information should be provided.
239///
240#[derive(bon::Builder)]
241#[non_exhaustive]
242pub struct FunctionBindgenComponentState {
243    pub(crate) component_idx: RuntimeComponentInstanceIndex,
244    pub(crate) realloc_fn_idx: Option<RuntimeReallocIndex>,
245    pub(crate) memory_idx: Option<RuntimeMemoryIndex>,
246    pub(crate) callback_fn_idx: Option<RuntimeCallbackIndex>,
247}
248
249/// JS expressions that resolve to or return component state
250#[derive(bon::Builder)]
251#[non_exhaustive]
252pub struct ComponentStateJsExprs {
253    /// JS expression that is a number, e.g. "0"
254    pub(crate) component_idx: String,
255    /// JS expression that is the generated name of the callback, e.g. "callback_1"
256    pub(crate) callback_fn_name: String,
257    /// JS function expression that returns the callback function for this function, e.g. "() => callback_1"
258    pub(crate) get_callback_fn: String,
259    /// JS expression that is the index of the top-level declared memory associated with this function, e.g. "1"
260    pub(crate) memory_idx: String,
261    /// JS function expression that returns the top-level declared memory associated with this function, e.g. "() => memory0"
262    pub(crate) get_memory_fn: String,
263    /// JS function expression that returns the realloc fn relevant to this function, e.g. "() => realloc0"
264    pub(crate) get_realloc_fn: String,
265}
266
267impl FunctionBindgenComponentState {
268    /// Get JS expressions that represent the bindgen state
269    ///
270    /// When certain values are missing either `null` or `() => null` JS expressions are returned
271    ///
272    fn get_js_exprs(&self) -> ComponentStateJsExprs {
273        ComponentStateJsExprs {
274            component_idx: self.component_idx.as_u32().to_string(),
275            callback_fn_name: match self.callback_fn_idx {
276                Some(idx) => format!("callback_{}", idx.as_u32()),
277                None => "null".into(),
278            },
279            get_callback_fn: match self.callback_fn_idx {
280                Some(idx) => format!("() => callback_{}", idx.as_u32()),
281                None => "() => null".into(),
282            },
283            memory_idx: match self.memory_idx {
284                Some(idx) => idx.as_u32().to_string(),
285                None => "null".into(),
286            },
287            get_memory_fn: match self.memory_idx {
288                Some(idx) => format!("() => memory{}", idx.as_u32()),
289                None => "() => null".into(),
290            },
291            get_realloc_fn: match self.realloc_fn_idx {
292                Some(idx) => format!("() => realloc{}", idx.as_u32()),
293                None => "undefined".into(),
294            },
295        }
296    }
297}
298
299impl FunctionBindgen<'_> {
300    fn tmp(&mut self) -> usize {
301        let ret = self.tmp;
302        self.tmp += 1;
303        ret
304    }
305
306    fn intrinsic(&mut self, intrinsic: Intrinsic) -> String {
307        self.intrinsics.insert(intrinsic);
308        intrinsic.name().to_string()
309    }
310
311    fn clamp_guest<T>(&mut self, results: &mut Vec<String>, operands: &[String], min: T, max: T)
312    where
313        T: std::fmt::Display,
314    {
315        let clamp = self.intrinsic(Intrinsic::ClampGuest);
316        results.push(format!("{}({}, {}, {})", clamp, operands[0], min, max));
317    }
318
319    fn load(
320        &mut self,
321        method: &str,
322        offset: ArchitectureSize,
323        operands: &[String],
324        results: &mut Vec<String>,
325    ) {
326        let view = self.intrinsic(Intrinsic::JsHelper(JsHelperIntrinsic::DataView));
327        let Some(memory) = self.memory.as_ref() else {
328            panic!(
329                "unexpectedly missing memory during bindgen for interface [{:?}] (callee {})",
330                self.iface_name, self.callee,
331            );
332        };
333        results.push(format!(
334            "{view}({memory}).{method}({} + {offset}, true)",
335            operands[0],
336            offset = offset.size_wasm32()
337        ));
338    }
339
340    fn store(&mut self, method: &str, offset: ArchitectureSize, operands: &[String]) {
341        let view = self.intrinsic(Intrinsic::JsHelper(JsHelperIntrinsic::DataView));
342        let memory = self.memory.as_ref().unwrap();
343        uwriteln!(
344            self.src,
345            "{view}({memory}).{method}({} + {offset}, {}, true);",
346            operands[1],
347            operands[0],
348            offset = offset.size_wasm32()
349        );
350    }
351
352    /// Write result assignment lines to output
353    ///
354    /// In general this either means writing preambles, for example that look like the following:
355    ///
356    /// ```js
357    /// let ret =
358    /// ```
359    ///
360    /// ```
361    /// var [ ret0, ret1, ret2 ] =
362    /// ```
363    ///
364    /// ```js
365    /// let ret;
366    /// ```
367    ///
368    /// This function returns as a first tuple parameter a list of
369    /// variables that should be created via let statements beforehand.
370    ///
371    /// # Arguments
372    ///
373    /// * `amt` - number of results
374    /// * `results` - list of variables that will be returned
375    ///
376    fn generate_result_assignment_lhs(
377        &mut self,
378        amt: usize,
379        results: &mut Vec<String>,
380        is_async: bool,
381    ) -> (String, String) {
382        let mut s = String::new();
383        let mut vars_init = String::new();
384        match amt {
385            0 => {
386                // Async functions with no returns still return async code,
387                // which will be used as the initial callback result going into the async driver
388                if is_async {
389                    uwrite!(s, "ret = ")
390                }
391                uwriteln!(vars_init, "let ret;");
392            }
393            1 => {
394                uwrite!(s, "ret = ");
395                results.push("ret".to_string());
396                uwriteln!(vars_init, "let ret;");
397            }
398            n => {
399                uwrite!(s, "[");
400                for i in 0..n {
401                    if i > 0 {
402                        uwrite!(s, ", ");
403                    }
404                    uwrite!(s, "ret{i}");
405                    results.push(format!("ret{i}"));
406                    uwriteln!(vars_init, "let ret;");
407                }
408                uwrite!(s, "] = ");
409            }
410        }
411        (vars_init, s)
412    }
413
414    fn bitcast(&mut self, cast: &Bitcast, op: &str) -> String {
415        match cast {
416            Bitcast::I32ToF32 => {
417                let cvt = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::I32ToF32));
418                format!("{cvt}({op})")
419            }
420            Bitcast::F32ToI32 => {
421                let cvt = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::F32ToI32));
422                format!("{cvt}({op})")
423            }
424            Bitcast::I64ToF64 => {
425                let cvt = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::I64ToF64));
426                format!("{cvt}({op})")
427            }
428            Bitcast::F64ToI64 => {
429                let cvt = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::F64ToI64));
430                format!("{cvt}({op})")
431            }
432            Bitcast::I32ToI64 => format!("BigInt({op})"),
433            Bitcast::I64ToI32 => format!("Number({op})"),
434            Bitcast::I64ToF32 => {
435                let cvt = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::I32ToF32));
436                format!("{cvt}(Number({op}))")
437            }
438            Bitcast::F32ToI64 => {
439                let cvt = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::F32ToI32));
440                format!("BigInt({cvt}({op}))")
441            }
442            Bitcast::None
443            | Bitcast::P64ToI64
444            | Bitcast::LToI32
445            | Bitcast::I32ToL
446            | Bitcast::LToP
447            | Bitcast::PToL
448            | Bitcast::PToI32
449            | Bitcast::I32ToP => op.to_string(),
450            Bitcast::PToP64 | Bitcast::I64ToP64 | Bitcast::LToI64 => format!("BigInt({op})"),
451            Bitcast::P64ToP | Bitcast::I64ToL => format!("Number({op})"),
452            Bitcast::Sequence(casts) => {
453                let mut statement = op.to_string();
454                for cast in casts.iter() {
455                    statement = self.bitcast(cast, &statement);
456                }
457                statement
458            }
459        }
460    }
461
462    /// Start the current task
463    ///
464    /// The code generated by this function *may* also start a subtask
465    /// where appropriate.
466    fn start_current_task(&mut self, instr: &Instruction) {
467        let is_async = self.is_async;
468        let is_manual_async = self.requires_async_porcelain;
469        let fn_name = self.callee;
470        let err_handling = self.err.to_js_string();
471
472        let (calling_wasm_export, prefix) = match instr {
473            Instruction::CallWasm { .. } => (true, "_wasm_call_"),
474            Instruction::CallInterface { .. } => (false, "_interface_call_"),
475            _ => unreachable!(
476                "unrecognized instruction triggering start of current task: [{instr:?}]"
477            ),
478        };
479        let start_current_task_fn = self.intrinsic(Intrinsic::AsyncTask(
480            AsyncTaskIntrinsic::CreateNewCurrentTask,
481        ));
482
483        let (component_idx_expr, get_callback_fn_expr, callback_fn_name) =
484            if let Some(state) = &self.component_state {
485                let ComponentStateJsExprs {
486                    component_idx,
487                    callback_fn_name,
488                    get_callback_fn,
489                    ..
490                } = state.get_js_exprs();
491                (component_idx, get_callback_fn, callback_fn_name)
492            } else {
493                ("1".into(), "() => null".into(), "null".into())
494            };
495
496        uwriteln!(
497            self.src,
498            r#"
499              const [task, {prefix}currentTaskID] = {start_current_task_fn}({{
500                  componentIdx: {component_idx_expr},
501                  isAsync: {is_async},
502                  isManualAsync: {is_manual_async},
503                  entryFnName: '{fn_name}',
504                  getCallbackFn: {get_callback_fn_expr},
505                  callbackFnName: {callback_fn_name},
506                  errHandling: '{err_handling}',
507                  callingWasmExport: {calling_wasm_export},
508              }});
509            "#,
510        );
511    }
512}
513
514impl ManagesIntrinsics for FunctionBindgen<'_> {
515    /// Add an intrinsic, supplying it's name afterwards
516    fn add_intrinsic(&mut self, intrinsic: Intrinsic) {
517        self.intrinsic(intrinsic);
518    }
519}
520
521impl Bindgen for FunctionBindgen<'_> {
522    type Operand = String;
523
524    /// Get the sizes and alignment for a given structure
525    fn sizes(&self) -> &SizeAlign {
526        self.sizes
527    }
528
529    /// Push a new block of code
530    fn push_block(&mut self) {
531        let prev = mem::take(&mut self.src);
532        self.block_storage.push(prev);
533    }
534
535    /// Finish a block of code
536    fn finish_block(&mut self, operands: &mut Vec<String>) {
537        let to_restore = self.block_storage.pop().unwrap();
538        let src = mem::replace(&mut self.src, to_restore);
539        self.blocks.push((src.into(), mem::take(operands)));
540    }
541
542    /// Output the return pointer
543    fn return_pointer(&mut self, _size: ArchitectureSize, _align: Alignment) -> String {
544        unimplemented!("determining the return pointer for this function is not implemented");
545    }
546
547    /// Check whether a list of the given element type can be represented as a builtni JS type
548    ///
549    /// # Arguments
550    ///
551    /// * `resolve` - the [`Resolve`] that might be used to resolve nested types (i.e. [`Type::TypeId`])
552    /// * `elem_ty` - the [`Type`] of the element stored in the list
553    ///
554    fn is_list_canonical(&self, resolve: &Resolve, elem_ty: &Type) -> bool {
555        js_array_ty(resolve, elem_ty).is_some()
556    }
557
558    fn emit(
559        &mut self,
560        resolve: &Resolve,
561        inst: &Instruction<'_>,
562        operands: &mut Vec<String>,
563        results: &mut Vec<String>,
564    ) {
565        match inst {
566            Instruction::GetArg { nth } => results.push(self.params[*nth].clone()),
567
568            Instruction::I32Const { val } => results.push(val.to_string()),
569
570            Instruction::ConstZero { tys } => {
571                for t in tys.iter() {
572                    match t {
573                        WasmType::I64 | WasmType::PointerOrI64 => results.push("0n".to_string()),
574                        WasmType::I32
575                        | WasmType::F32
576                        | WasmType::F64
577                        | WasmType::Pointer
578                        | WasmType::Length => results.push("0".to_string()),
579                    }
580                }
581            }
582
583            Instruction::U8FromI32 => self.clamp_guest(results, operands, u8::MIN, u8::MAX),
584
585            Instruction::S8FromI32 => self.clamp_guest(results, operands, i8::MIN, i8::MAX),
586
587            Instruction::U16FromI32 => self.clamp_guest(results, operands, u16::MIN, u16::MAX),
588
589            Instruction::S16FromI32 => self.clamp_guest(results, operands, i16::MIN, i16::MAX),
590
591            Instruction::U32FromI32 => results.push(format!("{} >>> 0", operands[0])),
592
593            Instruction::U64FromI64 => {
594                results.push(format!("BigInt.asUintN(64, BigInt({}))", operands[0]))
595            }
596
597            Instruction::S32FromI32 | Instruction::S64FromI64 => {
598                results.push(operands.pop().unwrap())
599            }
600
601            Instruction::I32FromU8 => {
602                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToUint8));
603                results.push(format!("{conv}({op})", op = operands[0]))
604            }
605
606            Instruction::I32FromS8 => {
607                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToInt8));
608                results.push(format!("{conv}({op})", op = operands[0]))
609            }
610
611            Instruction::I32FromU16 => {
612                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToUint16));
613                results.push(format!("{conv}({op})", op = operands[0]))
614            }
615
616            Instruction::I32FromS16 => {
617                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToInt16));
618                results.push(format!("{conv}({op})", op = operands[0]))
619            }
620
621            Instruction::I32FromU32 => {
622                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToUint32));
623                results.push(format!("{conv}({op})", op = operands[0]))
624            }
625
626            Instruction::I32FromS32 => {
627                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToInt32));
628                results.push(format!("{conv}({op})", op = operands[0]))
629            }
630
631            Instruction::I64FromU64 => {
632                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToBigUint64));
633                results.push(format!("{conv}({op})", op = operands[0]))
634            }
635
636            Instruction::I64FromS64 => {
637                let conv = self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToBigInt64));
638                results.push(format!("{conv}({op})", op = operands[0]))
639            }
640
641            Instruction::F32FromCoreF32 | Instruction::F64FromCoreF64 => {
642                results.push(operands.pop().unwrap())
643            }
644
645            Instruction::CoreF32FromF32 | Instruction::CoreF64FromF64 => {
646                results.push(format!("+{}", operands[0]))
647            }
648
649            Instruction::CharFromI32 => {
650                let validate =
651                    self.intrinsic(Intrinsic::String(StringIntrinsic::ValidateGuestChar));
652                results.push(format!("{}({})", validate, operands[0]));
653            }
654
655            Instruction::I32FromChar => {
656                let validate = self.intrinsic(Intrinsic::String(StringIntrinsic::ValidateHostChar));
657                results.push(format!("{}({})", validate, operands[0]));
658            }
659
660            Instruction::Bitcasts { casts } => {
661                for (cast, op) in casts.iter().zip(operands) {
662                    results.push(self.bitcast(cast, op));
663                }
664            }
665
666            Instruction::BoolFromI32 => {
667                let tmp = self.tmp();
668                uwrite!(self.src, "var bool{} = {};\n", tmp, operands[0]);
669                if self.valid_lifting_optimization {
670                    results.push(format!("!!bool{tmp}"));
671                } else {
672                    let throw = self.intrinsic(Intrinsic::ThrowInvalidBool);
673                    results.push(format!(
674                        "bool{tmp} == 0 ? false : (bool{tmp} == 1 ? true : {throw}())"
675                    ));
676                }
677            }
678
679            Instruction::I32FromBool => {
680                results.push(format!("{} ? 1 : 0", operands[0]));
681            }
682
683            Instruction::RecordLower { record, .. } => {
684                // use destructuring field access to get each
685                // field individually.
686                let tmp = self.tmp();
687                let mut expr = "var {".to_string();
688                for (i, field) in record.fields.iter().enumerate() {
689                    if i > 0 {
690                        expr.push_str(", ");
691                    }
692                    let name = format!("v{tmp}_{i}");
693                    expr.push_str(&field.name.to_lower_camel_case());
694                    expr.push_str(": ");
695                    expr.push_str(&name);
696                    results.push(name);
697                }
698                uwrite!(self.src, "{} }} = {};\n", expr, operands[0]);
699            }
700
701            Instruction::RecordLift { record, .. } => {
702                // records are represented as plain objects, so we
703                // make a new object and set all the fields with an object
704                // literal.
705                let mut result = "{\n".to_string();
706                for (field, op) in record.fields.iter().zip(operands) {
707                    result.push_str(&format!("{}: {},\n", field.name.to_lower_camel_case(), op));
708                }
709                result.push('}');
710                results.push(result);
711            }
712
713            Instruction::TupleLower { tuple, .. } => {
714                // Tuples are represented as an array, sowe can use
715                // destructuring assignment to lower the tuple into its
716                // components.
717                let tmp = self.tmp();
718                let mut expr = "var [".to_string();
719                for i in 0..tuple.types.len() {
720                    if i > 0 {
721                        expr.push_str(", ");
722                    }
723                    let name = format!("tuple{tmp}_{i}");
724                    expr.push_str(&name);
725                    results.push(name);
726                }
727                uwrite!(self.src, "{}] = {};\n", expr, operands[0]);
728            }
729
730            Instruction::TupleLift { .. } => {
731                // Tuples are represented as an array, so we just shove all
732                // the operands into an array.
733                results.push(format!("[{}]", operands.join(", ")));
734            }
735
736            Instruction::FlagsLower { flags, .. } => {
737                let op0 = &operands[0];
738
739                // Generate the result names.
740                for _ in 0..flags.repr().count() {
741                    let tmp = self.tmp();
742                    let name = format!("flags{tmp}");
743                    // Default to 0 so that in the null/undefined case, everything is false by
744                    // default.
745                    uwrite!(self.src, "let {name} = 0;\n");
746                    results.push(name);
747                }
748
749                uwrite!(
750                    self.src,
751                    "if (typeof {op0} === 'object' && {op0} !== null) {{\n"
752                );
753
754                for (i, chunk) in flags.flags.chunks(32).enumerate() {
755                    let result_name = &results[i];
756
757                    uwrite!(self.src, "{result_name} = ");
758                    for (i, flag) in chunk.iter().enumerate() {
759                        if i != 0 {
760                            uwrite!(self.src, " | ");
761                        }
762
763                        let flag = flag.name.to_lower_camel_case();
764                        uwrite!(self.src, "Boolean({op0}.{flag}) << {i}");
765                    }
766                    uwrite!(self.src, ";\n");
767                }
768
769                uwrite!(
770                            self.src,
771                            "\
772                    }} else if ({op0} !== null && {op0} !== undefined) {{
773                        throw new TypeError('only an object, undefined or null can be converted to flags');
774                    }}
775                ");
776
777                // We don't need to do anything else for the null/undefined
778                // case, since that's interpreted as everything false, and we
779                // already defaulted everyting to 0.
780            }
781
782            Instruction::FlagsLift { flags, .. } => {
783                let tmp = self.tmp();
784                results.push(format!("flags{tmp}"));
785
786                if let Some(op) = operands.last() {
787                    // We only need an extraneous bits check if the number of flags isn't a multiple
788                    // of 32, because if it is then all the bits are used and there are no
789                    // extraneous bits.
790                    if flags.flags.len() % 32 != 0 && !self.valid_lifting_optimization {
791                        let mask: u32 = 0xffffffff << (flags.flags.len() % 32);
792                        uwriteln!(
793                            self.src,
794                            "if (({op} & {mask}) !== 0) {{
795                                throw new TypeError('flags have extraneous bits set');
796                            }}"
797                        );
798                    }
799                }
800
801                uwriteln!(self.src, "var flags{tmp} = {{");
802
803                for (i, flag) in flags.flags.iter().enumerate() {
804                    let flag = flag.name.to_lower_camel_case();
805                    let op = &operands[i / 32];
806                    let mask: u32 = 1 << (i % 32);
807                    uwriteln!(self.src, "{flag}: Boolean({op} & {mask}),");
808                }
809
810                uwriteln!(self.src, "}};");
811            }
812
813            Instruction::VariantPayloadName => results.push("e".to_string()),
814
815            Instruction::VariantLower {
816                variant,
817                results: result_types,
818                name,
819                ..
820            } => {
821                let blocks = self
822                    .blocks
823                    .drain(self.blocks.len() - variant.cases.len()..)
824                    .collect::<Vec<_>>();
825                let tmp = self.tmp();
826                let op = &operands[0];
827                uwriteln!(self.src, "var variant{tmp} = {op};");
828
829                for i in 0..result_types.len() {
830                    uwriteln!(self.src, "let variant{tmp}_{i};");
831                    results.push(format!("variant{tmp}_{i}"));
832                }
833
834                let expr_to_match = format!("variant{tmp}.tag");
835
836                uwriteln!(self.src, "switch ({expr_to_match}) {{");
837                for (case, (block, block_results)) in variant.cases.iter().zip(blocks) {
838                    uwriteln!(self.src, "case '{}': {{", case.name.as_str());
839                    if case.ty.is_some() {
840                        uwriteln!(self.src, "const e = variant{tmp}.val;");
841                    }
842                    self.src.push_str(&block);
843
844                    for (i, result) in block_results.iter().enumerate() {
845                        uwriteln!(self.src, "variant{tmp}_{i} = {result};");
846                    }
847                    uwriteln!(
848                        self.src,
849                        "break;
850                        }}"
851                    );
852                }
853                let variant_name = name.to_upper_camel_case();
854                uwriteln!(
855                    self.src,
856                    r#"default: {{
857                        throw new TypeError(`invalid variant tag value \`${{JSON.stringify({expr_to_match})}}\` (received \`${{variant{tmp}}}\`) specified for \`{variant_name}\``);
858                    }}"#,
859                );
860                uwriteln!(self.src, "}}");
861            }
862
863            Instruction::VariantLift { variant, name, .. } => {
864                let blocks = self
865                    .blocks
866                    .drain(self.blocks.len() - variant.cases.len()..)
867                    .collect::<Vec<_>>();
868
869                let tmp = self.tmp();
870                let op = &operands[0];
871
872                uwriteln!(
873                    self.src,
874                    "let variant{tmp};
875                    switch ({op}) {{"
876                );
877
878                for (i, (case, (block, block_results))) in
879                    variant.cases.iter().zip(blocks).enumerate()
880                {
881                    let tag = case.name.as_str();
882                    uwriteln!(
883                        self.src,
884                        "case {i}: {{
885                            {block}\
886                            variant{tmp} = {{
887                                tag: '{tag}',"
888                    );
889                    if case.ty.is_some() {
890                        assert!(block_results.len() == 1);
891                        uwriteln!(self.src, "   val: {}", block_results[0]);
892                    } else {
893                        assert!(block_results.is_empty());
894                    }
895                    uwriteln!(
896                        self.src,
897                        "   }};
898                        break;
899                        }}"
900                    );
901                }
902                let variant_name = name.to_upper_camel_case();
903                if !self.valid_lifting_optimization {
904                    uwriteln!(
905                        self.src,
906                        "default: {{
907                            throw new TypeError('invalid variant discriminant for {variant_name}');
908                        }}",
909                    );
910                }
911                uwriteln!(self.src, "}}");
912                results.push(format!("variant{tmp}"));
913            }
914
915            Instruction::OptionLower {
916                payload,
917                results: result_types,
918                ..
919            } => {
920                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
921                let (mut some, some_results) = self.blocks.pop().unwrap();
922                let (mut none, none_results) = self.blocks.pop().unwrap();
923
924                let tmp = self.tmp();
925                let op = &operands[0];
926                uwriteln!(self.src, "var variant{tmp} = {op};");
927
928                for i in 0..result_types.len() {
929                    uwriteln!(self.src, "let variant{tmp}_{i};");
930                    results.push(format!("variant{tmp}_{i}"));
931
932                    let some_result = &some_results[i];
933                    let none_result = &none_results[i];
934                    uwriteln!(some, "variant{tmp}_{i} = {some_result};");
935                    uwriteln!(none, "variant{tmp}_{i} = {none_result};");
936                }
937
938                if maybe_null(resolve, payload) {
939                    uwriteln!(
940                        self.src,
941                        r#"switch (variant{tmp}.tag) {{
942                            case 'none': {{
943                                {none}
944                                break;
945                            }}
946                            case 'some': {{
947                                const e = variant{tmp}.val;
948                                {some}
949                                break;
950                            }}
951                            default: {{
952                                {debug_log_fn}("ERROR: invalid value (expected option as object with 'tag' member)", {{ value: variant{tmp}, valueType: typeof variant{tmp} }});
953                                throw new TypeError('invalid variant specified for option');
954                            }}
955                        }}"#,
956                    );
957                } else {
958                    uwriteln!(
959                        self.src,
960                        "if (variant{tmp} === null || variant{tmp} === undefined) {{
961                            {none}\
962                        }} else {{
963                            const e = variant{tmp};
964                            {some}\
965                        }}"
966                    );
967                }
968            }
969
970            Instruction::OptionLift { payload, .. } => {
971                let (some, some_results) = self.blocks.pop().unwrap();
972                let (none, none_results) = self.blocks.pop().unwrap();
973                assert!(none_results.is_empty());
974                assert!(some_results.len() == 1);
975                let some_result = &some_results[0];
976
977                let tmp = self.tmp();
978                let op = &operands[0];
979
980                let (v_none, v_some) = if maybe_null(resolve, payload) {
981                    (
982                        "{ tag: 'none' }",
983                        format!(
984                            "{{
985                                tag: 'some',
986                                val: {some_result}
987                            }}"
988                        ),
989                    )
990                } else {
991                    ("undefined", some_result.into())
992                };
993
994                if !self.valid_lifting_optimization {
995                    uwriteln!(
996                        self.src,
997                        "let variant{tmp};
998                        switch ({op}) {{
999                            case 0: {{
1000                                {none}\
1001                                variant{tmp} = {v_none};
1002                                break;
1003                            }}
1004                            case 1: {{
1005                                {some}\
1006                                variant{tmp} = {v_some};
1007                                break;
1008                            }}
1009                            default: {{
1010                                throw new TypeError('invalid variant discriminant for option');
1011                            }}
1012                        }}",
1013                    );
1014                } else {
1015                    uwriteln!(
1016                        self.src,
1017                        "let variant{tmp};
1018                        if ({op}) {{
1019                            {some}\
1020                            variant{tmp} = {v_some};
1021                        }} else {{
1022                            {none}\
1023                            variant{tmp} = {v_none};
1024                        }}"
1025                    );
1026                }
1027
1028                results.push(format!("variant{tmp}"));
1029            }
1030
1031            Instruction::ResultLower {
1032                results: result_types,
1033                ..
1034            } => {
1035                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
1036                let (mut err, err_results) = self.blocks.pop().unwrap();
1037                let (mut ok, ok_results) = self.blocks.pop().unwrap();
1038
1039                let tmp = self.tmp();
1040                let op = &operands[0];
1041                uwriteln!(self.src, "var variant{tmp} = {op};");
1042
1043                for i in 0..result_types.len() {
1044                    uwriteln!(self.src, "let variant{tmp}_{i};");
1045                    results.push(format!("variant{tmp}_{i}"));
1046
1047                    let ok_result = &ok_results[i];
1048                    let err_result = &err_results[i];
1049                    uwriteln!(ok, "variant{tmp}_{i} = {ok_result};");
1050                    uwriteln!(err, "variant{tmp}_{i} = {err_result};");
1051                }
1052
1053                uwriteln!(
1054                    self.src,
1055                    r#"switch (variant{tmp}.tag) {{
1056                        case 'ok': {{
1057                            const e = variant{tmp}.val;
1058                            {ok}
1059                            break;
1060                        }}
1061                        case 'err': {{
1062                            const e = variant{tmp}.val;
1063                            {err}
1064                            break;
1065                        }}
1066                        default: {{
1067                            {debug_log_fn}("ERROR: invalid value (expected result as object with 'tag' member)", {{ value: variant{tmp}, valueType: typeof variant{tmp} }});
1068                            throw new TypeError('invalid variant specified for result');
1069                        }}
1070                    }}"#,
1071                );
1072            }
1073
1074            Instruction::ResultLift { result, .. } => {
1075                let (err, err_results) = self.blocks.pop().unwrap();
1076                let (ok, ok_results) = self.blocks.pop().unwrap();
1077                let ok_result = if result.ok.is_some() {
1078                    assert_eq!(ok_results.len(), 1);
1079                    ok_results[0].to_string()
1080                } else {
1081                    assert_eq!(ok_results.len(), 0);
1082                    String::from("undefined")
1083                };
1084                let err_result = if result.err.is_some() {
1085                    assert_eq!(err_results.len(), 1);
1086                    err_results[0].to_string()
1087                } else {
1088                    assert_eq!(err_results.len(), 0);
1089                    String::from("undefined")
1090                };
1091                let tmp = self.tmp();
1092                let op0 = &operands[0];
1093
1094                if !self.valid_lifting_optimization {
1095                    uwriteln!(
1096                        self.src,
1097                        "let variant{tmp};
1098                        switch ({op0}) {{
1099                            case 0: {{
1100                                {ok}\
1101                                variant{tmp} = {{
1102                                    tag: 'ok',
1103                                    val: {ok_result}
1104                                }};
1105                                break;
1106                            }}
1107                            case 1: {{
1108                                {err}\
1109                                variant{tmp} = {{
1110                                    tag: 'err',
1111                                    val: {err_result}
1112                                }};
1113                                break;
1114                            }}
1115                            default: {{
1116                                throw new TypeError('invalid variant discriminant for expected');
1117                            }}
1118                        }}",
1119                    );
1120                } else {
1121                    uwriteln!(
1122                        self.src,
1123                        "let variant{tmp};
1124                        if ({op0}) {{
1125                            {err}\
1126                            variant{tmp} = {{
1127                                tag: 'err',
1128                                val: {err_result}
1129                            }};
1130                        }} else {{
1131                            {ok}\
1132                            variant{tmp} = {{
1133                                tag: 'ok',
1134                                val: {ok_result}
1135                            }};
1136                        }}"
1137                    );
1138                }
1139                results.push(format!("variant{tmp}"));
1140            }
1141
1142            Instruction::EnumLower { name, enum_, .. } => {
1143                let tmp = self.tmp();
1144
1145                let op = &operands[0];
1146                uwriteln!(self.src, "var val{tmp} = {op};");
1147
1148                // Declare a variable to hold the result.
1149                uwriteln!(
1150                    self.src,
1151                    "let enum{tmp};
1152                    switch (val{tmp}) {{"
1153                );
1154                for (i, case) in enum_.cases.iter().enumerate() {
1155                    uwriteln!(
1156                        self.src,
1157                        "case '{case}': {{
1158                            enum{tmp} = {i};
1159                            break;
1160                        }}",
1161                        case = case.name
1162                    );
1163                }
1164                uwriteln!(self.src, "default: {{");
1165                if !self.valid_lifting_optimization {
1166                    uwriteln!(
1167                        self.src,
1168                        "if (({op}) instanceof Error) {{
1169                        console.error({op});
1170                    }}"
1171                    );
1172                }
1173                uwriteln!(
1174                            self.src,
1175                            "
1176                            throw new TypeError(`\"${{val{tmp}}}\" is not one of the cases of {name}`);
1177                        }}
1178                    }}",
1179                        );
1180
1181                results.push(format!("enum{tmp}"));
1182            }
1183
1184            Instruction::EnumLift { name, enum_, .. } => {
1185                let tmp = self.tmp();
1186
1187                uwriteln!(
1188                    self.src,
1189                    "let enum{tmp};
1190                    switch ({}) {{",
1191                    operands[0]
1192                );
1193                for (i, case) in enum_.cases.iter().enumerate() {
1194                    uwriteln!(
1195                        self.src,
1196                        "case {i}: {{
1197                            enum{tmp} = '{case}';
1198                            break;
1199                        }}",
1200                        case = case.name
1201                    );
1202                }
1203                if !self.valid_lifting_optimization {
1204                    let name = name.to_upper_camel_case();
1205                    uwriteln!(
1206                        self.src,
1207                        "default: {{
1208                            throw new TypeError('invalid discriminant specified for {name}');
1209                        }}",
1210                    );
1211                }
1212                uwriteln!(self.src, "}}");
1213
1214                results.push(format!("enum{tmp}"));
1215            }
1216
1217            // The ListCanonLower instruction is called on async function parameter lowers,
1218            // which are separated in memory by one pointer follow.
1219            //
1220            // We ignore `realloc` in the instruction because it's the name of the *import* from the
1221            // component's side (i.e. `"cabi_realloc"`). Bindings have already set up the appropriate
1222            // realloc for the current component (e.g. `realloc0`) and it is available in the bindgen
1223            // object @ `self.realloc`
1224            //
1225            // Note that this can be called *inside* a "regular" ListCanonLower, for example
1226            // when a list of lists or list of Uint8Arrays is sent.
1227            //
1228            Instruction::ListCanonLower { element, .. } => {
1229                let tmp = self.tmp();
1230                let memory = self.memory.as_ref().unwrap();
1231                let realloc = self.realloc.unwrap();
1232
1233                // Alias the list to a local variable
1234                uwriteln!(self.src, "var val{tmp} = {};", operands[0]);
1235                if matches!(element, Type::U8) {
1236                    uwriteln!(
1237                        self.src,
1238                        "var len{tmp} = Array.isArray(val{tmp}) ? val{tmp}.length : val{tmp}.byteLength;"
1239                    );
1240                } else {
1241                    uwriteln!(self.src, "var len{tmp} = val{tmp}.length;");
1242                }
1243
1244                // Gather metadata about list element
1245                let size = self.sizes.size(element).size_wasm32();
1246                let align = self.sizes.align(element).align_wasm32();
1247
1248                // Allocate space for the type in question
1249                uwriteln!(
1250                    self.src,
1251                    "var ptr{tmp} = {realloc_call}(0, 0, {align}, len{tmp} * {size});",
1252                    realloc_call = if self.is_async {
1253                        format!("await {realloc}")
1254                    } else {
1255                        realloc.to_string()
1256                    },
1257                );
1258
1259                // Determine what methods to use with a DataView when setting the data
1260                let (dataview_set_method, check_fn_intrinsic) =
1261                    gen_dataview_set_and_check_fn_js_for_numeric_type(resolve, element);
1262
1263                // Detect whether we're dealing with a regular array
1264                uwriteln!(
1265                    self.src,
1266                    r#"
1267                        let valData{tmp};
1268                        const valLenBytes{tmp} = len{tmp} * {size};
1269                        if (Array.isArray(val{tmp})) {{
1270                            // Regular array likely containing numbers, write values to memory
1271                            let offset = 0;
1272                            const dv{tmp} = new DataView({memory}.buffer);
1273                            for (const v of val{tmp}) {{
1274                                {check_fn_intrinsic}(v);
1275                                dv{tmp}.{dataview_set_method}(ptr{tmp} + offset, v, true);
1276                                offset += {size};
1277                            }}
1278                        }} else {{
1279                            // TypedArray / ArrayBuffer-like, direct copy
1280                            valData{tmp} = new Uint8Array(val{tmp}.buffer || val{tmp}, val{tmp}.byteOffset, valLenBytes{tmp});
1281                            const out{tmp} = new Uint8Array({memory}.buffer, ptr{tmp}, valLenBytes{tmp});
1282                            out{tmp}.set(valData{tmp});
1283                        }}
1284                    "#,
1285                );
1286
1287                results.push(format!("ptr{tmp}"));
1288                results.push(format!("len{tmp}"));
1289            }
1290
1291            Instruction::ListCanonLift { element, .. } => {
1292                let tmp = self.tmp();
1293                let memory = self.memory.as_ref().unwrap();
1294                uwriteln!(self.src, "var ptr{tmp} = {};", operands[0]);
1295                uwriteln!(self.src, "var len{tmp} = {};", operands[1]);
1296                uwriteln!(
1297                    self.src,
1298                    "var result{tmp} = new {array_ty}({memory}.buffer.slice(ptr{tmp}, ptr{tmp} + len{tmp} * {elem_size}));",
1299                    elem_size = self.sizes.size(element).size_wasm32(),
1300                    array_ty = js_array_ty(resolve, element).unwrap(), // TODO: this is the wrong endianness
1301                );
1302                results.push(format!("result{tmp}"));
1303            }
1304
1305            Instruction::StringLower { .. } => {
1306                // Only Utf8 and Utf16 supported for now
1307                assert!(matches!(
1308                    self.encoding,
1309                    StringEncoding::UTF8 | StringEncoding::UTF16
1310                ));
1311
1312                let (call_prefix, encode_intrinsic) = match (self.encoding, self.is_async) {
1313                    (StringEncoding::UTF16, true) => (
1314                        "await ",
1315                        Intrinsic::String(StringIntrinsic::Utf16EncodeAsync),
1316                    ),
1317                    (StringEncoding::UTF16, false) => {
1318                        ("", Intrinsic::String(StringIntrinsic::Utf16Encode))
1319                    }
1320                    (StringEncoding::UTF8, true) => (
1321                        "await ",
1322                        Intrinsic::String(StringIntrinsic::Utf8EncodeAsync),
1323                    ),
1324                    (StringEncoding::UTF8, false) => {
1325                        ("", Intrinsic::String(StringIntrinsic::Utf8Encode))
1326                    }
1327                    _ => unreachable!("unsupported encoding {}", self.encoding),
1328                };
1329                let encode = self.intrinsic(encode_intrinsic);
1330
1331                let tmp = self.tmp();
1332                let memory = self.memory.as_ref().unwrap();
1333                let str = String::from("cabi_realloc");
1334                let realloc = self.realloc.unwrap_or(&str);
1335                let s = &operands[0];
1336                uwriteln!(
1337                    self.src,
1338                    r#"
1339                      var encodeRes = {call_prefix}{encode}({s}, {realloc}, {memory});
1340                      var ptr{tmp} = encodeRes.ptr;
1341                      var len{tmp} = {encoded_len};
1342                    "#,
1343                    encoded_len = match self.encoding {
1344                        StringEncoding::UTF8 => "encodeRes.len".into(),
1345                        _ => format!("{}.length", s),
1346                    }
1347                );
1348                results.push(format!("ptr{tmp}"));
1349                results.push(format!("len{tmp}"));
1350            }
1351
1352            Instruction::StringLift => {
1353                // Only Utf8 and Utf16 supported for now
1354                assert!(matches!(
1355                    self.encoding,
1356                    StringEncoding::UTF8 | StringEncoding::UTF16
1357                ));
1358                let decoder = self.intrinsic(match self.encoding {
1359                    StringEncoding::UTF16 => Intrinsic::String(StringIntrinsic::Utf16Decoder),
1360                    _ => Intrinsic::String(StringIntrinsic::GlobalTextDecoderUtf8),
1361                });
1362                let tmp = self.tmp();
1363                let memory = self.memory.as_ref().unwrap();
1364                uwriteln!(self.src, "var ptr{tmp} = {};", operands[0]);
1365                uwriteln!(self.src, "var len{tmp} = {};", operands[1]);
1366                uwriteln!(
1367                    self.src,
1368                    "var result{tmp} = {decoder}.decode(new Uint{}Array({memory}.buffer, ptr{tmp}, len{tmp}));",
1369                    if self.encoding == StringEncoding::UTF16 {
1370                        "16"
1371                    } else {
1372                        "8"
1373                    }
1374                );
1375                results.push(format!("result{tmp}"));
1376            }
1377
1378            Instruction::ListLower { element, .. } => {
1379                let (body, body_results) = self.blocks.pop().unwrap();
1380                assert!(body_results.is_empty());
1381                let tmp = self.tmp();
1382                let vec = format!("vec{tmp}");
1383                let result = format!("result{tmp}");
1384                let len = format!("len{tmp}");
1385                let size = self.sizes.size(element).size_wasm32();
1386                let align = ArchitectureSize::from(self.sizes.align(element)).size_wasm32();
1387
1388                // first store our vec-to-lower in a temporary since we'll
1389                // reference it multiple times.
1390                uwriteln!(self.src, "var {vec} = {};", operands[0]);
1391                uwriteln!(self.src, "var {len} = {vec}.length;");
1392
1393                // ... then realloc space for the result in the guest module
1394                let realloc = self.realloc.as_ref().unwrap();
1395                uwriteln!(
1396                    self.src,
1397                    "var {result} = {realloc_call}(0, 0, {align}, {len} * {size});",
1398                    realloc_call = if self.is_async {
1399                        format!("await {realloc}")
1400                    } else {
1401                        realloc.to_string()
1402                    },
1403                );
1404
1405                // ... then consume the vector and use the block to lower the
1406                // result.
1407                uwriteln!(self.src, "for (let i = 0; i < {vec}.length; i++) {{");
1408                uwriteln!(self.src, "const e = {vec}[i];");
1409                uwrite!(self.src, "const base = {result} + i * {size};");
1410                self.src.push_str(&body);
1411                uwrite!(self.src, "}}\n");
1412
1413                results.push(result);
1414                results.push(len);
1415            }
1416
1417            Instruction::ListLift { element, .. } => {
1418                let (body, body_results) = self.blocks.pop().unwrap();
1419                let tmp = self.tmp();
1420                let size = self.sizes.size(element).size_wasm32();
1421                let len = format!("len{tmp}");
1422                uwriteln!(self.src, "var {len} = {};", operands[1]);
1423                let base = format!("base{tmp}");
1424                uwriteln!(self.src, "var {base} = {};", operands[0]);
1425                let result = format!("result{tmp}");
1426                uwriteln!(self.src, "var {result} = [];");
1427                results.push(result.clone());
1428
1429                uwriteln!(self.src, "for (let i = 0; i < {len}; i++) {{");
1430                uwriteln!(self.src, "const base = {base} + i * {size};");
1431                self.src.push_str(&body);
1432                assert_eq!(body_results.len(), 1);
1433                uwriteln!(self.src, "{result}.push({});", body_results[0]);
1434                uwrite!(self.src, "}}\n");
1435            }
1436
1437            Instruction::FixedLengthListLower { size, .. } => {
1438                let tmp = self.tmp();
1439                let array = format!("array{tmp}");
1440                uwriteln!(self.src, "const {array} = {};", operands[0]);
1441                for i in 0..*size {
1442                    results.push(format!("{array}[{i}]"));
1443                }
1444            }
1445
1446            Instruction::FixedLengthListLift { .. } => {
1447                let tmp = self.tmp();
1448                let result = format!("result{tmp}");
1449                uwriteln!(self.src, "const {result} = [{}];", operands.join(", "));
1450                results.push(result);
1451            }
1452
1453            Instruction::FixedLengthListLowerToMemory {
1454                element, size: len, ..
1455            } => {
1456                let (body, body_results) = self.blocks.pop().unwrap();
1457                assert!(body_results.is_empty());
1458
1459                let tmp = self.tmp();
1460                let array = format!("array{tmp}");
1461                uwriteln!(self.src, "const {array} = {};", operands[0]);
1462                let addr = format!("addr{tmp}");
1463                uwriteln!(self.src, "const {addr} = {};", operands[1]);
1464                let elem_size = self.sizes.size(element).size_wasm32();
1465
1466                uwriteln!(self.src, "for (let i = 0; i < {len}; i++) {{");
1467                uwriteln!(self.src, "const e = {array}[i];");
1468                uwrite!(self.src, "const base = {addr} + i * {elem_size};");
1469                self.src.push_str(&body);
1470                uwrite!(self.src, "}}\n");
1471            }
1472
1473            Instruction::FixedLengthListLiftFromMemory {
1474                element, size: len, ..
1475            } => {
1476                let (body, body_results) = self.blocks.pop().unwrap();
1477                assert_eq!(body_results.len(), 1);
1478
1479                let tmp = self.tmp();
1480                let addr = format!("addr{tmp}");
1481                uwriteln!(self.src, "const {addr} = {};", operands[0]);
1482                let elem_size = self.sizes.size(element).size_wasm32();
1483                let result = format!("result{tmp}");
1484                uwriteln!(self.src, "const {result} = [];");
1485                results.push(result.clone());
1486
1487                uwriteln!(self.src, "for (let i = 0; i < {len}; i++) {{");
1488                uwrite!(self.src, "const base = {addr} + i * {elem_size};");
1489                self.src.push_str(&body);
1490                uwriteln!(self.src, "{result}.push({});", body_results[0]);
1491                uwrite!(self.src, "}}\n");
1492            }
1493
1494            Instruction::IterElem { .. } => results.push("e".to_string()),
1495
1496            Instruction::IterBasePointer => results.push("base".to_string()),
1497
1498            Instruction::CallWasm { name, sig } => {
1499                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
1500                let has_post_return = self.post_return.is_some();
1501                let is_async = self.is_async;
1502                uwriteln!(
1503                    self.src,
1504                    "{debug_log_fn}('{prefix} [Instruction::CallWasm] enter', {{
1505                         funcName: '{name}',
1506                         paramCount: {param_count},
1507                         async: {is_async},
1508                         postReturn: {has_post_return},
1509                      }});",
1510                    param_count = sig.params.len(),
1511                    prefix = self.tracing_prefix,
1512                );
1513
1514                // Write out whether the callee was host provided
1515                // (if we're calling into wasm then we know it was not)
1516                uwriteln!(self.src, "const hostProvided = false;");
1517
1518                // Inject machinery for starting a 'current' task
1519                // (this will define the 'task' variable)
1520                self.start_current_task(inst);
1521
1522                // TODO: trap if this component is already on the call stack (re-entrancy)
1523
1524                // TODO(threads): start a thread
1525                // TODO(threads): Task#enter needs to be called with the thread that is executing (inside thread_func)
1526                // TODO(threads): thread_func will contain the actual call rather than attempting to execute immediately
1527
1528                // If we're dealing with an async task, do explicit task enter
1529                if self.is_async || self.requires_async_porcelain {
1530                    uwriteln!(
1531                        self.src,
1532                        r#"
1533                        const started = await task.enter();
1534                        if (!started) {{
1535                            {debug_log_fn}('[Instruction::AsyncTaskReturn] failed to enter task', {{
1536                                taskID: task.id(),
1537                                subtaskID: currentSubtask?.id(),
1538                            }});
1539                            throw new Error("failed to enter task");
1540                        }}
1541                        "#,
1542                    );
1543                } else {
1544                    uwriteln!(self.src, "const started = task.enterSync();",);
1545                }
1546
1547                // Set up resource scope tracking, if we're in a resource call
1548                if self.callee_resource_dynamic {
1549                    let resource_borrows =
1550                        self.intrinsic(Intrinsic::Resource(ResourceIntrinsic::ResourceCallBorrows));
1551                    let handle_tables = self.intrinsic(Intrinsic::HandleTables);
1552                    let scope_id = self.intrinsic(Intrinsic::ScopeId);
1553                    uwriteln!(
1554                        self.src,
1555                        r#"
1556                          {scope_id}++;
1557                          task.registerOnResolveHandler(() => {{
1558                              {scope_id}--;
1559                              for (const {{ rid, handle }} of {resource_borrows}) {{
1560                                  const storedScopeId = {handle_tables}[rid][handle << 1]
1561                                  if (storedScopeId === {scope_id}) {{
1562                                      throw new TypeError('borrows not dropped for resource call');
1563                                  }}
1564                              }}
1565                              {resource_borrows} = [];
1566                          }}
1567
1568                          }});
1569                        "#
1570                    );
1571                    uwriteln!(self.src, "{scope_id}++;");
1572                }
1573
1574                // Set task memory index and memory object
1575                let (memory_idx_expr, get_memory_fn_expr) =
1576                    if let Some(state) = &self.component_state {
1577                        let ComponentStateJsExprs {
1578                            memory_idx,
1579                            get_memory_fn,
1580                            ..
1581                        } = state.get_js_exprs();
1582                        (memory_idx, get_memory_fn)
1583                    } else {
1584                        ("null".into(), "() => null".into())
1585                    };
1586                uwriteln!(
1587                    self.src,
1588                    r#"
1589                      if ({memory_idx_expr} !== null) {{
1590                          task.setReturnMemoryIdx({memory_idx_expr});
1591                          task.setReturnMemory({get_memory_fn_expr}());
1592                      }}
1593                    "#
1594                );
1595
1596                // Output result binding preamble (e.g. 'var ret =', 'var [ ret0, ret1] = exports...() ')
1597                // along with the code to perofrm the call
1598                let sig_results_length = sig.results.len();
1599                let (vars_init, assignment_lhs) =
1600                    self.generate_result_assignment_lhs(sig_results_length, results, is_async);
1601
1602                let (call_prefix, call_wrapper, call_err_cleanup) =
1603                    if self.requires_async_porcelain | self.is_async {
1604                        (
1605                            "await ",
1606                            Intrinsic::WithGlobalCurrentTaskMetaFnAsync.name(),
1607                            format!(
1608                                r#"
1609                              {debug_log_fn}('[Instruction::CallWasm] error during async call', {{
1610                                  taskID: task.id(),
1611                                  err,
1612                              }});
1613                              task.setErrored(err);
1614                              task.reject(err);
1615                              task.exit();
1616                              return task.completionPromise();
1617                            "#
1618                            ),
1619                        )
1620                    } else {
1621                        (
1622                            "",
1623                            Intrinsic::WithGlobalCurrentTaskMetaFn.name(),
1624                            format!(
1625                                r#"
1626                              {debug_log_fn}('[Instruction::CallWasm] error during sync call', {{
1627                                  taskID: task.id(),
1628                                  err,
1629                              }});
1630                              task.setErrored(err);
1631                              task.reject(err);
1632                              task.exit();
1633                              throw err;
1634                            "#
1635                            ),
1636                        )
1637                    };
1638
1639                let args = if self.asmjs {
1640                    let split_i64 =
1641                        self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::SplitBigInt64));
1642
1643                    let mut args = Vec::new();
1644                    for (i, op) in operands
1645                        .drain(operands.len() - sig.params.len()..)
1646                        .enumerate()
1647                    {
1648                        if matches!(sig.params[i], WasmType::I64) {
1649                            args.push(format!("...({split_i64}({op}))"));
1650                        } else {
1651                            args.push(op);
1652                        }
1653                    }
1654                    args
1655                } else {
1656                    mem::take(operands)
1657                };
1658
1659                let mut callee_invoke = format!(
1660                    "{callee}({args})",
1661                    callee = self.callee,
1662                    args = args.join(", ")
1663                );
1664
1665                if self.asmjs {
1666                    // wasm2js does not support multivalue return
1667                    // if/when it does, this will need changing.
1668                    assert!(sig.results.len() <= 1);
1669                    // same with async(?)
1670                    assert!(!self.requires_async_porcelain && !self.is_async);
1671
1672                    if sig.results.len() == 1 && matches!(sig.results[0], WasmType::I64) {
1673                        let merge_i64 = self
1674                            .intrinsic(Intrinsic::Conversion(ConversionIntrinsic::MergeBigInt64));
1675                        callee_invoke =
1676                            format!("{merge_i64}({callee_invoke}, task.tmpRetI64HighBits)");
1677                    }
1678                }
1679
1680                uwriteln!(
1681                    self.src,
1682                    r#"
1683                      {vars_init}
1684                      try {{
1685                           {assignment_lhs} {call_prefix} {call_wrapper}({{
1686                               taskID: task.id(),
1687                               componentIdx: task.componentIdx(),
1688                               fn: () => {callee_invoke},
1689                            }});
1690                      }} catch (err) {{
1691                          {call_err_cleanup}
1692                      }}
1693                    "#,
1694                );
1695
1696                if self.tracing_enabled {
1697                    let prefix = self.tracing_prefix;
1698                    let to_result_string =
1699                        self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToResultString));
1700                    uwriteln!(
1701                        self.src,
1702                        "console.error(`{prefix} return {}`);",
1703                        if sig_results_length > 0 || !results.is_empty() {
1704                            format!("result=${{{to_result_string}(ret)}}")
1705                        } else {
1706                            "".to_string()
1707                        }
1708                    );
1709                }
1710            }
1711
1712            // Call to an imported interface (normally provided by the host)
1713            Instruction::CallInterface { func, async_ } => {
1714                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
1715                let start_current_task_fn = self.intrinsic(Intrinsic::AsyncTask(
1716                    AsyncTaskIntrinsic::CreateNewCurrentTask,
1717                ));
1718                let current_task_get_fn =
1719                    self.intrinsic(Intrinsic::AsyncTask(AsyncTaskIntrinsic::GetCurrentTask));
1720
1721                // At first, use the global current task metadata, in case we are executing from
1722                // inside a with-global-current-task wrapper
1723                let get_global_current_task_meta_fn =
1724                    self.intrinsic(Intrinsic::GetGlobalCurrentTaskMetaFn);
1725
1726                uwriteln!(
1727                    self.src,
1728                    "{debug_log_fn}('{prefix} [Instruction::CallInterface] ({async_}, @ enter)');",
1729                    prefix = self.tracing_prefix,
1730                    async_ = async_.then_some("async").unwrap_or("sync"),
1731                );
1732
1733                // Determine the callee function and arguments
1734                let (callee_fn_js, callee_args_js) = if self.callee_resource_dynamic {
1735                    (
1736                        format!("{}.{}", operands[0], self.callee),
1737                        operands[1..].join(", "),
1738                    )
1739                } else {
1740                    (self.callee.into(), operands.join(", "))
1741                };
1742
1743                uwriteln!(self.src, "const hostProvided = true;");
1744
1745                // Set task memory index and memory object
1746                let (component_idx_expr, callback_fn_name_expr, get_callback_fn_expr) =
1747                    if let Some(state) = &self.component_state {
1748                        let ComponentStateJsExprs {
1749                            component_idx,
1750                            callback_fn_name,
1751                            get_callback_fn,
1752                            ..
1753                        } = state.get_js_exprs();
1754                        (component_idx, callback_fn_name, get_callback_fn)
1755                    } else {
1756                        ("-1".into(), "null".into(), "() => null".into())
1757                    };
1758
1759                // Start the necessary subtasks and/or host task
1760                //
1761                // We must create a subtask in the case of an async host import.
1762                //
1763                // If there's no parent task, we're not executing in a subtask situation,
1764                // so we can just create the new task and immediately continue execution.
1765                //
1766                // If there *is* a parent task, then we are likely about to create new task that
1767                // matches/belongs to an existing subtask in the parent task.
1768                //
1769                // If we're dealing with a function that has been marked as a host import, then
1770                // we expect that `Trampoline::LowerImport` and relevant intrinsics were called before
1771                // this, and a subtask has been set up.
1772                //
1773                uwriteln!(
1774                    self.src,
1775                    r#"
1776                    let parentTask;
1777                    let task;
1778                    let subtask;
1779
1780                    const createTask = () => {{
1781                        const results = {start_current_task_fn}({{
1782                            componentIdx: -1,
1783                            isAsync: {is_async},
1784                            entryFnName: '{fn_name}',
1785                            getCallbackFn: {get_callback_fn_expr},
1786                            callbackFnName: {callback_fn_name_expr},
1787                            errHandling: '{err_handling}',
1788                            callingWasmExport: false,
1789                        }});
1790                        task = results[0];
1791                    }};
1792
1793                    taskCreation: {{
1794                        parentTask = {current_task_get_fn}(
1795                            {component_idx_expr},
1796                            {get_global_current_task_meta_fn}({component_idx_expr})?.taskID,
1797                        )?.task;
1798
1799                        if (!parentTask) {{
1800                            createTask();
1801                            break taskCreation;
1802                        }}
1803
1804                        createTask();
1805
1806                        if (hostProvided) {{
1807                            subtask = parentTask.getLatestSubtask();
1808                            if (!subtask) {{
1809                                throw new Error(`Missing subtask (in parent task [${{parentTask.id()}}]) for host import, has the import been lowered? (ensure asyncImports are set properly)`);
1810                            }}
1811                            task.setParentSubtask(subtask);
1812                        }}
1813                    }}
1814                    "#,
1815                    is_async = self.is_async,
1816                    fn_name = self.callee,
1817                    err_handling = self.err.to_js_string(),
1818                );
1819
1820                let is_async = self.requires_async_porcelain || *async_;
1821
1822                // If we're async then we *know* that there is a result, even if the functoin doesn't have one
1823                // at the CM level -- async functions always return
1824                let fn_wasm_result_count = if func.result.is_none() { 0 } else { 1 };
1825
1826                // If the task is async, do an explicit wait for backpressure before the call execution
1827                if is_async {
1828                    uwriteln!(
1829                        self.src,
1830                        r#"
1831                        const started = await task.enter({{ isHost: hostProvided }});
1832                        if (!started) {{
1833                            {debug_log_fn}('[Instruction::CallInterface] failed to enter task', {{
1834                                taskID: task.id(),
1835                                subtaskID: currentSubtask?.id(),
1836                            }});
1837                            throw new Error("failed to enter task");
1838                        }}
1839                        "#,
1840                    );
1841                } else {
1842                    uwriteln!(self.src, "const started = task.enterSync();",);
1843                }
1844
1845                // Build the JS expression that calls the callee
1846                let (call_prefix, call_wrapper, call_err_cleanup) = if is_async
1847                    || self.requires_async_porcelain
1848                {
1849                    (
1850                        "await ",
1851                        Intrinsic::WithGlobalCurrentTaskMetaFnAsync.name(),
1852                        format!(
1853                            r#"
1854                              {debug_log_fn}('[Instruction::CallInterface] error during async call', {{
1855                                  taskID: task.id(),
1856                                  subtaskID: currentSubtask?.id(),
1857                                  err,
1858                              }});
1859                              task.setErrored(err);
1860                              task.reject(err);
1861                              task.exit();
1862                              return task.completionPromise();
1863                            "#
1864                        ),
1865                    )
1866                } else {
1867                    (
1868                        "",
1869                        Intrinsic::WithGlobalCurrentTaskMetaFn.name(),
1870                        format!(
1871                            r#"
1872                              {debug_log_fn}('[Instruction::CallInterface] error during sync call', {{
1873                                  taskID: task.id(),
1874                                  subtaskID: currentSubtask?.id(),
1875                                  err,
1876                              }});
1877                              task.setErrored(err);
1878                              task.reject(err);
1879                              task.exit();
1880                              throw err;
1881                            "#
1882                        ),
1883                    )
1884                };
1885
1886                let call = format!(
1887                    r#"{call_prefix} {call_wrapper}({{
1888                              componentIdx: task.componentIdx(),
1889                              taskID: task.id(),
1890                              fn: () => {callee_fn_js}({callee_args_js}),
1891                          }})
1892                        "#,
1893                );
1894
1895                match self.err {
1896                    // If configured to do *no* error handling at all or throw
1897                    // error objects directly, we can simply perform the call
1898                    ErrHandling::None | ErrHandling::ThrowResultErr => {
1899                        let (vars_init, assignment_lhs) = self.generate_result_assignment_lhs(
1900                            fn_wasm_result_count,
1901                            results,
1902                            is_async,
1903                        );
1904                        uwriteln!(
1905                            self.src,
1906                            r#"
1907                              {vars_init}
1908                              try {{
1909                                 {assignment_lhs}{call};
1910                              }} catch (err) {{
1911                                  {call_err_cleanup}
1912                              }}
1913                            "#
1914                        );
1915                    }
1916                    // If configured to force all thrown errors into result objects,
1917                    // then we add a try/catch around the call
1918                    ErrHandling::ResultCatchHandler => {
1919                        // result<_, string> allows JS error coercion only, while
1920                        // any other result type will trap for arbitrary JS errors.
1921                        let err_payload = if let (_, Some(Type::Id(err_ty))) =
1922                            get_thrown_type(self.resolve, func.result).unwrap()
1923                        {
1924                            match &self.resolve.types[*err_ty].kind {
1925                                TypeDefKind::Type(Type::String) => {
1926                                    self.intrinsic(Intrinsic::GetErrorPayloadString)
1927                                }
1928                                _ => self.intrinsic(Intrinsic::GetErrorPayload),
1929                            }
1930                        } else {
1931                            self.intrinsic(Intrinsic::GetErrorPayload)
1932                        };
1933                        uwriteln!(
1934                            self.src,
1935                            r#"
1936                            let ret;
1937                            try {{
1938                                ret = {{ tag: 'ok', val: {call} }};
1939                            }} catch (e) {{
1940                                ret = {{ tag: 'err', val: {err_payload}(e) }};
1941                            }}
1942                            "#,
1943                        );
1944                        results.push("ret".to_string());
1945                    }
1946                }
1947
1948                if self.tracing_enabled {
1949                    let prefix = self.tracing_prefix;
1950                    let to_result_string =
1951                        self.intrinsic(Intrinsic::Conversion(ConversionIntrinsic::ToResultString));
1952                    uwriteln!(
1953                        self.src,
1954                        "console.error(`{prefix} return {}`);",
1955                        if fn_wasm_result_count > 0 || !results.is_empty() {
1956                            format!("result=${{{to_result_string}(ret)}}")
1957                        } else {
1958                            "".to_string()
1959                        }
1960                    );
1961                }
1962
1963                // TODO: if it was an async call, we may not be able to clear the borrows yet.
1964                // save them to the task/ensure they are added to the task's list of borrows?
1965                //
1966                // TODO: if there is a subtask, we must not clear borrows until subtask.deliverReturn
1967                // is called.
1968
1969                // After a high level call, we need to deactivate the component resource borrows.
1970                if self.clear_resource_borrows {
1971                    let symbol_resource_handle = self.intrinsic(Intrinsic::SymbolResourceHandle);
1972                    let cur_resource_borrows =
1973                        self.intrinsic(Intrinsic::Resource(ResourceIntrinsic::CurResourceBorrows));
1974                    let is_host = matches!(
1975                        self.resource_map.iter().nth(0).unwrap().1.data,
1976                        ResourceData::Host { .. }
1977                    );
1978
1979                    if is_host {
1980                        uwriteln!(
1981                            self.src,
1982                            "for (const rsc of {cur_resource_borrows}) {{
1983                                rsc[{symbol_resource_handle}] = undefined;
1984                            }}
1985                            {cur_resource_borrows} = [];"
1986                        );
1987                    } else {
1988                        uwriteln!(
1989                            self.src,
1990                            "for (const {{ rsc, drop }} of {cur_resource_borrows}) {{
1991                                if (rsc[{symbol_resource_handle}]) {{
1992                                    drop(rsc[{symbol_resource_handle}]);
1993                                    rsc[{symbol_resource_handle}] = undefined;
1994                                }}
1995                            }}
1996                            {cur_resource_borrows} = [];"
1997                        );
1998                    }
1999                    self.clear_resource_borrows = false;
2000                }
2001            }
2002
2003            Instruction::Return {
2004                func,
2005                amt: stack_value_count,
2006            } => {
2007                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
2008                uwriteln!(
2009                    self.src,
2010                    "{debug_log_fn}('{prefix} [Instruction::Return]', {{
2011                         funcName: '{func_name}',
2012                         paramCount: {stack_value_count},
2013                         async: {is_async},
2014                         postReturn: {post_return_present}
2015                      }});",
2016                    func_name = func.name,
2017                    post_return_present = self.post_return.is_some(),
2018                    is_async = self.is_async,
2019                    prefix = self.tracing_prefix,
2020                );
2021
2022                // Get the component idx expr
2023                let component_idx_expr = if let Some(state) = &self.component_state {
2024                    let ComponentStateJsExprs { component_idx, .. } = state.get_js_exprs();
2025                    component_idx
2026                } else {
2027                    "-1".into()
2028                };
2029
2030                // Build the post return functionality
2031                // to clean up tasks and possibly return values
2032                let get_or_create_async_state_fn = self.intrinsic(Intrinsic::Component(
2033                    ComponentIntrinsic::GetOrCreateAsyncState,
2034                ));
2035                let gen_post_return_js =
2036                    |(post_return_call, ret_stmt): (String, Option<String>)| {
2037                        format!(
2038                            r#"
2039                        let cstate = {get_or_create_async_state_fn}({component_idx_expr});
2040                        cstate.mayLeave = false;
2041                        {post_return_call}
2042                        cstate.mayLeave = true;
2043                        task.exit();
2044                        {ret_stmt}
2045                            "#,
2046                            ret_stmt = ret_stmt.unwrap_or_default(),
2047                        )
2048                    };
2049
2050                assert!(!self.is_async, "async functions should use AsyncTaskReturn");
2051
2052                // Depending how many values are on the stack after returning, we must execute differently.
2053                //
2054                // In particular, if this function is async (distinct from whether async porcelain was necessary or not),
2055                // rather than simply executing the function we must return (or block for) the promise that was created
2056                // for the task.
2057                match stack_value_count {
2058                    // (sync) Handle no result case
2059                    0 => {
2060                        uwriteln!(self.src, "task.resolve([ret]);");
2061                        if let Some(f) = &self.post_return {
2062                            uwriteln!(
2063                                self.src,
2064                                "{post_return_js}",
2065                                post_return_js = gen_post_return_js((format!("{f}();"), None)),
2066                            );
2067                        } else {
2068                            uwriteln!(self.src, "task.exit();");
2069                        }
2070                    }
2071
2072                    // (sync) Handle single `result<t>` case
2073                    1 if self.err == ErrHandling::ThrowResultErr => {
2074                        let component_err = self.intrinsic(Intrinsic::ComponentError);
2075                        let op = &operands[0];
2076
2077                        uwriteln!(self.src, "const retCopy = {op};");
2078                        uwriteln!(self.src, "task.resolve([retCopy.val]);");
2079
2080                        if let Some(f) = &self.post_return {
2081                            uwriteln!(
2082                                self.src,
2083                                "{}",
2084                                gen_post_return_js((format!("{f}(ret);"), None))
2085                            );
2086                        } else {
2087                            uwriteln!(self.src, "task.exit();");
2088                        }
2089
2090                        uwriteln!(
2091                            self.src,
2092                            r#"
2093                              if (typeof retCopy === 'object' && retCopy.tag === 'err') {{
2094                                  throw new {component_err}(retCopy.val);
2095                              }}
2096                              return retCopy.val;
2097                            "#
2098                        );
2099                    }
2100
2101                    // (sync) Handle all other cases (including single parameter non-result<t>)
2102                    stack_value_count => {
2103                        let ret_val = match stack_value_count {
2104                            0 => unreachable!(
2105                                "unexpectedly zero return values for synchronous return"
2106                            ),
2107                            1 => operands[0].to_string(),
2108                            _ => format!("[{}]", operands.join(", ")),
2109                        };
2110
2111                        uwriteln!(self.src, "task.resolve([{ret_val}]);");
2112
2113                        // Handle the post return if necessary
2114                        if let Some(post_return_fn) = self.post_return {
2115                            // In the case there is a post return function, we'll want to copy the value
2116                            // then perform the post return before leaving
2117
2118                            // Write out the assignment for the given return value
2119                            uwriteln!(self.src, "const retCopy = {ret_val};");
2120
2121                            // Generate the JS that should perform the post return w/ the result
2122                            // and pass a copy fo the result to the actual caller
2123                            let post_return_js = gen_post_return_js((
2124                                format!("{post_return_fn}(ret);"),
2125                                Some(["return retCopy;"].join("\n")),
2126                            ));
2127                            uwriteln!(self.src, "{post_return_js}");
2128                        } else {
2129                            uwriteln!(self.src, "task.exit();");
2130                            uwriteln!(self.src, "return {ret_val};")
2131                        }
2132                    }
2133                }
2134            }
2135
2136            Instruction::I32Load { offset } => self.load("getInt32", *offset, operands, results),
2137
2138            Instruction::I64Load { offset } => self.load("getBigInt64", *offset, operands, results),
2139
2140            Instruction::F32Load { offset } => self.load("getFloat32", *offset, operands, results),
2141
2142            Instruction::F64Load { offset } => self.load("getFloat64", *offset, operands, results),
2143
2144            Instruction::I32Load8U { offset } => self.load("getUint8", *offset, operands, results),
2145
2146            Instruction::I32Load8S { offset } => self.load("getInt8", *offset, operands, results),
2147
2148            Instruction::I32Load16U { offset } => {
2149                self.load("getUint16", *offset, operands, results)
2150            }
2151
2152            Instruction::I32Load16S { offset } => self.load("getInt16", *offset, operands, results),
2153
2154            Instruction::I32Store { offset } => self.store("setInt32", *offset, operands),
2155
2156            Instruction::I64Store { offset } => self.store("setBigInt64", *offset, operands),
2157
2158            Instruction::F32Store { offset } => self.store("setFloat32", *offset, operands),
2159
2160            Instruction::F64Store { offset } => self.store("setFloat64", *offset, operands),
2161
2162            Instruction::I32Store8 { offset } => self.store("setInt8", *offset, operands),
2163
2164            Instruction::I32Store16 { offset } => self.store("setInt16", *offset, operands),
2165
2166            Instruction::LengthStore { offset } => self.store("setUint32", *offset, operands),
2167
2168            Instruction::LengthLoad { offset } => {
2169                self.load("getUint32", *offset, operands, results)
2170            }
2171
2172            Instruction::PointerStore { offset } => self.store("setUint32", *offset, operands),
2173
2174            Instruction::PointerLoad { offset } => {
2175                self.load("getUint32", *offset, operands, results)
2176            }
2177
2178            Instruction::Malloc { size, align, .. } => {
2179                let tmp = self.tmp();
2180                let realloc = self.realloc.as_ref().unwrap();
2181                let ptr = format!("ptr{tmp}");
2182                uwriteln!(
2183                    self.src,
2184                    "var {ptr} = {realloc_call}(0, 0, {align}, {size});",
2185                    align = align.align_wasm32(),
2186                    realloc_call = if self.is_async {
2187                        format!("await {realloc}")
2188                    } else {
2189                        realloc.to_string()
2190                    },
2191                    size = size.size_wasm32()
2192                );
2193                results.push(ptr);
2194            }
2195
2196            Instruction::HandleLift { handle, .. } => {
2197                let (Handle::Own(ty) | Handle::Borrow(ty)) = handle;
2198                let resource_ty = &crate::dealias(self.resolve, *ty);
2199                let ResourceTable { imported, data } = &self.resource_map[resource_ty];
2200
2201                let is_own = matches!(handle, Handle::Own(_));
2202                let rsc = format!("rsc{}", self.tmp());
2203                let handle = format!("handle{}", self.tmp());
2204                uwriteln!(self.src, "var {handle} = {};", &operands[0]);
2205
2206                match data {
2207                    ResourceData::Host {
2208                        tid,
2209                        rid,
2210                        local_name,
2211                        dtor_name,
2212                    } => {
2213                        let tid = tid.as_u32();
2214                        let rid = rid.as_u32();
2215                        let symbol_dispose = self.intrinsic(Intrinsic::SymbolDispose);
2216                        let rsc_table_remove = self
2217                            .intrinsic(Intrinsic::Resource(ResourceIntrinsic::ResourceTableRemove));
2218                        let rsc_flag = self
2219                            .intrinsic(Intrinsic::Resource(ResourceIntrinsic::ResourceTableFlag));
2220                        if !imported {
2221                            let symbol_resource_handle =
2222                                self.intrinsic(Intrinsic::SymbolResourceHandle);
2223
2224                            uwriteln!(
2225                                self.src,
2226                                "var {rsc} = new.target === {local_name} ? this : Object.create({local_name}.prototype);"
2227                            );
2228
2229                            if is_own {
2230                                // Sending an own handle out to JS as a return value - set up finalizer and disposal.
2231                                let empty_func = self
2232                                    .intrinsic(Intrinsic::JsHelper(JsHelperIntrinsic::EmptyFunc));
2233                                uwriteln!(self.src,
2234                                            "Object.defineProperty({rsc}, {symbol_resource_handle}, {{ writable: true, value: {handle} }});
2235                                    finalizationRegistry{tid}.register({rsc}, {handle}, {rsc});");
2236                                if let Some(dtor) = dtor_name {
2237                                    // The Symbol.dispose function gets disabled on drop, so we can rely on the own handle remaining valid.
2238                                    uwriteln!(
2239                                                self.src,
2240                                                "Object.defineProperty({rsc}, {symbol_dispose}, {{ writable: true, value: function () {{
2241                                            finalizationRegistry{tid}.unregister({rsc});
2242                                            {rsc_table_remove}(handleTable{tid}, {handle});
2243                                            {rsc}[{symbol_dispose}] = {empty_func};
2244                                            {rsc}[{symbol_resource_handle}] = undefined;
2245                                            {dtor}(handleTable{tid}[({handle} << 1) + 1] & ~{rsc_flag});
2246                                        }}}});"
2247                                            );
2248                                } else {
2249                                    // Set up Symbol.dispose for borrows to allow its call, even though it does nothing.
2250                                    uwriteln!(
2251                                        self.src,
2252                                        "Object.defineProperty({rsc}, {symbol_dispose}, {{ writable: true, value: {empty_func} }});",
2253                                    );
2254                                }
2255                            } else {
2256                                // Borrow handles of local resources have rep handles, which we carry through here.
2257                                uwriteln!(
2258                                    self.src,
2259                                    "Object.defineProperty({rsc}, {symbol_resource_handle}, {{ writable: true, value: {handle} }});"
2260                                );
2261                            }
2262                        } else {
2263                            let rep = format!("rep{}", self.tmp());
2264                            // Imported handles either lift as instance capture from a previous lowering,
2265                            // or we create a new JS class to represent it.
2266                            let symbol_resource_rep = self.intrinsic(Intrinsic::SymbolResourceRep);
2267                            let symbol_resource_handle =
2268                                self.intrinsic(Intrinsic::SymbolResourceHandle);
2269
2270                            uwriteln!(
2271                                self.src,
2272                                r#"
2273                                  var {rep} = handleTable{tid}[({handle} << 1) + 1] & ~{rsc_flag};
2274                                  var {rsc} = captureTable{rid}.get({rep});
2275                                  if (!{rsc}) {{
2276                                      {rsc} = Object.create({local_name}.prototype);
2277                                      Object.defineProperty({rsc}, {symbol_resource_handle}, {{ writable: true, value: {handle} }});
2278                                      Object.defineProperty({rsc}, {symbol_resource_rep}, {{ writable: true, value: {rep} }});
2279                                  }}
2280                                "#,
2281                            );
2282
2283                            if is_own {
2284                                // An own lifting is a transfer to JS, so existing own handle is implicitly dropped.
2285                                uwriteln!(
2286                                    self.src,
2287                                    "else {{
2288                                        captureTable{rid}.delete({rep});
2289                                    }}
2290                                    {rsc_table_remove}(handleTable{tid}, {handle});"
2291                                );
2292                            }
2293                        }
2294
2295                        // Borrow handles are tracked to release after the call by CallInterface.
2296                        if !is_own {
2297                            let cur_resource_borrows = self.intrinsic(Intrinsic::Resource(
2298                                ResourceIntrinsic::CurResourceBorrows,
2299                            ));
2300                            uwriteln!(self.src, "{cur_resource_borrows}.push({rsc});");
2301                            self.clear_resource_borrows = true;
2302                        }
2303                    }
2304
2305                    ResourceData::Guest {
2306                        resource_name,
2307                        prefix,
2308                        extra,
2309                    } => {
2310                        assert!(
2311                            extra.is_none(),
2312                            "plain resource handles do not carry extra data"
2313                        );
2314
2315                        let symbol_resource_handle =
2316                            self.intrinsic(Intrinsic::SymbolResourceHandle);
2317                        let prefix = prefix.as_deref().unwrap_or("");
2318                        let lower_camel = resource_name.to_lower_camel_case();
2319
2320                        if !imported {
2321                            if is_own {
2322                                uwriteln!(
2323                                    self.src,
2324                                    "var {rsc} = repTable.get($resource_{prefix}rep${lower_camel}({handle})).rep;"
2325                                );
2326                                uwrite!(
2327                                    self.src,
2328                                    r#"
2329                                      repTable.delete({handle});
2330                                      delete {rsc}[{symbol_resource_handle}];
2331                                      finalizationRegistry_export${prefix}{lower_camel}.unregister({rsc});
2332                                    "#
2333                                );
2334                            } else {
2335                                uwriteln!(self.src, "var {rsc} = repTable.get({handle}).rep;");
2336                            }
2337                        } else {
2338                            let upper_camel = resource_name.to_upper_camel_case();
2339
2340                            uwrite!(
2341                                self.src,
2342                                r#"
2343                                  var {rsc} = new.target === import_{prefix}{upper_camel} ? this : Object.create(import_{prefix}{upper_camel}.prototype);
2344                                   Object.defineProperty({rsc}, {symbol_resource_handle}, {{ writable: true, value: {handle} }});
2345                                "#
2346                            );
2347
2348                            uwriteln!(
2349                                self.src,
2350                                "finalizationRegistry_import${prefix}{lower_camel}.register({rsc}, {handle}, {rsc});",
2351                            );
2352
2353                            if !is_own {
2354                                let cur_resource_borrows = self.intrinsic(Intrinsic::Resource(
2355                                    ResourceIntrinsic::CurResourceBorrows,
2356                                ));
2357                                uwriteln!(
2358                                    self.src,
2359                                    "{cur_resource_borrows}.push({{ rsc: {rsc}, drop: $resource_import${prefix}drop${lower_camel} }});"
2360                                );
2361                                self.clear_resource_borrows = true;
2362                            }
2363                        }
2364                    }
2365                }
2366                results.push(rsc);
2367            }
2368
2369            Instruction::HandleLower { handle, name, .. } => {
2370                let (Handle::Own(ty) | Handle::Borrow(ty)) = handle;
2371                let is_own = matches!(handle, Handle::Own(_));
2372                let ResourceTable { imported, data } =
2373                    &self.resource_map[&crate::dealias(self.resolve, *ty)];
2374
2375                let class_name = name.to_upper_camel_case();
2376                let handle = format!("handle{}", self.tmp());
2377                let symbol_resource_handle = self.intrinsic(Intrinsic::SymbolResourceHandle);
2378                let symbol_dispose = self.intrinsic(Intrinsic::SymbolDispose);
2379                let op = &operands[0];
2380
2381                match data {
2382                    ResourceData::Host {
2383                        tid,
2384                        rid,
2385                        local_name,
2386                        ..
2387                    } => {
2388                        let tid = tid.as_u32();
2389                        let rid = rid.as_u32();
2390
2391                        match (imported, is_own) {
2392                            // Imported, owned host-provided resource
2393                            (_imported @ false, _owned @ true) => {
2394                                let empty_func = self
2395                                    .intrinsic(Intrinsic::JsHelper(JsHelperIntrinsic::EmptyFunc));
2396                                uwriteln!(
2397                                    self.src,
2398                                    r#"
2399                                      var {handle} = {op}[{symbol_resource_handle}];
2400                                      if (!{handle}) {{
2401                                          throw new TypeError('Resource error: Not a valid \"{class_name}\" resource.');
2402                                      }}
2403                                      finalizationRegistry{tid}.unregister({op});
2404                                      {op}[{symbol_dispose}] = {empty_func};
2405                                      {op}[{symbol_resource_handle}] = undefined;
2406                                    "#,
2407                                );
2408                            }
2409
2410                            // Imported, borrowed host-provdied resource
2411                            (_imported @ false, _owned @ false) => {
2412                                // When expecting a borrow, the JS resource provided will always be an own
2413                                // handle. This is because it is not possible for borrow handles to be passed
2414                                // back reentrantly.
2415                                // We then set the handle to the rep per the local borrow rule.
2416                                let rsc_flag = self.intrinsic(Intrinsic::Resource(
2417                                    ResourceIntrinsic::ResourceTableFlag,
2418                                ));
2419                                let own_handle = format!("handle{}", self.tmp());
2420                                uwriteln!(
2421                                    self.src,
2422                                    r#"
2423                                      var {own_handle} = {op}[{symbol_resource_handle}];
2424                                      if (!{own_handle} || (handleTable{tid}[({own_handle} << 1) + 1] & {rsc_flag}) === 0) {{
2425                                          throw new TypeError('Resource error: Not a valid \"{class_name}\" resource.');
2426                                      }}
2427                                      var {handle} = handleTable{tid}[({own_handle} << 1) + 1] & ~{rsc_flag};
2428                                    "#,
2429                                );
2430                            }
2431
2432                            // Imported, owned guest-provided resource
2433                            (_imported @ true, _owned @ true) => {
2434                                // Imported resources may already have a handle if they were constructed
2435                                // by a component and then passed out.
2436                                //
2437                                // If the handle is not present, in hybrid bindgen we check for a Symbol.for('cabiRep')
2438                                // to get the resource rep.
2439                                //
2440                                // Fall back to assign a new rep in the capture table, when the imported
2441                                // resource was constructed externally.
2442                                let symbol_resource_rep =
2443                                    self.intrinsic(Intrinsic::SymbolResourceRep);
2444                                let create_own_fn = self.intrinsic(Intrinsic::Resource(
2445                                    ResourceIntrinsic::ResourceTableCreateOwn,
2446                                ));
2447
2448                                uwriteln!(
2449                                    self.src,
2450                                    r#"
2451                                      if (!({op} instanceof {local_name})) {{
2452                                          throw new TypeError('Resource error: Not a valid \"{class_name}\" resource.');
2453                                      }}
2454                                      var {handle} = {op}[{symbol_resource_handle}];
2455                                      if (!{handle}) {{
2456                                          const rep = {op}[{symbol_resource_rep}] || ++captureCnt{rid};
2457                                          captureTable{rid}.set(rep, {op});
2458                                          {handle} = {create_own_fn}(handleTable{tid}, rep);
2459                                      }}
2460                                    "#
2461                                );
2462                            }
2463
2464                            // Imported, borrowed guest-provided resource
2465                            (_imported @ true, _owned @ false) => {
2466                                // Imported resources may already have a handle if they were constructed
2467                                // by a component and then passed out.
2468                                //
2469                                // Otherwise, in hybrid bindgen we check for a Symbol.for('cabiRep')
2470                                // to get the resource rep.
2471                                // Fall back to assign a new rep in the capture table, when the imported
2472                                // resource was constructed externally.
2473
2474                                let symbol_resource_rep =
2475                                    self.intrinsic(Intrinsic::SymbolResourceRep);
2476                                let scope_id = self.intrinsic(Intrinsic::ScopeId);
2477                                let create_borrow_fn = self.intrinsic(Intrinsic::Resource(
2478                                    ResourceIntrinsic::ResourceTableCreateBorrow,
2479                                ));
2480
2481                                uwriteln!(
2482                                    self.src,
2483                                    r#"
2484                                      if (!({op} instanceof {local_name})) {{
2485                                          throw new TypeError('Resource error: Not a valid \"{class_name}\" resource.');
2486                                      }}
2487                                      var {handle} = {op}[{symbol_resource_handle}];
2488                                      if (!{handle}) {{
2489                                          const rep = {op}[{symbol_resource_rep}] || ++captureCnt{rid};
2490                                          captureTable{rid}.set(rep, {op});
2491                                          {handle} = {create_borrow_fn}(handleTable{tid}, rep, {scope_id});
2492                                      }}
2493                                    "#
2494                                );
2495                            }
2496                        }
2497                    }
2498
2499                    ResourceData::Guest {
2500                        resource_name,
2501                        prefix,
2502                        extra,
2503                    } => {
2504                        assert!(
2505                            extra.is_none(),
2506                            "plain resource handles do not carry extra data"
2507                        );
2508
2509                        let upper_camel = resource_name.to_upper_camel_case();
2510                        let lower_camel = resource_name.to_lower_camel_case();
2511                        let prefix = prefix.as_deref().unwrap_or("");
2512
2513                        let symbol_resource_handle =
2514                            self.intrinsic(Intrinsic::SymbolResourceHandle);
2515
2516                        let tmp = self.tmp();
2517                        match (imported, is_own) {
2518                            // imported owned/borrowed guest resource
2519                            (_imported @ true, _owned) => {
2520                                uwrite!(
2521                                    self.src,
2522                                    r#"
2523                                      var {handle} = {op}[{symbol_resource_handle}];
2524                                      finalizationRegistry_import${prefix}{lower_camel}.unregister({op});
2525                                    "#
2526                                );
2527                            }
2528
2529                            // Not-imported, borrowed guest resource
2530                            (_imported @ false, _owned @ false) => {
2531                                let local_rep = format!("localRep{tmp}");
2532                                uwriteln!(
2533                                    self.src,
2534                                    r#"
2535                                      if (!({op} instanceof {upper_camel})) {{
2536                                          throw new TypeError('Resource error: Not a valid \"{upper_camel}\" resource.');
2537                                      }}
2538                                      let {handle} = {op}[{symbol_resource_handle}];
2539                                      if ({handle} === undefined) {{
2540                                          var {local_rep} = repCnt++;
2541                                          repTable.set({local_rep}, {{ rep: {op}, own: false }});
2542                                          {op}[{symbol_resource_handle}] = {local_rep};
2543                                      }}
2544                                    "#
2545                                );
2546                            }
2547
2548                            // Not-imported, owned guest resource
2549                            (_imported @ false, _owned @ true) => {
2550                                let local_rep = format!("localRep{tmp}");
2551                                uwriteln!(
2552                                    self.src,
2553                                    r#"
2554                                      if (!({op} instanceof {upper_camel})) {{
2555                                          throw new TypeError('Resource error: Not a valid \"{upper_camel}\" resource.');
2556                                      }}
2557                                      let {handle} = {op}[{symbol_resource_handle}];
2558                                      if ({handle} === undefined) {{
2559                                          var {local_rep} = repCnt++;
2560                                          repTable.set({local_rep}, {{ rep: {op}, own: true }});
2561                                          {handle} = $resource_{prefix}new${lower_camel}({local_rep});
2562                                          {op}[{symbol_resource_handle}] = {handle};
2563                                          finalizationRegistry_export${prefix}{lower_camel}.register({op}, {handle}, {op});
2564                                      }}
2565                                    "#
2566                                );
2567                            }
2568                        }
2569                    }
2570                }
2571                results.push(handle);
2572            }
2573
2574            Instruction::DropHandle { ty } => {
2575                let _ = ty;
2576                todo!("[Instruction::DropHandle] not yet implemented")
2577            }
2578
2579            Instruction::Flush { amt } => {
2580                for item in operands.iter().take(*amt) {
2581                    results.push(item.clone());
2582                }
2583            }
2584
2585            Instruction::ErrorContextLift => {
2586                let item = operands
2587                    .first()
2588                    .expect("unexpectedly missing ErrorContextLift arg");
2589                results.push(item.clone());
2590            }
2591
2592            Instruction::ErrorContextLower => {
2593                let item = operands
2594                    .first()
2595                    .expect("unexpectedly missing ErrorContextLower arg");
2596                results.push(item.clone());
2597            }
2598
2599            Instruction::FutureLower { ty, .. } => {
2600                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
2601                let get_or_create_async_state_fn = self.intrinsic(Intrinsic::Component(
2602                    ComponentIntrinsic::GetOrCreateAsyncState,
2603                ));
2604                let gen_future_host_inject_fn = self.intrinsic(Intrinsic::AsyncFuture(
2605                    AsyncFutureIntrinsic::GenFutureHostInjectFn,
2606                ));
2607                let is_future_lowerable_object_fn = self.intrinsic(Intrinsic::AsyncFuture(
2608                    AsyncFutureIntrinsic::IsFutureLowerableObject,
2609                ));
2610                let nested_future_symbol = self.intrinsic(Intrinsic::AsyncFuture(
2611                    AsyncFutureIntrinsic::NestedFutureSymbol,
2612                ));
2613
2614                let future_arg = operands
2615                    .first()
2616                    .expect("unexpectedly missing ErrorContextLower arg");
2617
2618                // Build the lowering function for the type produced by the future
2619                let type_id = &crate::dealias(self.resolve, *ty);
2620                let ResourceTable {
2621                    imported: true,
2622                    data:
2623                        ResourceData::Guest {
2624                            extra:
2625                                Some(ResourceExtraData::Future {
2626                                    table_idx: future_table_idx_ty,
2627                                    nesting_level,
2628                                    elem_ty,
2629                                }),
2630                            ..
2631                        },
2632                } = self
2633                    .resource_map
2634                    .get(type_id)
2635                    .expect("missing resource mapping for future lower")
2636                else {
2637                    unreachable!("invalid resource table observed during future lower");
2638                };
2639                let future_table_idx = future_table_idx_ty.as_u32();
2640
2641                // Generate payload metadata ('elemMeta')
2642                let (
2643                    payload_type_name_js,
2644                    lift_fn_js,
2645                    lower_fn_js,
2646                    payload_is_none,
2647                    payload_is_numeric,
2648                    payload_is_borrow,
2649                    payload_is_async_value,
2650                    payload_size32_js,
2651                    payload_align32_js,
2652                    payload_flat_count_js,
2653                ) = match elem_ty {
2654                    Some(PayloadTypeMetadata {
2655                        ty: _,
2656                        iface_ty,
2657                        lift_js_expr,
2658                        lower_js_expr,
2659                        size32,
2660                        align32,
2661                        flat_count,
2662                    }) => (
2663                        format!("'{iface_ty:?}'"),
2664                        lift_js_expr.as_str(),
2665                        lower_js_expr.as_str(),
2666                        "false",
2667                        format!(
2668                            "{}",
2669                            matches!(
2670                                iface_ty,
2671                                InterfaceType::U8
2672                                    | InterfaceType::U16
2673                                    | InterfaceType::U32
2674                                    | InterfaceType::U64
2675                                    | InterfaceType::S8
2676                                    | InterfaceType::S16
2677                                    | InterfaceType::S32
2678                                    | InterfaceType::S64
2679                                    | InterfaceType::Float32
2680                                    | InterfaceType::Float64
2681                            )
2682                        ),
2683                        format!("{}", matches!(iface_ty, InterfaceType::Borrow(_))),
2684                        format!(
2685                            "{}",
2686                            matches!(
2687                                iface_ty,
2688                                InterfaceType::Stream(_) | InterfaceType::Future(_)
2689                            )
2690                        ),
2691                        size32.to_string(),
2692                        align32.to_string(),
2693                        flat_count.unwrap_or(0).to_string(),
2694                    ),
2695                    None => (
2696                        "null".into(),
2697                        "() => {{ throw new Error('no lift fn'); }}",
2698                        "() => {{ throw new Error('no lower fn'); }}",
2699                        "true",
2700                        "false".into(),
2701                        "false".into(),
2702                        "false".into(),
2703                        "null".into(),
2704                        "null".into(),
2705                        "0".into(),
2706                    ),
2707                };
2708
2709                let tmp = self.tmp();
2710                let lowered_future_waitable_idx = format!("futureWaitableIdx{tmp}");
2711
2712                let (component_idx_expr, get_realloc_fn_expr) =
2713                    if let Some(state) = &self.component_state {
2714                        let ComponentStateJsExprs {
2715                            component_idx,
2716                            get_realloc_fn,
2717                            ..
2718                        } = state.get_js_exprs();
2719                        (component_idx, get_realloc_fn)
2720                    } else {
2721                        ("-1".into(), "undefined".into())
2722                    };
2723
2724                uwriteln!(
2725                    self.src,
2726                    r#"
2727                        if (!{is_future_lowerable_object_fn}({future_arg})) {{
2728                            {debug_log_fn}('[Instruction::FutureLower] object is not a Promise/Thenable', {{ {future_arg} }});
2729                            throw new Error('unrecognized future object (not Promise/Thenable)');
2730                        }}
2731
2732                        const cstate{tmp} = {get_or_create_async_state_fn}({component_idx_expr});
2733                        if (!cstate{tmp}) {{
2734                            throw new Error(`missing component state for component [{component_idx_expr}]`);
2735                        }}
2736
2737                        // TODO(feat): facilitate non utf8 string encoding for lowered futures
2738                        const stringEncoding = 'utf8';
2739
2740                        let outermostReadEnd{tmp};
2741                        let futuresList{tmp} = [];
2742                        let future{tmp} = {future_arg};
2743                        let nextFuture{tmp};
2744                        let openedCount = -1;
2745                        let futureNestingLevel{tmp} = {nesting_level};
2746
2747                        while (futureNestingLevel{tmp} >= 0) {{
2748                            const {{
2749                                writeEnd,
2750                                writeEndWaitableIdx,
2751                                readEnd,
2752                                readEndWaitableIdx
2753                            }} = cstate{tmp}.createFuture({{
2754                                tableIdx: {future_table_idx},
2755                                elemMeta: {{
2756                                    liftFn: {lift_fn_js},
2757                                    lowerFn: {lower_fn_js},
2758                                    payloadTypeName: {payload_type_name_js},
2759                                    isNone: {payload_is_none},
2760                                    isNumeric: {payload_is_numeric},
2761                                    isBorrowed: {payload_is_borrow},
2762                                    isAsyncValue: {payload_is_async_value},
2763                                    flatCount: {payload_flat_count_js},
2764                                    align32: {payload_align32_js},
2765                                    size32: {payload_size32_js},
2766                                    stringEncoding,
2767                                    getReallocFn: {get_realloc_fn_expr},
2768                                }}
2769                            }});
2770
2771                            const hostInjectFn = {gen_future_host_inject_fn}({{
2772                                promise: future{tmp},
2773                                stringEncoding,
2774                                hostWriteEnd: writeEnd,
2775                            }});
2776                            readEnd.setHostInjectFn(hostInjectFn);
2777
2778                            const meta{tmp} = {{
2779                                isInnermost: futureNestingLevel{tmp} === {nesting_level},
2780                                level: futureNestingLevel{tmp},
2781                            }};
2782
2783                            const innerFuture = future{tmp};
2784                            future{tmp} = {{ }};
2785                            future{tmp}[{nested_future_symbol}] = meta{tmp};
2786                            future{tmp}.readEndWaitableIdx = readEndWaitableIdx;
2787                            future{tmp}.writeEndWaitableIdx = writeEndWaitableIdx;
2788                            future{tmp}.futureTableIdx = {future_table_idx};
2789                            future{tmp}.componentIdx = {component_idx_expr};
2790                            future{tmp}.then = async (resolve, reject) => {{
2791                                let p;
2792                                if (openedCount === {nesting_level}) {{
2793                                    p = innerFuture;
2794                                }} else {{
2795                                    openedCount++;
2796                                    p = futuresList{tmp}[futuresList{tmp}.length - (openedCount + 1)];
2797                                }}
2798
2799                                try {{
2800                                    resolve(await p);
2801                                }} catch (err) {{
2802                                    reject(err);
2803                                }}
2804                            }};
2805
2806                            outermostReadEnd{tmp} = readEnd;
2807
2808                            futuresList{tmp}.push(future{tmp});
2809                            futureNestingLevel{tmp}--;
2810                        }}
2811
2812                        const readEnd{tmp} = outermostReadEnd{tmp};
2813
2814                        // TODO: need to *lower* the internal future???
2815
2816                        const {lowered_future_waitable_idx} = readEnd{tmp}.waitableIdx();
2817                    "#
2818                );
2819
2820                results.push(lowered_future_waitable_idx);
2821            }
2822
2823            Instruction::FutureLift { payload, ty } => {
2824                let future_new_from_lift_fn = self.intrinsic(Intrinsic::AsyncFuture(
2825                    AsyncFutureIntrinsic::FutureNewFromLift,
2826                ));
2827
2828                // We must look up the type idx to find the future
2829                let type_id = &crate::dealias(self.resolve, *ty);
2830                let ResourceTable {
2831                    imported: true,
2832                    data:
2833                        ResourceData::Guest {
2834                            extra:
2835                                Some(ResourceExtraData::Future {
2836                                    table_idx: future_table_idx_ty,
2837                                    elem_ty: future_element_ty,
2838                                    ..
2839                                }),
2840                            ..
2841                        },
2842                } = self
2843                    .resource_map
2844                    .get(type_id)
2845                    .expect("missing resource mapping for future lift")
2846                else {
2847                    unreachable!("invalid resource table observed during future lift");
2848                };
2849
2850                // if a future element is present, it should match the payload we're getting
2851                let (lift_fn_js, lower_fn_js) = match future_element_ty {
2852                    Some(PayloadTypeMetadata {
2853                        ty,
2854                        lift_js_expr,
2855                        lower_js_expr,
2856                        ..
2857                    }) => {
2858                        assert_eq!(Some(*ty), **payload, "future element type mismatch");
2859                        (lift_js_expr.to_string(), lower_js_expr.to_string())
2860                    }
2861                    None => (
2862                        "() => {{ throw new Error('no lift fn'); }}".into(),
2863                        "() => {{ throw new Error('no lower fn'); }}".into(),
2864                    ),
2865                };
2866                if let Some(PayloadTypeMetadata { ty, .. }) = future_element_ty {
2867                    assert_eq!(Some(*ty), **payload, "future element type mismatch");
2868                }
2869
2870                let tmp = self.tmp();
2871                let result_var = format!("futureResult{tmp}");
2872
2873                // Optionally preform the lift for the future in question
2874                match (self.is_async, self.for_import.unwrap_or_default()) {
2875                    // It is possible for lifting to be called both at the *start* and *end* of
2876                    // a given function depending on how it called:
2877                    //
2878                    // 1. lifting results to convert a component-produced result for use by the host *after* `CallWasm` returns
2879                    // 2. lifting parameters (`future` -> `Promise`), for use by the host, *before* `CallInterface`
2880                    //
2881                    // In (1), the function being generated must correspond to an export (from a component), and we only
2882                    // perform the lifting if we know the value is imminently ready (i.e. the sync case).
2883                    //
2884                    // In (2) the function must correspond to an import (from the host), and regardless of whether
2885                    // the function being generated is async or not, the host *must* deal in terms of lifted values
2886                    // (i.e. `Promise`, not index to a future)
2887                    //
2888                    (_is_async @ false, _for_import @ false) | (_is_async, _for_import @ true) => {
2889                        // If we're dealing with a sync function, we can use the return directly
2890                        let arg_future_end_idx = operands
2891                            .first()
2892                            .expect("unexpectedly missing future end return arg in FutureLift");
2893
2894                        let (payload_ty_size32_js, payload_ty_align32_js) =
2895                            if let Some(payload_ty) = payload {
2896                                (
2897                                    self.sizes.size(payload_ty).size_wasm32().to_string(),
2898                                    self.sizes.align(payload_ty).align_wasm32().to_string(),
2899                                )
2900                            } else {
2901                                ("null".into(), "null".into())
2902                            };
2903
2904                        let future_table_idx = future_table_idx_ty.as_u32();
2905
2906                        // Set task memory index and memory object
2907                        let component_idx_expr = if let Some(state) = &self.component_state {
2908                            let ComponentStateJsExprs { component_idx, .. } = state.get_js_exprs();
2909                            component_idx
2910                        } else {
2911                            "-1".into()
2912                        };
2913
2914                        // We only need to write the result var for use *if* the
2915                        // function that is being executed is provided by the host (i.e. `CallInterface`)
2916                        //
2917                        // The future value in question is being lifted *from* the component,
2918                        // such that the host call can use it (as a `Promise`).
2919                        //
2920                        // `hostProvided` is set hoistably in `CallWasm`/`CallInterface`
2921                        uwriteln!(
2922                            self.src,
2923                            r#"
2924                              const {result_var} = {future_new_from_lift_fn}({{
2925                                  componentIdx: {component_idx_expr},
2926                                  futureTableIdx: {future_table_idx},
2927                                  futureEndWaitableIdx: {arg_future_end_idx},
2928                                  payloadLiftFn: {lift_fn_js},
2929                                  payloadLowerFn: {lower_fn_js},
2930                                  payloadTypeSize32: {payload_ty_size32_js},
2931                                  payloadTypeAlign32: {payload_ty_align32_js},
2932                              }});
2933                            "#,
2934                        );
2935                    }
2936
2937                    // For all other cases, we do not need to perform the lift
2938                    _ => {}
2939                }
2940
2941                results.push(result_var.clone());
2942            }
2943
2944            Instruction::StreamLower { ty, .. } => {
2945                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
2946                let stream_arg = operands
2947                    .first()
2948                    .expect("unexpectedly missing StreamLower arg");
2949                let async_iterator_symbol = self.intrinsic(Intrinsic::SymbolAsyncIterator);
2950                let iterator_symbol = self.intrinsic(Intrinsic::SymbolIterator);
2951                let symbol_dispose = self.intrinsic(Intrinsic::SymbolDispose);
2952                let external_readable_stream_class =
2953                    self.intrinsic(Intrinsic::PlatformReadableStreamClass);
2954                let get_or_create_async_state_fn = self.intrinsic(Intrinsic::Component(
2955                    ComponentIntrinsic::GetOrCreateAsyncState,
2956                ));
2957                let gen_stream_host_inject_fn = self.intrinsic(Intrinsic::AsyncStream(
2958                    AsyncStreamIntrinsic::GenStreamHostInjectFn,
2959                ));
2960
2961                // TODO(???): A component could end up receiving a stream that it outputted,
2962                // and the below would fail (imported: false)?
2963
2964                // Build the lowering function for the type produced by the stream
2965                let type_id = &crate::dealias(self.resolve, *ty);
2966                let ResourceTable {
2967                    imported: true,
2968                    data:
2969                        ResourceData::Guest {
2970                            extra:
2971                                Some(ResourceExtraData::Stream {
2972                                    table_idx: stream_table_idx_ty,
2973                                    elem_ty,
2974                                }),
2975                            ..
2976                        },
2977                } = self
2978                    .resource_map
2979                    .get(type_id)
2980                    .expect("missing resource mapping for stream lower")
2981                else {
2982                    unreachable!("invalid resource table observed during stream lower");
2983                };
2984
2985                let stream_table_idx = stream_table_idx_ty.as_u32();
2986
2987                let (
2988                    payload_type_name_js,
2989                    lift_fn_js,
2990                    lower_fn_js,
2991                    payload_is_none,
2992                    payload_is_numeric,
2993                    payload_is_borrow,
2994                    payload_is_async_value,
2995                    payload_size32_js,
2996                    payload_align32_js,
2997                    payload_flat_count_js,
2998                ) = match elem_ty {
2999                    Some(PayloadTypeMetadata {
3000                        ty: _,
3001                        iface_ty,
3002                        lift_js_expr,
3003                        lower_js_expr,
3004                        size32,
3005                        align32,
3006                        flat_count,
3007                    }) => (
3008                        format!("'{iface_ty:?}'"),
3009                        lift_js_expr.as_str(),
3010                        lower_js_expr.as_str(),
3011                        "false",
3012                        format!(
3013                            "{}",
3014                            matches!(
3015                                iface_ty,
3016                                InterfaceType::U8
3017                                    | InterfaceType::U16
3018                                    | InterfaceType::U32
3019                                    | InterfaceType::U64
3020                                    | InterfaceType::S8
3021                                    | InterfaceType::S16
3022                                    | InterfaceType::S32
3023                                    | InterfaceType::S64
3024                                    | InterfaceType::Float32
3025                                    | InterfaceType::Float64
3026                            )
3027                        ),
3028                        format!("{}", matches!(iface_ty, InterfaceType::Borrow(_))),
3029                        format!(
3030                            "{}",
3031                            matches!(
3032                                iface_ty,
3033                                InterfaceType::Stream(_) | InterfaceType::Future(_)
3034                            )
3035                        ),
3036                        size32.to_string(),
3037                        align32.to_string(),
3038                        flat_count.unwrap_or(0).to_string(),
3039                    ),
3040                    None => (
3041                        "null".into(),
3042                        "() => {{ throw new Error('no lift fn'); }}",
3043                        "() => {{ throw new Error('no lower fn'); }}",
3044                        "true",
3045                        "false".into(),
3046                        "false".into(),
3047                        "false".into(),
3048                        "null".into(),
3049                        "null".into(),
3050                        "0".into(),
3051                    ),
3052                };
3053
3054                // Set task memory index and memory object
3055                let (component_idx_expr, get_realloc_fn_expr) =
3056                    if let Some(state) = &self.component_state {
3057                        let ComponentStateJsExprs {
3058                            component_idx,
3059                            get_realloc_fn,
3060                            ..
3061                        } = state.get_js_exprs();
3062                        (component_idx, get_realloc_fn)
3063                    } else {
3064                        ("-1".into(), "undefined".into())
3065                    };
3066
3067                let tmp = self.tmp();
3068                let lowered_stream_waitable_idx = format!("streamWaitableIdx{tmp}");
3069                uwriteln!(
3070                    self.src,
3071                    r#"
3072                        if (!({async_iterator_symbol} in {stream_arg})
3073                            && !({iterator_symbol} in {stream_arg})
3074                            && !({stream_arg} instanceof {external_readable_stream_class})) {{
3075                            {debug_log_fn}('[Instruction::StreamLower] object with no supported stream protocol', {{ {stream_arg} }});
3076                            throw new Error('unrecognized stream object (no supported stream protocol)');
3077                        }}
3078
3079                        const cstate{tmp} = {get_or_create_async_state_fn}({component_idx_expr});
3080                        if (!cstate{tmp}) {{ throw new Error(`missing component state for component [{component_idx_expr}]`); }}
3081
3082                        const {{ writeEnd: hostWriteEnd{tmp}, readEnd: readEnd{tmp} }} = cstate{tmp}.createStream({{
3083                            tableIdx: {stream_table_idx},
3084                            elemMeta: {{
3085                                liftFn: {lift_fn_js},
3086                                lowerFn: {lower_fn_js},
3087                                payloadTypeName: {payload_type_name_js},
3088                                isNone: {payload_is_none},
3089                                isNumeric: {payload_is_numeric},
3090                                isBorrowed: {payload_is_borrow},
3091                                isAsyncValue: {payload_is_async_value},
3092                                flatCount: {payload_flat_count_js},
3093                                align32: {payload_align32_js},
3094                                size32: {payload_size32_js},
3095                                // TODO(feat): facilitate non utf8 string encoding for lowered streams
3096                                stringEncoding: 'utf8',
3097                                getReallocFn: {get_realloc_fn_expr},
3098                            }},
3099                        }});
3100
3101                        let readFn{tmp};
3102                        if ({async_iterator_symbol} in {stream_arg}) {{
3103                            let asyncIterator = {stream_arg}[{async_iterator_symbol}]();
3104                            readFn{tmp} = () => asyncIterator.next();
3105                            readFn{tmp}.drop = (reason) => asyncIterator.return?.(reason) ?? {stream_arg}[{symbol_dispose}]?.();
3106                        }} else if ({iterator_symbol} in {stream_arg}) {{
3107                            let iterator = {stream_arg}[{iterator_symbol}]();
3108                            readFn{tmp} = async () => iterator.next();
3109                            readFn{tmp}.drop = (reason) => iterator.return?.(reason) ?? {stream_arg}[{symbol_dispose}]?.();
3110                        }} else if ({stream_arg} instanceof {external_readable_stream_class}) {{
3111                            // At this point we're dealing with a readable stream that *somehow *does not*
3112                            // implement the async iterator protocol.
3113                            const lockedReader = {stream_arg}.getReader();
3114                            readFn{tmp} = () => lockedReader.read();
3115                            readFn{tmp}.drop = (reason) => lockedReader.cancel(reason).finally(() => lockedReader.releaseLock());
3116                        }}
3117
3118                        const hostInjectFn = {gen_stream_host_inject_fn}({{
3119                            readFn: readFn{tmp},
3120                            hostWriteEnd: hostWriteEnd{tmp},
3121                            readEnd: readEnd{tmp},
3122                        }});
3123                        readEnd{tmp}.setHostInjectFn(hostInjectFn);
3124                        readEnd{tmp}.setHostDropFn(readFn{tmp}.drop);
3125
3126                        const {lowered_stream_waitable_idx} = readEnd{tmp}.waitableIdx();
3127                    "#
3128                );
3129
3130                results.push(lowered_stream_waitable_idx);
3131            }
3132
3133            Instruction::StreamLift { payload, ty } => {
3134                let stream_new_from_lift_fn = self.intrinsic(Intrinsic::AsyncStream(
3135                    AsyncStreamIntrinsic::StreamNewFromLift,
3136                ));
3137
3138                // We must look up the type idx to find the stream
3139                let type_id = &crate::dealias(self.resolve, *ty);
3140                let ResourceTable {
3141                    imported: true,
3142                    data:
3143                        ResourceData::Guest {
3144                            extra:
3145                                Some(ResourceExtraData::Stream {
3146                                    table_idx: stream_table_idx_ty,
3147                                    elem_ty: stream_element_ty,
3148                                }),
3149                            ..
3150                        },
3151                } = self
3152                    .resource_map
3153                    .get(type_id)
3154                    .expect("missing resource mapping for stream lift")
3155                else {
3156                    unreachable!("invalid resource table observed during stream lift");
3157                };
3158
3159                // if a stream element is present, it should match the payload we're getting
3160                let (lift_fn_js, lower_fn_js) = match stream_element_ty {
3161                    Some(PayloadTypeMetadata {
3162                        ty,
3163                        lift_js_expr,
3164                        lower_js_expr,
3165                        ..
3166                    }) => {
3167                        assert_eq!(Some(*ty), **payload, "stream element type mismatch");
3168                        (lift_js_expr.to_string(), lower_js_expr.to_string())
3169                    }
3170                    None => (
3171                        "() => {{ throw new Error('no lift fn'); }}".into(),
3172                        "() => {{ throw new Error('no lower fn'); }}".into(),
3173                    ),
3174                };
3175                if let Some(PayloadTypeMetadata { ty, .. }) = stream_element_ty {
3176                    assert_eq!(Some(*ty), **payload, "stream element type mismatch");
3177                }
3178
3179                let tmp = self.tmp();
3180                let result_var = format!("streamResult{tmp}");
3181
3182                // Optionally preform the lift for the stream in question
3183                match (self.is_async, self.for_import.unwrap_or_default()) {
3184                    // It is possible for lifting to be called both at the *start* and *end* of
3185                    // a given function depending on how it called:
3186                    //
3187                    // 1. lifting results to convert a component-produced result for use by the host *after* `CallWasm` returns
3188                    // 2. lifting parameters (`stream` -> `AsyncIterator`), for use by the host, *before* `CallInterface`
3189                    //
3190                    // In (1), the function being generated must correspond to an export (from a component), and we only
3191                    // perform the lifting if we know the value is imminently ready (i.e. the sync case).
3192                    //
3193                    // In (2) the function must correspond to an import (from the host), and regardless of whether
3194                    // the function being generated is async or not, the host *must* deal in terms of lifted values
3195                    // (i.e. `AsyncIterator`, not index to a stream)
3196                    //
3197                    (_is_async @ false, _for_import @ false) | (_is_async, _for_import @ true) => {
3198                        let arg_stream_end_idx = operands
3199                            .first()
3200                            .expect("unexpectedly missing stream end return arg in StreamLift");
3201
3202                        let (payload_ty_size32_js, payload_ty_align32_js) =
3203                            if let Some(payload_ty) = payload {
3204                                (
3205                                    self.sizes.size(payload_ty).size_wasm32().to_string(),
3206                                    self.sizes.align(payload_ty).align_wasm32().to_string(),
3207                                )
3208                            } else {
3209                                ("null".into(), "null".into())
3210                            };
3211
3212                        let stream_table_idx = stream_table_idx_ty.as_u32();
3213
3214                        // Set task memory index and memory object
3215                        let component_idx_expr = if let Some(state) = &self.component_state {
3216                            let ComponentStateJsExprs { component_idx, .. } = state.get_js_exprs();
3217                            component_idx
3218                        } else {
3219                            "-1".into()
3220                        };
3221
3222                        uwriteln!(
3223                            self.src,
3224                            r#"
3225                              const {result_var} = {stream_new_from_lift_fn}({{
3226                                  componentIdx: {component_idx_expr},
3227                                  streamTableIdx: {stream_table_idx},
3228                                  streamEndWaitableIdx: {arg_stream_end_idx},
3229                                  payloadLiftFn: {lift_fn_js},
3230                                  payloadLowerFn: {lower_fn_js},
3231                                  payloadTypeSize32: {payload_ty_size32_js},
3232                                  payloadTypeAlign32: {payload_ty_align32_js},
3233                              }});
3234                            "#,
3235                        );
3236                    }
3237
3238                    // For other cases, we can do nothing as the future idx passes right through
3239                    _ => {}
3240                };
3241
3242                // TODO(fix): in the async case we return an uninitialized var, which should not be necessary
3243                results.push(result_var.clone());
3244            }
3245
3246            // Instruction::AsyncTaskReturn does *not* correspond to an canonical `task.return`,
3247            // but rather to a "return"/exit from an a lifted async function (e.g. pre-callback)
3248            //
3249            // To modify behavior of the `task.return` intrinsic, see:
3250            //   - `Trampoline::TaskReturn`
3251            //   - `AsyncTaskIntrinsic::TaskReturn`
3252            //
3253            // This is simply the end of the async function definition (e.g. `CallWasm`) that has been
3254            // lifted, which contains information about the async state.
3255            //
3256            // For an async function 'some-func', this instruction is triggered w/ the following `name`s:
3257            // - '[task-return]some-func'
3258            //
3259            // At this point in code generation, the following things have already been set:
3260            // - `parentTask`: A parent task, if one was executing before
3261            // - `subtask`: A subtask, if the current task is a subtask of a parent task
3262            // - `task`: the currently executing task
3263            // - `ret`: the original function return value, via (i.e. via `CallWasm`/`CallInterface`)
3264            // - `hostProvided`: whether the original function was a host-provided (i.e. host provided import)
3265            //
3266            Instruction::AsyncTaskReturn { name, params } => {
3267                let debug_log_fn = self.intrinsic(Intrinsic::DebugLog);
3268                let is_async_js = self.requires_async_porcelain | self.is_async;
3269                let async_driver_loop_fn =
3270                    self.intrinsic(Intrinsic::AsyncTask(AsyncTaskIntrinsic::DriverLoop));
3271                let get_or_create_async_state_fn = self.intrinsic(Intrinsic::Component(
3272                    ComponentIntrinsic::GetOrCreateAsyncState,
3273                ));
3274
3275                // Set task memory index and memory object
3276                let component_idx_expr = if let Some(state) = &self.component_state {
3277                    let ComponentStateJsExprs { component_idx, .. } = state.get_js_exprs();
3278                    component_idx
3279                } else {
3280                    "-1".into()
3281                };
3282
3283                uwriteln!(
3284                    self.src,
3285                    "{debug_log_fn}('{prefix}  [Instruction::AsyncTaskReturn]', {{
3286                         funcName: '{name}',
3287                         paramCount: {param_count},
3288                         componentIdx: {component_idx_expr},
3289                         postReturn: {post_return_present},
3290                         hostProvided,
3291                      }});",
3292                    param_count = params.len(),
3293                    post_return_present = self.post_return.is_some(),
3294                    prefix = self.tracing_prefix,
3295                );
3296
3297                assert!(
3298                    self.is_async,
3299                    "non-async functions should not be performing async returns (func {name})",
3300                );
3301
3302                // If we're dealing with an async call, then `ret` is actually the
3303                // state of async behavior.
3304                //
3305                // The result *should* be a Promise that resolves to whatever the current task
3306                // will eventually resolve to.
3307                //
3308                // NOTE: Regardless of whether async porcelain is required here, we want to return the result
3309                // of the computation as a whole, not the current async state (which is what `ret` currently is).
3310                //
3311                // `ret` is only a Promise if we have async-lowered the function in question (e.g. via JSPI)
3312                //
3313                // ```ts
3314                // type ret = number | Promise<number>;
3315                // ```ts
3316                //
3317                // If the import was host provided we *already* have the result via
3318                // JSPI and simply calling the host provided JS function -- there is no need
3319                // to drive the async loop as with an async import that came from a component.
3320                //
3321                // If a subtask is defined, then we're in the case of a lowered async import,
3322                // which means that the first async call (to the callee fn) has occurred,
3323                // and a subtask has been created, but has not been triggered as started.
3324                //
3325                // NOTE: for host provided functions, we know that the resolution fo the
3326                // function itself are the lifted (component model -- i.e. a string not a pointer + len)
3327                // results. In those cases, we can simply return the result that was provided by the host.
3328                //
3329                // Alternatively, if we have entered an async return, and are part of a subtask
3330                // then we should start it, given that the task we have recently created (however we got to
3331                // the async return) is going to continue to be polled soon (via the driver loop).
3332                //
3333                uwriteln!(
3334                    self.src,
3335                    r#"
3336                      if (hostProvided) {{
3337                          {debug_log_fn}('[Instruction::AsyncTaskReturn] signaling host-provided async return completion', {{
3338                              task: task.id(),
3339                              subtask: subtask?.id(),
3340                              result: ret,
3341                          }})
3342                          task.resolve([ret]);
3343                          task.exit();
3344                          return task.completionPromise();
3345                      }}
3346
3347                      const componentState = {get_or_create_async_state_fn}({component_idx_expr});
3348                      if (!componentState) {{ throw new Error('failed to lookup current component state'); }}
3349
3350                      queueMicrotask(async (resolve, reject) => {{
3351                          try {{
3352                              {debug_log_fn}("[Instruction::AsyncTaskReturn] starting driver loop", {{
3353                                  fnName: '{name}',
3354                                  componentInstanceIdx: {component_idx_expr},
3355                                  taskID: task.id(),
3356                              }});
3357                              await {async_driver_loop_fn}({{
3358                                  componentInstanceIdx: {component_idx_expr},
3359                                  componentState,
3360                                  task,
3361                                  fnName: '{name}',
3362                                  isAsync: {is_async_js},
3363                                  callbackResult: ret,
3364                              }});
3365                          }} catch (err) {{
3366                              {debug_log_fn}("[Instruction::AsyncTaskReturn] driver loop call failure", {{ err }});
3367                          }}
3368                      }});
3369
3370                      let taskRes = await task.completionPromise();
3371                      if (task.getErrHandling() === 'throw-result-err') {{
3372                          if (typeof taskRes !== 'object') {{ return taskRes; }}
3373                          if (taskRes.tag === 'err') {{ throw taskRes.val; }}
3374                          if (taskRes.tag === 'ok') {{ taskRes = taskRes.val; }}
3375                      }}
3376
3377                      return taskRes;
3378                      "#,
3379                );
3380            }
3381
3382            Instruction::GuestDeallocate { .. }
3383            | Instruction::GuestDeallocateString
3384            | Instruction::GuestDeallocateList { .. }
3385            | Instruction::GuestDeallocateVariant { .. } => unimplemented!("Guest deallocation"),
3386
3387            Instruction::MapLower { .. }
3388            | Instruction::MapLift { .. }
3389            | Instruction::IterMapKey { .. }
3390            | Instruction::IterMapValue { .. }
3391            | Instruction::GuestDeallocateMap { .. } => unimplemented!("map support"),
3392        }
3393    }
3394}
3395
3396/// Tests whether `ty` can be represented with `null`, and if it can then
3397/// the "other type" is returned. If `Some` is returned that means that `ty`
3398/// is `null | <return>`. If `None` is returned that means that `null` can't
3399/// be used to represent `ty`.
3400pub fn as_nullable<'a>(resolve: &'a Resolve, ty: &'a Type) -> Option<&'a Type> {
3401    let id = match ty {
3402        Type::Id(id) => *id,
3403        _ => return None,
3404    };
3405    match &resolve.types[id].kind {
3406        // If `ty` points to an `option<T>`, then `ty` can be represented
3407        // with `null` if `t` itself can't be represented with null. For
3408        // example `option<option<u32>>` can't be represented with `null`
3409        // since that's ambiguous if it's `none` or `some(none)`.
3410        //
3411        // Note, oddly enough, that `option<option<option<u32>>>` can be
3412        // represented as `null` since:
3413        //
3414        // * `null` => `none`
3415        // * `{ tag: "none" }` => `some(none)`
3416        // * `{ tag: "some", val: null }` => `some(some(none))`
3417        // * `{ tag: "some", val: 1 }` => `some(some(some(1)))`
3418        //
3419        // It's doubtful anyone would actually rely on that though due to
3420        // how confusing it is.
3421        TypeDefKind::Option(t) => {
3422            if !maybe_null(resolve, t) {
3423                Some(t)
3424            } else {
3425                None
3426            }
3427        }
3428        TypeDefKind::Type(t) => as_nullable(resolve, t),
3429        _ => None,
3430    }
3431}
3432
3433pub fn maybe_null(resolve: &Resolve, ty: &Type) -> bool {
3434    as_nullable(resolve, ty).is_some()
3435}
3436
3437/// Retrieve the specialized JS array type that would contain a given element type,
3438/// if one exists.
3439///
3440/// e.g. a Wasm [`Type::U8`] would be represetned by a JS `Uint8Array`
3441///
3442/// # Arguments
3443///
3444/// * `resolve` - The [`Resolve`] used to look up nested type IDs if necessary
3445/// * `element_ty` - The [`Type`] that represents elements of the array
3446pub fn js_array_ty(resolve: &Resolve, element_ty: &Type) -> Option<&'static str> {
3447    match element_ty {
3448        Type::Bool => None,
3449        Type::U8 => Some("Uint8Array"),
3450        Type::S8 => Some("Int8Array"),
3451        Type::U16 => Some("Uint16Array"),
3452        Type::S16 => Some("Int16Array"),
3453        Type::U32 => Some("Uint32Array"),
3454        Type::S32 => Some("Int32Array"),
3455        Type::U64 => Some("BigUint64Array"),
3456        Type::S64 => Some("BigInt64Array"),
3457        Type::F32 => Some("Float32Array"),
3458        Type::F64 => Some("Float64Array"),
3459        Type::Char => None,
3460        Type::String => None,
3461        Type::ErrorContext => None,
3462        Type::Id(id) => match &resolve.types[*id].kind {
3463            // Recur to resolve type aliases, etc.
3464            TypeDefKind::Type(t) => js_array_ty(resolve, t),
3465            _ => None,
3466        },
3467    }
3468}
3469
3470/// Generate the JS `DataView` set and numeric checks for a given numeric type
3471///
3472/// # Arguments
3473///
3474/// * `ty` - the [`Type`] to check
3475///
3476fn gen_dataview_set_and_check_fn_js_for_numeric_type(
3477    resolve: &Resolve,
3478    ty: &Type,
3479) -> (&'static str, String) {
3480    let check_fn = Intrinsic::Conversion(ConversionIntrinsic::RequireValidNumericPrimitive).name();
3481    match ty {
3482        // Unsigned Integers
3483        Type::Bool => ("setUint8", format!("{check_fn}.bind(null, 'u8')",)),
3484        Type::U8 => ("setUint8", format!("{check_fn}.bind(null, 'u8')",)),
3485        Type::U16 => ("setUint16", format!("{check_fn}.bind(null, 'u16')",)),
3486        Type::U32 => ("setUint32", format!("{check_fn}.bind(null, 'u32')",)),
3487        Type::U64 => ("setBigUint64", format!("{check_fn}.bind(null, 'u64')",)),
3488        // Signed integers
3489        Type::S8 => ("setInt8", format!("{check_fn}.bind(null, 's8')",)),
3490        Type::S16 => ("setInt16", format!("{check_fn}.bind(null, 's16')",)),
3491        Type::S32 => ("setInt32", format!("{check_fn}.bind(null, 's32')",)),
3492        Type::S64 => ("setBigInt64", format!("{check_fn}.bind(null, 's64')",)),
3493        // Floating point
3494        Type::F32 => ("setFloat32", format!("{check_fn}.bind(null, 'f32')",)),
3495        Type::F64 => ("setFloat64", format!("{check_fn}.bind(null, 'f64')",)),
3496        Type::Id(id) => match resolve.types.get(*id) {
3497            // Type aliases should resolve to types that have the kind `TypeDefKind::Type`
3498            Some(TypeDef {
3499                kind: TypeDefKind::Type(inner_ty),
3500                ..
3501            }) => gen_dataview_set_and_check_fn_js_for_numeric_type(resolve, inner_ty),
3502            // We do not expect to resolve to types that *do not* have the `TypeDefKind::Type(...)`
3503            Some(inner_ty) => {
3504                unreachable!(
3505                    "unexpected non-type-kind typedef [{inner_ty:?}] (as type {ty:?}) for canonical list lower [{ty:?}]",
3506                )
3507            }
3508            // All type ids should resolve via the passed in `Resolve`
3509            None => unreachable!("missing/unresolvable type [{ty:?}]"),
3510        },
3511        _ => unreachable!("unsupported type [{ty:?}] for canonical list lower"),
3512    }
3513}
3514
3515#[cfg(test)]
3516mod tests {
3517    use super::*;
3518
3519    #[test]
3520    fn test_alias_type_gen_dataview_set_and_check_fn_js_for_numeric_type() {
3521        let mut resolve = Resolve::new();
3522
3523        let owner = wit_parser::TypeOwner::Interface(resolve.interfaces.next_id());
3524
3525        let ty_id = resolve.types.alloc(wit_parser::TypeDef {
3526            name: None,
3527            kind: TypeDefKind::Type(Type::U64),
3528            docs: Default::default(),
3529            stability: Default::default(),
3530            owner,
3531            span: Default::default(),
3532        });
3533
3534        let ty_ = Type::Id(ty_id);
3535
3536        let (dataview_set_method, check_fn_intrinsic) =
3537            gen_dataview_set_and_check_fn_js_for_numeric_type(&resolve, &ty_);
3538
3539        assert_eq!(dataview_set_method, "setBigUint64");
3540
3541        let check_fn =
3542            Intrinsic::Conversion(ConversionIntrinsic::RequireValidNumericPrimitive).name();
3543
3544        assert_eq!(check_fn_intrinsic, format!("{check_fn}.bind(null, 'u64')",));
3545    }
3546}