Commit Graph

82 Commits

Author SHA1 Message Date
Andreas Kling
52fcaae71c LibWeb: Make Document::url() return URL by value
Returning it by reference can lead to unpleasant situations if we use
this getter when the document may go away. Better to make the getter
return a copy than have to think about this everywhere.
2020-06-06 14:14:43 +02:00
Luke
9de92ae4c1 LibWeb: Fix location.reload.length
This was accidentally being set to JS::Attribute::Enumerable
instead of 0.
2020-06-06 09:55:30 +02:00
Andreas Kling
6ed11f1d1c LibWeb: Move ResourceLoader into a new Loader/ directory 2020-06-01 20:42:50 +02:00
Matthew Olsson
5ae9419a06 LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.

The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).

General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).

Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-28 17:17:13 +02:00
Matthew Olsson
dd08c992e8 LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.

Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".

This implies two things about put operations:
    - They will search the prototype chain for setters and call them, if
      necessary.
    - If no property exists with a particular key, the put operation
      should create a new property with the default attributes
      (configurable, writable, and enumerable).

In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).

Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".

Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-27 13:17:35 +02:00
Andreas Kling
82444048de LibWeb: Add cached global attribute name FlyStrings
Instead of creating extremely common FlyStrings like "id" and "class"
on demand every time they are needed, we now have AttributeNames.h,
which provides Web::HTML::AttributeNames::{id,class_}

This avoids a bunch of string allocations during selector matching.
2020-05-26 23:45:43 +02:00
Andreas Kling
8ff4ebb589 LibWeb: Add Element.getAttribute() and Element.setAttribute() :^) 2020-05-26 12:27:10 +02:00
Linus Groh
67b742bf32 LibWeb: Add document.querySelector() 2020-05-26 00:12:20 +02:00
Linus Groh
fd7cbb5389 LibWeb: Add navigator.language and navigator.languages
Hardcoded to "en-US" and ["en-US"] respectively.
2020-05-25 15:15:31 +02:00
Linus Groh
7defc521be LibWeb: Ignore non-finite args in CanvasRenderingContext2D.{scale,translate}() 2020-05-21 15:16:48 +02:00
Linus Groh
b962728c4e LibWeb: Enforce set{Interval,Timeout}() min interval of 0 2020-05-21 15:16:48 +02:00
Linus Groh
c769784406 LibWeb: Allow setInterval() with no interval 2020-05-21 15:16:48 +02:00
Linus Groh
e3e9749d88 LibWeb: Let various functions throw if not enough arguments
...instead of handing out null / undefined / empty values.
2020-05-21 15:16:48 +02:00
Linus Groh
2d503b20da LibWeb: Embrace Interpreter::{argument_count(), argument(index)} 2020-05-21 15:16:48 +02:00
Andreas Kling
63d98bbd76 LibWeb: Send User-Agent in HTTP requests
Coming soon to a Browser Market Share graph near you! :^)
2020-05-21 12:58:57 +02:00
Linus Groh
a0f3e3c50e LibWeb: Add CanvasRenderingContext2D.canvas 2020-05-21 01:24:36 +02:00
Linus Groh
abb33d425e LibWeb: Let HTMLCanvasElement.getContext() return null for unknown types
Currently we would assert. Also make it case sensitive.
2020-05-21 01:24:36 +02:00
Linus Groh
b69abb3626 LibWeb: Make window.location.reload() enumerable only 2020-05-20 08:30:22 +02:00
Linus Groh
eb0810bf1a LibWeb: Make window.location properties non-configurable
Technically the property descriptors for these should not have
"writable: true" but "get" and "set" instead - but we don't support that
yet.
2020-05-20 08:30:22 +02:00
Linus Groh
6caacfec85 LibWeb: Add leading "?" to window.location.search if not empty 2020-05-20 08:30:22 +02:00
Linus Groh
8a913f336a LibWeb: Add leading "#" to window.location.hash if not empty 2020-05-20 08:30:22 +02:00
Andreas Kling
3b11e471bd LibWeb: Allow reloading the current page with location.reload() 2020-05-18 22:05:13 +02:00
Andreas Kling
71007f6ebb LibWeb: Add location.protocol and location.host 2020-05-18 21:59:16 +02:00
Andreas Kling
efdfdbabdb LibWeb: Allow navigating to a new URL by setting window.location.href 2020-05-18 21:52:50 +02:00
Andreas Kling
1ec4db04cd LibWeb: Add a simple window.location object with some getters :^) 2020-05-18 21:42:40 +02:00
Linus Groh
476094922b LibJS: Pass Interpreter& to Value::to_number() et al.
This patch is unfortunately rather large and might make some things feel
bloated, but it is necessary to fix a few flaws in LibJS, primarily
blindly coercing values to numbers without exception checks - i.e.

interpreter.argument(0).to_i32();  // can fail!!!

Some examples where the interpreter would actually crash:

var o = { toString: () => { throw Error() } };
+o;
o - 1;
"foo".charAt(o);
"bar".repeat(o);

To fix this, we now have the following...

to_double(Interpreter&)
to_i32()
to_i32(Interpreter&)
to_size_t()
to_size_t(Interpreter&)

...and a whole lot of exception checking.

There's intentionally no to_double(), use as_double() directly instead.

This way we still can use these convenient utility functions but don't
need to check for exceptions if we are sure the value already is a
number.

Fixes #2267.
2020-05-18 09:39:55 +02:00
Linus Groh
1a1394f7a2 LibJS: Change Value::to_object(Heap& -> Interpreter&)
Passing a Heap& to it only to then call interpreter() on that is weird.
Let's just give it the Interpreter& directly, like some of the other
to_something() functions.
2020-05-18 09:39:55 +02:00
Andreas Kling
c6ddbd1f3e LibJS: Add side-effect-free version of Value::to_string()
There are now two API's on Value:

- Value::to_string(Interpreter&) -- may throw.
- Value::to_string_without_side_effects() -- will never throw.

These are some pretty big sweeping changes, so it's possible that I did
some part the wrong way. We'll work it out as we go. :^)

Fixes #2123.
2020-05-15 13:50:42 +02:00
AnotherTest
a82419469f LibWeb: Add canvas.fill
This implements only one of the two forms of this function,
ctx.fill(winding_rule).
Also tweaks the quadratic curve demo to have a nice looking filled
shape.
2020-05-06 14:50:29 +02:00
AnotherTest
0a55679de4 LibWeb: Add canvas.quadraticCurveTo()
Also adds a test, and removes debug spam ™️
2020-05-05 09:21:07 +02:00
Linus Groh
99be27b4a1 LibJS: Add "name" property to functions 2020-05-02 20:41:31 +02:00
mattco98
23ec578a01 LibJS: Implement correct attributes for (almost) all properties
Added the ability to include a u8 attributes parameter with all of the
various put methods in the Object class. They can be omitted, in which
case it defaults to "Writable | Enumerable | Configurable", just like
before this commit.

All of the attribute values for each property were gathered from
SpiderMonkey in the Firefox console. Some properties (e.g. all of the
canvas element properties) have undefined property descriptors... not
quite sure what that means. Those were left as the default specified
above.
2020-04-28 09:29:50 +02:00
Linus Groh
602a36970f LibWeb: Add XMLHttpRequest.readyState and constants 2020-04-23 11:03:17 +02:00
Emanuele Torre
d5d3e0b4ed LibWeb: Use proper length values for CanvasRenderingContext2D functions 2020-04-23 09:43:51 +02:00
Nick Tiberi
d12f27b58e LibWeb: support confirm() with no arguments 2020-04-22 11:37:33 +02:00
Andreas Kling
2d4c91df8e LibWeb: Add ImageData objects and implement 2D context putImageData()
An ImageData is a wrapper around a Bitmap wrapper around a
JS::Uint8ClampedArray.
2020-04-21 23:49:51 +02:00
Andreas Kling
fca08bd000 LibJS: Move builtin prototypes to the global object
This moves us towards being able to run JavaScript in different global
objects without allocating a separate GC heap.
2020-04-18 13:24:45 +02:00
Andreas Kling
bc1ece7f37 LibJS+LibWeb: Pass prototype to Object constructor
Everyone who constructs an Object must now pass a prototype object when
applicable. There's still a fair amount of code that passes something
fetched from the Interpreter, but this brings us closer to being able
to detach prototypes from Interpreter eventually.
2020-04-18 11:00:55 +02:00
Andreas Kling
2d7b495244 LibJS: Make Array constructor take its prototype
Let's start moving towards native JS objects taking their prototype as
a constructor argument.

This will eventually allow us to move prototypes off of Interpreter and
into GlobalObject.
2020-04-18 10:28:22 +02:00
Nick Tiberi
1f6578ee0a LibWeb: Implement JS confirm() 2020-04-17 13:41:03 +02:00
Andreas Kling
0d93e249c3 LibWeb: Add some basic path drawing functionality to the canvas element
This patch adds the following methods to CanvasRenderingContext2D:

- beginPath()
- moveTo(x, y)
- lineTo(x, y)
- closePath()
- stroke()

We also add the lineWidth property. :^)
2020-04-16 21:06:03 +02:00
Linus Groh
9eb9b46640 LibWeb: Support alert() with no arguments
No idea why someone would use that though.
2020-04-16 09:45:39 +02:00
Andreas Kling
3f698db85d LibWeb: Limit the maximum size of <canvas> bitmap buffers
We will no longer create bitmap buffers for canvases that exceed a
total area of (16384 * 16384) pixels. This matches what some other
browser do.

Thanks to @itamar8910 for finding this! :^)
2020-04-15 12:29:21 +02:00
Andreas Kling
067ea5a2e0 LibWeb: Add CanvasRenderingContext2D.drawImage(image, x, y)
This function allows you to draw a loaded <img> element into a canvas.
2020-04-14 20:38:44 +02:00
Andreas Kling
370befbf52 LibWeb: Add a JavaScript wrapper for HTMLImageElement :^) 2020-04-14 20:37:01 +02:00
Linus Groh
9fab52a390 LibJS: Remove shift, pop, push functions from Array object
This abstraction isn't really that useful, as we can access the
underlying Vector<Value> using elements() and operate on it directly.
2020-04-14 13:40:04 +02:00
Linus Groh
62d0fa5af8 LibWeb: Use specific error classes when throwing exceptions
Generally:

- interpreter.throw_exception<JS::Error>("TypeError", "Message");
+ interpreter.throw_exception<JS::TypeError>("Message");
2020-04-13 00:47:53 +02:00
Andreas Kling
4036f15728 LibWeb: Support relative URL's in XMLHttpRequest
In order to complete a relative URL, we need a Document. Fix this by
giving XMLHttpRequest a pointer to its window object. Then we can go
from the window to the document, and then we're home free. :^)
2020-04-08 21:46:43 +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
Andreas Kling
b3c62d0bc8 LibWeb: Expose the global object as "window" 2020-04-08 21:46:43 +02:00