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
|
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/init/v8.h"
#include "src/api/api-inl.h"
#include "src/heap/heap-inl.h"
#include "src/interpreter/interpreter-intrinsics.h"
#include "src/objects/objects-inl.h"
#include "test/cctest/interpreter/interpreter-tester.h"
namespace v8 {
namespace internal {
namespace interpreter {
namespace {
class InvokeIntrinsicHelper {
public:
InvokeIntrinsicHelper(Isolate* isolate, Zone* zone,
Runtime::FunctionId function_id)
: isolate_(isolate),
zone_(zone),
factory_(isolate->factory()),
function_id_(function_id) {}
template <class... A>
Handle<Object> Invoke(A... args) {
CHECK(IntrinsicsHelper::IsSupported(function_id_));
int parameter_count = sizeof...(args);
#ifdef V8_REVERSE_JSARGS
// Move the parameter to locals, since the order of the
// arguments in the stack is reversed.
BytecodeArrayBuilder builder(zone_, parameter_count + 1, parameter_count,
nullptr);
for (int i = 0; i < parameter_count; i++) {
builder.MoveRegister(builder.Parameter(i), builder.Local(i));
}
RegisterList reg_list =
InterpreterTester::NewRegisterList(0, parameter_count);
#else
// Add the receiver in the parameter count.
BytecodeArrayBuilder builder(zone_, parameter_count + 1, 0, nullptr);
RegisterList reg_list = InterpreterTester::NewRegisterList(
builder.Parameter(0).index(), parameter_count);
#endif
builder.CallRuntime(function_id_, reg_list).Return();
InterpreterTester tester(isolate_, builder.ToBytecodeArray(isolate_));
auto callable = tester.GetCallable<A...>();
return callable(args...).ToHandleChecked();
}
Handle<Object> NewObject(const char* script) {
return v8::Utils::OpenHandle(*CompileRun(script));
}
Handle<Object> Undefined() { return factory_->undefined_value(); }
Handle<Object> Null() { return factory_->null_value(); }
private:
Isolate* isolate_;
Zone* zone_;
Factory* factory_;
Runtime::FunctionId function_id_;
};
} // namespace
TEST(IsJSReceiver) {
HandleAndZoneScope handles;
InvokeIntrinsicHelper helper(handles.main_isolate(), handles.main_zone(),
Runtime::kInlineIsJSReceiver);
Factory* factory = handles.main_isolate()->factory();
CHECK_EQ(*factory->true_value(),
*helper.Invoke(helper.NewObject("new Date()")));
CHECK_EQ(*factory->true_value(),
*helper.Invoke(helper.NewObject("(function() {})")));
CHECK_EQ(*factory->true_value(), *helper.Invoke(helper.NewObject("([1])")));
CHECK_EQ(*factory->true_value(), *helper.Invoke(helper.NewObject("({})")));
CHECK_EQ(*factory->true_value(), *helper.Invoke(helper.NewObject("(/x/)")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.Undefined()));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.Null()));
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("'string'")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("42")));
}
TEST(IsArray) {
HandleAndZoneScope handles;
InvokeIntrinsicHelper helper(handles.main_isolate(), handles.main_zone(),
Runtime::kInlineIsArray);
Factory* factory = handles.main_isolate()->factory();
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("new Date()")));
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("(function() {})")));
CHECK_EQ(*factory->true_value(), *helper.Invoke(helper.NewObject("([1])")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("({})")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("(/x/)")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.Undefined()));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.Null()));
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("'string'")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("42")));
}
TEST(IsSmi) {
HandleAndZoneScope handles;
InvokeIntrinsicHelper helper(handles.main_isolate(), handles.main_zone(),
Runtime::kInlineIsSmi);
Factory* factory = handles.main_isolate()->factory();
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("new Date()")));
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("(function() {})")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("([1])")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("({})")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("(/x/)")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.Undefined()));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.Null()));
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("'string'")));
CHECK_EQ(*factory->false_value(), *helper.Invoke(helper.NewObject("42.2")));
CHECK_EQ(*factory->false_value(),
*helper.Invoke(helper.NewObject("4294967297")));
CHECK_EQ(*factory->true_value(), *helper.Invoke(helper.NewObject("42")));
}
TEST(Call) {
HandleAndZoneScope handles;
Isolate* isolate = handles.main_isolate();
Factory* factory = isolate->factory();
InvokeIntrinsicHelper helper(isolate, handles.main_zone(),
Runtime::kInlineCall);
CHECK_EQ(Smi::FromInt(20),
*helper.Invoke(helper.NewObject("(function() { return this.x; })"),
helper.NewObject("({ x: 20 })")));
CHECK_EQ(Smi::FromInt(50),
*helper.Invoke(helper.NewObject("(function(arg1) { return arg1; })"),
factory->undefined_value(),
handle(Smi::FromInt(50), isolate)));
CHECK_EQ(
Smi::FromInt(20),
*helper.Invoke(
helper.NewObject("(function(a, b, c) { return a + b + c; })"),
factory->undefined_value(), handle(Smi::FromInt(10), isolate),
handle(Smi::FromInt(7), isolate), handle(Smi::FromInt(3), isolate)));
}
TEST(IntrinsicAsStubCall) {
HandleAndZoneScope handles;
Isolate* isolate = handles.main_isolate();
Factory* factory = isolate->factory();
InvokeIntrinsicHelper has_property_helper(isolate, handles.main_zone(),
Runtime::kInlineHasProperty);
CHECK_EQ(
*factory->true_value(),
*has_property_helper.Invoke(has_property_helper.NewObject("({ x: 20 })"),
has_property_helper.NewObject("'x'")));
CHECK_EQ(
*factory->false_value(),
*has_property_helper.Invoke(has_property_helper.NewObject("({ x: 20 })"),
has_property_helper.NewObject("'y'")));
}
} // namespace interpreter
} // namespace internal
} // namespace v8
|