Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-aptos committed Nov 13, 2024
1 parent 7423097 commit 43449cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
20 changes: 20 additions & 0 deletions aptos-move/aptos-vm-environment/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@
use aptos_native_interface::SafeNativeBuilder;
use move_core_types::language_storage::CORE_CODE_ADDRESS;
use move_vm_runtime::native_functions::NativeFunctionTable;
use std::collections::HashSet;

/// Builds and returns all Aptos native functions.
pub fn aptos_natives_with_builder(
builder: &mut SafeNativeBuilder,
inject_create_signer_for_gov_sim: bool,
) -> NativeFunctionTable {
let vector_bytecode_instruction_methods = HashSet::from([
"empty",
"length",
"borrow",
"borrow_mut",
"push_back",
"pop_back",
"destroy_empty",
"swap",
]);

#[allow(unreachable_code)]
aptos_move_stdlib::natives::all_natives(CORE_CODE_ADDRESS, builder)
.into_iter()
.filter(|(_, name, func_name, _)|
if name.as_str() == "vector" && vector_bytecode_instruction_methods.contains(func_name.as_str()) {
println!("ERROR: Tried to register as native a vector bytecode_instruction method {}, skipping.", func_name.as_str());
false
} else {
true
}
)
.chain(aptos_framework::natives::all_natives(
CORE_CODE_ADDRESS,
builder,
Expand Down
20 changes: 1 addition & 19 deletions third_party/move/move-vm/runtime/src/native_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use move_vm_types::{
loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value,
};
use std::{
collections::{HashMap, HashSet, VecDeque},
collections::{HashMap, VecDeque},
fmt::Write,
sync::Arc,
};
Expand Down Expand Up @@ -81,26 +81,8 @@ impl NativeFunctions {
where
I: IntoIterator<Item = (AccountAddress, Identifier, Identifier, NativeFunction)>,
{
let vector_bytecode_instruction_methods = HashSet::from([
"empty",
"length",
"borrow",
"borrow_mut",
"push_back",
"pop_back",
"destroy_empty",
"swap",
]);

let mut map = HashMap::new();
for (addr, module_name, func_name, func) in natives.into_iter() {
if module_name.as_str() == "string"
&& vector_bytecode_instruction_methods.contains(func_name.as_str())
{
println!("ERROR: Tried to register as native a vector bytecode_instruction method {}, skipping.", func_name.as_str());
continue;
}

let modules = map.entry(addr).or_insert_with(HashMap::new);
let funcs = modules
.entry(module_name.into_string())
Expand Down
7 changes: 5 additions & 2 deletions third_party/move/move-vm/types/src/values/values_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2236,10 +2236,11 @@ impl VectorRef {
/// to vector `to`, inserting them starting at the `insert_position`.
/// In the `from` vector, elements after the selected range are moved left to fill the hole
/// (i.e. range is removed, while the order of the rest of the elements is kept)
/// In the `to` vector, elements after the `insert_position` are moved the the right to make space for new elements
/// In the `to` vector, elements after the `insert_position` are moved to the right to make space for new elements
/// (i.e. range is inserted, while the order of the rest of the elements is kept).
///
/// Move prevents from having two mutable references to the same value, so `from` and `to` vectors are guaranted to be distinct.
/// Precondition for this function is that `from` and `to` vectors are required to be distinct
/// Move will guaranteee that invariant, because it prevents from having two mutable references to the same value.
pub fn move_range(
from_self: &Self,
removal_position: usize,
Expand All @@ -2253,6 +2254,8 @@ impl VectorRef {

// potentially unnecessary as native call should've checked the types already
// (unlike other vector functions that are bytecodes)
// TODO: potentially unnecessary, can be removed - as these are only required for
// bytecode instructions, as types are checked when native functions are called.
check_elem_layout(type_param, from_c)?;
check_elem_layout(type_param, to_c)?;

Expand Down

0 comments on commit 43449cb

Please sign in to comment.