Skip to content

Commit

Permalink
Merge a057c58 into d025207
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Aug 18, 2020
2 parents d025207 + a057c58 commit 7d99582
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 237 deletions.
84 changes: 84 additions & 0 deletions boa/examples/classes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use boa::builtins::object::{Class, ClassBuilder};
use boa::builtins::value::*;
use boa::exec::*;
use boa::realm::Realm;
use boa::*;

use gc::{Finalize, Trace};

#[derive(Debug, Trace, Finalize)]
struct Person {
name: String,
age: u32,
}

impl Person {
fn say_hello(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result<Value> {
if let Some(object) = this.as_object() {
if let Some(person) = object.downcast_ref::<Person>() {
println!(
"Hello my name is {}, I'm {} years old",
person.name, person.age
);
return Ok(Value::undefined());
}
}
ctx.throw_type_error("'this' is not a Person object")
}
}

impl Class for Person {
const NAME: &'static str = "Person";
const LENGTH: usize = 2;

fn constructor(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Self> {
let name = args.get(0).cloned().unwrap_or_default().to_string(ctx)?;
let age = args.get(1).cloned().unwrap_or_default().to_u32(ctx)?;

let person = Person {
name: name.to_string(),
age,
};

Ok(person)
}

fn methods(class: &mut ClassBuilder) -> Result<()> {
class.method("sayHello", 0, Self::say_hello);
class.static_method("is", 1, |_this, args, _ctx| {
if let Some(arg) = args.get(0) {
if let Some(object) = arg.as_object() {
if object.is::<Person>() {
return Ok(true.into());
}
}
}
Ok(false.into())
});

Ok(())
}
}

fn main() {
let realm = Realm::create();
let mut context = Interpreter::new(realm);

context.register_global_class::<Person>().unwrap();

forward_val(
&mut context,
r"
let person = new Person('John', 19);
person.sayHello();
if (Person.is(person)) {
console.log('person is a Person class instance.');
}
if (!Person.is('Hello')) {
console.log('\'Hello\' string is not a Person class instance.');
}
",
)
.unwrap();
}
6 changes: 3 additions & 3 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ unsafe impl Trace for FunctionBody {

bitflags! {
#[derive(Finalize, Default)]
struct FunctionFlags: u8 {
pub(crate) struct FunctionFlags: u8 {
const CALLABLE = 0b0000_0001;
const CONSTRUCTABLE = 0b0000_0010;
}
}

impl FunctionFlags {
fn from_parameters(callable: bool, constructable: bool) -> Self {
pub(crate) fn from_parameters(callable: bool, constructable: bool) -> Self {
let mut flags = Self::default();

if callable {
Expand Down Expand Up @@ -142,7 +142,7 @@ pub struct Function {
// Environment, built-in functions don't need Environments
pub environment: Option<Environment>,
/// Is it constructable or
flags: FunctionFlags,
pub(crate) flags: FunctionFlags,
}

impl Function {
Expand Down
11 changes: 6 additions & 5 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ impl Json {
holder: &mut Value,
key: Value,
) -> Result<Value> {
let mut value = holder.get_field(key.clone());
let value = holder.get_field(key.clone());

let obj = value.as_object().as_deref().cloned();
if let Some(obj) = obj {
for key in obj.properties().keys() {
let v = Self::walk(reviver, ctx, &mut value, Value::from(key.as_str()));
if let Value::Object(ref object) = value {
let keys: Vec<_> = object.borrow().properties().keys().cloned().collect();

for key in keys {
let v = Self::walk(reviver, ctx, &mut value.clone(), Value::from(key.as_str()));
match v {
Ok(v) if !v.is_undefined() => {
value.set_field(key.as_str(), v);
Expand Down
82 changes: 40 additions & 42 deletions boa/src/builtins/object/internal_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ impl Object {
return Value::undefined();
}

let parent_obj = Object::from(&parent).expect("Failed to get object");

return parent_obj.get(property_key);
return parent.get_field(property_key);
}

if desc.is_data_descriptor() {
Expand Down Expand Up @@ -298,45 +296,45 @@ impl Object {
}
}

/// `Object.setPropertyOf(obj, prototype)`
///
/// This method sets the prototype (i.e., the internal `[[Prototype]]` property)
/// of a specified object to another object or `null`.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
pub fn set_prototype_of(&mut self, val: Value) -> bool {
debug_assert!(val.is_object() || val.is_null());
let current = self.prototype.clone();
if same_value(&current, &val) {
return true;
}
if !self.is_extensible() {
return false;
}
let mut p = val.clone();
let mut done = false;
while !done {
if p.is_null() {
done = true
} else if same_value(&Value::from(self.clone()), &p) {
return false;
} else {
let prototype = p
.as_object()
.expect("prototype should be null or object")
.prototype
.clone();
p = prototype;
}
}
self.prototype = val;
true
}
// /// `Object.setPropertyOf(obj, prototype)`
// ///
// /// This method sets the prototype (i.e., the internal `[[Prototype]]` property)
// /// of a specified object to another object or `null`.
// ///
// /// More information:
// /// - [ECMAScript reference][spec]
// /// - [MDN documentation][mdn]
// ///
// /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
// /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
// pub fn set_prototype_of(&mut self, val: Value) -> bool {
// debug_assert!(val.is_object() || val.is_null());
// let current = self.prototype.clone();
// if same_value(&current, &val) {
// return true;
// }
// if !self.is_extensible() {
// return false;
// }
// let mut p = val.clone();
// let mut done = false;
// while !done {
// if p.is_null() {
// done = true
// } else if same_value(&Value::from(self.clone()), &p) {
// return false;
// } else {
// let prototype = p
// .as_object()
// .expect("prototype should be null or object")
// .prototype
// .clone();
// p = prototype;
// }
// }
// self.prototype = val;
// true
// }

/// Returns either the prototype or null
///
Expand Down
64 changes: 0 additions & 64 deletions boa/src/builtins/object/internal_state.rs

This file was deleted.

0 comments on commit 7d99582

Please sign in to comment.