1 // Copyright 2021 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 #if !V8_ENABLE_WEBASSEMBLY 6 #error This header should only be included if WebAssembly is enabled. 7 #endif // !V8_ENABLE_WEBASSEMBLY 8 9 #ifndef V8_WASM_INIT_EXPR_INTERFACE_H_ 10 #define V8_WASM_INIT_EXPR_INTERFACE_H_ 11 12 #include "src/wasm/decoder.h" 13 #include "src/wasm/function-body-decoder-impl.h" 14 #include "src/wasm/wasm-value.h" 15 16 namespace v8 { 17 namespace internal { 18 19 class WasmInstanceObject; 20 class JSArrayBuffer; 21 22 namespace wasm { 23 24 // An interface for WasmFullDecoder used to decode initializer expressions. This 25 // interface has two modes: only validation (when {isolate_ == nullptr}), which 26 // is used in module-decoder, and code-generation (when {isolate_ != nullptr}), 27 // which is used in module-instantiate. We merge two distinct functionalities 28 // in one class to reduce the number of WasmFullDecoder instantiations, and thus 29 // V8 binary code size. 30 class InitExprInterface { 31 public: 32 static constexpr Decoder::ValidateFlag validate = Decoder::kFullValidation; 33 static constexpr DecodingMode decoding_mode = kInitExpression; 34 35 struct Value : public ValueBase<validate> { 36 WasmValue runtime_value; 37 38 template <typename... Args> ValueValue39 explicit Value(Args&&... args) V8_NOEXCEPT 40 : ValueBase(std::forward<Args>(args)...) {} 41 }; 42 43 using Control = ControlBase<Value, validate>; 44 using FullDecoder = 45 WasmFullDecoder<validate, InitExprInterface, decoding_mode>; 46 InitExprInterface(const WasmModule * module,Isolate * isolate,Handle<WasmInstanceObject> instance,Handle<FixedArray> tagged_globals,Handle<JSArrayBuffer> untagged_globals)47 InitExprInterface(const WasmModule* module, Isolate* isolate, 48 Handle<WasmInstanceObject> instance, 49 Handle<FixedArray> tagged_globals, 50 Handle<JSArrayBuffer> untagged_globals) 51 : module_(module), 52 outer_module_(nullptr), 53 isolate_(isolate), 54 instance_(instance), 55 tagged_globals_(tagged_globals), 56 untagged_globals_(untagged_globals) { 57 DCHECK_NOT_NULL(isolate); 58 } 59 InitExprInterface(WasmModule * outer_module)60 explicit InitExprInterface(WasmModule* outer_module) 61 : module_(nullptr), outer_module_(outer_module), isolate_(nullptr) {} 62 63 #define EMPTY_INTERFACE_FUNCTION(name, ...) \ 64 V8_INLINE void name(FullDecoder* decoder, ##__VA_ARGS__) {} 65 INTERFACE_META_FUNCTIONS(EMPTY_INTERFACE_FUNCTION) INTERFACE_NON_CONSTANT_FUNCTIONS(EMPTY_INTERFACE_FUNCTION)66 INTERFACE_NON_CONSTANT_FUNCTIONS(EMPTY_INTERFACE_FUNCTION) 67 #undef EMPTY_INTERFACE_FUNCTION 68 69 #define DECLARE_INTERFACE_FUNCTION(name, ...) \ 70 void name(FullDecoder* decoder, ##__VA_ARGS__); 71 INTERFACE_CONSTANT_FUNCTIONS(DECLARE_INTERFACE_FUNCTION) 72 #undef DECLARE_INTERFACE_FUNCTION 73 74 WasmValue result() { 75 DCHECK_NOT_NULL(isolate_); 76 return result_; 77 } end_found()78 bool end_found() { return end_found_; } 79 80 private: 81 byte* GetRawUntaggedGlobalPtr(const WasmGlobal& global); 82 83 bool end_found_ = false; 84 WasmValue result_; 85 const WasmModule* module_; 86 WasmModule* outer_module_; 87 Isolate* isolate_; 88 Handle<WasmInstanceObject> instance_; 89 Handle<FixedArray> tagged_globals_; 90 Handle<JSArrayBuffer> untagged_globals_; 91 }; 92 93 } // namespace wasm 94 } // namespace internal 95 } // namespace v8 96 97 #endif // V8_WASM_INIT_EXPR_INTERFACE_H_ 98