golem_wasm/analysis/
mod.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod model;
16pub use model::*;
17
18/// Protobuf representation of analysis results
19#[cfg(feature = "host")]
20pub mod protobuf;
21
22/// Wave format support for types.
23///
24/// This module is optional and can be enabled with the `metadata` feature flag. It is enabled by default.
25#[cfg(feature = "host")]
26pub mod wave;
27
28#[cfg(feature = "host")]
29pub mod wit_parser;
30
31pub type AnalysisResult<A> = Result<A, AnalysisFailure>;
32
33#[cfg(test)]
34mod tests {
35    use crate::analysis::analysed_type::{f32, field, handle, record, result, str, u32, u64};
36    use crate::analysis::{
37        AnalysedFunction, AnalysedFunctionParameter, AnalysedFunctionResult, AnalysedResourceId,
38        AnalysedResourceMode,
39    };
40    use test_r::test;
41
42    #[test]
43    fn analysed_function_kind() {
44        let cons = AnalysedFunction {
45            name: "[constructor]cart".to_string(),
46            parameters: vec![AnalysedFunctionParameter {
47                name: "user-id".to_string(),
48                typ: str(),
49            }],
50            result: Some(AnalysedFunctionResult {
51                typ: handle(AnalysedResourceId(0), AnalysedResourceMode::Owned),
52            }),
53        };
54        let method = AnalysedFunction {
55            name: "[method]cart.add-item".to_string(),
56            parameters: vec![
57                AnalysedFunctionParameter {
58                    name: "self".to_string(),
59                    typ: handle(AnalysedResourceId(0), AnalysedResourceMode::Borrowed),
60                },
61                AnalysedFunctionParameter {
62                    name: "item".to_string(),
63                    typ: record(vec![
64                        field("product-id", str()),
65                        field("name", str()),
66                        field("price", f32()),
67                        field("quantity", u32()),
68                    ]),
69                },
70            ],
71            result: None,
72        };
73        let static_method = AnalysedFunction {
74            name: "[static]cart.merge".to_string(),
75            parameters: vec![
76                AnalysedFunctionParameter {
77                    name: "self".to_string(),
78                    typ: handle(AnalysedResourceId(0), AnalysedResourceMode::Borrowed),
79                },
80                AnalysedFunctionParameter {
81                    name: "that".to_string(),
82                    typ: handle(AnalysedResourceId(0), AnalysedResourceMode::Borrowed),
83                },
84            ],
85            result: Some(AnalysedFunctionResult {
86                typ: handle(AnalysedResourceId(0), AnalysedResourceMode::Owned),
87            }),
88        };
89        let fun = AnalysedFunction {
90            name: "hash".to_string(),
91            parameters: vec![AnalysedFunctionParameter {
92                name: "path".to_string(),
93                typ: str(),
94            }],
95            result: Some(AnalysedFunctionResult {
96                typ: result(
97                    record(vec![field("lower", u64()), field("upper", u64())]),
98                    str(),
99                ),
100            }),
101        };
102
103        assert!(cons.is_constructor());
104        assert!(method.is_method());
105        assert!(static_method.is_static_method());
106        assert!(!fun.is_constructor());
107        assert!(!fun.is_method());
108        assert!(!fun.is_static_method());
109    }
110}