Skip to content

Commit

Permalink
Rename function type
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Sep 30, 2023
1 parent bd93901 commit e97db0c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 35 deletions.
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ unsafe impl Trace for FunctionKind {
///
/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects>
#[derive(Debug, Trace, Finalize)]
pub struct Function {
pub struct OrdinaryFunction {
/// The code block containing the compiled function.
pub(crate) code: Gc<CodeBlock>,

Expand All @@ -238,7 +238,7 @@ pub struct Function {
pub(crate) kind: FunctionKind,
}

impl Function {
impl OrdinaryFunction {
/// Returns the codeblock of the function.
#[must_use]
pub fn codeblock(&self) -> &CodeBlock {
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/regexp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn species() {
// symbol-species
TestAction::assert_eq("descriptor.set", JsValue::undefined()),
TestAction::assert_with_op("accessor", |v, _| {
v.as_object().map_or(false, JsObject::is_function)
v.as_object().map_or(false, JsObject::is_native_function)
}),
TestAction::assert("!descriptor.enumerable"),
TestAction::assert("descriptor.configurable"),
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/object/internal_methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ pub(crate) fn non_existant_call(
context: &mut Context<'_>,
) -> JsResult<JsValue> {
Err(JsNativeError::typ()
.with_message("only callable objects / functions can be called")
.with_message("not a callable function")
.with_realm(context.realm().clone())
.into())
}
Expand Down
18 changes: 15 additions & 3 deletions boa_engine/src/object/jsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,20 @@ impl JsObject {
#[inline]
#[must_use]
#[track_caller]
pub fn is_function(&self) -> bool {
self.borrow().is_function()
pub fn is_ordinary_function(&self) -> bool {
self.borrow().is_ordinary_function()
}

/// Checks if it's a native `Function` object.
///
/// # Panics
///
/// Panics if the object is currently mutably borrowed.
#[inline]
#[must_use]
#[track_caller]
pub fn is_native_function(&self) -> bool {
self.borrow().is_native_function()
}

/// Checks if it's a `Generator` object.
Expand Down Expand Up @@ -1149,7 +1161,7 @@ impl Debug for JsObject {
let ptr: *const _ = self.as_ref();
let obj = self.borrow();
let kind = obj.kind();
if obj.is_function() {
if obj.is_ordinary_function() {
let name_prop = obj
.properties()
.get(&PropertyKey::String(JsString::from("name")));
Expand Down
39 changes: 19 additions & 20 deletions boa_engine/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::{
async_generator::AsyncGenerator,
error::ErrorKind,
function::arguments::Arguments,
function::{arguments::ParameterMap, BoundFunction, ConstructorKind, Function},
function::{arguments::ParameterMap, BoundFunction, ConstructorKind, OrdinaryFunction},
generator::Generator,
iterable::AsyncFromSyncIterator,
map::ordered_map::OrderedMap,
Expand Down Expand Up @@ -299,7 +299,7 @@ pub enum ObjectKind {
AsyncGenerator(AsyncGenerator),

/// The `AsyncGeneratorFunction` object kind.
AsyncGeneratorFunction(Function),
AsyncGeneratorFunction(OrdinaryFunction),

/// The `Array` object kind.
Array,
Expand Down Expand Up @@ -335,7 +335,7 @@ pub enum ObjectKind {
ForInIterator(ForInIterator),

/// The `Function` object kind.
Function(Function),
OrdinaryFunction(OrdinaryFunction),

/// The `BoundFunction` object kind.
BoundFunction(BoundFunction),
Expand All @@ -344,7 +344,7 @@ pub enum ObjectKind {
Generator(Generator),

/// The `GeneratorFunction` object kind.
GeneratorFunction(Function),
GeneratorFunction(OrdinaryFunction),

/// A native rust function.
NativeFunction {
Expand Down Expand Up @@ -459,7 +459,7 @@ unsafe impl Trace for ObjectKind {
Self::RegExpStringIterator(i) => mark(i),
Self::DataView(v) => mark(v),
Self::ForInIterator(i) => mark(i),
Self::Function(f) | Self::GeneratorFunction(f) | Self::AsyncGeneratorFunction(f) => mark(f),
Self::OrdinaryFunction(f) | Self::GeneratorFunction(f) | Self::AsyncGeneratorFunction(f) => mark(f),
Self::BoundFunction(f) => mark(f),
Self::Generator(g) => mark(g),
Self::NativeFunction { function, constructor: _, realm } => {
Expand Down Expand Up @@ -536,7 +536,7 @@ impl ObjectData {

/// Create the `AsyncGeneratorFunction` object data
#[must_use]
pub fn async_generator_function(function: Function) -> Self {
pub fn async_generator_function(function: OrdinaryFunction) -> Self {
Self {
internal_methods: &FUNCTION_INTERNAL_METHODS,
kind: ObjectKind::GeneratorFunction(function),
Expand Down Expand Up @@ -651,9 +651,9 @@ impl ObjectData {
}
}

/// Create the `Function` object data
/// Create the ordinary function object data
#[must_use]
pub fn function(function: Function, constructor: bool) -> Self {
pub fn ordinary_function(function: OrdinaryFunction, constructor: bool) -> Self {
let internal_methods = if constructor {
&CONSTRUCTOR_INTERNAL_METHODS
} else {
Expand All @@ -662,11 +662,11 @@ impl ObjectData {

Self {
internal_methods,
kind: ObjectKind::Function(function),
kind: ObjectKind::OrdinaryFunction(function),
}
}

/// Create the `Function` object data
/// Create the native function object data
#[must_use]
pub fn native_function(function: NativeFunction, constructor: bool, realm: Realm) -> Self {
let internal_methods = if constructor {
Expand Down Expand Up @@ -709,7 +709,7 @@ impl ObjectData {

/// Create the `GeneratorFunction` object data
#[must_use]
pub fn generator_function(function: Function) -> Self {
pub fn generator_function(function: OrdinaryFunction) -> Self {
Self {
internal_methods: &FUNCTION_INTERNAL_METHODS,
kind: ObjectKind::GeneratorFunction(function),
Expand Down Expand Up @@ -969,7 +969,7 @@ impl Debug for ObjectKind {
Self::ArrayIterator(_) => "ArrayIterator",
Self::ArrayBuffer(_) => "ArrayBuffer",
Self::ForInIterator(_) => "ForInIterator",
Self::Function(_) => "Function",
Self::OrdinaryFunction(_) => "Function",
Self::BoundFunction(_) => "BoundFunction",
Self::Generator(_) => "Generator",
Self::GeneratorFunction(_) => "GeneratorFunction",
Expand Down Expand Up @@ -1307,30 +1307,29 @@ impl Object {
/// Checks if the object is a `Function` object.
#[inline]
#[must_use]
pub const fn is_function(&self) -> bool {
pub const fn is_ordinary_function(&self) -> bool {
matches!(
self.kind,
ObjectKind::Function(_) | ObjectKind::GeneratorFunction(_)
ObjectKind::OrdinaryFunction(_) | ObjectKind::GeneratorFunction(_)
)
}

/// Gets the function data if the object is a `Function`.
#[inline]
#[must_use]
pub const fn as_function(&self) -> Option<&Function> {
pub const fn as_function(&self) -> Option<&OrdinaryFunction> {
match self.kind {
ObjectKind::Function(ref function) | ObjectKind::GeneratorFunction(ref function) => {
Some(function)
}
ObjectKind::OrdinaryFunction(ref function)
| ObjectKind::GeneratorFunction(ref function) => Some(function),
_ => None,
}
}

/// Gets the mutable function data if the object is a `Function`.
#[inline]
pub fn as_function_mut(&mut self) -> Option<&mut Function> {
pub fn as_function_mut(&mut self) -> Option<&mut OrdinaryFunction> {
match self.kind {
ObjectKind::Function(ref mut function)
ObjectKind::OrdinaryFunction(ref mut function)
| ObjectKind::GeneratorFunction(ref mut function) => Some(function),
_ => None,
}
Expand Down
18 changes: 10 additions & 8 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! This module is for the `CodeBlock` which implements a function representation in the VM
use crate::{
builtins::function::{arguments::Arguments, ConstructorKind, Function, FunctionKind, ThisMode},
builtins::function::{
arguments::Arguments, ConstructorKind, FunctionKind, OrdinaryFunction, ThisMode,
},
context::intrinsics::StandardConstructors,
environments::{BindingLocator, CompileTimeEnvironment, FunctionSlots, ThisBindingStatus},
error::JsNativeError,
Expand Down Expand Up @@ -769,7 +771,7 @@ pub(crate) fn create_function_object(
let script_or_module = context.get_active_script_or_module();

let function = if r#async {
Function {
OrdinaryFunction {
code,
environments: context.vm.environments.clone(),
home_object: None,
Expand All @@ -779,7 +781,7 @@ pub(crate) fn create_function_object(
realm: context.realm().clone(),
}
} else {
Function {
OrdinaryFunction {
code,
environments: context.vm.environments.clone(),
home_object: None,
Expand All @@ -794,7 +796,7 @@ pub(crate) fn create_function_object(
}
};

let data = ObjectData::function(function, !r#async);
let data = ObjectData::ordinary_function(function, !r#async);

let templates = context.intrinsics().templates();

Expand Down Expand Up @@ -857,7 +859,7 @@ pub(crate) fn create_function_object_fast(
}
};

let function = Function {
let function = OrdinaryFunction {
code,
environments: context.vm.environments.clone(),
class_object: None,
Expand All @@ -867,7 +869,7 @@ pub(crate) fn create_function_object_fast(
realm: context.realm().clone(),
};

let data = ObjectData::function(function, !method && !arrow && !r#async);
let data = ObjectData::ordinary_function(function, !method && !arrow && !r#async);

if r#async {
context
Expand Down Expand Up @@ -950,7 +952,7 @@ pub(crate) fn create_generator_function_object(
let script_or_module = context.get_active_script_or_module();

let constructor = if r#async {
let function = Function {
let function = OrdinaryFunction {
code,
environments: context.vm.environments.clone(),
home_object: None,
Expand All @@ -965,7 +967,7 @@ pub(crate) fn create_generator_function_object(
ObjectData::async_generator_function(function),
)
} else {
let function = Function {
let function = OrdinaryFunction {
code,
environments: context.vm.environments.clone(),
home_object: None,
Expand Down

0 comments on commit e97db0c

Please sign in to comment.