-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow with modifier for builtin and user functions (#42)
Signed-off-by: Anand Krishnamoorthi <[email protected]>
- Loading branch information
Showing
9 changed files
with
216 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use crate::ast::Expr; | ||
use crate::builtins; | ||
use crate::builtins::utils::ensure_args_count; | ||
use crate::lexer::Span; | ||
use crate::value::Value; | ||
|
||
use std::collections::HashMap; | ||
use std::time::SystemTime; | ||
|
||
use anyhow::{bail, Result}; | ||
|
||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) { | ||
m.insert("time.now_ns", (now_ns, 0)); | ||
} | ||
|
||
fn now_ns(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> { | ||
let name = "time.now_ns"; | ||
ensure_args_count(span, name, params, args, 0)?; | ||
|
||
let now = SystemTime::now(); | ||
let elapsed = match now.duration_since(SystemTime::UNIX_EPOCH) { | ||
Ok(e) => e, | ||
Err(e) => bail!(span.error(format!("could not fetch elapsed time. {e}").as_str())), | ||
}; | ||
let nanos = elapsed.as_nanos(); | ||
Ok(Value::from_u128(nanos)) | ||
} |
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.