2016-03-22 11:35:09 +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.
|
|
|
|
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2016-03-22 11:35:09 +00:00
|
|
|
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/heap/heap-inl.h"
|
2016-03-22 11:35:09 +00:00
|
|
|
#include "src/interpreter/interpreter-intrinsics.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2016-03-22 11:35:09 +00:00
|
|
|
#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_));
|
2020-03-19 14:33:26 +00:00
|
|
|
int parameter_count = sizeof...(args);
|
|
|
|
// 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);
|
2016-09-30 09:02:59 +00:00
|
|
|
builder.CallRuntime(function_id_, reg_list).Return();
|
2016-08-18 13:42:05 +00:00
|
|
|
InterpreterTester tester(isolate_, builder.ToBytecodeArray(isolate_));
|
2016-05-27 22:29:01 +00:00
|
|
|
auto callable = tester.GetCallable<A...>();
|
2016-03-22 11:35:09 +00:00
|
|
|
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")));
|
2016-06-03 10:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")));
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 22:29:01 +00:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2016-06-22 12:22:30 +00:00
|
|
|
TEST(IntrinsicAsStubCall) {
|
|
|
|
HandleAndZoneScope handles;
|
|
|
|
Isolate* isolate = handles.main_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
|
|
|
|
InvokeIntrinsicHelper has_property_helper(isolate, handles.main_zone(),
|
|
|
|
Runtime::kInlineHasProperty);
|
2018-08-09 11:44:32 +00:00
|
|
|
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'")));
|
2016-06-22 12:22:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 11:35:09 +00:00
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|