Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative fallback fields implementation #441

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/rune-core/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ define! {
doc: ["Allows an indexing set operation to work."],
};

/// The function to access a field dynamically.
pub const [DYNAMIC_FIELD_GET, DYNAMIC_FIELD_GET_HASH]: Protocol = Protocol {
name: "dynamic_get",
hash: 0x6dda58b140dfeaf9,
repr: Some("let output = $value"),
doc: ["Allows a dynamic get operation to work."],
};

/// The function to set a field dynamically.
pub const [DYNAMIC_FIELD_SET, DYNAMIC_FIELD_SET_HASH]: Protocol = Protocol {
name: "dynamic_set",
hash: 0xbe28c02896ca0b64,
repr: Some("$value = input"),
doc: ["Allows a dynamic set operation to work."],
};

/// Check two types for partial equality.
pub const [PARTIAL_EQ, PARTIAL_EQ_HASH]: Protocol = Protocol {
name: "partial_eq",
Expand Down
3 changes: 2 additions & 1 deletion crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::collections::BTreeMap;

use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use rune_core::Hash;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::Token;

use rune_core::Hash;

use crate::context::{Context, Generate, GenerateTarget, Tokens, TypeAttr};

/// An internal call to the macro.
Expand Down
3 changes: 2 additions & 1 deletion crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::cell::RefCell;

use crate::internals::*;
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::quote_spanned;
Expand All @@ -10,6 +9,8 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned as _;
use syn::Token;

use crate::internals::*;

/// Parsed `#[rune(..)]` field attributes.
#[derive(Default)]
pub(crate) struct FieldAttrs {
Expand Down
5 changes: 3 additions & 2 deletions crates/rune-macros/src/internals.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use proc_macro2::Span;
use quote::{ToTokens, TokenStreamExt as _};
use std::fmt;

#[derive(Copy, Clone)]
pub struct Symbol(&'static str);
Expand All @@ -19,12 +20,12 @@ pub const NAME: Symbol = Symbol("name");
pub const ITEM: Symbol = Symbol("item");
pub const MODULE: Symbol = Symbol("module");
pub const INSTALL_WITH: Symbol = Symbol("install_with");

pub const CONSTRUCTOR: Symbol = Symbol("constructor");
pub const BUILTIN: Symbol = Symbol("builtin");
pub const STATIC_TYPE: Symbol = Symbol("static_type");
pub const FROM_VALUE: Symbol = Symbol("from_value");
pub const FROM_VALUE_PARAMS: Symbol = Symbol("from_value_params");

pub const GET: Symbol = Symbol("get");
pub const SET: Symbol = Symbol("set");
pub const COPY: Symbol = Symbol("copy");
Expand Down
1 change: 1 addition & 0 deletions crates/rune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ disable-io = ["alloc"]
fmt = ["alloc"]
std = ["alloc", "num/std", "serde/std", "rune-core/std", "rune-alloc/std", "musli/std", "musli-storage/std", "once_cell/std", "anyhow/std"]
alloc = ["anyhow", "rune-alloc/alloc", "rune-core/alloc", "once_cell/alloc", "serde/alloc"]
dynamic_fields = []

[dependencies]
rune-macros = { version = "=0.13.1", path = "../rune-macros" }
Expand Down
7 changes: 4 additions & 3 deletions crates/rune/src/any.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use core::any;

use crate::compile::Named;
use crate::hash::Hash;

/// Macro to mark a value as external, which will implement all the appropriate
/// traits.
///
Expand Down Expand Up @@ -56,6 +53,9 @@ use crate::hash::Hash;
/// ```
pub use rune_macros::Any;

use crate::compile::Named;
use crate::hash::Hash;

/// A trait which can be stored inside of an [AnyObj](crate::runtime::AnyObj).
///
/// We use our own marker trait that must be explicitly derived to prevent other
Expand All @@ -72,6 +72,7 @@ pub use rune_macros::Any;
/// name: String,
/// }
/// ```

pub trait Any: Named + any::Any {
/// The type hash of the type.
///
Expand Down
83 changes: 33 additions & 50 deletions crates/rune/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,61 @@
//! The main entry to compiling rune source is [prepare][crate::prepare] which
//! uses this compiler. In here you'll just find compiler-specific types.

mod assembly;
pub(crate) use self::assembly::{Assembly, AssemblyInst};

pub(crate) mod attrs;

pub(crate) mod error;
pub use self::error::{Error, ImportStep, MetaError};
pub(crate) use self::error::{ErrorKind, IrErrorKind};
pub use meta_info::MetaInfo;
pub use rune_core::{Component, ComponentRef, IntoComponent, Item, ItemBuf};

mod compile_visitor;
pub(crate) use self::assembly::{Assembly, AssemblyInst};
pub(crate) use self::compile::compile;
pub use self::compile_visitor::CompileVisitor;
#[cfg(feature = "std")]
pub(crate) use self::compile_visitor::NoopCompileVisitor;

pub(crate) mod context;
pub use self::context::Context;

pub(crate) mod context_error;
pub use self::context_error::ContextError;

pub(crate) mod meta_info;
pub use meta_info::MetaInfo;

mod docs;
pub(crate) use self::docs::Docs;

mod prelude;
pub use self::error::{Error, ImportStep, MetaError};
pub(crate) use self::error::{ErrorKind, IrErrorKind};
pub(crate) use self::location::DynLocation;
pub use self::location::{Located, Location};
pub(crate) use self::meta::{Doc, ItemMeta};
pub use self::meta::{MetaRef, SourceMeta};
pub use self::named::Named;
pub(crate) use self::names::Names;
pub use self::options::{Options, ParseOptionError};
pub(crate) use self::pool::{ItemId, ModId, ModMeta, Pool};
pub(crate) use self::prelude::Prelude;

pub(crate) mod ir;

pub use rune_core::{Component, ComponentRef, IntoComponent, Item, ItemBuf};

mod source_loader;
#[cfg(feature = "std")]
pub use self::source_loader::FileSourceLoader;
pub use self::source_loader::{NoopSourceLoader, SourceLoader};

mod unit_builder;
pub use self::unit_builder::LinkerError;
pub(crate) use self::unit_builder::UnitBuilder;
pub(crate) use self::visibility::Visibility;
pub use self::with_span::{HasSpan, WithSpan};

pub(crate) mod v1;
mod assembly;
pub(crate) mod attrs;

mod options;
pub use self::options::{Options, ParseOptionError};
mod compile_visitor;
pub(crate) mod context;
pub(crate) mod context_error;
mod docs;
pub(crate) mod error;
pub(crate) mod ir;
pub(crate) mod meta_info;
mod prelude;

mod location;
pub(crate) use self::location::DynLocation;
pub use self::location::{Located, Location};
mod source_loader;
mod unit_builder;
pub(crate) mod v1;

mod compile;
mod location;
pub mod meta;
pub(crate) use self::meta::{Doc, ItemMeta};
pub use self::meta::{MetaRef, SourceMeta};

mod pool;
pub(crate) use self::pool::{ItemId, ModId, ModMeta, Pool};

mod named;
pub use self::named::Named;

mod names;
pub(crate) use self::names::Names;

mod options;
mod pool;
mod visibility;
pub(crate) use self::visibility::Visibility;

mod with_span;
pub use self::with_span::{HasSpan, WithSpan};

mod compile;
pub(crate) use self::compile::compile;

/// Helper alias for compile results.
pub type Result<T, E = Error> = ::core::result::Result<T, E>;
34 changes: 28 additions & 6 deletions crates/rune/src/compile/options.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
use core::fmt;

use ::rust_alloc::boxed::Box;
use core::fmt;

/// Error raised when trying to parse an invalid option.
#[derive(Debug, Clone)]
pub struct ParseOptionError {
option: Box<str>,
pub enum ParseOptionError {
/// Error raised when trying to parse an invalid option.
UnsupportedOption {
/// The unsupported option.
option: Box<str>,
},
/// Error raised when trying to enable a option which is not enabled by a feature gate.
DisabledFeatureOptionError {
/// The disabled option.
option: Box<str>,
/// The required feature to enable this option.
required_feature: &'static str,
},
}

impl fmt::Display for ParseOptionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Unsupported compile option `{}`", self.option)
match self {
Self::UnsupportedOption { option } => {
write!(f, "Unsupported compile option `{}`", option)
}
Self::DisabledFeatureOptionError {
option,
required_feature,
} => write!(
f,
"Featured gated compile option `{}` requires feature `{}`",
option, required_feature
),
}
}
}

Expand Down Expand Up @@ -77,7 +99,7 @@ impl Options {
self.function_body = it.next() == Some("true");
}
_ => {
return Err(ParseOptionError {
return Err(ParseOptionError::UnsupportedOption {
option: option.into(),
});
}
Expand Down
7 changes: 3 additions & 4 deletions crates/rune/src/runtime/value.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod serde;

use ::rust_alloc::sync::Arc;
use core::any;
use core::borrow::Borrow;
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use core::fmt;
use core::hash;
use core::ptr;

use ::rust_alloc::sync::Arc;
use ::serde::{Deserialize, Serialize};

use crate::alloc::fmt::TryWrite;
use crate::alloc::prelude::*;
Expand All @@ -25,7 +24,7 @@ use crate::runtime::{
use crate::runtime::{Hasher, Tuple};
use crate::{Any, Hash};

use ::serde::{Deserialize, Serialize};
mod serde;

// Small helper function to build errors.
fn err<T, E>(error: E) -> VmResult<T>
Expand Down
Loading
Loading