Commit graph

4954 commits

Author SHA1 Message Date
Linus Groh
0bb66890c8 LibJS: Fix Object::delete_property() with numeric string property
- We have to check if the property name is a string before calling
  as_string() on it
- We can't as_number() the same property name but have to use the parsed
  index number

Fixes #3950.
2020-11-05 19:15:00 +01:00
Linus Groh
8d96f428ef LibJS: ASSERT(property_name.is_valid()) in more Object methods 2020-11-05 19:15:00 +01:00
Linus Groh
2cf8649d0e LibJS: Fix ProxyObject get/set with symbol property name
We can't assume that property names can be converted to strings anymore,
as we have symbols. Use name.to_value() instead.

This makes something like this possible:

    new Proxy(Object, { get(t, p) { return t[p] }  })[Symbol.hasInstance]
2020-11-04 23:06:44 +01:00
Linus Groh
44e38b8457 LibJS: Replace a bunch of vm() calls in ProxyObject with reference
This was probably a result of search & replace, it's quite ridiculous in
some places. Let use the existing pattern of getting a reference to the
VM once at each function start consistently.
2020-11-04 23:06:44 +01:00
Linus Groh
2645dfafcf LibJS: Implement Object(value) constructor
Not sure why we didn't have this yet, it's super simple :^)
2020-11-04 23:06:44 +01:00
Andreas Kling
70eaadc1cd LibGfx: Load the system default fonts by name
Instead of loading them by absolute path, that is.
2020-11-04 21:21:52 +01:00
Linus Groh
0603402c80 LibJS: Handle circular references in Array.prototype.join()
This fixes Array.prototype.{join,toString}() crashing with arrays
containing themselves, i.e. circular references.

The spec is suspiciously silent about this, and indeed engine262, a
"100% spec compliant" ECMA-262 implementation, can't handle these cases.
I had a look at some major engines instead and they all seem to keep
track or check for circular references and return an empty string for
already seen objects.

- SpiderMonkey: "AutoCycleDetector detector(cx, obj)"
- V8: "CycleProtectedArrayJoin<JSArray>(...)"
- JavaScriptCore: "StringRecursionChecker checker(globalObject, thisObject)"
- ChakraCore: "scriptContext->CheckObject(thisArg)"

To keep things simple & consistent this uses the same pattern as
JSONObject, MarkupGenerator and js: simply putting each seen object in a
HashTable<Object*>.

Fixes #3929.
2020-11-04 19:35:43 +01:00
Linus Groh
e5845ba3a0 LibJS: Use "," separator in Array.prototype.join() if first arg is undefined
This is how the spec describes it, not "if the first arg is missing".
Also swap length & separator steps to match spec.
2020-11-04 19:35:43 +01:00
Linus Groh
fb89c324c5 LibJS: Implement spec-compliant OrdinaryToPrimitive
This renames Object::to_primitive() to Object::ordinary_to_primitive()
for two reasons:

- No confusion with Value::to_primitive()
- To match the spec's name

Also change existing uses of Object::to_primitive() to
Value::to_primitive() when the spec uses the latter (which will still
call Object::ordinary_to_primitive()). Object::to_string() has been
removed as it's not needed anymore (and nothing the spec uses).

This makes it possible to overwrite an object's toString and valueOf and
have them provide results for anything that uses to_primitive() - e.g.:

    const o = { toString: undefined, valueOf: () => 42 };
    Number(o) // 42, previously NaN
    ["foo", o].toString(); // "foo,42", previously "foo,[object Object]"
    ++o // 43, previously NaN

etc.
2020-11-04 19:33:49 +01:00
Linus Groh
e163db248d LibJS: Implement RegExp.prototype.toString() as standalone function
This should not just inherit Object.prototype.toString() (and override
Object::to_string()) but be its own function, i.e.
'RegExp.prototype.toString !== Object.prototype.toString'.
2020-11-04 19:33:49 +01:00
Linus Groh
41837f548d LibJS: Don't create "valid" PropertyName from null string
When value.to_string() throws an exception it returns a null string in
which case we must not construct a valid PropertyName.

Also ASSERT in PropertyName(String) and PropertyName(FlyString) to
prevent this from happening in the future.

Fixes #3941.
2020-11-04 15:31:39 +01:00
Linus Groh
8afe1c8165 LibJS: Fix incorrect exception checks in ProxyObject
We must *never* call some method that expects a non-empty value on the
result of a function call without checking for exceptions first. It
won't work reliably.

Fixes #3939.
2020-11-04 14:21:06 +01:00
Andreas Kling
cfce6b37be LibVT: Copying from terminal scrollback resulted in wrong text
This regressed when turning the terminal history into a circular buffer
as only the non-const version of Terminal::line() was updated with
the new indexing logic.
2020-11-03 20:08:05 +01:00
AnotherTest
060ddd2a7a AK: Really disallow making OwnPtrs from refcounted types
This looks at three things:
- if the type has a typedef `AllowOwnPtr', respect that
- if not, disallow construction if both of `ref()' and `unref()' are
  present.
Note that in the second case, if a type only defines `ref()' or only
defines `unref()', an OwnPtr can be created, as a RefPtr of that type
would be ill-formed.

Also marks a `Performance' to explicitly allow OwnPtrs.
2020-11-03 19:14:34 +01:00
Linus Groh
565a26808d LibJS: Fix crashing exception in Value::ordinary_has_instance()
Two issues:

- throw_exception() with ErrorType::InstanceOfOperatorBadPrototype would
  receive rhs_prototype.to_string_without_side_effects(), which would
  ASSERT_NOT_REACHED() as to_string_without_side_effects() must not be
  called on an empty value. It should (and now does) receive the RHS
  value instead as the message is "'prototype' property of {} is not an
  object".
- Value::instance_of() was missing an exception check after calling
  has_instance_method, to_boolean() on an empty value result would crash
  as well.

Fixes #3930.
2020-11-03 19:14:13 +01:00
AnotherTest
6e9c6acc87 LibGUI: Implement shift-select
shift-selecting simply toggles all indices in the range between the
cursor and the index that is being 'selected'.
2020-11-03 16:47:56 +01:00
Linus Groh
39a1c9d827 LibJS: Implement 'new.target'
This adds a new MetaProperty AST node which will be used for
'new.target' and 'import.meta' meta properties. The parser now
distinguishes between "in function context" and "in arrow function
context" (which is required for this).
When encountering TokenType::New we will attempt to parse it as meta
property and resort to regular new expression parsing if that fails,
much like the parsing of labelled statements.
2020-11-02 22:40:59 +01:00
Linus Groh
e07a39c816 LibJS: Replace 'size_t line, size_t column' with 'Optional<Position>'
This is a bit nicer for two reasons:

- The absence of line number/column information isn't based on 'values
  are zero' anymore but on Optional's value
- When reporting syntax errors with position information other than the
  current token's position we had to store line and column ourselves,
  like this:

      auto foo_start_line = m_parser_state.m_current_token.line_number();
      auto foo_start_column = m_parser_state.m_current_token.line_column();
      ...
      syntax_error("...", foo_start_line, foo_start_column);

  Which now becomes:

      auto foo_start= position();
      ...
      syntax_error("...", foo_start);

  This makes it easier to report correct positions for syntax errors
  that only emerge a few tokens later :^)
2020-11-02 22:40:59 +01:00
Joseph
9efcb5d290
LibGUI: Return early if drag_data_type is null (#3919)
If the user tries to drag an item with a null drag data type from an
AbstractView, return from the mouse event early.
2020-11-02 22:26:38 +01:00
Brendan Coles
fb5ea8a212 WindowServer+LibGfx: Add Gfx::StandardCursor::Hidden cursor 2020-11-02 20:58:07 +01:00
Linus Groh
9e80c67608 LibJS: Fix "use strict" directive false positives
By having the "is this a use strict directive?" logic in
parse_string_literal() we would apply it to *any* string literal, which
is incorrect and would lead to false positives - e.g.:

    "use strict" + 1
    `"use strict"`
    "\123"; ({"use strict": ...})

Relevant part from the spec which is now implemented properly:

[...] and where each ExpressionStatement in the sequence consists
entirely of a StringLiteral token [...]

I also got rid of UseStrictDirectiveState which is not needed anymore.

Fixes #3903.
2020-11-02 13:13:54 +01:00
Andreas Kling
ee21a724c7 LibGfx: Add some more assertions to Gfx::Bitmap
Let's also be paranoid about get_pixel() since we started worrying
about set_pixel(). :^)
2020-11-02 11:01:00 +01:00
Peter Nelson
23c4f1a3d4 LibGfx: assert Bitmap::set_pixel does not write out of bounds 2020-11-01 20:13:25 +01:00
Peter Nelson
5567408bab LibGfx: add bounds checking before set_pixel call in GIF decoder
This fixes a crash when a GIF frame extends beyond the limits of the
logical screen, causing writes past the end of the frame buffer
2020-11-01 20:13:25 +01:00
Andreas Kling
a28f29c82c Kernel+LibC: Don't allow a directory to become a subdirectory of itself
If you try to do this (e.g "mv directory directory"), sys$rename() will
now fail with EDIRINTOSELF.

Dr. POSIX says we should return EINVAL for this, but a custom error
code allows us to print a much more helpful error message when this
problem occurs. :^)
2020-11-01 19:21:19 +01:00
Andreas Kling
a7f4f6afc3 LibGUI: Allow dragging a previously unselected item with a single click
Views would previously require that an item be selected before it could
be dragged. This patch makes us consider initiating a drag immediately
after the view has been selected, without requiring a mouseup event in
between.
2020-11-01 15:42:09 +01:00
Andreas Kling
844c5709ac LibGUI: Views should ignore double clicks that don't hit any index 2020-11-01 15:31:18 +01:00
Andreas Kling
e499b0f161 LibGUI: Invalidate view cursor on model update
This is sad (since it would be nice to preserve the cursor+selection)
but until we implement persistent model indexes, this at least prevents
us from keeping a stale cursor index.
2020-11-01 10:39:37 +01:00
Brendan Coles
3482b9b937 LibWeb: Enforce Same-Origin Policy (SOP) for XMLHttpRequest requests
`DOM::XMLHttpRequest` now checks if the requested URL has the same
`Origin` as the requesting `Document`. If the requested URL is in
violation of SOP the request is rejected and an "error" `DOM::Event`
is dispatched.
2020-11-01 10:23:08 +01:00
Linus Groh
0aeef47abd LibMarkdown: Use JS::MarkupGenerator for "js" code blocks :^) 2020-10-31 20:52:54 +01:00
Linus Groh
10c19d5207 LibMarkdown: Replace inline styles in HTML with CSS in <head> 2020-10-31 20:52:54 +01:00
Linus Groh
e5ec4d35ea LibWeb: Don't use 'font-weight: lighter' for Csilla in Default.css
Base/res/fonts/CsillaThin7x10.font was renamed to
Base/res/fonts/CsillaRegular10.font in 5abc03d, breaking the default
styles of <code> and <pre>.
The font lookup should still find a font variant when a non-existent
weight is specified, but that's another issue for another day.
2020-10-31 20:52:54 +01:00
Linus Groh
d2a2d19a86 LibJS: Handle multi-line source code in MarkupGenerator
The previous approach (keeping track of the current source position
manually) was only working for single line sources (which is fair
considering this was developed for Browser's JS console).
The new approach is much simpler: append token trivia (all whitespace
and comments since the last token), then append styled token value.
2020-10-31 20:52:54 +01:00
Linus Groh
a598a2c19d LibJS: Function declarations in if statement clauses
https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses

B.3.4 FunctionDeclarations in IfStatement Statement Clauses

The following augments the IfStatement production in 13.6:

    IfStatement[Yield, Await, Return] :
        if ( Expression[+In, ?Yield, ?Await] ) FunctionDeclaration[?Yield, ?Await, ~Default] else Statement[?Yield, ?Await, ?Return]
        if ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] else FunctionDeclaration[?Yield, ?Await, ~Default]
        if ( Expression[+In, ?Yield, ?Await] ) FunctionDeclaration[?Yield, ?Await, ~Default] else FunctionDeclaration[?Yield, ?Await, ~Default]
        if ( Expression[+In, ?Yield, ?Await] ) FunctionDeclaration[?Yield, ?Await, ~Default]

This production only applies when parsing non-strict code. Code matching
this production is processed as if each matching occurrence of
FunctionDeclaration[?Yield, ?Await, ~Default] was the sole
StatementListItem of a BlockStatement occupying that position in the
source code. The semantics of such a synthetic BlockStatement includes
the web legacy compatibility semantics specified in B.3.3.
2020-10-31 15:25:12 +01:00
Andreas Kling
982e066100 LibGfx: Move FontDatabase from LibGUI to LibGfx
Not sure why I put this into LibGUI in the first place.
2020-10-31 13:56:21 +01:00
Andreas Kling
66a8e151fb LibGUI: Remove redundant scroll_into_view() calls in TableView
The calls to set_cursor() already take care of scrolling the new cursor
into view if needed.
2020-10-30 23:52:59 +01:00
Linus Groh
563d3c8055 LibJS: Require initializer for 'const' variable declaration 2020-10-30 23:43:38 +01:00
AnotherTest
812e3ecedd LibProtocol+LibGemini+LibHTTP: Provide root certificates to LibTLS
Now we (almost) verify all the sites we browse.
Certificate verification failures should not be unexpected, as the
existing CA certificates are likely not complete.
2020-10-30 23:42:03 +01:00
AnotherTest
37c089fb7b LibTLS: (Almost) verify certificate chain against root CA certificates
Also adds a very primitive systemwide ca_certs.ini file.
2020-10-30 23:42:03 +01:00
AnotherTest
34f8d55100 LibCrypto: Remove spammy ModPow debug log 2020-10-30 23:42:03 +01:00
AnotherTest
1746e6f9ca LibTLS: Also read out the Organisational Unit from the certificate
This needs to be read out if we want to actually verify the cert chain.
2020-10-30 23:42:03 +01:00
AnotherTest
a2186fd64a LibTLS: Move out Certificate to its own header file 2020-10-30 23:42:03 +01:00
AnotherTest
a461526b07 LibHTTP+ProtocolServer+LibGemini: Remove Request::schedule()
This API is only used for HttpRequest, but replicated in GeminiRequest
without an actual user, so remove it and construct the job like the rest
of the protocols.
2020-10-30 23:42:03 +01:00
Andreas Kling
b11b4b29e9 LibGUI: Add Widget::has_focus_within()
This returns true if the widget has focus, or if one of its descendant
widgets does. Use this in StackWidget and TabWidget.

This also fixes HackStudio crashing on startup in StackWidget, due to
running before the window has a focused widget.
2020-10-30 23:40:23 +01:00
Andreas Kling
dee639f19b LibGUI: Improve TabWidget+StackWidget focus behavior further
When setting a new active widget, transfer focus if it's anywhere in
the old active widget's tree, not just at the immediate child.
2020-10-30 19:17:09 +01:00
Andreas Kling
b0cdb6b074 LibGUI: Deduplicate widgets with proxied focus in focus chain
When computing the chain of focusable widgets in a window, only include
each widget once (to avoid loops) and resolve focus proxies immediately
instead of lazily. This prevents the focus from getting stuck when
cycling backwards and hitting a proxy that points forward.
2020-10-30 19:17:09 +01:00
Uma Sankar Yedida
9ccae7a908 WindowServer+LibGfx: Added Crosshair cursor 2020-10-30 19:10:15 +01:00
Andreas Kling
ddad7575a9 LibGUI: Improve automatic focus guessing somewhat
When opening a new window, we'll now try to find a suitable widget for
initial focus by picking the first available mouse-focusable one.

Whenever you press the tab key in a window with no focused widget,
we'll attempt to find a keyboard-focusable widget and give it focus.

This should make all applications keyboard-interactive immediately
without having to manually place focus with the mouse.
2020-10-30 17:03:29 +01:00
Andreas Kling
28df2ede06 LibGUI: Use ToolBarButton helper class inside ToolBar 2020-10-30 17:03:29 +01:00
Andreas Kling
9baa649389 LibGUI: Fix null parent deref in AbstractButton::set_checked()
If a button is orphaned, there are no siblings anyway, so there's no
need to try to update them.
2020-10-30 17:03:29 +01:00