Skip to content

Commit

Permalink
Merge 2e236a1 into a357a18
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored Apr 16, 2022
2 parents a357a18 + 2e236a1 commit 3b74e5d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 36 deletions.
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
context::intrinsics::StandardConstructors,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder,
JsObject, ObjectData,
JsFunction, JsObject, ObjectData,
},
property::{Attribute, PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
Expand Down Expand Up @@ -2857,7 +2857,7 @@ impl Array {
}
}

pub(crate) fn values_intrinsic(context: &mut Context) -> JsObject {
pub(crate) fn values_intrinsic(context: &mut Context) -> JsFunction {
FunctionBuilder::native(context, Self::values)
.name("values")
.length(0)
Expand Down
14 changes: 7 additions & 7 deletions boa_engine/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
use crate::{
builtins::function::NativeFunctionSignature,
object::{ConstructorBuilder, JsObject, NativeObject, ObjectData, PROTOTYPE},
object::{ConstructorBuilder, JsFunction, JsObject, NativeObject, ObjectData, PROTOTYPE},
property::{Attribute, PropertyDescriptor, PropertyKey},
Context, JsResult, JsValue,
};
Expand Down Expand Up @@ -168,8 +168,8 @@ impl<'context> ClassBuilder<'context> {
}

#[inline]
pub(crate) fn build(mut self) -> JsObject {
self.builder.build()
pub(crate) fn build(mut self) -> JsFunction {
JsFunction::from_object_unchecked(self.builder.build().into())
}

/// Add a method to the class.
Expand Down Expand Up @@ -239,8 +239,8 @@ impl<'context> ClassBuilder<'context> {
pub fn accessor<K>(
&mut self,
key: K,
get: Option<JsObject>,
set: Option<JsObject>,
get: Option<JsFunction>,
set: Option<JsFunction>,
attribute: Attribute,
) -> &mut Self
where
Expand All @@ -257,8 +257,8 @@ impl<'context> ClassBuilder<'context> {
pub fn static_accessor<K>(
&mut self,
key: K,
get: Option<JsObject>,
set: Option<JsObject>,
get: Option<JsFunction>,
set: Option<JsFunction>,
attribute: Attribute,
) -> &mut Self
where
Expand Down
18 changes: 9 additions & 9 deletions boa_engine/src/object/jsarray.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
builtins::Array,
object::{JsObject, JsObjectType},
object::{JsFunction, JsObject, JsObjectType},
value::IntoOrUndefined,
Context, JsResult, JsString, JsValue,
};
Expand Down Expand Up @@ -207,7 +207,7 @@ impl JsArray {
#[inline]
pub fn find(
&self,
predicate: JsObject,
predicate: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand All @@ -221,7 +221,7 @@ impl JsArray {
#[inline]
pub fn filter(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<Self> {
Expand All @@ -240,7 +240,7 @@ impl JsArray {
#[inline]
pub fn map(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<Self> {
Expand All @@ -259,7 +259,7 @@ impl JsArray {
#[inline]
pub fn every(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<bool> {
Expand All @@ -277,7 +277,7 @@ impl JsArray {
#[inline]
pub fn some(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<bool> {
Expand All @@ -293,7 +293,7 @@ impl JsArray {
}

#[inline]
pub fn sort(&self, compare_fn: Option<JsObject>, context: &mut Context) -> JsResult<Self> {
pub fn sort(&self, compare_fn: Option<JsFunction>, context: &mut Context) -> JsResult<Self> {
Array::sort(
&self.inner.clone().into(),
&[compare_fn.into_or_undefined()],
Expand Down Expand Up @@ -325,7 +325,7 @@ impl JsArray {
#[inline]
pub fn reduce(
&self,
callback: JsObject,
callback: JsFunction,
initial_value: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand All @@ -339,7 +339,7 @@ impl JsArray {
#[inline]
pub fn reduce_right(
&self,
callback: JsObject,
callback: JsFunction,
initial_value: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand Down
53 changes: 53 additions & 0 deletions boa_engine/src/object/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::{
object::{JsObject, JsObjectType},
Context, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use std::ops::Deref;

/// JavaScript `Function` rust object.
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsFunction {
inner: JsObject,
}

impl JsFunction {
#[inline]
pub(crate) fn from_object_unchecked(object: JsObject) -> Self {
Self { inner: object }
}

#[inline]
pub fn from_object(object: JsObject, context: &mut Context) -> JsResult<Self> {
if object.borrow().is_function() {
Ok(Self::from_object_unchecked(object))
} else {
context.throw_type_error("object is not an Function")
}
}
}

impl From<JsFunction> for JsObject {
#[inline]
fn from(o: JsFunction) -> Self {
o.inner.clone()
}
}

impl From<JsFunction> for JsValue {
#[inline]
fn from(o: JsFunction) -> Self {
o.inner.clone().into()
}
}

impl Deref for JsFunction {
type Target = JsObject;

#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl JsObjectType for JsFunction {}
20 changes: 10 additions & 10 deletions boa_engine/src/object/jstypedarray.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
builtins::typed_array::TypedArray,
object::{JsArray, JsObject, JsObjectType},
object::{JsArray, JsFunction, JsObject, JsObjectType},
value::IntoOrUndefined,
Context, JsResult, JsString, JsValue,
};
Expand All @@ -21,7 +21,7 @@ impl JsTypedArray {
inner: object.into(),
})
} else {
context.throw_type_error("object is not an TypedArray")
context.throw_type_error("object is not a TypedArray")
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ impl JsTypedArray {

pub fn every(
&self,
predicate: JsObject,
predicate: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<bool> {
Expand All @@ -109,7 +109,7 @@ impl JsTypedArray {
#[inline]
pub fn some(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<bool> {
Expand All @@ -125,7 +125,7 @@ impl JsTypedArray {
}

#[inline]
pub fn sort(&self, compare_fn: Option<JsObject>, context: &mut Context) -> JsResult<Self> {
pub fn sort(&self, compare_fn: Option<JsFunction>, context: &mut Context) -> JsResult<Self> {
TypedArray::sort(&self.inner, &[compare_fn.into_or_undefined()], context)?;

Ok(self.clone())
Expand All @@ -134,7 +134,7 @@ impl JsTypedArray {
#[inline]
pub fn filter(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<Self> {
Expand All @@ -150,7 +150,7 @@ impl JsTypedArray {
#[inline]
pub fn map(
&self,
callback: JsObject,
callback: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<Self> {
Expand All @@ -166,7 +166,7 @@ impl JsTypedArray {
#[inline]
pub fn reduce(
&self,
callback: JsObject,
callback: JsFunction,
initial_value: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand All @@ -180,7 +180,7 @@ impl JsTypedArray {
#[inline]
pub fn reduce_right(
&self,
callback: JsObject,
callback: JsFunction,
initial_value: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand Down Expand Up @@ -216,7 +216,7 @@ impl JsTypedArray {
#[inline]
pub fn find(
&self,
predicate: JsObject,
predicate: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand Down
18 changes: 10 additions & 8 deletions boa_engine/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ mod tests;

pub(crate) mod internal_methods;
mod jsarray;
mod jsfunction;
mod jsobject;
mod jstypedarray;
mod operations;
mod property_map;

pub use jsarray::*;
pub use jsfunction::*;
pub use jstypedarray::*;

pub(crate) trait JsObjectType:
Expand Down Expand Up @@ -1505,7 +1507,7 @@ impl<'context> FunctionBuilder<'context> {

/// Build the function object.
#[inline]
pub fn build(self) -> JsObject {
pub fn build(self) -> JsFunction {
let function = JsObject::from_proto_and_data(
self.context
.intrinsics()
Expand All @@ -1521,7 +1523,7 @@ impl<'context> FunctionBuilder<'context> {
function.insert_property("length", property.clone().value(self.length));
function.insert_property("name", property.value(self.name));

function
JsFunction::from_object_unchecked(function)
}

/// Initializes the `Function.prototype` function object.
Expand Down Expand Up @@ -1806,8 +1808,8 @@ impl<'context> ConstructorBuilder<'context> {
pub fn accessor<K>(
&mut self,
key: K,
get: Option<JsObject>,
set: Option<JsObject>,
get: Option<JsFunction>,
set: Option<JsFunction>,
attribute: Attribute,
) -> &mut Self
where
Expand All @@ -1827,8 +1829,8 @@ impl<'context> ConstructorBuilder<'context> {
pub fn static_accessor<K>(
&mut self,
key: K,
get: Option<JsObject>,
set: Option<JsObject>,
get: Option<JsFunction>,
set: Option<JsFunction>,
attribute: Attribute,
) -> &mut Self
where
Expand Down Expand Up @@ -1941,7 +1943,7 @@ impl<'context> ConstructorBuilder<'context> {
}

/// Build the constructor function object.
pub fn build(&mut self) -> JsObject {
pub fn build(&mut self) -> JsFunction {
// Create the native function
let function = Function::Native {
function: self.function,
Expand Down Expand Up @@ -2013,6 +2015,6 @@ impl<'context> ConstructorBuilder<'context> {
}
}

self.object.clone()
JsFunction::from_object_unchecked(self.object.clone())
}
}

0 comments on commit 3b74e5d

Please sign in to comment.