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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! Support for compiling scripts and modules in tests.

use compiler::Compiler;

use diem_types::transaction::{Module, Script};
use move_binary_format::CompiledModule;

/// Compile the provided Move code into a blob which can be used as the code to be published
/// (a Module).
pub fn compile_module(file_name: &str, code: &str) -> (CompiledModule, Module) {
    let compiled_module = Compiler {
        deps: diem_framework_releases::current_modules().iter().collect(),
    }
    .into_compiled_module(file_name, code)
    .expect("Module compilation failed");
    let module = Module::new(
        Compiler {
            deps: diem_framework_releases::current_modules().iter().collect(),
        }
        .into_module_blob(file_name, code)
        .expect("Module compilation failed"),
    );
    (compiled_module, module)
}

/// Compile the provided Move code into a blob which can be used as the code to be executed
/// (a Script).
pub fn compile_script(file_name: &str, code: &str, extra_deps: Vec<CompiledModule>) -> Script {
    let compiler = Compiler {
        deps: diem_framework_releases::current_modules()
            .iter()
            .chain(extra_deps.iter())
            .collect(),
    };
    Script::new(
        compiler
            .into_script_blob(file_name, code)
            .expect("Script compilation failed"),
        vec![],
        vec![],
    )
}