Commit graph

1458 commits

Author SHA1 Message Date
Linus Groh
31505dde7e LibJS: Add String.prototype.pad{Start,End}() 2020-04-10 16:37:04 +02:00
Linus Groh
7636dee2cb LibJS: Remove assert function from Object.defineProperty() test 2020-04-10 16:37:04 +02:00
Andreas Kling
0fea525373 LibJS: Key shape transitions on both property name and attributes
This allows us to cache forward transitions that reconfigure existing
properties as well, leading to better shape reuse.
2020-04-10 16:33:44 +02:00
Linus Groh
0243ac5d04 LibJS: Remove mock String.prototype from tests 2020-04-10 15:51:39 +02:00
Linus Groh
32c27afdf0 LibJS: Add String.prototype.toString() 2020-04-10 15:49:40 +02:00
Andreas Kling
6f3ea75569 LibJS: Add String constructor :^) 2020-04-10 14:14:02 +02:00
Andreas Kling
cb0dfd8f72 LibJS: Use enumerator macros for boilerplate code around native types 2020-04-10 14:06:52 +02:00
Andreas Kling
070a8f2689 LibJS: Boolean, Number and String prototypes should have values too
It appears that calling .valueOf() on an objectified primitive's
prototype should return a value after all.

This matches what other engines are doing.
2020-04-10 13:09:35 +02:00
Andreas Kling
e5da1cc566 LibJS: Throw real TypeError, ReferenceError, etc objects
Instead of just throwing Error objects with a name string, we now throw
the real Error subclass types. :^)
2020-04-10 13:09:35 +02:00
Andreas Kling
58ab76269c LibJS: Add all the Error subclasses
This patch adds instance, constructor and prototype classes for:

    - EvalError
    - InternalError
    - RangeError
    - ReferenceError
    - SyntaxError
    - TypeError
    - URIError

Enumerator macros are used to reduce the amount of typing. :^)
2020-04-10 13:09:35 +02:00
Barney Wilks
4f48fcdb94 LibJS: Add tests for exception if assignment LHS is invalid 2020-04-10 11:27:10 +02:00
Barney Wilks
56474bab15 LibJS: Throw exception if LHS of assignment is of unexpected type 2020-04-10 11:27:10 +02:00
Andreas Kling
8286f8b996 LibJS: Add property configuration transitions
Object.defineProperty() can now change the attributes of a property
already on the object. Internally this becomes a shape transition with
the TransitionType::Configure. Such transitions don't expand the
property storage capacity, but rather simply keep attributes up to date
when generating a property table.
2020-04-10 00:36:06 +02:00
Andreas Kling
e6d920d87d LibJS: Add Object.defineProperty() and start caring about attributes
We now care (a little bit) about the "configurable" and "writable"
property attributes.

Property attributes are stored together with the property name in
the Shape object. Forward transitions are not attribute-savvy and will
cause poor Shape reuse in the case of multiple same-name properties
with different attributes.

Oh, and this patch also adds Object.getOwnPropertyDescriptor() :^)
2020-04-10 00:36:06 +02:00
Linus Groh
c06a6c67d5 LibJS: Add globalThis
We already have "global" as a way to access the global object in js(1)
(both REPL and script mode). This replaces it with "globalThis", which
is available in all environments, not just js.
2020-04-09 15:58:49 +02:00
Andreas Kling
cbecb23e1d LibJS: Fix BooleanPrototype build 2020-04-09 14:29:57 +02:00
Linus Groh
520c4254c9 LibJS: Make BooleanPrototype inherit from Object
BooleanPrototype should inherit from Object, not BooleanObject.
2020-04-09 13:21:24 +02:00
Andreas Kling
4ffac713b9 LibWeb: Add XMLHttpRequest object :^)
This patch adds very basic XMLHttpRequest support to LibWeb. Here's an
example that currently works:

    var callback = function() { alert(this.responseText); }
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", callback);
    xhr.open("GET", "http://serenityos.org/~kling/test/example.txt");
    xhr.send();

There are many limitations and bugs, but it's pretty dang awesome that
we have XHR. :^)
2020-04-08 21:46:43 +02:00
Linus Groh
531335f57c LibJS: Handle empty values in Array.prototype.toString() 2020-04-08 20:01:42 +02:00
Andreas Kling
2ffa054574 LibJS: Add Value::to_double() for convenience 2020-04-08 17:19:46 +02:00
Emanuele Torre
38dfd04633 LibJS: rename JS::DeclarationType => JS::DeclarationKind
Many other parsers call it with this name.

Also Type can be confusing in this context since the DeclarationType is
not the type (number, string, etc.) of the variables that are being
declared by the VariableDeclaration.
2020-04-08 14:50:14 +02:00
Andreas Kling
f07f8d5a44 LibJS: Add "constructor" property to constructor prototypes 2020-04-08 11:08:07 +02:00
Linus Groh
5ecc7e6a1c LibJS: Add Number.isSafeInteger() 2020-04-07 21:28:43 +02:00
Linus Groh
b7032b4972 LibJS: Add Number constants 2020-04-07 21:28:43 +02:00
Linus Groh
3e677fd03d LibJS: Break loop on EOF when parsing object expression 2020-04-07 21:26:38 +02:00
Linus Groh
f631f6a2e6 LibJS: Add Number() 2020-04-07 17:25:50 +02:00
Linus Groh
40ac94995d LibJS: Reformat BooleanConstructor.{cpp,h} 2020-04-07 17:25:50 +02:00
Andreas Kling
19cbfaee54 LibJS: Add SequenceExpression AST node (comma operator)
This patch only adds the AST node, the parser doesn't create them yet.
2020-04-07 15:56:26 +02:00
DexesTTP
e586dc285a LibJS: Allow parsing numeric and string literals in object expressions
Also updated the object-basic.js test to include this change
2020-04-07 09:05:16 +02:00
Linus Groh
22f20cd51d LibJS: Add String.prototype.toUpperCase() 2020-04-07 08:50:35 +02:00
Jack Karamanian
edae926cb0 LibJS: Add Boolean constructor object 2020-04-07 08:41:25 +02:00
Jack Karamanian
57bd194e5a LibJS: Return false for NaN numbers in Value::to_boolean() 2020-04-07 08:41:25 +02:00
DexesTTP
4a9485f830 LibJS: Fix impossible member access for negative integers
The PropertyName class able to match a number or an array can only
accept positive numerical values. However, the computed_property_name
method sometimes returned negative values.

This commit also adds a basic object access test case.
2020-04-06 21:45:20 +02:00
Andreas Kling
cb18b2c74d LibJS: Add String.prototype.toLowerCase() 2020-04-06 20:46:08 +02:00
Andreas Kling
4fe14aab3b LibJS: Inline JS::Value()
I had this out of line for debugging reasons. Put it back inline.
2020-04-06 20:30:36 +02:00
Andreas Kling
bdffc9e7fb LibJS: Support array holes, encoded as empty JS::Value
This patch adds a new kind of JS::Value, the empty value.
It's what you get when you do JSValue() (or most commonly, {} in C++.)

An empty Value signifies the absence of a value, and should never be
visible to JavaScript itself. As of right now, it's used for array
holes and as a return value when an exception has been thrown and we
just want to unwind.

This patch is a bit of a mess as I had to fix a whole bunch of code
that was relying on JSValue() being undefined, etc.
2020-04-06 20:27:44 +02:00
Andreas Kling
5495f06af5 LibJS: Give argument vectors an inline capacity of 8
This avoids one malloc/free pair for every function call if there are
8 arguments or fewer.
2020-04-06 19:22:12 +02:00
Andreas Kling
be019f28ca LibJS: Add a PropertyName class that represents a string or a number
Now that we have two separate storages for Object properties depending
on what kind of index they have, it's nice to have an abstraction that
still allows us to say "here's a property name".

We use PropertyName to always choose the optimal storage path directly
while interpreting the AST. :^)
2020-04-06 18:09:26 +02:00
Andreas Kling
90ba0145f6 LibJS: Add a number-indexed property storage to all Objects
Objects can have both named and indexed properties. Previously we kept
all property names as strings. This patch separates named and indexed
properties and splits them between Object::m_storage and m_elements.

This allows us to do much faster array-style access using numeric
indices. It also makes the Array class much less special, since all
Objects now have number-indexed storage. :^)
2020-04-06 18:09:26 +02:00
Andreas Kling
9aaf19f4de LibJS: Do a garbage collection every N allocations (N=10'000)
To prevent the heap from growing infinitely large, we now do a full GC
every 10'000 allocations. :^)
2020-04-06 15:54:46 +02:00
Emanuele Torre
1d6c8724b9 LibJS: Fix some tests for Math.min()
In some of the tests in Math.min.js, we were testing Math.max() instead
of Math.min()
2020-04-06 15:14:34 +02:00
Emanuele Torre
ba67fc68b8
LibJS: Rename variable "max" to "min" in MathObject::min() (#1665) 2020-04-06 14:08:56 +02:00
Andreas Kling
67f7763ab9 LibJS: Object needs to protect values in its storage
Otherwise the garbage collector will eat them way too soon! This made
it impossible to use "js -g" without crashing.
2020-04-06 13:35:20 +02:00
Andreas Kling
e323246517 Meta: Add missing copyright headers 2020-04-06 11:09:01 +02:00
Linus Groh
f5dacfbb5b LibJS: Add Math.{cos,sin,tan}() 2020-04-06 10:58:16 +02:00
Linus Groh
04a36b247b LibJS: Simplify MathObject functions 2020-04-06 10:58:16 +02:00
Andreas Kling
6e7713a5f4 LibJS: Remove unnecessary malloc+free in AssignmentExpression::execute
We were creating a temporary AK::Function for no good reason, and this
was dominating profiles. Reorganize the code so it's not necessary.
2020-04-06 08:26:26 +02:00
Andreas Kling
003741db1c LibJS: Add Math.min() 2020-04-06 08:26:26 +02:00
Linus Groh
9e7dcaa106 LibJS: Return -Infinity in Math.max() with no argument 2020-04-05 19:30:47 +02:00
Andreas Kling
8bfee015bc LibJS: Make Object::to_string() call the "toString" property if present 2020-04-05 18:19:56 +02:00