mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:48:11 +00:00
LibJS: Initial class implementation; allow super expressions in object
literal methods; add EnvrionmentRecord fields and methods to LexicalEnvironment Adding EnvrionmentRecord's fields and methods lets us throw an exception when |this| is not initialized, which occurs when the super constructor in a derived class has not yet been called, or when |this| has already been initialized (the super constructor was already called).
This commit is contained in:
parent
a535d58cac
commit
7533fd8b02
18 changed files with 967 additions and 92 deletions
|
@ -24,7 +24,11 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/Function.h>
|
||||
#include <LibJS/Runtime/LexicalEnvironment.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
@ -32,12 +36,24 @@ LexicalEnvironment::LexicalEnvironment()
|
|||
{
|
||||
}
|
||||
|
||||
LexicalEnvironment::LexicalEnvironment(EnvironmentRecordType environment_record_type)
|
||||
: m_environment_record_type(environment_record_type)
|
||||
{
|
||||
}
|
||||
|
||||
LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent)
|
||||
: m_parent(parent)
|
||||
, m_variables(move(variables))
|
||||
{
|
||||
}
|
||||
|
||||
LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent, EnvironmentRecordType environment_record_type)
|
||||
: m_parent(parent)
|
||||
, m_variables(move(variables))
|
||||
, m_environment_record_type(environment_record_type)
|
||||
{
|
||||
}
|
||||
|
||||
LexicalEnvironment::~LexicalEnvironment()
|
||||
{
|
||||
}
|
||||
|
@ -46,6 +62,10 @@ void LexicalEnvironment::visit_children(Visitor& visitor)
|
|||
{
|
||||
Cell::visit_children(visitor);
|
||||
visitor.visit(m_parent);
|
||||
visitor.visit(m_this_value);
|
||||
visitor.visit(m_home_object);
|
||||
visitor.visit(m_new_target);
|
||||
visitor.visit(m_current_function);
|
||||
for (auto& it : m_variables)
|
||||
visitor.visit(it.value.value);
|
||||
}
|
||||
|
@ -60,4 +80,53 @@ void LexicalEnvironment::set(const FlyString& name, Variable variable)
|
|||
m_variables.set(name, variable);
|
||||
}
|
||||
|
||||
bool LexicalEnvironment::has_super_binding() const
|
||||
{
|
||||
return m_environment_record_type == EnvironmentRecordType::Function && this_binding_status() != ThisBindingStatus::Lexical && m_home_object.is_object();
|
||||
}
|
||||
|
||||
Value LexicalEnvironment::get_super_base()
|
||||
{
|
||||
ASSERT(has_super_binding());
|
||||
if (m_home_object.is_object())
|
||||
return m_home_object.as_object().prototype();
|
||||
return {};
|
||||
}
|
||||
|
||||
bool LexicalEnvironment::has_this_binding() const
|
||||
{
|
||||
// More like "is_capable_of_having_a_this_binding".
|
||||
switch (m_environment_record_type) {
|
||||
case EnvironmentRecordType::Declarative:
|
||||
case EnvironmentRecordType::Object:
|
||||
return false;
|
||||
case EnvironmentRecordType::Function:
|
||||
return this_binding_status() != ThisBindingStatus::Lexical;
|
||||
case EnvironmentRecordType::Module:
|
||||
case EnvironmentRecordType::Global:
|
||||
return true;
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
Value LexicalEnvironment::get_this_binding() const
|
||||
{
|
||||
ASSERT(has_this_binding());
|
||||
if (this_binding_status() == ThisBindingStatus::Uninitialized)
|
||||
return interpreter().throw_exception<ReferenceError>(ErrorType::ThisHasNotBeenInitialized);
|
||||
|
||||
return m_this_value;
|
||||
}
|
||||
|
||||
void LexicalEnvironment::bind_this_value(Value this_value)
|
||||
{
|
||||
ASSERT(has_this_binding());
|
||||
if (m_this_binding_status == ThisBindingStatus::Initialized) {
|
||||
interpreter().throw_exception<ReferenceError>(ErrorType::ThisIsAlreadyInitialized);
|
||||
return;
|
||||
}
|
||||
m_this_value = this_value;
|
||||
m_this_binding_status = ThisBindingStatus::Initialized;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue