1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::*;
use wast::{
    component::{CoreItemRef, ItemRef},
    core::Local,
    kw,
};

impl<'a, 'i> IntoWasm<'a, Start<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> Start<'i> {
        Start { func: self.symbol.as_index(), args: vec![], results: vec![] }
    }
}

impl<'a, 'i> IntoWasm<'a, Func<'i>> for FunctionType
where
    'a: 'i,
{
    /// 怎么把核心函数提升到组件函数
    fn as_wast(&'a self) -> Func<'i> {
        Func {
            span: Span::from_offset(0),
            id: self.symbol.as_id(),
            name: None,
            exports: self.export.as_wast(),
            kind: self.as_wast(),
        }
    }
}

impl<'a, 'i> IntoWasm<'a, CoreFunc<'i>> for FunctionType
where
    'a: 'i,
{
    /// 怎么把组件函数降低为核心函数
    fn as_wast(&'a self) -> CoreFunc<'i> {
        CoreFunc { span: Span::from_offset(0), id: self.symbol.as_id(), name: None, kind: self.as_wast() }
    }
}

impl<'a, 'i> IntoWasm<'a, wast::core::Func<'i>> for FunctionType
where
    'a: 'i,
{
    /// 核心函数的实现
    fn as_wast(&'a self) -> wast::core::Func<'i> {
        wast::core::Func {
            span: Span::from_offset(0),
            id: self.symbol.as_id(),
            name: None,
            exports: self.export.as_wast(),
            kind: self.as_wast(),
            ty: TypeUse { index: None, inline: Some(self.as_wast()) },
        }
    }
}

impl<'a, 'i> IntoWasm<'a, FuncKind<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> FuncKind<'i> {
        FuncKind::Lift { ty: ComponentTypeUse::Inline(self.as_wast()), info: self.as_wast() }
    }
}
impl<'a, 'i> IntoWasm<'a, CoreFuncKind<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> CoreFuncKind<'i> {
        CoreFuncKind::Lower(CanonLower {
            func: ItemRef { kind: kw::func(Span::from_offset(0)), idx: self.symbol.as_index(), export_names: Vec::new() },
            opts: Vec::new(),
        })
    }
}
impl<'a, 'i> IntoWasm<'a, CanonLift<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> CanonLift<'i> {
        CanonLift {
            func: CoreItemRef { kind: kw::func(Span::from_offset(0)), idx: self.symbol.as_index(), export_name: None },
            opts: Vec::new(),
        }
    }
}
impl<'a, 'i> IntoWasm<'a, ComponentFunctionType<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> ComponentFunctionType<'i> {
        let params: Vec<_> = self.input.values().map(|v| v.as_wast()).collect();
        let result: Vec<_> = self.output.iter().map(|v| v.as_wast()).collect();
        ComponentFunctionType { params: Box::from(params), results: Box::from(result) }
    }
}
impl<'a, 'i> IntoWasm<'a, wast::core::FunctionType<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> wast::core::FunctionType<'i> {
        let params: Vec<_> = self.input.values().map(|v| v.as_wast()).collect();
        let result: Vec<_> = self.output.iter().map(|v| v.as_wast()).collect();
        wast::core::FunctionType { params: Box::from(params), results: Box::from(result) }
    }
}

impl<'a, 'i> IntoWasm<'a, ComponentFunctionParam<'i>> for ParameterType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> ComponentFunctionParam<'i> {
        ComponentFunctionParam { name: self.name.as_ref(), ty: self.type_hint.as_wast() }
    }
}

impl<'a, 'i> IntoWasm<'a, (Option<Id<'a>>, Option<NameAnnotation<'a>>, ValType<'a>)> for ParameterType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> (Option<Id<'a>>, Option<NameAnnotation<'a>>, ValType<'a>) {
        (self.name.as_id(), None, self.type_hint.as_wast())
    }
}

impl<'a, 'i> IntoWasm<'a, ComponentFunctionResult<'i>> for WasmType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> ComponentFunctionResult<'i> {
        ComponentFunctionResult { name: None, ty: self.as_wast() }
    }
}

impl<'a, 'i> IntoWasm<'a, wast::core::FuncKind<'i>> for FunctionType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> wast::core::FuncKind<'i> {
        let locals: Vec<_> = self.local.values().map(|p| p.as_wast()).collect();
        wast::core::FuncKind::Inline { locals: Box::from(locals), expression: self.body.as_wast() }
    }
}
impl<'a, 'i> IntoWasm<'a, Local<'i>> for ParameterType
where
    'a: 'i,
{
    fn as_wast(&'a self) -> Local<'i> {
        Local { id: self.name.as_id(), name: None, ty: self.type_hint.as_wast() }
    }
}

impl<'a, 'i> IntoWasm<'a, Expression<'i>> for FunctionBody
where
    'a: 'i,
{
    fn as_wast(&'a self) -> Expression<'i> {
        let mut bytecode = Vec::with_capacity(self.codes.len());
        for code in &self.codes {
            code.emit(&mut bytecode)
        }
        Expression { instrs: Box::from(bytecode) }
    }
}