forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.cpp
More file actions
179 lines (159 loc) · 5.47 KB
/
Driver.cpp
File metadata and controls
179 lines (159 loc) · 5.47 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* 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.
*/
#ifdef HERMESVM_API_TRACE
#include <hermes/TraceInterpreter.h>
#include <hermes/hermes.h>
#include <gtest/gtest.h>
#include <llvh/Support/FileSystem.h>
#include <llvh/Support/MemoryBuffer.h>
#include <llvh/Support/SHA1.h>
#include <iterator>
#include <memory>
#include <regex>
#include <string>
#include <vector>
#include "tests/TestFunctions.h"
using ParamType = std::tuple<
const char *,
std::function<const char *()>,
std::function<const char *()>>;
#define TEST_FUNC_MAKE_TUPLE(name) \
ParamType{ \
#name, \
facebook::hermes::synthtest::name##Trace, \
facebook::hermes::synthtest::name##Source, \
},
namespace {
using namespace facebook::hermes;
using namespace hermes::vm;
class SynthBenchmarkTestFixture : public ::testing::TestWithParam<ParamType> {
protected:
SynthBenchmarkTestFixture()
: testName_(std::get<0>(GetParam())),
trace_(std::get<1>(GetParam())()),
source_(std::get<2>(GetParam())()) {}
const char *testName_;
const char *trace_;
const char *source_;
/// All of the tests provide a trace. We run each test in two modes
/// -- specify property names as string IDs, or as PropNameIDs.
/// The traces use the convention of creating fake
/// "CreatePropNameRecords". These are translated either into
/// CreateStringRecords or CreatePropeNameIDRecords, via regex
/// replacement. The references to these are specified as StringIDs,
/// and are translated for the PropNameID case. (We can't do this
/// trick for creation, because some CreateStringRecords should not
/// be translated to PropNameID.)
/// This method is then run on the translated trace.
void canRunWithoutException(const std::string &trace) {
tracing::TraceInterpreter::ExecuteOptions options;
std::vector<std::unique_ptr<llvh::MemoryBuffer>> sources;
sources.emplace_back(llvh::MemoryBuffer::getMemBuffer(source_));
EXPECT_NO_THROW({
tracing::TraceInterpreter::execFromMemoryBuffer(
llvh::MemoryBuffer::getMemBuffer(trace.c_str()),
std::move(sources),
options,
nullptr);
}) << "Failed on test: "
<< testName_;
}
/// Substitute \p to for \p from in \p str, modifying \p str.
static void
subst(const std::string &from, const std::string &to, std::string &str) {
size_t pos = 0;
while ((pos = str.find(from, pos)) != std::string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
}
};
TEST_P(SynthBenchmarkTestFixture, CanRunWithoutExceptionString) {
// Convert CreatePropNameRecords to CreateStringRecords.
std::string stringNameIDTrace{trace_};
subst("CreatePropNameRecord", "CreateStringRecord", stringNameIDTrace);
canRunWithoutException(stringNameIDTrace);
}
TEST_P(SynthBenchmarkTestFixture, CanRunWithoutExceptionPropNameID) {
// Convert CreatePropNameRecords to CreatePropNameIDRecords.
std::string propNameIDTrace{trace_};
subst("CreatePropNameRecord", "CreatePropNameIDRecord", propNameIDTrace);
canRunWithoutException(propNameIDTrace);
}
TEST(SynthBenchmark, RunMultipleSourceFiles) {
tracing::TraceInterpreter::ExecuteOptions options;
const char *const traceFmt = R"(
{
"globalObjID": 1,
"env": {
"mathRandomSeed": 0,
"callsToDateNow": [],
"callsToNewDate": [],
"callsToDateAsFunction": [],
"callsToHermesInternalGetInstrumentedStats": [],
},
"trace": [
{
"type": "BeginExecJSRecord",
"time": 0,
"sourceHash": "%s"
},
{
"type": "EndExecJSRecord",
"retval": "string:1111",
"time": 0
},
{
"type": "BeginExecJSRecord",
"time": 0,
"sourceHash": "%s"
},
{
"type": "EndExecJSRecord",
"retval": "string:1112",
"time": 0
}
]
})";
std::string source1 = R"("hello";)";
std::string source2 = R"("goodbye";)";
std::string trace;
{
auto hash1 = llvh::SHA1::hash(llvh::makeArrayRef(
reinterpret_cast<const uint8_t *>(source1.c_str()), source1.length()));
auto hash2 = llvh::SHA1::hash(llvh::makeArrayRef(
reinterpret_cast<const uint8_t *>(source2.c_str()), source2.length()));
std::string hash1AsStr = ::hermes::hashAsString(hash1);
std::string hash2AsStr = ::hermes::hashAsString(hash2);
int numChars =
snprintf(nullptr, 0, traceFmt, hash1AsStr.c_str(), hash2AsStr.c_str());
char *buf = new char[numChars + 1];
snprintf(
buf, numChars + 1, traceFmt, hash1AsStr.c_str(), hash2AsStr.c_str());
trace = std::string(buf);
delete[] buf;
}
std::vector<std::unique_ptr<llvh::MemoryBuffer>> sources;
sources.emplace_back(llvh::MemoryBuffer::getMemBuffer(source1));
sources.emplace_back(llvh::MemoryBuffer::getMemBuffer(source2));
EXPECT_NO_THROW({
tracing::TraceInterpreter::execFromMemoryBuffer(
llvh::MemoryBuffer::getMemBuffer(trace),
std::move(sources),
options,
nullptr);
});
}
std::vector<ParamType> testGenerator() {
return {FOREACH_TEST(TEST_FUNC_MAKE_TUPLE)};
}
INSTANTIATE_TEST_CASE_P(
ExceptionTest,
SynthBenchmarkTestFixture,
::testing::ValuesIn(testGenerator()));
} // namespace
#endif