Files
not-python-rust/src/obj/module.rs
Alek Ratzloff 3fd0ba3a0b Remove BaseObj and fix Object::equals
* BaseObj felt a bit redundant. For everything that BaseObj did, we use
  Obj instead.
* Object::equals was a little weird. It was used for giving back
  equality, except when it wasn't. It's a little better defined now,
  here's what I'm shooting for:
    * *In general*, Object::equals will return true when two objects
      refer to the same object.
    * The exception to this rule is for "constant" objects, or "copy on
      write" objects. These include, but are not limited to: Int, Float,
      Bool, Nil, Str. Their base values are immutable and are the heart
      of object equality.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2024-10-10 20:30:24 -07:00

75 lines
1.7 KiB
Rust

use std::fmt::{self, Debug, Display};
use std::rc::Rc;
use gc::{Finalize, Trace};
use crate::obj::macros::*;
use crate::obj::prelude::*;
use crate::obj::Obj;
use crate::vm::Chunk;
#[derive(Trace, Finalize)]
pub struct Module {
base: Obj,
#[unsafe_ignore_trace]
path: Rc<String>,
#[unsafe_ignore_trace]
chunk: Rc<Chunk>,
globals: Vec<String>,
evaluated_value: Option<ObjP>,
}
impl Module {
pub fn new(path: impl ToString, chunk: Rc<Chunk>, globals: Vec<String>) -> Self {
Module {
base: Default::default(),
path: Rc::new(path.to_string()),
chunk,
globals,
evaluated_value: None,
}
}
pub fn path(&self) -> &Rc<String> {
&self.path
}
pub fn chunk(&self) -> &Rc<Chunk> {
&self.chunk
}
pub fn globals(&self) -> &Vec<String> {
&self.globals
}
pub fn evaluated_value(&self) -> &Option<ObjP> {
&self.evaluated_value
}
pub fn set_evaluated_value(&mut self, value: Option<ObjP>) {
self.evaluated_value = value;
}
impl_create!(path: impl ToString, chunk: Rc<Chunk>, globals: Vec<String>);
}
impl Debug for Module {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "<Module {}>", self.path())
}
}
impl Display for Module {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, fmt)
}
}
impl Object for Module {
impl_base_obj!(Module);
}
////////////////////////////////////////////////////////////////////////////////
// Module method implementations
////////////////////////////////////////////////////////////////////////////////