Rust编写的JavaScript引擎,该项目是一个试验性质的项目。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1116 lines
42 KiB

//! Boa's implementation of ECMAScript's global `Function` object and Native Functions.
//!
//! Objects wrap `Function`s and expose them via call/construct slots.
//!
//! The `Function` object is used for matching text with a pattern.
//!
//! More information:
//! - [ECMAScript reference][spec]
//! - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-function-objects
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
use crate::{
builtins::{
BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject, OrdinaryObject,
},
bytecompiler::FunctionCompiler,
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
environments::{EnvironmentStack, FunctionSlots, PrivateEnvironment, ThisBindingStatus},
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
error::JsNativeError,
js_string,
native_function::NativeFunctionObject,
object::{internal_methods::get_prototype_from_constructor, JsObject},
object::{
internal_methods::{CallValue, InternalObjectMethods, ORDINARY_INTERNAL_METHODS},
JsData, JsFunction, PrivateElement, PrivateName,
},
property::{Attribute, PropertyDescriptor, PropertyKey},
realm::Realm,
string::{common::StaticJsStrings, utf16},
Make `JsSymbol` thread-safe (#2539) The section about `Symbol` on the [specification](https://tc39.es/ecma262/#sec-ecmascript-language-types-symbol-type) says: > The Symbol type is the set of all non-String values that may be used as the key of an Object property ([6.1.7](https://tc39.es/ecma262/#sec-object-type)). Each possible Symbol value is unique and immutable. Our previous implementation of `JsSymbol` used `Rc` and a thread local `Cell<usize>`. However, this meant that two different symbols in two different threads could share the same hash, making symbols not unique. Also, the [GlobalSymbolRegistry](https://tc39.es/ecma262/#table-globalsymbolregistry-record-fields) is meant to be shared by all realms, including realms that are not in the same thread as the main one; this forces us to replace our current thread local global symbol registry with a thread-safe one that uses `DashMap` for concurrent access. However, the global symbol registry uses `JsString`s as keys and values, which forces us to either use `Vec<u16>` instead (wasteful and needs to allocate to convert to `JsString` on each access) or make `JsString` thread-safe with an atomic counter. For this reason, I implemented the second option. This PR changes the following: - Makes `JsSymbol` thread-safe by using Arc instead of Rc, and making `SYMBOL_HASH_COUNT` an `AtomicU64`. - ~~Makes `JsString` thread-safe by using `AtomicUsize` instead of `Cell<usize>` for its ref count.~~ EDIT: Talked with @jasonwilliams and we decided to use `Box<[u16]>` for the global registry instead, because this won't penalize common usage of `JsString`, which is used a LOT more than `JsSymbol`. - Makes the `GLOBAL_SYMBOL_REGISTRY` truly global, using `DashMap` as our global map that is shared by all threads. - Replaces some thread locals with thread-safe alternatives, such as static arrays and static indices. - Various improvements to all related code for this.
1 year ago
symbol::JsSymbol,
value::IntegerOrInfinity,
vm::{ActiveRunnable, CallFrame, CallFrameFlags, CodeBlock},
Context, JsArgs, JsResult, JsString, JsValue,
};
use boa_ast::{
function::{FormalParameterList, FunctionBody},
operations::{
all_private_identifiers_valid, bound_names, contains, lexically_declared_names,
ContainsSymbol,
},
};
use boa_gc::{self, custom_trace, Finalize, Gc, Trace};
use boa_interner::Sym;
use boa_parser::{Parser, Source};
use boa_profiler::Profiler;
use thin_vec::ThinVec;
use super::Proxy;
pub(crate) mod arguments;
mod bound;
pub use bound::BoundFunction;
#[cfg(test)]
mod tests;
/// Represents the `[[ThisMode]]` internal slot of function objects.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-ecmascript-function-objects
#[derive(Debug, Trace, Finalize, PartialEq, Eq, Clone)]
pub enum ThisMode {
/// The `this` value refers to the `this` value of a lexically enclosing function.
Lexical,
/// The `this` value is used exactly as provided by an invocation of the function.
Strict,
/// The `this` value of `undefined` or `null` is interpreted as a reference to the global object,
/// and any other `this` value is first passed to `ToObject`.
Global,
}
impl ThisMode {
/// Returns `true` if the this mode is `Lexical`.
#[must_use]
pub const fn is_lexical(&self) -> bool {
matches!(self, Self::Lexical)
}
/// Returns `true` if the this mode is `Strict`.
#[must_use]
pub const fn is_strict(&self) -> bool {
matches!(self, Self::Strict)
}
/// Returns `true` if the this mode is `Global`.
#[must_use]
pub const fn is_global(&self) -> bool {
matches!(self, Self::Global)
}
}
/// Represents the `[[ConstructorKind]]` internal slot of function objects.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-ecmascript-function-objects
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConstructorKind {
/// The class constructor is not derived.
Base,
/// The class constructor is a derived class constructor.
Derived,
}
impl ConstructorKind {
/// Returns `true` if the constructor kind is `Base`.
#[must_use]
pub const fn is_base(&self) -> bool {
matches!(self, Self::Base)
}
/// Returns `true` if the constructor kind is `Derived`.
#[must_use]
pub const fn is_derived(&self) -> bool {
matches!(self, Self::Derived)
}
}
/// Record containing the field definition of classes.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-classfielddefinition-record-specification-type
#[derive(Clone, Debug, Finalize)]
pub enum ClassFieldDefinition {
/// A class field definition with a `string` or `symbol` as a name.
Public(PropertyKey, JsFunction),
/// A class field definition with a private name.
Private(PrivateName, JsFunction),
}
unsafe impl Trace for ClassFieldDefinition {
custom_trace! {this, mark, {
match this {
Self::Public(_key, func) => {
mark(func);
}
Self::Private(_, func) => {
mark(func);
}
}
}}
}
/// Boa representation of a JavaScript Function Object.
///
/// `FunctionBody` is specific to this interpreter, it will either be Rust code or JavaScript code
/// (AST Node).
///
/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects>
#[derive(Debug, Trace, Finalize)]
pub struct OrdinaryFunction {
/// The code block containing the compiled function.
pub(crate) code: Gc<CodeBlock>,
/// The `[[Environment]]` internal slot.
pub(crate) environments: EnvironmentStack,
/// The `[[HomeObject]]` internal slot.
pub(crate) home_object: Option<JsObject>,
/// The `[[ScriptOrModule]]` internal slot.
pub(crate) script_or_module: Option<ActiveRunnable>,
/// The [`Realm`] the function is defined in.
pub(crate) realm: Realm,
/// The `[[Fields]]` internal slot.
fields: ThinVec<ClassFieldDefinition>,
/// The `[[PrivateMethods]]` internal slot.
private_methods: ThinVec<(PrivateName, PrivateElement)>,
}
impl JsData for OrdinaryFunction {
fn internal_methods(&self) -> &'static InternalObjectMethods {
static FUNCTION_METHODS: InternalObjectMethods = InternalObjectMethods {
__call__: function_call,
..ORDINARY_INTERNAL_METHODS
};
static CONSTRUCTOR_METHODS: InternalObjectMethods = InternalObjectMethods {
__call__: function_call,
__construct__: function_construct,
..ORDINARY_INTERNAL_METHODS
};
if self.code.has_prototype_property() {
&CONSTRUCTOR_METHODS
} else {
&FUNCTION_METHODS
}
}
}
impl OrdinaryFunction {
pub(crate) fn new(
code: Gc<CodeBlock>,
environments: EnvironmentStack,
script_or_module: Option<ActiveRunnable>,
realm: Realm,
) -> Self {
Self {
code,
environments,
home_object: None,
script_or_module,
realm,
fields: ThinVec::default(),
private_methods: ThinVec::default(),
}
}
/// Returns the codeblock of the function.
#[must_use]
pub fn codeblock(&self) -> &CodeBlock {
&self.code
}
/// Push a private environment to the function.
pub(crate) fn push_private_environment(&mut self, environment: Gc<PrivateEnvironment>) {
self.environments.push_private(environment);
}
/// Returns true if the function object is a derived constructor.
pub(crate) fn is_derived_constructor(&self) -> bool {
self.code.is_derived_constructor()
}
/// Does this function have the `[[ClassFieldInitializerName]]` internal slot set to non-empty value.
pub(crate) fn in_class_field_initializer(&self) -> bool {
self.code.in_class_field_initializer()
}
/// Returns a reference to the function `[[HomeObject]]` slot if present.
pub(crate) const fn get_home_object(&self) -> Option<&JsObject> {
self.home_object.as_ref()
}
/// Sets the `[[HomeObject]]` slot if present.
pub(crate) fn set_home_object(&mut self, object: JsObject) {
self.home_object = Some(object);
}
/// Returns the values of the `[[Fields]]` internal slot.
pub(crate) fn get_fields(&self) -> &[ClassFieldDefinition] {
&self.fields
}
/// Pushes a value to the `[[Fields]]` internal slot if present.
pub(crate) fn push_field(&mut self, key: PropertyKey, value: JsFunction) {
self.fields.push(ClassFieldDefinition::Public(key, value));
}
/// Pushes a private value to the `[[Fields]]` internal slot if present.
pub(crate) fn push_field_private(&mut self, name: PrivateName, value: JsFunction) {
self.fields.push(ClassFieldDefinition::Private(name, value));
}
/// Returns the values of the `[[PrivateMethods]]` internal slot.
pub(crate) fn get_private_methods(&self) -> &[(PrivateName, PrivateElement)] {
&self.private_methods
}
/// Pushes a private method to the `[[PrivateMethods]]` internal slot if present.
pub(crate) fn push_private_method(&mut self, name: PrivateName, method: PrivateElement) {
self.private_methods.push((name, method));
}
/// Gets the `Realm` from where this function originates.
#[must_use]
pub const fn realm(&self) -> &Realm {
&self.realm
}
/// Checks if this function is an ordinary function.
pub(crate) fn is_ordinary(&self) -> bool {
self.code.is_ordinary()
}
}
/// The internal representation of a `Function` object.
#[derive(Debug, Clone, Copy)]
pub struct BuiltInFunctionObject;
impl IntrinsicObject for BuiltInFunctionObject {
fn init(realm: &Realm) {
let _timer = Profiler::global().start_event(std::any::type_name::<Self>(), "init");
Implement `Hidden classes` (#2723) This PR implements `Hidden Classes`, I named them as `Shapes` (like Spidermonkey does), calling them maps like v8 seems confusing because there already is a JS builtin, likewise with `Hidden classes` since there are already classes in JS. There are two types of shapes: `shared` shapes that create the transition tree, and are shared between objects, this is mainly intended for user defined objects this makes more sense because shapes can create transitions trees, doing that for the builtins seems wasteful (unless users wanted to creating an object with the same property names and the same property attributes in the same order... which seems unlikely). That's why I added `unique` shapes, only one object has it. This is similar to previous solution, but this architecture enables us to use inline caching. There will probably be a performance hit until we implement inline caching. There still a lot of work that needs to be done, on this: - [x] Move Property Attributes to shape - [x] Move Prototype to shape - [x] ~~Move extensible flag to shape~~, On further evaluation this doesn't give any benefit (at least right now), since it isn't used by inline caching also adding one more transition. - [x] Implement delete for unique shapes. - [x] If the chain is too long we should probably convert it into a `unique` shape - [x] Figure out threshold ~~(maybe more that 256 properties ?)~~ curently set to an arbitrary number (`1024`) - [x] Implement shared property table between shared shapes - [x] Add code Document - [x] Varying size storage for properties (get+set = 2, data = 1) - [x] Add shapes to more object: - [x] ordinary object - [x] Arrays - [x] Functions - [x] Other builtins - [x] Add `shapes.md` doc explaining shapes in depth with mermaid diagrams :) - [x] Add `$boa.shape` module - [x] `$boa.shape.id(o)` - [x] `$boa.shape.type(o)` - [x] `$boa.shape.same(o1, o2)` - [x] add doc to `boa_object.md`
1 year ago
let has_instance = BuiltInBuilder::callable(realm, Self::has_instance)
.name(js_string!("[Symbol.hasInstance]"))
.length(1)
.build();
let throw_type_error = realm.intrinsics().objects().throw_type_error();
BuiltInBuilder::from_standard_constructor::<Self>(realm)
.method(Self::apply, js_string!("apply"), 2)
.method(Self::bind, js_string!("bind"), 1)
.method(Self::call, js_string!("call"), 1)
.method(Self::to_string, js_string!("toString"), 0)
.property(JsSymbol::has_instance(), has_instance, Attribute::default())
.accessor(
utf16!("caller"),
Some(throw_type_error.clone()),
Some(throw_type_error.clone()),
Attribute::CONFIGURABLE,
)
.accessor(
utf16!("arguments"),
Some(throw_type_error.clone()),
Some(throw_type_error),
Attribute::CONFIGURABLE,
)
.build();
Implement `Hidden classes` (#2723) This PR implements `Hidden Classes`, I named them as `Shapes` (like Spidermonkey does), calling them maps like v8 seems confusing because there already is a JS builtin, likewise with `Hidden classes` since there are already classes in JS. There are two types of shapes: `shared` shapes that create the transition tree, and are shared between objects, this is mainly intended for user defined objects this makes more sense because shapes can create transitions trees, doing that for the builtins seems wasteful (unless users wanted to creating an object with the same property names and the same property attributes in the same order... which seems unlikely). That's why I added `unique` shapes, only one object has it. This is similar to previous solution, but this architecture enables us to use inline caching. There will probably be a performance hit until we implement inline caching. There still a lot of work that needs to be done, on this: - [x] Move Property Attributes to shape - [x] Move Prototype to shape - [x] ~~Move extensible flag to shape~~, On further evaluation this doesn't give any benefit (at least right now), since it isn't used by inline caching also adding one more transition. - [x] Implement delete for unique shapes. - [x] If the chain is too long we should probably convert it into a `unique` shape - [x] Figure out threshold ~~(maybe more that 256 properties ?)~~ curently set to an arbitrary number (`1024`) - [x] Implement shared property table between shared shapes - [x] Add code Document - [x] Varying size storage for properties (get+set = 2, data = 1) - [x] Add shapes to more object: - [x] ordinary object - [x] Arrays - [x] Functions - [x] Other builtins - [x] Add `shapes.md` doc explaining shapes in depth with mermaid diagrams :) - [x] Add `$boa.shape` module - [x] `$boa.shape.id(o)` - [x] `$boa.shape.type(o)` - [x] `$boa.shape.same(o1, o2)` - [x] add doc to `boa_object.md`
1 year ago
let prototype = realm.intrinsics().constructors().function().prototype();
BuiltInBuilder::callable_with_object(realm, prototype.clone(), Self::prototype)
.name(js_string!())
Implement `Hidden classes` (#2723) This PR implements `Hidden Classes`, I named them as `Shapes` (like Spidermonkey does), calling them maps like v8 seems confusing because there already is a JS builtin, likewise with `Hidden classes` since there are already classes in JS. There are two types of shapes: `shared` shapes that create the transition tree, and are shared between objects, this is mainly intended for user defined objects this makes more sense because shapes can create transitions trees, doing that for the builtins seems wasteful (unless users wanted to creating an object with the same property names and the same property attributes in the same order... which seems unlikely). That's why I added `unique` shapes, only one object has it. This is similar to previous solution, but this architecture enables us to use inline caching. There will probably be a performance hit until we implement inline caching. There still a lot of work that needs to be done, on this: - [x] Move Property Attributes to shape - [x] Move Prototype to shape - [x] ~~Move extensible flag to shape~~, On further evaluation this doesn't give any benefit (at least right now), since it isn't used by inline caching also adding one more transition. - [x] Implement delete for unique shapes. - [x] If the chain is too long we should probably convert it into a `unique` shape - [x] Figure out threshold ~~(maybe more that 256 properties ?)~~ curently set to an arbitrary number (`1024`) - [x] Implement shared property table between shared shapes - [x] Add code Document - [x] Varying size storage for properties (get+set = 2, data = 1) - [x] Add shapes to more object: - [x] ordinary object - [x] Arrays - [x] Functions - [x] Other builtins - [x] Add `shapes.md` doc explaining shapes in depth with mermaid diagrams :) - [x] Add `$boa.shape` module - [x] `$boa.shape.id(o)` - [x] `$boa.shape.type(o)` - [x] `$boa.shape.same(o1, o2)` - [x] add doc to `boa_object.md`
1 year ago
.length(0)
.build();
prototype.set_prototype(Some(realm.intrinsics().constructors().object().prototype()));
}
fn get(intrinsics: &Intrinsics) -> JsObject {
Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
}
}
impl BuiltInObject for BuiltInFunctionObject {
const NAME: JsString = StaticJsStrings::FUNCTION;
}
impl BuiltInConstructor for BuiltInFunctionObject {
const LENGTH: usize = 1;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::function;
/// `Function ( p1, p2, … , pn, body )`
///
/// The `apply()` method invokes self with the first argument as the `this` value
/// and the rest of the arguments provided as an array (or an array-like object).
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function
fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let active_function = context
.active_function_object()
.unwrap_or_else(|| context.intrinsics().constructors().function().constructor());
Self::create_dynamic_function(active_function, new_target, args, false, false, context)
.map(Into::into)
}
}
impl BuiltInFunctionObject {
/// `CreateDynamicFunction ( constructor, newTarget, kind, args )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-createdynamicfunction
pub(crate) fn create_dynamic_function(
constructor: JsObject,
new_target: &JsValue,
args: &[JsValue],
r#async: bool,
generator: bool,
context: &mut Context,
) -> JsResult<JsObject> {
// 1. If newTarget is undefined, set newTarget to constructor.
let new_target = if new_target.is_undefined() {
constructor.into()
} else {
new_target.clone()
};
let default = if r#async && generator {
// 5. Else,
// a. Assert: kind is async-generator.
// b. Let prefix be "async function*".
// c. Let exprSym be the grammar symbol AsyncGeneratorExpression.
// d. Let bodySym be the grammar symbol AsyncGeneratorBody.
// e. Let parameterSym be the grammar symbol FormalParameters[+Yield, +Await].
// f. Let fallbackProto be "%AsyncGeneratorFunction.prototype%".
StandardConstructors::async_generator_function
} else if r#async {
// 4. Else if kind is async, then
// a. Let prefix be "async function".
// b. Let exprSym be the grammar symbol AsyncFunctionExpression.
// c. Let bodySym be the grammar symbol AsyncFunctionBody.
// d. Let parameterSym be the grammar symbol FormalParameters[~Yield, +Await].
// e. Let fallbackProto be "%AsyncFunction.prototype%".
StandardConstructors::async_function
} else if generator {
// 3. Else if kind is generator, then
// a. Let prefix be "function*".
// b. Let exprSym be the grammar symbol GeneratorExpression.
// c. Let bodySym be the grammar symbol GeneratorBody.
// d. Let parameterSym be the grammar symbol FormalParameters[+Yield, ~Await].
// e. Let fallbackProto be "%GeneratorFunction.prototype%".
StandardConstructors::generator_function
} else {
// 2. If kind is normal, then
// a. Let prefix be "function".
// b. Let exprSym be the grammar symbol FunctionExpression.
// c. Let bodySym be the grammar symbol FunctionBody[~Yield, ~Await].
// d. Let parameterSym be the grammar symbol FormalParameters[~Yield, ~Await].
// e. Let fallbackProto be "%Function.prototype%".
StandardConstructors::function
};
// 22. Let proto be ? GetPrototypeFromConstructor(newTarget, fallbackProto).
let prototype = get_prototype_from_constructor(&new_target, default, context)?;
// 6. Let argCount be the number of elements in parameterArgs.
let (body, param_list) = if let Some((body, params)) = args.split_last() {
// 7. Let parameterStrings be a new empty List.
let mut parameters = Vec::with_capacity(args.len());
// 8. For each element arg of parameterArgs, do
for param in params {
// a. Append ? ToString(arg) to parameterStrings.
parameters.push(param.to_string(context)?);
}
// 9. Let bodyString be ? ToString(bodyArg).
let body = body.to_string(context)?;
(body, parameters)
} else {
(js_string!(), Vec::new())
};
let current_realm = context.realm().clone();
context.host_hooks().ensure_can_compile_strings(
current_realm,
&param_list,
&body,
false,
context,
)?;
let parameters = if param_list.is_empty() {
FormalParameterList::default()
} else {
// 12. Let P be the empty String.
// 13. If argCount > 0, then
// a. Set P to parameterStrings[0].
// b. Let k be 1.
// c. Repeat, while k < argCount,
// i. Let nextArgString be parameterStrings[k].
// ii. Set P to the string-concatenation of P, "," (a comma), and nextArgString.
// iii. Set k to k + 1.
let parameters = param_list.join(utf16!(","));
let mut parser = Parser::new(Source::from_utf16(&parameters));
parser.set_identifier(context.next_parser_identifier());
// 17. Let parameters be ParseText(StringToCodePoints(P), parameterSym).
// 18. If parameters is a List of errors, throw a SyntaxError exception.
let parameters = parser
.parse_formal_parameters(context.interner_mut(), generator, r#async)
.map_err(|e| {
JsNativeError::syntax()
.with_message(format!("failed to parse function parameters: {e}"))
})?;
// It is a Syntax Error if FormalParameters Contains YieldExpression is true.
if generator && contains(&parameters, ContainsSymbol::YieldExpression) {
return Err(JsNativeError::syntax().with_message(
if r#async {
"yield expression is not allowed in formal parameter list of async generator"
} else {
"yield expression is not allowed in formal parameter list of generator"
}
).into());
}
// It is a Syntax Error if FormalParameters Contains AwaitExpression is true.
if r#async && contains(&parameters, ContainsSymbol::AwaitExpression) {
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
return Err(JsNativeError::syntax()
.with_message(
if generator {
"await expression is not allowed in formal parameter list of async function"
} else {
"await expression is not allowed in formal parameter list of async generator"
})
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
.into());
}
parameters
};
let body = if body.is_empty() {
FunctionBody::default()
} else {
// 14. Let bodyParseString be the string-concatenation of 0x000A (LINE FEED), bodyString, and 0x000A (LINE FEED).
let mut body_parse = Vec::with_capacity(body.len());
body_parse.push(u16::from(b'\n'));
body_parse.extend_from_slice(&body);
body_parse.push(u16::from(b'\n'));
// 19. Let body be ParseText(StringToCodePoints(bodyParseString), bodySym).
// 20. If body is a List of errors, throw a SyntaxError exception.
let mut parser = Parser::new(Source::from_utf16(&body_parse));
parser.set_identifier(context.next_parser_identifier());
// 19. Let body be ParseText(StringToCodePoints(bodyParseString), bodySym).
// 20. If body is a List of errors, throw a SyntaxError exception.
let body = parser
.parse_function_body(context.interner_mut(), generator, r#async)
.map_err(|e| {
JsNativeError::syntax()
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
.with_message(format!("failed to parse function body: {e}"))
})?;
// It is a Syntax Error if AllPrivateIdentifiersValid of StatementList with argument « »
// is false unless the source text containing ScriptBody is eval code that is being
// processed by a direct eval.
// https://tc39.es/ecma262/#sec-scripts-static-semantics-early-errors
if !all_private_identifiers_valid(&body, Vec::new()) {
return Err(JsNativeError::syntax()
.with_message("invalid private identifier usage")
.into());
}
// 21. NOTE: The parameters and body are parsed separately to ensure that each is valid alone. For example, new Function("/*", "*/ ) {") does not evaluate to a function.
// 22. NOTE: If this step is reached, sourceText must have the syntax of exprSym (although the reverse implication does not hold). The purpose of the next two steps is to enforce any Early Error rules which apply to exprSym directly.
// 23. Let expr be ParseText(sourceText, exprSym).
// 24. If expr is a List of errors, throw a SyntaxError exception.
// Check for errors that apply for the combination of body and parameters.
// Early Error: If BindingIdentifier is present and the source text matched by BindingIdentifier is strict mode code,
// it is a Syntax Error if the StringValue of BindingIdentifier is "eval" or "arguments".
if body.strict() {
for name in bound_names(&parameters) {
if name == Sym::ARGUMENTS || name == Sym::EVAL {
return Err(JsNativeError::syntax()
.with_message("Unexpected 'eval' or 'arguments' in strict mode")
.into());
}
}
}
// Early Error: If the source code matching FormalParameters is strict mode code,
// the Early Error rules for UniqueFormalParameters : FormalParameters are applied.
if (body.strict()) && parameters.has_duplicates() {
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
return Err(JsNativeError::syntax()
.with_message("Duplicate parameter name not allowed in this context")
.into());
}
// Early Error: It is a Syntax Error if FunctionBodyContainsUseStrict of GeneratorBody is true
// and IsSimpleParameterList of FormalParameters is false.
if body.strict() && !parameters.is_simple() {
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
return Err(JsNativeError::syntax()
.with_message(
"Illegal 'use strict' directive in function with non-simple parameter list",
)
.into());
}
// It is a Syntax Error if FunctionBody Contains SuperProperty is true.
if contains(&body, ContainsSymbol::SuperProperty) {
return Err(JsNativeError::syntax()
.with_message("invalid `super` reference")
.into());
}
// It is a Syntax Error if FunctionBody Contains SuperCall is true.
if contains(&body, ContainsSymbol::SuperCall) {
return Err(JsNativeError::syntax()
.with_message("invalid `super` call")
.into());
}
// It is a Syntax Error if any element of the BoundNames of FormalParameters
// also occurs in the LexicallyDeclaredNames of FunctionBody.
// https://tc39.es/ecma262/#sec-function-definitions-static-semantics-early-errors
{
let lexically_declared_names = lexically_declared_names(&body);
for name in bound_names(&parameters) {
if lexically_declared_names.contains(&name) {
return Err(JsNativeError::syntax()
.with_message(format!(
"Redeclaration of formal parameter `{}`",
context.interner().resolve_expect(name.sym())
))
.into());
}
}
}
body
};
let code = FunctionCompiler::new()
.name(js_string!("anonymous"))
.generator(generator)
.r#async(r#async)
.compile(
&parameters,
&body,
context.realm().environment().compile_env(),
context.realm().environment().compile_env(),
context,
);
let environments = context.vm.environments.pop_to_global();
let function_object = crate::vm::create_function_object(code, prototype, context);
context.vm.environments.extend(environments);
Ok(function_object)
}
/// `Function.prototype.apply ( thisArg, argArray )`
///
/// The `apply()` method invokes self with the first argument as the `this` value
/// and the rest of the arguments provided as an array (or an array-like object).
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function.prototype.apply
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
fn apply(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let func be the this value.
// 2. If IsCallable(func) is false, throw a TypeError exception.
let func = this.as_callable().ok_or_else(|| {
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
JsNativeError::typ().with_message(format!("{} is not a function", this.display()))
})?;
let this_arg = args.get_or_undefined(0);
let arg_array = args.get_or_undefined(1);
// 3. If argArray is undefined or null, then
if arg_array.is_null_or_undefined() {
// a. Perform PrepareForTailCall().
// TODO?: 3.a. PrepareForTailCall
// b. Return ? Call(func, thisArg).
return func.call(this_arg, &[], context);
}
// 4. Let argList be ? CreateListFromArrayLike(argArray).
let arg_list = arg_array.create_list_from_array_like(&[], context)?;
// 5. Perform PrepareForTailCall().
// TODO?: 5. PrepareForTailCall
// 6. Return ? Call(func, thisArg, argList).
func.call(this_arg, &arg_list, context)
}
/// `Function.prototype.bind ( thisArg, ...args )`
///
/// The `bind()` method creates a new function that, when called, has its
/// this keyword set to the provided value, with a given sequence of arguments
/// preceding any provided when the new function is called.
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function.prototype.bind
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
fn bind(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let Target be the this value.
// 2. If IsCallable(Target) is false, throw a TypeError exception.
let target = this.as_callable().ok_or_else(|| {
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
JsNativeError::typ()
.with_message("cannot bind `this` without a `[[Call]]` internal method")
})?;
let this_arg = args.get_or_undefined(0).clone();
Lexer string interning (#1758) This Pull Request is part of #279. It adds a string interner to Boa, which allows many types to not contain heap-allocated strings, and just contain a `NonZeroUsize` instead. This can move types to the stack (hopefully I'll be able to move `Token`, for example, maybe some `Node` types too. Note that the internet is for now only available in the lexer. Next steps (in this PR or future ones) would include also using interning in the parser, and finally in execution. The idea is that strings should be represented with a `Sym` until they are displayed. Talking about display. I have changed the `ParseError` type in order to not contain anything that could contain a `Sym` (basically tokens), which might be a bit faster, but what is important is that we don't depend on the interner when displaying errors. The issue I have now is in order to display tokens. This requires the interner if we want to know identifiers, for example. The issue here is that Rust doesn't allow using a `fmt::Formatter` (only in nightly), which is making my head hurt. Maybe someone of you can find a better way of doing this. Then, about `cursor.expect()`, this is the only place where we don't have the expected token type as a static string, so it's failing to compile. We have the option of changing the type definition of `ParseError` to contain an owned string, but maybe we can avoid this by having a `&'static str` come from a `TokenKind` with the default values, such as "identifier" for an identifier. I wanted for you to think about it and maybe we can just add that and avoid allocations there. Oh, and this depends on the VM-only branch, so that has to be merged before :) Another thing to check: should the interner be in its own module?
2 years ago
let bound_args = args.get(1..).unwrap_or(&[]).to_vec();
let arg_count = bound_args.len() as i64;
// 3. Let F be ? BoundFunctionCreate(Target, thisArg, args).
let f = BoundFunction::create(target.clone(), this_arg, bound_args, context)?;
// 4. Let L be 0.
let mut l = JsValue::new(0);
// 5. Let targetHasLength be ? HasOwnProperty(Target, "length").
// 6. If targetHasLength is true, then
if target.has_own_property(StaticJsStrings::LENGTH, context)? {
// a. Let targetLen be ? Get(Target, "length").
let target_len = target.get(StaticJsStrings::LENGTH, context)?;
// b. If Type(targetLen) is Number, then
if target_len.is_number() {
// 1. Let targetLenAsInt be ! ToIntegerOrInfinity(targetLen).
match target_len
.to_integer_or_infinity(context)
.expect("to_integer_or_infinity cannot fail for a number")
{
// i. If targetLen is +∞𝔽, set L to +∞.
IntegerOrInfinity::PositiveInfinity => l = f64::INFINITY.into(),
// ii. Else if targetLen is -∞𝔽, set L to 0.
IntegerOrInfinity::NegativeInfinity => {}
// iii. Else,
IntegerOrInfinity::Integer(target_len) => {
// 2. Assert: targetLenAsInt is finite.
// 3. Let argCount be the number of elements in args.
// 4. Set L to max(targetLenAsInt - argCount, 0).
l = (target_len - arg_count).max(0).into();
}
}
}
}
// 7. Perform ! SetFunctionLength(F, L).
f.define_property_or_throw(
StaticJsStrings::LENGTH,
PropertyDescriptor::builder()
.value(l)
.writable(false)
.enumerable(false)
.configurable(true),
context,
)
.expect("defining the `length` property for a new object should not fail");
// 8. Let targetName be ? Get(Target, "name").
let target_name = target.get(utf16!("name"), context)?;
// 9. If Type(targetName) is not String, set targetName to the empty String.
let target_name = target_name
.as_string()
.map_or_else(JsString::default, Clone::clone);
// 10. Perform SetFunctionName(F, targetName, "bound").
set_function_name(&f, &target_name.into(), Some(js_string!("bound")), context);
// 11. Return F.
Ok(f.into())
}
/// `Function.prototype.call ( thisArg, ...args )`
///
/// The `call()` method calls a function with a given this value and arguments provided individually.
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function.prototype.call
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
fn call(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let func be the this value.
// 2. If IsCallable(func) is false, throw a TypeError exception.
let func = this.as_callable().ok_or_else(|| {
Create new lazy Error type (#2283) This is an experiment that tries to migrate the codebase from eager `Error` objects to lazy ones. In short words, this redefines `JsResult = Result<JsValue, JsError>`, where `JsError` is a brand new type that stores only the essential part of an error type, and only transforms those errors to `JsObject`s on demand (when having to pass them as arguments to functions or store them inside async/generators). This change is pretty big, because it unblocks a LOT of code from having to take a `&mut Context` on each call. It also paves the road for possibly making `JsError` a proper variant of `JsValue`, which can be a pretty big optimization for try/catch. A downside of this is that it exposes some brand new error types to our public API. However, we can now implement `Error` on `JsError`, making our `JsResult` type a bit more inline with Rust's best practices. ~Will mark this as draft, since it's missing some documentation and a lot of examples, but~ it's pretty much feature complete. As always, any comments about the design are very much appreciated! Note: Since there are a lot of changes which are essentially just rewriting `context.throw` to `JsNativeError::%type%`, I'll leave an "index" of the most important changes here: - [boa_engine/src/error.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-f15f2715655440626eefda5c46193d29856f4949ad37380c129a8debc6b82f26) - [boa_engine/src/builtins/error/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-3eb1e4b4b5c7210eb98192a5277f5a239148423c6b970c4ae05d1b267f8f1084) - [boa_tester/src/exec/mod.rs](https://github.com/boa-dev/boa/pull/2283/files#diff-fc3d7ad7b5e64574258c9febbe56171f3309b74e0c8da35238a76002f3ee34d9)
2 years ago
JsNativeError::typ().with_message(format!("{} is not a function", this.display()))
})?;
let this_arg = args.get_or_undefined(0);
// 3. Perform PrepareForTailCall().
// TODO?: 3. Perform PrepareForTailCall
// 4. Return ? Call(func, thisArg, args).
func.call(this_arg, args.get(1..).unwrap_or(&[]), context)
}
/// `Function.prototype.toString()`
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function.prototype.tostring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString
#[allow(clippy::wrong_self_convention)]
fn to_string(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let func be the this value.
let func = this;
// TODO:
// 2. If func is an Object, func has a [[SourceText]] internal slot, func.[[SourceText]] is a sequence of Unicode code points,and HostHasSourceTextAvailable(func) is true, then
// a. Return CodePointsToString(func.[[SourceText]]).
// 3. If func is a built-in function object, return an implementation-defined String source code representation of func.
// The representation must have the syntax of a NativeFunction. Additionally, if func has an [[InitialName]] internal slot and
// func.[[InitialName]] is a String, the portion of the returned String that would be matched by
// NativeFunctionAccessor_opt PropertyName must be the value of func.[[InitialName]].
// 4. If func is an Object and IsCallable(func) is true, return an implementation-defined String source code representation of func.
// The representation must have the syntax of a NativeFunction.
// 5. Throw a TypeError exception.
let Some(object) = func.as_callable() else {
return Err(JsNativeError::typ().with_message("not a function").into());
};
let object_borrow = object.borrow();
if object_borrow.is::<NativeFunctionObject>() {
let name = {
// Is there a case here where if there is no name field on a value
// name should default to None? Do all functions have names set?
let value = object.get(utf16!("name"), &mut *context)?;
if value.is_null_or_undefined() {
js_string!()
} else {
value.to_string(context)?
}
};
return Ok(
js_string!(utf16!("function "), &name, utf16!("() { [native code] }")).into(),
);
} else if object_borrow.is::<Proxy>() || object_borrow.is::<BoundFunction>() {
return Ok(js_string!(utf16!("function () { [native code] }")).into());
}
let function = object_borrow
.downcast_ref::<OrdinaryFunction>()
.ok_or_else(|| JsNativeError::typ().with_message("not a function"))?;
let code = function.codeblock();
Ok(js_string!(
utf16!("function "),
code.name(),
utf16!("() { [native code] }")
)
.into())
}
/// `Function.prototype [ @@hasInstance ] ( V )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function.prototype-@@hasinstance
fn has_instance(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let F be the this value.
// 2. Return ? OrdinaryHasInstance(F, V).
Ok(JsValue::ordinary_has_instance(this, args.get_or_undefined(0), context)?.into())
}
#[allow(clippy::unnecessary_wraps)]
fn prototype(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Ok(JsValue::undefined())
}
}
/// Abstract operation `SetFunctionName`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-setfunctionname
pub(crate) fn set_function_name(
function: &JsObject,
name: &PropertyKey,
prefix: Option<JsString>,
context: &mut Context,
) {
// 1. Assert: F is an extensible object that does not have a "name" own property.
// 2. If Type(name) is Symbol, then
let mut name = match name {
PropertyKey::Symbol(sym) => {
// a. Let description be name's [[Description]] value.
// b. If description is undefined, set name to the empty String.
// c. Else, set name to the string-concatenation of "[", description, and "]".
sym.description().map_or_else(
|| js_string!(),
|desc| js_string!(utf16!("["), &desc, utf16!("]")),
)
}
PropertyKey::String(string) => string.clone(),
PropertyKey::Index(index) => js_string!(format!("{}", index.get())),
};
// 3. Else if name is a Private Name, then
// a. Set name to name.[[Description]].
// todo: implement Private Names
// 4. If F has an [[InitialName]] internal slot, then
// a. Set F.[[InitialName]] to name.
// todo: implement [[InitialName]] for builtins
// 5. If prefix is present, then
if let Some(prefix) = prefix {
name = js_string!(&prefix, utf16!(" "), &name);
// b. If F has an [[InitialName]] internal slot, then
// i. Optionally, set F.[[InitialName]] to name.
// todo: implement [[InitialName]] for builtins
}
// 6. Return ! DefinePropertyOrThrow(F, "name", PropertyDescriptor { [[Value]]: name,
// [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).
function
.define_property_or_throw(
utf16!("name"),
PropertyDescriptor::builder()
.value(name)
.writable(false)
.enumerable(false)
.configurable(true),
context,
)
.expect("defining the `name` property must not fail per the spec");
}
/// Call this object.
///
/// # Panics
///
/// Panics if the object is currently mutably borrowed.
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist>
pub(crate) fn function_call(
function_object: &JsObject,
argument_count: usize,
context: &mut Context,
) -> JsResult<CallValue> {
context.check_runtime_limits()?;
let function = function_object
.downcast_ref::<OrdinaryFunction>()
.expect("not a function");
let realm = function.realm().clone();
if function.code.is_class_constructor() {
debug_assert!(
function.is_ordinary(),
"only ordinary functions can be classes"
);
return Err(JsNativeError::typ()
.with_message("class constructor cannot be invoked without 'new'")
.with_realm(realm)
.into());
}
let code = function.code.clone();
let environments = function.environments.clone();
let script_or_module = function.script_or_module.clone();
drop(function);
let env_fp = environments.len() as u32;
let frame = CallFrame::new(code.clone(), script_or_module, environments, realm)
.with_argument_count(argument_count as u32)
.with_env_fp(env_fp);
context.vm.push_frame(frame);
let this = context.vm.frame().this(&context.vm);
let lexical_this_mode = code.this_mode == ThisMode::Lexical;
let this = if lexical_this_mode {
ThisBindingStatus::Lexical
} else if code.strict() {
ThisBindingStatus::Initialized(this.clone())
} else if this.is_null_or_undefined() {
ThisBindingStatus::Initialized(context.realm().global_this().clone().into())
} else {
ThisBindingStatus::Initialized(
this.to_object(context)
.expect("conversion cannot fail")
.into(),
)
};
let mut last_env = 0;
if code.has_binding_identifier() {
let index = context
.vm
.environments
.push_lexical(code.constant_compile_time_environment(last_env));
context
.vm
.environments
.put_lexical_value(index, 0, function_object.clone().into());
last_env += 1;
}
context.vm.environments.push_function(
code.constant_compile_time_environment(last_env),
FunctionSlots::new(this, function_object.clone(), None),
);
Ok(CallValue::Ready)
}
/// Construct an instance of this object with the specified arguments.
///
/// # Panics
///
/// Panics if the object is currently mutably borrowed.
// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget>
fn function_construct(
this_function_object: &JsObject,
argument_count: usize,
context: &mut Context,
) -> JsResult<CallValue> {
context.check_runtime_limits()?;
let function = this_function_object
.downcast_ref::<OrdinaryFunction>()
.expect("not a function");
let realm = function.realm().clone();
debug_assert!(
function.is_ordinary(),
"only ordinary functions can be constructed"
);
let code = function.code.clone();
let environments = function.environments.clone();
let script_or_module = function.script_or_module.clone();
drop(function);
let env_fp = environments.len() as u32;
let new_target = context.vm.pop();
let this = if code.is_derived_constructor() {
None
} else {
// If the prototype of the constructor is not an object, then use the default object
// prototype as prototype for the new object
// see <https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor>
// see <https://tc39.es/ecma262/#sec-getprototypefromconstructor>
let prototype =
get_prototype_from_constructor(&new_target, StandardConstructors::object, context)?;
let this = JsObject::from_proto_and_data_with_shared_shape(
Implement `Hidden classes` (#2723) This PR implements `Hidden Classes`, I named them as `Shapes` (like Spidermonkey does), calling them maps like v8 seems confusing because there already is a JS builtin, likewise with `Hidden classes` since there are already classes in JS. There are two types of shapes: `shared` shapes that create the transition tree, and are shared between objects, this is mainly intended for user defined objects this makes more sense because shapes can create transitions trees, doing that for the builtins seems wasteful (unless users wanted to creating an object with the same property names and the same property attributes in the same order... which seems unlikely). That's why I added `unique` shapes, only one object has it. This is similar to previous solution, but this architecture enables us to use inline caching. There will probably be a performance hit until we implement inline caching. There still a lot of work that needs to be done, on this: - [x] Move Property Attributes to shape - [x] Move Prototype to shape - [x] ~~Move extensible flag to shape~~, On further evaluation this doesn't give any benefit (at least right now), since it isn't used by inline caching also adding one more transition. - [x] Implement delete for unique shapes. - [x] If the chain is too long we should probably convert it into a `unique` shape - [x] Figure out threshold ~~(maybe more that 256 properties ?)~~ curently set to an arbitrary number (`1024`) - [x] Implement shared property table between shared shapes - [x] Add code Document - [x] Varying size storage for properties (get+set = 2, data = 1) - [x] Add shapes to more object: - [x] ordinary object - [x] Arrays - [x] Functions - [x] Other builtins - [x] Add `shapes.md` doc explaining shapes in depth with mermaid diagrams :) - [x] Add `$boa.shape` module - [x] `$boa.shape.id(o)` - [x] `$boa.shape.type(o)` - [x] `$boa.shape.same(o1, o2)` - [x] add doc to `boa_object.md`
1 year ago
context.root_shape(),
prototype,
OrdinaryObject,
);
this.initialize_instance_elements(this_function_object, context)?;
Some(this)
};
let mut frame = CallFrame::new(code.clone(), script_or_module, environments, realm)
.with_argument_count(argument_count as u32)
.with_env_fp(env_fp)
.with_flags(CallFrameFlags::CONSTRUCT);
// We push the `this` below so we can mark this function as having the this value
// cached if it's initialized.
frame
.flags
.set(CallFrameFlags::THIS_VALUE_CACHED, this.is_some());
let len = context.vm.stack.len();
context.vm.push_frame(frame);
// NOTE(HalidOdat): +1 because we insert `this` value below.
context.vm.frame_mut().rp = len as u32 + 1;
let mut last_env = 0;
if code.has_binding_identifier() {
let index = context
.vm
.environments
.push_lexical(code.constant_compile_time_environment(last_env));
context
.vm
.environments
.put_lexical_value(index, 0, this_function_object.clone().into());
last_env += 1;
}
context.vm.environments.push_function(
code.constant_compile_time_environment(last_env),
FunctionSlots::new(
this.clone().map_or(ThisBindingStatus::Uninitialized, |o| {
ThisBindingStatus::Initialized(o.into())
}),
this_function_object.clone(),
Some(
new_target
.as_object()
.expect("new.target should be an object")
.clone(),
),
),
);
// Insert `this` value
context.vm.stack.insert(
len - argument_count - 1,
this.map(JsValue::new).unwrap_or_default(),
);
Ok(CallValue::Ready)
}