| 494 | // Values |
| 495 | |
| 496 | class Val { |
| 497 | ValKind kind_; |
| 498 | union impl { |
| 499 | int32_t i32; |
| 500 | int64_t i64; |
| 501 | float32_t f32; |
| 502 | float64_t f64; |
| 503 | Ref* ref; |
| 504 | } impl_; |
| 505 | |
| 506 | Val(ValKind kind, impl impl) : kind_(kind), impl_(impl) {} |
| 507 | |
| 508 | public: |
| 509 | Val() : kind_(ValKind::EXTERNREF) { impl_.ref = nullptr; } |
| 510 | explicit Val(int32_t i) : kind_(ValKind::I32) { impl_.i32 = i; } |
| 511 | explicit Val(int64_t i) : kind_(ValKind::I64) { impl_.i64 = i; } |
| 512 | explicit Val(float32_t z) : kind_(ValKind::F32) { impl_.f32 = z; } |
| 513 | explicit Val(float64_t z) : kind_(ValKind::F64) { impl_.f64 = z; } |
| 514 | explicit Val(own<Ref>&& r) : kind_(ValKind::EXTERNREF) { impl_.ref = r.release(); } |
| 515 | |
| 516 | Val(Val&& that) : kind_(that.kind_), impl_(that.impl_) { |
| 517 | if (is_ref()) that.impl_.ref = nullptr; |
| 518 | } |
| 519 | |
| 520 | ~Val() { |
| 521 | reset(); |
| 522 | } |
| 523 | |
| 524 | auto is_num() const -> bool { return wasm::is_num(kind_); } |
| 525 | auto is_ref() const -> bool { return wasm::is_ref(kind_); } |
| 526 | |
| 527 | static auto i32(int32_t x) -> Val { return Val(x); } |
| 528 | static auto i64(int64_t x) -> Val { return Val(x); } |
| 529 | static auto f32(float32_t x) -> Val { return Val(x); } |
| 530 | static auto f64(float64_t x) -> Val { return Val(x); } |
| 531 | static auto ref(own<Ref>&& x) -> Val { return Val(std::move(x)); } |
| 532 | template<class T> inline static auto make(T x) -> Val; |
| 533 | template<class T> inline static auto make(own<T>&& x) -> Val; |
| 534 | |
| 535 | void reset() { |
| 536 | if (is_ref() && impl_.ref) { |
| 537 | destroyer()(impl_.ref); |
| 538 | impl_.ref = nullptr; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void reset(Val& that) { |
| 543 | reset(); |
| 544 | kind_ = that.kind_; |
| 545 | impl_ = that.impl_; |
| 546 | if (is_ref()) that.impl_.ref = nullptr; |
| 547 | } |
| 548 | |
| 549 | auto operator=(Val&& that) -> Val& { |
| 550 | reset(that); |
| 551 | return *this; |
| 552 | } |
| 553 | |