diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-08-12 00:55:40 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-12 21:03:53 +0200 |
commit | 2b35e9f9becd63a90e2772614d0b111fa896a275 (patch) | |
tree | 605b09c12661dd2332474b5d7bbfc5ea111679b0 /Userland/Libraries/LibWasm | |
parent | f0e7e5bbe8871158b79fac9e2e9cae6d3be51933 (diff) | |
download | serenity-2b35e9f9becd63a90e2772614d0b111fa896a275.zip |
LibWasm: Generate Value::type() on the fly instead of storing it
The variant member already contains enough information to give us the
type when needed, so remove the type member and synthesize it when
needed, this allows lots of optimisation opportunaties when copying and
moving Values around.
Diffstat (limited to 'Userland/Libraries/LibWasm')
-rw-r--r-- | Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h | 66 |
1 files changed, 20 insertions, 46 deletions
diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index 659a3bae78..b391e19e18 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -66,37 +66,18 @@ class Value { public: Value() : m_value(0) - , m_type(ValueType::I32) { } using AnyValueType = Variant<i32, i64, float, double, Reference>; explicit Value(AnyValueType value) : m_value(move(value)) - , m_type(ValueType::I32) { - if (m_value.has<i32>()) - m_type = ValueType { ValueType::I32 }; - else if (m_value.has<i64>()) - m_type = ValueType { ValueType::I64 }; - else if (m_value.has<float>()) - m_type = ValueType { ValueType::F32 }; - else if (m_value.has<double>()) - m_type = ValueType { ValueType::F64 }; - else if (m_value.has<Reference>() && m_value.get<Reference>().ref().has<Reference::Func>()) - m_type = ValueType { ValueType::FunctionReference }; - else if (m_value.has<Reference>() && m_value.get<Reference>().ref().has<Reference::Extern>()) - m_type = ValueType { ValueType::ExternReference }; - else if (m_value.has<Reference>()) - m_type = m_value.get<Reference>().ref().get<Reference::Null>().type; - else - VERIFY_NOT_REACHED(); } template<typename T> requires(sizeof(T) == sizeof(u64)) explicit Value(ValueType type, T raw_value) : m_value(0) - , m_type(type) { switch (type.kind()) { case ValueType::Kind::ExternReference: @@ -130,31 +111,10 @@ public: } } - ALWAYS_INLINE Value(Value const& value) - : m_value(AnyValueType { value.m_value }) - , m_type(value.m_type) - { - } - - ALWAYS_INLINE Value(Value&& value) - : m_value(move(value.m_value)) - , m_type(move(value.m_type)) - { - } - - ALWAYS_INLINE Value& operator=(Value&& value) - { - m_value = move(value.m_value); - m_type = move(value.m_type); - return *this; - } - - ALWAYS_INLINE Value& operator=(Value const& value) - { - m_value = value.m_value; - m_type = value.m_type; - return *this; - } + ALWAYS_INLINE Value(Value const& value) = default; + ALWAYS_INLINE Value(Value&& value) = default; + ALWAYS_INLINE Value& operator=(Value&& value) = default; + ALWAYS_INLINE Value& operator=(Value const& value) = default; template<typename T> ALWAYS_INLINE Optional<T> to() @@ -184,12 +144,26 @@ public: return result; } - auto& type() const { return m_type; } + ValueType type() const + { + return ValueType(m_value.visit( + [](i32) { return ValueType::Kind::I32; }, + [](i64) { return ValueType::Kind::I64; }, + [](float) { return ValueType::Kind::F32; }, + [](double) { return ValueType::Kind::F64; }, + [&](Reference const& type) { + return type.ref().visit( + [](Reference::Func const&) { return ValueType::Kind::FunctionReference; }, + [](Reference::Null const& null_type) { + return null_type.type.kind() == ValueType::ExternReference ? ValueType::Kind::NullExternReference : ValueType::Kind::NullFunctionReference; + }, + [](Reference::Extern const&) { return ValueType::Kind::ExternReference; }); + })); + } auto& value() const { return m_value; } private: AnyValueType m_value; - ValueType m_type; }; struct Trap { |