Commit graph

1114 commits

Author SHA1 Message Date
Linus Groh
9be0b664e3 LibJS: Make length_of_array_like() take an Object rather than Value
The pseudo-code from the spec says "Assert: Type(obj) is Object.", so we
can just enforce this at compile time rather than taking it literally
and doing "ASSERT(value.is_object())".

Also fix an issue where the absence of a "length" property on the object
would cause a crash (to_number() on empty value).
2021-01-10 21:57:03 +01:00
Marcin Gasperowicz
b24ce0b5ee LibJS: Implement String.prototype.split
This adds a String.prototype.split implementation modelled after 
ECMA262 specification. 

Additionally, `Value::to_u32` was added as an implementation of
the standard `ToUint32` abstract operation.

There is a tiny kludge for when the separator is an empty string. 
Basic tests and visiting google.com prove that this is working.
2021-01-10 21:27:59 +01:00
asynts
1160817a9e AK: Add Formatter<FormatString> as helper class. 2021-01-09 21:11:09 +01:00
Linus Groh
7b2fdd08ce LibJS: Add tests for bitwise NOT operator 2021-01-09 19:09:02 +01:00
Linus Groh
9fca86109b LibJS: Make bitwise NOT work correctly with NaN and Infinity
This was missing a "toInt32()" which returns 0 for NaN and Infinity.
From the spec:

    6.1.6.1.2 Number::bitwiseNOT ( x )

    The abstract operation Number::bitwiseNOT takes argument x (a Number).
    It performs the following steps when called:

        Let oldValue be ! ToInt32(x).
        Return the result of applying bitwise complement to oldValue.
        The mathematical value of the result is exactly representable as
        a 32-bit two's complement bit string.

Fixes #4868.
2021-01-09 19:09:02 +01:00
Linus Groh
c55cb7843a LibJS: Use INVALID some more in abstract operations 2021-01-09 19:09:02 +01:00
Andreas Kling
7ed89703fe LibCrypto+LibJS: Fix broken subtraction of two negative signed bigints
Found by oss-fuzz: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29326
2021-01-07 08:57:37 +01:00
Andreas Kling
51b880b038 LibJS: Disable ASAN during the conservative GC stack scan
This allows the JS fuzzer to survive garbage collection (so we can find
more interesting bugs!)

Found by oss-fuzz: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29266
2021-01-05 12:27:45 +01:00
Andreas Kling
fdd974b7ef LibJS: Fix UB downcast during GlobalObject construction
When constructing a GlobalObject, it has to pass itself as the global
object to its own Shape. Since this is done in the Object constructor,
and Object is a base class of GlobalObject, it's not yet valid to cast
"this" to a GlobalObject*.

Fix this by having Shape store the global object as an Object& and move
Shape::global_object() to GlobalObject.h where we can at least perform a
valid static_cast in the getter.

Found by oss-fuzz: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29267
2021-01-05 12:02:59 +01:00
Andreas Kling
db790dda62 LibJS: Remove hand-rolled type information in JS AST in favor of RTTI 2021-01-01 19:34:07 +01:00
Andreas Kling
d2613403e0 LibJS+LibWeb: Stop generating is_foo_wrapper() for JS DOM wrappers 2021-01-01 18:06:38 +01:00
Andreas Kling
f48751a739 LibJS: Remove hand-rolled Object is_foo() helpers in favor of RTTI 2021-01-01 17:46:39 +01:00
Andreas Kling
8333055c3d LibJS: Use RTTI for inheritance checks
This replaces the hand-rolled string-based inheritance check tech.
2021-01-01 17:00:20 +01:00
Linus Groh
aa92adeedf LibJS: Fix email in TypedArray{Constructor,Prototype} copyright headers 2021-01-01 15:02:58 +01:00
Luke
0f66589007 Everywhere: Fix more typos 2020-12-31 01:47:41 +01:00
asynts
7e62ffbc6e AK+Format: Remove TypeErasedFormatParams& from format function. 2020-12-30 20:33:53 +01:00
AnotherTest
8ca0e8325a LibJS: Don't save rule start positions along with the parser state
This fixes #4617.
Also fixes the small problem where some save states would be leaked.
2020-12-29 17:39:42 +01:00
Egor Ananyin
1dbd264239 LibJS: Uncomment the tests that pass now 2020-12-29 13:43:16 +01:00
AnotherTest
d0363bca01 LibJS: `save_state()' before creating a RulePosition
Fixes #4617.
2020-12-29 10:51:33 +01:00
AnotherTest
b34b681811 LibJS: Track source positions all the way down to exceptions
This makes exceptions have a trace of source positions too, which could
probably be helpful in making fancier error tracebacks.
2020-12-29 00:58:43 +01:00
Egor Ananyin
f30d4f22ef LibJS: Add tests for new Math functions 2020-12-28 19:03:11 +01:00
Egor Ananyin
7c9c3a10d3 LibJS: Add almost all Math functions 2020-12-28 19:03:11 +01:00
Luke
be30dc2b18 LibJS: Implement Object.prototype.isPrototypeOf
Spec: https://tc39.es/ecma262/#sec-object.prototype.isprototypeof
2020-12-28 13:10:07 +01:00
Xavier Cooney
ca0f3db004 LibJS: Implement Array.prototype.sort() 2020-12-27 23:24:33 +01:00
Andreas Kling
a103eae0d4 LibJS: Run "prettier" on the tests :^) 2020-12-27 23:13:52 +01:00
Stephan Unverwerth
f603128e55 LibJS: Fix old object numeric key test now that toString() is correct 2020-12-27 23:04:09 +01:00
Stephan Unverwerth
d3524f47a0 LibJS: Implement (mostly) spec compliant version of Number.toString() 2020-12-27 23:04:09 +01:00
Stephan Unverwerth
be9c2feff0 LibJS: Fix parsing of numeric object keys
Numeric keys were interpreted as their source text, leading to
something like {0x10:true} to end up as {"0x10":true}
instead of {16:true}
2020-12-27 23:04:09 +01:00
Linus Groh
5122f98198 Base+LibJS+LibWeb: Make prettier clean
Also use "// prettier-ignore" comments where necessary rather than
excluding whole files (via .prettierignore).
2020-12-27 21:25:27 +01:00
Xavier Cooney
1cf92d39eb LibJS: Implement String.prototype.endsWith() 2020-12-26 01:09:04 +01:00
Xavier Cooney
43f948b357 LibJS: Implement IsRegExp abstract operation
This is needed by various String.prototype operations, as well as
the RegExp constructor.
2020-12-26 01:09:04 +01:00
Luke
200c7572b7 LibJS: Implement Object.prototype.propertyIsEnumerable
Spec: https://tc39.es/ecma262/#sec-object.prototype.propertyisenumerable

This is used by core-js, which is used by frameworks such as Vue.
2020-12-24 21:00:28 +01:00
AnotherTest
7c8d35600c Spreadsheet: Override visit_edges() and visit stored JS objects
...and don't let them leak out of their evaluation contexts.
Also keep the exceptions separate from the actual values.
This greatly reduces the number of assertions hit while entering random
data into a sheet.
2020-12-22 23:35:29 +01:00
Lenny Maiorani
765936ebae
Everywhere: Switch from (void) to [[maybe_unused]] (#4473)
Problem:
- `(void)` simply casts the expression to void. This is understood to
  indicate that it is ignored, but this is really a compiler trick to
  get the compiler to not generate a warning.

Solution:
- Use the `[[maybe_unused]]` attribute to indicate the value is unused.

Note:
- Functions taking a `(void)` argument list have also been changed to
  `()` because this is not needed and shows up in the same grep
  command.
2020-12-21 00:09:48 +01:00
Linus Groh
0974991d05 LibJS: Don't treat '?.' followed by decimal digit as QuestionMarkPeriod
From the spec: https://tc39.es/ecma262/#sec-punctuators

    OptionalChainingPunctuator ::
        ?. [lookahead ∉ DecimalDigit]

We were missing the lookahead and therefore incorrectly treating any
'?.' as TokenType::QuestionMarkPeriod.

Fixes #4409.
2020-12-14 22:25:46 +01:00
asynts
2981f10a5e LibWeb: Apply suggested fixes. 2020-12-09 21:05:06 +01:00
Andreas Kling
e99cfd517c LibJS: Add test for Math.asin() 2020-12-08 23:36:19 +01:00
Andreas Kling
48d2545572 LibJS: Get rid of Argument and ArgumentVector
This was used for a feature where you could pass a vector of arguments
to enter_scope(). Since that way of passing arguments was not GC-aware
(as vectors use C++ heap storage), let's avoid using it and make sure
everything that needs to stay alive is either on the stack or in traced
storage instead.
2020-12-08 18:28:18 +01:00
Andreas Kling
38268f1c53 LibJS: Create lexical scope for "catch" on the spot when throwing 2020-12-08 18:22:47 +01:00
Andreas Kling
fc9e43728b LibJS: Stop creating a redundant lexical scope on function call
We were scoping the arguments twice, first in execute_function_body(),
and then again in enter_scope().
2020-12-08 18:04:54 +01:00
Andreas Kling
df2a6cb4ab LibJS: Add Math.acos() and Math.asin() 2020-12-08 17:49:02 +01:00
Andreas Kling
231171364d LibJS: Remove some unnecessary null checks
It's okay to add nullptr to the conservative roots set. We'll just
ignore it later on anyway.
2020-12-08 17:49:02 +01:00
Andreas Kling
d2e3e038d6 LibJS: Use IndexedProperties::for_each_value() in update_function_name()
This allows us to get rid of IndexedProperties::values_unordered().
2020-12-08 17:49:02 +01:00
Andreas Kling
6c4b823cef LibJS: Make marking object indexed properties less allocation-heavy
We were building up a vector with all the values in an object's indexed
property storage, and then iterating over the vector to mark values.
Instead of this, simply iterate over the property storage directly. :^)
2020-12-08 17:49:02 +01:00
Andreas Kling
930fae633e LibJS: Make sure GlobalObject marks the Proxy constructor 2020-12-08 17:49:02 +01:00
Andreas Kling
63b748642a LibJS: Add Math.atan() 2020-12-08 17:49:02 +01:00
Linus Groh
5eb1f752ab LibJS: Use new format functions everywhere
This changes the remaining uses of the following functions across LibJS:

- String::format() => String::formatted()
- dbg() => dbgln()
- printf() => out(), outln()
- fprintf() => warnln()

I also removed the relevant 'LogStream& operator<<' overloads as they're
not needed anymore.
2020-12-06 18:52:52 +01:00
Linus Groh
abd49c174a LibJS: Include source location hint in Parser::print_errors() 2020-12-06 18:52:52 +01:00
Linus Groh
28552f3f36 LibJS: Remove unused {INTERPRETER,VM}_DEBUG 2020-12-06 18:52:43 +01:00
Linus Groh
a70aacd7c3 LibJS: Add Float{32,64}Array 2020-12-05 23:52:17 +01:00
Andreas Kling
cc14b5a6d7 LibJS: Implement a very hackish "arguments" object
We now lazily create an "arguments" array inside functions when code
tries to access it.

This doesn't follow the spec at all but still covers a lot of the
basic uses of arguments, i.e "arguments.length" and "arguments[n]"
2020-12-05 16:41:41 +01:00
Andreas Kling
e6dadd9e5b LibJS: Implement parseInt()
Here's a reasonably faithful implementation of ECMAScript 2021 18.2.5.
Some corner cases are not covered, I've left them as FIXME's in the
included unit test.

Also I had to tweak JS::Value::to_i32() to always convert infinity to
zero, which is in accordance with ToInt32 AFAICT.
2020-12-05 13:55:55 +01:00
Linus Groh
909f430612 LibJS: Move is_script_function() to Object
Every Object should be able to tell us if it is a ScriptFunction, not
only Function objects!
2020-12-05 10:05:28 +01:00
Linus Groh
cc5be96724 LibJS: Use ArrayBuffer for typed array data
This is how the spec describes it, and it allows sharing data between
multiple typed arrays.
Typed arrays now support constructing from an existing ArrayBuffer,
and has been prepared for constructing from another typed array or
iterator as well.
2020-12-02 23:49:00 +01:00
Linus Groh
32571dfa53 LibJS: Add ArrayBuffer 2020-12-02 23:49:00 +01:00
Linus Groh
cf9da66b3e LibJS: Use Value::to_index() in typed array constructors 2020-12-02 23:49:00 +01:00
Linus Groh
eaa85969c4 LibJS: Add Value::to_{index,length,integer_or_infinity} abstract operations
We should pay more attention to using the well-defined abstract
operations from the spec rather than making up our own, often slightly
different rules. This is another step in that direction.
2020-12-02 23:49:00 +01:00
Linus Groh
6de4f1fcb3 LibJS: Add generic InvalidLength error type
We have multiple array types now, so ArrayInvalidLength has been
replaced with a generic InvalidLength.

Also fixes a small issue in the Array constructor, it should throw
RangeError for invalid lengths, not TypeError.
2020-12-02 23:49:00 +01:00
Linus Groh
0b086c759a LibJS: Move TypedArray length getter to prototype 2020-12-02 14:39:53 +01:00
Linus Groh
ddaab598a7 LibJS: Add TypedArray.BYTES_PER_ELEMENT 2020-12-02 12:52:31 +01:00
Linus Groh
bb6bc70c5b LibJS: Add more tests for TypedArray 2020-12-02 12:52:31 +01:00
Linus Groh
3d05836a3e LibJS: Make TypedArray constructor (somewhat) spec-compliant
- Calling without 'new' is an error
- If the first argument is an object, we need a separate code path to
  initialize from TypedArray, ArrayBuffer, Iterable or Array-like
  object (marked TODO for now)
- Don't insert values into array if more than one argument is present
  (that's not part of the spec)
2020-12-02 12:52:31 +01:00
Linus Groh
1bff65c591 LibJS: Add ErrorType::ConstructorWithoutNew
...and use it in Proxy::call(), rather than having a specific error
type just for that.
2020-12-02 12:52:31 +01:00
Linus Groh
7fb299fe46 LibJS: Clean up TypedArray constructors and prototypes
The current implementation is not entirely correct yet. Two classes have
been added:
- TypedArrayConstructor, which the various typed array constructors now
  inherit from. Calling or constructing this class (from JS, that is)
  directly is not possible, we might want to move this abstract class
  functionality to NativeFunction at a later point.
- TypedArrayPrototype, which the various typed array prototypes now have
  as their own prototype. This will be the place where most of the
  functionality is being shared.

Relevant parts from the spec:

22.2.1 The %TypedArray% Intrinsic Object
The %TypedArray% intrinsic object:
- is a constructor function object that all of the TypedArray
  constructor objects inherit from.
- along with its corresponding prototype object, provides common
  properties that are inherited by all TypedArray constructors and their
  instances.

22.2.2 Properties of the %TypedArray% Intrinsic Object
The %TypedArray% intrinsic object:
- has a [[Prototype]] internal slot whose value is %Function.prototype%.

22.2.2.3 %TypedArray%.prototype
The initial value of %TypedArray%.prototype is the %TypedArray%
prototype object.

22.2.6 Properties of the TypedArray Constructors
Each TypedArray constructor:
- has a [[Prototype]] internal slot whose value is %TypedArray%.

22.2.6.2 TypedArray.prototype
The initial value of TypedArray.prototype is the corresponding
TypedArray prototype intrinsic object (22.2.7).

22.2.7 Properties of the TypedArray Prototype Objects
Each TypedArray prototype object:
- has a [[Prototype]] internal slot whose value is %TypedArray.prototype%.

22.2.7.2 TypedArray.prototype.constructor
The initial value of a TypedArray.prototype.constructor is the
corresponding %TypedArray% intrinsic object.
2020-12-02 12:52:31 +01:00
Andreas Kling
3565d3c60c LibJS: Add six typed arrays (signed and unsigned 8/16/32-bit)
This patch adds six of the standard type arrays and tries to share as
much code as possible:

- Uint8Array
- Uint16Array
- Uint32Array
- Int8Array
- Int16Array
- Int32Array
2020-12-01 21:05:25 +01:00
Andreas Kling
93feb7a81f LibJS: Have Uint8ClampedArray delegate OOB accesses to JS::Object
Uint8ClampedArray itself only cares about legitimate in-bounds accesses
since that's what where the specialization happens.
2020-12-01 17:12:04 +01:00
Andreas Kling
f2c7caf2db LibJS: Zero out memory in newly allocated Uint8ClampedArray objects 2020-12-01 17:06:48 +01:00
Linus Groh
48c19cdb06 LibJS: Remove ProxyPrototype
Proxy is an "exotic object" and doesn't have its own prototype. Use the
regular object prototype instead, but most stuff is happening on the
target object anyway. :^)
2020-12-01 09:54:37 +01:00
Linus Groh
ea08bf5812 LibJS: Fix crash in Lexer on EOF in unterminated template literal
Fixes #4252.
2020-11-30 11:36:42 +01:00
Andreas Kling
2d9d883274 LibJS: Add a basic implementation of String.prototype.substr() 2020-11-29 20:29:49 +01:00
Andreas Kling
2e4832c3da LibJS: Constructor function's "prototype" property should be writable
This matches other engines.
2020-11-29 19:49:27 +01:00
Andreas Kling
01c8765519 LibJS+LibWeb: Log JavaScript exceptions raised by web content
Instead of hiding JS exceptions raised on the web, we now print them to
the debug log. This will make it a bit easier to work out why some web
pages aren't working right. :^)
2020-11-29 16:50:32 +01:00
Linus Groh
3ac7fb9f6c LibJS: Disallow 'with' statement in strict mode 2020-11-28 20:33:41 +01:00
Andreas Kling
42b1b36eb4 LibJS: Run clang-format on WithScope.h 2020-11-28 17:36:38 +01:00
Andreas Kling
1fad95fec5 LibJS: Add a basic unit test for the "with" statement 2020-11-28 17:16:48 +01:00
Andreas Kling
9de6443ab7 LibJS: Add basic support for "with" statements
with statements evaluate an expression and put the result of it at the
"front" of the scope chain. This is implemented by creating a WithScope
object and placing it in front of the VM's current call frame's scope.
2020-11-28 17:16:48 +01:00
Andreas Kling
c3fe9b4df8 LibJS: Add a scope object abstraction
Both GlobalObject and LexicalEnvironment now inherit from ScopeObject,
and the VM's call frames point to a ScopeObject chain rather than just
a LexicalEnvironment chain.

This gives us much more flexibility to implement things like "with",
and also unifies some of the code paths that previously required
special handling of the global object.

There's a bunch of more cleanup that can be done in the wake of this
change, and there might be some oversights in the handling of the
"super" keyword, but this generally seems like a good architectural
improvement. :^)
2020-11-28 17:16:48 +01:00
Andreas Kling
e1bbc7c075 LibJS: Make JS::Value constructors take const cell pointers
Taking non-const cell pointers is asking for trouble, since passing e.g
a "const Object*" to Value(Object*) will actually call Value(bool),
which is most likely not what you want.
2020-11-28 17:16:48 +01:00
Andreas Kling
d617120499 LibJS: Parse "with" statements :^) 2020-11-28 17:16:48 +01:00
Andreas Kling
98f2da9834 LibJS: Rename Cell::visit_children() => Cell::visit_edges()
The GC heap is really a graph of cells, so "children" didn't quite feel
appropriate here.
2020-11-28 17:16:48 +01:00
Andreas Kling
97a05ac9ac LibJS: Allow Shape without a global object
It would be nice to be able to cache some shapes globally in the VM,
but then they can't be tied to a specific global object. So let's just
get rid of the requirement that shapes are tied to a global object.
2020-11-28 17:16:48 +01:00
Linus Groh
149b54e26f LibJS: Remove quotes from RegExpCompileError message
They look a bit out of place, especially for multi-line error messages.
2020-11-28 16:46:25 +01:00
Linus Groh
7dd77020ae LibJS: Use enumerator macros to define RegExp.prototype flag getters 2020-11-28 01:20:11 +01:00
Linus Groh
8a9a7f1677 LibJS: Make RegExp.prototype.source spec-compliant
Basically:
- And edge case for this object being RegExp.prototype.source
- Return "(?:)" for empty pattern
- Escape some things properly
2020-11-28 01:20:11 +01:00
Linus Groh
b6e5442d55 LibJS: Make RegExp.prototype.toString() spec-compliant
It should use the 'source' and 'flags' properties of the object, and
therefore work with non-RegExp objects as well.
2020-11-28 01:20:11 +01:00
Linus Groh
ee66eaa1b0 LibJS: Make RegExp.prototype.flags spec-compliant
This should be using the individual flag boolean properties rather than
the [[OriginalFlags]] internal slot.
Use an enumerator macro here for brevity, this will be useful for other
things as well. :^)
2020-11-28 01:20:11 +01:00
Linus Groh
5cb45e4feb LibJS: Make RegExp() constructor spec-compliant
- Default values should depend on arguments being undefined, not being
  missing
- "(?:)" for empty pattern happens in RegExp.prototype.source, not the
  constructor
2020-11-28 01:20:11 +01:00
AnotherTest
210a3db44d LibJS: Implement `RegExpPrototype::exec()'
This implements *only* the builtin exec() function.
2020-11-27 21:32:41 +01:00
AnotherTest
8ba273a2f3 LibJS: Hook up Regex<ECMA262> to RegExpObject and implement `test()'
This makes RegExpObject compile and store a Regex<ECMA262>, adds
all flag-related properties, and implements `RegExpPrototype.test()`
(complete with 'lastIndex' support) :^)
It should be noted that this only implements `test()' using the builtin
`exec()'.
2020-11-27 21:32:41 +01:00
AnotherTest
3200ff5f4f LibJS+js: Rename RegExp.{content => pattern}
The spec talks about it as 'pattern', so let's use that instead.
2020-11-27 21:32:41 +01:00
Linus Groh
922d0759b0 LibJS: Fix possible OOB read during Lexer construction
The Lexer constructor calls consume() once, which initializes m_position
to be > 0 and sets m_character. consume() calls is_line_terminator(),
which wasn't accounting for this state.
2020-11-25 22:43:30 +01:00
Linus Groh
f6f0d3cbae LibJS: Support receiver in ProxyObject::get/put()
If a receiver is given, e.g. via Reflect.get/set(), forward it to the
target object's get()/put() or use it as last argument of the trap
function. The default value is the Proxy object itself.
2020-11-24 21:35:03 +01:00
Linus Groh
48369194d2 LibJS: Forward receiver value to native property getters/setters
There's no reason why only (user-defined) accessors would have set the
receiver as this value for their getters/setters, this is an oversight.
2020-11-22 19:00:19 +01:00
Linus Groh
c52739ea4b LibJS: Make call_native_property_{g,s}etter() take a NativeProperty&
Passing in a plain Value and expecting it to be a native property is
error prone, let's use a more narrow type and pass a NativeProperty
reference directly.
2020-11-22 19:00:19 +01:00
Luke
bbc0487ced LibJS: Fix build with VM_DEBUG defined 2020-11-22 11:35:13 +01:00
Linus Groh
5a307836c1 LibJS: Handle symbol PropertyName in primitive assignment error
We can't just to_string() the PropertyName, it might be a symbol.
Instead to_value() it and then use to_string_without_side_effects() as
usual.

Fixes #4062.
2020-11-12 11:40:29 +01:00
Linus Groh
e77202fe0f LibJS/Tests: Use canParseSource() for toEval()
We can now enable the "new.target is syntax error outside of function"
test :^)
2020-11-12 10:14:57 +01:00
Linus Groh
8694d804c7 LibJS: Run prettier on test-common.js 2020-11-12 10:14:57 +01:00
Linus Groh
1b0c862f3a LibJS: Throw TypeError when calling class constructor without 'new' 2020-11-12 10:14:00 +01:00
Linus Groh
b07c7f589f LibJS: Refactor ScriptFunction::call() a bit
- Get VM reference once
- Less nesting
- Better variable names
2020-11-12 10:14:00 +01:00