summaryrefslogtreecommitdiff
path: root/crates/typst-eval/src/vm.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-02-03 17:04:54 +0100
committerGitHub <noreply@github.com>2025-02-03 16:04:54 +0000
commiteee903b0f8d5c0dfda3539888d7473c6163841b0 (patch)
treed92b2f4565b0153c03cbb63575e2edd4e911e853 /crates/typst-eval/src/vm.rs
parent12dbb012b19a29612fc863c558901200b4013f5d (diff)
Refactor `Scope` (#5797)
Diffstat (limited to 'crates/typst-eval/src/vm.rs')
-rw-r--r--crates/typst-eval/src/vm.rs23
1 files changed, 17 insertions, 6 deletions
diff --git a/crates/typst-eval/src/vm.rs b/crates/typst-eval/src/vm.rs
index a5cbb6fa..52cfb4b5 100644
--- a/crates/typst-eval/src/vm.rs
+++ b/crates/typst-eval/src/vm.rs
@@ -1,7 +1,7 @@
use comemo::Tracked;
use typst_library::diag::warning;
use typst_library::engine::Engine;
-use typst_library::foundations::{Context, IntoValue, Scopes, Value};
+use typst_library::foundations::{Binding, Context, IntoValue, Scopes, Value};
use typst_library::World;
use typst_syntax::ast::{self, AstNode};
use typst_syntax::Span;
@@ -42,13 +42,23 @@ impl<'a> Vm<'a> {
self.engine.world
}
- /// Define a variable in the current scope.
+ /// Bind a value to an identifier.
+ ///
+ /// This will create a [`Binding`] with the value and the identifier's span.
pub fn define(&mut self, var: ast::Ident, value: impl IntoValue) {
- let value = value.into_value();
+ self.bind(var, Binding::new(value, var.span()));
+ }
+
+ /// Insert a binding into the current scope.
+ ///
+ /// This will insert the value into the top-most scope and make it available
+ /// for dynamic tracing, assisting IDE functionality.
+ pub fn bind(&mut self, var: ast::Ident, binding: Binding) {
if self.inspected == Some(var.span()) {
- self.trace(value.clone());
+ self.trace(binding.read().clone());
}
- // This will become an error in the parser if 'is' becomes a keyword.
+
+ // This will become an error in the parser if `is` becomes a keyword.
if var.get() == "is" {
self.engine.sink.warn(warning!(
var.span(),
@@ -58,7 +68,8 @@ impl<'a> Vm<'a> {
hint: "try `is_` instead"
));
}
- self.scopes.top.define_ident(var, value);
+
+ self.scopes.top.bind(var.get().clone(), binding);
}
/// Trace a value.