-
Notifications
You must be signed in to change notification settings - Fork 62
feat(compiler): support registered host-await builtins for natural function call syntax #667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kusha
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
kusha:markbirger/registered-host-await-builtins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e359349
feat(compiler): support registered host-await builtins
2864312
Update src/languages/rego/compiler/function_calls.rs
kusha f7e0ec0
fix(compiler): address PR #667 review feedback on host-await registra…
357fbf3
compiler: split CallTarget::HostAwait into explicit and registered va…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ use crate::lexer::Span; | |
| use crate::rvm::instructions::{BuiltinCallParams, FunctionCallParams}; | ||
| use crate::rvm::Instruction; | ||
| use crate::utils::get_path_string; | ||
| use crate::value::Value; | ||
| use alloc::{format, string::ToString, vec::Vec}; | ||
|
|
||
| enum CallTarget { | ||
|
|
@@ -127,21 +128,50 @@ impl<'a> Compiler<'a> { | |
| self.emit_instruction(Instruction::BuiltinCall { params_index }, &span); | ||
| } | ||
| CallTarget::HostAwait { .. } => { | ||
| if arg_regs.len() != 2 { | ||
| return Err(CompilerError::General { | ||
| message: format!( | ||
| "__builtin_host_await expects 2 arguments, got {}", | ||
| arg_regs.len() | ||
| ), | ||
| let (arg_reg, id_reg) = if original_fcn_path == "__builtin_host_await" { | ||
| // Explicit __builtin_host_await(arg, id) — 2 arguments | ||
| if arg_regs.len() != 2 { | ||
| return Err(CompilerError::General { | ||
| message: format!( | ||
| "__builtin_host_await expects 2 arguments, got {}", | ||
| arg_regs.len() | ||
| ), | ||
| } | ||
| .at(&span)); | ||
| } | ||
| .at(&span)); | ||
| } | ||
| (arg_regs[0], arg_regs[1]) | ||
| } else { | ||
| // Registered host-awaitable builtin — identifier is the function name | ||
| if arg_regs.len() != 1 { | ||
| return Err(CompilerError::General { | ||
| message: format!( | ||
| "host-awaitable builtin '{}' expects exactly 1 argument, got {}", | ||
| original_fcn_path, | ||
| arg_regs.len() | ||
| ), | ||
| } | ||
| .at(&span)); | ||
| } | ||
| let id_reg = self.alloc_register(); | ||
| let literal_idx = self.add_literal(Value::String(original_fcn_path.into())); | ||
| self.emit_instruction( | ||
| Instruction::Load { | ||
| dest: id_reg, | ||
| literal_idx, | ||
| }, | ||
| &span, | ||
| ); | ||
| // HostAwait carries a single arg register; registered builtins | ||
| // are restricted to arg_count == 1 at registration time, so | ||
| // arg_regs[0] is the only argument. | ||
| (arg_regs[0], id_reg) | ||
| }; | ||
|
|
||
| self.emit_instruction( | ||
| Instruction::HostAwait { | ||
| dest, | ||
| arg: arg_regs[0], | ||
| id: arg_regs[1], | ||
| arg: arg_reg, | ||
| id: id_reg, | ||
| }, | ||
| &span, | ||
| ); | ||
|
|
@@ -192,6 +222,13 @@ impl<'a> Compiler<'a> { | |
| }); | ||
| } | ||
|
|
||
| // Check registered host-awaitable builtins | ||
| if let Some(&arg_count) = self.host_await_builtins.get(original_fcn_path) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to detect shadowing here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment above |
||
| return Ok(CallTarget::HostAwait { | ||
|
anakrish marked this conversation as resolved.
Outdated
|
||
| expected_args: Some(arg_count), | ||
| }); | ||
| } | ||
|
|
||
| if self.is_user_defined_function(full_fcn_path) { | ||
| let rule_index = self.get_or_assign_rule_index(full_fcn_path)?; | ||
| let expected_args = self | ||
|
|
||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to define behavior when registered host await builtin shadows a user write policy rule (both regular and function)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move it out of scope for this PR? Shadowing already happens for
__builtin_host_await. Also we need to decide on approach. IMO compilation check is not great choice as it makes compiled policies dependent on engine state. The easiest would be to prioritize user defined if it is present.