Skip to content

Commit

Permalink
Use enum classes (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossberg committed Jan 16, 2020
1 parent d9a8009 commit b0b4bfb
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 247 deletions.
20 changes: 10 additions & 10 deletions example/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
// Print a Wasm value
auto operator<<(std::ostream& out, const wasm::Val& val) -> std::ostream& {
switch (val.kind()) {
case wasm::I32: {
case wasm::ValKind::I32: {
out << val.i32();
} break;
case wasm::I64: {
case wasm::ValKind::I64: {
out << val.i64();
} break;
case wasm::F32: {
case wasm::ValKind::F32: {
out << val.f32();
} break;
case wasm::F64: {
case wasm::ValKind::F64: {
out << val.f64();
} break;
case wasm::ANYREF:
case wasm::FUNCREF: {
case wasm::ValKind::ANYREF:
case wasm::ValKind::FUNCREF: {
if (val.ref() == nullptr) {
out << "null";
} else {
Expand Down Expand Up @@ -87,8 +87,8 @@ void run() {
// Create external print functions.
std::cout << "Creating callback..." << std::endl;
auto print_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32))
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32))
);
auto print_func = wasm::Func::make(store, print_type.get(), print_callback);

Expand All @@ -97,7 +97,7 @@ void run() {
int i = 42;
auto closure_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32))
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32))
);
auto closure_func = wasm::Func::make(store, closure_type.get(), closure_callback, &i);

Expand All @@ -113,7 +113,7 @@ void run() {
// Extract export.
std::cout << "Extracting export..." << std::endl;
auto exports = instance->exports();
if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
if (exports.size() == 0 || exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func()) {
std::cout << "> Error accessing export!" << std::endl;
exit(1);
}
Expand Down
8 changes: 4 additions & 4 deletions example/global.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ void run() {
// Create external globals.
std::cout << "Creating globals..." << std::endl;
auto const_f32_type = wasm::GlobalType::make(
wasm::ValType::make(wasm::F32), wasm::CONST);
wasm::ValType::make(wasm::ValKind::F32), wasm::Mutability::CONST);
auto const_i64_type = wasm::GlobalType::make(
wasm::ValType::make(wasm::I64), wasm::CONST);
wasm::ValType::make(wasm::ValKind::I64), wasm::Mutability::CONST);
auto var_f32_type = wasm::GlobalType::make(
wasm::ValType::make(wasm::F32), wasm::VAR);
wasm::ValType::make(wasm::ValKind::F32), wasm::Mutability::VAR);
auto var_i64_type = wasm::GlobalType::make(
wasm::ValType::make(wasm::I64), wasm::VAR);
wasm::ValType::make(wasm::ValKind::I64), wasm::Mutability::VAR);
auto const_f32_import = wasm::Global::make(store, const_f32_type.get(), wasm::Val::f32(1));
auto const_i64_import = wasm::Global::make(store, const_i64_type.get(), wasm::Val::i64(2));
auto var_f32_import = wasm::Global::make(store, var_f32_type.get(), wasm::Val::f32(3));
Expand Down
2 changes: 1 addition & 1 deletion example/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void run() {
// Extract export.
std::cout << "Extracting export..." << std::endl;
auto exports = instance->exports();
if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
if (exports.size() == 0 || exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func()) {
std::cout << "> Error accessing export!" << std::endl;
exit(1);
}
Expand Down
4 changes: 2 additions & 2 deletions example/hostref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ void run() {
// Create external callback function.
std::cout << "Creating callback..." << std::endl;
auto callback_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ANYREF)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ANYREF))
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::ANYREF)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::ANYREF))
);
auto callback_func = wasm::Func::make(store, callback_type.get(), callback);

Expand Down
10 changes: 5 additions & 5 deletions example/multi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ void run() {
// Create external print functions.
std::cout << "Creating callback..." << std::endl;
auto tuple = wasm::ownvec<wasm::ValType>::make(
wasm::ValType::make(wasm::I32),
wasm::ValType::make(wasm::I64),
wasm::ValType::make(wasm::I64),
wasm::ValType::make(wasm::I32)
wasm::ValType::make(wasm::ValKind::I32),
wasm::ValType::make(wasm::ValKind::I64),
wasm::ValType::make(wasm::ValKind::I64),
wasm::ValType::make(wasm::ValKind::I32)
);
auto callback_type =
wasm::FuncType::make(tuple.deep_copy(), tuple.deep_copy());
Expand All @@ -76,7 +76,7 @@ void run() {
// Extract export.
std::cout << "Extracting export..." << std::endl;
auto exports = instance->exports();
if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
if (exports.size() == 0 || exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func()) {
std::cout << "> Error accessing export!" << std::endl;
exit(1);
}
Expand Down
26 changes: 13 additions & 13 deletions example/reflect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

auto operator<<(std::ostream& out, wasm::Mutability mut) -> std::ostream& {
switch (mut) {
case wasm::VAR: return out << "var";
case wasm::CONST: return out << "const";
case wasm::Mutability::VAR: return out << "var";
case wasm::Mutability::CONST: return out << "const";
}
return out;
}
Expand All @@ -23,12 +23,12 @@ auto operator<<(std::ostream& out, wasm::Limits limits) -> std::ostream& {

auto operator<<(std::ostream& out, const wasm::ValType& type) -> std::ostream& {
switch (type.kind()) {
case wasm::I32: return out << "i32";
case wasm::I64: return out << "i64";
case wasm::F32: return out << "f32";
case wasm::F64: return out << "f64";
case wasm::ANYREF: return out << "anyref";
case wasm::FUNCREF: return out << "funcref";
case wasm::ValKind::I32: return out << "i32";
case wasm::ValKind::I64: return out << "i64";
case wasm::ValKind::F32: return out << "f32";
case wasm::ValKind::F64: return out << "f64";
case wasm::ValKind::ANYREF: return out << "anyref";
case wasm::ValKind::FUNCREF: return out << "funcref";
}
return out;
}
Expand All @@ -48,16 +48,16 @@ auto operator<<(std::ostream& out, const wasm::ownvec<wasm::ValType>& types) ->

auto operator<<(std::ostream& out, const wasm::ExternType& type) -> std::ostream& {
switch (type.kind()) {
case wasm::EXTERN_FUNC: {
case wasm::ExternKind::FUNC: {
out << "func " << type.func()->params() << " -> " << type.func()->results();
} break;
case wasm::EXTERN_GLOBAL: {
case wasm::ExternKind::GLOBAL: {
out << "global " << type.global()->mutability() << " " << *type.global()->content();
} break;
case wasm::EXTERN_TABLE: {
case wasm::ExternKind::TABLE: {
out << "table " << type.table()->limits() << " " << *type.table()->element();
} break;
case wasm::EXTERN_MEMORY: {
case wasm::ExternKind::MEMORY: {
out << "memory " << type.memory()->limits();
} break;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ void run() {
std::cout << "> export " << i << " " << export_types[i]->name() << std::endl;
std::cout << ">> initial: " << *export_types[i]->type() << std::endl;
std::cout << ">> current: " << *exports[i]->type() << std::endl;
if (exports[i]->kind() == wasm::EXTERN_FUNC) {
if (exports[i]->kind() == wasm::ExternKind::FUNC) {
auto func = exports[i]->func();
std::cout << ">> in-arity: " << func->param_arity();
std::cout << ", out-arity: " << func->result_arity() << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion example/serialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void run() {
// Extract export.
std::cout << "Extracting export..." << std::endl;
auto exports = instance->exports();
if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
if (exports.size() == 0 || exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func()) {
std::cout << "> Error accessing export!" << std::endl;
exit(1);
}
Expand Down
6 changes: 3 additions & 3 deletions example/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ void run() {
// Create external function.
std::cout << "Creating callback..." << std::endl;
auto neg_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32))
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32))
);
auto h = wasm::Func::make(store, neg_type.get(), neg_callback);

Expand Down Expand Up @@ -174,7 +174,7 @@ void run() {
// TODO(wasm+): Once Wasm allows multiple tables, turn this into import.
std::cout << "Creating stand-alone table..." << std::endl;
auto tabletype = wasm::TableType::make(
wasm::ValType::make(wasm::FUNCREF), wasm::Limits(5, 5));
wasm::ValType::make(wasm::ValKind::FUNCREF), wasm::Limits(5, 5));
auto table2 = wasm::Table::make(store, tabletype.get());
check(table2->size() == 5);
check(! table2->grow(1));
Expand Down
8 changes: 4 additions & 4 deletions example/threads.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const int N_REPS = 3;
auto callback(
void* env, const wasm::Val args[], wasm::Val results[]
) -> wasm::own<wasm::Trap> {
assert(args[0].kind() == wasm::I32);
assert(args[0].kind() == wasm::ValKind::I32);
std::lock_guard<std::mutex>(*reinterpret_cast<std::mutex*>(env));
std::cout << "Thread " << args[0].i32() << " running..." << std::endl;
std::cout.flush();
Expand Down Expand Up @@ -42,13 +42,13 @@ void run(

// Create imports.
auto func_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32)),
wasm::ownvec<wasm::ValType>::make()
);
auto func = wasm::Func::make(store, func_type.get(), callback, mutex);

auto global_type = wasm::GlobalType::make(
wasm::ValType::make(wasm::I32), wasm::CONST);
wasm::ValType::make(wasm::ValKind::I32), wasm::Mutability::CONST);
auto global = wasm::Global::make(
store, global_type.get(), wasm::Val::i32(i));

Expand All @@ -63,7 +63,7 @@ void run(

// Extract export.
auto exports = instance->exports();
if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
if (exports.size() == 0 || exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func()) {
std::lock_guard<std::mutex> lock(*mutex);
std::cout << "> Error accessing export!" << std::endl;
exit(1);
Expand Down
6 changes: 3 additions & 3 deletions example/trap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void run() {
std::cout << "Creating callback..." << std::endl;
auto fail_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32))
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32))
);
auto fail_func =
wasm::Func::make(store, fail_type.get(), fail_callback, store);
Expand All @@ -76,8 +76,8 @@ void run() {
std::cout << "Extracting exports..." << std::endl;
auto exports = instance->exports();
if (exports.size() < 2 ||
exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func() ||
exports[1]->kind() != wasm::EXTERN_FUNC || !exports[1]->func()) {
exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func() ||
exports[1]->kind() != wasm::ExternKind::FUNC || !exports[1]->func()) {
std::cout << "> Error accessing exports!" << std::endl;
exit(1);
}
Expand Down
32 changes: 16 additions & 16 deletions include/wasm.hh
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public:

// Type attributes

enum Mutability : uint8_t { CONST, VAR };
enum class Mutability : uint8_t { CONST, VAR };

struct Limits {
uint32_t min;
Expand All @@ -240,13 +240,13 @@ struct Limits {

// Value Types

enum ValKind : uint8_t {
enum class ValKind : uint8_t {
I32, I64, F32, F64,
ANYREF = 128, FUNCREF,
};

inline bool is_num(ValKind k) { return k < ANYREF; }
inline bool is_ref(ValKind k) { return k >= ANYREF; }
inline bool is_num(ValKind k) { return k < ValKind::ANYREF; }
inline bool is_ref(ValKind k) { return k >= ValKind::ANYREF; }


class WASM_API_EXTERN ValType {
Expand All @@ -266,8 +266,8 @@ public:

// External Types

enum ExternKind : uint8_t {
EXTERN_FUNC, EXTERN_GLOBAL, EXTERN_TABLE, EXTERN_MEMORY
enum class ExternKind : uint8_t {
FUNC, GLOBAL, TABLE, MEMORY
};

class FuncType;
Expand Down Expand Up @@ -430,12 +430,12 @@ class Val {
Val(ValKind kind, impl impl) : kind_(kind), impl_(impl) {}

public:
Val() : kind_(ANYREF) { impl_.ref = nullptr; }
Val(int32_t i) : kind_(I32) { impl_.i32 = i; }
Val(int64_t i) : kind_(I64) { impl_.i64 = i; }
Val(float32_t z) : kind_(F32) { impl_.f32 = z; }
Val(float64_t z) : kind_(F64) { impl_.f64 = z; }
Val(own<Ref>&& r) : kind_(ANYREF) { impl_.ref = r.release(); }
Val() : kind_(ValKind::ANYREF) { impl_.ref = nullptr; }
Val(int32_t i) : kind_(ValKind::I32) { impl_.i32 = i; }
Val(int64_t i) : kind_(ValKind::I64) { impl_.i64 = i; }
Val(float32_t z) : kind_(ValKind::F32) { impl_.f32 = z; }
Val(float64_t z) : kind_(ValKind::F64) { impl_.f64 = z; }
Val(own<Ref>&& r) : kind_(ValKind::ANYREF) { impl_.ref = r.release(); }

Val(Val&& that) : kind_(that.kind_), impl_(that.impl_) {
if (is_ref()) that.impl_.ref = nullptr;
Expand Down Expand Up @@ -476,10 +476,10 @@ public:
}

auto kind() const -> ValKind { return kind_; }
auto i32() const -> int32_t { assert(kind_ == I32); return impl_.i32; }
auto i64() const -> int64_t { assert(kind_ == I64); return impl_.i64; }
auto f32() const -> float32_t { assert(kind_ == F32); return impl_.f32; }
auto f64() const -> float64_t { assert(kind_ == F64); return impl_.f64; }
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;

Expand Down
Loading

0 comments on commit b0b4bfb

Please sign in to comment.