Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(idk) make to_boolean return a bool #72

Merged
merged 9 commits into from Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ pub(crate) fn iterator_next(
/// The abstract operation IteratorComplete takes argument iterResult (an
/// Object) and returns either a normal completion containing a Boolean or a
/// throw completion.
pub(crate) fn iterator_complete(agent: &mut Agent, iter_result: Object) -> JsResult<Value> {
pub(crate) fn iterator_complete(agent: &mut Agent, iter_result: Object) -> JsResult<bool> {
// 1. Return ToBoolean(? Get(iterResult, "done")).
let done = get(agent, iter_result, String::from_small_string("done").into())?;
to_boolean(agent, done)
Ok(to_boolean(agent, done))
}

/// [7.4.6 IteratorValue ( iterResult )](https://tc39.es/ecma262/#sec-iteratorvalue)
Expand Down Expand Up @@ -196,7 +196,7 @@ pub(crate) fn iterator_step(
let done = iterator_complete(agent, result)?;

// 3. If done is true, return false.
if done.is_true() {
if done {
return Ok(None);
}

Expand Down
25 changes: 16 additions & 9 deletions nova_vm/src/ecmascript/abstract_operations/type_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use crate::{
ecmascript::{
execution::{agent::JsError, Agent, JsResult},
execution::{agent::ExceptionType, agent::JsError, Agent, JsResult},
types::{BigInt, Number, Object, PropertyKey, String, Value},
},
heap::WellKnownSymbolIndexes,
Expand Down Expand Up @@ -140,10 +140,10 @@ pub(crate) fn ordinary_to_primitive(
}

/// ### [7.1.2 ToBoolean ( argument )](https://tc39.es/ecma262/#sec-toboolean)
pub(crate) fn to_boolean(agent: &mut Agent, argument: Value) -> JsResult<Value> {
pub(crate) fn to_boolean(agent: &mut Agent, argument: Value) -> bool {
// 1. If argument is a Boolean, return argument.
if argument.is_boolean() {
return Ok(argument);
if let Value::Boolean(ret) = argument {
return ret;
}

// 2. If argument is one of undefined, null, +0𝔽, -0𝔽, NaN, 0ℤ, or the empty String, return false.
Expand All @@ -155,13 +155,13 @@ pub(crate) fn to_boolean(agent: &mut Agent, argument: Value) -> JsResult<Value>
|| argument.is_nan(agent)
|| argument.is_empty_string()
{
return Ok(false.into());
return false;
}

// 3. NOTE: This step is replaced in section B.3.6.1.

// 4. Return true.
Ok(true.into())
true
}

/// ### [7.1.3 ToNumeric ( value )](https://tc39.es/ecma262/#sec-tonumeric)
Expand All @@ -186,8 +186,15 @@ pub(crate) fn to_number(agent: &mut Agent, argument: Value) -> JsResult<Number>
}

// 2. If argument is either a Symbol or a BigInt, throw a TypeError exception.
if argument.is_symbol() || argument.is_bigint() {
todo!();
if argument.is_symbol() {
return Err(
agent.throw_exception(ExceptionType::TypeError, "cannot convert symbol to number")
);
}
if argument.is_bigint() {
return Err(
agent.throw_exception(ExceptionType::TypeError, "cannot convert bigint to number")
);
}

// 3. If argument is undefined, return NaN.
Expand All @@ -207,7 +214,7 @@ pub(crate) fn to_number(agent: &mut Agent, argument: Value) -> JsResult<Number>

// 6. If argument is a String, return StringToNumber(argument).
if argument.is_string() {
todo!();
todo!("implement StringToNumber");
}

// 7. Assert: argument is an Object.
Expand Down