2020-05-21 11:14:23 -07:00
|
|
|
/*
|
2021-04-22 16:53:07 -07:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2021-04-22 22:51:19 +02:00
|
|
|
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
|
2020-05-21 11:14:23 -07:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-21 11:14:23 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
2020-09-27 19:59:33 +02:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-05-21 11:14:23 -07:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
class Accessor final : public Cell {
|
|
|
|
|
public:
|
2021-06-27 21:48:34 +02:00
|
|
|
static Accessor* create(VM& vm, FunctionObject* getter, FunctionObject* setter)
|
2020-05-21 11:14:23 -07:00
|
|
|
{
|
2020-09-27 19:59:33 +02:00
|
|
|
return vm.heap().allocate_without_global_object<Accessor>(getter, setter);
|
2020-05-21 11:14:23 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
Accessor(FunctionObject* getter, FunctionObject* setter)
|
2020-05-21 11:14:23 -07:00
|
|
|
: m_getter(getter)
|
|
|
|
|
, m_setter(setter)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* getter() const { return m_getter; }
|
|
|
|
|
void set_getter(FunctionObject* getter) { m_getter = getter; }
|
2020-05-21 11:14:23 -07:00
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* setter() const { return m_setter; }
|
|
|
|
|
void set_setter(FunctionObject* setter) { m_setter = setter; }
|
2020-05-23 23:27:10 +01:00
|
|
|
|
|
|
|
|
Value call_getter(Value this_value)
|
2020-05-21 11:14:23 -07:00
|
|
|
{
|
2020-05-23 23:27:10 +01:00
|
|
|
if (!m_getter)
|
2020-05-21 11:14:23 -07:00
|
|
|
return js_undefined();
|
2021-09-23 20:56:28 +03:00
|
|
|
return TRY_OR_DISCARD(vm().call(*m_getter, this_value));
|
2020-05-21 11:14:23 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-23 23:27:10 +01:00
|
|
|
void call_setter(Value this_value, Value setter_value)
|
2020-05-21 11:14:23 -07:00
|
|
|
{
|
2020-05-23 23:27:10 +01:00
|
|
|
if (!m_setter)
|
2020-05-21 11:14:23 -07:00
|
|
|
return;
|
2020-08-14 17:31:07 +02:00
|
|
|
// FIXME: It might be nice if we had a way to communicate to our caller if an exception happened after this.
|
2020-12-20 16:09:48 -07:00
|
|
|
[[maybe_unused]] auto rc = vm().call(*m_setter, this_value, setter_value);
|
2020-05-21 11:14:23 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-28 14:33:36 +01:00
|
|
|
void visit_edges(Cell::Visitor& visitor) override
|
2020-05-21 11:14:23 -07:00
|
|
|
{
|
|
|
|
|
visitor.visit(m_getter);
|
|
|
|
|
visitor.visit(m_setter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const char* class_name() const override { return "Accessor"; };
|
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* m_getter { nullptr };
|
|
|
|
|
FunctionObject* m_setter { nullptr };
|
2020-05-21 11:14:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|