2015-02-27 06:56:44 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-03-18 11:42:36 +00:00
|
|
|
#include "src/bootstrapper.h"
|
2017-07-27 11:35:49 +00:00
|
|
|
#include "src/callable.h"
|
2015-02-27 06:56:44 +00:00
|
|
|
#include "src/code-stubs.h"
|
2016-08-31 08:49:14 +00:00
|
|
|
#include "src/compilation-info.h"
|
2015-03-18 11:42:36 +00:00
|
|
|
#include "src/compiler/common-operator.h"
|
2015-02-27 06:56:44 +00:00
|
|
|
#include "src/compiler/graph.h"
|
2015-05-11 11:45:02 +00:00
|
|
|
#include "src/compiler/js-graph.h"
|
|
|
|
#include "src/compiler/js-operator.h"
|
2015-02-27 06:56:44 +00:00
|
|
|
#include "src/compiler/linkage.h"
|
2015-05-11 11:45:02 +00:00
|
|
|
#include "src/compiler/machine-operator.h"
|
2015-02-27 06:56:44 +00:00
|
|
|
#include "src/compiler/pipeline.h"
|
2017-02-23 11:46:29 +00:00
|
|
|
#include "src/objects-inl.h"
|
2015-02-27 06:56:44 +00:00
|
|
|
#include "test/cctest/compiler/function-tester.h"
|
|
|
|
|
2015-10-30 09:16:26 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
2015-02-27 06:56:44 +00:00
|
|
|
|
2017-07-19 13:58:55 +00:00
|
|
|
class StubTester {
|
|
|
|
public:
|
|
|
|
StubTester(Isolate* isolate, Zone* zone, CodeStub* stub)
|
|
|
|
: zone_(zone),
|
|
|
|
info_(ArrayVector("test"), isolate, zone,
|
|
|
|
Code::ComputeFlags(Code::HANDLER)),
|
|
|
|
interface_descriptor_(stub->GetCallInterfaceDescriptor()),
|
|
|
|
descriptor_(Linkage::GetStubCallDescriptor(
|
|
|
|
isolate, zone, interface_descriptor_,
|
|
|
|
stub->GetStackParameterCount(), CallDescriptor::kNoFlags,
|
|
|
|
Operator::kNoProperties)),
|
|
|
|
graph_(zone_),
|
|
|
|
common_(zone_),
|
2017-07-27 11:35:49 +00:00
|
|
|
tester_(InitializeFunctionTester(stub->GetCode()),
|
|
|
|
GetParameterCountWithContext()) {}
|
|
|
|
|
|
|
|
StubTester(Isolate* isolate, Zone* zone, Builtins::Name name)
|
|
|
|
: zone_(zone),
|
|
|
|
info_(ArrayVector("test"), isolate, zone,
|
|
|
|
Code::ComputeFlags(Code::HANDLER)),
|
|
|
|
interface_descriptor_(
|
|
|
|
Builtins::CallableFor(isolate, name).descriptor()),
|
|
|
|
descriptor_(Linkage::GetStubCallDescriptor(
|
|
|
|
isolate, zone, interface_descriptor_,
|
|
|
|
interface_descriptor_.GetStackParameterCount(),
|
|
|
|
CallDescriptor::kNoFlags, Operator::kNoProperties)),
|
|
|
|
graph_(zone_),
|
|
|
|
common_(zone_),
|
|
|
|
tester_(InitializeFunctionTester(
|
|
|
|
Handle<Code>(isolate->builtins()->builtin(name))),
|
|
|
|
GetParameterCountWithContext()) {}
|
2017-07-19 13:58:55 +00:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
Handle<Object> Call(Args... args) {
|
|
|
|
DCHECK_EQ(interface_descriptor_.GetParameterCount(), sizeof...(args));
|
2017-07-27 11:35:49 +00:00
|
|
|
MaybeHandle<Object> result =
|
|
|
|
tester_.Call(args..., Handle<HeapObject>(tester_.function->context()))
|
|
|
|
.ToHandleChecked();
|
2017-07-19 13:58:55 +00:00
|
|
|
return result.ToHandleChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionTester& ft() { return tester_; }
|
|
|
|
|
|
|
|
private:
|
2017-07-27 11:35:49 +00:00
|
|
|
Graph* InitializeFunctionTester(Handle<Code> stub) {
|
|
|
|
// Add target, effect and control.
|
|
|
|
int node_count = GetParameterCountWithContext() + 3;
|
|
|
|
// Add extra inputs for the JSFunction parameter and the receiver (which for
|
|
|
|
// the tester is always undefined) to the start node.
|
|
|
|
Node* start =
|
|
|
|
graph_.NewNode(common_.Start(GetParameterCountWithContext() + 2));
|
2017-07-19 13:58:55 +00:00
|
|
|
Node** node_array = zone_->NewArray<Node*>(node_count);
|
2017-07-27 11:35:49 +00:00
|
|
|
node_array[0] = graph_.NewNode(common_.HeapConstant(stub));
|
|
|
|
for (int i = 0; i < GetParameterCountWithContext(); ++i) {
|
2017-07-19 13:58:55 +00:00
|
|
|
CHECK(IsAnyTagged(descriptor_->GetParameterType(i).representation()));
|
|
|
|
node_array[i + 1] = graph_.NewNode(common_.Parameter(i + 1), start);
|
|
|
|
}
|
2017-07-27 11:35:49 +00:00
|
|
|
node_array[node_count - 2] = start;
|
|
|
|
node_array[node_count - 1] = start;
|
2017-07-19 13:58:55 +00:00
|
|
|
Node* call =
|
|
|
|
graph_.NewNode(common_.Call(descriptor_), node_count, &node_array[0]);
|
|
|
|
|
|
|
|
Node* zero = graph_.NewNode(common_.Int32Constant(0));
|
|
|
|
Node* ret = graph_.NewNode(common_.Return(), zero, call, call, start);
|
|
|
|
Node* end = graph_.NewNode(common_.End(1), ret);
|
|
|
|
graph_.SetStart(start);
|
|
|
|
graph_.SetEnd(end);
|
|
|
|
return &graph_;
|
|
|
|
}
|
|
|
|
|
2017-07-27 11:35:49 +00:00
|
|
|
int GetParameterCountWithContext() {
|
|
|
|
return interface_descriptor_.GetParameterCount() + 1;
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:58:55 +00:00
|
|
|
Zone* zone_;
|
|
|
|
CompilationInfo info_;
|
|
|
|
CallInterfaceDescriptor interface_descriptor_;
|
|
|
|
CallDescriptor* descriptor_;
|
|
|
|
Graph graph_;
|
|
|
|
CommonOperatorBuilder common_;
|
|
|
|
FunctionTester tester_;
|
|
|
|
};
|
2015-02-27 06:56:44 +00:00
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
TEST(RunStringLengthStub) {
|
2015-02-27 06:56:44 +00:00
|
|
|
HandleAndZoneScope scope;
|
|
|
|
Isolate* isolate = scope.main_isolate();
|
|
|
|
Zone* zone = scope.main_zone();
|
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
StringLengthStub stub(isolate);
|
2017-07-19 13:58:55 +00:00
|
|
|
StubTester tester(isolate, zone, &stub);
|
2015-02-27 06:56:44 +00:00
|
|
|
|
|
|
|
// Actuall call through to the stub, verifying its result.
|
|
|
|
const char* testString = "Und das Lamm schrie HURZ!";
|
2017-07-19 13:58:55 +00:00
|
|
|
Handle<Object> receiverArg =
|
|
|
|
Object::ToObject(isolate, tester.ft().Val(testString)).ToHandleChecked();
|
|
|
|
Handle<Object> nameArg = tester.ft().Val("length");
|
|
|
|
Handle<Object> slot = tester.ft().Val(0.0);
|
|
|
|
Handle<Object> vector = tester.ft().Val(0.0);
|
|
|
|
Handle<Object> result = tester.Call(receiverArg, nameArg, slot, vector);
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(static_cast<int>(strlen(testString)), Smi::ToInt(*result));
|
2015-02-27 06:56:44 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:31:41 +00:00
|
|
|
|
2015-10-30 09:16:26 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|