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.
|
|
|
|
|
2017-09-21 02:04:27 +00:00
|
|
|
#ifndef V8_TEST_CCTEST_INTERPRETER_INTERPRETER_TESTER_H_
|
|
|
|
#define V8_TEST_CCTEST_INTERPRETER_INTERPRETER_TESTER_H_
|
|
|
|
|
2021-08-23 13:01:06 +00:00
|
|
|
#include "include/v8-function.h"
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/execution.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles.h"
|
2021-08-23 13:01:06 +00:00
|
|
|
#include "src/init/v8.h"
|
2016-03-22 11:35:09 +00:00
|
|
|
#include "src/interpreter/bytecode-array-builder.h"
|
|
|
|
#include "src/interpreter/interpreter.h"
|
2018-12-17 10:38:41 +00:00
|
|
|
#include "src/objects/feedback-cell.h"
|
2016-03-22 11:35:09 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/test-feedback-vector.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
template <class... A>
|
|
|
|
static MaybeHandle<Object> CallInterpreter(Isolate* isolate,
|
|
|
|
Handle<JSFunction> function,
|
2020-03-16 13:19:33 +00:00
|
|
|
Handle<Object> receiver, A... args) {
|
|
|
|
// Pad the array with an empty handle to ensure that argv size is at least 1.
|
|
|
|
// It avoids MSVC error C2466.
|
|
|
|
Handle<Object> argv[] = {args..., Handle<Object>()};
|
|
|
|
return Execution::Call(isolate, function, receiver, sizeof...(args), argv);
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... A>
|
|
|
|
class InterpreterCallable {
|
|
|
|
public:
|
2020-03-16 13:19:33 +00:00
|
|
|
virtual ~InterpreterCallable() = default;
|
|
|
|
|
|
|
|
FeedbackVector vector() const { return function_->feedback_vector(); }
|
|
|
|
|
|
|
|
protected:
|
2016-03-22 11:35:09 +00:00
|
|
|
InterpreterCallable(Isolate* isolate, Handle<JSFunction> function)
|
|
|
|
: isolate_(isolate), function_(function) {}
|
2020-03-16 13:19:33 +00:00
|
|
|
|
|
|
|
Isolate* isolate_;
|
|
|
|
Handle<JSFunction> function_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class... A>
|
|
|
|
class InterpreterCallableUndefinedReceiver : public InterpreterCallable<A...> {
|
|
|
|
public:
|
|
|
|
InterpreterCallableUndefinedReceiver(Isolate* isolate,
|
|
|
|
Handle<JSFunction> function)
|
|
|
|
: InterpreterCallable<A...>(isolate, function) {}
|
2016-03-22 11:35:09 +00:00
|
|
|
|
|
|
|
MaybeHandle<Object> operator()(A... args) {
|
2020-03-16 13:19:33 +00:00
|
|
|
return CallInterpreter(this->isolate_, this->function_,
|
|
|
|
this->isolate_->factory()->undefined_value(),
|
|
|
|
args...);
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
2020-03-16 13:19:33 +00:00
|
|
|
};
|
2016-03-22 11:35:09 +00:00
|
|
|
|
2020-03-16 13:19:33 +00:00
|
|
|
template <class... A>
|
|
|
|
class InterpreterCallableWithReceiver : public InterpreterCallable<A...> {
|
|
|
|
public:
|
|
|
|
InterpreterCallableWithReceiver(Isolate* isolate, Handle<JSFunction> function)
|
|
|
|
: InterpreterCallable<A...>(isolate, function) {}
|
2017-01-26 10:03:00 +00:00
|
|
|
|
2020-03-16 13:19:33 +00:00
|
|
|
MaybeHandle<Object> operator()(Handle<Object> receiver, A... args) {
|
|
|
|
return CallInterpreter(this->isolate_, this->function_, receiver, args...);
|
|
|
|
}
|
2016-03-22 11:35:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class InterpreterTester {
|
|
|
|
public:
|
|
|
|
InterpreterTester(Isolate* isolate, const char* source,
|
|
|
|
MaybeHandle<BytecodeArray> bytecode,
|
2017-02-07 14:05:02 +00:00
|
|
|
MaybeHandle<FeedbackMetadata> feedback_metadata,
|
2016-03-22 11:35:09 +00:00
|
|
|
const char* filter);
|
|
|
|
|
|
|
|
InterpreterTester(Isolate* isolate, Handle<BytecodeArray> bytecode,
|
2017-02-07 14:05:02 +00:00
|
|
|
MaybeHandle<FeedbackMetadata> feedback_metadata =
|
|
|
|
MaybeHandle<FeedbackMetadata>(),
|
2016-03-22 11:35:09 +00:00
|
|
|
const char* filter = kFunctionName);
|
|
|
|
|
|
|
|
InterpreterTester(Isolate* isolate, const char* source,
|
|
|
|
const char* filter = kFunctionName);
|
|
|
|
|
|
|
|
virtual ~InterpreterTester();
|
2020-11-06 02:39:19 +00:00
|
|
|
InterpreterTester(const InterpreterTester&) = delete;
|
|
|
|
InterpreterTester& operator=(const InterpreterTester&) = delete;
|
2016-03-22 11:35:09 +00:00
|
|
|
|
|
|
|
template <class... A>
|
2020-03-16 13:19:33 +00:00
|
|
|
InterpreterCallableUndefinedReceiver<A...> GetCallable() {
|
|
|
|
return InterpreterCallableUndefinedReceiver<A...>(
|
|
|
|
isolate_, GetBytecodeFunction<A...>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... A>
|
|
|
|
InterpreterCallableWithReceiver<A...> GetCallableWithReceiver() {
|
|
|
|
return InterpreterCallableWithReceiver<A...>(isolate_,
|
|
|
|
GetBytecodeFunction<A...>());
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Local<Message> CheckThrowsReturnMessage();
|
|
|
|
|
|
|
|
static Handle<Object> NewObject(const char* script);
|
|
|
|
|
|
|
|
static Handle<String> GetName(Isolate* isolate, const char* name);
|
|
|
|
|
|
|
|
static std::string SourceForBody(const char* body);
|
|
|
|
|
|
|
|
static std::string function_name();
|
|
|
|
|
2017-09-04 10:47:27 +00:00
|
|
|
static const char kFunctionName[];
|
|
|
|
|
2017-12-13 12:19:44 +00:00
|
|
|
// Expose raw RegisterList construction to tests.
|
|
|
|
static RegisterList NewRegisterList(int first_reg_index, int register_count) {
|
|
|
|
return RegisterList(first_reg_index, register_count);
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:05 +00:00
|
|
|
inline bool HasFeedbackMetadata() { return !feedback_metadata_.is_null(); }
|
|
|
|
|
2016-03-22 11:35:09 +00:00
|
|
|
private:
|
|
|
|
Isolate* isolate_;
|
|
|
|
const char* source_;
|
|
|
|
MaybeHandle<BytecodeArray> bytecode_;
|
2017-02-07 14:05:02 +00:00
|
|
|
MaybeHandle<FeedbackMetadata> feedback_metadata_;
|
2016-03-22 11:35:09 +00:00
|
|
|
|
|
|
|
template <class... A>
|
|
|
|
Handle<JSFunction> GetBytecodeFunction() {
|
|
|
|
Handle<JSFunction> function;
|
2020-06-03 11:14:02 +00:00
|
|
|
IsCompiledScope is_compiled_scope;
|
2016-03-22 11:35:09 +00:00
|
|
|
if (source_) {
|
|
|
|
CompileRun(source_);
|
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Isolate::GetCurrent()->GetCurrentContext();
|
|
|
|
Local<Function> api_function =
|
|
|
|
Local<Function>::Cast(CcTest::global()
|
|
|
|
->Get(context, v8_str(kFunctionName))
|
|
|
|
.ToLocalChecked());
|
|
|
|
function = Handle<JSFunction>::cast(v8::Utils::OpenHandle(*api_function));
|
2020-07-06 13:43:56 +00:00
|
|
|
is_compiled_scope = function->shared().is_compiled_scope(isolate_);
|
2016-03-22 11:35:09 +00:00
|
|
|
} else {
|
|
|
|
int arg_count = sizeof...(A);
|
|
|
|
std::string source("(function " + function_name() + "(");
|
|
|
|
for (int i = 0; i < arg_count; i++) {
|
|
|
|
source += i == 0 ? "a" : ", a";
|
|
|
|
}
|
|
|
|
source += "){})";
|
|
|
|
function = Handle<JSFunction>::cast(v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Function>::Cast(CompileRun(source.c_str()))));
|
2021-12-16 15:33:05 +00:00
|
|
|
function->set_code(*BUILTIN_CODE(isolate_, InterpreterEntryTrampoline));
|
2020-07-06 13:43:56 +00:00
|
|
|
is_compiled_scope = function->shared().is_compiled_scope(isolate_);
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bytecode_.is_null()) {
|
2020-10-05 09:22:13 +00:00
|
|
|
function->shared().set_function_data(*bytecode_.ToHandleChecked(),
|
|
|
|
kReleaseStore);
|
2020-07-06 13:43:56 +00:00
|
|
|
is_compiled_scope = function->shared().is_compiled_scope(isolate_);
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
2018-12-19 13:17:05 +00:00
|
|
|
if (HasFeedbackMetadata()) {
|
2018-12-05 17:31:16 +00:00
|
|
|
function->set_raw_feedback_cell(isolate_->heap()->many_closures_cell());
|
2018-04-06 11:49:15 +00:00
|
|
|
// Set the raw feedback metadata to circumvent checks that we are not
|
|
|
|
// overwriting existing metadata.
|
|
|
|
function->shared().set_raw_outer_scope_info_or_feedback_metadata(
|
2017-01-26 10:03:00 +00:00
|
|
|
*feedback_metadata_.ToHandleChecked());
|
2022-03-01 13:47:02 +00:00
|
|
|
JSFunction::EnsureFeedbackVector(isolate_, function, &is_compiled_scope);
|
2016-03-22 11:35:09 +00:00
|
|
|
}
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2017-09-21 02:04:27 +00:00
|
|
|
|
|
|
|
#endif // V8_TEST_CCTEST_INTERPRETER_INTERPRETER_TESTER_H_
|