rune/modules/core.rs
1//! Core types and methods in Rune.
2
3use crate as rune;
4use crate::alloc::prelude::*;
5use crate::compile;
6use crate::macros::{quote, FormatArgs, MacroContext, TokenStream};
7use crate::parse::Parser;
8use crate::runtime::{Panic, Value, VmResult};
9use crate::{docstring, ContextError, Module};
10
11#[rune::module(::std)]
12/// Core types and methods in Rune.
13///
14/// These are types and methods for which Rune as a language would not work without.
15pub fn module() -> Result<Module, ContextError> {
16 let mut module = Module::from_meta(self::module_meta)?.with_unique("std");
17
18 module.ty::<bool>()?.docs(docstring! {
19 /// The primitive boolean type.
20 })?;
21 module.ty::<char>()?.docs(docstring! {
22 /// The primitive character type.
23 })?;
24 module.ty::<u64>()?.docs(docstring! {
25 /// The unsigned integer type.
26 })?;
27 module.ty::<i64>()?.docs(docstring! {
28 /// The signed integer type.
29 })?;
30 module.ty::<f64>()?.docs(docstring! {
31 /// The primitive float type.
32 })?;
33
34 module.function_meta(panic)?;
35 module.function_meta(is_readable)?;
36 module.function_meta(is_writable)?;
37
38 module.macro_meta(stringify_macro)?;
39 module.macro_meta(panic_macro)?;
40 Ok(module)
41}
42
43/// Cause a vm panic with the given `message`.
44///
45/// A panic in Rune causes the current execution to unwind and terminate. The
46/// panic will not be propagated into Rust, but will instead be signatted
47/// through a `VmError`.
48///
49/// If you want to format a message, consider using the [panic!] macro.
50#[rune::function]
51fn panic(message: &str) -> VmResult<()> {
52 VmResult::err(Panic::custom(vm_try!(message.try_to_owned())))
53}
54
55/// Test if the given `value` is readable.
56///
57/// A value is readable if can be acquired for shared access, such as producing
58/// an immutable reference.
59///
60/// A value that is moved is no longer considered readable.
61///
62/// # Examples
63///
64/// ```rune
65/// let value = Some(42);
66/// assert!(is_readable(value));
67/// let value2 = value.map(|v| v + 1);
68/// drop(value);
69/// assert!(!is_readable(value));
70/// assert_eq!(value2, Some(43));
71/// ```
72#[rune::function]
73fn is_readable(value: Value) -> bool {
74 value.is_readable()
75}
76
77/// Test if the given `value` is writable.
78///
79/// A value is writable if can be acquired for exclusive access, such as
80/// producing a mutable reference or taking ownership.
81///
82/// # Examples
83///
84/// ```rune
85/// let value = Some(42);
86/// assert!(is_writable(value));
87/// let value2 = value.map(|v| v + 1);
88/// drop(value);
89/// assert!(!is_writable(value));
90/// assert_eq!(value2, Some(43));
91/// ```
92#[rune::function]
93fn is_writable(value: Value) -> bool {
94 value.is_writable()
95}
96
97/// Stringify the given argument, causing it to expand to its underlying token
98/// stream.
99///
100/// This can be used by macros to convert a stream of tokens into a readable
101/// string.
102#[rune::macro_(path = stringify)]
103pub(crate) fn stringify_macro(
104 cx: &mut MacroContext<'_, '_, '_>,
105 stream: &TokenStream,
106) -> compile::Result<TokenStream> {
107 let lit = cx.stringify(stream)?.try_to_string()?;
108 let lit = cx.lit(lit)?;
109 Ok(quote!(#lit).into_token_stream(cx)?)
110}
111
112/// Cause a vm panic with a formatted message.
113///
114/// A panic in Rune causes the current execution to unwind and terminate. The
115/// panic will not be propagated into Rust, but will instead be signatted
116/// through a `VmError`.
117#[rune::macro_(path = panic)]
118pub(crate) fn panic_macro(
119 cx: &mut MacroContext<'_, '_, '_>,
120 stream: &TokenStream,
121) -> compile::Result<TokenStream> {
122 let mut p = Parser::from_token_stream(stream, cx.input_span());
123 let args = p.parse_all::<FormatArgs>()?;
124 let expanded = args.expand(cx)?;
125 Ok(quote!(::std::panic(#expanded)).into_token_stream(cx)?)
126}