File: rmake.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (60 lines) | stat: -rw-r--r-- 1,736 bytes parent folder | download
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
extern crate run_make_support;

use run_make_support::{out_dir, rustc, wasmparser};
use std::collections::HashMap;
use std::path::Path;
use wasmparser::ExternalKind::*;

fn main() {
    if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
        return;
    }

    test(&[]);
    test(&["-O"]);
    test(&["-Clto"]);
}

fn test(args: &[&str]) {
    eprintln!("running with {args:?}");
    rustc().arg("bar.rs").arg("--target=wasm32-wasip1").args(args).run();
    rustc().arg("foo.rs").arg("--target=wasm32-wasip1").args(args).run();
    rustc().arg("main.rs").arg("--target=wasm32-wasip1").args(args).run();

    verify_exports(
        &out_dir().join("foo.wasm"),
        &[("foo", Func), ("FOO", Global), ("memory", Memory)],
    );
    verify_exports(
        &out_dir().join("main.wasm"),
        &[
            ("foo", Func),
            ("FOO", Global),
            ("_start", Func),
            ("__main_void", Func),
            ("memory", Memory),
        ],
    );
}

fn verify_exports(path: &Path, exports: &[(&str, wasmparser::ExternalKind)]) {
    println!("verify {path:?}");
    let file = std::fs::read(path).unwrap();
    let mut wasm_exports = HashMap::new();
    for payload in wasmparser::Parser::new(0).parse_all(&file) {
        let payload = payload.unwrap();
        if let wasmparser::Payload::ExportSection(s) = payload {
            for export in s {
                let export = export.unwrap();
                wasm_exports.insert(export.name, export.kind);
            }
        }
    }

    eprintln!("found exports {wasm_exports:?}");

    assert_eq!(exports.len(), wasm_exports.len());
    for (export, expected_kind) in exports {
        assert_eq!(wasm_exports[export], *expected_kind);
    }
}