diff --git a/.gitignore b/.gitignore index a343936c..d88dc51c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ resources/tests/game-referee/*.sym resources/tests/game-referee/*.clvm.hex __pycache__ .pytest_cache +main.sym +venv/ diff --git a/Cargo.lock b/Cargo.lock index 9b0302f8..425c7907 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,18 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -12,16 +24,16 @@ dependencies = [ ] [[package]] -name = "anyhow" -version = "1.0.75" +name = "allocator-api2" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] -name = "arbitrary" -version = "1.3.2" +name = "anyhow" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] name = "autocfg" @@ -150,38 +162,36 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chia-bls" -version = "0.3.3" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba003303bda3019f2fe94e5478555a96addff1d6a1ea84bfe207b3f075cd8420" +checksum = "725b43b268cb81f014559eed11a74d0e5b9dd55282cae272ff509796099ab0b9" dependencies = [ "anyhow", - "arbitrary", "blst", "chia-traits", "hex", "hkdf", + "lru", "sha2 0.10.8", "thiserror", - "tiny-bip39", ] [[package]] name = "chia-traits" -version = "0.3.3" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe532ed59df94db00cc1fb85edc3c4dd7287d0590eefb356d960671b93081334" +checksum = "1fb786114e5c748fe0af3ba1e95840fa1910b28f7300c05184506045aff60bb6" dependencies = [ "chia_streamable_macro", - "hex", "sha2 0.10.8", "thiserror", ] [[package]] name = "chia_streamable_macro" -version = "0.3.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "342cdb29dd8c1214d60fac24100ab1567756270710e93336db910a531fe8a264" +checksum = "43975e01fb4293021af4950366a6f80c45d7301d499622359c7533bd7af19592" dependencies = [ "proc-macro-crate", "proc-macro2 1.0.66", @@ -189,16 +199,6 @@ dependencies = [ "syn 2.0.32", ] -[[package]] -name = "clvm-traits" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5548b7de9921c68b2ca1101c08dd416bfa9afdf319f5b45183ecb3574ee177a3" -dependencies = [ - "num-bigint", - "thiserror", -] - [[package]] name = "clvm_tools_rs" version = "0.1.44" @@ -237,13 +237,12 @@ dependencies = [ [[package]] name = "clvmr" -version = "0.3.3" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deabd4e4163efb64486937a7c80996e5bdfb88d21a6baa9c0f802e6b45906109" +checksum = "903233f3bc9392b44a589f0cea46a53a67fedc3afb4b1303f636977074a505c5" dependencies = [ "chia-bls", - "clvm-traits", - "hex", + "hex-literal", "k256", "lazy_static", "num-bigint", @@ -491,6 +490,10 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +dependencies = [ + "ahash", + "allocator-api2", +] [[package]] name = "heck" @@ -510,6 +513,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hkdf" version = "0.12.4" @@ -692,6 +701,15 @@ version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +[[package]] +name = "lru" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37ee39891760e7d94734f6f63fedc29a2e4a152f836120753a72503f09fcf904" +dependencies = [ + "hashbrown", +] + [[package]] name = "memchr" version = "2.6.4" @@ -850,15 +868,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "pbkdf2" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" -dependencies = [ - "digest 0.10.7", -] - [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -1102,12 +1111,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustix" version = "0.38.4" @@ -1327,40 +1330,6 @@ dependencies = [ "num_cpus", ] -[[package]] -name = "tiny-bip39" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62cc94d358b5a1e84a5cb9109f559aa3c4d634d2b1b4de3d0fa4adc7c78e2861" -dependencies = [ - "anyhow", - "hmac", - "once_cell", - "pbkdf2", - "rand", - "rustc-hash", - "sha2 0.10.8", - "thiserror", - "unicode-normalization", - "wasm-bindgen", - "zeroize", -] - -[[package]] -name = "tinyvec" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - [[package]] name = "toml_datetime" version = "0.6.5" @@ -1390,15 +1359,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" -[[package]] -name = "unicode-normalization" -version = "0.1.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" -dependencies = [ - "tinyvec", -] - [[package]] name = "unicode-segmentation" version = "1.10.1" @@ -1646,6 +1606,26 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2 1.0.66", + "quote 1.0.32", + "syn 2.0.32", +] + [[package]] name = "zeroize" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index 3a81eb1f..0b144328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ do-notation = "0.1.3" serde_json = "1.0" sha2 = "0.9.5" tempfile = "3.3.0" -clvmr = { version = "=0.3.3", features = ["pre-eval"] } +clvmr = { version = "0.8.0", features = ["pre-eval"] } binascii = "0.1.4" yaml-rust = "0.4" linked-hash-map = "0.5.6" diff --git a/src/classic/clvm/casts.rs b/src/classic/clvm/casts.rs index de10c719..0158c8a5 100644 --- a/src/classic/clvm/casts.rs +++ b/src/classic/clvm/casts.rs @@ -1,6 +1,6 @@ +use clvm_rs::NodePtr; use num_bigint::ToBigInt; -use clvm_rs::allocator::Allocator; use clvm_rs::reduction::EvalErr; use crate::classic::clvm::__type_compatibility__::{ @@ -12,16 +12,12 @@ pub struct TConvertOption { pub signed: bool, } -pub fn int_from_bytes( - allocator: &Allocator, - b: Bytes, - option: Option, -) -> Result { +pub fn int_from_bytes(b: Bytes, option: Option) -> Result { if b.length() == 0 { return Ok(0); } else if b.length() * 8 > 64 { return Err(EvalErr( - allocator.null(), + NodePtr::NIL, "Cannot convert Bytes to Integer larger than 64bit. Use bigint_from_bytes instead." .to_string(), )); diff --git a/src/classic/clvm/serialize.rs b/src/classic/clvm/serialize.rs index 6deb9dd7..a9f57c24 100644 --- a/src/classic/clvm/serialize.rs +++ b/src/classic/clvm/serialize.rs @@ -100,7 +100,8 @@ impl<'a> Iterator for SExpToBytesIterator<'a> { SExp::Atom => { // The only node we have in scope is x, so this atom // capture is trivial. - let buf = self.allocator.atom(x).to_vec(); + let atom = self.allocator.atom(x); + let buf = atom.as_ref().to_vec(); let bytes = Bytes::new(Some(BytesFromType::Raw(buf.clone()))); match atom_size_blob(&bytes) { Ok((original, b)) => { @@ -184,7 +185,7 @@ impl OpStackEntry for OpReadSexp { ) -> Option { let blob = f.read(1); if blob.length() == 0 { - return Some(EvalErr(allocator.null(), "bad encoding".to_string())); + return Some(EvalErr(NodePtr::NIL, "bad encoding".to_string())); } let b = blob.at(0); @@ -236,7 +237,7 @@ pub fn sexp_from_stream<'a>( } Err(EvalErr( - allocator.null(), + NodePtr::NIL, "No value left after conversion".to_string(), )) } @@ -250,7 +251,7 @@ pub fn atom_from_stream<'a>( let mut b = b_; if b == 0x80 { - return Ok(allocator.null()); + return Ok(NodePtr::NIL); } else if b <= MAX_SINGLE_BYTE as u8 { return allocator.new_atom(&[b]); } @@ -268,17 +269,17 @@ pub fn atom_from_stream<'a>( if bit_count > 1 { let bin = f.read(bit_count - 1); if bin.length() != bit_count - 1 { - return Err(EvalErr(allocator.null(), "bad encoding".to_string())); + return Err(EvalErr(NodePtr::NIL, "bad encoding".to_string())); } size_blob = size_blob.concat(&bin); } - int_from_bytes(allocator, size_blob, None).and_then(|size| { + int_from_bytes(size_blob, None).and_then(|size| { if size >= 0x400000000 { - return Err(EvalErr(allocator.null(), "blob too large".to_string())); + return Err(EvalErr(NodePtr::NIL, "blob too large".to_string())); } let blob = f.read(size as usize); if blob.length() != size as usize { - return Err(EvalErr(allocator.null(), "bad encoding".to_string())); + return Err(EvalErr(NodePtr::NIL, "bad encoding".to_string())); } return allocator.new_atom(blob.data()); }) diff --git a/src/classic/clvm/sexp.rs b/src/classic/clvm/sexp.rs index bf8b1fd3..84f69725 100644 --- a/src/classic/clvm/sexp.rs +++ b/src/classic/clvm/sexp.rs @@ -43,7 +43,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { - return Err(EvalErr(allocator.null(), "empty value stack".to_string())); + return Err(EvalErr(NodePtr::NIL, "empty value stack".to_string())); } Some(rc) => rc, }; @@ -57,7 +57,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { let target_index = stack.len(); - match allocator.new_pair(allocator.null(), allocator.null()) { + match allocator.new_pair(NodePtr::NIL, NodePtr::NIL) { Ok(pair) => { stack.push(Rc::new(CastableType::CLVMObject(pair))); } @@ -75,7 +75,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { let target_index = stack.len(); - stack.push(Rc::new(CastableType::CLVMObject(allocator.null()))); + stack.push(Rc::new(CastableType::CLVMObject(NodePtr::NIL))); for vi in v.iter().take(v.len() - 1) { stack.push(vi.clone()); ops.push(SexpStackOp::OpPrepend(target_index)); @@ -158,14 +158,14 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { return Err(EvalErr( - allocator.null(), + NodePtr::NIL, format!("Setting wing of non pair {:?}", stack[target]), )); } }, _ => { return Err(EvalErr( - allocator.null(), + NodePtr::NIL, format!("op_set_pair on atom item {target:?} in vec {stack:?} ops {ops:?}"), )); } @@ -183,16 +183,13 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { return Err(EvalErr( - allocator.null(), + NodePtr::NIL, format!("unrealized pair prepended {:?}", stack[target]), )); } }, _ => { - return Err(EvalErr( - allocator.null(), - format!("unrealized prepend {top:?}"), - )); + return Err(EvalErr(NodePtr::NIL, format!("unrealized prepend {top:?}"))); } }, } @@ -200,17 +197,17 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result Err(EvalErr(allocator.null(), "stack empty".to_string())), + None => Err(EvalErr(NodePtr::NIL, "stack empty".to_string())), Some(top) => match top.borrow() { CastableType::CLVMObject(o) => Ok(*o), _ => Err(EvalErr( - allocator.null(), + NodePtr::NIL, format!("unimplemented {:?}", stack[0]), )), }, @@ -227,7 +224,7 @@ pub fn bool_sexp(allocator: &Allocator, b: bool) -> NodePtr { if b { allocator.one() } else { - allocator.null() + NodePtr::NIL } } @@ -355,7 +352,11 @@ pub fn rest(allocator: &Allocator, sexp: NodePtr) -> Result { pub fn atom(allocator: &Allocator, sexp: NodePtr) -> Result, EvalErr> { match allocator.sexp(sexp) { - SExp::Atom => Ok(allocator.atom(sexp).to_vec()), // only sexp in scope + SExp::Atom => { + // only sexp in scope + let atom = allocator.atom(sexp); + Ok(atom.as_ref().to_vec()) + } _ => Err(EvalErr(sexp, "not an atom".to_string())), } } @@ -383,7 +384,7 @@ pub fn proper_list(allocator: &Allocator, sexp: NodePtr, store: bool) -> Option< } pub fn enlist(allocator: &mut Allocator, vec: &[NodePtr]) -> Result { - let mut built = allocator.null(); + let mut built = NodePtr::NIL; for i_reverse in 0..vec.len() { let i = vec.len() - i_reverse - 1; diff --git a/src/classic/clvm_tools/binutils.rs b/src/classic/clvm_tools/binutils.rs index e06192a3..4678847a 100644 --- a/src/classic/clvm_tools/binutils.rs +++ b/src/classic/clvm_tools/binutils.rs @@ -25,7 +25,7 @@ pub fn assemble_from_ir( ir_sexp: Rc, ) -> Result { match ir_sexp.borrow() { - IRRepr::Null => Ok(allocator.null()), + IRRepr::Null => Ok(NodePtr::NIL), IRRepr::Quotes(b) => allocator.new_atom(b.data()), IRRepr::Int(b, _signed) => allocator.new_atom(b.data()), IRRepr::Hex(b) => allocator.new_atom(b.data()), @@ -127,7 +127,8 @@ pub fn disassemble_to_ir_with_kw( SExp::Atom => { // sexp is the only node in scope. - let bytes = Bytes::new(Some(BytesFromType::Raw(allocator.atom(sexp).to_vec()))); + let atom = allocator.atom(sexp); + let bytes = Bytes::new(Some(BytesFromType::Raw(atom.as_ref().to_vec()))); ir_for_atom(&bytes, allow_keyword, keyword_from_atom) } } @@ -157,6 +158,6 @@ pub fn assemble(allocator: &mut Allocator, s: &str) -> Result let mut reader = IRReader::new(stream); reader .read_expr() - .map_err(|e| EvalErr(allocator.null(), e.to_string())) + .map_err(|e| EvalErr(NodePtr::NIL, e.to_string())) .and_then(|ir| assemble_from_ir(allocator, Rc::new(ir))) } diff --git a/src/classic/clvm_tools/clvmc.rs b/src/classic/clvm_tools/clvmc.rs index 9db074e3..ec6f9cea 100644 --- a/src/classic/clvm_tools/clvmc.rs +++ b/src/classic/clvm_tools/clvmc.rs @@ -90,7 +90,7 @@ pub fn compile_clvm_text_maybe_opt( input_path: &str, classic_with_opts: bool, ) -> Result { - let ir_src = read_ir(text).map_err(|s| EvalErr(allocator.null(), s.to_string()))?; + let ir_src = read_ir(text).map_err(|s| EvalErr(NodePtr::NIL, s.to_string()))?; let assembled_sexp = assemble_from_ir(allocator, Rc::new(ir_src))?; let dialect = detect_modern(allocator, assembled_sexp); @@ -118,7 +118,7 @@ pub fn compile_clvm_text_maybe_opt( Ok(convert_to_clvm_rs(allocator, res)?) } else { let compile_invoke_code = run(allocator); - let input_sexp = allocator.new_pair(assembled_sexp, allocator.null())?; + let input_sexp = allocator.new_pair(assembled_sexp, NodePtr::NIL)?; let run_program = run_program_for_search_paths(input_path, &opts.get_search_paths(), false); if classic_with_opts { run_program.set_compiler_opts(Some(opts)); diff --git a/src/classic/clvm_tools/cmds.rs b/src/classic/clvm_tools/cmds.rs index 7640b9ce..7607b489 100644 --- a/src/classic/clvm_tools/cmds.rs +++ b/src/classic/clvm_tools/cmds.rs @@ -241,7 +241,7 @@ impl TConversion for OpcConversion { .and_then(|ir_sexp| assemble_from_ir(allocator, Rc::new(ir_sexp)).map_err(|e| e.1)) .map(|sexp| t(sexp, sexp_as_bin(allocator, sexp).hex())) .map(Ok) // Flatten result type to Ok - .unwrap_or_else(|err| Ok(t(allocator.null(), err))) // Original code printed error messages on stdout, ret 0 on CLVM error + .unwrap_or_else(|err| Ok(t(NodePtr::NIL, err))) // Original code printed error messages on stdout, ret 0 on CLVM error } } @@ -805,7 +805,7 @@ fn calculate_cost_offset( This is a hack and need to go away, probably when we do dialects for real, and then the dialect can have a `run_program` API. */ - let almost_empty_list = enlist(allocator, &[allocator.null()]).unwrap(); + let almost_empty_list = enlist(allocator, &[NodePtr::NIL]).unwrap(); let cost = run_program .run_program(allocator, run_script, almost_empty_list, None) .map(|x| x.0) @@ -1284,13 +1284,14 @@ pub fn launch_tool(stdout: &mut Stream, args: &[String], tool_name: &str, defaul }); #[allow(clippy::type_complexity)] - let closure: Rc Box)>> = Rc::new(move |v| { - let post_eval_fn_clone = post_eval_fn.clone(); - Box::new(move |n| { - let post_eval_fn_clone_2 = post_eval_fn_clone.clone(); - (*post_eval_fn_clone_2)(v, n) - }) - }); + let closure: Rc Box)>> = + Rc::new(move |v| { + let post_eval_fn_clone = post_eval_fn.clone(); + Box::new(move |_allocator, n| { + let post_eval_fn_clone_2 = post_eval_fn_clone.clone(); + (*post_eval_fn_clone_2)(v, n) + }) + }); if emit_symbol_output { #[allow(clippy::type_complexity)] @@ -1299,7 +1300,8 @@ pub fn launch_tool(stdout: &mut Stream, args: &[String], tool_name: &str, defaul &mut Allocator, NodePtr, NodePtr, - ) -> Result))>>, EvalErr>, + ) + -> Result))>>, EvalErr>, > = Box::new(move |allocator, sexp, args| { let pre_eval_clone = pre_eval_fn.clone(); trace_pre_eval( diff --git a/src/classic/clvm_tools/debug.rs b/src/classic/clvm_tools/debug.rs index 15bd5142..32d08293 100644 --- a/src/classic/clvm_tools/debug.rs +++ b/src/classic/clvm_tools/debug.rs @@ -102,7 +102,7 @@ pub fn build_symbol_dump( let mut map_result: Vec = Vec::new(); for (k, v) in constants_lookup.iter() { - let run_result = run_program.run_program(allocator, *v, allocator.null(), None)?; + let run_result = run_program.run_program(allocator, *v, NodePtr::NIL, None)?; let sha256 = sha256tree(allocator, run_result.1).hex(); let sha_atom = allocator.new_atom(sha256.as_bytes())?; @@ -152,7 +152,7 @@ fn text_trace( let mut env = env_; match symbol { Some(sym) => { - env = rest(allocator, env).unwrap_or_else(|_| allocator.null()); + env = rest(allocator, env).unwrap_or(NodePtr::NIL); let symbol_atom = allocator.new_atom(sym.as_bytes()).unwrap(); let symbol_list = allocator.new_pair(symbol_atom, env).unwrap(); symbol_val = disassemble_f(allocator, symbol_list); @@ -180,7 +180,7 @@ fn table_trace( ) { let (sexp, args) = match allocator.sexp(form) { SExp::Pair(sexp, args) => (sexp, args), - SExp::Atom => (form, allocator.null()), + SExp::Atom => (form, NodePtr::NIL), }; stdout.write_str(&format!("exp: {}\n", disassemble_f(allocator, sexp))); diff --git a/src/classic/clvm_tools/pattern_match.rs b/src/classic/clvm_tools/pattern_match.rs index 02e894b4..e770b56d 100644 --- a/src/classic/clvm_tools/pattern_match.rs +++ b/src/classic/clvm_tools/pattern_match.rs @@ -61,24 +61,33 @@ pub fn match_sexp( } (SExp::Pair(pleft, pright), _) => match (allocator.sexp(pleft), allocator.sexp(pright)) { (SExp::Atom, SExp::Atom) => { - let pright_atom = allocator.atom(pright).to_vec(); + let left_atom = allocator.atom(pleft); + let right_atom = allocator.atom(pright); + + // This is a false positive due to Allocator lifetime. + #[allow(clippy::unnecessary_to_owned)] match allocator.sexp(sexp) { SExp::Atom => { // Expression is ($ . $), sexp is '$', result: no capture. // Avoid double borrow. - if allocator.atom(pleft) == ATOM_MATCH { - if allocator.atom(pright) == ATOM_MATCH { - if allocator.atom(sexp) == ATOM_MATCH { + let sexp_atom = allocator.atom(sexp); + if left_atom.as_ref() == ATOM_MATCH { + if right_atom.as_ref() == ATOM_MATCH { + if sexp_atom.as_ref() == ATOM_MATCH { return Some(HashMap::new()); } return None; } - return unify_bindings(allocator, known_bindings, &pright_atom, sexp); + return unify_bindings( + allocator, + known_bindings, + &right_atom.as_ref().to_vec(), + sexp, + ); } - if allocator.atom(pleft) == SEXP_MATCH { - if allocator.atom(pright) == SEXP_MATCH - && allocator.atom(sexp) == SEXP_MATCH + if left_atom.as_ref() == SEXP_MATCH { + if right_atom.as_ref() == SEXP_MATCH && sexp_atom.as_ref() == SEXP_MATCH { return Some(HashMap::new()); } @@ -87,7 +96,7 @@ pub fn match_sexp( allocator, known_bindings, // pat_right_bytes - &pright_atom, + &right_atom.as_ref().to_vec(), sexp, ); } @@ -95,14 +104,12 @@ pub fn match_sexp( None } SExp::Pair(sleft, sright) => { - if allocator.atom(pleft) == SEXP_MATCH - && allocator.atom(pright) != SEXP_MATCH - { + if left_atom.as_ref() == SEXP_MATCH && right_atom.as_ref() != SEXP_MATCH { return unify_bindings( allocator, known_bindings, // pat_right_bytes - &pright_atom, + &right_atom.as_ref().to_vec(), sexp, ); } diff --git a/src/classic/clvm_tools/sha256tree.rs b/src/classic/clvm_tools/sha256tree.rs index ebbc3197..38649bb1 100644 --- a/src/classic/clvm_tools/sha256tree.rs +++ b/src/classic/clvm_tools/sha256tree.rs @@ -34,11 +34,14 @@ pub fn sha256tree(allocator: &mut Allocator, v: NodePtr) -> Bytes { .concat(&right), ) } - SExp::Atom => sha256( - Bytes::new(Some(BytesFromType::Raw(vec![1]))).concat(&Bytes::new(Some( - // only v in scope. - BytesFromType::Raw(allocator.atom(v).to_vec()), - ))), - ), + SExp::Atom => { + let atom = allocator.atom(v); + sha256( + Bytes::new(Some(BytesFromType::Raw(vec![1]))).concat(&Bytes::new(Some( + // only v in scope. + BytesFromType::Raw(atom.as_ref().to_vec()), + ))), + ) + } } } diff --git a/src/classic/clvm_tools/stages/stage_2/compile.rs b/src/classic/clvm_tools/stages/stage_2/compile.rs index 64b367c4..a19793f0 100644 --- a/src/classic/clvm_tools/stages/stage_2/compile.rs +++ b/src/classic/clvm_tools/stages/stage_2/compile.rs @@ -129,17 +129,18 @@ pub fn compile_qq( SExp::Pair(op, sexp_rest) => { if let SExp::Atom = allocator.sexp(op) { // opbuf => op - if allocator.atom(op).to_vec() == qq_atom() { + let op_atom = allocator.atom(op); + if op_atom.as_ref() == qq_atom() { return m! { cons_atom <- allocator.new_atom(&[4]); subexp <- compile_qq(allocator, sexp_rest, macro_lookup, symbol_table, runner.clone(), level+1); - quoted_null <- quote(allocator, allocator.null()); + quoted_null <- quote(allocator, NodePtr::NIL); consed <- enlist(allocator, &[cons_atom, subexp, quoted_null]); run_list <- enlist(allocator, &[cons_atom, op, consed]); com_qq(allocator, "qq sexp pair".to_string(), macro_lookup, symbol_table, runner, run_list) }; - } else if allocator.atom(op).to_vec() == unquote_atom() { + } else if op_atom.as_ref() == unquote_atom() { // opbuf if level == 1 { // (qq (unquote X)) => X @@ -153,7 +154,7 @@ pub fn compile_qq( cons_atom <- allocator.new_atom(&[4]); subexp <- compile_qq(allocator, sexp_rest, macro_lookup, symbol_table, runner.clone(), level-1); - quoted_null <- quote(allocator, allocator.null()); + quoted_null <- quote(allocator, NodePtr::NIL); consed_subexp <- enlist(allocator, &[cons_atom, subexp, quoted_null]); run_list <- enlist(allocator, &[cons_atom, op, consed_subexp]); com_qq(allocator, "qq pair general".to_string(), macro_lookup, symbol_table, runner, run_list) @@ -212,7 +213,8 @@ fn lower_quote_(allocator: &mut Allocator, prog: NodePtr) -> Result 1 { mp_list[1] } else { - allocator.null() + NodePtr::NIL }; match allocator.sexp(mp_list[0]) { SExp::Atom => { // was macro_name, but it's singular and probably // not useful to rename. - if allocator.atom(mp_list[0]) == operator { + let atom = allocator.atom(mp_list[0]); + if atom.as_ref() == operator { return Ok(Some(value)); } } @@ -382,13 +385,14 @@ fn transform_program_atom( continue; } - let value = if v.len() > 1 { v[1] } else { allocator.null() }; + let value = if v.len() > 1 { v[1] } else { NodePtr::NIL }; match allocator.sexp(v[0]) { SExp::Atom => { // v[0] is close by, and probably not useful to // rename here. - if allocator.atom(v[0]) == a { + let atom = allocator.atom(v[0]); + if atom.as_ref() == a { return Ok(Reduction(1, value)); } } @@ -465,15 +469,15 @@ fn find_symbol_match( SExp::Atom => { let symbol = symdef[0]; let value = if symdef.len() == 1 { - allocator.null() + NodePtr::NIL } else { symdef[1] }; let symbuf = allocator.atom(symdef[0]); - if b"*" == symbuf { + if b"*" == symbuf.as_ref() { return Ok(Some(SymbolResult::Direct(r))); - } else if opname == symbuf { + } else if opname == symbuf.as_ref() { return Ok(Some(SymbolResult::Matched(symbol, value))); } } @@ -639,11 +643,13 @@ fn do_com_prog_( match allocator.sexp(prog) { SExp::Atom => { // Note: can't co-borrow with allocator below. - let prog_bytes = allocator.atom(prog).to_vec(); + let prog_atom = allocator.atom(prog); transform_program_atom( allocator, prog, - &prog_bytes, + // This is a false positive due to Allocator lifetime. + #[allow(clippy::unnecessary_to_owned)] + &prog_atom.as_ref().to_vec(), symbol_table ) }, @@ -651,8 +657,9 @@ fn do_com_prog_( match allocator.sexp(operator) { SExp::Atom => { // Note: can't co-borrow with allocator below. - let opbuf = allocator.atom(operator).to_vec(); - get_macro_program(allocator, &opbuf, macro_lookup). + let op_atom = allocator.atom(operator); + let op_buf = op_atom.as_ref().to_vec(); + get_macro_program(allocator, op_atom.as_ref(), macro_lookup). and_then(|x| match x { Some(value) => { try_expand_macro_for_atom( @@ -667,7 +674,7 @@ fn do_com_prog_( compile_operator_atom( allocator, prog, - &opbuf, + &op_buf, macro_lookup, symbol_table, run_program.clone() @@ -676,7 +683,7 @@ fn do_com_prog_( allocator, prog, operator, - &opbuf, + &op_buf, prog_rest, macro_lookup, symbol_table, @@ -722,7 +729,7 @@ pub fn do_com_prog_for_dialect( ) -> Response { match allocator.sexp(sexp) { SExp::Pair(prog, extras) => { - let mut symbol_table = allocator.null(); + let mut symbol_table = NodePtr::NIL; let macro_lookup; let mut elist = Vec::new(); @@ -776,20 +783,22 @@ pub fn get_compile_filename( let cvt_prog = assemble(allocator, "(_get_compile_filename)")?; let Reduction(_, cvt_prog_result) = - runner.run_program(allocator, cvt_prog, allocator.null(), None)?; + runner.run_program(allocator, cvt_prog, NodePtr::NIL, None)?; - if cvt_prog_result == allocator.null() { + if cvt_prog_result == NodePtr::NIL { return Ok(None); } if let SExp::Atom = allocator.sexp(cvt_prog_result) { // only cvt_prog_result in scope. - let abuf = allocator.atom(cvt_prog_result).to_vec(); - return Ok(Some(Bytes::new(Some(BytesFromType::Raw(abuf))).decode())); + let atom = allocator.atom(cvt_prog_result); + return Ok(Some( + Bytes::new(Some(BytesFromType::Raw(atom.as_ref().to_vec()))).decode(), + )); } Err(EvalErr( - allocator.null(), + NodePtr::NIL, "Couldn't decode result filename".to_string(), )) } @@ -800,16 +809,15 @@ pub fn get_search_paths( ) -> Result, EvalErr> { let search_paths_prog = assemble(allocator, "(_get_include_paths)")?; let search_path_result = - runner.run_program(allocator, search_paths_prog, allocator.null(), None)?; + runner.run_program(allocator, search_paths_prog, NodePtr::NIL, None)?; let mut res = Vec::new(); if let Some(l) = proper_list(allocator, search_path_result.1, true) { for elt in l.iter().copied() { if let SExp::Atom = allocator.sexp(elt) { // Only elt in scope. - res.push( - Bytes::new(Some(BytesFromType::Raw(allocator.atom(elt).to_vec()))).decode(), - ); + let atom = allocator.atom(elt); + res.push(Bytes::new(Some(BytesFromType::Raw(atom.as_ref().to_vec()))).decode()); } } } diff --git a/src/classic/clvm_tools/stages/stage_2/defaults.rs b/src/classic/clvm_tools/stages/stage_2/defaults.rs index 5c09c13d..96a39506 100644 --- a/src/classic/clvm_tools/stages/stage_2/defaults.rs +++ b/src/classic/clvm_tools/stages/stage_2/defaults.rs @@ -106,7 +106,7 @@ fn build_default_macro_lookup( macros_src: &[String], ) -> NodePtr { let run = assemble(allocator, "(a (com 2 3) 1)").unwrap(); - let mut default_macro_lookup: NodePtr = allocator.null(); + let mut default_macro_lookup: NodePtr = NodePtr::NIL; for macro_src in macros_src { let macro_sexp = assemble(allocator, macro_src).unwrap(); let env = allocator diff --git a/src/classic/clvm_tools/stages/stage_2/inline.rs b/src/classic/clvm_tools/stages/stage_2/inline.rs index 215c3679..8562cbb3 100644 --- a/src/classic/clvm_tools/stages/stage_2/inline.rs +++ b/src/classic/clvm_tools/stages/stage_2/inline.rs @@ -19,7 +19,8 @@ pub fn is_at_capture( allocator.sexp(tree_first), proper_list(allocator, tree_rest, true), ) { - if allocator.atom(tree_first) == b"@" && spec.len() == 2 { + let first_atom = allocator.atom(tree_first); + if first_atom.as_ref() == b"@" && spec.len() == 2 { return Some((spec[0], spec[1])); } } @@ -100,7 +101,8 @@ fn formulate_path_selections_for_destructuring_arg( }; // Was cbuf from capture. - selections.insert(allocator.atom(capture).to_vec(), tail); + let capture_atom = allocator.atom(capture); + selections.insert(capture_atom.as_ref().to_vec(), tail); return formulate_path_selections_for_destructuring_arg( allocator, @@ -149,7 +151,8 @@ fn formulate_path_selections_for_destructuring_arg( } SExp::Atom => { // Note: can't co-borrow with allocator below. - let buf = allocator.atom(arg_sexp).to_vec(); + let buf_atom = allocator.atom(arg_sexp); + let buf = buf_atom.as_ref().to_vec(); if !buf.is_empty() { if let Some(capture) = referenced_from { let tail = wrap_path_selection(allocator, arg_path + arg_depth, capture)?; @@ -229,8 +232,8 @@ pub fn formulate_path_selections_for_destructuring( let quoted_arg_list = wrap_in_unquote(allocator, capture)?; let tail = wrap_in_compile_time_list(allocator, quoted_arg_list)?; // Was: cbuf from capture. - let buf = allocator.atom(capture); - selections.insert(buf.to_vec(), tail); + let buf_atom = allocator.atom(capture); + selections.insert(buf_atom.as_ref().to_vec(), tail); let newsub = formulate_path_selections_for_destructuring_arg( allocator, substructure, diff --git a/src/classic/clvm_tools/stages/stage_2/module.rs b/src/classic/clvm_tools/stages/stage_2/module.rs index 74083616..c4a77b15 100644 --- a/src/classic/clvm_tools/stages/stage_2/module.rs +++ b/src/classic/clvm_tools/stages/stage_2/module.rs @@ -52,7 +52,7 @@ impl CompileOutput { // export type TBuildTree = Bytes | Tuple | []; fn build_tree(allocator: &mut Allocator, items: &[Vec]) -> Result { if items.is_empty() { - Ok(allocator.null()) + Ok(NodePtr::NIL) } else if items.len() == 1 { allocator.new_atom(&items[0]) } else { @@ -72,7 +72,7 @@ fn build_tree_program(allocator: &mut Allocator, items: &[NodePtr]) -> Result>>(); if !matching_args.is_empty() { @@ -293,8 +294,8 @@ fn defun_inline_to_macro( None } }) - .filter(|x| !x.is_empty()) - .map(|v| v.to_vec()) + .filter(|x| !x.as_ref().is_empty()) + .map(|v| v.as_ref().to_vec()) .collect::>>(); let unquoted_code = unquote_args(allocator, code, &arg_name_list, &destructure_matches)?; @@ -326,12 +327,18 @@ fn parse_mod_sexp( let op = match allocator.sexp(op_node) { // op_node in use. - SExp::Atom => allocator.atom(op_node).to_vec(), + SExp::Atom => { + let atom = allocator.atom(op_node); + atom.as_ref().to_vec() + } _ => Vec::new(), }; let name = match allocator.sexp(name_node) { // name_node in use. - SExp::Atom => allocator.atom(name_node).to_vec(), + SExp::Atom => { + let atom = allocator.atom(name_node); + atom.as_ref().to_vec() + } _ => Vec::new(), }; @@ -462,7 +469,7 @@ fn compile_mod_stage_1( let main_list = enlist( allocator, - &[allocator.null(), *delayed_body] + &[NodePtr::NIL, *delayed_body] )?; result_collection.functions.insert( @@ -499,7 +506,7 @@ fn compile_mod_stage_1( run_program.run_program( allocator, compiled, - allocator.null(), + NodePtr::NIL, None )?; @@ -507,7 +514,7 @@ fn compile_mod_stage_1( run_program.run_program( allocator, compilation_result.1, - allocator.null(), + NodePtr::NIL, None )?; @@ -518,11 +525,11 @@ fn compile_mod_stage_1( } if !processed { - return Err(EvalErr(allocator.null(), "got stuck untangling defconst dependencies".to_string())); + return Err(EvalErr(NodePtr::NIL, "got stuck untangling defconst dependencies".to_string())); } } - let uncompiled_main = nonempty_last(allocator.null(), &alist)?; + let uncompiled_main = nonempty_last(NodePtr::NIL, &alist)?; let main_list = enlist( allocator, @@ -835,7 +842,7 @@ pub fn compile_mod( ) -> Result { // Deal with the "mod" keyword. let produce_extra_info_prog = assemble(allocator, "(_symbols_extra_info)")?; - let produce_extra_info_null = allocator.null(); + let produce_extra_info_null = NodePtr::NIL; let extra_info_res = run_program.run_program( allocator, produce_extra_info_prog, diff --git a/src/classic/clvm_tools/stages/stage_2/operators.rs b/src/classic/clvm_tools/stages/stage_2/operators.rs index 5b433395..36e1ecdd 100644 --- a/src/classic/clvm_tools/stages/stage_2/operators.rs +++ b/src/classic/clvm_tools/stages/stage_2/operators.rs @@ -146,7 +146,7 @@ impl CompilerOperatorsInternal { if self.symbols_extra_info { Ok(Reduction(1, allocator.new_atom(&[1])?)) } else { - Ok(Reduction(1, allocator.null())) + Ok(Reduction(1, NodePtr::NIL)) } } @@ -195,7 +195,7 @@ impl CompilerOperatorsInternal { // return EvalErr. let parse_file_content = |allocator: &mut Allocator, content: &String| { read_ir(content) - .map_err(|e| EvalErr(allocator.null(), e.to_string())) + .map_err(|e| EvalErr(NodePtr::NIL, e.to_string())) .and_then(|ir| { assemble_from_ir(allocator, Rc::new(ir)).map(|ir_sexp| Reduction(1, ir_sexp)) }) @@ -204,8 +204,9 @@ impl CompilerOperatorsInternal { match allocator.sexp(sexp) { SExp::Pair(f, _) => match allocator.sexp(f) { SExp::Atom => { + let atom = allocator.atom(f); let filename = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(f).to_vec()))).decode(); + Bytes::new(Some(BytesFromType::Raw(atom.as_ref().to_vec()))).decode(); // Use the read interface in CompilerOpts if we have one. if let Some(opts) = self.get_compiler_opts() { if let Ok((_, content)) = @@ -218,16 +219,13 @@ impl CompilerOperatorsInternal { // Use the filesystem like normal if the opts couldn't find // the file. fs::read_to_string(filename) - .map_err(|_| EvalErr(allocator.null(), "Failed to read file".to_string())) + .map_err(|_| EvalErr(NodePtr::NIL, "Failed to read file".to_string())) .and_then(|content| parse_file_content(allocator, &content)) } - _ => Err(EvalErr( - allocator.null(), - "filename is not an atom".to_string(), - )), + _ => Err(EvalErr(NodePtr::NIL, "filename is not an atom".to_string())), }, _ => Err(EvalErr( - allocator.null(), + NodePtr::NIL, "given a program that is an atom".to_string(), )), } @@ -237,9 +235,9 @@ impl CompilerOperatorsInternal { if let SExp::Pair(filename_sexp, r) = allocator.sexp(sexp) { if let SExp::Pair(data, _) = allocator.sexp(r) { if let SExp::Atom = allocator.sexp(filename_sexp) { - let filename_buf = allocator.atom(filename_sexp); + let filename_atom = allocator.atom(filename_sexp); let filename_bytes = - Bytes::new(Some(BytesFromType::Raw(filename_buf.to_vec()))); + Bytes::new(Some(BytesFromType::Raw(filename_atom.as_ref().to_vec()))); let ir = disassemble_to_ir_with_kw( allocator, data, @@ -252,7 +250,7 @@ impl CompilerOperatorsInternal { .map_err(|_| { EvalErr(sexp, format!("failed to write {}", filename_bytes.decode())) }) - .map(|_| Reduction(1, allocator.null())); + .map(|_| Reduction(1, NodePtr::NIL)); } } } @@ -266,7 +264,7 @@ impl CompilerOperatorsInternal { } fn get_include_paths(&self, allocator: &mut Allocator) -> Response { - let mut converted_search_paths = allocator.null(); + let mut converted_search_paths = NodePtr::NIL; for s in self.search_paths.iter().rev() { let search_path_string = allocator.new_atom(s.as_bytes())?; converted_search_paths = @@ -285,8 +283,9 @@ impl CompilerOperatorsInternal { if let SExp::Pair(l, _r) = allocator.sexp(sexp) { if let SExp::Atom = allocator.sexp(l) { // l most relevant in scope. + let atom = allocator.atom(l); let filename = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(l).to_vec()))).decode(); + Bytes::new(Some(BytesFromType::Raw(atom.as_ref().to_vec()))).decode(); // If we have a compiler opts injected, let that handle reading // files. The name will bubble up to the _read function. if self.get_compiler_opts().is_some() { @@ -319,11 +318,13 @@ impl CompilerOperatorsInternal { if let SExp::Pair(hash, name) = allocator.sexp(*kv) { if let (SExp::Atom, SExp::Atom) = (allocator.sexp(hash), allocator.sexp(name)) { // hash and name in scope. + let hash_atom = allocator.atom(hash); let hash_text = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(hash).to_vec()))) + Bytes::new(Some(BytesFromType::Raw(hash_atom.as_ref().to_vec()))) .decode(); + let name_atom = allocator.atom(name); let name_text = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(name).to_vec()))) + Bytes::new(Some(BytesFromType::Raw(name_atom.as_ref().to_vec()))) .decode(); self.compile_outcomes.replace_with(|co| { @@ -336,7 +337,7 @@ impl CompilerOperatorsInternal { } } - Ok(Reduction(1, allocator.null())) + Ok(Reduction(1, NodePtr::NIL)) } fn get_disassembly_ver(&self) -> usize { @@ -347,16 +348,16 @@ impl CompilerOperatorsInternal { } impl Dialect for CompilerOperatorsInternal { - fn quote_kw(&self) -> &[u8] { - &[1] + fn quote_kw(&self) -> u32 { + 1 } - fn apply_kw(&self) -> &[u8] { - &[2] + fn apply_kw(&self) -> u32 { + 2 } - fn softfork_kw(&self) -> &[u8] { - &[36] + fn softfork_kw(&self) -> u32 { + 36 } // The softfork operator comes with an extension argument. @@ -387,26 +388,27 @@ impl Dialect for CompilerOperatorsInternal { match allocator.sexp(op) { SExp::Atom => { // use of op obvious. - let opbuf = allocator.atom(op); - if opbuf == "_read".as_bytes() { + let op_atom = allocator.atom(op); + let opbuf = op_atom.as_ref(); + if opbuf == b"_read" { self.read(allocator, sexp) - } else if opbuf == "_write".as_bytes() { + } else if opbuf == b"_write" { self.write(allocator, sexp) - } else if opbuf == "com".as_bytes() { + } else if opbuf == b"com" { do_com_prog_for_dialect(self.get_runner(), allocator, sexp) - } else if opbuf == "opt".as_bytes() { + } else if opbuf == b"opt" { do_optimize(self.get_runner(), allocator, &self.opt_memo, sexp) - } else if opbuf == "_set_symbol_table".as_bytes() { + } else if opbuf == b"_set_symbol_table" { self.set_symbol_table(allocator, sexp) - } else if opbuf == "_get_compile_filename".as_bytes() { + } else if opbuf == b"_get_compile_filename" { self.get_compile_filename(allocator) - } else if opbuf == "_get_include_paths".as_bytes() { + } else if opbuf == b"_get_include_paths" { self.get_include_paths(allocator) - } else if opbuf == "_full_path_for_name".as_bytes() { + } else if opbuf == b"_full_path_for_name" { self.get_full_path_for_filename(allocator, sexp) - } else if opbuf == "_symbols_extra_info".as_bytes() { + } else if opbuf == b"_symbols_extra_info" { self.symbols_extra_info(allocator) - } else if opbuf == "_get_source_file".as_bytes() { + } else if opbuf == b"_get_source_file" { self.get_source_file(allocator) } else { self.base_dialect.op( diff --git a/src/classic/clvm_tools/stages/stage_2/optimize.rs b/src/classic/clvm_tools/stages/stage_2/optimize.rs index f48d2a14..c7fdce41 100644 --- a/src/classic/clvm_tools/stages/stage_2/optimize.rs +++ b/src/classic/clvm_tools/stages/stage_2/optimize.rs @@ -42,7 +42,7 @@ pub fn seems_constant_tail(allocator: &mut Allocator, sexp_: NodePtr) -> bool { sexp = r; } SExp::Atom => { - return sexp == allocator.null(); + return sexp == NodePtr::NIL; } } } @@ -51,16 +51,16 @@ pub fn seems_constant_tail(allocator: &mut Allocator, sexp_: NodePtr) -> bool { pub fn seems_constant(allocator: &mut Allocator, sexp: NodePtr) -> bool { match allocator.sexp(sexp) { SExp::Atom => { - return sexp == allocator.null(); + return sexp == NodePtr::NIL; } SExp::Pair(operator, r) => { match allocator.sexp(operator) { SExp::Atom => { // Was buf of operator. let atom = allocator.atom(operator); - if atom.len() == 1 && atom[0] == 1 { + if atom.as_ref().len() == 1 && atom.as_ref()[0] == 1 { return true; - } else if atom.len() == 1 && atom[0] == 8 { + } else if atom.as_ref().len() == 1 && atom.as_ref()[0] == 8 { return false; } } @@ -95,7 +95,7 @@ pub fn constant_optimizer( // first relevant in scope. if let SExp::Atom = allocator.sexp(first) { let buf = allocator.atom(first); - if buf.len() == 1 && buf[0] == 1 { + if buf.as_ref().len() == 1 && buf.as_ref()[0] == 1 { // Short circuit already quoted expression. return Ok(r); } @@ -117,7 +117,7 @@ pub fn constant_optimizer( res <- runner.run_program( allocator, r, - allocator.null(), + NodePtr::NIL, None ); let r1 = res.1; @@ -140,7 +140,7 @@ pub fn is_args_call(allocator: &Allocator, r: NodePtr) -> bool { if let SExp::Atom = allocator.sexp(r) { // Only r in scope. let buf = allocator.atom(r); - buf.len() == 1 && buf[0] == 1 + buf.as_ref().len() == 1 && buf.as_ref()[0] == 1 } else { false } @@ -192,7 +192,7 @@ fn cons_f(allocator: &mut Allocator, args: NodePtr) -> Result } else { m! { first_atom <- allocator.new_atom(&[5]); - tail <- allocator.new_pair(args, allocator.null()); + tail <- allocator.new_pair(args, NodePtr::NIL); allocator.new_pair(first_atom, tail) } } @@ -207,7 +207,7 @@ fn cons_r(allocator: &mut Allocator, args: NodePtr) -> Result } else { m! { rest_atom <- allocator.new_atom(&[6]); - tail <- allocator.new_pair(args, allocator.null()); + tail <- allocator.new_pair(args, NodePtr::NIL); allocator.new_pair(rest_atom, tail) } } @@ -222,7 +222,8 @@ fn path_from_args( match allocator.sexp(sexp) { SExp::Atom => { // Only sexp in scope. - let v = number_from_u8(allocator.atom(sexp)); + let atom = allocator.atom(sexp); + let v = number_from_u8(atom.as_ref()); if v <= bi_one() { Ok(new_args) } else { @@ -257,7 +258,7 @@ pub fn sub_args( SExp::Atom => { // Atom is a reflection of first_pre. let atom = allocator.atom(first_pre); - if atom.len() == 1 && atom[0] == 1 { + if atom.as_ref().len() == 1 && atom.as_ref()[0] == 1 { return Ok(sexp); } else { first = first_pre; @@ -377,7 +378,8 @@ pub fn var_change_optimizer_cons_eval( SExp::Atom => { // Atom reflects val_first. let vf_buf = allocator.atom(val_first); - (vf_buf.len() != 1 || vf_buf[0] != 1) as i32 + (vf_buf.as_ref().len() != 1 || vf_buf.as_ref()[0] != 1) + as i32 } _ => 0, }, @@ -420,7 +422,8 @@ pub fn children_optimizer( return Ok(r); } if let SExp::Atom = allocator.sexp(list[0]) { - if allocator.atom(list[0]).to_vec() == vec![1] { + let atom = allocator.atom(list[0]); + if atom.as_ref().to_vec() == vec![1] { return Ok(r); } } @@ -569,7 +572,7 @@ fn quote_null_optimizer( // This applies the transform `(q . 0)` => `0` let t1 = match_sexp(allocator, quote_pattern_1, r, HashMap::new()); - Ok(t1.map(|_| allocator.null()).unwrap_or_else(|| r)) + Ok(t1.map(|_| NodePtr::NIL).unwrap_or_else(|| r)) } fn apply_null_pattern_1(allocator: &mut Allocator) -> NodePtr { @@ -586,7 +589,7 @@ fn apply_null_optimizer( // This applies the transform `(a 0 ARGS)` => `0` let t1 = match_sexp(allocator, apply_null_pattern_1, r, HashMap::new()); - Ok(t1.map(|_| allocator.null()).unwrap_or_else(|| r)) + Ok(t1.map(|_| NodePtr::NIL).unwrap_or_else(|| r)) } struct OptimizerRunner<'a> { diff --git a/src/classic/clvm_tools/stages/stage_2/reader.rs b/src/classic/clvm_tools/stages/stage_2/reader.rs index 716988f7..5f6f05d0 100644 --- a/src/classic/clvm_tools/stages/stage_2/reader.rs +++ b/src/classic/clvm_tools/stages/stage_2/reader.rs @@ -32,7 +32,7 @@ pub fn convert_hex_to_sexp( let content_bytes = Bytes::new_validated(Some(UnvalidatedBytesFromType::Hex(decode_string( file_data, )))) - .map_err(|e| EvalErr(allocator.null(), e.to_string()))?; + .map_err(|e| EvalErr(NodePtr::NIL, e.to_string()))?; let mut reader_stream = Stream::new(Some(content_bytes)); Ok(sexp_from_stream( allocator, @@ -97,9 +97,12 @@ pub fn process_embed_file( ) { // Note: we don't want to keep borrowing here because we // need the mutable borrow below. - let name_buf = allocator.atom(l[0]).to_vec(); - let kind_buf = allocator.atom(l[1]); - let filename_buf = allocator.atom(l[2]).to_vec(); + let name_atom = allocator.atom(l[0]); + let kind_atom = allocator.atom(l[1]); + let filename_atom = allocator.atom(l[2]); + let name_buf = name_atom.as_ref().to_vec(); + let kind_buf = kind_atom.as_ref().to_vec(); + let filename_buf = filename_atom.as_ref().to_vec(); let file_data = if kind_buf == b"bin" { let file = read_file( runner, diff --git a/src/compiler/cldb.rs b/src/compiler/cldb.rs index 82d715fd..f0985f0d 100644 --- a/src/compiler/cldb.rs +++ b/src/compiler/cldb.rs @@ -608,12 +608,8 @@ fn hex_to_modern_sexp_inner( hex_to_modern_sexp_inner(allocator, symbol_table, srcloc.clone(), a)?, hex_to_modern_sexp_inner(allocator, symbol_table, srcloc, b)?, ))), - _ => convert_from_clvm_rs(allocator, srcloc, program).map_err(|_| { - EvalErr( - Allocator::null(allocator), - "clvm_rs allocator failed".to_string(), - ) - }), + _ => convert_from_clvm_rs(allocator, srcloc, program) + .map_err(|_| EvalErr(NodePtr::NIL, "clvm_rs allocator failed".to_string())), } } diff --git a/src/compiler/clvm.rs b/src/compiler/clvm.rs index 2d01ee41..c70f1e39 100644 --- a/src/compiler/clvm.rs +++ b/src/compiler/clvm.rs @@ -255,7 +255,7 @@ pub fn convert_to_clvm_rs( head: Rc, ) -> Result { match head.borrow() { - SExp::Nil(_) => Ok(allocator.null()), + SExp::Nil(_) => Ok(NodePtr::NIL), SExp::Atom(_l, x) => allocator .new_atom(x) .map_err(|_e| RunFailure::RunErr(head.loc(), format!("failed to alloc atom {head}"))), @@ -264,7 +264,7 @@ pub fn convert_to_clvm_rs( .map_err(|_e| RunFailure::RunErr(head.loc(), format!("failed to alloc string {head}"))), SExp::Integer(_, i) => { if NewStyleIntConversion::setting() && *i == bi_zero() { - Ok(allocator.null()) + Ok(NodePtr::NIL) } else { allocator .new_atom(&u8_from_number(i.clone())) @@ -292,7 +292,8 @@ pub fn convert_from_clvm_rs( match allocator.sexp(head) { allocator::SExp::Atom => { let int_conv = NewStyleIntConversion::setting(); - let atom_data = allocator.atom(head); + let atom = allocator.atom(head); + let atom_data = atom.as_ref(); if atom_data.is_empty() { Ok(Rc::new(SExp::Nil(loc))) } else { @@ -339,7 +340,8 @@ fn test_convert_from_clvm_rs_00_byte() { ); let node = convert_to_clvm_rs(&mut allocator, result).expect("should convert from mod"); - assert_eq!(allocator.atom(node), &[0]); + let atom = allocator.atom(node); + assert_eq!(atom.as_ref(), &[0]); } #[test] @@ -357,7 +359,8 @@ fn test_convert_to_clvm_rs_m129() { ); let node = convert_to_clvm_rs(&mut allocator, result).expect("should convert from mod"); - assert_eq!(allocator.atom(node), &[255, 127]); + let atom = allocator.atom(node); + assert_eq!(atom.as_ref(), &[255, 127]); } fn generate_argument_refs(start: Number, sexp: Rc) -> Rc { diff --git a/src/compiler/dialect.rs b/src/compiler/dialect.rs index 4345558c..47e0826e 100644 --- a/src/compiler/dialect.rs +++ b/src/compiler/dialect.rs @@ -110,8 +110,10 @@ fn include_dialect(allocator: &Allocator, e: &[NodePtr]) -> Option(allocator: &'a mut Allocator, src: String) -> NodePtr { let ir = read_ir(&src).unwrap(); let assembled = assemble_from_ir(allocator, Rc::new(ir)).unwrap(); let runner = DefaultProgramRunner::new(); - let null = allocator.null(); let res = runner - .run_program(allocator, assembled, null, None) + .run_program(allocator, assembled, NodePtr::NIL, None) .unwrap(); return res.1; } @@ -262,7 +261,7 @@ fn compile_program<'a>( let runner = run_program_for_search_paths("*test*", &vec![include_path], false); let input_ir = read_ir(&src); let input_program = assemble_from_ir(allocator, Rc::new(input_ir.unwrap())).unwrap(); - let input_sexp = allocator.new_pair(input_program, allocator.null()).unwrap(); + let input_sexp = allocator.new_pair(input_program, NodePtr::NIL).unwrap(); let res = runner.run_program(allocator, run_script, input_sexp, None); return res.map(|x| disassemble(allocator, x.1, Some(0))); @@ -298,8 +297,8 @@ fn can_run_from_source_nil() { let res = run_from_source(&mut allocator, "()".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 0); + let res_bytes = allocator.atom_len(res); + assert_eq!(res_bytes, 0); } _ => { assert_eq!("expected atom", ""); @@ -313,8 +312,8 @@ fn can_echo_quoted_nil() { let res = run_from_source(&mut allocator, "(1)".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 0); + let res_bytes = allocator.atom_len(res); + assert_eq!(res_bytes, 0); } _ => { assert_eq!("expected atom", ""); @@ -325,12 +324,11 @@ fn can_echo_quoted_nil() { #[test] fn can_echo_quoted() { let mut allocator = Allocator::new(); - let null = allocator.null(); let res = run_from_source(&mut allocator, "(1 ())".to_string()); match allocator.sexp(res) { SExp::Pair(l, r) => { - assert_eq!(l, null); - assert_eq!(r, null); + assert_eq!(l, NodePtr::NIL); + assert_eq!(r, NodePtr::NIL); } _ => { assert_eq!("expected pair", ""); @@ -345,8 +343,8 @@ fn can_echo_quoted_atom() { match allocator.sexp(res) { SExp::Atom => { let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 1); - assert_eq!(res_bytes[0], 3); + assert_eq!(res_bytes.as_ref().len(), 1); + assert_eq!(res_bytes.as_ref()[0], 3); } _ => { assert_eq!("expected atom", ""); @@ -361,8 +359,8 @@ fn can_do_operations() { match allocator.sexp(res) { SExp::Atom => { let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 1); - assert_eq!(res_bytes[0], 8); + assert_eq!(res_bytes.as_ref().len(), 1); + assert_eq!(res_bytes.as_ref()[0], 8); } _ => { assert_eq!("expected atom", ""); @@ -377,8 +375,8 @@ fn can_do_operations_kw() { match allocator.sexp(res) { SExp::Atom => { let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 1); - assert_eq!(res_bytes[0], 8); + assert_eq!(res_bytes.as_ref().len(), 1); + assert_eq!(res_bytes.as_ref()[0], 8); } _ => { assert_eq!("expected atom", ""); diff --git a/src/tests/compiler/clvm.rs b/src/tests/compiler/clvm.rs index af4595f9..e1d54e94 100644 --- a/src/tests/compiler/clvm.rs +++ b/src/tests/compiler/clvm.rs @@ -262,7 +262,8 @@ fn test_integer_0_to_clvm() { let mut allocator = Allocator::new(); let clvm_target = convert_to_clvm_rs(&mut allocator, value).expect("ok"); let empty: [u8; 0] = []; - assert_eq!(allocator.atom(clvm_target), &empty); + let atom = allocator.atom(clvm_target); + assert_eq!(atom.as_ref(), &empty); } #[test] diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index ce275c35..9dd44066 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -18,7 +18,7 @@ path = "src/mod.rs" [dependencies] clvm_tools_rs = { path= "..", features = [] } -clvmr = { version = "0.3.0", features = ["pre-eval"] } +clvmr = { version = "0.8.0", features = ["pre-eval"] } wasm-bindgen = "=0.2.92" wasm-bindgen-test = "=0.3.25" js-sys = "0.3.60"