From e97db0cbc24d9b46a82fe9898b94ee2faf8a9f26 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Sat, 30 Sep 2023 15:05:04 +0200 Subject: [PATCH] Rename function type --- boa_engine/src/builtins/function/mod.rs | 4 +- boa_engine/src/builtins/regexp/tests.rs | 2 +- boa_engine/src/object/internal_methods/mod.rs | 2 +- boa_engine/src/object/jsobject.rs | 18 +++++++-- boa_engine/src/object/mod.rs | 39 +++++++++---------- boa_engine/src/vm/code_block.rs | 18 +++++---- 6 files changed, 48 insertions(+), 35 deletions(-) diff --git a/boa_engine/src/builtins/function/mod.rs b/boa_engine/src/builtins/function/mod.rs index 91205719506..eeaf54f8fc3 100644 --- a/boa_engine/src/builtins/function/mod.rs +++ b/boa_engine/src/builtins/function/mod.rs @@ -215,7 +215,7 @@ unsafe impl Trace for FunctionKind { /// /// #[derive(Debug, Trace, Finalize)] -pub struct Function { +pub struct OrdinaryFunction { /// The code block containing the compiled function. pub(crate) code: Gc, @@ -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 { diff --git a/boa_engine/src/builtins/regexp/tests.rs b/boa_engine/src/builtins/regexp/tests.rs index 610185df759..29f9dbd69b2 100644 --- a/boa_engine/src/builtins/regexp/tests.rs +++ b/boa_engine/src/builtins/regexp/tests.rs @@ -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"), diff --git a/boa_engine/src/object/internal_methods/mod.rs b/boa_engine/src/object/internal_methods/mod.rs index 0abb1f857f9..4fdb576ab6b 100644 --- a/boa_engine/src/object/internal_methods/mod.rs +++ b/boa_engine/src/object/internal_methods/mod.rs @@ -918,7 +918,7 @@ pub(crate) fn non_existant_call( context: &mut Context<'_>, ) -> JsResult { Err(JsNativeError::typ() - .with_message("only callable objects / functions can be called") + .with_message("not a callable function") .with_realm(context.realm().clone()) .into()) } diff --git a/boa_engine/src/object/jsobject.rs b/boa_engine/src/object/jsobject.rs index 131da7d68a1..a6a2323d2ac 100644 --- a/boa_engine/src/object/jsobject.rs +++ b/boa_engine/src/object/jsobject.rs @@ -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. @@ -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"))); diff --git a/boa_engine/src/object/mod.rs b/boa_engine/src/object/mod.rs index 1be8aaf2d63..142649016c9 100644 --- a/boa_engine/src/object/mod.rs +++ b/boa_engine/src/object/mod.rs @@ -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, @@ -299,7 +299,7 @@ pub enum ObjectKind { AsyncGenerator(AsyncGenerator), /// The `AsyncGeneratorFunction` object kind. - AsyncGeneratorFunction(Function), + AsyncGeneratorFunction(OrdinaryFunction), /// The `Array` object kind. Array, @@ -335,7 +335,7 @@ pub enum ObjectKind { ForInIterator(ForInIterator), /// The `Function` object kind. - Function(Function), + OrdinaryFunction(OrdinaryFunction), /// The `BoundFunction` object kind. BoundFunction(BoundFunction), @@ -344,7 +344,7 @@ pub enum ObjectKind { Generator(Generator), /// The `GeneratorFunction` object kind. - GeneratorFunction(Function), + GeneratorFunction(OrdinaryFunction), /// A native rust function. NativeFunction { @@ -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 } => { @@ -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), @@ -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 { @@ -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 { @@ -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), @@ -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", @@ -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, } diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index b45ec692f81..69357b18d8b 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -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, @@ -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, @@ -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, @@ -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(); @@ -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, @@ -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 @@ -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, @@ -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,