• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 6815798908

09 Nov 2023 05:17PM CUT coverage: 72.607% (-2.4%) from 75.01%
6815798908

push

github

web-flow
Implement `Any/BufferProvider` for some smart pointers (#4255)

Allows storing them as a `Box<dyn Any/BufferProvider>` without using a
wrapper type that implements the trait.

44281 of 60987 relevant lines covered (72.61%)

201375.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/components/collections/codepointtrie_builder/src/wasm.rs
1
// This file is part of ICU4X. For terms of use, please see the file
2
// called LICENSE at the top level of the ICU4X source tree
3
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4

5
use crate::CodePointTrieBuilder;
6
use crate::CodePointTrieBuilderData;
7
use icu_collections::codepointtrie::TrieType;
8
use icu_collections::codepointtrie::TrieValue;
9
use once_cell::sync::OnceCell;
10
use wasmer::{Instance, Module, Store};
11
use wasmer_wasi::{Pipe, WasiState};
12

13
const WASM_BYTES: &[u8] = include_bytes!("../list_to_ucptrie.wasm");
14

15
static STORE: OnceCell<Store> = OnceCell::new();
16
static MODULE: OnceCell<Module> = OnceCell::new();
17

18
pub(crate) fn run_wasm<T>(builder: &CodePointTrieBuilder<T>) -> String
5✔
19
where
20
    T: TrieValue + Into<u32>,
21
{
22
    // Set up the execution environment with a WasiState
23
    let args = &[
10✔
24
        format!("{}", builder.default_value.into()),
5✔
25
        format!("{}", builder.error_value.into()),
5✔
26
        match builder.trie_type {
10✔
27
            TrieType::Fast => "fast",
1✔
28
            TrieType::Small => "small",
4✔
29
        }
30
        .to_owned(),
31
        format!("{}", std::mem::size_of::<T::ULE>() * 8),
5✔
32
    ];
×
33
    let mut wasi_env = WasiState::new("list_to_ucptrie")
15✔
34
        .stdin(Box::new(Pipe::new()))
5✔
35
        .stdout(Box::new(Pipe::new()))
5✔
36
        .args(args)
5✔
37
        .finalize()
38
        .expect("valid arguments + in-memory filesystem");
5✔
39

40
    let module = MODULE.get_or_init(|| {
7✔
41
        let store = STORE.get_or_init(Store::default);
2✔
42
        Module::new(store, WASM_BYTES).expect("valid WASM")
2✔
43
    });
2✔
44

45
    // Create the WebAssembly instance with the module and the WasiState
46
    let import_object = wasi_env.import_object(module).expect("walid wasm file");
5✔
47
    let instance = Instance::new(module, &import_object).expect("valid wasm file");
5✔
48

49
    // To write to the stdin, we need a mutable reference to the pipe
50
    //
51
    // We access WasiState in a nested scope to ensure we're not holding
52
    // the mutex after we need it.
53
    {
54
        let mut state = wasi_env.state();
5✔
55
        let wasi_stdin = state
5✔
56
            .fs
57
            .stdin_mut()
58
            .expect("valid pipe")
59
            .as_mut()
60
            .expect("valid pipe");
61
        // Write each value to the pipe
62
        let CodePointTrieBuilderData::ValuesByCodePoint(values) = builder.data;
5✔
63
        writeln!(wasi_stdin, "{}", values.len()).expect("valid pipe");
5✔
64

65
        for value in values {
3,879,429✔
66
            let num: u32 = (*value).into();
3,879,424✔
67
            writeln!(wasi_stdin, "{num}").expect("valid pipe");
3,879,424✔
68
        }
69
    }
5✔
70

71
    // Call the `_start` function to run the tool
72
    let start = instance
5✔
73
        .exports
74
        .get_function("_start")
75
        .expect("function exists");
76
    let exit_result = start.call(&[]);
5✔
77

78
    if let Err(e) = exit_result {
5✔
79
        panic!("list_to_ucptrie failed in C++: args were: {args:?}: {e}");
×
80
    }
×
81

82
    // To read from the stdout/stderr, we again need a mutable reference to the pipe
83
    let mut state = wasi_env.state();
5✔
84
    let wasi_stdout = state
5✔
85
        .fs
86
        .stdout_mut()
87
        .expect("valid pipe")
88
        .as_mut()
89
        .expect("valid pipe");
90

91
    // The output is a TOML blob, which we can save in a string
92
    let mut buf = String::new();
5✔
93
    wasi_stdout
5✔
94
        .read_to_string(&mut buf)
95
        .expect("pipe contains valid utf-8");
96

97
    buf
98
}
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc