Skip to content
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

WIP - tremor-script VM #2650

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,5 @@
],
"rust-analyzer.linkedProjects": [
"./Cargo.toml",
"./tremor-connectors/Cargo.toml",
"./tremor-connectors/Cargo.toml",
"./tremor-connectors/Cargo.toml",
"./tremor-connectors-gcp/Cargo.toml",
"./tremor-connectors-gcp/Cargo.toml",
"./tremor-connectors-gcp/Cargo.toml"
]
}
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tremor-script/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'script> BytesPart<'script> {
/// Binary semiliteral
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Bytes<'script> {
mid: Box<NodeMeta>,
pub(crate) mid: Box<NodeMeta>,
/// Bytes
pub value: Vec<BytesPart<'script>>,
}
Expand Down
2 changes: 1 addition & 1 deletion tremor-script/src/ast/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn write_bits_le(
}
}

fn write_bits(
pub(crate) fn write_bits(
bytes: &mut Vec<u8>,
bits: u8,
endianess: Endian,
Expand Down
12 changes: 3 additions & 9 deletions tremor-script/src/ast/visitors/impls/const_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::ast::{BooleanBinExpr, BooleanBinOpKind};
use crate::{
ast::{base_expr::Ranged, binary::extend_bytes_from_value, NodeMeta},
errors::{
err_generic, err_invalid_unary, err_need_int, error_array_out_of_bound, error_bad_key,
error_decreasing_range, error_need_arr, error_need_obj,
err_generic, err_need_int, error_array_out_of_bound, error_bad_key, error_decreasing_range,
error_need_arr, error_need_obj,
},
interpreter::{exec_binary, exec_unary, Env},
lexer::Span,
Expand Down Expand Up @@ -160,13 +160,7 @@ impl<'run, 'script: 'run> ImutExprVisitor<'script> for ConstFolder<'run, 'script
mid,
} = b.as_ref()
{
let value = exec_unary(*kind, value)
.ok_or_else(|| {
let inner = b.extent();
let outer = b.extent();
err_invalid_unary(&outer, &inner, *kind, value)
})?
.into_owned();
let value = exec_unary(b.as_ref(), b.as_ref(), *kind, value)?.into_owned();
Lit(Literal {
mid: mid.clone(),
value,
Expand Down
21 changes: 7 additions & 14 deletions tremor-script/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ impl ErrorKind {
Msg, NoClauseHit, NoConstsAllowed, NoEventReferencesAllowed, NoLocalsAllowed,
NoObjectError, NotConstant, NotFound, Oops, Overflow, ParseIntError, ParserError,
PatchKeyExists, PipelineUnknownPort, QueryNodeDuplicateName, QueryNodeReservedName,
QueryStreamNotDefined, RecursionLimit, RuntimeError, TailingHereDoc, TypeConflict,
TypeError, UnexpectedCharacter, UnexpectedEndOfStream, UnexpectedEscapeCode,
UnknownLocal, UnrecognizedToken, UnterminatedExtractor, UnterminatedHereDoc,
UnterminatedIdentLiteral, UnterminatedInterpolation, UnterminatedStringLiteral,
UpdateKeyMissing, Utf8Error, ValueError, WithParamNoArg,
QueryStreamNotDefined, RecursionLimit, RuntimeError, TailingHereDoc, TryFromInt,
TypeConflict, TypeError, UnexpectedCharacter, UnexpectedEndOfStream,
UnexpectedEscapeCode, UnknownLocal, UnrecognizedToken, UnterminatedExtractor,
UnterminatedHereDoc, UnterminatedIdentLiteral, UnterminatedInterpolation,
UnterminatedStringLiteral, UpdateKeyMissing, Utf8Error, ValueError, WithParamNoArg,
};
match self {
NoClauseHit(outer)
Expand Down Expand Up @@ -350,6 +350,7 @@ impl ErrorKind {
| Self::__Nonexhaustive { .. }
| Utf8Error(_)
| FromUtf8Error(_)
| TryFromInt(_)
| ValueError(_) => (Some(Span::yolo()), None),
}
}
Expand Down Expand Up @@ -567,6 +568,7 @@ error_chain! {
AccessError(value_trait::AccessError);
CodecError(tremor_codec::Error);
Common(tremor_common::Error);
TryFromInt(std::num::TryFromIntError);
}
errors {
/*
Expand Down Expand Up @@ -1211,15 +1213,6 @@ pub(crate) fn error_guard_not_bool<T, O: Ranged, I: Ranged>(
error_type_conflict(outer, inner, got.value_type(), ValueType::Bool)
}

pub(crate) fn error_invalid_unary<T, O: Ranged, I: Ranged>(
outer: &O,
inner: &I,
op: ast::UnaryOpKind,
val: &Value,
) -> Result<T> {
Err(err_invalid_unary(outer, inner, op, val))
}

pub(crate) fn err_invalid_unary<O: Ranged, I: Ranged>(
outer: &O,
inner: &I,
Expand Down
61 changes: 35 additions & 26 deletions tremor-script/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
},
ctx::NO_CONTEXT,
errors::{
err_need_obj, error_array_out_of_bound, error_bad_array_index, error_bad_key,
error_bad_key_err, error_decreasing_range, error_division_by_zero, error_guard_not_bool,
error_invalid_binary, error_invalid_bitshift, error_need_arr, error_need_int,
error_need_obj, error_need_str, error_oops, error_overflow, error_patch_key_exists,
error_patch_merge_type_conflict, error_patch_update_key_missing, unknown_local, Result,
err_invalid_unary, err_need_obj, error_array_out_of_bound, error_bad_array_index,
error_bad_key, error_bad_key_err, error_decreasing_range, error_division_by_zero,
error_guard_not_bool, error_invalid_binary, error_invalid_bitshift, error_need_arr,
error_need_int, error_need_obj, error_need_str, error_oops, error_overflow,
error_patch_key_exists, error_patch_merge_type_conflict, error_patch_update_key_missing,
unknown_local, Result,
},
prelude::*,
stry, NO_AGGRS, NO_CONSTS,
Expand Down Expand Up @@ -385,16 +386,14 @@
}
}
#[inline]
pub(crate) fn exec_binary<'run, 'event, OuterExpr, InnerExpr>(
outer: &OuterExpr,
inner: &InnerExpr,
pub(crate) fn exec_binary<'run, 'event>(
outer: &impl BaseExpr,
inner: &impl BaseExpr,
op: BinOpKind,
lhs: &Value<'event>,
rhs: &Value<'event>,
) -> Result<Cow<'run, Value<'event>>>
where
OuterExpr: BaseExpr,
InnerExpr: BaseExpr,
'event: 'run,
{
// Lazy Heinz doesn't want to write that 10000 times
Expand Down Expand Up @@ -499,49 +498,56 @@

#[inline]
pub(crate) fn exec_unary<'run, 'event: 'run>(
outer: &impl BaseExpr,
inner: &impl BaseExpr,
op: UnaryOpKind,
val: &Value<'event>,
) -> Option<Cow<'run, Value<'event>>> {
) -> Result<Cow<'run, Value<'event>>> {
// Lazy Heinz doesn't want to write that 10000 times
// - snot badger - Darach
use UnaryOpKind::{BitNot, Minus, Not, Plus};
if let Some(x) = val.as_f64() {
match &op {
Minus => Some(Cow::Owned(Value::from(-x))),
Plus => Some(Cow::Owned(Value::from(x))),
_ => None,
Minus => Ok(Cow::Owned(Value::from(-x))),
Plus => Ok(Cow::Owned(Value::from(x))),
_ => Err(err_invalid_unary(outer, inner, op, val)),

Check warning on line 513 in tremor-script/src/interpreter.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/interpreter.rs#L512-L513

Added lines #L512 - L513 were not covered by tests
}
} else if let Some(x) = val.as_u64() {
match &op {
Minus => {
if x == 9_223_372_036_854_775_808 {
Some(Cow::Owned(Value::from(i64::MIN)))
Ok(Cow::Owned(Value::from(i64::MIN)))
} else {
x.try_into()
.ok()
.and_then(i64::checked_neg)
.map(Value::from)
.map(Cow::Owned)
.ok_or_else(|| err_invalid_unary(outer, inner, op, val))
}
}
Plus => Some(Cow::Owned(Value::from(x))),
BitNot => Some(Cow::Owned(Value::from(!x))),
Not => None,
Plus => Ok(Cow::Owned(Value::from(x))),
BitNot => Ok(Cow::Owned(Value::from(!x))),
Not => Err(err_invalid_unary(outer, inner, op, val)),

Check warning on line 531 in tremor-script/src/interpreter.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/interpreter.rs#L529-L531

Added lines #L529 - L531 were not covered by tests
}
} else if let Some(x) = val.as_i64() {
match &op {
Minus => x.checked_neg().map(Value::from).map(Cow::Owned),
Plus => Some(Cow::Owned(Value::from(x))),
BitNot => Some(Cow::Owned(Value::from(!x))),
Not => None,
Minus => x
.checked_neg()
.map(Value::from)
.map(Cow::Owned)
.ok_or_else(|| err_invalid_unary(outer, inner, op, val)),
Plus => Ok(Cow::Owned(Value::from(x))),
BitNot => Ok(Cow::Owned(Value::from(!x))),
Not => Err(err_invalid_unary(outer, inner, op, val)),

Check warning on line 542 in tremor-script/src/interpreter.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/interpreter.rs#L535-L542

Added lines #L535 - L542 were not covered by tests
}
} else if let Some(x) = val.as_bool() {
match &op {
BitNot | Not => Some(static_bool!(!x)),
_ => None,
BitNot | Not => Ok(static_bool!(!x)),
_ => Err(err_invalid_unary(outer, inner, op, val)),

Check warning on line 547 in tremor-script/src/interpreter.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/interpreter.rs#L547

Added line #L547 was not covered by tests
}
} else {
None
Err(err_invalid_unary(outer, inner, op, val))

Check warning on line 550 in tremor-script/src/interpreter.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/interpreter.rs#L550

Added line #L550 was not covered by tests
}
}

Expand Down Expand Up @@ -745,7 +751,10 @@
))
}

fn merge_values<'event>(value: &mut Value<'event>, replacement: &Value<'event>) -> Result<()> {
pub(crate) fn merge_values<'event>(
value: &mut Value<'event>,
replacement: &Value<'event>,
) -> Result<()> {
if let Some((rep, map)) = replacement.as_object().zip(value.as_object_mut()) {
for (k, v) in rep {
if let Some(k) = map.get_mut(k) {
Expand Down
10 changes: 3 additions & 7 deletions tremor-script/src/interpreter/imut_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use crate::{
errors::Kind as ErrorKind,
errors::{
err_invalid_fold, error_bad_key, error_decreasing_range, error_invalid_bool_op,
error_invalid_unary, error_need_obj, error_need_str, error_no_clause_hit, error_oops,
error_oops_err, Result,
error_need_obj, error_need_str, error_no_clause_hit, error_oops, error_oops_err, Result,
},
interpreter::{
exec_binary, exec_unary, merge_values, patch_value, resolve, set_local_shadow, test_guard,
Expand Down Expand Up @@ -726,11 +725,8 @@ impl<'script> ImutExpr<'script> {
'script: 'event,
{
let rhs = stry!(expr.expr.run(opts, env, event, state, meta, local));
// TODO align this implemenation to be similar to exec_binary?
match exec_unary(expr.kind, &rhs) {
Some(v) => Ok(v),
None => error_invalid_unary(self, &expr.expr, expr.kind, &rhs),
}
// TOO align this implemenation to be similar to exec_binary?
exec_unary(self, expr, expr.kind, &rhs)
}

// TODO: Quite some overlap with `interpreter::resolve` (and some with `expr::assign`)
Expand Down
5 changes: 4 additions & 1 deletion tremor-script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ pub mod srs;
mod std_lib;
/// Utility functions
pub mod utils;
pub use srs::{EventPayload, ValueAndMeta};

mod vm;

pub use crate::ast::deploy::raw::run_script;
pub use crate::ast::module;
Expand All @@ -85,6 +86,8 @@ pub use crate::registry::{
TremorAggrFnWrapper, TremorFn, TremorFnWrapper,
};
pub use crate::script::{Return, Script};
pub use crate::srs::{EventPayload, ValueAndMeta};
pub use crate::vm::compiler::Compiler;
use ast::{Consts, InvokeAggrFn};
pub use interpreter::{AggrType, FALSE, NULL, TRUE};
use lazy_static::lazy_static;
Expand Down
1 change: 0 additions & 1 deletion tremor-script/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ impl Script {
// helper.consts.args = args.clone_static();
let mut script = script_raw.up_script(&mut helper)?;
Optimizer::new(&helper).walk_script(&mut script)?;
let script = script;

Ok(Self {
script,
Expand Down
Loading
Loading