|
| 1 | +use genco::{prelude::*, tokens::Tokens}; |
| 2 | +use wit_bindgen_core::wit_parser::{Record, Resolve, Type, TypeDef, TypeDefKind}; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + codegen::wasm::{Wasm, WasmData}, |
| 6 | + go::*, |
| 7 | + resolve_type, |
| 8 | +}; |
| 9 | + |
| 10 | +/// The WIT bindings for a world. |
| 11 | +pub struct Bindings { |
| 12 | + /// The cumulative output tokens containing the Go bindings. |
| 13 | + // TODO(#16): Don't use the internal bindings.out field |
| 14 | + pub out: Tokens<Go>, |
| 15 | + |
| 16 | + /// The identifier of the Go variable containing the WebAssembly bytes. |
| 17 | + raw_wasm_var: GoIdentifier, |
| 18 | +} |
| 19 | + |
| 20 | +impl Bindings { |
| 21 | + /// Creates a new bindings generator for the selected world. |
| 22 | + pub fn new(world: &str) -> Self { |
| 23 | + let wasm_var = GoIdentifier::private(format!("wasm-file-{world}")); |
| 24 | + Self { |
| 25 | + // world, |
| 26 | + out: Tokens::new(), |
| 27 | + raw_wasm_var: wasm_var, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Adds the given Wasm to the bindings. |
| 32 | + pub fn include_wasm(&mut self, wasm: WasmData) { |
| 33 | + Wasm::new(&self.raw_wasm_var, wasm).format_into(&mut self.out) |
| 34 | + } |
| 35 | + |
| 36 | + pub fn define_type(&mut self, typ_def: &TypeDef, resolve: &Resolve) { |
| 37 | + let TypeDef { name, kind, .. } = typ_def; |
| 38 | + match kind { |
| 39 | + TypeDefKind::Record(Record { fields }) => { |
| 40 | + let name = GoIdentifier::public(name.as_deref().expect("record to have a name")); |
| 41 | + let fields = fields.iter().map(|field| { |
| 42 | + ( |
| 43 | + GoIdentifier::public(&field.name), |
| 44 | + resolve_type(&field.ty, resolve), |
| 45 | + ) |
| 46 | + }); |
| 47 | + |
| 48 | + quote_in! { self.out => |
| 49 | + $['\n'] |
| 50 | + type $name struct { |
| 51 | + $(for (name, typ) in fields join ($['\r']) => $name $typ) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + TypeDefKind::Resource => todo!("TODO(#5): implement resources"), |
| 56 | + TypeDefKind::Handle(_) => todo!("TODO(#5): implement resources"), |
| 57 | + TypeDefKind::Flags(_) => todo!("TODO(#4):generate flags type definition"), |
| 58 | + TypeDefKind::Tuple(_) => todo!("TODO(#4):generate tuple type definition"), |
| 59 | + TypeDefKind::Variant(_) => { |
| 60 | + // TODO(#4): Generate aliases if the variant name doesn't match the struct name |
| 61 | + } |
| 62 | + TypeDefKind::Enum(inner) => { |
| 63 | + let name = name.clone().expect("enum to have a name"); |
| 64 | + let enum_type = &GoIdentifier::private(&name); |
| 65 | + |
| 66 | + let enum_interface = GoIdentifier::public(&name); |
| 67 | + |
| 68 | + let enum_function = &GoIdentifier::private(format!("is-{}", &name)); |
| 69 | + |
| 70 | + let variants = inner |
| 71 | + .cases |
| 72 | + .iter() |
| 73 | + .map(|variant| GoIdentifier::public(&variant.name)); |
| 74 | + |
| 75 | + quote_in! { self.out => |
| 76 | + $['\n'] |
| 77 | + type $enum_interface interface { |
| 78 | + $enum_function() |
| 79 | + } |
| 80 | + |
| 81 | + type $enum_type int |
| 82 | + |
| 83 | + func ($enum_type) $enum_function() {} |
| 84 | + |
| 85 | + const ( |
| 86 | + $(for name in variants join ($['\r']) => $name $enum_type = iota) |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + TypeDefKind::Option(_) => todo!("TODO(#4): generate option type definition"), |
| 91 | + TypeDefKind::Result(_) => todo!("TODO(#4): generate result type definition"), |
| 92 | + TypeDefKind::List(_) => todo!("TODO(#4): generate list type definition"), |
| 93 | + TypeDefKind::Future(_) => todo!("TODO(#4): generate future type definition"), |
| 94 | + TypeDefKind::Stream(_) => todo!("TODO(#4): generate stream type definition"), |
| 95 | + TypeDefKind::Type(Type::Id(_)) => { |
| 96 | + // TODO(#4): Only skip this if we have already generated the type |
| 97 | + } |
| 98 | + TypeDefKind::Type(Type::Bool) => todo!("TODO(#4): generate bool type alias"), |
| 99 | + TypeDefKind::Type(Type::U8) => todo!("TODO(#4): generate u8 type alias"), |
| 100 | + TypeDefKind::Type(Type::U16) => todo!("TODO(#4): generate u16 type alias"), |
| 101 | + TypeDefKind::Type(Type::U32) => todo!("TODO(#4): generate u32 type alias"), |
| 102 | + TypeDefKind::Type(Type::U64) => todo!("TODO(#4): generate u64 type alias"), |
| 103 | + TypeDefKind::Type(Type::S8) => todo!("TODO(#4): generate s8 type alias"), |
| 104 | + TypeDefKind::Type(Type::S16) => todo!("TODO(#4): generate s16 type alias"), |
| 105 | + TypeDefKind::Type(Type::S32) => todo!("TODO(#4): generate s32 type alias"), |
| 106 | + TypeDefKind::Type(Type::S64) => todo!("TODO(#4): generate s64 type alias"), |
| 107 | + TypeDefKind::Type(Type::F32) => todo!("TODO(#4): generate f32 type alias"), |
| 108 | + TypeDefKind::Type(Type::F64) => todo!("TODO(#4): generate f64 type alias"), |
| 109 | + TypeDefKind::Type(Type::Char) => todo!("TODO(#4): generate char type alias"), |
| 110 | + TypeDefKind::Type(Type::String) => { |
| 111 | + let name = |
| 112 | + GoIdentifier::public(name.as_deref().expect("string alias to have a name")); |
| 113 | + // TODO(#4): We might want a Type Definition (newtype) instead of Type Alias here |
| 114 | + quote_in! { self.out => |
| 115 | + $['\n'] |
| 116 | + type $name = string |
| 117 | + } |
| 118 | + } |
| 119 | + TypeDefKind::Type(Type::ErrorContext) => { |
| 120 | + todo!("TODO(#4): generate error context definition") |
| 121 | + } |
| 122 | + TypeDefKind::FixedSizeList(_, _) => { |
| 123 | + todo!("TODO(#4): generate fixed size list definition") |
| 124 | + } |
| 125 | + TypeDefKind::Unknown => panic!("cannot generate Unknown type"), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments