forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileJS.cpp
More file actions
55 lines (43 loc) · 1.42 KB
/
CompileJS.cpp
File metadata and controls
55 lines (43 loc) · 1.42 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
/*
* 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 "CompileJS.h"
#include "hermes/BCGen/HBC/BytecodeProviderFromSrc.h"
#include "hermes/Support/Algorithms.h"
#include "llvh/Support/SHA1.h"
namespace hermes {
bool compileJS(
const std::string &str,
const std::string &sourceURL,
std::string &bytecode,
bool optimize) {
hbc::CompileFlags flags{};
flags.optimize = optimize;
// Note that we are relying the zero termination provided by str.data(),
// because the parser requires it.
auto res = hbc::BCProviderFromSrc::createBCProviderFromSrc(
hermes::make_unique<hermes::Buffer>(
(const uint8_t *)str.data(), str.size()),
sourceURL,
flags);
if (!res.first)
return false;
llvh::raw_string_ostream bcstream(bytecode);
BytecodeGenerationOptions opts(::hermes::EmitBundle);
opts.optimizationEnabled = optimize;
hbc::BytecodeSerializer BS{bcstream, opts};
BS.serialize(
*res.first->getBytecodeModule(),
llvh::SHA1::hash(llvh::makeArrayRef(
reinterpret_cast<const uint8_t *>(str.data()), str.size())));
// Flush to string.
bcstream.flush();
return true;
}
bool compileJS(const std::string &str, std::string &bytecode, bool optimize) {
return compileJS(str, "", bytecode, optimize);
}
} // namespace hermes