Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cleanup] Make Function code nicer #15389

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aptos-move/aptos-gas-meter/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ where
fn charge_call(
&mut self,
_module_id: &ModuleId,
_func_name: &str,
_func_name: &IdentStr,
args: impl ExactSizeIterator<Item = impl ValueView>,
num_locals: NumArgs,
) -> PartialVMResult<()> {
Expand All @@ -204,7 +204,7 @@ where
fn charge_call_generic(
&mut self,
_module_id: &ModuleId,
_func_name: &str,
_func_name: &IdentStr,
ty_args: impl ExactSizeIterator<Item = impl TypeView>,
args: impl ExactSizeIterator<Item = impl ValueView>,
num_locals: NumArgs,
Expand Down
8 changes: 4 additions & 4 deletions aptos-move/aptos-gas-profiling/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ where
fn charge_call(
&mut self,
module_id: &ModuleId,
func_name: &str,
func_name: &IdentStr,
args: impl ExactSizeIterator<Item = impl ValueView> + Clone,
num_locals: NumArgs,
) -> PartialVMResult<()> {
Expand All @@ -419,7 +419,7 @@ where
self.record_bytecode(Opcodes::CALL, cost);
self.frames.push(CallFrame::new_function(
module_id.clone(),
Identifier::new(func_name).unwrap(),
func_name.to_owned(),
vec![],
));

Expand All @@ -429,7 +429,7 @@ where
fn charge_call_generic(
&mut self,
module_id: &ModuleId,
func_name: &str,
func_name: &IdentStr,
ty_args: impl ExactSizeIterator<Item = impl TypeView> + Clone,
args: impl ExactSizeIterator<Item = impl ValueView> + Clone,
num_locals: NumArgs,
Expand All @@ -446,7 +446,7 @@ where
self.record_bytecode(Opcodes::CALL_GENERIC, cost);
self.frames.push(CallFrame::new_function(
module_id.clone(),
Identifier::new(func_name).unwrap(),
func_name.to_owned(),
ty_tags,
));

Expand Down
4 changes: 2 additions & 2 deletions aptos-move/aptos-memory-usage-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ where
fn charge_call(
&mut self,
module_id: &ModuleId,
func_name: &str,
func_name: &IdentStr,
args: impl ExactSizeIterator<Item = impl ValueView> + Clone,
num_locals: NumArgs,
) -> PartialVMResult<()>;
Expand Down Expand Up @@ -177,7 +177,7 @@ where
fn charge_call_generic(
&mut self,
module_id: &ModuleId,
func_name: &str,
func_name: &IdentStr,
ty_args: impl ExactSizeIterator<Item = impl TypeView> + Clone,
args: impl ExactSizeIterator<Item = impl ValueView> + Clone,
num_locals: NumArgs,
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/framework/src/natives/function_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn native_check_dispatch_type_compatibility_impl(
Ok(smallvec![Value::bool(
rhs.ty_param_abilities() == lhs.ty_param_abilities()
&& rhs.return_tys() == lhs.return_tys()
&& &lhs.param_tys()[0..lhs.param_count() - 1] == rhs.param_tys()
&& &lhs.param_tys()[0..lhs.param_tys().len() - 1] == rhs.param_tys()
&& !rhs.is_friend_or_private()
&& (!context
.get_feature_flags()
Expand Down
63 changes: 37 additions & 26 deletions third_party/move/move-vm/runtime/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{
access_control::AccessControlState,
data_cache::TransactionDataCache,
loader::{LegacyModuleStorageAdapter, Loader, Resolver},
loader::{LegacyModuleStorageAdapter, LoadedFunctionOwner, Loader, Resolver},
module_traversal::TraversalContext,
native_extensions::NativeContextExtensions,
native_functions::NativeContext,
Expand Down Expand Up @@ -504,7 +504,7 @@ impl Interpreter {
gas_meter.balance_internal(),
traversal_context,
);
let native_function = function.get_native()?;
let native_function = function.as_native_function()?;

gas_meter.charge_native_function_before_execution(
ty_args.iter().map(|ty| TypeWithLoader { ty, resolver }),
Expand Down Expand Up @@ -1007,19 +1007,20 @@ impl Interpreter {
}
debug_writeln!(buf)?;

// Print out the current instruction.
debug_writeln!(buf)?;
debug_writeln!(buf, " Code:")?;
let pc = frame.pc as usize;
let code = function.code();
let before = if pc > 3 { pc - 3 } else { 0 };
let after = min(code.len(), pc + 4);
for (idx, instr) in code.iter().enumerate().take(pc).skip(before) {
debug_writeln!(buf, " [{}] {:?}", idx, instr)?;
}
debug_writeln!(buf, " > [{}] {:?}", pc, &code[pc])?;
for (idx, instr) in code.iter().enumerate().take(after).skip(pc + 1) {
debug_writeln!(buf, " [{}] {:?}", idx, instr)?;
if let Some(code) = function.code() {
// Print out the current instruction.
debug_writeln!(buf)?;
debug_writeln!(buf, " Code:")?;
let pc = frame.pc as usize;
let before = if pc > 3 { pc - 3 } else { 0 };
let after = min(code.len(), pc + 4);
for (idx, instr) in code.iter().enumerate().take(pc).skip(before) {
debug_writeln!(buf, " [{}] {:?}", idx, instr)?;
}
debug_writeln!(buf, " > [{}] {:?}", pc, &code[pc])?;
for (idx, instr) in code.iter().enumerate().take(after).skip(pc + 1) {
debug_writeln!(buf, " [{}] {:?}", idx, instr)?;
}
}

// Print out the locals.
Expand Down Expand Up @@ -1085,15 +1086,16 @@ impl Interpreter {
)
.as_str(),
);
let code = current_frame.function.code();
let pc = current_frame.pc as usize;
if pc < code.len() {
let mut i = 0;
for bytecode in &code[..pc] {
internal_state.push_str(format!("{}> {:?}\n", i, bytecode).as_str());
i += 1;
if let Some(code) = current_frame.function.code() {
let pc = current_frame.pc as usize;
if pc < code.len() {
let mut i = 0;
for bytecode in &code[..pc] {
internal_state.push_str(format!("{}> {:?}\n", i, bytecode).as_str());
i += 1;
}
internal_state.push_str(format!("{}* {:?}\n", i, code[pc]).as_str());
}
internal_state.push_str(format!("{}* {:?}\n", i, code[pc]).as_str());
}
internal_state.push_str(
format!(
Expand Down Expand Up @@ -2284,7 +2286,10 @@ impl Frame {
};
}

let code = self.function.code();
let code = self.function.code().ok_or_else(|| {
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message("Native function cannot be an entry point".to_string())
})?;
loop {
for instruction in &code[self.pc as usize..] {
trace!(
Expand Down Expand Up @@ -3139,8 +3144,14 @@ impl Frame {
module_store: &'a LegacyModuleStorageAdapter,
module_storage: &'a impl ModuleStorage,
) -> Resolver<'a> {
self.function
.get_resolver(loader, module_store, module_storage)
match &self.function.owner {
LoadedFunctionOwner::Module(module) => {
Resolver::for_module(loader, module_store, module_storage, module.clone())
},
LoadedFunctionOwner::Script(script) => {
Resolver::for_script(loader, module_store, module_storage, script.clone())
},
}
}

fn location(&self) -> Location {
Expand Down
Loading
Loading