use crate as rune;
use crate::alloc::prelude::*;
use crate::compile;
use crate::macros::{quote, FormatArgs, MacroContext, TokenStream};
use crate::parse::Parser;
use crate::runtime::{Panic, Value, VmResult};
use crate::{ContextError, Module};
#[rune::module(::std)]
pub fn module() -> Result<Module, ContextError> {
let mut module = Module::from_meta(self::module_meta)?.with_unique("std");
module.ty::<bool>()?.docs(docstring! {
})?;
module.ty::<char>()?.docs(docstring! {
})?;
module.ty::<u64>()?.docs(docstring! {
})?;
module.ty::<i64>()?.docs(docstring! {
})?;
module.ty::<f64>()?.docs(docstring! {
})?;
module.function_meta(panic)?;
module.function_meta(is_readable)?;
module.function_meta(is_writable)?;
module.macro_meta(stringify_macro)?;
module.macro_meta(panic_macro)?;
Ok(module)
}
#[rune::function]
fn panic(message: &str) -> VmResult<()> {
VmResult::err(Panic::custom(vm_try!(message.try_to_owned())))
}
#[rune::function]
fn is_readable(value: Value) -> bool {
value.is_readable()
}
#[rune::function]
fn is_writable(value: Value) -> bool {
value.is_writable()
}
#[rune::macro_(path = stringify)]
pub(crate) fn stringify_macro(
cx: &mut MacroContext<'_, '_, '_>,
stream: &TokenStream,
) -> compile::Result<TokenStream> {
let lit = cx.stringify(stream)?.try_to_string()?;
let lit = cx.lit(lit)?;
Ok(quote!(#lit).into_token_stream(cx)?)
}
#[rune::macro_(path = panic)]
pub(crate) fn panic_macro(
cx: &mut MacroContext<'_, '_, '_>,
stream: &TokenStream,
) -> compile::Result<TokenStream> {
let mut p = Parser::from_token_stream(stream, cx.input_span());
let args = p.parse_all::<FormatArgs>()?;
let expanded = args.expand(cx)?;
Ok(quote!(::std::panic(#expanded)).into_token_stream(cx)?)
}