Skip to content

Commit f5727c4

Browse files
committed
feat: handle std functions
1 parent 12be327 commit f5727c4

File tree

2 files changed

+301
-88
lines changed

2 files changed

+301
-88
lines changed

components/ts-to-clar/src/clarity_std.rs

Lines changed: 151 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
use clarity::vm::functions::NativeFunctions;
2-
use std::sync::OnceLock;
1+
use clarity::vm::{functions::NativeFunctions, types::TypeSignature};
2+
use std::{collections::HashMap, sync::LazyLock};
33

4-
pub static STD_PKG_NAME: &str = "@hirosystems/clarity-std";
4+
pub static STD_PKG_NAME: &str = "clarity";
5+
6+
pub static KEYWORDS_TYPES: LazyLock<HashMap<&str, (&str, TypeSignature)>> = LazyLock::new(|| {
7+
use clarity::vm::types::TypeSignature::*;
8+
HashMap::from([
9+
// chain
10+
("chainId", ("chain-id", UIntType)),
11+
("isInMainnet", ("is-in-mainnet", BoolType)),
12+
("isInRegtest", ("is-in-regtest", BoolType)),
13+
// heights
14+
("burnBlockHeight", ("burn-block-height", UIntType)),
15+
("stacksBlockHeight", ("stacks-block-height", UIntType)),
16+
("tenureHeight", ("tenure-height", UIntType)),
17+
// call er context
18+
("contractCaller", ("contract-caller", PrincipalType)),
19+
("txSender", ("tx-sender", PrincipalType)),
20+
(
21+
"txSponsor",
22+
("tx-sponsor?", OptionalType(PrincipalType.into())),
23+
),
24+
// stx
25+
("stxLiquidSupply", ("stx-liquid-supply", UIntType)),
26+
])
27+
});
528

629
#[allow(dead_code)]
730
pub fn get_std() -> Vec<String> {
@@ -46,21 +69,139 @@ pub fn get_std() -> Vec<String> {
4669
.collect::<Vec<String>>()
4770
}
4871

49-
static STD_FUNCTIONS: OnceLock<Vec<String>> = OnceLock::new();
72+
/*
73+
std: [
74+
// done
75+
"to-int",
76+
"to-uint",
77+
78+
// todo
79+
"get-stacks-block-info?",
80+
"get-tenure-info?",
81+
"get-burn-block-info?",
82+
"to-consensus-buff?",
83+
"from-consensus-buff?",
84+
85+
// to consider
86+
"buff-to-int-le",
87+
"buff-to-uint-le",
88+
"buff-to-int-be",
89+
"buff-to-uint-be",
90+
"sha256",
91+
"sha512",
92+
"sha512/256",
93+
"keccak256",
94+
"secp256k1-recover?",
95+
"secp256k1-verify",
96+
"print",
97+
98+
// not todo
99+
"map",
100+
"fold",
101+
"append",
102+
"concat",
103+
"as-max-len?",
104+
"len",
105+
"element-at",
106+
"index-of",
107+
"is-standard",
108+
"principal-destruct?",
109+
"principal-construct?",
110+
"string-to-int?",
111+
"string-to-uint?",
112+
"int-to-ascii",
113+
"int-to-utf8",
114+
"hash160",
115+
"contract-call?",
116+
"as-contract",
117+
"contract-of",
118+
"principal-of?",
119+
"at-block",
120+
"get-block-info?",
121+
"err",
122+
"ok",
123+
"some",
124+
"default-to",
125+
"asserts!",
126+
"unwrap!",
127+
"unwrap-err!",
128+
"unwrap-panic",
129+
"unwrap-err-panic",
130+
"match",
131+
"try!",
132+
"is-ok",
133+
"is-none",
134+
"is-err",
135+
"is-some",
136+
"filter",
137+
"stx-get-balance",
138+
"stx-transfer?",
139+
"stx-transfer-memo?",
140+
"stx-burn?",
141+
"stx-account",
142+
"bit-shift-left",
143+
"bit-shift-right",
144+
"bit-xor",
145+
"slice?",
146+
"replace-at?",
147+
] */
50148

51149
#[allow(dead_code)]
52-
pub fn get_std_functions() -> &'static Vec<String> {
53-
STD_FUNCTIONS.get_or_init(get_std)
150+
pub enum Parameter {
151+
Any,
152+
Value(TypeSignature),
153+
Identifiers(Vec<String>),
154+
}
155+
156+
pub struct StdFunction {
157+
pub name: &'static str,
158+
pub _parameters: Vec<Parameter>,
159+
// return type might not be needed at all in transpiler at some point
160+
// because the type might just be avaible in the ts ast
161+
// making it Optional instead of overengineering it for now
162+
pub _return_type: Option<TypeSignature>,
54163
}
55164

165+
pub static FUNCTIONS: LazyLock<HashMap<&str, StdFunction>> = LazyLock::new(|| {
166+
use NativeFunctions::*;
167+
use TypeSignature::*;
168+
169+
HashMap::from([
170+
(
171+
"toInt",
172+
StdFunction {
173+
name: ToInt.get_name_str(),
174+
_parameters: vec![Parameter::Value(UIntType)],
175+
_return_type: Some(IntType),
176+
},
177+
),
178+
(
179+
"toUint",
180+
StdFunction {
181+
name: ToUInt.get_name_str(),
182+
_parameters: vec![Parameter::Value(IntType)],
183+
_return_type: Some(UIntType),
184+
},
185+
),
186+
(
187+
"print",
188+
StdFunction {
189+
name: Print.get_name_str(),
190+
_parameters: vec![Parameter::Any],
191+
_return_type: None,
192+
},
193+
),
194+
])
195+
});
196+
56197
#[cfg(test)]
57198
mod tests {
58199
use super::*;
59200

60201
#[test]
61-
fn test_get_std() {
62-
let functions = get_std_functions();
63-
// println!("std: {:#?}", functions);
64-
assert_eq!(functions.len(), 66);
202+
fn test_functions() {
203+
// let functions = FUNCTIONS;
204+
205+
// println!("Functions: {:?}", FUNCTIONS.keys().collect::<Vec<_>>());
65206
}
66207
}

0 commit comments

Comments
 (0)