Skip to content

Commit

Permalink
Feature JsFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Apr 24, 2022
1 parent 3226e54 commit 257097b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 42 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
10 changes: 6 additions & 4 deletions boa_engine/src/builtins/intl/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use crate::{
context::intrinsics::StandardConstructors,
object::internal_methods::get_prototype_from_constructor,
object::{ConstructorBuilder, JsObject, ObjectData},
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, JsFunction, JsObject,
ObjectData,
},
Context, JsResult, JsString, JsValue,
};

Expand Down Expand Up @@ -44,7 +46,7 @@ pub struct DateTimeFormat {
impl DateTimeFormat {
const NAME: &'static str = "DateTimeFormat";

pub(super) fn init(context: &mut Context) -> JsObject {
pub(super) fn init(context: &mut Context) -> JsFunction {
let _timer = Profiler::global().start_event(Self::NAME, "init");

ConstructorBuilder::new(context, Self::constructor)
Expand Down Expand Up @@ -109,6 +111,6 @@ impl DateTimeFormat {
// TODO b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this).

// 5. Return dateTimeFormat.
Ok(JsValue::Object(date_time_format))
Ok(date_time_format.into())
}
}
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
22 changes: 11 additions & 11 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 @@ -34,9 +34,9 @@ impl JsArray {
}
}

/// Create an array from a `JsObject`, if the object is not an array throw a `TypeError`.
/// Create a [`JsArray`] from a [`JsObject`], if the object is not an array throw a `TypeError`.
///
/// This does not copy the fields of the array, it only does a shallow copy.
/// This does not clone the fields of the array, it only does a shallow clone of the object.
#[inline]
pub fn from_object(object: JsObject, context: &mut Context) -> JsResult<Self> {
if object.borrow().is_array() {
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
56 changes: 56 additions & 0 deletions boa_engine/src/object/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 }
}

/// Create a [`JsFunction`] from a [`JsObject`], if the object is not a function throw a `TypeError`.
///
/// This does not clone the fields of the function, it only does a shallow clone of the 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 {}
23 changes: 13 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 @@ -14,14 +14,17 @@ pub struct JsTypedArray {
}

impl JsTypedArray {
/// Create a [`JsTypedArray`] from a [`JsObject`], if the object is not a typed array throw a `TypeError`.
///
/// This does not clone the fields of the typed array, it only does a shallow clone of the object.
#[inline]
pub fn from_object(object: JsObject, context: &mut Context) -> JsResult<Self> {
if object.borrow().is_typed_array() {
Ok(Self {
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 +94,7 @@ impl JsTypedArray {

pub fn every(
&self,
predicate: JsObject,
predicate: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<bool> {
Expand All @@ -109,7 +112,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 +128,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 +137,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 +153,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 +169,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 +183,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 +219,7 @@ impl JsTypedArray {
#[inline]
pub fn find(
&self,
predicate: JsObject,
predicate: JsFunction,
this_arg: Option<JsValue>,
context: &mut Context,
) -> JsResult<JsValue> {
Expand Down
Loading

0 comments on commit 257097b

Please sign in to comment.