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
29/// Compile source with the default compiler configuration.
30///
31/// ```
32/// use mirascript_vm::{MiraValue, Runtime, compile};
33///
34/// let script = compile("6 * 7")?;
35/// let mut runtime = Runtime::new();
36/// assert_eq!(runtime.run(&script)?, MiraValue::number(42.0));
37/// # Ok::<(), Box<mirascript_vm::MiraError>>(())
38/// ```
39pub fn compile(source: &str) -> Result<MiraScript> {
40    compile_with(source, &CompileConfig::default())
41}
42
43/// Compile source with an explicit [`CompileConfig`].
44///
45/// ```
46/// use mirascript_vm::{MiraValue, Runtime, CompileConfig, InputMode, compile_with};
47///
48/// let mut config = CompileConfig::new();
49/// config.input_mode = InputMode::Template;
50/// let script = compile_with("Hello, $name!", &config)?;
51/// let mut runtime = Runtime::new();
52/// runtime.insert_global("name", &"Alice")?;
53/// assert_eq!(runtime.run(&script)?.as_str(&runtime)?.unwrap(), "Hello, Alice!");
54/// # Ok::<(), Box<mirascript_vm::MiraError>>(())
55/// ```
56pub fn compile_with(source: &str, config: &CompileConfig) -> Result<MiraScript> {
57    let (chunk, diagnostics) = core::Compiler::compile(source, config);
58    let chunk = chunk.ok_or_else(|| MiraError::compile(&diagnostics))?;
59    Ok(MiraScript::new(Program::decode(&chunk)?))
60}
61
62/// Items used by the derive macros. They are not a stable user-facing API.
63#[doc(hidden)]
64pub mod __private {
65    pub use crate::{MiraError, MiraManageable, MiraValue, Result};
66
67    pub use crate::value::types::field::{
68        MiraField, MiraFieldGetter, array_from_array, array_from_record, shaped_array_from_array,
69        shaped_array_from_record, shaped_record_from_array, shaped_record_from_record,
70    };
71
72    pub use crate::value::types::function::{
73        MiraFunction, MiraNativeFn,
74        helper::{native_argument, native_argument_optional, native_result},
75    };
76}