Commit graph

494 commits

Author SHA1 Message Date
Idan Horowitz
f9395efaac LibJS: Use ToPropertyKey in Object.getOwnPropertyDescriptor
The specification requires this. (And the current usage of
PropertyName::from_value is invalid since integers are not allowed in
this context)
2021-06-07 16:51:09 +01:00
Idan Horowitz
1c51edb639 LibJS: Add missing length field to Symbol.prototype[Symbol.ToPrimitive]
Since the argument was missing Attribute::Configurable was used as the
length, which resulted in incorrect attributes being applied.
2021-06-07 16:51:09 +01:00
Ali Mohammad Pur
71b4433b0d LibWeb+LibSyntax: Implement nested syntax highlighters
And use them to highlight javascript in HTML source.
This commit also changes how TextDocumentSpan::data is interpreted,
as it used to be an opaque pointer, but everyone stuffed an enum value
inside it, which made the values not unique to each highlighter;
that field is now a u64 serial id.
The syntax highlighters don't need to change their ways of stuffing
token types into that field, but a highlighter that calls another
nested highlighter needs to register the nested types for use with
token pairs.
2021-06-07 14:45:49 +04:30
Andreas Kling
61c56e75f4 LibJS: Flatten Shape::property_table()
In the common case, we take the early return in ensure_property_table()
so let's make sure it gets inlined into property_table().
2021-06-07 10:22:25 +02:00
Andreas Kling
d24f4462c7 LibJS: Add VM::dump_backtrace()
This is just a simple helper that dumps the current VM call stack
to the debug console. I find myself rewriting this function over and
over, so let's just have it in the tree.
2021-06-07 10:22:25 +02:00
Linus Groh
3dfd450f2d LibJS: Use Array::create() length arg in favor of set_array_like_size()
This way we don't bypass the maximum length check.
2021-06-06 23:27:47 +01:00
Linus Groh
1c906b07a4 LibJS: Add length parameter to Array::create()
This is now a bit closer to the spec's 10.4.2.2 ArrayCreate - it will
throw a RangeError if the requested length exceeds 2^32 - 1, so anyone
passing in a custom value (defaults to zero for same behaviour as
before) will need an exception check at the call site.
2021-06-06 23:25:33 +01:00
Linus Groh
e7bfd34ea7 LibJS: Add dbgln() to Heap::allocator_for_size() before crashing
If we can't get a CellAllocator for the requested cell size, at least
print a debug message before dying.
2021-06-06 23:08:15 +01:00
Idan Horowitz
f65cb63aab LibJS: Check dates are below the time_clip threshold 2021-06-06 19:14:11 +01:00
Idan Horowitz
c4530e95f4 LibJS: Add Date.prototype.toJSON() 2021-06-06 19:14:11 +01:00
Idan Horowitz
ed7eb403fe LibJS: Add Date.prototype[@@toPrimitive]() 2021-06-06 19:14:11 +01:00
Idan Horowitz
60e70e5ee4 LibJS: Add Date.setUTC{Date, Month, Hours, ...}() aliases
These are a bit hacky, since they are supposed to be separate methods,
but since serenity only supports UTC currently, they are equivalent.
2021-06-06 19:14:11 +01:00
Idan Horowitz
46214f0657 LibJS: Store Date milliseconds as signed to support negative offsets
We need to support the range of -999 to 999.
2021-06-06 19:14:11 +01:00
Idan Horowitz
17afe015a5 LibJS: Add Date.prototype.setTime() 2021-06-06 19:14:11 +01:00
Idan Horowitz
a93b1c7ea0 LibJS: Add Date.prototype.setMonth() 2021-06-06 19:14:11 +01:00
Idan Horowitz
59034554a4 LibJS: Account for differences in month representations (0-11 vs 1-12)
Since DateTime stores months as 1 to 12, while JS accepts months as
0 to 11, we have to account for the difference (by subtracting or
adding 1) where appropriate.
2021-06-06 19:14:11 +01:00
Idan Horowitz
b893963651 LibJS: Add Date.prototype.setDate() 2021-06-06 19:14:11 +01:00
Idan Horowitz
3eb05d9413 LibJS: Stub out Date.prototype.getTimezoneOffset()
We only support UTC currently, so this always returns 0 as long as the
date is not invalid.
2021-06-06 19:14:11 +01:00
Idan Horowitz
44926b3f80 LibJS: Ignore arguments in Date's constructor when called as a function
Since theres no way to drop the arguments before the call to the
constructor (or to signal to the constructor that it was not called
directly), we simply reuse the code for the no arguments provided
special case. (And to prevent code duplication, the code was extracted
into the separate static function Date::now(GlobalObject&).
2021-06-06 19:14:11 +01:00
Linus Groh
ba9d3bc38c LibJS: Create 1970-01-01 00:00:00 local time Date for invalid ctor call
When using Core::DateTime::from_timestamp(0) the resulting Date is
1970-01-01 00:00:00 in UTC, which might be something different in local
time - this is incorrect and relevant as invalid Dates can be made valid
later on.
2021-06-06 19:34:43 +02:00
Linus Groh
ff5aa3ca70 LibJS: Make it so that Date.prototype.toGMTString === .toUTCString 2021-06-06 19:34:43 +02:00
Linus Groh
60aace8400 LibJS: Remove unused includes from SymbolPrototype.cpp 2021-06-06 19:34:43 +02:00
Linus Groh
b661363dfe LibJS: Implement String.prototype[@@toPrimitive]() 2021-06-06 19:34:43 +02:00
Linus Groh
e9a0759b16 LibJS: Add String.prototype.valueOf()
It should have its own and not inherit from the Object prototype.
2021-06-06 19:34:43 +02:00
Linus Groh
8d7ec28924 LibJS: Remove String.prototype.length
A string's length property is supposed to be a regular non-writable,
non-enumerable, non-configurable property on the StringObject instead.
2021-06-06 19:34:43 +02:00
Linus Groh
fc2673d111 LibJS: Replace SymbolPrototype's typed_this() with this_symbol_value()
This brings the code close to the spec with no trade-offs, which is
always valuable.

No functionality change.
2021-06-06 19:34:43 +02:00
Linus Groh
6f96f01171 LibJS: Replace StringPrototype's typed_this() with this_string_value()
This brings the code close to the spec with no trade-offs, which is
always valuable.

No functionality change.
2021-06-06 19:34:43 +02:00
Linus Groh
68d669443f LibJS: Replace bigint_object_from() with this_bigint_value()
This brings the code close to the spec with no trade-offs, which is
always valuable.

No functionality change.
2021-06-06 19:34:43 +02:00
Linus Groh
d255e6892b LibJS: Update NumberPrototype's this_number_value() to take a Value
This is now about as close to the spec as it gets - instead of querying
the |this| value inside of the function, we now pass it in from the
outside.
Also get rid of the oddly specific error messages, they're nice but
pretty inconsistent with most others. Let's prefer consistency and
simplicity for now.

Other than that, no functionality change.
2021-06-06 19:34:43 +02:00
Linus Groh
3cfd9f51f7 LibJS: Replace some is_nullish() checks with require_object_coercible() 2021-06-06 19:34:43 +02:00
Linus Groh
cd12b2aa57 LibJS: Implement the RequireObjectCoercible abstract operation
Throws an exception if the given value is nullish, returns it otherwise.
We can now gradually replace such manual checks with this function where
applicable.

This also has the advantage that the somewhat useless "ToObject on null
or undefined" will be replaced with "null cannot be converted to an
object" or "undefined cannot be converted to an object". :^)
2021-06-06 19:34:43 +02:00
Andreas Kling
f290048662 LibJS: Pass unwinding target labels a bit more efficiently
Use const reference or move semantics when passing these labels.
2021-06-06 11:45:52 +02:00
Linus Groh
887852f36d LibJS: Make Number.prototype.toString() radix coercion spec compliant
This should use the ToIntegerOrInfinity() abstract operation.
2021-06-06 07:03:12 +01:00
Linus Groh
adc3de4480 LibJS: Implement Number.prototype.valueOf() 2021-06-06 06:56:08 +01:00
Idan Horowitz
0bf597e99d LibJS: Uncomment and add parseInt tests
Added another test that checks radices > 16, as well as uncommented
several "FIXME" tests that are now working.
2021-06-06 01:34:22 +01:00
Idan Horowitz
aa5b144f90 LibJS: Correct modulo behaviour in to_i32 to match the specification 2021-06-06 01:34:22 +01:00
Idan Horowitz
bbf75d0bea LibJS: Trim initial whitespace in parseFloat 2021-06-06 01:34:22 +01:00
Idan Horowitz
bda32e9440 LibJS: Parse digits with parse_ascii_base36_digit in parseInt
This was accidentally replaced with parse_ascii_hex_digit in
bc8d16ad28 which caused radices above
16 (hex) to fail.
2021-06-06 01:34:22 +01:00
Idan Horowitz
26a0dbdd9e LibJS: Set the length property of parseInt to 2
The method takes 2 arguments
2021-06-06 01:34:22 +01:00
Idan Horowitz
2a8f4f097c LibJS: Throw TypeError on write to non-writable property in strict mode 2021-06-05 23:54:08 +01:00
Idan Horowitz
442ef63008 LibJS: Add the global escape() & unescape() methods 2021-06-05 18:55:08 +01:00
Idan Horowitz
e2fb7943f7 LibJS: Correctly handle NaN and negative infinity in Math.atan2
The current implementation was missing an early return on a NaN
argument and mixed up a couple of the positive/negative infinity early
returns.
2021-06-05 14:56:58 +01:00
Idan Horowitz
57a52094d1 LibJS: Rewrite Math.hypot to handle exceptions, NaNs, Infinity properly
The specification requires that we immediately return Infinity during
the iteration over the arguments if positive or negative infinity is
encountered, and return a NaN if it is encountered and no Infinity was
found. The specification also requires all arguments to be coerced into
numbers before the operation starts, or else a number conversion
exception could be missed due to the Infinity/NaN early return.
2021-06-05 14:56:58 +01:00
Idan Horowitz
03255c1c53 LibJS: Handle NaN/Infinity/Zero edge cases in Math.pow()
This commit replaces the current simple call to LibM's pow with the
full implementation of 6.1.6.1.3 Number::exponentiate:
https://tc39.es/ecma262/#sec-numeric-types-number-exponentiate
2021-06-05 14:56:58 +01:00
Idan Horowitz
7507999230 LibJS: Rewrite Math.{max, min} to handle exceptions and NaNs properly
The specification requires that we immediately return a NaN during the
iteration over the arguments if one is encountered. It also requires
all arguments to be coerced into numbers before the operation starts,
or else a number conversion exception could be missed due to the NaN
early return.
2021-06-05 14:56:58 +01:00
Idan Horowitz
24ffe91b16 LibJS: Handle negative zero and negative infinity in Math.abs()
As required by the specification:
3. If n is -0, return +0.
4. If n is -∞, return +∞.
2021-06-05 14:56:58 +01:00
Idan Horowitz
9d2e90d569 LibJS: Add Math.imul() 2021-06-05 14:56:58 +01:00
Idan Horowitz
de10f0dc6c LibJS: Support symbol keys in Object.prototype.propertyIsEnumerable 2021-06-05 14:15:28 +01:00
Idan Horowitz
eb0b1c432a LibJS: Replace StringOrSymbol::from_value with Value::to_property_key
This is a more specification compliant implementation of the
abstract operation 7.1.19 ToPropertyKey which should handle boxed
symbols correctly.
2021-06-05 14:15:28 +01:00
Andreas Kling
42fcc2219d LibJS: Use PropertyName::as_string() in Object::get()
After we've already checked is_string(), we can use as_string() to
avoid a temporary String.
2021-06-05 13:00:34 +02:00