Skip to content

Commit

Permalink
Added tips about dynamic libraries (nervosnetwork#117)
Browse files Browse the repository at this point in the history
* Added tips about dynamic libraries

* Use lazy_static
  • Loading branch information
mohanson authored Sep 30, 2024
1 parent a448be5 commit 548a42b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
49 changes: 47 additions & 2 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions contracts/ckb-std-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ edition = "2021"
ckb-std = { path = "../../", features = ["build-with-clang", "dlopen-c", "log"] }
blake2b-ref = { version = "0.3", default-features = false }
bytes = { version = "1.7", default-features = false }
lazy_static = { version = "1.5.0", default-features = false, features = ["spin_no_std"] }
log = { version = "0.4", default-features = false }
spin = { version = "0.9" }
32 changes: 22 additions & 10 deletions contracts/ckb-std-tests/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use ckb_std::{dynamic_loading, dynamic_loading_c_impl};
use core::ffi::c_void;
#[cfg(target_arch = "riscv64")]
use core::mem::size_of_val;
#[cfg(target_arch = "riscv64")]
use lazy_static::lazy_static;
#[cfg(target_arch = "riscv64")]
use spin::Mutex;

fn new_blake2b() -> Blake2b {
const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
Expand Down Expand Up @@ -795,6 +799,19 @@ fn test_log() {
ckb_std::log::error!("this is error");
}

#[cfg(target_arch = "riscv64")]
lazy_static! {
// Context should not be dropped.
static ref old_context: Mutex<ContextTypeOld> = {
#[allow(deprecated)]
Mutex::new(unsafe { ContextTypeOld::new() })
};
// Context should not be dropped.
static ref new_context: Mutex<ContextType> = {
Mutex::new(unsafe { ContextType::new() })
};
}

pub fn main() -> Result<(), Error> {
test_basic();
test_load_data();
Expand All @@ -805,6 +822,11 @@ pub fn main() -> Result<(), Error> {
test_query();
test_calc_data_hash();

#[cfg(target_arch = "riscv64")]
test_dynamic_loading(&mut old_context.lock());
#[cfg(target_arch = "riscv64")]
test_dynamic_loading_c_impl(&mut new_context.lock());

test_vm_version();
test_current_cycles();
test_since();
Expand All @@ -815,15 +837,5 @@ pub fn main() -> Result<(), Error> {
test_log();
}

#[cfg(target_arch = "riscv64")]
unsafe {
let mut context = ContextType::new();
#[allow(deprecated)]
let mut old_context = ContextTypeOld::new();

test_dynamic_loading(&mut old_context);
test_dynamic_loading_c_impl(&mut context);
}

Ok(())
}
2 changes: 2 additions & 0 deletions test/simulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ path = "src/exec_callee.rs"
ckb-std = { path = "../..", default-features=false, features = ["allocator", "calc-hash", "ckb-types", "libc", "native-simulator"] }
blake2b-ref = { version = "0.3", default-features = false }
bytes = { version = "1.6.0", default-features = false }
lazy_static = { version = "1.5.0", default-features = false, features = ["spin_no_std"] }
log = { version = "0.4.17", default-features = false }
spin = { version = "0.9" }

[dev-dependencies]
libloading = "0.8.4"

0 comments on commit 548a42b

Please sign in to comment.