mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Use Rc<str> instead of string. (#52)
This reduces std::mem::size_of::<Value>() to 16 bytes. String values are also efficiently copied like Objects, arrays, sets etc. When multiple function rules are evaluated, ensure that constant argument values match before running the rule. If actual parameter does not match the constant parameter, then the rule is skipped. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
4db2270dcf
commit
3514594c56
@@ -12,7 +12,7 @@ use test_generator::test_resources;
|
||||
pub fn process_value(v: &Value) -> Result<Value> {
|
||||
match v {
|
||||
// Handle Undefined encoded as a string "#undefined"
|
||||
Value::String(s) if s == "#undefined" => Ok(Value::Undefined),
|
||||
Value::String(s) if s.as_ref() == "#undefined" => Ok(Value::Undefined),
|
||||
|
||||
// Handle set encoded as an object
|
||||
// set! :
|
||||
|
||||
+17
-17
@@ -20,7 +20,7 @@ macro_rules! my_assert_eq {
|
||||
}
|
||||
|
||||
fn skip_value(v: &Value) -> bool {
|
||||
matches!(v, Value::String(s) if s == "--skip--")
|
||||
matches!(v, Value::String(s) if s.as_ref() == "--skip--")
|
||||
}
|
||||
|
||||
fn match_span(s: &Span, v: &Value) -> Result<()> {
|
||||
@@ -28,7 +28,7 @@ fn match_span(s: &Span, v: &Value) -> Result<()> {
|
||||
Value::String(vs) => {
|
||||
my_assert_eq!(
|
||||
*s.text(),
|
||||
vs,
|
||||
vs.as_ref(),
|
||||
"{}",
|
||||
s.source
|
||||
.message(s.line, s.col, "match-error", "mismatch happened here.")
|
||||
@@ -151,7 +151,7 @@ fn match_expr_impl(e: &Expr, v: &Value) -> Result<()> {
|
||||
Expr::UnaryExpr { span, expr } => {
|
||||
match_span_opt(span, &v["span"])?;
|
||||
my_assert_eq!(
|
||||
&Value::String("-".to_owned()),
|
||||
&Value::String("-".into()),
|
||||
&v["op"],
|
||||
"{}",
|
||||
span.source.message(
|
||||
@@ -324,8 +324,8 @@ fn match_expr_opt(s: &Span, e: &Option<Ref<Expr>>, v: &Value) -> Result<()> {
|
||||
|
||||
fn match_bin_op(s: &Span, op: &BinOp, v: &Value) -> Result<()> {
|
||||
match (op, v) {
|
||||
(BinOp::And, Value::String(s)) if s == "&" => Ok(()),
|
||||
(BinOp::Or, Value::String(s)) if s == "|" => Ok(()),
|
||||
(BinOp::And, Value::String(s)) if s.as_ref() == "&" => Ok(()),
|
||||
(BinOp::Or, Value::String(s)) if s.as_ref() == "|" => Ok(()),
|
||||
_ => bail!(
|
||||
"{}",
|
||||
s.source.message(
|
||||
@@ -340,10 +340,10 @@ fn match_bin_op(s: &Span, op: &BinOp, v: &Value) -> Result<()> {
|
||||
|
||||
fn match_arith_op(s: &Span, op: &ArithOp, v: &Value) -> Result<()> {
|
||||
match (op, v) {
|
||||
(ArithOp::Add, Value::String(s)) if s == "+" => Ok(()),
|
||||
(ArithOp::Sub, Value::String(s)) if s == "-" => Ok(()),
|
||||
(ArithOp::Mul, Value::String(s)) if s == "*" => Ok(()),
|
||||
(ArithOp::Div, Value::String(s)) if s == "/" => Ok(()),
|
||||
(ArithOp::Add, Value::String(s)) if s.as_ref() == "+" => Ok(()),
|
||||
(ArithOp::Sub, Value::String(s)) if s.as_ref() == "-" => Ok(()),
|
||||
(ArithOp::Mul, Value::String(s)) if s.as_ref() == "*" => Ok(()),
|
||||
(ArithOp::Div, Value::String(s)) if s.as_ref() == "/" => Ok(()),
|
||||
_ => bail!(
|
||||
"{}",
|
||||
s.source.message(
|
||||
@@ -358,11 +358,11 @@ fn match_arith_op(s: &Span, op: &ArithOp, v: &Value) -> Result<()> {
|
||||
|
||||
fn match_bool_op(s: &Span, op: &BoolOp, v: &Value) -> Result<()> {
|
||||
match (op, v) {
|
||||
(BoolOp::Lt, Value::String(s)) if s == "<" => Ok(()),
|
||||
(BoolOp::Le, Value::String(s)) if s == "<=" => Ok(()),
|
||||
(BoolOp::Eq, Value::String(s)) if s == "==" => Ok(()),
|
||||
(BoolOp::Ge, Value::String(s)) if s == ">=" => Ok(()),
|
||||
(BoolOp::Gt, Value::String(s)) if s == ">" => Ok(()),
|
||||
(BoolOp::Lt, Value::String(s)) if s.as_ref() == "<" => Ok(()),
|
||||
(BoolOp::Le, Value::String(s)) if s.as_ref() == "<=" => Ok(()),
|
||||
(BoolOp::Eq, Value::String(s)) if s.as_ref() == "==" => Ok(()),
|
||||
(BoolOp::Ge, Value::String(s)) if s.as_ref() == ">=" => Ok(()),
|
||||
(BoolOp::Gt, Value::String(s)) if s.as_ref() == ">" => Ok(()),
|
||||
_ => bail!(
|
||||
"{}",
|
||||
s.source.message(
|
||||
@@ -377,8 +377,8 @@ fn match_bool_op(s: &Span, op: &BoolOp, v: &Value) -> Result<()> {
|
||||
|
||||
fn match_assign_op(s: &Span, op: &AssignOp, v: &Value) -> Result<()> {
|
||||
match (op, v) {
|
||||
(AssignOp::Eq, Value::String(s)) if s == "=" => Ok(()),
|
||||
(AssignOp::ColEq, Value::String(s)) if s == ":=" => Ok(()),
|
||||
(AssignOp::Eq, Value::String(s)) if s.as_ref() == "=" => Ok(()),
|
||||
(AssignOp::ColEq, Value::String(s)) if s.as_ref() == ":=" => Ok(()),
|
||||
_ => bail!(
|
||||
"{}",
|
||||
s.source.message(
|
||||
@@ -470,7 +470,7 @@ fn match_literal(l: &Literal, v: &Value) -> Result<()> {
|
||||
Literal::NotExpr { expr, span } => {
|
||||
let v = &v["notexpr"];
|
||||
match &v["op"] {
|
||||
Value::String(s) if s == "not" => (),
|
||||
Value::String(s) if s.as_ref() == "not" => (),
|
||||
_ => {
|
||||
bail!(
|
||||
"{}",
|
||||
|
||||
+5
-5
@@ -86,7 +86,7 @@ fn display_number() {
|
||||
#[test]
|
||||
fn serialize_string() -> Result<()> {
|
||||
assert_eq!(
|
||||
Value::String("Hello, World\n".to_owned()).to_json_str()?,
|
||||
Value::String("Hello, World\n".into()).to_json_str()?,
|
||||
"\"Hello, World\\n\""
|
||||
);
|
||||
Ok(())
|
||||
@@ -122,7 +122,7 @@ fn value_as_index() -> Result<()> {
|
||||
assert_eq!(&Value::Undefined[&idx], &Value::Undefined);
|
||||
assert_eq!(&Value::Null[&idx], &Value::Undefined);
|
||||
assert_eq!(&Value::Bool(true)[&idx], &Value::Undefined);
|
||||
assert_eq!(&Value::String("Hello".to_owned())[&idx], &Value::Undefined);
|
||||
assert_eq!(&Value::String("Hello".into())[&idx], &Value::Undefined);
|
||||
assert_eq!(&Value::new_set()[&idx], &Value::Undefined);
|
||||
|
||||
Ok(())
|
||||
@@ -151,7 +151,7 @@ fn api() -> Result<()> {
|
||||
assert!(&Value::from_json_str("{}")?.as_object()?.is_empty());
|
||||
let mut v = Value::new_object();
|
||||
v.as_object_mut()?
|
||||
.insert(Value::String("a".to_owned()), Value::from_float(3.145));
|
||||
.insert(Value::String("a".into()), Value::from_float(3.145));
|
||||
assert_eq!(v["a"], Value::from_float(3.145));
|
||||
assert_eq!(v.as_object()?.len(), 1);
|
||||
|
||||
@@ -168,8 +168,8 @@ fn api() -> Result<()> {
|
||||
assert!(Value::Null.as_set().is_err());
|
||||
assert!(Value::Null.as_set_mut().is_err());
|
||||
|
||||
assert!(Value::String("anc".to_owned()).as_array().is_err());
|
||||
assert!(Value::String("anc".to_owned()).as_array_mut().is_err());
|
||||
assert!(Value::String("anc".into()).as_array().is_err());
|
||||
assert!(Value::String("anc".into()).as_array_mut().is_err());
|
||||
|
||||
assert!(Value::new_object().as_number().is_err());
|
||||
assert!(Value::new_object().as_number_mut().is_err());
|
||||
|
||||
Reference in New Issue
Block a user