Skip to content

Commit

Permalink
Fix Function.prototype.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Oct 11, 2023
1 parent f654ad1 commit 5dea5ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
28 changes: 11 additions & 17 deletions boa_engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,12 @@ impl BuiltInFunctionObject {
// 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_object() else {
let Some(object) = func.as_callable() else {
return Err(JsNativeError::typ().with_message("not a function").into());
};

if object.borrow().is_native_function() {
let object = object.borrow();
if object.is_native_function() {
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?
Expand All @@ -880,29 +881,22 @@ impl BuiltInFunctionObject {
return Ok(
js_string!(utf16!("function "), &name, utf16!("() { [native code] }")).into(),
);
} else if object.is_proxy() || object.is_bound_function() {
return Ok(js_string!(utf16!("function () { [native code] }")).into());
}

let object = object.borrow();
let function = object
.as_function()
.ok_or_else(|| JsNativeError::typ().with_message("not a function"))?;

let code = function.codeblock();

let prefix = match function.kind {
FunctionKind::Ordinary { .. } => {
utf16!("function ")
}
FunctionKind::Async { .. } => {
utf16!("async function ")
}
FunctionKind::Generator { .. } => {
utf16!("function* ")
}
FunctionKind::AsyncGenerator { .. } => utf16!("async function* "),
};

Ok(js_string!(prefix, code.name(), utf16!("() { [native code] }")).into())
Ok(js_string!(
utf16!("function "),
code.name(),
utf16!("() { [native code] }")
)
.into())
}

/// `Function.prototype [ @@hasInstance ] ( V )`
Expand Down
7 changes: 7 additions & 0 deletions boa_engine/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,13 @@ impl Object {
}
}

/// Checks if the object is a `BoundFunction` object.
#[inline]
#[must_use]
pub const fn is_bound_function(&self) -> bool {
matches!(self.kind, ObjectKind::BoundFunction(_))
}

/// Gets the bound function data if the object is a `BoundFunction`.
#[inline]
#[must_use]
Expand Down

0 comments on commit 5dea5ea

Please sign in to comment.