Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Bring move-stdlib/src/natives/debug.rs on par with sui/debug.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksolana committed Mar 4, 2024
1 parent 5acd116 commit bef3552
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
72 changes: 59 additions & 13 deletions language/move-stdlib/src/natives/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ pub struct PrintGasParameters {
pub base_cost: InternalGas,
}

#[inline]
fn native_print_nop(
gas_params: &PrintGasParameters,
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(args.len() == 1);
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
}

#[inline]
fn native_print(
gas_params: &PrintGasParameters,
Expand Down Expand Up @@ -68,14 +79,23 @@ fn native_print(
}

pub fn make_native_print(
silent: bool,
gas_params: PrintGasParameters,
move_std_addr: AccountAddress,
) -> NativeFunction {
Arc::new(
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print(&gas_params, context, ty_args, args, move_std_addr)
},
)
if silent {
Arc::new(
move |_context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print_nop(&gas_params, ty_args, args)
},
)
} else {
Arc::new(
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print(&gas_params, context, ty_args, args, move_std_addr)
},
)
}
}

/***************************************************************************************************
Expand All @@ -88,6 +108,17 @@ pub struct PrintStackTraceGasParameters {
pub base_cost: InternalGas,
}

#[inline]
fn native_print_stack_trace_nop(
gas_params: &PrintStackTraceGasParameters,
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.is_empty());
Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
}

#[allow(unused_variables)]
#[inline]
fn native_print_stack_trace(
Expand All @@ -109,12 +140,23 @@ fn native_print_stack_trace(
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)
},
)
pub fn make_native_print_stack_trace(
silent: bool,
gas_params: PrintStackTraceGasParameters,
) -> NativeFunction {
if silent {
Arc::new(
move |_context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print_stack_trace_nop(&gas_params, ty_args, args)
},
)
} else {
Arc::new(
move |context, ty_args, args| -> PartialVMResult<NativeResult> {
native_print_stack_trace(&gas_params, context, ty_args, args)
},
)
}
}

/***************************************************************************************************
Expand All @@ -127,14 +169,18 @@ pub struct GasParameters {
}

pub fn make_all(
silent: bool,
gas_params: GasParameters,
move_std_addr: AccountAddress,
) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [
("print", make_native_print(gas_params.print, move_std_addr)),
(
"print",
make_native_print(silent, gas_params.print, move_std_addr),
),
(
"print_stack_trace",
make_native_print_stack_trace(gas_params.print_stack_trace),
make_native_print_stack_trace(silent, gas_params.print_stack_trace),
),
];

Expand Down
5 changes: 4 additions & 1 deletion language/move-stdlib/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ pub fn nursery_natives(
}

add_natives!("event", event::make_all(gas_params.event));
add_natives!("debug", debug::make_all(gas_params.debug, move_std_addr));
add_natives!(
"debug",
debug::make_all(true, gas_params.debug, move_std_addr)
);

make_table_from_iter(move_std_addr, natives)
}

0 comments on commit bef3552

Please sign in to comment.