mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 08:27: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:
parent
ee0bf55127
commit
24cce3674b
5 changed files with 101 additions and 12 deletions
|
@ -411,6 +411,8 @@ Value UnaryExpression::execute(Interpreter& interpreter) const
|
||||||
return Value(true);
|
return Value(true);
|
||||||
// FIXME: Support deleting locals
|
// FIXME: Support deleting locals
|
||||||
ASSERT(!reference.is_local_variable());
|
ASSERT(!reference.is_local_variable());
|
||||||
|
if (reference.is_global_variable())
|
||||||
|
return interpreter.global_object().delete_property(reference.name());
|
||||||
auto* base_object = reference.base().to_object(interpreter.heap());
|
auto* base_object = reference.base().to_object(interpreter.heap());
|
||||||
if (!base_object)
|
if (!base_object)
|
||||||
return {};
|
return {};
|
||||||
|
@ -844,7 +846,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
|
||||||
if (reference.is_unresolvable())
|
if (reference.is_unresolvable())
|
||||||
return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
|
return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
|
||||||
|
|
||||||
reference.assign(interpreter, rhs_result);
|
reference.put(interpreter, rhs_result);
|
||||||
|
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return {};
|
return {};
|
||||||
|
@ -853,9 +855,11 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
|
||||||
|
|
||||||
Value UpdateExpression::execute(Interpreter& interpreter) const
|
Value UpdateExpression::execute(Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
ASSERT(m_argument->is_identifier());
|
auto reference = m_argument->to_reference(interpreter);
|
||||||
auto name = static_cast<const Identifier&>(*m_argument).string();
|
if (interpreter.exception())
|
||||||
auto old_value = m_argument->execute(interpreter);
|
return {};
|
||||||
|
|
||||||
|
auto old_value = reference.get(interpreter);
|
||||||
if (interpreter.exception())
|
if (interpreter.exception())
|
||||||
return {};
|
return {};
|
||||||
old_value = old_value.to_number();
|
old_value = old_value.to_number();
|
||||||
|
@ -873,7 +877,9 @@ Value UpdateExpression::execute(Interpreter& interpreter) const
|
||||||
}
|
}
|
||||||
|
|
||||||
auto new_value = Value(old_value.as_double() + op_result);
|
auto new_value = Value(old_value.as_double() + op_result);
|
||||||
interpreter.set_variable(name, new_value);
|
reference.put(interpreter, new_value);
|
||||||
|
if (interpreter.exception())
|
||||||
|
return {};
|
||||||
return m_prefixed ? new_value : old_value;
|
return m_prefixed ? new_value : old_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -173,7 +173,7 @@ Reference Interpreter::get_reference(const FlyString& name)
|
||||||
return { Reference::LocalVariable, name };
|
return { Reference::LocalVariable, name };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { &global_object(), PropertyName(name) };
|
return { Reference::GlobalVariable, name };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
|
void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
|
||||||
|
|
|
@ -24,20 +24,29 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibJS/Interpreter.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/Object.h>
|
||||||
|
#include <LibJS/Runtime/Reference.h>
|
||||||
|
|
||||||
namespace JS {
|
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().
|
// 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()) {
|
if (is_local_variable() || is_global_variable()) {
|
||||||
interpreter.set_variable(m_name.to_string(), value);
|
if (is_local_variable())
|
||||||
|
interpreter.set_variable(m_name.to_string(), value);
|
||||||
|
else
|
||||||
|
interpreter.global_object().put(m_name, value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,4 +57,46 @@ void Reference::assign(Interpreter& interpreter, Value value)
|
||||||
object->put(m_name, 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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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; }
|
Value base() const { return m_base; }
|
||||||
const PropertyName& name() const { return m_name; }
|
const PropertyName& name() const { return m_name; }
|
||||||
bool is_strict() const { return m_strict; }
|
bool is_strict() const { return m_strict; }
|
||||||
|
@ -71,13 +80,22 @@ public:
|
||||||
return m_local_variable;
|
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:
|
private:
|
||||||
|
void throw_reference_error(Interpreter&);
|
||||||
|
|
||||||
Value m_base { js_undefined() };
|
Value m_base { js_undefined() };
|
||||||
PropertyName m_name;
|
PropertyName m_name;
|
||||||
bool m_strict { false };
|
bool m_strict { false };
|
||||||
bool m_local_variable { false };
|
bool m_local_variable { false };
|
||||||
|
bool m_global_variable { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
const LogStream& operator<<(const LogStream&, const Value&);
|
const LogStream& operator<<(const LogStream&, const Value&);
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
load("test-common.js");
|
||||||
|
|
||||||
|
try {
|
||||||
|
var o = {};
|
||||||
|
o.f = 1;
|
||||||
|
|
||||||
|
assert(o.f++ === 1);
|
||||||
|
assert(++o.f === 3);
|
||||||
|
assert(isNaN(++o.missing));
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue