Line data Source code
1 : // Copyright 2016 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 : #ifndef V8_WASM_MODULE_RUNNER_H_
6 : #define V8_WASM_MODULE_RUNNER_H_
7 :
8 : #include "src/isolate.h"
9 : #include "src/objects.h"
10 : #include "src/wasm/wasm-interpreter.h"
11 : #include "src/wasm/wasm-module.h"
12 : #include "src/wasm/wasm-objects.h"
13 : #include "src/wasm/wasm-result.h"
14 : #include "src/zone/zone.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : template <typename T>
20 : class Handle;
21 :
22 : template <typename T>
23 : class MaybeHandle;
24 :
25 : namespace wasm {
26 : namespace testing {
27 :
28 : // Decodes the given encoded module.
29 : std::shared_ptr<WasmModule> DecodeWasmModuleForTesting(
30 : Isolate* isolate, ErrorThrower* thrower, const byte* module_start,
31 : const byte* module_end, ModuleOrigin origin, bool verify_functions = false);
32 :
33 : // Returns a MaybeHandle to the JsToWasm wrapper of the wasm function exported
34 : // with the given name by the provided instance.
35 : MaybeHandle<WasmExportedFunction> GetExportedFunction(
36 : Isolate* isolate, Handle<WasmInstanceObject> instance, const char* name);
37 :
38 : // Call an exported wasm function by name. Returns -1 if the export does not
39 : // exist or throws an error. Errors are cleared from the isolate before
40 : // returning.
41 : int32_t CallWasmFunctionForTesting(Isolate* isolate,
42 : Handle<WasmInstanceObject> instance,
43 : ErrorThrower* thrower, const char* name,
44 : int argc, Handle<Object> argv[]);
45 :
46 : // Interprets an exported wasm function by name. Returns false if it was not
47 : // possible to execute the function (e.g. because it does not exist), or if the
48 : // interpretation does not finish after kMaxNumSteps. Otherwise returns true.
49 : // The arguments array is extended with default values if necessary.
50 : bool InterpretWasmModuleForTesting(Isolate* isolate,
51 : Handle<WasmInstanceObject> instance,
52 : const char* name, size_t argc,
53 : WasmValue* args);
54 :
55 : // Decode, verify, and run the function labeled "main" in the
56 : // given encoded module. The module should have no imports.
57 : int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
58 : const byte* module_end);
59 :
60 : // Decode, compile, and instantiate the given module with no imports.
61 : MaybeHandle<WasmInstanceObject> CompileAndInstantiateForTesting(
62 : Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes);
63 :
64 : class WasmInterpretationResult {
65 : public:
66 : static WasmInterpretationResult Stopped() { return {kStopped, 0, false}; }
67 : static WasmInterpretationResult Trapped(bool possible_nondeterminism) {
68 : return {kTrapped, 0, possible_nondeterminism};
69 : }
70 : static WasmInterpretationResult Finished(int32_t result,
71 : bool possible_nondeterminism) {
72 : return {kFinished, result, possible_nondeterminism};
73 : }
74 :
75 : bool stopped() const { return status_ == kStopped; }
76 1 : bool trapped() const { return status_ == kTrapped; }
77 : bool finished() const { return status_ == kFinished; }
78 :
79 : int32_t result() const {
80 : DCHECK_EQ(status_, kFinished);
81 : return result_;
82 : }
83 :
84 : bool possible_nondeterminism() const { return possible_nondeterminism_; }
85 :
86 : private:
87 : enum Status { kFinished, kTrapped, kStopped };
88 :
89 : const Status status_;
90 : const int32_t result_;
91 : const bool possible_nondeterminism_;
92 :
93 : WasmInterpretationResult(Status status, int32_t result,
94 : bool possible_nondeterminism)
95 : : status_(status),
96 : result_(result),
97 1 : possible_nondeterminism_(possible_nondeterminism) {}
98 : };
99 :
100 : // Interprets the given module, starting at the function specified by
101 : // {function_index}. The return type of the function has to be int32. The module
102 : // should not have any imports or exports
103 : WasmInterpretationResult InterpretWasmModule(
104 : Isolate* isolate, Handle<WasmInstanceObject> instance,
105 : int32_t function_index, WasmValue* args);
106 :
107 : // Runs the module instance with arguments.
108 : int32_t RunWasmModuleForTesting(Isolate* isolate,
109 : Handle<WasmInstanceObject> instance, int argc,
110 : Handle<Object> argv[]);
111 :
112 : // Install function map, module symbol for testing
113 : void SetupIsolateForWasmModule(Isolate* isolate);
114 :
115 : } // namespace testing
116 : } // namespace wasm
117 : } // namespace internal
118 : } // namespace v8
119 :
120 : #endif // V8_WASM_MODULE_RUNNER_H_
|