1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
|
// WebAssembly C++ API
#ifndef WASM_HH
#define WASM_HH
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <new>
#include <limits>
#include <string>
#ifndef WASM_API_EXTERN
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(LIBWASM_STATIC)
#define WASM_API_EXTERN __declspec(dllimport)
#else
#define WASM_API_EXTERN
#endif
#endif
///////////////////////////////////////////////////////////////////////////////
// Auxiliaries
// Machine types
static_assert(sizeof(float) == sizeof(int32_t), "incompatible float type");
static_assert(sizeof(double) == sizeof(int64_t), "incompatible double type");
static_assert(sizeof(intptr_t) == sizeof(int32_t) ||
sizeof(intptr_t) == sizeof(int64_t), "incompatible pointer type");
using byte_t = char;
using float32_t = float;
using float64_t = double;
namespace wasm {
// Vectors
template<class T>
class vec {
static const size_t invalid_size = SIZE_MAX;
size_t size_;
std::unique_ptr<T[]> data_;
#ifdef WASM_API_DEBUG
WASM_API_EXTERN void make_data();
WASM_API_EXTERN void free_data();
#else
void make_data() {}
void free_data() {}
#endif
vec(size_t size) : vec(size, size ? new(std::nothrow) T[size] : nullptr) {
make_data();
}
vec(size_t size, T* data) : size_(size), data_(data) {
assert(!!size_ == !!data_ || size_ == invalid_size);
}
public:
using elem_type = T;
vec(vec<T>&& that) : vec(that.size_, that.data_.release()) {
that.size_ = invalid_size;
}
~vec() {
free_data();
}
operator bool() const {
return bool(size_ != invalid_size);
}
auto size() const -> size_t {
return size_;
}
auto get() const -> const T* {
return data_.get();
}
auto get() -> T* {
return data_.get();
}
auto release() -> T* {
size_ = invalid_size;
return data_.release();
}
void reset() {
free_data();
size_ = invalid_size;
data_.reset();
}
void reset(vec& that) {
free_data();
size_ = that.size_;
data_.reset(that.data_.release());
that.size_ = invalid_size;
}
auto operator=(vec&& that) -> vec& {
reset(that);
return *this;
}
auto operator[](size_t i) -> T& {
assert(i < size_);
return data_[i];
}
auto operator[](size_t i) const -> const T& {
assert(i < size_);
return data_[i];
}
auto copy() const -> vec {
auto v = vec(size_);
if (v) for (size_t i = 0; i < size_; ++i) v.data_[i] = data_[i];
return v;
}
// TODO: This can't be used for e.g. vec<Val>
auto deep_copy() const -> vec {
auto v = vec(size_);
if (v) for (size_t i = 0; i < size_; ++i) v.data_[i] = data_[i]->copy();
return v;
}
static auto make_uninitialized(size_t size = 0) -> vec {
return vec(size);
}
static auto make(size_t size, T init[]) -> vec {
auto v = vec(size);
if (v) for (size_t i = 0; i < size; ++i) v.data_[i] = std::move(init[i]);
return v;
}
static auto make(std::string s) -> vec<char> {
auto v = vec(s.length());
if (v) std::strncpy(v.get(), s.data(), s.length());
return v;
}
static auto make_nt(std::string s) -> vec<char> {
auto v = vec(s.length() + 1);
if (v) std::strcpy(v.get(), s.data());
return v;
}
// TODO(mvsc): MVSC requires this special case:
static auto make() -> vec {
return vec(0);
}
template<class... Ts>
static auto make(Ts&&... args) -> vec {
T data[] = { std::forward<Ts>(args)... };
return make(sizeof...(Ts), data);
}
static auto adopt(size_t size, T data[]) -> vec {
return vec(size, data);
}
static auto invalid() -> vec {
return vec(invalid_size, nullptr);
}
};
// Ownership
class destroyer {
public:
template <typename T>
void operator()(T* ptr) {
ptr->destroy();
}
};
template<class T> using own = std::unique_ptr<T, destroyer>;
template<class T> using ownvec = vec<own<T>>;
template<class T> own<T> make_own(T* ptr) {
return own<T>(ptr);
}
///////////////////////////////////////////////////////////////////////////////
// Runtime Environment
// Configuration
class WASM_API_EXTERN Config {
friend class destroyer;
void destroy();
protected:
Config() = default;
~Config() = default;
public:
static auto make() -> own<Config>;
// Implementations may provide custom methods for manipulating Configs.
};
// Engine
class WASM_API_EXTERN Engine {
friend class destroyer;
void destroy();
protected:
Engine() = default;
~Engine() = default;
public:
static auto make(own<Config>&& = Config::make()) -> own<Engine>;
};
// Store
class WASM_API_EXTERN Store {
friend class destroyer;
void destroy();
protected:
Store() = default;
~Store() = default;
public:
static auto make(Engine*) -> own<Store>;
};
///////////////////////////////////////////////////////////////////////////////
// Type Representations
// Type attributes
enum class Mutability : uint8_t { CONST, VAR };
struct Limits {
uint32_t min;
uint32_t max;
Limits(uint32_t min, uint32_t max = std::numeric_limits<uint32_t>::max()) :
min(min), max(max) {}
};
// Value Types
enum class ValKind : uint8_t {
I32, I64, F32, F64,
EXTERNREF = 128, FUNCREF,
};
inline bool is_num(ValKind k) { return k < ValKind::EXTERNREF; }
inline bool is_ref(ValKind k) { return k >= ValKind::EXTERNREF; }
class WASM_API_EXTERN ValType {
friend class destroyer;
void destroy();
protected:
ValType() = default;
~ValType() = default;
public:
static auto make(ValKind) -> own<ValType>;
auto copy() const -> own<ValType>;
auto kind() const -> ValKind;
auto is_num() const -> bool { return wasm::is_num(kind()); }
auto is_ref() const -> bool { return wasm::is_ref(kind()); }
};
// External Types
enum class ExternKind : uint8_t {
FUNC, GLOBAL, TABLE, MEMORY
};
class FuncType;
class GlobalType;
class TableType;
class MemoryType;
class WASM_API_EXTERN ExternType {
friend class destroyer;
void destroy();
protected:
ExternType() = default;
~ExternType() = default;
public:
auto copy() const-> own<ExternType>;
auto kind() const -> ExternKind;
auto func() -> FuncType*;
auto global() -> GlobalType*;
auto table() -> TableType*;
auto memory() -> MemoryType*;
auto func() const -> const FuncType*;
auto global() const -> const GlobalType*;
auto table() const -> const TableType*;
auto memory() const -> const MemoryType*;
};
// Function Types
class WASM_API_EXTERN FuncType : public ExternType {
friend class destroyer;
void destroy();
protected:
FuncType() = default;
~FuncType() = default;
public:
static auto make(
ownvec<ValType>&& params = ownvec<ValType>::make(),
ownvec<ValType>&& results = ownvec<ValType>::make()
) -> own<FuncType>;
auto copy() const -> own<FuncType>;
auto params() const -> const ownvec<ValType>&;
auto results() const -> const ownvec<ValType>&;
};
// Global Types
class WASM_API_EXTERN GlobalType : public ExternType {
friend class destroyer;
void destroy();
protected:
GlobalType() = default;
~GlobalType() = default;
public:
static auto make(own<ValType>&&, Mutability) -> own<GlobalType>;
auto copy() const -> own<GlobalType>;
auto content() const -> const ValType*;
auto mutability() const -> Mutability;
};
// Table Types
class WASM_API_EXTERN TableType : public ExternType {
friend class destroyer;
void destroy();
protected:
TableType() = default;
~TableType() = default;
public:
static auto make(own<ValType>&&, Limits) -> own<TableType>;
auto copy() const -> own<TableType>;
auto element() const -> const ValType*;
auto limits() const -> const Limits&;
};
// Memory Types
class WASM_API_EXTERN MemoryType : public ExternType {
friend class destroyer;
void destroy();
protected:
MemoryType() = default;
~MemoryType() = default;
public:
static auto make(Limits) -> own<MemoryType>;
auto copy() const -> own<MemoryType>;
auto limits() const -> const Limits&;
};
// Import Types
using Name = vec<byte_t>;
class WASM_API_EXTERN ImportType {
friend class destroyer;
void destroy();
protected:
ImportType() = default;
~ImportType() = default;
public:
static auto make(Name&& module, Name&& name, own<ExternType>&&) ->
own<ImportType>;
auto copy() const -> own<ImportType>;
auto module() const -> const Name&;
auto name() const -> const Name&;
auto type() const -> const ExternType*;
};
// Export Types
class WASM_API_EXTERN ExportType {
friend class destroyer;
void destroy();
protected:
ExportType() = default;
~ExportType() = default;
public:
static auto make(Name&&, own<ExternType>&&) -> own<ExportType>;
auto copy() const -> own<ExportType>;
auto name() const -> const Name&;
auto type() const -> const ExternType*;
};
///////////////////////////////////////////////////////////////////////////////
// Runtime Objects
// References
class WASM_API_EXTERN Ref {
friend class destroyer;
void destroy();
protected:
Ref() = default;
~Ref() = default;
public:
auto copy() const -> own<Ref>;
auto same(const Ref*) const -> bool;
auto get_host_info() const -> void*;
void set_host_info(void* info, void (*finalizer)(void*) = nullptr);
};
// Values
class Val {
ValKind kind_;
union impl {
int32_t i32;
int64_t i64;
float32_t f32;
float64_t f64;
Ref* ref;
} impl_;
Val(ValKind kind, impl impl) : kind_(kind), impl_(impl) {}
public:
Val() : kind_(ValKind::EXTERNREF) { impl_.ref = nullptr; }
explicit Val(int32_t i) : kind_(ValKind::I32) { impl_.i32 = i; }
explicit Val(int64_t i) : kind_(ValKind::I64) { impl_.i64 = i; }
explicit Val(float32_t z) : kind_(ValKind::F32) { impl_.f32 = z; }
explicit Val(float64_t z) : kind_(ValKind::F64) { impl_.f64 = z; }
explicit Val(own<Ref>&& r) : kind_(ValKind::EXTERNREF) { impl_.ref = r.release(); }
Val(Val&& that) : kind_(that.kind_), impl_(that.impl_) {
if (is_ref()) that.impl_.ref = nullptr;
}
~Val() {
reset();
}
auto is_num() const -> bool { return wasm::is_num(kind_); }
auto is_ref() const -> bool { return wasm::is_ref(kind_); }
static auto i32(int32_t x) -> Val { return Val(x); }
static auto i64(int64_t x) -> Val { return Val(x); }
static auto f32(float32_t x) -> Val { return Val(x); }
static auto f64(float64_t x) -> Val { return Val(x); }
static auto ref(own<Ref>&& x) -> Val { return Val(std::move(x)); }
template<class T> inline static auto make(T x) -> Val;
template<class T> inline static auto make(own<T>&& x) -> Val;
void reset() {
if (is_ref() && impl_.ref) {
destroyer()(impl_.ref);
impl_.ref = nullptr;
}
}
void reset(Val& that) {
reset();
kind_ = that.kind_;
impl_ = that.impl_;
if (is_ref()) that.impl_.ref = nullptr;
}
auto operator=(Val&& that) -> Val& {
reset(that);
return *this;
}
auto kind() const -> ValKind { return kind_; }
auto i32() const -> int32_t { assert(kind_ == ValKind::I32); return impl_.i32; }
auto i64() const -> int64_t { assert(kind_ == ValKind::I64); return impl_.i64; }
auto f32() const -> float32_t { assert(kind_ == ValKind::F32); return impl_.f32; }
auto f64() const -> float64_t { assert(kind_ == ValKind::F64); return impl_.f64; }
auto ref() const -> Ref* { assert(is_ref()); return impl_.ref; }
template<class T> inline auto get() const -> T;
auto release_ref() -> own<Ref> {
assert(is_ref());
auto ref = impl_.ref;
impl_.ref = nullptr;
return own<Ref>(ref);
}
auto copy() const -> Val {
if (is_ref() && impl_.ref != nullptr) {
// TODO(mvsc): MVSC cannot handle this:
// impl impl = {.ref = impl_.ref->copy().release()};
impl impl;
impl.ref = impl_.ref->copy().release();
return Val(kind_, impl);
} else {
return Val(kind_, impl_);
}
}
};
template<> inline auto Val::make<int32_t>(int32_t x) -> Val { return Val(x); }
template<> inline auto Val::make<int64_t>(int64_t x) -> Val { return Val(x); }
template<> inline auto Val::make<float32_t>(float32_t x) -> Val { return Val(x); }
template<> inline auto Val::make<float64_t>(float64_t x) -> Val { return Val(x); }
template<> inline auto Val::make<Ref>(own<Ref>&& x) -> Val {
return Val(std::move(x));
}
template<> inline auto Val::make<uint32_t>(uint32_t x) -> Val {
return Val(static_cast<int32_t>(x));
}
template<> inline auto Val::make<uint64_t>(uint64_t x) -> Val {
return Val(static_cast<int64_t>(x));
}
template<> inline auto Val::get<int32_t>() const -> int32_t { return i32(); }
template<> inline auto Val::get<int64_t>() const -> int64_t { return i64(); }
template<> inline auto Val::get<float32_t>() const -> float32_t { return f32(); }
template<> inline auto Val::get<float64_t>() const -> float64_t { return f64(); }
template<> inline auto Val::get<Ref*>() const -> Ref* { return ref(); }
template<> inline auto Val::get<uint32_t>() const -> uint32_t {
return static_cast<uint32_t>(i32());
}
template<> inline auto Val::get<uint64_t>() const -> uint64_t {
return static_cast<uint64_t>(i64());
}
// Traps
using Message = vec<byte_t>; // null terminated
class Instance;
class WASM_API_EXTERN Frame {
friend class destroyer;
void destroy();
protected:
Frame() = default;
~Frame() = default;
public:
auto copy() const -> own<Frame>;
auto instance() const -> Instance*;
auto func_index() const -> uint32_t;
auto func_offset() const -> size_t;
auto module_offset() const -> size_t;
};
class WASM_API_EXTERN Trap : public Ref {
friend class destroyer;
void destroy();
protected:
Trap() = default;
~Trap() = default;
public:
static auto make(Store*, const Message& msg) -> own<Trap>;
auto copy() const -> own<Trap>;
auto message() const -> Message;
auto origin() const -> own<Frame>; // may be null
auto trace() const -> ownvec<Frame>; // may be empty, origin first
};
// Modules
template<class T> class WASM_API_EXTERN Shared;
class WASM_API_EXTERN Module : public Ref {
friend class destroyer;
void destroy();
protected:
Module() = default;
~Module() = default;
public:
static auto validate(Store*, const vec<byte_t>& binary) -> bool;
static auto make(Store*, const vec<byte_t>& binary) -> own<Module>;
auto copy() const -> own<Module>;
auto imports() const -> ownvec<ImportType>;
auto exports() const -> ownvec<ExportType>;
auto share() const -> own<Shared<Module>>;
static auto obtain(Store*, const Shared<Module>*) -> own<Module>;
auto serialize() const -> vec<byte_t>;
static auto deserialize(Store*, const vec<byte_t>&) -> own<Module>;
};
// Shared objects
template<>
class WASM_API_EXTERN Shared<Module> {
friend class destroyer;
void destroy();
protected:
Shared() = default;
~Shared() = default;
};
// Foreign Objects
class WASM_API_EXTERN Foreign : public Ref {
friend class destroyer;
void destroy();
protected:
Foreign() = default;
~Foreign() = default;
public:
static auto make(Store*) -> own<Foreign>;
auto copy() const -> own<Foreign>;
};
// Externals
class Func;
class Global;
class Table;
class Memory;
class WASM_API_EXTERN Extern : public Ref {
friend class destroyer;
void destroy();
protected:
Extern() = default;
~Extern() = default;
public:
auto copy() const -> own<Extern>;
auto kind() const -> ExternKind;
auto type() const -> own<ExternType>;
auto func() -> Func*;
auto global() -> Global*;
auto table() -> Table*;
auto memory() -> Memory*;
auto func() const -> const Func*;
auto global() const -> const Global*;
auto table() const -> const Table*;
auto memory() const -> const Memory*;
};
// Function Instances
class WASM_API_EXTERN Func : public Extern {
friend class destroyer;
void destroy();
protected:
Func() = default;
~Func() = default;
public:
using callback = auto (*)(const vec<Val>&, vec<Val>&) -> own<Trap>;
using callback_with_env = auto (*)(void*, const vec<Val>&, vec<Val>&) -> own<Trap>;
static auto make(Store*, const FuncType*, callback) -> own<Func>;
static auto make(Store*, const FuncType*, callback_with_env,
void*, void (*finalizer)(void*) = nullptr) -> own<Func>;
auto copy() const -> own<Func>;
auto type() const -> own<FuncType>;
auto param_arity() const -> size_t;
auto result_arity() const -> size_t;
auto call(const vec<Val>&, vec<Val>&) const -> own<Trap>;
};
// Global Instances
class WASM_API_EXTERN Global : public Extern {
friend class destroyer;
void destroy();
protected:
Global() = default;
~Global() = default;
public:
static auto make(Store*, const GlobalType*, const Val&) -> own<Global>;
auto copy() const -> own<Global>;
auto type() const -> own<GlobalType>;
auto get() const -> Val;
void set(const Val&);
};
// Table Instances
class WASM_API_EXTERN Table : public Extern {
friend class destroyer;
void destroy();
protected:
Table() = default;
~Table() = default;
public:
using size_t = uint32_t;
static auto make(
Store*, const TableType*, const Ref* init = nullptr) -> own<Table>;
auto copy() const -> own<Table>;
auto type() const -> own<TableType>;
auto get(size_t index) const -> own<Ref>;
auto set(size_t index, const Ref*) -> bool;
auto size() const -> size_t;
auto grow(size_t delta, const Ref* init = nullptr) -> bool;
};
// Memory Instances
class WASM_API_EXTERN Memory : public Extern {
friend class destroyer;
void destroy();
protected:
Memory() = default;
~Memory() = default;
public:
static auto make(Store*, const MemoryType*) -> own<Memory>;
auto copy() const -> own<Memory>;
using pages_t = uint32_t;
static const size_t page_size = 0x10000;
auto type() const -> own<MemoryType>;
auto data() const -> byte_t*;
auto data_size() const -> size_t;
auto size() const -> pages_t;
auto grow(pages_t delta) -> bool;
};
// Module Instances
class WASM_API_EXTERN Instance : public Ref {
friend class destroyer;
void destroy();
protected:
Instance() = default;
~Instance() = default;
public:
static auto make(
Store*, const Module*, const vec<Extern*>&, own<Trap>* = nullptr
) -> own<Instance>;
auto copy() const -> own<Instance>;
auto exports() const -> ownvec<Extern>;
};
///////////////////////////////////////////////////////////////////////////////
} // namespace wasm
#endif // #ifdef WASM_HH
|