1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:47:35 +00:00

LibJS: Support o.f++ :^)

This patch teaches UpdateExpression how to use a Reference. Some other
changes were necessary to keep tests working:
A Reference can now also refer to a local or global variable. This is
not fully aligned with the spec since we don't have a Record concept.
This commit is contained in:
Andreas Kling 2020-04-28 14:44:48 +02:00
parent ee0bf55127
commit 24cce3674b
5 changed files with 101 additions and 12 deletions

View file

@ -24,20 +24,29 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringBuilder.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Reference.h>
namespace JS {
void Reference::assign(Interpreter& interpreter, Value value)
void Reference::put(Interpreter& interpreter, Value value)
{
// NOTE: The caller is responsible for doing an exception check after assign().
ASSERT(!is_unresolvable());
if (is_unresolvable()) {
throw_reference_error(interpreter);
return;
}
if (is_local_variable()) {
interpreter.set_variable(m_name.to_string(), value);
if (is_local_variable() || is_global_variable()) {
if (is_local_variable())
interpreter.set_variable(m_name.to_string(), value);
else
interpreter.global_object().put(m_name, value);
return;
}
@ -48,4 +57,46 @@ void Reference::assign(Interpreter& interpreter, Value value)
object->put(m_name, value);
}
void Reference::throw_reference_error(Interpreter& interpreter)
{
auto property_name = m_name.to_string();
String message;
if (property_name.is_empty())
message = "Unresolvable reference";
else
message = String::format("'%s' not known", property_name.characters());
interpreter.throw_exception<ReferenceError>(message);
}
Value Reference::get(Interpreter& interpreter)
{
// NOTE: The caller is responsible for doing an exception check after fetch().
if (is_unresolvable()) {
throw_reference_error(interpreter);
return {};
}
if (is_local_variable() || is_global_variable()) {
Value value;
if (is_local_variable())
value = interpreter.get_variable(m_name.to_string());
else
value = interpreter.global_object().get(m_name);
if (interpreter.exception())
return {};
if (value.is_empty()) {
throw_reference_error(interpreter);
return {};
}
return value;
}
auto* object = base().to_object(interpreter.heap());
if (!object)
return {};
return object->get(m_name).value_or(js_undefined());
}
}

View file

@ -51,6 +51,15 @@ public:
{
}
enum GlobalVariableTag { GlobalVariable };
Reference(GlobalVariableTag, const String& name, bool strict = false)
: m_base(js_null())
, m_name(name)
, m_strict(strict)
, m_global_variable(true)
{
}
Value base() const { return m_base; }
const PropertyName& name() const { return m_name; }
bool is_strict() const { return m_strict; }
@ -71,13 +80,22 @@ public:
return m_local_variable;
}
void assign(Interpreter&, Value);
bool is_global_variable() const
{
return m_global_variable;
}
void put(Interpreter&, Value);
Value get(Interpreter&);
private:
void throw_reference_error(Interpreter&);
Value m_base { js_undefined() };
PropertyName m_name;
bool m_strict { false };
bool m_local_variable { false };
bool m_global_variable { false };
};
const LogStream& operator<<(const LogStream&, const Value&);