Skip to content

Commit

Permalink
Create the module scope as a function
Browse files Browse the repository at this point in the history
Summary:
Currently, we do not correctly process declarations at the top level of
a module because the DeclCollector is only run on functions. This is a
problem for modules, because they are defined as a scope in a dummy
global function, which does not actually contain the AST for the
modules. So when the DeclCollector runs, the declarations in the module
scope cannot be reached.

Using a function also ensures that vars declared at the top level of a
module are not treated as global properties.

Reviewed By: tmikov

Differential Revision: D36231405

fbshipit-source-id: 20a16daa905fe84e659630dc3410d9245ecb1831
  • Loading branch information
neildhar authored and facebook-github-bot committed May 9, 2022
1 parent 5c7a666 commit f1462aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
25 changes: 14 additions & 11 deletions unsupported/juno/crates/juno/src/sema/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<'gc> Resolver<'gc, '_> {
scope_node: &'gc Node<'gc>,
f: F,
) -> R {
// New biding table scope.
// New binding table scope.
self.binding_table.push_scope();
// New lexical scope.
let prev_scope = self.current_scope;
Expand Down Expand Up @@ -447,17 +447,20 @@ impl<'gc> Resolver<'gc, '_> {
pself.declare_known_globals(lock, *global.range());
}

pself.in_new_scope(lock, node, |pself| {
// Search for "use strict".
if find_use_strict(lock, &node_cast!(Node::Module, node).body).is_some() {
pself
.sem
.function_mut(pself.function_context().func_id)
.strict = true;
}
// Create the module scope as a function.
pself.in_new_function(lock, node, |pself| {
pself.in_new_scope(lock, node, |pself| {
// Search for "use strict".
if find_use_strict(lock, &node_cast!(Node::Module, node).body).is_some() {
pself
.sem
.function_mut(pself.function_context().func_id)
.strict = true;
}

pself.process_collected_declarations(lock, node);
node.visit_children(lock, pself);
pself.process_collected_declarations(lock, node);
node.visit_children(lock, pself);
})
});
})
});
Expand Down
16 changes: 16 additions & 0 deletions unsupported/juno/lit/juno/sema/module-function-decl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %juno %s %s | %FileCheck %s --match-full-lines

function foo(){
return 42;
}

// CHECK-LABEL: Module: {{.*}}/module-function-decl.js
// CHECK: 1 declarations
// CHECK: Decl#0 'foo' ScopedFunction NotSpecial
14 changes: 14 additions & 0 deletions unsupported/juno/lit/juno/sema/module-var-decl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %juno %s %s | %FileCheck %s --match-full-lines

var x = 5;

// CHECK-LABEL: Module: {{.*}}/module-var-decl.js
// CHECK: 1 declarations
// CHECK: Decl#0 'x' Var NotSpecial

0 comments on commit f1462aa

Please sign in to comment.