|
|
@ -3,7 +3,7 @@ |
|
|
|
use crate::{ |
|
|
|
use crate::{ |
|
|
|
builtins::{ |
|
|
|
builtins::{ |
|
|
|
array::array_iterator::ArrayIterator, |
|
|
|
array::array_iterator::ArrayIterator, |
|
|
|
function::{BuiltInFunction, Function, FunctionFlags, NativeFunction}, |
|
|
|
function::{Function, NativeFunction}, |
|
|
|
map::map_iterator::MapIterator, |
|
|
|
map::map_iterator::MapIterator, |
|
|
|
map::ordered_map::OrderedMap, |
|
|
|
map::ordered_map::OrderedMap, |
|
|
|
regexp::regexp_string_iterator::RegExpStringIterator, |
|
|
|
regexp::regexp_string_iterator::RegExpStringIterator, |
|
|
@ -22,6 +22,7 @@ use std::{ |
|
|
|
any::Any, |
|
|
|
any::Any, |
|
|
|
fmt::{self, Debug, Display}, |
|
|
|
fmt::{self, Debug, Display}, |
|
|
|
ops::{Deref, DerefMut}, |
|
|
|
ops::{Deref, DerefMut}, |
|
|
|
|
|
|
|
rc::Rc, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
#[cfg(test)] |
|
|
@ -264,7 +265,7 @@ impl Object { |
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-iscallable
|
|
|
|
/// [spec]: https://tc39.es/ecma262/#sec-iscallable
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn is_callable(&self) -> bool { |
|
|
|
pub fn is_callable(&self) -> bool { |
|
|
|
matches!(self.data, ObjectData::Function(ref f) if f.is_callable()) |
|
|
|
matches!(self.data, ObjectData::Function(_)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// It determines if Object is a function object with a `[[Construct]]` internal method.
|
|
|
|
/// It determines if Object is a function object with a `[[Construct]]` internal method.
|
|
|
@ -682,24 +683,40 @@ where |
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
pub struct FunctionBuilder<'context> { |
|
|
|
pub struct FunctionBuilder<'context> { |
|
|
|
context: &'context mut Context, |
|
|
|
context: &'context mut Context, |
|
|
|
function: BuiltInFunction, |
|
|
|
function: Option<Function>, |
|
|
|
name: Option<String>, |
|
|
|
name: JsString, |
|
|
|
length: usize, |
|
|
|
length: usize, |
|
|
|
callable: bool, |
|
|
|
|
|
|
|
constructable: bool, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'context> FunctionBuilder<'context> { |
|
|
|
impl<'context> FunctionBuilder<'context> { |
|
|
|
/// Create a new `FunctionBuilder`
|
|
|
|
/// Create a new `FunctionBuilder` for creating a native function.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn new(context: &'context mut Context, function: NativeFunction) -> Self { |
|
|
|
pub fn native(context: &'context mut Context, function: NativeFunction) -> Self { |
|
|
|
Self { |
|
|
|
Self { |
|
|
|
context, |
|
|
|
context, |
|
|
|
function: function.into(), |
|
|
|
function: Some(Function::Native { |
|
|
|
name: None, |
|
|
|
function: function.into(), |
|
|
|
|
|
|
|
constructable: false, |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
name: JsString::default(), |
|
|
|
|
|
|
|
length: 0, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new `FunctionBuilder` for creating a closure function.
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
|
|
pub fn closure<F>(context: &'context mut Context, function: F) -> Self |
|
|
|
|
|
|
|
where |
|
|
|
|
|
|
|
F: Fn(&Value, &[Value], &mut Context) -> Result<Value, Value> + 'static, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Self { |
|
|
|
|
|
|
|
context, |
|
|
|
|
|
|
|
function: Some(Function::Closure { |
|
|
|
|
|
|
|
function: Rc::new(function), |
|
|
|
|
|
|
|
constructable: false, |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
name: JsString::default(), |
|
|
|
length: 0, |
|
|
|
length: 0, |
|
|
|
callable: true, |
|
|
|
|
|
|
|
constructable: false, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -711,7 +728,7 @@ impl<'context> FunctionBuilder<'context> { |
|
|
|
where |
|
|
|
where |
|
|
|
N: AsRef<str>, |
|
|
|
N: AsRef<str>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
self.name = Some(name.as_ref().into()); |
|
|
|
self.name = name.as_ref().into(); |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -726,21 +743,16 @@ impl<'context> FunctionBuilder<'context> { |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Specify the whether the object function object can be called.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// The default is `true`.
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
|
|
pub fn callable(&mut self, yes: bool) -> &mut Self { |
|
|
|
|
|
|
|
self.callable = yes; |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Specify the whether the object function object can be called with `new` keyword.
|
|
|
|
/// Specify the whether the object function object can be called with `new` keyword.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// The default is `false`.
|
|
|
|
/// The default is `false`.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn constructable(&mut self, yes: bool) -> &mut Self { |
|
|
|
pub fn constructable(&mut self, yes: bool) -> &mut Self { |
|
|
|
self.constructable = yes; |
|
|
|
match self.function.as_mut() { |
|
|
|
|
|
|
|
Some(Function::Native { constructable, .. }) => *constructable = yes, |
|
|
|
|
|
|
|
Some(Function::Closure { constructable, .. }) => *constructable = yes, |
|
|
|
|
|
|
|
_ => unreachable!(), |
|
|
|
|
|
|
|
} |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -748,10 +760,7 @@ impl<'context> FunctionBuilder<'context> { |
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn build(&mut self) -> GcObject { |
|
|
|
pub fn build(&mut self) -> GcObject { |
|
|
|
let mut function = Object::function( |
|
|
|
let mut function = Object::function( |
|
|
|
Function::BuiltIn( |
|
|
|
self.function.take().unwrap(), |
|
|
|
self.function, |
|
|
|
|
|
|
|
FunctionFlags::from_parameters(self.callable, self.constructable), |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
self.context |
|
|
|
self.context |
|
|
|
.standard_objects() |
|
|
|
.standard_objects() |
|
|
|
.function_object() |
|
|
|
.function_object() |
|
|
@ -759,11 +768,7 @@ impl<'context> FunctionBuilder<'context> { |
|
|
|
.into(), |
|
|
|
.into(), |
|
|
|
); |
|
|
|
); |
|
|
|
let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE; |
|
|
|
let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE; |
|
|
|
if let Some(name) = self.name.take() { |
|
|
|
function.insert_property("name", self.name.clone(), attribute); |
|
|
|
function.insert_property("name", name, attribute); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
function.insert_property("name", "", attribute); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
function.insert_property("length", self.length, attribute); |
|
|
|
function.insert_property("length", self.length, attribute); |
|
|
|
|
|
|
|
|
|
|
|
GcObject::new(function) |
|
|
|
GcObject::new(function) |
|
|
@ -772,10 +777,7 @@ impl<'context> FunctionBuilder<'context> { |
|
|
|
/// Initializes the `Function.prototype` function object.
|
|
|
|
/// Initializes the `Function.prototype` function object.
|
|
|
|
pub(crate) fn build_function_prototype(&mut self, object: &GcObject) { |
|
|
|
pub(crate) fn build_function_prototype(&mut self, object: &GcObject) { |
|
|
|
let mut object = object.borrow_mut(); |
|
|
|
let mut object = object.borrow_mut(); |
|
|
|
object.data = ObjectData::Function(Function::BuiltIn( |
|
|
|
object.data = ObjectData::Function(self.function.take().unwrap()); |
|
|
|
self.function, |
|
|
|
|
|
|
|
FunctionFlags::from_parameters(self.callable, self.constructable), |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
object.set_prototype_instance( |
|
|
|
object.set_prototype_instance( |
|
|
|
self.context |
|
|
|
self.context |
|
|
|
.standard_objects() |
|
|
|
.standard_objects() |
|
|
@ -783,12 +785,8 @@ impl<'context> FunctionBuilder<'context> { |
|
|
|
.prototype() |
|
|
|
.prototype() |
|
|
|
.into(), |
|
|
|
.into(), |
|
|
|
); |
|
|
|
); |
|
|
|
let attribute = Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT; |
|
|
|
let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE; |
|
|
|
if let Some(name) = self.name.take() { |
|
|
|
object.insert_property("name", self.name.clone(), attribute); |
|
|
|
object.insert_property("name", name, attribute); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
object.insert_property("name", "", attribute); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
object.insert_property("length", self.length, attribute); |
|
|
|
object.insert_property("length", self.length, attribute); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -836,10 +834,9 @@ impl<'context> ObjectInitializer<'context> { |
|
|
|
B: Into<FunctionBinding>, |
|
|
|
B: Into<FunctionBinding>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
let binding = binding.into(); |
|
|
|
let binding = binding.into(); |
|
|
|
let function = FunctionBuilder::new(self.context, function) |
|
|
|
let function = FunctionBuilder::native(self.context, function) |
|
|
|
.name(binding.name) |
|
|
|
.name(binding.name) |
|
|
|
.length(length) |
|
|
|
.length(length) |
|
|
|
.callable(true) |
|
|
|
|
|
|
|
.constructable(false) |
|
|
|
.constructable(false) |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
@ -876,7 +873,7 @@ pub struct ConstructorBuilder<'context> { |
|
|
|
constructor_function: NativeFunction, |
|
|
|
constructor_function: NativeFunction, |
|
|
|
constructor_object: GcObject, |
|
|
|
constructor_object: GcObject, |
|
|
|
prototype: GcObject, |
|
|
|
prototype: GcObject, |
|
|
|
name: Option<String>, |
|
|
|
name: JsString, |
|
|
|
length: usize, |
|
|
|
length: usize, |
|
|
|
callable: bool, |
|
|
|
callable: bool, |
|
|
|
constructable: bool, |
|
|
|
constructable: bool, |
|
|
@ -907,7 +904,7 @@ impl<'context> ConstructorBuilder<'context> { |
|
|
|
constructor_object: GcObject::new(Object::default()), |
|
|
|
constructor_object: GcObject::new(Object::default()), |
|
|
|
prototype: GcObject::new(Object::default()), |
|
|
|
prototype: GcObject::new(Object::default()), |
|
|
|
length: 0, |
|
|
|
length: 0, |
|
|
|
name: None, |
|
|
|
name: JsString::default(), |
|
|
|
callable: true, |
|
|
|
callable: true, |
|
|
|
constructable: true, |
|
|
|
constructable: true, |
|
|
|
inherit: None, |
|
|
|
inherit: None, |
|
|
@ -926,7 +923,7 @@ impl<'context> ConstructorBuilder<'context> { |
|
|
|
constructor_object: object.constructor, |
|
|
|
constructor_object: object.constructor, |
|
|
|
prototype: object.prototype, |
|
|
|
prototype: object.prototype, |
|
|
|
length: 0, |
|
|
|
length: 0, |
|
|
|
name: None, |
|
|
|
name: JsString::default(), |
|
|
|
callable: true, |
|
|
|
callable: true, |
|
|
|
constructable: true, |
|
|
|
constructable: true, |
|
|
|
inherit: None, |
|
|
|
inherit: None, |
|
|
@ -940,10 +937,9 @@ impl<'context> ConstructorBuilder<'context> { |
|
|
|
B: Into<FunctionBinding>, |
|
|
|
B: Into<FunctionBinding>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
let binding = binding.into(); |
|
|
|
let binding = binding.into(); |
|
|
|
let function = FunctionBuilder::new(self.context, function) |
|
|
|
let function = FunctionBuilder::native(self.context, function) |
|
|
|
.name(binding.name) |
|
|
|
.name(binding.name) |
|
|
|
.length(length) |
|
|
|
.length(length) |
|
|
|
.callable(true) |
|
|
|
|
|
|
|
.constructable(false) |
|
|
|
.constructable(false) |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
@ -967,10 +963,9 @@ impl<'context> ConstructorBuilder<'context> { |
|
|
|
B: Into<FunctionBinding>, |
|
|
|
B: Into<FunctionBinding>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
let binding = binding.into(); |
|
|
|
let binding = binding.into(); |
|
|
|
let function = FunctionBuilder::new(self.context, function) |
|
|
|
let function = FunctionBuilder::native(self.context, function) |
|
|
|
.name(binding.name) |
|
|
|
.name(binding.name) |
|
|
|
.length(length) |
|
|
|
.length(length) |
|
|
|
.callable(true) |
|
|
|
|
|
|
|
.constructable(false) |
|
|
|
.constructable(false) |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
@ -1081,7 +1076,7 @@ impl<'context> ConstructorBuilder<'context> { |
|
|
|
where |
|
|
|
where |
|
|
|
N: AsRef<str>, |
|
|
|
N: AsRef<str>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
self.name = Some(name.as_ref().into()); |
|
|
|
self.name = name.as_ref().into(); |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1122,17 +1117,17 @@ impl<'context> ConstructorBuilder<'context> { |
|
|
|
/// Build the constructor function object.
|
|
|
|
/// Build the constructor function object.
|
|
|
|
pub fn build(&mut self) -> GcObject { |
|
|
|
pub fn build(&mut self) -> GcObject { |
|
|
|
// Create the native function
|
|
|
|
// Create the native function
|
|
|
|
let function = Function::BuiltIn( |
|
|
|
let function = Function::Native { |
|
|
|
self.constructor_function.into(), |
|
|
|
function: self.constructor_function.into(), |
|
|
|
FunctionFlags::from_parameters(self.callable, self.constructable), |
|
|
|
constructable: self.constructable, |
|
|
|
); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
let length = DataDescriptor::new( |
|
|
|
let length = DataDescriptor::new( |
|
|
|
self.length, |
|
|
|
self.length, |
|
|
|
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, |
|
|
|
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, |
|
|
|
); |
|
|
|
); |
|
|
|
let name = DataDescriptor::new( |
|
|
|
let name = DataDescriptor::new( |
|
|
|
self.name.take().unwrap_or_else(|| String::from("[object]")), |
|
|
|
self.name.clone(), |
|
|
|
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, |
|
|
|
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|