Commit graph

1289 commits

Author SHA1 Message Date
Linus Groh
6bdd62b51b LibJS: Convert Now AOs to ThrowCompletionOr 2021-09-17 23:43:01 +02:00
Linus Groh
d764f1c699 LibJS: Convert PlainDateTime AOs to ThrowCompletionOr 2021-09-17 23:43:01 +02:00
Linus Groh
35bba1c98d LibJS: Convert PlainDate AOs to ThrowCompletionOr 2021-09-17 23:43:01 +02:00
Linus Groh
20337a34a2 LibJS: Convert Instant AOs to ThrowCompletionOr 2021-09-17 08:25:12 +01:00
Linus Groh
ea7cc70118 LibJS: Convert Duration AOs to ThrowCompletionOr 2021-09-17 08:25:12 +01:00
Linus Groh
f8d92232c8 LibJS: Convert Calendar AOs to ThrowCompletionOr 2021-09-17 08:25:12 +01:00
Linus Groh
6b4777c558 LibJS: Convert prepare_temporal_fields() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
9ac426c906 LibJS: Convert to_positive_integer() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
2f56fd48ca LibJS: Convert parse_temporal_year_month_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
14f16d9ed4 LibJS: Convert parse_temporal_time_zone_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
0ccd11ba5f LibJS: Convert parse_temporal_time_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
f86fa12deb LibJS: Convert parse_temporal_duration_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
3c530dec5a LibJS: Convert parse_temporal_date_time_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
4cb6eaf588 LibJS: Convert parse_temporal_date_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
2e28f0b371 LibJS: Convert parse_temporal_calendar_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
4f487266b4 LibJS: Convert parse_temporal_instant_string() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
3112de6f35 LibJS: Convert parse_iso_date_time() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
8792fdfdf6 LibJS: Convert reject_temporal_calendar_type() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
8dd45a1ba2 LibJS: Convert validate_temporal_unit_range() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
448a364210 LibJS: Convert to_smallest_temporal_unit() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
92187591dd LibJS: Convert to_largest_temporal_unit() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
dd483d84f8 LibJS: Convert to_seconds_string_precision() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
f8f074f8a9 LibJS: Convert to_temporal_rounding_increment() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
65d26dd25e LibJS: Convert to_show_calendar_option() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
509d13cf67 LibJS: Convert to_temporal_rounding_mode() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
9f03647f1f LibJS: Convert to_temporal_overflow() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
b1e7e62657 LibJS: Convert get_string_or_number_option() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
8d6ac267dc LibJS: Convert Temporal::get_option() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
08dd1c3e2d LibJS: Convert get_options_object() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
f2b5ddd167 LibJS: Convert iterable_to_list_of_type() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
683e31e1ff LibJS: Convert to_integer_throw_on_infinity() to ThrowCompletionOr 2021-09-16 22:34:24 +01:00
Linus Groh
fea27143e9 LibJS: Initialize value in ThrowCompletionOr<void> default constructor
Otherwise, TRY() will crash when calling release_value() on the empty
m_value Optional.
2021-09-16 22:34:24 +01:00
Andreas Kling
63a0ebcc90 js: Add a simple loadJSON(path) built-in to load JSON from a file
This is very handy when you want to load data from /proc, for example.
2021-09-16 21:49:50 +02:00
Idan Horowitz
5ea1810ada LibJS: Convert PlainMonthDay AOs to ThrowCompletionOr 2021-09-16 13:53:37 +01:00
Idan Horowitz
229a5ce149 LibJS: Convert PlainTime AOs to ThrowCompletionOr 2021-09-16 13:53:37 +01:00
Idan Horowitz
c57a78423d LibJS: Convert PlainYearMonth AOs to ThrowCompletionOr 2021-09-16 13:53:37 +01:00
Idan Horowitz
cc00a726a8 LibJS: Convert TimeZone AOs to ThrowCompletionOr 2021-09-16 13:53:37 +01:00
Idan Horowitz
830d484d78 LibJS: Change GetISOPartsFromEpoch's return type to ISODateTime
This AO can't throw so its optional is never empty.
2021-09-16 13:53:37 +01:00
Idan Horowitz
1822b2938e LibJS: Convert ZonedDateTime AOs to ThrowCompletionOr 2021-09-16 13:53:37 +01:00
Idan Horowitz
5a4c90fcb1 LibJS: Convert ordinary_create_from_constructor<T> to ThrowCompletionOr 2021-09-16 13:53:37 +01:00
Brian Gianforcaro
9f50e288f7 LibJS: Fix Clang Toolchain CI build
Clang was failing because because it rightfully saw we were attempting
to call a deleted constructor of `MarkedValueList`. If you explicitly
called move(list) then GCC would complain that the move was unnecessary.

For what ever reason both tool chains accept when we construct the
ThrowCompletionOr explicitly that we move the list into and return that.
2021-09-16 10:05:45 +01:00
Linus Groh
2d4650714f LibJS: Use ThrowCompletionOr in get_prototype_from_constructor()
Also add spec step comments to it while we're here.
2021-09-15 23:46:53 +01:00
Linus Groh
bc1b8f9cc8 LibJS: Use ThrowCompletionOr in get_function_realm() 2021-09-15 23:46:53 +01:00
Linus Groh
3d43eb0774 LibJS: Use ThrowCompletionOr in species_constructor()
Also add spec step comments to it as well as a missing exception check
while we're here.
2021-09-15 23:46:53 +01:00
Linus Groh
c4c40f4cf3 LibJS: Use ThrowCompletionOr in create_list_from_array_like()
Also add spec step comments to it while we're here.
2021-09-15 23:46:53 +01:00
Linus Groh
568296d0cc LibJS: Use ThrowCompletionOr in require_object_coercible() 2021-09-15 23:46:53 +01:00
Linus Groh
33679a8445 LibJS: Add a JS::Completion class and JS::ThrowCompletionOr<T> template
We decided that we want to move away from throwing exceptions in AOs
and regular functions implicitly and then returning some
default-constructed value (usually an empty JS::Value) - this requires
remembering to check for an exception at the call site, which is
error-prone. It's also awkward for return values that cannot be
default-constructed, e.g. MarkedValueList.
Instead, the thrown value should be part of the function return value.

The solution to this is moving closer to the spec and using something
they call "completion records":
https://tc39.es/ecma262/#sec-completion-record-specification-type

This has various advantages:

- It becomes crystal clear whether some AO can fail or not, and errors
  need to be handled and values unwrapped explicitly (for that reason
  compatibility with the TRY() macro is already built-in, and a similar
  TRY_OR_DISCARD() macro has been added specifically for use in LibJS,
  while the majority of functions doesn't return ThrowCompletionOr yet)
- We no longer need to mix "valid" and "invalid" values of various types
  for the success and exception outcomes (e.g. null/non-null AK::String,
  empty/non-empty JS::Value)
- Subsequently it's no longer possible to accidentally use an exception
  outcome return value as a success outcome return value (e.g. any AO
  that returns a numeric type would return 0 even after throwing an
  exception, at least before we started making use of Optional for that)
- Eventually the VM will no longer need to store an exception, and
  temporarily clearing an exception (e.g. to call a function) becomes
  obsolete - instead, completions will simply propagate up to the caller
  outside of LibJS, which then can deal with it in any way
- Similar to throw we'll be able to implement the functionality of
  break, continue, and return using completions, which will lead to
  easier to understand code and fewer workarounds - the current
  unwinding mechanism is not even remotely similar to the spec's
  approach

The spec's NormalCompletion and ThrowCompletion AOs have been
implemented as simple wrappers around the JS::Completion constructor.
UpdateEmpty has been implemented as a JS::Completion method.

There's also a new VM::throw_completion<T>() helper, which basically
works like VM::throw_exception<T>() - it creates a T object (usually a
JS::Error), and returns it wrapped in a JS::Completion of Type::Throw.

Two temporary usage patterns have emerged:

1. Callee already returns ThrowCompletionOr, but caller doesn't:

    auto foo = TRY_OR_DISCARD(bar());

2. Caller already returns ThrowCompletionOr, but callee doesn't:

    auto foo = bar();
    if (auto* exception = vm.exception())
        return throw_completion(exception->value());

Eventually all error handling and unwrapping can be done with just TRY()
or possibly even operator? in the future :^)

Co-authored-by: Andreas Kling <kling@serenityos.org>
2021-09-15 23:46:53 +01:00
Linus Groh
1a7828a9f3 LibJS: Return default-constructed values instead of the INVALID constant
This is much more common across the whole codebase and even these two
files. The same is used to return an empty JS::Value in an exception
check, for example.
2021-09-15 18:41:33 +01:00
Linus Groh
657d17ace3 LibJS: Remove two unused includes from AbstractOperations.cpp 2021-09-15 18:36:06 +01:00
Ali Mohammad Pur
53d24fbd65 LibJS: Make References see into Environment's bindings as well
'bindings' is the spec-compliant version of 'variables', but we were
simply not even looking at them, which made things using bindings (such
as named function expressions) break in unexpected ways after the move
to using references in call expressions.

Co-Authored-By: davidot <david.tuin@gmail.com>
2021-09-15 11:56:00 +02:00