forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRInstrumentationTest.cpp
More file actions
73 lines (59 loc) · 2.26 KB
/
IRInstrumentationTest.cpp
File metadata and controls
73 lines (59 loc) · 2.26 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
/*
* 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 HERMES_ENABLE_IR_INSTRUMENTATION
#include "hermes/BCGen/HBC/BytecodeProviderFromSrc.h"
#include "hermes/VM/Runtime.h"
#include "gtest/gtest.h"
using namespace hermes::vm;
using namespace hermes::hbc;
namespace {
class IRInstrumentationTest : public ::testing::Test {
protected:
std::shared_ptr<Runtime> runtime_;
void SetUp() override {
auto config = RuntimeConfig::Builder().build();
runtime_ = Runtime::create(config);
}
CallResult<HermesValue> run(llvh::StringRef ref, bool instrument) {
CompileFlags flags;
flags.instrumentIR = instrument;
flags.optimize = false;
auto result = runtime_->run(ref, "IRInstrumentationTest.cpp", flags);
EXPECT_EQ(ExecutionStatus::RETURNED, result.getStatus());
return result;
}
CallResult<HermesValue> run(llvh::StringRef code) {
return run(code, true);
}
void setHook(llvh::StringRef code) {
auto result = run(code, false);
ASSERT_EQ(ExecutionStatus::RETURNED, result.getStatus());
}
};
TEST_F(IRInstrumentationTest, binaryHooksCanPassCookieAndOverride) {
setHook(
"__instrument.preBinary = function(iid, op, lhs, rhs) { return 41; }");
setHook(
"__instrument.postBinary = function(iid, cookie, op, result, lhs, rhs) { return cookie+1; }");
ASSERT_EQ(42, run("1+2")->getDouble());
}
TEST_F(IRInstrumentationTest, unaryHooksCanPassCookieAndOverride) {
setHook("__instrument.preUnary = function(iid, op, operand) { return 41; }");
setHook(
"__instrument.postUnary = function(iid, cookie, op, result, operand) { return cookie+1; }");
ASSERT_EQ(42, run("-1234")->getDouble());
}
TEST_F(IRInstrumentationTest, assignmentHooksCanPassCookieAndOverride) {
setHook(
"__instrument.preAssignment = function(iid, op, lhs, rhs) { return 41; }");
setHook(
"__instrument.postAssignment = function(iid, cookie, op, result, lhs, rhs) { return rhs === undefined ? cookie+1 : result; }");
ASSERT_EQ(17, run("var x; x = 17; x")->getDouble());
ASSERT_EQ(42, run("var x = 1; x ^= undefined; x")->getDouble());
}
} // anonymous namespace
#endif // HERMES_ENABLE_IR_INSTRUMENTATION