diff options
author | davidot <david.tuin@gmail.com> | 2021-09-22 12:44:56 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-30 08:16:32 +0100 |
commit | 830ea0414cc6e4686514ff7de337787dbb6c3b71 (patch) | |
tree | 14713d4415fefa215f720b032b650e5661da2ce6 /Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp | |
parent | 4428e494b0996e7211754cf05089293769ff808a (diff) | |
download | serenity-830ea0414cc6e4686514ff7de337787dbb6c3b71.zip |
LibJS: Make scoping follow the spec
Before this we used an ad-hoc combination of references and 'variables'
stored in a hashmap. This worked in most cases but is not spec like.
Additionally hoisting, dynamically naming functions and scope analysis
was not done properly.
This patch fixes all of that by:
- Implement BindingInitialization for destructuring assignment.
- Implementing a new ScopePusher which tracks the lexical and var
scoped declarations. This hoists functions to the top level if no
lexical declaration name overlaps. Furthermore we do checking of
redeclarations in the ScopePusher now requiring less checks all over
the place.
- Add methods for parsing the directives and statement lists instead
of having that code duplicated in multiple places. This allows
declarations to pushed to the appropriate scope more easily.
- Remove the non spec way of storing 'variables' in
DeclarativeEnvironment and make Reference follow the spec instead of
checking both the bindings and 'variables'.
- Remove all scoping related things from the Interpreter. And instead
use environments as specified by the spec. This also includes fixing
that NativeFunctions did not produce a valid FunctionEnvironment
which could cause issues with callbacks and eval. All
FunctionObjects now have a valid NewFunctionEnvironment
implementation.
- Remove execute_statements from Interpreter and instead use
ASTNode::execute everywhere this simplifies AST.cpp as you no longer
need to worry about which method to call.
- Make ScopeNodes setup their own environment. This uses four
different methods specified by the spec
{Block, Function, Eval, Global}DeclarationInstantiation with the
annexB extensions.
- Implement and use NamedEvaluation where specified.
Additionally there are fixes to things exposed by these changes to eval,
{for, for-in, for-of} loops and assignment.
Finally it also fixes some tests in test-js which where passing before
but not now that we have correct behavior :^).
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp | 18 |
1 files changed, 0 insertions, 18 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp index 2da747851c..f597d8bcb2 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp @@ -24,24 +24,6 @@ void ObjectEnvironment::visit_edges(Cell::Visitor& visitor) visitor.visit(&m_binding_object); } -Optional<Variable> ObjectEnvironment::get_from_environment(FlyString const& name) const -{ - if (!m_binding_object.storage_has(name)) - return {}; - auto value = m_binding_object.get(name); - return Variable { value, DeclarationKind::Var }; -} - -bool ObjectEnvironment::put_into_environment(FlyString const& name, Variable variable) -{ - return m_binding_object.set(name, variable.value, Object::ShouldThrowExceptions::No); -} - -bool ObjectEnvironment::delete_from_environment(FlyString const& name) -{ - return TRY_OR_DISCARD(m_binding_object.internal_delete(name)); -} - // 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n bool ObjectEnvironment::has_binding(FlyString const& name) const { |