Skip to main content

mirascript_vm/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(missing_docs)]
3
4extern crate self as mirascript_vm;
5
6mod bytecode;
7mod compile;
8mod error;
9mod interpreter;
10mod operations;
11mod run_options;
12mod runtime_providers;
13mod standard_library;
14mod value;
15
16pub use core::{CompileConfig, DiagnosticPositionEncoding, InputMode};
17pub use mirascript_core as core;
18pub use mirascript_vm_derive::{MiraArray, MiraRecord, mira};
19
20use bytecode::Program;
21pub use compile::{MiraScript, ScriptId};
22pub use error::*;
23pub use interpreter::Runtime;
24pub use run_options::RunOptions;
25pub use runtime_providers::{RuntimeProviders, default_runtime_providers};
26pub(crate) use value::MiraValueKind;
27pub use value::{
28    MiraArray, MiraExtern, MiraFunction, MiraHandle, MiraManageable, MiraModule, MiraNativeFn,
29    MiraRecord, MiraShapedArray, MiraShapedRecord, MiraType, MiraValue, TryFromMira,
30};
31
32/// Compile source with the default compiler configuration.
33///
34/// ```
35/// use mirascript_vm::{MiraValue, Runtime, compile};
36///
37/// let script = compile("6 * 7")?;
38/// let mut runtime = Runtime::new();
39/// assert_eq!(runtime.run(&script)?, MiraValue::number(42.0));
40/// # Ok::<(), Box<mirascript_vm::MiraError>>(())
41/// ```
42pub fn compile(source: &str) -> Result<MiraScript> {
43    compile_with(source, &CompileConfig::default())
44}
45
46/// Compile source with an explicit [`CompileConfig`].
47///
48/// ```
49/// use mirascript_vm::{MiraValue, Runtime, CompileConfig, InputMode, compile_with};
50///
51/// let mut config = CompileConfig::new();
52/// config.input_mode = InputMode::Template;
53/// let script = compile_with("Hello, $name!", &config)?;
54/// let mut runtime = Runtime::new();
55/// runtime.insert_global("name", &"Alice")?;
56/// assert_eq!(runtime.run(&script)?.as_str(&runtime)?.unwrap(), "Hello, Alice!");
57/// # Ok::<(), Box<mirascript_vm::MiraError>>(())
58/// ```
59pub fn compile_with(source: &str, config: &CompileConfig) -> Result<MiraScript> {
60    let (chunk, diagnostics) = core::Compiler::compile(source, config);
61    let chunk = chunk.ok_or_else(|| MiraError::compile(&diagnostics))?;
62    Ok(MiraScript::new(Program::decode(&chunk)?))
63}
64
65/// Items used by the derive macros. They are not a stable user-facing API.
66#[doc(hidden)]
67pub mod __private {
68    pub use crate::{MiraError, MiraManageable, MiraValue, Result};
69
70    pub use crate::value::types::field::{
71        MiraField, MiraFieldGetter, array_from_array, array_from_record, shaped_array_from_array,
72        shaped_array_from_record, shaped_record_from_array, shaped_record_from_record,
73    };
74
75    pub use crate::value::types::function::{
76        MiraFunction, MiraNativeFn,
77        helper::{native_argument, native_argument_optional, native_result},
78    };
79}