summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
AgeCommit message (Collapse)Author
2020-05-08LibJS: Correct tiny issue with passing a String to String::formatAnotherTest
I'm not sure how this even _builds_
2020-05-08LibJS: Be a bit more explicit about sizeof(buf) / sizeof(FlatPtr)AnotherTest
This (seemingly) no-op cast communicates our intention to clang
2020-05-08LibJS: Add Array.of()Linus Groh
2020-05-08LibJS: Add Array.isArray()Linus Groh
2020-05-08LibJS: Support multiple arguments in Array constructorLinus Groh
2020-05-08LibJS: Add Value::is_integer()Linus Groh
2020-05-08LibJS: Spec-compliant equality comparisonsMatthew Olsson
The ECMAScript spec defines multiple equality operations which are used all over the spec; this patch introduces them. Of course, the two primary equality operations are AbtractEquals ('==') and StrictEquals ('==='), which have been renamed to 'abstract_eq' and 'strict_eq' in this patch. In support of the two operations mentioned above, the following have also been added: SameValue, SameValueZero, and SameValueNonNumeric. These are important to have, because they are used elsewhere in the spec aside from the two primary equality comparisons.
2020-05-07LibJS: Limit scope of 'for' loop variablesYonatan Goldschmidt
This required 2 changes: 1. In the parser, create a new variable scope, so the variable is declared in it instead of the scope in which the 'for' is found. 2. On execute, push the variable into the newly created block. Existing code created an empty block (no variables, no arguments) which allows Interpreter::enter_scope() to skip the creation of a new environment, therefore when the variable initializer is executed, it sets the variable to the outer scope. By attaching the variable to the new block, the block gets a new environment. This is only needed for 'let' / 'const' declarations, since 'var' declarations are expected to leak. Fixes: #2103
2020-05-07LibJS: Add String.rawMatthew Olsson
2020-05-07LibJS: Add raw strings to tagged template literalsMatthew Olsson
When calling a function with a tagged template, the first array that is passed in now contains a "raw" property with the raw, escaped strings.
2020-05-07LibJS: Fix shellcheck warnings in Tests/run-testsEmanuele Torre
2020-05-06LibJS: Add function call spreadingMatthew Olsson
Adds support for the following syntax: myFunction(...x, ...[1, 2, 3], ...o.foo, ...'abcd')
2020-05-06LibJS: Function.length respects default and rest parametersMatthew Olsson
"[Function.length is] the number of formal parameters. This number excludes the rest parameter and only includes parameters before the first one with a default value." - MDN
2020-05-06LibJS: Simplify a Value type check in Object::to_string()Andreas Kling
2020-05-06LibJS: Implement tagged template literals (foo`bar`)Linus Groh
To make processing tagged template literals easier, template literals will now add one empty StringLiteral before and after each template expression *if* there's no other string - e.g.: `${foo}` -> "", foo, "" `test${foo}${bar}test` -> "test", foo, "", bar, "test" This also matches the behaviour of many other parsers.
2020-05-06LibJS: Add Value::{is, as}_function()Linus Groh
2020-05-06LibJS: Fix syntax error for arrow function non-decl variable assignmentMatthew Olsson
A regression was introduced in dc9b4da where the parser would incorrectly parse the assignment of arrow functions to (non-declaration) variables. For example, consider: a = () => {} Because the parser was aware of default parameters, in try_parse_arrow_function, the equals sign would be interpreted as a default argument, leading to incorrect parsing of the overall expression. Also resulted in some funny behavior (a = () => {} => {} worked just fine!). The simple fix is to only look for default parameters if the arrow function is required to have parenthesis.
2020-05-05LibJS: Switch objects to unique shape after 100 property additionsAndreas Kling
At that point, it seems unlikely that the shape is gonna be shared with other objects, and we avoid getting stuck holding a big bag of shapes.
2020-05-05LibJS: Implement modulo assignment operator (%=)Linus Groh
2020-05-05LibJS: Implement exponentiation assignment operator (**=)Linus Groh
2020-05-05LibJS: Implement bitwise assignment operators (&=, |=, ^=)Linus Groh
2020-05-05LibJS: Add test for assignment operatorsLinus Groh
2020-05-05LibJS: Implement ConsoleClientEmanuele Torre
Now, you can optionally specify a ConsoleClient, to customise the behaviour of the LibJS Console. To customise the console, create a new ConsoleClient class that inherits from JS::ConsoleClient and override all the abstract methods. When Console::log() is called, if Console has a ConsoleClient, ConsoleClient::log() is called instead. These abstract methods are Value(void) functions: you can return a Value which will be returned by the JavaScript function which calls that method, in JavaScript.
2020-05-05LibJS: Add some helpers and use them to re-implement Console functionsEmanuele Torre
Also add const overloads for some getters. Also const-qualify Interpreter::join_arguments().
2020-05-05LibJS: run clang-format on all the filesEmanuele Torre
2020-05-05LibJS: Re-implement console functions as wrappers around Console methodsEmanuele Torre
Console methods are now Value(void) functions. JavaScript functions in the JavaScript ConsoleObject are now implemented as simple wrappers around Console methods. This will make it possible for LibJS users to easily override the default behaviour of JS console functions (even their return value!) once we add a way to override Console behaviour.
2020-05-05LibJS: Move join_args() in InterpreterEmanuele Torre
It can be useful outside of Runtime/ConsoleObject.cpp. join_args() => Interpreter::join_arguments()
2020-05-05LibJS: Remove ConsoleMessage from LibJSEmanuele Torre
We don't need to store the past messages in LibJS. We'll implement a way to let LibJS users expand the vanilla Console.
2020-05-04LibJS: Implement rest parametersLinus Groh
2020-05-04LibJS: Add indentation to sections in SwitchCase::dump()Linus Groh
This now matches the output of Program (Variables) ... (Children) ... or FunctionDeclaration 'foo' (Parameters) ... (Body) ... etc. Also don't print each consequent statement index, it doesn't add any value.
2020-05-04LibJS: Add template literalsmattco98
Adds fully functioning template literals. Because template literals contain expressions, most of the work has to be done in the Lexer rather than the Parser. And because of the complexity of template literals (expressions, nesting, escapes, etc), the Lexer needs to have some template-related state. When entering a new template literal, a TemplateLiteralStart token is emitted. When inside a literal, all text will be parsed up until a '${' or '`' (or EOF, but that's a syntax error) is seen, and then a TemplateLiteralExprStart token is emitted. At this point, the Lexer proceeds as normal, however it keeps track of the number of opening and closing curly braces it has seen in order to determine the close of the expression. Once it finds a matching curly brace for the '${', a TemplateLiteralExprEnd token is emitted and the state is updated accordingly. When the Lexer is inside of a template literal, but not an expression, and sees a '`', this must be the closing grave: a TemplateLiteralEnd token is emitted. The state required to correctly parse template strings consists of a vector (for nesting) of two pieces of information: whether or not we are in a template expression (as opposed to a template string); and the count of the number of unmatched open curly braces we have seen (only applicable if the Lexer is currently in a template expression). TODO: Add support for template literal newlines in the JS REPL (this will cause a syntax error currently): > `foo > bar` 'foo bar'
2020-05-03LibJS: Support empty statementsLinus Groh
We already skipped random semicolons in Parser::parse_program(), but now they are properly matched and parsed as empty statements - and thus recognized as a valid body of an if / else / while / ... statement.
2020-05-03LibJS: Set name of anonymous functions during assignmentLinus Groh
2020-05-03LibJS: Add function default argumentsMatthew Olsson
Adds the ability for function arguments to have default values. This works for standard functions as well as arrow functions. Default values are not printed in a <function>.toString() call, as nodes cannot print their source string representation.
2020-05-02LibJS: Fix build (GlobalObject::add_constructor not visible in LibWeb)Andreas Kling
2020-05-02LibJS: Add "name" property to functionsLinus Groh
2020-05-02LibJS: Set correct "length" of Object constructorLinus Groh
2020-05-02LibJS: Minor formatting changes in Function.cppLinus Groh
2020-05-02LibJS: Name functions created by "Function" "anonymous"Linus Groh
...as it is supposed to be.
2020-05-02LibJS: Add ConsoleMessage conceptEmanuele Torre
A ConsoleMessage is a struct cointaining: * AK::String text; represents the text of the message sent to the console. * ConsoleMessageKind kind; represents the kind of JS `console` function from which the message was sent. Now, Javascript `console` functions only send a ConsoleMessage to the Interpreter's Console instead of printing text directly to stdout. The Console then stores the recived ConsoleMessage in Console::m_messages; the Console does not print to stdout by default. You can set Console::on_new_message to a void(ConsoleMessage&); this function will get call everytime a new message is added to the Console's messages and can be used, for example, to print ConsoleMessages to stdout or to color the output based on the kind of ConsoleMessage. In this patch, I also: * Re-implement all the previously implemented functions in the JavaScript ConsoleObject, as wrappers around Console functions that add new message to the Console. * Implement console.clear() like so: - m_messages get cleared; - a new_message with kind set ConsoleMessageKind::Clear gets added to m_messages, its text is an empty AK::String; * Give credit to linusg in Console.cpp since I used his console.trace() algorithm in Console::trace(). I think that having this abstration will help us in the implementation of a browser console or a JS debugger. We could also add more MetaData to ConsoleMessage, e.g. Object IDs of the arguments passed to console functions in order to make hyperlinks, Timestamps, ecc.; which could be interesting to see. This will also help in implementing a `/bin/js` option to make, for example, return a ConsoleMessageWrapper to console functions instead of undefined. This will be useful to make tests for functions like console.count() and console.countClear(). :^)
2020-05-02LibJS: Implement ConsoleObject::count() as a Console::count() wrapperEmanuele Torre
Also implement ConsoleObject::count_clear() as a wrapper for Console::count_clear()
2020-05-02LibJS: Start implementing a Console class for the interpreterEmanuele Torre
The goal is to start factoring out core ConsoleObject functionality and to make ConsoleObject only a JS wrapper around Console.
2020-05-01LibJS: Implement (no-op) debugger statementLinus Groh
2020-05-01LibJS: Implement most of the Reflect objectLinus Groh
2020-05-01LibJS: Implement indexed access for StringObjectLinus Groh
2020-05-01LibJS: Make Array.length non-configurableLinus Groh
This was incorrect, it's only writable.
2020-05-01LibJS: Add Value::to_size_t()Linus Groh
2020-05-01LibJS: Add Object::has_property()Linus Groh
Like Object::has_own_property() but going down the prototype chain.
2020-05-01LibJS: Return a bool from Object::put* to indicate successLinus Groh
2020-05-01LibJS: Add String.prototype.lastIndexOfKesse Jones