Skip to content

Commit

Permalink
Implement memory limits in Global allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Sep 30, 2023
1 parent 481fd86 commit b28430e
Show file tree
Hide file tree
Showing 430 changed files with 10,239 additions and 5,220 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --workspace --exclude no-std-examples --exclude generate --all-features --all-targets -- -D warnings
- run: cargo clippy --workspace --exclude generate --all-features --all-targets -- -D warnings

docs:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
strategy:
fail-fast: false
matrix:
feature: [capture-io, doc, fmt, cli, workspace, byte-code]
feature: [capture-io, "cli,doc", "cli,fmt", cli, workspace, byte-code]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
Expand All @@ -117,15 +117,24 @@ jobs:
- uses: Swatinem/rust-cache@v2
- run: cargo build -p rune-wasm --target wasm32-unknown-unknown

no_std_minimal:
runs-on: windows-latest
needs: basics
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
- uses: Swatinem/rust-cache@v2
- run: cargo run --manifest-path=no-std-examples/Cargo.toml --profile unix --example no_std_minimal

test:
runs-on: ubuntu-latest
needs: [no_default_features, build_feature, docs, msrv, miri_rune, miri_rune_alloc, wasm]
needs: [no_default_features, build_feature, docs, msrv, miri_rune, miri_rune_alloc, no_std_minimal, wasm]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo build -p rune --no-default-features --features alloc
- run: cargo test --workspace --exclude no-std-examples --all-targets
- run: cargo test --workspace --exclude no-std-examples --doc
- run: cargo test --workspace --all-targets
- run: cargo test --workspace --doc
- run: cargo run --bin rune -- check --recursive --experimental scripts
- run: cargo run --bin rune -- test --recursive --experimental scripts --opt include-std
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
[workspace]
resolver = "2"

members = [
"crates/*",
"examples",
"benches",
"tools/*",
"no-std-examples",
]

default-members = [
"crates/*",
"examples",
"benches",
"tools/site",
"tools/builder",
]
exclude = ["no-std-examples"]

[profile.bench]
lto = false
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ use rune::termcolor::{ColorChoice, StandardStream};
use std::sync::Arc;

#[tokio::main]
async fn main() -> rune::Result<()> {
async fn main() -> rune::support::Result<()> {
let context = Context::with_default_modules()?;
let runtime = Arc::new(context.runtime());
let runtime = Arc::new(context.runtime()?);

let mut sources = Sources::new();
sources.insert(Source::new(
Expand Down
4 changes: 2 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Must now instead do this:
pub(crate) fn stringy_math(
cx: &mut MacroContext<'_, '_, '_>,
stream: &TokenStream,
) -> rune::Result<TokenStream> {
) -> rune::support::Result<TokenStream> {
let mut parser = Parser::from_token_stream(stream, cx.stream_span());

let mut output = quote!(0);
Expand Down Expand Up @@ -199,4 +199,4 @@ pub(crate) fn stringy_math(
[MacroContext]: https://docs.rs/rune/0.10.0/rune/macros/struct.MacroContext.html
[rune::macros::eval]: https://docs.rs/rune/0.9.1/rune/macros/fn.eval.html
[rune::macros::resolve]: https://docs.rs/rune/0.9.1/rune/macros/fn.resolve.html
[rune::macros::stringify]: https://docs.rs/rune/0.9.1/rune/macros/fn.stringify.html
[rune::macros::stringify]: https://docs.rs/rune/0.9.1/rune/macros/fn.stringify.html
6 changes: 4 additions & 2 deletions benches/benches/bench_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ pub(crate) fn vm(
.with_diagnostics(diagnostics)
.build()?;

let context = Arc::new(context.runtime());
let context = Arc::new(context.runtime()?);
Ok(Vm::new(context, Arc::new(unit)))
}

pub(crate) fn sources(source: &str) -> Sources {
let mut sources = Sources::new();
sources.insert(Source::new("main", source));
sources
.insert(Source::new("main", source))
.expect("Failed to insert source");
sources
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/benchmarks/aoc_2020_19b.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::Criterion;

use rune::alloc::TryClone;
use rune::alloc::prelude::*;

criterion::criterion_group!(benches, aoc_2020_19b);

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/benchmarks/aoc_2020_1a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use anyhow::Context;
use criterion::Criterion;
use rune::alloc::TryClone;
use rune::alloc::prelude::*;

criterion::criterion_group!(benches, aoc_2020_1a);

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/benchmarks/aoc_2020_1b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use criterion::Criterion;

use rune::alloc::TryClone;
use rune::alloc::prelude::*;

criterion::criterion_group!(benches, aoc_2020_1b);

Expand Down
3 changes: 2 additions & 1 deletion benches/benches/benchmarks/brainfuck.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::Criterion;
use rune::{Hash, Result, Vm};
use rune::support::Result;
use rune::{Hash, Vm};

use rune::modules::capture_io::CaptureIo;

Expand Down
22 changes: 22 additions & 0 deletions crates/rune-alloc-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "rune-alloc-macros"
version = "0.12.3"
authors = ["John-John Tedro <[email protected]>"]
edition = "2021"
rust-version = "1.70"
description = "Macros for the Rune Language, an embeddable dynamic programming language for Rust."
documentation = "https://docs.rs/rune"
readme = "README.md"
homepage = "https://github.com/rune-rs/rune"
repository = "https://github.com/rune-rs/rune"
license = "MIT OR Apache-2.0"
keywords = ["language", "scripting", "scripting-language"]
categories = ["parser-implementations"]

[dependencies]
syn = { version = "2.0.16", features = ["full"] }
quote = "1.0.27"
proc-macro2 = "1.0.56"

[lib]
proc-macro = true
68 changes: 68 additions & 0 deletions crates/rune-alloc-macros/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::cell::RefCell;

use proc_macro2::Span;
use syn::spanned::Spanned as _;

#[derive(Default)]
pub(crate) struct Context {
pub(crate) errors: RefCell<Vec<syn::Error>>,
pub(crate) module: Option<syn::Path>,
}

impl Context {
/// Construct a new context.
pub(crate) fn new() -> Self {
Self::default()
}

/// Register an error.
pub(crate) fn error(&self, error: syn::Error) {
self.errors.borrow_mut().push(error)
}

/// Test if context has any errors.
pub(crate) fn has_errors(&self) -> bool {
!self.errors.borrow().is_empty()
}

/// Convert into errors.
pub(crate) fn into_errors(self) -> Vec<syn::Error> {
self.errors.into_inner()
}

pub(crate) fn tokens_with_module(&self, module: Option<&syn::Path>) -> Tokens {
let default_module;

let m = match module {
Some(module) => module,
None => match &self.module {
Some(module) => module,
None => {
default_module = syn::Path::from(syn::Ident::new("rune", Span::call_site()));
&default_module
}
},
};

Tokens {
try_clone: path(m, ["alloc", "clone", "TryClone"]),
alloc: path(m, ["alloc"]),
}
}
}

fn path<const N: usize>(base: &syn::Path, path: [&'static str; N]) -> syn::Path {
let mut base = base.clone();

for s in path {
let ident = syn::Ident::new(s, base.span());
base.segments.push(syn::PathSegment::from(ident));
}

base
}

pub(crate) struct Tokens {
pub(crate) try_clone: syn::Path,
pub(crate) alloc: syn::Path,
}
47 changes: 47 additions & 0 deletions crates/rune-alloc-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! <img alt="rune logo" src="https://raw.githubusercontent.com/rune-rs/rune/main/assets/icon.png" />
//! <br>
//! <a href="https://github.com/rune-rs/rune"><img alt="github" src="https://img.shields.io/badge/github-rune--rs/rune-8da0cb?style=for-the-badge&logo=github" height="20"></a>
//! <a href="https://crates.io/crates/rune-macros"><img alt="crates.io" src="https://img.shields.io/crates/v/rune-macros.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20"></a>
//! <a href="https://docs.rs/rune-macros"><img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rune--macros-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20"></a>
//! <a href="https://discord.gg/v5AeNkT"><img alt="chat on discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square" height="20"></a>
//! <br>
//! Minimum support: Rust <b>1.70+</b>.
//! <br>
//! <br>
//! <a href="https://rune-rs.github.io"><b>Visit the site 🌐</b></a>
//! &mdash;
//! <a href="https://rune-rs.github.io/book/"><b>Read the book 📖</b></a>
//! <br>
//! <br>
//!
//! Macros for the Rune Language, an embeddable dynamic programming language for Rust.
//!
//! <br>
//!
//! ## Usage
//!
//! This is part of the [Rune Language](https://rune-rs.github.io).
#![allow(clippy::manual_map)]

extern crate proc_macro;

mod context;
mod try_clone;

#[proc_macro_derive(TryClone, attributes(try_clone))]
pub fn try_clone(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);

try_clone::expand(input)
.unwrap_or_else(to_compile_errors)
.into()
}

fn to_compile_errors<I>(errors: I) -> proc_macro2::TokenStream
where
I: IntoIterator<Item = syn::Error>,
{
let compile_errors = errors.into_iter().map(syn::Error::into_compile_error);
::quote::quote!(#(#compile_errors)*)
}
Loading

0 comments on commit b28430e

Please sign in to comment.