1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
use std::{any::Any, fmt, marker::PhantomData, rc::Rc};
use crate::{
builtin_traits::{BuiltinTrait, BuiltinTraitFunction},
ffvariants,
hl::userdata::Type,
ll::{
bytecode::{
BuiltinTraits, DispatchTable, Environment, Function, FunctionKind, MethodSignature,
},
gc::{Gc, Memory},
value::{self, Closure},
},
Error, ForeignFunction, FunctionParameterCount, MethodParameterCount, Value,
};
struct UnresolvedMethodSignature {
name: Rc<str>,
parameter_count: MethodParameterCount,
builtin_trait: BuiltinTrait,
}
impl UnresolvedMethodSignature {
fn resolve(self, builtin_traits: &BuiltinTraits) -> MethodSignature {
MethodSignature {
name: self.name,
parameter_count: self.parameter_count,
trait_id: match self.builtin_trait {
BuiltinTrait::None => None,
BuiltinTrait::Iterator => Some(builtin_traits.iterator),
},
}
}
}
/// A descriptor for a dispatch table. Defines which methods are available on the table, as well
/// as their implementations.
#[derive(Default)]
pub(crate) struct DispatchTableDescriptor {
methods: Vec<(UnresolvedMethodSignature, FunctionKind)>,
}
impl DispatchTableDescriptor {
fn add_function_to_dtable(
env: &mut Environment,
gc: &mut Memory,
dtable: &mut DispatchTable,
builtin_traits: &BuiltinTraits,
signature: UnresolvedMethodSignature,
f: FunctionKind,
) -> Result<(), Error> {
let name = Rc::from(format!("{}.{}", &dtable.pretty_name, signature.name));
let function_id = env
.create_function(Function {
name: Rc::clone(&name),
parameter_count: FunctionParameterCount::Fixed(u16::from(
signature.parameter_count.to_count_without_self(),
)),
kind: f,
hidden_in_stack_traces: false,
})
.map_err(|_| Error::TooManyFunctions)?;
let signature = signature.resolve(builtin_traits);
let index =
env.get_or_create_method_index(&signature).map_err(|_| Error::TooManyMethods)?;
dtable.set_method(index, gc.allocate(Closure { name, function_id, captures: Vec::new() }));
Ok(())
}
/// Builds a dispatch table from this descriptor.
pub(crate) fn build_dtable(
self,
mut dtable: DispatchTable,
env: &mut Environment,
gc: &mut Memory,
builtin_traits: &BuiltinTraits,
) -> Result<DispatchTable, Error> {
for (signature, f) in self.methods {
Self::add_function_to_dtable(env, gc, &mut dtable, builtin_traits, signature, f)?;
}
Ok(dtable)
}
}
/// A builder that allows for binding APIs with user-defined types.
///
/// By default, the Mica VM does not know anything and cannot interact with Rust types. This API,
/// in conjunction with [`Engine::add_type`][crate::Engine::add_type], serves as an extension point
/// to let Mica programs interact with Rust data.
///
/// # Opaque user data
///
/// Rust values passed into Mica VMs by default are **opaque**, which means they possess no Mica
/// type information. Opaque values do not have any methods and have unfriendly type names (as
/// returned by [`std::any::type_name`]).
///
/// Opaque user data do possess Rust's runtime type information however, so it's possible to pass
/// them back into arguments of Rust functions available in Mica.
pub struct TypeBuilder<T>
where
T: ?Sized,
{
type_name: Rc<str>,
type_dtable: DispatchTableDescriptor,
instance_dtable: DispatchTableDescriptor,
_data: PhantomData<T>,
}
impl<T> TypeBuilder<T>
where
T: ?Sized,
{
/// Creates a new [`TypeBuilder`].
///
/// The `type_name` is used for referring to the type inside scripts and should reflect the Rust
/// type name. For generic types, it's best to define a concrete [type alias][type] to make the
/// binding code a little bit more self-documenting.
///
/// [type]: https://doc.rust-lang.org/stable/std/keyword.type.html
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use mica::{Engine, TypeBuilder, UserData};
///
/// struct Empty;
/// impl UserData for Empty {}
///
/// // A type builder is useless on its own, so we need to create an engine first.
/// let mut engine = Engine::new();
/// engine.add_type(TypeBuilder::<Empty>::new("Empty"))?;
/// # Ok(())
/// # }
/// ```
pub fn new(type_name: impl Into<Rc<str>>) -> Self
where
T: Any,
{
let type_name = type_name.into();
Self {
type_dtable: Default::default(),
instance_dtable: Default::default(),
type_name,
_data: PhantomData,
}
}
/// Internal converter for use with `BareExactArgs` parameter counts.
fn function_to_method_parameter_count(count: FunctionParameterCount) -> MethodParameterCount {
MethodParameterCount::from_count_without_self(
count.to_fixed().expect("BareExactArgs functions are never varargs"),
)
.expect("generated ForeignFunction variants only support up to 8 arguments") // Thus, overflow is impossible.
}
/// Adds a static function to the struct.
///
/// The function must follow the "bare" calling convention, in that it doesn't accept a
/// reference to `T` as its first parameter.
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use mica::{Engine, TypeBuilder, UserData};
///
/// struct Constants;
/// impl UserData for Constants {}
///
/// let mut engine = Engine::new();
/// engine.add_type(
/// TypeBuilder::<Constants>::new("Constants")
/// .add_static("the_meaning_of_life_universe_and_everything", || 42_i32),
/// )?;
///
/// let i: i32 = engine
/// .start("constant.mi", "Constants.the_meaning_of_life_universe_and_everything")?
/// .trampoline()?;
/// assert_eq!(i, 42);
/// # Ok(())
/// # }
/// ```
pub fn add_static<F, V>(self, name: &str, f: F) -> Self
where
V: ffvariants::BareExactArgs,
F: ForeignFunction<V, ParameterCount = FunctionParameterCount>,
{
self.add_raw_static(
name,
Self::function_to_method_parameter_count(F::PARAMETER_COUNT),
FunctionKind::Foreign(f.into_raw_foreign_function()),
)
}
/// Adds an instance function to the struct.
///
/// The function must follow the "method" calling convention, in that it accepts `&T` or
/// `&mut T` as its first parameter.
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use mica::{Engine, TypeBuilder, UserData};
///
/// struct Counter {
/// value: i32,
/// }
///
/// impl UserData for Counter {}
///
/// impl Counter {
/// fn increment(&mut self) {
/// self.value += 1;
/// }
///
/// fn value(&self) -> i32 {
/// self.value
/// }
/// }
///
/// let mut engine = Engine::new();
/// engine.add_type(
/// TypeBuilder::<Counter>::new("Counter")
/// .add_static("new", || Counter { value: 0 })
/// .add_function("increment", Counter::increment)
/// .add_function("value", Counter::value),
/// )?;
///
/// let i: i32 = engine
/// .start(
/// "counter.mi",
/// r#" count = Counter.new()
/// count.increment()
/// count.increment()
/// count.value "#
/// )?
/// .trampoline()?;
/// assert_eq!(i, 2);
/// # Ok(())
/// # }
/// ```
pub fn add_function<F, V>(self, name: &str, f: F) -> Self
where
V: ffvariants::Method<T>,
F: ForeignFunction<V, ParameterCount = MethodParameterCount>,
{
self.add_raw_function(
name,
F::PARAMETER_COUNT,
FunctionKind::Foreign(f.into_raw_foreign_function()),
)
}
/// Adds a function that's part of a built-in trait implementation.
///
/// The function must have a signature that's compatible with the built-in trait in question.
/// See the [`builtin_traits`][crate::builtin_traits] module for more information on each trait,
/// its methods, and their signatures.
///
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use mica::{builtin_traits::iterator, Engine, TypeBuilder, UserData};
///
/// struct Count10 {
/// i: i32,
/// }
///
/// impl UserData for Count10 {}
///
/// impl Count10 {
/// fn has_next(&self) -> bool {
/// self.i < 10
/// }
///
/// fn next(&mut self) {
/// self.i += 1;
/// }
/// }
///
/// let mut engine = Engine::new();
/// engine.add_type(
/// TypeBuilder::<Count10>::new("Count10")
/// .add_static("new", || Count10 { i: 1 })
/// .add_builtin_trait_function(iterator::HasNext, Count10::has_next)
/// .add_builtin_trait_function(iterator::Next, Count10::next),
/// )?;
///
/// let i: i32 = engine
/// .start(
/// "counter.mi",
/// r#"
/// i = 1
/// for _ in Count10.new() do
/// i = i * 2
/// end
/// i
/// "#
/// )?
/// .trampoline()?;
/// assert_eq!(i, 512);
/// # Ok(())
/// # }
/// ```
pub fn add_builtin_trait_function<S, B, F>(mut self, which: B, f: F) -> Self
where
B: BuiltinTraitFunction<S>,
F: ForeignFunction<S, ParameterCount = MethodParameterCount>,
{
self.instance_dtable.methods.push((
UnresolvedMethodSignature {
name: Rc::from(B::NAME),
parameter_count: F::PARAMETER_COUNT,
builtin_trait: which.owning_trait(),
},
FunctionKind::Foreign(f.into_raw_foreign_function()),
));
self
}
/// Adds a _raw_ instance function to the type.
///
/// You should generally prefer [`add_function`][`Self::add_function`] instead of this.
///
/// `parameter_count` should reflect the parameter count of the function. Method calls resolve
/// differently from function calls, because they match the parameter count exactly - it is
/// impossible to call the method `my_method/2` with three parameters. Thus, you can expect
/// the `arguments` array inside of foreign functions to always have `parameter_count` elements.
pub fn add_raw_function(
mut self,
name: &str,
parameter_count: MethodParameterCount,
f: FunctionKind,
) -> Self {
self.instance_dtable.methods.push((
UnresolvedMethodSignature {
name: Rc::from(name),
parameter_count,
builtin_trait: BuiltinTrait::None,
},
f,
));
self
}
/// Adds a _raw_ static function to the type.
///
/// You should generally prefer [`add_static`][`Self::add_static`] instead of this.
///
/// `parameter_count` should reflect the parameter count of the function. Method calls resolve
/// differently from function calls, because they match the parameter count exactly - it is
/// impossible to call the method `my_method/2` with three parameters. Thus, you can expect
/// the `arguments` array inside of foreign functions to always have `parameter_count` elements.
pub fn add_raw_static(
mut self,
name: &str,
parameter_count: MethodParameterCount,
f: FunctionKind,
) -> Self {
self.type_dtable.methods.push((
UnresolvedMethodSignature {
name: Rc::from(name),
parameter_count,
builtin_trait: BuiltinTrait::None,
},
f,
));
self
}
/// Builds the struct builder into its type dtable and instance dtable, respectively.
pub(crate) fn build(
self,
env: &mut Environment,
gc: &mut Memory,
builtin_traits: &BuiltinTraits,
) -> Result<BuiltType<T>, Error>
where
T: Any + Sized,
{
let mut type_dtable = self.type_dtable.build_dtable(
DispatchTable::new_for_type(Rc::clone(&self.type_name)),
env,
gc,
builtin_traits,
)?;
let instance_dtable = self.instance_dtable.build_dtable(
DispatchTable::new_for_instance(Rc::clone(&self.type_name)),
env,
gc,
builtin_traits,
)?;
let instance_dtable = Gc::new(instance_dtable);
env.add_user_dtable::<T>(Gc::clone(&instance_dtable));
type_dtable.instance = Some(Gc::as_raw(&instance_dtable));
let type_dtable = Gc::new(type_dtable);
Ok(BuiltType {
type_dtable,
instance_dtable,
type_name: self.type_name,
_data: PhantomData,
})
}
}
impl<T> fmt::Debug for TypeBuilder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TypeBuilder").finish_non_exhaustive()
}
}
/// Dispatch tables for a finished type.
pub(crate) struct BuiltType<T>
where
T: ?Sized,
{
pub(crate) type_name: Rc<str>,
pub(crate) type_dtable: Gc<DispatchTable>,
pub(crate) instance_dtable: Gc<DispatchTable>,
_data: PhantomData<T>,
}
impl<T> BuiltType<T> {
/// Makes a `Type<T>` user data value from the built type.
pub(crate) fn make_type(&self, gc: &mut Memory) -> Value
where
T: Any,
{
gc.manage(&self.type_dtable);
gc.manage(&self.instance_dtable);
let user_data: Box<dyn value::UserData> =
Box::new(Type::<T>::new(Gc::clone(&self.type_dtable)));
Value::UserData(Gc::new(user_data))
}
}