forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTestCompile.cpp
More file actions
101 lines (84 loc) · 2.95 KB
/
SegmentTestCompile.cpp
File metadata and controls
101 lines (84 loc) · 2.95 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* 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 "SegmentTestCompile.h"
#include "hermes/AST/CommonJS.h"
#include "hermes/AST/Context.h"
#include "hermes/AST/SemValidate.h"
#include "hermes/BCGen/HBC/BytecodeDisassembler.h"
#include "hermes/BCGen/HBC/HBC.h"
#include "hermes/IR/IR.h"
#include "hermes/IRGen/IRGen.h"
#include "hermes/Parser/JSParser.h"
#include "hermes/Support/Algorithms.h"
#include "llvh/ADT/StringRef.h"
#include "llvh/Support/MemoryBuffer.h"
namespace hermes {
std::pair<std::string, std::string> genSplitCode(
std::string &mainCode,
std::string &segmentCode) {
std::vector<uint32_t> segments{
0,
1,
};
hermes::CodeGenerationSettings codeGenOpts;
hermes::OptimizationSettings optimizationOpts;
auto context = std::make_shared<hermes::Context>(
codeGenOpts, optimizationOpts, nullptr, segments);
context->setUseCJSModules(true);
hermes::Module M(context);
hermes::sem::SemContext semCtx{};
auto parseJS = [&](std::unique_ptr<llvh::MemoryBuffer> fileBuf,
bool wrapCJSModule = false) -> hermes::ESTree::NodePtr {
::hermes::parser::JSParser jsParser(*context, std::move(fileBuf));
auto parsedJs = jsParser.parse();
hermes::ESTree::NodePtr parsedAST = parsedJs.getValue();
if (wrapCJSModule) {
parsedAST = hermes::wrapCJSModule(
context, cast<hermes::ESTree::ProgramNode>(parsedAST));
}
validateAST(*context, semCtx, parsedAST);
return parsedAST;
};
::hermes::DeclarationFileListTy declFileList;
auto globalMemBuffer = llvh::MemoryBuffer::getMemBufferCopy("", "<global>");
auto *globalAST = parseJS(std::move(globalMemBuffer));
generateIRFromESTree(globalAST, &M, declFileList, {});
auto *topLevelFunction = M.getTopLevelFunction();
auto genModule = [&](uint32_t segmentID,
uint32_t id,
std::unique_ptr<llvh::MemoryBuffer> fileBuf) {
llvh::StringRef filename = fileBuf->getBufferIdentifier();
auto *ast = parseJS(std::move(fileBuf), true);
generateIRForCJSModule(
cast<hermes::ESTree::FunctionExpressionNode>(ast),
segmentID,
id,
filename,
&M,
topLevelFunction,
declFileList);
};
genModule(
segments[0],
0,
llvh::MemoryBuffer::getMemBufferCopy(mainCode, "main.js"));
genModule(
segments[1],
1,
llvh::MemoryBuffer::getMemBufferCopy(segmentCode, "foo.js"));
hermes::BytecodeGenerationOptions genOpts{hermes::EmitBundle};
hermes::SHA1 sourceHash;
auto genBC = [&](uint32_t segment) {
std::string bc{};
llvh::raw_string_ostream os{bc};
hermes::hbc::generateBytecode(
&M, os, genOpts, sourceHash, segment, nullptr, nullptr);
return os.str();
};
return std::make_pair(genBC(segments[0]), genBC(segments[1]));
}
} // namespace hermes