Skip to content

Commit

Permalink
Class module (#677)
Browse files Browse the repository at this point in the history
- Added example and documenation
  • Loading branch information
HalidOdat authored Sep 6, 2020
1 parent 41a3874 commit 76aba2a
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 205 deletions.
13 changes: 4 additions & 9 deletions boa/examples/classes.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use boa::{
builtins::{
object::{Class, ClassBuilder},
property::Attribute,
value::Value,
},
builtins::{property::Attribute, value::Value},
class::{Class, ClassBuilder},
exec::Interpreter,
forward_val,
realm::Realm,
Result,
Finalize, Result, Trace,
};

use gc::{Finalize, Trace};

// We create a new struct that is going to represent a person.
//
// We derive `Debug`, `Trace` and `Finalize`, It automatically implements `NativeObject`
Expand Down Expand Up @@ -140,7 +135,7 @@ fn main() {
if (!Person.is('Hello')) {
console.log('\'Hello\' string is not a Person class instance.');
}
console.log(Person.staticProperty);
console.log(person.inheritedProperty);
console.log(Person.prototype.inheritedProperty === person.inheritedProperty);
Expand Down
197 changes: 3 additions & 194 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
builtins::{
function::Function,
map::ordered_map::OrderedMap,
property::{Attribute, Property, PropertyKey},
property::{Property, PropertyKey},
value::{RcBigInt, RcString, RcSymbol, Value},
BigInt, Date, RegExp,
},
Expand All @@ -26,13 +26,10 @@ use crate::{
};
use gc::{Finalize, Trace};
use rustc_hash::FxHashMap;
use std::any::Any;
use std::fmt::{Debug, Display, Error, Formatter};
use std::result::Result as StdResult;
use std::{any::Any, result::Result as StdResult};

use super::function::{
make_builtin_fn, make_constructor_fn, BuiltInFunction, FunctionFlags, NativeFunction,
};
use super::function::{make_builtin_fn, make_constructor_fn};
use crate::builtins::value::same_value;

mod gcobject;
Expand Down Expand Up @@ -69,194 +66,6 @@ impl<T: Any + Debug + Trace> NativeObject for T {
}
}

/// Native class.
pub trait Class: NativeObject + Sized {
/// The binding name of the object.
const NAME: &'static str;
/// The amount of arguments the class `constructor` takes, default is `0`.
const LENGTH: usize = 0;
/// The attibutes the class will be binded with, default is `writable`, `enumerable`, `configurable`.
const ATTRIBUTE: Attribute = Attribute::all();

/// The constructor of the class.
fn constructor(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Self>;

/// Initializes the internals and the methods of the class.
fn init(class: &mut ClassBuilder<'_>) -> Result<()>;
}

/// This is a wrapper around `Class::constructor` that sets the internal data of a class.
///
/// This is automatically implemented, when a type implements `Class`.
pub trait ClassConstructor: Class {
fn raw_constructor(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value>
where
Self: Sized;
}

impl<T: Class> ClassConstructor for T {
fn raw_constructor(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value>
where
Self: Sized,
{
let object_instance = Self::constructor(this, args, ctx)?;
this.set_data(ObjectData::NativeObject(Box::new(object_instance)));
Ok(this.clone())
}
}

/// Class builder which allows adding methods and static methods to the class.
#[derive(Debug)]
pub struct ClassBuilder<'context> {
context: &'context mut Interpreter,
object: GcObject,
prototype: GcObject,
}

impl<'context> ClassBuilder<'context> {
pub(crate) fn new<T>(context: &'context mut Interpreter) -> Self
where
T: ClassConstructor,
{
let global = context.global();

let prototype = {
let object_prototype = global.get_field("Object").get_field(PROTOTYPE);

let object = Object::create(object_prototype);
GcObject::new(object)
};
// Create the native function
let function = Function::BuiltIn(
BuiltInFunction(T::raw_constructor),
FunctionFlags::CONSTRUCTABLE,
);

// Get reference to Function.prototype
// Create the function object and point its instance prototype to Function.prototype
let mut constructor =
Object::function(function, global.get_field("Function").get_field(PROTOTYPE));

let length = Property::data_descriptor(
T::LENGTH.into(),
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);
constructor.insert_property("length", length);

let name = Property::data_descriptor(
T::NAME.into(),
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);
constructor.insert_property("name", name);

let constructor = GcObject::new(constructor);

prototype
.borrow_mut()
.insert_field("constructor", constructor.clone().into());

constructor
.borrow_mut()
.insert_field(PROTOTYPE, prototype.clone().into());

Self {
context,
object: constructor,
prototype,
}
}

pub(crate) fn build(self) -> GcObject {
self.object
}

/// Add a method to the class.
///
/// It is added to `prototype`.
pub fn method<N>(&mut self, name: N, length: usize, function: NativeFunction)
where
N: Into<String>,
{
let name = name.into();
let mut function = Object::function(
Function::BuiltIn(function.into(), FunctionFlags::CALLABLE),
self.context
.global()
.get_field("Function")
.get_field("prototype"),
);

function.insert_field("length", Value::from(length));
function.insert_field("name", Value::from(name.as_str()));

self.prototype
.borrow_mut()
.insert_field(name, Value::from(function));
}

/// Add a static method to the class.
///
/// It is added to class object itself.
pub fn static_method<N>(&mut self, name: N, length: usize, function: NativeFunction)
where
N: Into<String>,
{
let name = name.into();
let mut function = Object::function(
Function::BuiltIn(function.into(), FunctionFlags::CALLABLE),
self.context
.global()
.get_field("Function")
.get_field("prototype"),
);

function.insert_field("length", Value::from(length));
function.insert_field("name", Value::from(name.as_str()));

self.object
.borrow_mut()
.insert_field(name, Value::from(function));
}

/// Add a property to the class, with the specified attribute.
///
/// It is added to `prototype`.
#[inline]
pub fn property<K, V>(&mut self, key: K, value: V, attribute: Attribute)
where
K: Into<PropertyKey>,
V: Into<Value>,
{
// We bitwise or (`|`) with `Attribute::default()` (`READONLY | NON_ENUMERABLE | PERMANENT`)
// so we dont get an empty attribute.
let property = Property::data_descriptor(value.into(), attribute | Attribute::default());
self.prototype
.borrow_mut()
.insert_property(key.into(), property);
}

/// Add a static property to the class, with the specified attribute.
///
/// It is added to class object itself.
#[inline]
pub fn static_property<K, V>(&mut self, key: K, value: V, attribute: Attribute)
where
K: Into<PropertyKey>,
V: Into<Value>,
{
// We bitwise or (`|`) with `Attribute::default()` (`READONLY | NON_ENUMERABLE | PERMANENT`)
// so we dont get an empty attribute.
let property = Property::data_descriptor(value.into(), attribute | Attribute::default());
self.object
.borrow_mut()
.insert_property(key.into(), property);
}

pub fn context(&mut self) -> &'_ mut Interpreter {
self.context
}
}

/// The internal representation of an JavaScript object.
#[derive(Debug, Trace, Finalize)]
pub struct Object {
Expand Down
Loading

0 comments on commit 76aba2a

Please sign in to comment.