summaryrefslogtreecommitdiff
path: root/library/src/base
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-24 17:39:08 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-24 17:41:41 +0100
commit8d3c68a1deb28dce2b80ed61f85141180ce6a951 (patch)
treede007203d448d6b6a2df7838e802f85d23ccd1a6 /library/src/base
parent5ae81971f299688b05d77af208d7bb44ffce5e2d (diff)
Protect Vm
Diffstat (limited to 'library/src/base')
-rw-r--r--library/src/base/calc.rs18
-rw-r--r--library/src/base/color.rs6
-rw-r--r--library/src/base/data.rs12
-rw-r--r--library/src/base/mod.rs8
-rw-r--r--library/src/base/string.rs14
5 files changed, 29 insertions, 29 deletions
diff --git a/library/src/base/calc.rs b/library/src/base/calc.rs
index 355e5c02..db40df06 100644
--- a/library/src/base/calc.rs
+++ b/library/src/base/calc.rs
@@ -3,7 +3,7 @@ use std::cmp::Ordering;
use crate::prelude::*;
/// Convert a value to an integer.
-pub fn int(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn int(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("value")?;
Ok(Value::Int(match v {
Value::Bool(v) => v as i64,
@@ -18,7 +18,7 @@ pub fn int(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Convert a value to a float.
-pub fn float(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn float(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("value")?;
Ok(Value::Float(match v {
Value::Int(v) => v as f64,
@@ -32,7 +32,7 @@ pub fn float(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// The absolute value of a numeric value.
-pub fn abs(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn abs(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("numeric value")?;
Ok(match v {
Value::Int(v) => Value::Int(v.abs()),
@@ -48,12 +48,12 @@ pub fn abs(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// The minimum of a sequence of values.
-pub fn min(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn min(_: &Vm, args: &mut Args) -> SourceResult<Value> {
minmax(args, Ordering::Less)
}
/// The maximum of a sequence of values.
-pub fn max(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn max(_: &Vm, args: &mut Args) -> SourceResult<Value> {
minmax(args, Ordering::Greater)
}
@@ -79,17 +79,17 @@ fn minmax(args: &mut Args, goal: Ordering) -> SourceResult<Value> {
}
/// Whether an integer is even.
-pub fn even(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn even(_: &Vm, args: &mut Args) -> SourceResult<Value> {
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0))
}
/// Whether an integer is odd.
-pub fn odd(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn odd(_: &Vm, args: &mut Args) -> SourceResult<Value> {
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0))
}
/// The modulo of two numbers.
-pub fn mod_(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn mod_(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: v1, span: span1 } = args.expect("integer or float")?;
let Spanned { v: v2, span: span2 } = args.expect("integer or float")?;
@@ -117,7 +117,7 @@ pub fn mod_(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Create a sequence of numbers.
-pub fn range(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn range(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let first = args.expect::<i64>("end")?;
let (start, end) = match args.eat::<i64>()? {
Some(second) => (first, second),
diff --git a/library/src/base/color.rs b/library/src/base/color.rs
index d54911ca..2db41ebf 100644
--- a/library/src/base/color.rs
+++ b/library/src/base/color.rs
@@ -3,13 +3,13 @@ use std::str::FromStr;
use crate::prelude::*;
/// Create a grayscale color.
-pub fn luma(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn luma(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Component(luma) = args.expect("gray component")?;
Ok(Value::Color(LumaColor::new(luma).into()))
}
/// Create an RGB(A) color.
-pub fn rgb(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn rgb(_: &Vm, args: &mut Args) -> SourceResult<Value> {
Ok(Value::Color(if let Some(string) = args.find::<Spanned<EcoString>>()? {
match RgbaColor::from_str(&string.v) {
Ok(color) => color.into(),
@@ -25,7 +25,7 @@ pub fn rgb(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Create a CMYK color.
-pub fn cmyk(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn cmyk(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let RatioComponent(c) = args.expect("cyan component")?;
let RatioComponent(m) = args.expect("magenta component")?;
let RatioComponent(y) = args.expect("yellow component")?;
diff --git a/library/src/base/data.rs b/library/src/base/data.rs
index 67ef2f2c..4f6e3b67 100644
--- a/library/src/base/data.rs
+++ b/library/src/base/data.rs
@@ -5,12 +5,12 @@ use typst::diag::{format_xml_like_error, FileError};
use crate::prelude::*;
/// Read structured data from a CSV file.
-pub fn csv(vm: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn csv(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to csv file")?;
let path = vm.locate(&path).at(span)?;
- let data = vm.world.file(&path).at(span)?;
+ let data = vm.world().file(&path).at(span)?;
let mut builder = csv::ReaderBuilder::new();
builder.has_headers(false);
@@ -45,12 +45,12 @@ fn format_csv_error(error: csv::Error) -> String {
}
/// Read structured data from a JSON file.
-pub fn json(vm: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn json(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to json file")?;
let path = vm.locate(&path).at(span)?;
- let data = vm.world.file(&path).at(span)?;
+ let data = vm.world().file(&path).at(span)?;
let value: serde_json::Value =
serde_json::from_slice(&data).map_err(format_json_error).at(span)?;
@@ -85,12 +85,12 @@ fn format_json_error(error: serde_json::Error) -> String {
}
/// Read structured data from an XML file.
-pub fn xml(vm: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn xml(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to xml file")?;
let path = vm.locate(&path).at(span)?;
- let data = vm.world.file(&path).at(span)?;
+ let data = vm.world().file(&path).at(span)?;
let text = std::str::from_utf8(&data).map_err(FileError::from).at(span)?;
let document = roxmltree::Document::parse(text).map_err(format_xml_error).at(span)?;
diff --git a/library/src/base/mod.rs b/library/src/base/mod.rs
index 837a2f0e..86ebd666 100644
--- a/library/src/base/mod.rs
+++ b/library/src/base/mod.rs
@@ -17,12 +17,12 @@ use typst::syntax::Source;
use crate::prelude::*;
/// The name of a value's type.
-pub fn type_(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn type_(_: &Vm, args: &mut Args) -> SourceResult<Value> {
Ok(args.expect::<Value>("value")?.type_name().into())
}
/// Ensure that a condition is fulfilled.
-pub fn assert(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn assert(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?;
if !v {
bail!(span, "assertion failed");
@@ -31,10 +31,10 @@ pub fn assert(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Evaluate a string as Typst markup.
-pub fn eval(vm: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn eval(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: text, span } = args.expect::<Spanned<String>>("source")?;
let source = Source::synthesized(text, span);
let route = Route::default();
- let module = model::eval(vm.world, route.track(), &source)?;
+ let module = model::eval(vm.world(), route.track(), &source)?;
Ok(Value::Content(module.content))
}
diff --git a/library/src/base/string.rs b/library/src/base/string.rs
index ed444d35..058ee248 100644
--- a/library/src/base/string.rs
+++ b/library/src/base/string.rs
@@ -3,12 +3,12 @@ use typst::model::Regex;
use crate::prelude::*;
/// The string representation of a value.
-pub fn repr(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn repr(_: &Vm, args: &mut Args) -> SourceResult<Value> {
Ok(args.expect::<Value>("value")?.repr().into())
}
/// Convert a value to a string.
-pub fn str(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn str(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("value")?;
Ok(Value::Str(match v {
Value::Int(v) => format_str!("{}", v),
@@ -19,29 +19,29 @@ pub fn str(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Create blind text.
-pub fn lorem(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn lorem(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let words: usize = args.expect("number of words")?;
Ok(Value::Str(lipsum::lipsum(words).into()))
}
/// Create a regular expression.
-pub fn regex(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn regex(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect::<Spanned<EcoString>>("regular expression")?;
Ok(Regex::new(&v).at(span)?.into())
}
/// Converts an integer into one or multiple letters.
-pub fn letter(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn letter(_: &Vm, args: &mut Args) -> SourceResult<Value> {
numbered(Numbering::Letter, args)
}
/// Converts an integer into a roman numeral.
-pub fn roman(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn roman(_: &Vm, args: &mut Args) -> SourceResult<Value> {
numbered(Numbering::Roman, args)
}
/// Convert a number into a symbol.
-pub fn symbol(_: &mut Vm, args: &mut Args) -> SourceResult<Value> {
+pub fn symbol(_: &Vm, args: &mut Args) -> SourceResult<Value> {
numbered(Numbering::Symbol, args)
}