mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Allow with modifier for builtin and user functions (#42)
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
1c492144b3
commit
a1d0f8576a
+2
-1
@@ -13,6 +13,7 @@ pub mod numbers;
|
||||
mod objects;
|
||||
pub mod sets;
|
||||
mod strings;
|
||||
mod time;
|
||||
mod tracing;
|
||||
pub mod types;
|
||||
mod utils;
|
||||
@@ -51,7 +52,7 @@ lazy_static! {
|
||||
encoding::register(&mut m);
|
||||
//token_signing::register(&mut m);
|
||||
//token_verification::register(&mut m);
|
||||
//time::register(&mut m);
|
||||
time::register(&mut m);
|
||||
//cryptography::register(&mut m);
|
||||
//graphs::register(&mut m);
|
||||
//graphql::register(&mut m);
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user