-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added selector inline macro for starknet.
commit-id:5bae488f
- Loading branch information
Showing
17 changed files
with
88 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod selector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, InlinePluginResult, PluginDiagnostic}; | ||
use cairo_lang_semantic::inline_macros::unsupported_bracket_diagnostic; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{ast, TypedSyntaxNode}; | ||
|
||
use crate::contract::starknet_keccak; | ||
|
||
/// Macro for expanding a selector to a string literal. | ||
#[derive(Debug, Default)] | ||
pub struct SelectorMacro; | ||
impl SelectorMacro { | ||
pub const NAME: &'static str = "selector"; | ||
} | ||
impl InlineMacroExprPlugin for SelectorMacro { | ||
fn generate_code( | ||
&self, | ||
db: &dyn SyntaxGroup, | ||
syntax: &ast::ExprInlineMacro, | ||
) -> InlinePluginResult { | ||
let ast::WrappedExprList::ParenthesizedExprList(args) = syntax.arguments(db) else { | ||
return unsupported_bracket_diagnostic(db, syntax); | ||
}; | ||
|
||
let arguments = &args.expressions(db).elements(db); | ||
if arguments.len() != 1 { | ||
let diagnostics = vec![PluginDiagnostic { | ||
stable_ptr: syntax.stable_ptr().untyped(), | ||
message: "selector macro must have a single argument".to_string(), | ||
}]; | ||
return InlinePluginResult { code: None, diagnostics }; | ||
} | ||
|
||
let ast::Expr::String(input_string) = &arguments[0] else { | ||
let diagnostics = vec![PluginDiagnostic { | ||
stable_ptr: syntax.stable_ptr().untyped(), | ||
message: "selector macro argument must be a string".to_string(), | ||
}]; | ||
return InlinePluginResult { code: None, diagnostics }; | ||
}; | ||
let selector_string = input_string.string_value(db).unwrap(); | ||
|
||
let selector = starknet_keccak(selector_string.as_bytes()); | ||
let code: String = format!("0x{}", selector.to_str_radix(16)); | ||
InlinePluginResult { code: Some(code), diagnostics: vec![] } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.