2020-03-07 18:42:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-12-04 22:58:10 +00:00
|
|
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
2020-03-07 18:42:11 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-12 09:51:41 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-11-09 19:10:59 +00:00
|
|
|
#include <AK/Format.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2020-03-25 22:10:29 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-12 09:51:41 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
2020-10-25 23:36:06 +00:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <LibJS/AST.h>
|
2020-05-04 14:09:05 +00:00
|
|
|
#include <LibJS/Console.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-11 18:27:43 +00:00
|
|
|
#include <LibJS/Parser.h>
|
2020-03-26 11:26:11 +00:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-12-04 22:58:10 +00:00
|
|
|
#include <LibJS/Runtime/ArrayBuffer.h>
|
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
2020-03-31 17:43:45 +00:00
|
|
|
#include <LibJS/Runtime/Date.h>
|
2020-04-02 07:54:15 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-03-26 13:54:43 +00:00
|
|
|
#include <LibJS/Runtime/Function.h>
|
2020-04-01 16:53:28 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-12-04 22:58:10 +00:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
|
|
#include <LibJS/Runtime/NumberObject.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
2020-12-04 22:58:10 +00:00
|
|
|
#include <LibJS/Runtime/ProxyObject.h>
|
2020-06-03 23:05:49 +00:00
|
|
|
#include <LibJS/Runtime/RegExpObject.h>
|
2020-12-04 22:58:10 +00:00
|
|
|
#include <LibJS/Runtime/ScriptFunction.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-12-04 22:58:10 +00:00
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
|
|
|
#include <LibJS/Runtime/TypedArray.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-31 11:34:57 +00:00
|
|
|
#include <LibLine/Editor.h>
|
2020-04-11 09:09:10 +00:00
|
|
|
#include <signal.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-09-23 19:29:57 +00:00
|
|
|
RefPtr<JS::VM> vm;
|
2020-04-02 15:52:40 +00:00
|
|
|
Vector<String> repl_statements;
|
|
|
|
|
2020-04-01 19:20:32 +00:00
|
|
|
class ReplObject : public JS::GlobalObject {
|
|
|
|
public:
|
|
|
|
ReplObject();
|
2020-04-18 11:18:06 +00:00
|
|
|
virtual void initialize() override;
|
2020-04-01 19:20:32 +00:00
|
|
|
virtual ~ReplObject() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual const char* class_name() const override { return "ReplObject"; }
|
2020-06-20 11:55:34 +00:00
|
|
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(exit_interpreter);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(repl_help);
|
2020-07-06 22:17:39 +00:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(load_file);
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(save_to_file);
|
2020-04-01 19:20:32 +00:00
|
|
|
};
|
|
|
|
|
2020-04-26 14:19:30 +00:00
|
|
|
static bool s_dump_ast = false;
|
|
|
|
static bool s_print_last_result = false;
|
2020-05-26 10:34:39 +00:00
|
|
|
static RefPtr<Line::Editor> s_editor;
|
2020-10-25 23:36:06 +00:00
|
|
|
static String s_history_path = String::formatted("{}/.js-history", Core::StandardPaths::home_directory());
|
2020-04-26 14:19:30 +00:00
|
|
|
static int s_repl_line_level = 0;
|
2020-05-25 12:27:07 +00:00
|
|
|
static bool s_fail_repl = false;
|
2020-04-09 03:30:45 +00:00
|
|
|
|
|
|
|
static String prompt_for_level(int level)
|
|
|
|
{
|
|
|
|
static StringBuilder prompt_builder;
|
|
|
|
prompt_builder.clear();
|
|
|
|
prompt_builder.append("> ");
|
|
|
|
|
|
|
|
for (auto i = 0; i < level; ++i)
|
|
|
|
prompt_builder.append(" ");
|
|
|
|
|
|
|
|
return prompt_builder.build();
|
|
|
|
}
|
2020-03-25 22:10:29 +00:00
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static String read_next_piece()
|
2020-03-25 22:10:29 +00:00
|
|
|
{
|
|
|
|
StringBuilder piece;
|
|
|
|
|
2020-05-30 17:12:11 +00:00
|
|
|
auto line_level_delta_for_next_line { 0 };
|
|
|
|
|
2020-03-25 22:10:29 +00:00
|
|
|
do {
|
2020-05-25 12:27:07 +00:00
|
|
|
auto line_result = s_editor->get_line(prompt_for_level(s_repl_line_level));
|
|
|
|
|
2020-05-30 17:12:11 +00:00
|
|
|
line_level_delta_for_next_line = 0;
|
|
|
|
|
2020-05-25 12:27:07 +00:00
|
|
|
if (line_result.is_error()) {
|
|
|
|
s_fail_repl = true;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& line = line_result.value();
|
2020-04-26 14:19:30 +00:00
|
|
|
s_editor->add_to_history(line);
|
2020-03-25 22:10:29 +00:00
|
|
|
|
|
|
|
piece.append(line);
|
|
|
|
auto lexer = JS::Lexer(line);
|
|
|
|
|
2020-05-30 17:12:11 +00:00
|
|
|
enum {
|
|
|
|
NotInLabelOrObjectKey,
|
|
|
|
InLabelOrObjectKeyIdentifier,
|
|
|
|
InLabelOrObjectKey
|
|
|
|
} label_state { NotInLabelOrObjectKey };
|
|
|
|
|
2020-03-25 22:10:29 +00:00
|
|
|
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
|
|
|
switch (token.type()) {
|
|
|
|
case JS::TokenType::BracketOpen:
|
|
|
|
case JS::TokenType::CurlyOpen:
|
|
|
|
case JS::TokenType::ParenOpen:
|
2020-05-30 17:12:11 +00:00
|
|
|
label_state = NotInLabelOrObjectKey;
|
2020-04-26 14:19:30 +00:00
|
|
|
s_repl_line_level++;
|
2020-03-25 22:10:29 +00:00
|
|
|
break;
|
|
|
|
case JS::TokenType::BracketClose:
|
|
|
|
case JS::TokenType::CurlyClose:
|
|
|
|
case JS::TokenType::ParenClose:
|
2020-05-30 17:12:11 +00:00
|
|
|
label_state = NotInLabelOrObjectKey;
|
2020-04-26 14:19:30 +00:00
|
|
|
s_repl_line_level--;
|
2020-03-25 22:10:29 +00:00
|
|
|
break;
|
2020-05-30 17:12:11 +00:00
|
|
|
|
|
|
|
case JS::TokenType::Identifier:
|
|
|
|
case JS::TokenType::StringLiteral:
|
|
|
|
if (label_state == NotInLabelOrObjectKey)
|
|
|
|
label_state = InLabelOrObjectKeyIdentifier;
|
|
|
|
else
|
|
|
|
label_state = NotInLabelOrObjectKey;
|
|
|
|
break;
|
|
|
|
case JS::TokenType::Colon:
|
|
|
|
if (label_state == InLabelOrObjectKeyIdentifier)
|
|
|
|
label_state = InLabelOrObjectKey;
|
|
|
|
else
|
|
|
|
label_state = NotInLabelOrObjectKey;
|
|
|
|
break;
|
2020-03-25 22:10:29 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 17:12:11 +00:00
|
|
|
|
|
|
|
if (label_state == InLabelOrObjectKey) {
|
|
|
|
// If there's a label or object literal key at the end of this line,
|
|
|
|
// prompt for more lines but do not change the line level.
|
|
|
|
line_level_delta_for_next_line += 1;
|
|
|
|
}
|
|
|
|
} while (s_repl_line_level + line_level_delta_for_next_line > 0);
|
2020-03-25 22:10:29 +00:00
|
|
|
|
|
|
|
return piece.to_string();
|
|
|
|
}
|
|
|
|
|
2020-03-26 11:26:11 +00:00
|
|
|
static void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects);
|
|
|
|
|
2020-12-04 22:58:10 +00:00
|
|
|
static void print_type(const FlyString& name)
|
|
|
|
{
|
|
|
|
out("[\033[36;1m{}\033[0m]", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_separator(bool& first)
|
|
|
|
{
|
|
|
|
out(first ? " " : ", ");
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
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-27 18:35:09 +00:00
|
|
|
static void print_array(JS::Array& array, HashTable<JS::Object*>& seen_objects)
|
2020-03-26 11:26:11 +00:00
|
|
|
{
|
2020-12-04 22:58:10 +00:00
|
|
|
out("[");
|
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-27 18:35:09 +00:00
|
|
|
bool first = true;
|
|
|
|
for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) {
|
2020-12-04 22:58:10 +00:00
|
|
|
print_separator(first);
|
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-27 18:35:09 +00:00
|
|
|
auto value = it.value_and_attributes(&array).value;
|
|
|
|
// The V8 repl doesn't throw an exception here, and instead just
|
|
|
|
// prints 'undefined'. We may choose to replicate that behavior in
|
|
|
|
// the future, but for now lets just catch the error
|
2020-09-23 19:29:57 +00:00
|
|
|
if (vm->exception())
|
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-27 18:35:09 +00:00
|
|
|
return;
|
|
|
|
print_value(value, seen_objects);
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
2020-12-04 22:58:10 +00:00
|
|
|
if (!first)
|
|
|
|
out(" ");
|
|
|
|
out("]");
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
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-27 18:35:09 +00:00
|
|
|
static void print_object(JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
2020-03-26 11:26:11 +00:00
|
|
|
{
|
2020-12-04 22:58:10 +00:00
|
|
|
out("{{");
|
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-27 18:35:09 +00:00
|
|
|
bool first = true;
|
|
|
|
for (auto& entry : object.indexed_properties()) {
|
2020-12-04 22:58:10 +00:00
|
|
|
print_separator(first);
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\"\033[33;1m{}\033[0m\": ", entry.index());
|
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-27 18:35:09 +00:00
|
|
|
auto value = entry.value_and_attributes(&object).value;
|
|
|
|
// The V8 repl doesn't throw an exception here, and instead just
|
|
|
|
// prints 'undefined'. We may choose to replicate that behavior in
|
|
|
|
// the future, but for now lets just catch the error
|
2020-09-23 19:29:57 +00:00
|
|
|
if (vm->exception())
|
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-27 18:35:09 +00:00
|
|
|
return;
|
|
|
|
print_value(value, seen_objects);
|
2020-04-06 14:53:02 +00:00
|
|
|
}
|
2020-04-29 02:19:31 +00:00
|
|
|
for (auto& it : object.shape().property_table_ordered()) {
|
2020-12-04 22:58:10 +00:00
|
|
|
print_separator(first);
|
2020-07-08 04:38:46 +00:00
|
|
|
if (it.key.is_string()) {
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\"\033[33;1m{}\033[0m\": ", it.key.to_display_string());
|
2020-07-08 04:38:46 +00:00
|
|
|
} else {
|
2020-12-04 22:58:10 +00:00
|
|
|
out("[\033[33;1m{}\033[0m]: ", it.key.to_display_string());
|
2020-07-08 04:38:46 +00:00
|
|
|
}
|
2020-04-02 17:32:21 +00:00
|
|
|
print_value(object.get_direct(it.value.offset), seen_objects);
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
2020-12-04 22:58:10 +00:00
|
|
|
if (!first)
|
|
|
|
out(" ");
|
|
|
|
out("}}");
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 22:58:10 +00:00
|
|
|
static void print_function(const JS::Object& object, HashTable<JS::Object*>&)
|
2020-03-26 13:54:43 +00:00
|
|
|
{
|
2020-12-04 22:58:10 +00:00
|
|
|
print_type(object.class_name());
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::ScriptFunction>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
out(" {}", static_cast<const JS::ScriptFunction&>(object).name());
|
2021-01-01 16:46:39 +00:00
|
|
|
else if (is<JS::NativeFunction>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
out(" {}", static_cast<const JS::NativeFunction&>(object).name());
|
2020-03-26 13:54:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 20:18:47 +00:00
|
|
|
static void print_date(const JS::Object& date, HashTable<JS::Object*>&)
|
2020-03-31 17:43:45 +00:00
|
|
|
{
|
2020-12-04 22:58:10 +00:00
|
|
|
print_type("Date");
|
|
|
|
out(" \033[34;1m{}\033[0m", static_cast<const JS::Date&>(date).string());
|
2020-03-31 17:43:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 07:54:15 +00:00
|
|
|
static void print_error(const JS::Object& object, HashTable<JS::Object*>&)
|
|
|
|
{
|
|
|
|
auto& error = static_cast<const JS::Error&>(object);
|
2020-12-04 22:58:10 +00:00
|
|
|
print_type(error.name());
|
2020-04-02 13:18:19 +00:00
|
|
|
if (!error.message().is_empty())
|
2020-12-04 22:58:10 +00:00
|
|
|
out(" \033[31;1m{}\033[0m", error.message());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_regexp_object(const JS::Object& object, HashTable<JS::Object*>&)
|
|
|
|
{
|
|
|
|
auto& regexp_object = static_cast<const JS::RegExpObject&>(object);
|
|
|
|
// Use RegExp.prototype.source rather than RegExpObject::pattern() so we get proper escaping
|
|
|
|
auto source = regexp_object.get("source").to_primitive_string(object.global_object())->string();
|
|
|
|
print_type("RegExp");
|
|
|
|
out(" \033[34;1m/{}/{}\033[0m", source, regexp_object.flags());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_proxy_object(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
|
|
|
{
|
|
|
|
auto& proxy_object = static_cast<const JS::ProxyObject&>(object);
|
|
|
|
print_type("Proxy");
|
|
|
|
out("\n target: ");
|
|
|
|
print_value(&proxy_object.target(), seen_objects);
|
|
|
|
out("\n handler: ");
|
|
|
|
print_value(&proxy_object.handler(), seen_objects);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_array_buffer(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
|
|
|
{
|
|
|
|
auto& array_buffer = static_cast<const JS::ArrayBuffer&>(object);
|
|
|
|
auto& buffer = array_buffer.buffer();
|
|
|
|
auto byte_length = array_buffer.byte_length();
|
|
|
|
print_type("ArrayBuffer");
|
|
|
|
out("\n byteLength: ");
|
|
|
|
print_value(JS::Value((double)byte_length), seen_objects);
|
|
|
|
outln();
|
|
|
|
for (size_t i = 0; i < byte_length; ++i) {
|
|
|
|
out("{:02x}", buffer[i]);
|
|
|
|
if (i + 1 < byte_length) {
|
|
|
|
if ((i + 1) % 32 == 0)
|
|
|
|
outln();
|
|
|
|
else if ((i + 1) % 16 == 0)
|
|
|
|
out(" ");
|
|
|
|
else
|
|
|
|
out(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_typed_array(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
|
|
|
{
|
|
|
|
auto& typed_array_base = static_cast<const JS::TypedArrayBase&>(object);
|
|
|
|
auto length = typed_array_base.array_length();
|
|
|
|
print_type(object.class_name());
|
|
|
|
out("\n length: ");
|
|
|
|
print_value(JS::Value(length), seen_objects);
|
|
|
|
out("\n byteLength: ");
|
|
|
|
print_value(JS::Value(typed_array_base.byte_length()), seen_objects);
|
|
|
|
out("\n buffer: ");
|
|
|
|
print_type("ArrayBuffer");
|
|
|
|
out(" @ {:p}", typed_array_base.viewed_array_buffer());
|
|
|
|
if (!length)
|
|
|
|
return;
|
|
|
|
outln();
|
|
|
|
// FIXME: This kinda sucks.
|
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
if (StringView(object.class_name()) == StringView(#ClassName)) { \
|
|
|
|
out("[ "); \
|
|
|
|
auto& typed_array = static_cast<const JS::ClassName&>(typed_array_base); \
|
|
|
|
auto data = typed_array.data(); \
|
|
|
|
for (size_t i = 0; i < length; ++i) { \
|
|
|
|
if (i > 0) \
|
|
|
|
out(", "); \
|
|
|
|
print_value(JS::Value(data[i]), seen_objects); \
|
|
|
|
} \
|
|
|
|
out(" ]"); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
JS_ENUMERATE_TYPED_ARRAYS
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-04-02 07:54:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 22:58:10 +00:00
|
|
|
static void print_primitive_wrapper_object(const FlyString& name, const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
2020-06-03 23:05:49 +00:00
|
|
|
{
|
2020-12-04 22:58:10 +00:00
|
|
|
// BooleanObject, NumberObject, StringObject
|
|
|
|
print_type(name);
|
|
|
|
out(" ");
|
|
|
|
print_value(object.value_of(), seen_objects);
|
2020-06-03 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects)
|
2020-03-26 11:26:11 +00:00
|
|
|
{
|
2020-04-08 16:38:09 +00:00
|
|
|
if (value.is_empty()) {
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[34;1m<empty>\033[0m");
|
2020-04-08 16:38:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-09 03:30:45 +00:00
|
|
|
|
2020-03-26 11:26:11 +00:00
|
|
|
if (value.is_object()) {
|
2020-04-01 20:18:47 +00:00
|
|
|
if (seen_objects.contains(&value.as_object())) {
|
2020-03-26 11:26:11 +00:00
|
|
|
// FIXME: Maybe we should only do this for circular references,
|
|
|
|
// not for all reoccurring objects.
|
2020-11-09 19:10:59 +00:00
|
|
|
out("<already printed Object {}>", &value.as_object());
|
2020-03-26 11:26:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-01 20:18:47 +00:00
|
|
|
seen_objects.set(&value.as_object());
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_array())
|
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-27 18:35:09 +00:00
|
|
|
return print_array(static_cast<JS::Array&>(value.as_object()), seen_objects);
|
2020-04-01 16:53:28 +00:00
|
|
|
|
2020-03-31 17:43:45 +00:00
|
|
|
if (value.is_object()) {
|
2020-04-01 20:18:47 +00:00
|
|
|
auto& object = value.as_object();
|
|
|
|
if (object.is_function())
|
2020-03-31 17:43:45 +00:00
|
|
|
return print_function(object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::Date>(object))
|
2020-03-31 17:43:45 +00:00
|
|
|
return print_date(object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::Error>(object))
|
2020-04-02 07:54:15 +00:00
|
|
|
return print_error(object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::RegExpObject>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
return print_regexp_object(object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::ProxyObject>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
return print_proxy_object(object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::ArrayBuffer>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
return print_array_buffer(object, seen_objects);
|
|
|
|
if (object.is_typed_array())
|
|
|
|
return print_typed_array(object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::StringObject>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
return print_primitive_wrapper_object("String", object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::NumberObject>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
return print_primitive_wrapper_object("Number", object, seen_objects);
|
2021-01-01 16:46:39 +00:00
|
|
|
if (is<JS::BooleanObject>(object))
|
2020-12-04 22:58:10 +00:00
|
|
|
return print_primitive_wrapper_object("Boolean", object, seen_objects);
|
2020-03-31 17:43:45 +00:00
|
|
|
return print_object(object, seen_objects);
|
|
|
|
}
|
2020-03-26 11:26:11 +00:00
|
|
|
|
|
|
|
if (value.is_string())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[32;1m");
|
2020-06-06 00:14:10 +00:00
|
|
|
else if (value.is_number() || value.is_bigint())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[35;1m");
|
2020-03-26 11:26:11 +00:00
|
|
|
else if (value.is_boolean())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[33;1m");
|
2020-03-26 13:54:43 +00:00
|
|
|
else if (value.is_null())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[33;1m");
|
2020-03-26 13:54:43 +00:00
|
|
|
else if (value.is_undefined())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[34;1m");
|
2020-03-26 11:26:11 +00:00
|
|
|
if (value.is_string())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\"");
|
2020-10-12 16:03:07 +00:00
|
|
|
else if (value.is_negative_zero())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("-");
|
|
|
|
out("{}", value.to_string_without_side_effects());
|
2020-03-26 11:26:11 +00:00
|
|
|
if (value.is_string())
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\"");
|
|
|
|
out("\033[0m");
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print(JS::Value value)
|
|
|
|
{
|
|
|
|
HashTable<JS::Object*> seen_objects;
|
|
|
|
print_value(value, seen_objects);
|
2020-11-09 19:10:59 +00:00
|
|
|
outln();
|
2020-03-26 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static bool file_has_shebang(AK::ByteBuffer file_contents)
|
2020-04-01 19:20:32 +00:00
|
|
|
{
|
|
|
|
if (file_contents.size() >= 2 && file_contents[0] == '#' && file_contents[1] == '!')
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static StringView strip_shebang(AK::ByteBuffer file_contents)
|
2020-04-01 19:20:32 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
for (i = 2; i < file_contents.size(); ++i) {
|
|
|
|
if (file_contents[i] == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return StringView((const char*)file_contents.data() + i, file_contents.size() - i);
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static bool write_to_file(const StringView& path)
|
2020-04-02 15:52:40 +00:00
|
|
|
{
|
|
|
|
int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
|
|
for (size_t i = 0; i < repl_statements.size(); i++) {
|
|
|
|
auto line = repl_statements[i];
|
|
|
|
if (line.length() && i != repl_statements.size() - 1) {
|
|
|
|
ssize_t nwritten = write(fd, line.characters(), line.length());
|
|
|
|
if (nwritten < 0) {
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i != repl_statements.size() - 1) {
|
|
|
|
char ch = '\n';
|
|
|
|
ssize_t nwritten = write(fd, &ch, 1);
|
|
|
|
if (nwritten != 1) {
|
|
|
|
perror("write");
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static bool parse_and_run(JS::Interpreter& interpreter, const StringView& source)
|
2020-05-14 15:29:24 +00:00
|
|
|
{
|
|
|
|
auto parser = JS::Parser(JS::Lexer(source));
|
|
|
|
auto program = parser.parse_program();
|
|
|
|
|
|
|
|
if (s_dump_ast)
|
|
|
|
program->dump(0);
|
|
|
|
|
|
|
|
if (parser.has_errors()) {
|
|
|
|
auto error = parser.errors()[0];
|
2020-05-26 12:00:36 +00:00
|
|
|
auto hint = error.source_location_hint(source);
|
|
|
|
if (!hint.is_empty())
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("{}", hint);
|
2020-09-27 13:18:55 +00:00
|
|
|
vm->throw_exception<JS::SyntaxError>(interpreter.global_object(), error.to_string());
|
2020-05-14 15:29:24 +00:00
|
|
|
} else {
|
2020-06-08 19:25:16 +00:00
|
|
|
interpreter.run(interpreter.global_object(), *program);
|
2020-05-14 15:29:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 19:29:57 +00:00
|
|
|
if (vm->exception()) {
|
2020-11-09 19:10:59 +00:00
|
|
|
out("Uncaught exception: ");
|
2020-09-23 19:29:57 +00:00
|
|
|
print(vm->exception()->value());
|
|
|
|
auto trace = vm->exception()->trace();
|
2020-06-02 13:12:04 +00:00
|
|
|
if (trace.size() > 1) {
|
2020-11-08 12:58:49 +00:00
|
|
|
unsigned repetitions = 0;
|
|
|
|
for (size_t i = 0; i < trace.size(); ++i) {
|
|
|
|
auto& function_name = trace[i];
|
|
|
|
if (i + 1 < trace.size() && trace[i + 1] == function_name) {
|
|
|
|
repetitions++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (repetitions > 4) {
|
|
|
|
// If more than 5 (1 + >4) consecutive function calls with the same name, print
|
|
|
|
// the name only once and show the number of repetitions instead. This prevents
|
|
|
|
// printing ridiculously large call stacks of recursive functions.
|
|
|
|
outln(" -> {}", function_name);
|
|
|
|
outln(" {} more calls", repetitions);
|
|
|
|
} else {
|
|
|
|
for (size_t j = 0; j < repetitions + 1; ++j)
|
|
|
|
outln(" -> {}", function_name);
|
|
|
|
}
|
|
|
|
repetitions = 0;
|
|
|
|
}
|
2020-06-02 13:12:04 +00:00
|
|
|
}
|
2020-09-27 13:18:55 +00:00
|
|
|
vm->clear_exception();
|
2020-05-14 15:29:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (s_print_last_result)
|
2020-09-27 13:18:55 +00:00
|
|
|
print(vm->last_value());
|
2020-05-14 15:29:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:20:32 +00:00
|
|
|
ReplObject::ReplObject()
|
|
|
|
{
|
2020-04-18 11:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplObject::initialize()
|
|
|
|
{
|
|
|
|
GlobalObject::initialize();
|
2020-06-02 11:02:29 +00:00
|
|
|
define_property("global", this, JS::Attribute::Enumerable);
|
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 04:33:37 +00:00
|
|
|
define_native_function("exit", exit_interpreter);
|
|
|
|
define_native_function("help", repl_help);
|
|
|
|
define_native_function("load", load_file, 1);
|
|
|
|
define_native_function("save", save_to_file, 1);
|
2020-04-01 19:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplObject::~ReplObject()
|
|
|
|
{
|
|
|
|
}
|
2020-04-13 13:04:24 +00:00
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReplObject::save_to_file)
|
2020-04-02 15:52:40 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
if (!vm.argument_count())
|
2020-04-02 15:52:40 +00:00
|
|
|
return JS::Value(false);
|
2020-09-27 16:36:49 +00:00
|
|
|
String save_path = vm.argument(0).to_string_without_side_effects();
|
2020-04-02 15:52:40 +00:00
|
|
|
StringView path = StringView(save_path.characters());
|
|
|
|
if (write_to_file(path)) {
|
|
|
|
return JS::Value(true);
|
|
|
|
}
|
|
|
|
return JS::Value(false);
|
|
|
|
}
|
2020-04-13 13:04:24 +00:00
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReplObject::exit_interpreter)
|
2020-04-01 19:20:32 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
if (!vm.argument_count())
|
2020-04-01 19:20:32 +00:00
|
|
|
exit(0);
|
2020-09-27 16:36:49 +00:00
|
|
|
auto exit_code = vm.argument(0).to_number(global_object);
|
2020-09-23 19:29:57 +00:00
|
|
|
if (::vm->exception())
|
2020-05-17 23:28:00 +00:00
|
|
|
return {};
|
|
|
|
exit(exit_code.as_double());
|
2020-04-01 19:20:32 +00:00
|
|
|
}
|
2020-04-13 13:04:24 +00:00
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReplObject::repl_help)
|
2020-04-01 19:20:32 +00:00
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("REPL commands:");
|
|
|
|
outln(" exit(code): exit the REPL with specified code. Defaults to 0.");
|
|
|
|
outln(" help(): display this menu");
|
|
|
|
outln(" load(files): accepts file names as params to load into running session. For example load(\"js/1.js\", \"js/2.js\", \"js/3.js\")");
|
|
|
|
outln(" save(file): accepts a file name, writes REPL input history to a file. For example: save(\"foo.txt\")");
|
2020-04-13 20:08:54 +00:00
|
|
|
return JS::js_undefined();
|
2020-04-01 19:20:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReplObject::load_file)
|
2020-04-01 19:20:32 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
if (!vm.argument_count())
|
2020-04-01 19:20:32 +00:00
|
|
|
return JS::Value(false);
|
2020-04-01 20:38:59 +00:00
|
|
|
|
2020-09-27 16:36:49 +00:00
|
|
|
for (auto& file : vm.call_frame().arguments) {
|
2020-04-29 10:35:39 +00:00
|
|
|
String file_name = file.as_string().string();
|
2020-04-01 19:20:32 +00:00
|
|
|
auto js_file = Core::File::construct(file_name);
|
|
|
|
if (!js_file->open(Core::IODevice::ReadOnly)) {
|
2020-11-09 19:10:59 +00:00
|
|
|
warnln("Failed to open {}: {}", file_name, js_file->error_string());
|
|
|
|
continue;
|
2020-04-01 19:20:32 +00:00
|
|
|
}
|
|
|
|
auto file_contents = js_file->read_all();
|
|
|
|
|
|
|
|
StringView source;
|
|
|
|
if (file_has_shebang(file_contents)) {
|
|
|
|
source = strip_shebang(file_contents);
|
|
|
|
} else {
|
|
|
|
source = file_contents;
|
|
|
|
}
|
2020-09-27 16:36:49 +00:00
|
|
|
parse_and_run(vm.interpreter(), source);
|
2020-04-01 19:20:32 +00:00
|
|
|
}
|
|
|
|
return JS::Value(true);
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void repl(JS::Interpreter& interpreter)
|
2020-03-25 22:10:29 +00:00
|
|
|
{
|
2020-05-25 12:27:07 +00:00
|
|
|
while (!s_fail_repl) {
|
2020-03-25 22:10:29 +00:00
|
|
|
String piece = read_next_piece();
|
|
|
|
if (piece.is_empty())
|
2020-03-31 11:29:42 +00:00
|
|
|
continue;
|
2020-04-02 15:52:40 +00:00
|
|
|
repl_statements.append(piece);
|
2020-05-14 15:29:24 +00:00
|
|
|
parse_and_run(interpreter, piece);
|
2020-03-25 22:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 20:58:27 +00:00
|
|
|
|
2020-04-24 16:59:16 +00:00
|
|
|
static Function<void()> interrupt_interpreter;
|
2020-08-10 21:48:37 +00:00
|
|
|
static void sigint_handler()
|
2020-04-24 16:59:16 +00:00
|
|
|
{
|
|
|
|
interrupt_interpreter();
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:09:05 +00:00
|
|
|
class ReplConsoleClient final : public JS::ConsoleClient {
|
|
|
|
public:
|
|
|
|
ReplConsoleClient(JS::Console& console)
|
|
|
|
: ConsoleClient(console)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual JS::Value log() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("{}", vm().join_arguments());
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value info() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("(i) {}", vm().join_arguments());
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value debug() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("\033[36;1m{}\033[0m", vm().join_arguments());
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value warn() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("\033[33;1m{}\033[0m", vm().join_arguments());
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value error() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("\033[31;1m{}\033[0m", vm().join_arguments());
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value clear() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
out("\033[3J\033[H\033[2J");
|
2020-05-04 14:09:05 +00:00
|
|
|
fflush(stdout);
|
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value trace() override
|
|
|
|
{
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("{}", vm().join_arguments());
|
2020-06-02 12:54:26 +00:00
|
|
|
auto trace = get_trace();
|
2020-05-05 09:48:57 +00:00
|
|
|
for (auto& function_name : trace) {
|
|
|
|
if (function_name.is_empty())
|
2020-05-04 14:09:05 +00:00
|
|
|
function_name = "<anonymous>";
|
2020-11-09 19:10:59 +00:00
|
|
|
outln(" -> {}", function_name);
|
2020-05-04 14:09:05 +00:00
|
|
|
}
|
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value count() override
|
|
|
|
{
|
2020-09-29 19:15:06 +00:00
|
|
|
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
2020-05-04 14:09:05 +00:00
|
|
|
auto counter_value = m_console.counter_increment(label);
|
2020-11-09 19:10:59 +00:00
|
|
|
outln("{}: {}", label, counter_value);
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
virtual JS::Value count_reset() override
|
|
|
|
{
|
2020-09-29 19:15:06 +00:00
|
|
|
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
2020-11-09 19:10:59 +00:00
|
|
|
if (m_console.counter_reset(label))
|
|
|
|
outln("{}: 0", label);
|
|
|
|
else
|
|
|
|
outln("\033[33;1m\"{}\" doesn't have a count\033[0m", label);
|
2020-05-04 14:09:05 +00:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-12 09:51:41 +00:00
|
|
|
int main(int argc, char** argv)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-03-16 18:18:46 +00:00
|
|
|
bool gc_on_every_allocation = false;
|
2020-04-30 20:37:50 +00:00
|
|
|
bool disable_syntax_highlight = false;
|
2020-03-12 09:51:41 +00:00
|
|
|
const char* script_path = nullptr;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("This is a JavaScript interpreter.");
|
2020-04-26 14:19:30 +00:00
|
|
|
args_parser.add_option(s_dump_ast, "Dump the AST", "dump-ast", 'A');
|
|
|
|
args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l');
|
2020-03-16 18:18:46 +00:00
|
|
|
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
2020-04-30 20:37:50 +00:00
|
|
|
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
|
2020-03-25 22:10:29 +00:00
|
|
|
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
|
2020-03-12 09:51:41 +00:00
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
2020-04-30 20:37:50 +00:00
|
|
|
bool syntax_highlight = !disable_syntax_highlight;
|
|
|
|
|
2020-09-23 19:29:57 +00:00
|
|
|
vm = JS::VM::create();
|
2020-04-24 16:59:16 +00:00
|
|
|
OwnPtr<JS::Interpreter> interpreter;
|
|
|
|
|
|
|
|
interrupt_interpreter = [&] {
|
|
|
|
auto error = JS::Error::create(interpreter->global_object(), "Error", "Received SIGINT");
|
2020-09-27 13:18:55 +00:00
|
|
|
vm->throw_exception(interpreter->global_object(), error);
|
2020-04-24 16:59:16 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 22:10:29 +00:00
|
|
|
if (script_path == nullptr) {
|
2020-05-14 15:29:24 +00:00
|
|
|
s_print_last_result = true;
|
2020-09-20 17:24:44 +00:00
|
|
|
interpreter = JS::Interpreter::create<ReplObject>(*vm);
|
2020-09-29 19:15:06 +00:00
|
|
|
ReplConsoleClient console_client(interpreter->global_object().console());
|
|
|
|
interpreter->global_object().console().set_client(console_client);
|
2020-04-01 19:20:32 +00:00
|
|
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
2020-09-27 13:18:55 +00:00
|
|
|
interpreter->vm().set_underscore_is_last_value(true);
|
2020-04-01 19:20:32 +00:00
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
s_editor = Line::Editor::construct();
|
2020-10-25 23:36:06 +00:00
|
|
|
s_editor->load_history(s_history_path);
|
2020-04-11 09:09:10 +00:00
|
|
|
|
|
|
|
signal(SIGINT, [](int) {
|
2020-04-28 20:01:22 +00:00
|
|
|
if (!s_editor->is_editing())
|
|
|
|
sigint_handler();
|
2020-10-25 23:36:06 +00:00
|
|
|
s_editor->save_history(s_history_path);
|
2020-04-11 09:09:10 +00:00
|
|
|
});
|
|
|
|
|
2020-04-26 14:19:30 +00:00
|
|
|
s_editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) {
|
2020-04-09 03:30:45 +00:00
|
|
|
auto stylize = [&](Line::Span span, Line::Style styles) {
|
|
|
|
if (syntax_highlight)
|
|
|
|
editor.stylize(span, styles);
|
|
|
|
};
|
|
|
|
editor.strip_styles();
|
|
|
|
|
2020-04-26 14:19:30 +00:00
|
|
|
size_t open_indents = s_repl_line_level;
|
2020-04-09 03:30:45 +00:00
|
|
|
|
2020-05-26 17:00:30 +00:00
|
|
|
auto line = editor.line();
|
|
|
|
JS::Lexer lexer(line);
|
2020-04-09 03:30:45 +00:00
|
|
|
bool indenters_starting_line = true;
|
|
|
|
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
|
|
|
auto length = token.value().length();
|
2020-05-26 17:00:30 +00:00
|
|
|
auto start = token.line_column() - 1;
|
2020-04-09 03:30:45 +00:00
|
|
|
auto end = start + length;
|
|
|
|
if (indenters_starting_line) {
|
|
|
|
if (token.type() != JS::TokenType::ParenClose && token.type() != JS::TokenType::BracketClose && token.type() != JS::TokenType::CurlyClose) {
|
|
|
|
indenters_starting_line = false;
|
|
|
|
} else {
|
|
|
|
--open_indents;
|
2020-04-05 14:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 03:30:45 +00:00
|
|
|
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
switch (token.category()) {
|
|
|
|
case JS::TokenCategory::Invalid:
|
2020-05-10 07:57:36 +00:00
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Red), Line::Style::Underline });
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::Number:
|
2020-05-10 07:57:36 +00:00
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Magenta) });
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::String:
|
2020-05-10 07:57:36 +00:00
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Green), Line::Style::Bold });
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::Punctuation:
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::Operator:
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::Keyword:
|
|
|
|
switch (token.type()) {
|
|
|
|
case JS::TokenType::BoolLiteral:
|
|
|
|
case JS::TokenType::NullLiteral:
|
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Yellow), Line::Style::Bold });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Blue), Line::Style::Bold });
|
|
|
|
break;
|
|
|
|
}
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::ControlKeyword:
|
2020-05-10 07:57:36 +00:00
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Cyan), Line::Style::Italic });
|
2020-04-09 03:30:45 +00:00
|
|
|
break;
|
LibJS: Unify syntax highlighting
So far we have three different syntax highlighters for LibJS:
- js's Line::Editor stylization
- JS::MarkupGenerator
- GUI::JSSyntaxHighlighter
This not only caused repetition of most token types in each highlighter
but also a lot of inconsistency regarding the styling of certain tokens:
- JSSyntaxHighlighter was considering TokenType::Period to be an
operator whereas MarkupGenerator categorized it as punctuation.
- MarkupGenerator was considering TokenType::{Break,Case,Continue,
Default,Switch,With} control keywords whereas JSSyntaxHighlighter just
disregarded them
- MarkupGenerator considered some future reserved keywords invalid and
others not. JSSyntaxHighlighter and js disregarded most
Adding a new token type meant adding it to ENUMERATE_JS_TOKENS as well
as each individual highlighter's switch/case construct.
I added a TokenCategory enum, and each TokenType is now associated to a
certain category, which the syntax highlighters then can use for styling
rather than operating on the token type directly. This also makes
changing a token's category everywhere easier, should we need to do that
(e.g. I decided to make TokenType::{Period,QuestionMarkPeriod}
TokenCategory::Operator for now, but we might want to change them to
Punctuation.
2020-10-04 21:28:59 +00:00
|
|
|
case JS::TokenCategory::Identifier:
|
2020-05-10 07:57:36 +00:00
|
|
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::White), Line::Style::Bold });
|
2020-04-09 03:30:45 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.set_prompt(prompt_for_level(open_indents));
|
|
|
|
};
|
2020-04-11 09:09:55 +00:00
|
|
|
|
2020-05-19 04:12:01 +00:00
|
|
|
auto complete = [&interpreter](const Line::Editor& editor) -> Vector<Line::CompletionSuggestion> {
|
|
|
|
auto line = editor.line(editor.cursor());
|
|
|
|
|
|
|
|
JS::Lexer lexer { line };
|
|
|
|
enum {
|
|
|
|
Initial,
|
|
|
|
CompleteVariable,
|
|
|
|
CompleteNullProperty,
|
|
|
|
CompleteProperty,
|
|
|
|
} mode { Initial };
|
|
|
|
|
|
|
|
StringView variable_name;
|
|
|
|
StringView property_name;
|
2020-04-11 14:32:15 +00:00
|
|
|
|
2020-04-11 09:09:55 +00:00
|
|
|
// we're only going to complete either
|
|
|
|
// - <N>
|
|
|
|
// where N is part of the name of a variable
|
|
|
|
// - <N>.<P>
|
|
|
|
// where N is the complete name of a variable and
|
|
|
|
// P is part of the name of one of its properties
|
2020-05-19 04:12:01 +00:00
|
|
|
auto js_token = lexer.next();
|
|
|
|
for (; js_token.type() != JS::TokenType::Eof; js_token = lexer.next()) {
|
|
|
|
switch (mode) {
|
|
|
|
case CompleteVariable:
|
|
|
|
switch (js_token.type()) {
|
|
|
|
case JS::TokenType::Period:
|
|
|
|
// ...<name> <dot>
|
|
|
|
mode = CompleteNullProperty;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// not a dot, reset back to initial
|
|
|
|
mode = Initial;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CompleteNullProperty:
|
|
|
|
if (js_token.is_identifier_name()) {
|
|
|
|
// ...<name> <dot> <name>
|
|
|
|
mode = CompleteProperty;
|
|
|
|
property_name = js_token.value();
|
|
|
|
} else {
|
|
|
|
mode = Initial;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CompleteProperty:
|
|
|
|
// something came after the property access, reset to initial
|
|
|
|
case Initial:
|
|
|
|
if (js_token.is_identifier_name()) {
|
|
|
|
// ...<name>...
|
|
|
|
mode = CompleteVariable;
|
|
|
|
variable_name = js_token.value();
|
|
|
|
} else {
|
|
|
|
mode = Initial;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool last_token_has_trivia = js_token.trivia().length() > 0;
|
|
|
|
|
|
|
|
if (mode == CompleteNullProperty) {
|
|
|
|
mode = CompleteProperty;
|
|
|
|
property_name = "";
|
|
|
|
last_token_has_trivia = false; // <name> <dot> [tab] is sensible to complete.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == Initial || last_token_has_trivia)
|
|
|
|
return {}; // we do not know how to complete this
|
|
|
|
|
2020-04-19 14:02:28 +00:00
|
|
|
Vector<Line::CompletionSuggestion> results;
|
2020-04-11 09:09:55 +00:00
|
|
|
|
|
|
|
Function<void(const JS::Shape&, const StringView&)> list_all_properties = [&results, &list_all_properties](const JS::Shape& shape, auto& property_pattern) {
|
|
|
|
for (const auto& descriptor : shape.property_table()) {
|
2020-07-08 04:38:46 +00:00
|
|
|
if (!descriptor.key.is_string())
|
|
|
|
continue;
|
|
|
|
auto key = descriptor.key.as_string();
|
|
|
|
if (key.view().starts_with(property_pattern)) {
|
|
|
|
Line::CompletionSuggestion completion { key, Line::CompletionSuggestion::ForSearch };
|
2020-05-02 15:01:09 +00:00
|
|
|
if (!results.contains_slow(completion)) { // hide duplicates
|
2020-07-08 04:38:46 +00:00
|
|
|
results.append(key);
|
2020-04-11 14:32:15 +00:00
|
|
|
}
|
2020-04-11 09:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto* prototype = shape.prototype()) {
|
|
|
|
list_all_properties(prototype->shape(), property_pattern);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-19 04:12:01 +00:00
|
|
|
switch (mode) {
|
|
|
|
case CompleteProperty: {
|
2020-09-27 13:18:55 +00:00
|
|
|
auto maybe_variable = vm->get_variable(variable_name, interpreter->global_object());
|
2020-04-25 16:43:34 +00:00
|
|
|
if (maybe_variable.is_empty()) {
|
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 04:33:37 +00:00
|
|
|
maybe_variable = interpreter->global_object().get(FlyString(variable_name));
|
2020-04-25 16:43:34 +00:00
|
|
|
if (maybe_variable.is_empty())
|
2020-05-19 04:12:01 +00:00
|
|
|
break;
|
2020-04-11 09:09:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 16:43:34 +00:00
|
|
|
auto variable = maybe_variable;
|
2020-04-11 09:09:55 +00:00
|
|
|
if (!variable.is_object())
|
2020-05-19 04:12:01 +00:00
|
|
|
break;
|
2020-04-11 09:09:55 +00:00
|
|
|
|
2020-09-27 16:36:49 +00:00
|
|
|
const auto* object = variable.to_object(interpreter->global_object());
|
2020-04-11 09:09:55 +00:00
|
|
|
const auto& shape = object->shape();
|
2020-05-19 04:12:01 +00:00
|
|
|
list_all_properties(shape, property_name);
|
2020-04-11 14:32:15 +00:00
|
|
|
if (results.size())
|
2020-05-19 04:12:01 +00:00
|
|
|
editor.suggest(property_name.length());
|
|
|
|
break;
|
2020-04-11 09:09:55 +00:00
|
|
|
}
|
2020-05-19 04:12:01 +00:00
|
|
|
case CompleteVariable: {
|
|
|
|
const auto& variable = interpreter->global_object();
|
|
|
|
list_all_properties(variable.shape(), variable_name);
|
|
|
|
if (results.size())
|
|
|
|
editor.suggest(variable_name.length());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-04-11 09:09:55 +00:00
|
|
|
return results;
|
|
|
|
};
|
2020-05-19 04:12:01 +00:00
|
|
|
s_editor->on_tab_complete = move(complete);
|
2020-04-01 19:04:51 +00:00
|
|
|
repl(*interpreter);
|
2020-10-25 23:36:06 +00:00
|
|
|
s_editor->save_history(s_history_path);
|
2020-03-25 22:10:29 +00:00
|
|
|
} else {
|
2020-09-20 17:24:44 +00:00
|
|
|
interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm);
|
2020-09-29 19:15:06 +00:00
|
|
|
ReplConsoleClient console_client(interpreter->global_object().console());
|
|
|
|
interpreter->global_object().console().set_client(console_client);
|
2020-04-01 19:20:32 +00:00
|
|
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
|
|
|
|
2020-04-24 16:59:16 +00:00
|
|
|
signal(SIGINT, [](int) {
|
|
|
|
sigint_handler();
|
|
|
|
});
|
|
|
|
|
2020-03-25 22:10:29 +00:00
|
|
|
auto file = Core::File::construct(script_path);
|
|
|
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
2020-11-09 19:10:59 +00:00
|
|
|
warnln("Failed to open {}: {}", script_path, file->error_string());
|
2020-03-25 22:10:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto file_contents = file->read_all();
|
|
|
|
|
|
|
|
StringView source;
|
2020-04-01 19:20:32 +00:00
|
|
|
if (file_has_shebang(file_contents)) {
|
|
|
|
source = strip_shebang(file_contents);
|
2020-03-25 22:10:29 +00:00
|
|
|
} else {
|
|
|
|
source = file_contents;
|
|
|
|
}
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-05-14 15:29:24 +00:00
|
|
|
if (!parse_and_run(*interpreter, source))
|
2020-04-13 00:05:21 +00:00
|
|
|
return 1;
|
2020-03-25 22:10:29 +00:00
|
|
|
}
|
2020-03-08 18:59:59 +00:00
|
|
|
|
2020-03-07 18:42:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|