This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 685
/
debug.rs
117 lines (102 loc) · 3.62 KB
/
debug.rs
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
// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0
use crate::natives::helpers::make_module_natives;
use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_algebra::InternalGas;
use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
#[allow(unused_imports)]
use move_vm_types::values::{values_impl::debug::print_reference, Reference};
#[allow(unused_imports)]
use move_vm_types::{
loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value,
};
use smallvec::smallvec;
use std::{collections::VecDeque, sync::Arc};
/***************************************************************************************************
* native fun print
*
* gas cost: base_cost
**************************************************************************************************/
#[derive(Debug, Clone)]
pub struct PrintGasParameters {
pub base_cost: InternalGas,
}
#[inline]
fn native_print(
gas_params: &PrintGasParameters,
_context: &mut NativeContext,
mut ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(args.len() == 1);
// No-op if the feature flag is not present.
#[cfg(feature = "testing")]
{
let _ty = ty_args.pop().unwrap();
let r = pop_arg!(args, Reference);
let mut buf = String::new();
print_reference(&mut buf, &r)?;
println!("[debug] {}", buf);
}
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
}
pub fn make_native_print(gas_params: PrintGasParameters) -> NativeFunction {
Arc::new(
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print(&gas_params, context, ty_args, args)
},
)
}
/***************************************************************************************************
* native fun print_stack_trace
*
* gas cost: base_cost
**************************************************************************************************/
#[derive(Debug, Clone)]
pub struct PrintStackTraceGasParameters {
pub base_cost: InternalGas,
}
#[inline]
fn native_print_stack_trace(
gas_params: &PrintStackTraceGasParameters,
context: &mut NativeContext,
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.is_empty());
#[cfg(feature = "testing")]
{
let mut s = String::new();
context.print_stack_trace(&mut s)?;
println!("{}", s);
}
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
}
pub fn make_native_print_stack_trace(gas_params: PrintStackTraceGasParameters) -> NativeFunction {
Arc::new(
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print_stack_trace(&gas_params, context, ty_args, args)
},
)
}
/***************************************************************************************************
* module
**************************************************************************************************/
#[derive(Debug, Clone)]
pub struct GasParameters {
pub print: PrintGasParameters,
pub print_stack_trace: PrintStackTraceGasParameters,
}
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [
("print", make_native_print(gas_params.print)),
(
"print_stack_trace",
make_native_print_stack_trace(gas_params.print_stack_trace),
),
];
make_module_natives(natives)
}