OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "src/execution.h" |
| 8 #include "src/handles.h" |
| 9 #include "src/interpreter/bytecode-array-builder.h" |
| 10 #include "src/interpreter/interpreter.h" |
| 11 #include "test/cctest/cctest.h" |
| 12 #include "test/cctest/test-feedback-vector.h" |
| 13 |
| 14 namespace v8 { |
| 15 namespace internal { |
| 16 namespace interpreter { |
| 17 |
| 18 MaybeHandle<Object> CallInterpreter(Isolate* isolate, |
| 19 Handle<JSFunction> function); |
| 20 template <class... A> |
| 21 static MaybeHandle<Object> CallInterpreter(Isolate* isolate, |
| 22 Handle<JSFunction> function, |
| 23 A... args) { |
| 24 Handle<Object> argv[] = {args...}; |
| 25 return Execution::Call(isolate, function, |
| 26 isolate->factory()->undefined_value(), sizeof...(args), |
| 27 argv); |
| 28 } |
| 29 |
| 30 template <class... A> |
| 31 class InterpreterCallable { |
| 32 public: |
| 33 InterpreterCallable(Isolate* isolate, Handle<JSFunction> function) |
| 34 : isolate_(isolate), function_(function) {} |
| 35 virtual ~InterpreterCallable() {} |
| 36 |
| 37 MaybeHandle<Object> operator()(A... args) { |
| 38 return CallInterpreter(isolate_, function_, args...); |
| 39 } |
| 40 |
| 41 private: |
| 42 Isolate* isolate_; |
| 43 Handle<JSFunction> function_; |
| 44 }; |
| 45 |
| 46 constexpr char kFunctionName[] = "f"; |
| 47 |
| 48 class InterpreterTester { |
| 49 public: |
| 50 InterpreterTester(Isolate* isolate, const char* source, |
| 51 MaybeHandle<BytecodeArray> bytecode, |
| 52 MaybeHandle<TypeFeedbackVector> feedback_vector, |
| 53 const char* filter); |
| 54 |
| 55 InterpreterTester(Isolate* isolate, Handle<BytecodeArray> bytecode, |
| 56 MaybeHandle<TypeFeedbackVector> feedback_vector = |
| 57 MaybeHandle<TypeFeedbackVector>(), |
| 58 const char* filter = kFunctionName); |
| 59 |
| 60 InterpreterTester(Isolate* isolate, const char* source, |
| 61 const char* filter = kFunctionName); |
| 62 |
| 63 virtual ~InterpreterTester(); |
| 64 |
| 65 template <class... A> |
| 66 InterpreterCallable<A...> GetCallable() { |
| 67 return InterpreterCallable<A...>(isolate_, GetBytecodeFunction<A...>()); |
| 68 } |
| 69 |
| 70 Local<Message> CheckThrowsReturnMessage(); |
| 71 |
| 72 static Handle<Object> NewObject(const char* script); |
| 73 |
| 74 static Handle<String> GetName(Isolate* isolate, const char* name); |
| 75 |
| 76 static std::string SourceForBody(const char* body); |
| 77 |
| 78 static std::string function_name(); |
| 79 |
| 80 private: |
| 81 Isolate* isolate_; |
| 82 const char* source_; |
| 83 MaybeHandle<BytecodeArray> bytecode_; |
| 84 MaybeHandle<TypeFeedbackVector> feedback_vector_; |
| 85 |
| 86 template <class... A> |
| 87 Handle<JSFunction> GetBytecodeFunction() { |
| 88 Handle<JSFunction> function; |
| 89 if (source_) { |
| 90 CompileRun(source_); |
| 91 v8::Local<v8::Context> context = |
| 92 v8::Isolate::GetCurrent()->GetCurrentContext(); |
| 93 Local<Function> api_function = |
| 94 Local<Function>::Cast(CcTest::global() |
| 95 ->Get(context, v8_str(kFunctionName)) |
| 96 .ToLocalChecked()); |
| 97 function = Handle<JSFunction>::cast(v8::Utils::OpenHandle(*api_function)); |
| 98 } else { |
| 99 int arg_count = sizeof...(A); |
| 100 std::string source("(function " + function_name() + "("); |
| 101 for (int i = 0; i < arg_count; i++) { |
| 102 source += i == 0 ? "a" : ", a"; |
| 103 } |
| 104 source += "){})"; |
| 105 function = Handle<JSFunction>::cast(v8::Utils::OpenHandle( |
| 106 *v8::Local<v8::Function>::Cast(CompileRun(source.c_str())))); |
| 107 function->ReplaceCode( |
| 108 *isolate_->builtins()->InterpreterEntryTrampoline()); |
| 109 } |
| 110 |
| 111 if (!bytecode_.is_null()) { |
| 112 function->shared()->set_function_data(*bytecode_.ToHandleChecked()); |
| 113 } |
| 114 if (!feedback_vector_.is_null()) { |
| 115 function->shared()->set_feedback_vector( |
| 116 *feedback_vector_.ToHandleChecked()); |
| 117 } |
| 118 return function; |
| 119 } |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(InterpreterTester); |
| 122 }; |
| 123 |
| 124 } // namespace interpreter |
| 125 } // namespace internal |
| 126 } // namespace v8 |
OLD | NEW |