forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeChainTest.cpp
More file actions
68 lines (57 loc) · 2.16 KB
/
ScopeChainTest.cpp
File metadata and controls
68 lines (57 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/AST/SemValidate.h"
#include "hermes/IRGen/IRGen.h"
#include "hermes/Parser/JSParser.h"
#include "gtest/gtest.h"
using namespace hermes;
namespace {
/// Verify that scope chains are respected in IRGen.
TEST(IRScopeChainTest, BasicScopeChainTest) {
SourceErrorManager sm;
CodeGenerationSettings codeGenOpts;
codeGenOpts.unlimitedRegisters = false;
auto context = std::make_shared<Context>(sm, codeGenOpts);
// This simulates local eval. We have an incoming nested scope with a variable
// "alpha", inside a scope containing variables "beta" and "delta". "gamma" is
// not part of our scope chain and should be global.
ScopeChain scopeChain{};
scopeChain.functions.emplace_back();
scopeChain.functions.back().variables = {"alpha"};
scopeChain.functions.emplace_back();
scopeChain.functions.back().variables = {"beta", "delta"};
hermes::parser::JSParser jsParser(
*context, "print(alpha, beta, gamma, delta);");
auto parsed = jsParser.parse();
ASSERT_TRUE(parsed);
sem::SemContext semCtx{};
auto validated = validateAST(*context, semCtx, *parsed);
ASSERT_TRUE(validated);
auto *ast = parsed.getValue();
Module M(context);
DeclarationFileListTy declFileList;
hermes::generateIRFromESTree(ast, &M, declFileList, scopeChain);
// Count how many frame loads there are.
int loadsFromGlobals = 0;
int loadsFromNonglobals = 0;
for (const auto &F : M) {
for (const auto &BB : F.getBasicBlockList()) {
for (const auto &Ins : BB) {
if (llvh::isa<LoadFrameInst>(&Ins)) {
loadsFromNonglobals++;
} else if (const auto *LP = llvh::dyn_cast<LoadPropertyInst>(&Ins)) {
if (llvh::isa<GlobalObject>(LP->getObject()))
loadsFromGlobals++;
}
}
}
}
EXPECT_EQ(2u, M.size()); // one global function and one inner function.
EXPECT_EQ(2, loadsFromGlobals); // "gamma", plus print function.
EXPECT_EQ(3, loadsFromNonglobals); // "alpha", "beta", "delta"
}
} // end anonymous namespace