summaryrefslogtreecommitdiff
path: root/crates/typst-eval/src/import.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/import.rs
parent12dbb012b19a29612fc863c558901200b4013f5d (diff)
Refactor `Scope` (#5797)
Diffstat (limited to 'crates/typst-eval/src/import.rs')
-rw-r--r--crates/typst-eval/src/import.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/crates/typst-eval/src/import.rs b/crates/typst-eval/src/import.rs
index 2bbc7e41..27b06af4 100644
--- a/crates/typst-eval/src/import.rs
+++ b/crates/typst-eval/src/import.rs
@@ -4,7 +4,7 @@ use typst_library::diag::{
bail, error, warning, At, FileError, SourceResult, Trace, Tracepoint,
};
use typst_library::engine::Engine;
-use typst_library::foundations::{Content, Module, Value};
+use typst_library::foundations::{Binding, Content, Module, Value};
use typst_library::World;
use typst_syntax::ast::{self, AstNode, BareImportError};
use typst_syntax::package::{PackageManifest, PackageSpec};
@@ -43,7 +43,7 @@ impl Eval for ast::ModuleImport<'_> {
}
}
- // Source itself is imported if there is no import list or a rename.
+ // If there is a rename, import the source itself under that name.
let bare_name = self.bare_name();
let new_name = self.new_name();
if let Some(new_name) = new_name {
@@ -57,8 +57,7 @@ impl Eval for ast::ModuleImport<'_> {
}
}
- // Define renamed module on the scope.
- vm.scopes.top.define_ident(new_name, source.clone());
+ vm.define(new_name, source.clone());
}
let scope = source.scope().unwrap();
@@ -76,7 +75,7 @@ impl Eval for ast::ModuleImport<'_> {
"this import has no effect",
));
}
- vm.scopes.top.define_spanned(name, source, source_span);
+ vm.scopes.top.bind(name, Binding::new(source, source_span));
}
Ok(_) | Err(BareImportError::Dynamic) => bail!(
source_span, "dynamic import requires an explicit name";
@@ -92,8 +91,8 @@ impl Eval for ast::ModuleImport<'_> {
}
}
Some(ast::Imports::Wildcard) => {
- for (var, value, span) in scope.iter() {
- vm.scopes.top.define_spanned(var.clone(), value.clone(), span);
+ for (var, binding) in scope.iter() {
+ vm.scopes.top.bind(var.clone(), binding.clone());
}
}
Some(ast::Imports::Items(items)) => {
@@ -103,7 +102,7 @@ impl Eval for ast::ModuleImport<'_> {
let mut scope = scope;
while let Some(component) = &path.next() {
- let Some(value) = scope.get(component) else {
+ let Some(binding) = scope.get(component) else {
errors.push(error!(component.span(), "unresolved import"));
break;
};
@@ -111,6 +110,7 @@ impl Eval for ast::ModuleImport<'_> {
if path.peek().is_some() {
// Nested import, as this is not the last component.
// This must be a submodule.
+ let value = binding.read();
let Some(submodule) = value.scope() else {
let error = if matches!(value, Value::Func(function) if function.scope().is_none())
{
@@ -153,7 +153,7 @@ impl Eval for ast::ModuleImport<'_> {
}
}
- vm.define(item.bound_name(), value.clone());
+ vm.bind(item.bound_name(), binding.clone());
}
}
}