-
Notifications
You must be signed in to change notification settings - Fork 76
feat(ecmascript): Temporal.PlainTime constructor #965
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
Open
jesperkha
wants to merge
3
commits into
trynova:main
Choose a base branch
from
jesperkha:constructor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,11 @@ | |
| use crate::{ | ||
| ecmascript::{ | ||
| Agent, ArgumentsList, BUILTIN_STRING_MEMORY, Behaviour, Builtin, | ||
| BuiltinIntrinsicConstructor, JsResult, Object, Realm, String, Value, | ||
| builders::BuiltinFunctionBuilder, | ||
| BuiltinIntrinsicConstructor, ExceptionType, Function, JsResult, Object, Realm, String, | ||
| Value, builders::BuiltinFunctionBuilder, create_temporal_plain_time, | ||
| temporal_err_to_js_err, to_integer_with_truncation, | ||
| }, | ||
| engine::{GcScope, NoGcScope}, | ||
| engine::{Bindable as _, GcScope, NoGcScope, Scopable}, | ||
| heap::IntrinsicConstructorIndexes, | ||
| }; | ||
|
|
||
|
|
@@ -25,14 +26,125 @@ impl BuiltinIntrinsicConstructor for TemporalPlainTimeConstructor { | |
| } | ||
|
|
||
| impl TemporalPlainTimeConstructor { | ||
| /// ### [4.1.1 Temporal.PlainTime](https://tc39.es/proposal-temporal/#sec-temporal.plaintime) | ||
| fn constructor<'gc>( | ||
| agent: &mut Agent, | ||
| _: Value, | ||
| _args: ArgumentsList, | ||
| _new_target: Option<Object>, | ||
| gc: GcScope<'gc, '_>, | ||
| args: ArgumentsList, | ||
| new_target: Option<Object>, | ||
| mut gc: GcScope<'gc, '_>, | ||
| ) -> JsResult<'gc, Value<'gc>> { | ||
| Err(agent.todo("Temporal.PlainTime", gc.into_nogc())) | ||
| let hours = args.get(1).scope(agent, gc.nogc()); | ||
| let minutes = args.get(2).scope(agent, gc.nogc()); | ||
| let seconds = args.get(3).scope(agent, gc.nogc()); | ||
| let milliseconds = args.get(4).scope(agent, gc.nogc()); | ||
| let microseconds = args.get(5).scope(agent, gc.nogc()); | ||
| let nanoseconds = args.get(6).scope(agent, gc.nogc()); | ||
|
|
||
| let new_target = new_target.bind(gc.nogc()); | ||
|
|
||
| // 1. If NewTarget is undefined, throw a TypeError exception. | ||
| let Some(new_target) = new_target else { | ||
| return Err(agent.throw_exception_with_static_message( | ||
| ExceptionType::TypeError, | ||
| "calling a builtin Temporal.PlainTime constructor without new is forbidden", | ||
| gc.into_nogc(), | ||
| )); | ||
| }; | ||
|
|
||
| let Ok(new_target) = Function::try_from(new_target) else { | ||
| unreachable!() | ||
| }; | ||
| let new_target = new_target.scope(agent, gc.nogc()); | ||
|
|
||
| // 2. If hour is undefined, set hour to 0; else set hour to ? ToIntegerWithTruncation(hour). | ||
| let h = if hours.get(agent).is_undefined() { | ||
| Ok(0) | ||
| } else { | ||
| u8::try_from( | ||
| to_integer_with_truncation(agent, hours.get(agent), gc.reborrow()).unbind()?, | ||
| ) | ||
| }; | ||
|
|
||
| // 3. If minute is undefined, set minute to 0; else set minute to ? ToIntegerWithTruncation(minute). | ||
| let m = if minutes.get(agent).is_undefined() { | ||
| Ok(0) | ||
| } else { | ||
| u8::try_from( | ||
| to_integer_with_truncation(agent, minutes.get(agent), gc.reborrow()).unbind()?, | ||
| ) | ||
| }; | ||
|
|
||
| // 4. If second is undefined, set second to 0; else set second to ? ToIntegerWithTruncation(second). | ||
| let s = if seconds.get(agent).is_undefined() { | ||
| Ok(0) | ||
| } else { | ||
| u8::try_from( | ||
| to_integer_with_truncation(agent, seconds.get(agent), gc.reborrow()).unbind()?, | ||
| ) | ||
| }; | ||
|
|
||
| // 5. If millisecond is undefined, set millisecond to 0; else set millisecond to ? ToIntegerWithTruncation(millisecond). | ||
| let ms = if milliseconds.get(agent).is_undefined() { | ||
| Ok(0) | ||
| } else { | ||
| u16::try_from( | ||
| to_integer_with_truncation(agent, milliseconds.get(agent), gc.reborrow()) | ||
| .unbind()?, | ||
| ) | ||
| }; | ||
|
|
||
| // 6. If microsecond is undefined, set microsecond to 0; else set microsecond to ? ToIntegerWithTruncation(microsecond). | ||
| let mis = if microseconds.get(agent).is_undefined() { | ||
| Ok(0) | ||
| } else { | ||
| u16::try_from( | ||
| to_integer_with_truncation(agent, microseconds.get(agent), gc.reborrow()) | ||
| .unbind()?, | ||
| ) | ||
| }; | ||
|
|
||
| // 7. If nanosecond is undefined, set nanosecond to 0; else set nanosecond to ? ToIntegerWithTruncation(nanosecond). | ||
| let ns = if nanoseconds.get(agent).is_undefined() { | ||
| Ok(0) | ||
| } else { | ||
| u16::try_from( | ||
| to_integer_with_truncation(agent, nanoseconds.get(agent), gc.reborrow()) | ||
| .unbind()?, | ||
| ) | ||
| }; | ||
|
|
||
| // 8. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception. | ||
| // 9. Let time be CreateTimeRecord(hour, minute, second, millisecond, microsecond, nanosecond). | ||
| let time = if let ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: Here splitting into the |
||
| Ok(hour), | ||
| Ok(minute), | ||
| Ok(second), | ||
| Ok(millisecond), | ||
| Ok(microsecond), | ||
| Ok(nanosecond), | ||
| ) = (h, m, s, ms, mis, ns) | ||
| { | ||
| temporal_rs::PlainTime::try_new( | ||
| hour, | ||
| minute, | ||
| second, | ||
| millisecond, | ||
| microsecond, | ||
| nanosecond, | ||
| ) | ||
| .map_err(|err| temporal_err_to_js_err(agent, err, gc.nogc())) | ||
| .unbind()? | ||
| } else { | ||
| return Err(agent.throw_exception_with_static_message( | ||
| ExceptionType::RangeError, | ||
| "not a valid time", | ||
| gc.into_nogc(), | ||
| )); | ||
| }; | ||
|
|
||
| // 10. Return ? CreateTemporalTime(time, NewTarget). | ||
| create_temporal_plain_time(agent, time, Some(new_target.get(agent)), gc).map(Value::from) | ||
| } | ||
|
|
||
| pub(crate) fn create_intrinsic(agent: &mut Agent, realm: Realm<'static>, _gc: NoGcScope) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Let's end all of the
try_from's with anunwrap_or(u8/u16::MAX), and replaceOk(0)s with just0.