mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-16 17:18:57 +00:00
Move Webassembly objects to Store and remove Context
Co-authored-by: ptitSeb <sebastien.chev@gmail.com> Co-authored-by: Manos Pitsidianakis <manos@wasmer.io>
This commit is contained in:
committed by
Manos Pitsidianakis
parent
b5ae6399ce
commit
a419ccdf52
@@ -35,7 +35,7 @@ void wasm_val_print(wasm_val_t val) {
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* print_callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n> ");
|
||||
wasm_val_print(args->data[0]);
|
||||
@@ -48,9 +48,9 @@ own wasm_trap_t* print_callback(
|
||||
|
||||
// A function closure.
|
||||
own wasm_trap_t* closure_callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
int i = *(int*) wasm_context_ref_mut_get(ctx_mut);
|
||||
int i = *(int*)env;
|
||||
printf("Calling back closure...\n");
|
||||
printf("> %d\n", i);
|
||||
|
||||
@@ -65,9 +65,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
int i = 42;
|
||||
wasm_context_t* ctx = wasm_context_new(store, &i);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
@@ -102,8 +99,9 @@ int main(int argc, const char* argv[]) {
|
||||
own wasm_functype_t* print_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
|
||||
own wasm_func_t* print_func = wasm_func_new(store, print_type, print_callback);
|
||||
|
||||
int i = 42;
|
||||
own wasm_functype_t* closure_type = wasm_functype_new_0_1(wasm_valtype_new_i32());
|
||||
own wasm_func_t* closure_func = wasm_func_new(store, closure_type, closure_callback);
|
||||
own wasm_func_t* closure_func = wasm_func_new_with_env(store, closure_type, closure_callback, &i, NULL);
|
||||
|
||||
wasm_functype_delete(print_type);
|
||||
wasm_functype_delete(closure_type);
|
||||
|
||||
@@ -7,22 +7,22 @@
|
||||
#include "wasm.hh"
|
||||
|
||||
// Print a Wasm value
|
||||
auto operator<<(std::ostream& out, const wasm::Value& val) -> std::ostream& {
|
||||
auto operator<<(std::ostream& out, const wasm::Val& val) -> std::ostream& {
|
||||
switch (val.kind()) {
|
||||
case wasm::ValueKind::I32: {
|
||||
case wasm::ValKind::I32: {
|
||||
out << val.i32();
|
||||
} break;
|
||||
case wasm::ValueKind::I64: {
|
||||
case wasm::ValKind::I64: {
|
||||
out << val.i64();
|
||||
} break;
|
||||
case wasm::ValueKind::F32: {
|
||||
case wasm::ValKind::F32: {
|
||||
out << val.f32();
|
||||
} break;
|
||||
case wasm::ValueKind::F64: {
|
||||
case wasm::ValKind::F64: {
|
||||
out << val.f64();
|
||||
} break;
|
||||
case wasm::ValueKind::ANYREF:
|
||||
case wasm::ValueKind::FUNCREF: {
|
||||
case wasm::ValKind::ANYREF:
|
||||
case wasm::ValKind::FUNCREF: {
|
||||
if (val.ref() == nullptr) {
|
||||
out << "null";
|
||||
} else {
|
||||
@@ -35,7 +35,7 @@ auto operator<<(std::ostream& out, const wasm::Value& val) -> std::ostream& {
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
auto print_callback(
|
||||
const wasm::vec<wasm::Value>& args, wasm::vec<wasm::Value>& results
|
||||
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
|
||||
) -> wasm::own<wasm::Trap> {
|
||||
std::cout << "Calling back..." << std::endl << "> " << args[0] << std::endl;
|
||||
results[0] = args[0].copy();
|
||||
@@ -45,12 +45,12 @@ auto print_callback(
|
||||
|
||||
// A function closure.
|
||||
auto closure_callback(
|
||||
void* env, const wasm::vec<wasm::Value>& args, wasm::vec<wasm::Value>& results
|
||||
void* env, const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
|
||||
) -> wasm::own<wasm::Trap> {
|
||||
auto i = *reinterpret_cast<int*>(env);
|
||||
std::cout << "Calling back closure..." << std::endl;
|
||||
std::cout << "> " << i << std::endl;
|
||||
results[0] = wasm::Value::i32(static_cast<int32_t>(i));
|
||||
results[0] = wasm::Val::i32(static_cast<int32_t>(i));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -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::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::I32)),
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::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);
|
||||
|
||||
@@ -96,8 +96,8 @@ void run() {
|
||||
std::cout << "Creating closure..." << std::endl;
|
||||
int i = 42;
|
||||
auto closure_type = wasm::FuncType::make(
|
||||
wasm::ownvec<wasm::ValueType>::make(),
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::I32))
|
||||
wasm::ownvec<wasm::ValType>::make(),
|
||||
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::I32))
|
||||
);
|
||||
auto closure_func = wasm::Func::make(store, closure_type.get(), closure_callback, &i);
|
||||
|
||||
@@ -122,8 +122,8 @@ void run() {
|
||||
|
||||
// Call.
|
||||
std::cout << "Calling export..." << std::endl;
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::i32(3), wasm::Value::i32(4));
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(3), wasm::Val::i32(4));
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (run_func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
|
||||
@@ -12,8 +12,8 @@ const int iterations = 100000;
|
||||
int live_count = 0;
|
||||
|
||||
void finalize(void* data) {
|
||||
int i = (int)data;
|
||||
if (i % (iterations / 10) == 0) printf("Finalizing #%d...\n", i);
|
||||
intptr_t i = (intptr_t)data;
|
||||
if (i % (iterations / 10) == 0) printf("Finalizing #%" PRIdPTR "...\n", i);
|
||||
--live_count;
|
||||
}
|
||||
|
||||
@@ -74,8 +74,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Live count %d\n", live_count);
|
||||
printf("Creating store 1...\n");
|
||||
wasm_store_t* store1 = wasm_store_new(engine);
|
||||
wasm_context_t* ctx1 = wasm_context_new(store1, 0);
|
||||
wasm_store_context_set(store1, ctx1);
|
||||
|
||||
printf("Running in store 1...\n");
|
||||
run_in_store(store1);
|
||||
@@ -83,8 +81,6 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
printf("Creating store 2...\n");
|
||||
wasm_store_t* store2 = wasm_store_new(engine);
|
||||
wasm_context_t* ctx2 = wasm_context_new(store2, 0);
|
||||
wasm_store_context_set(store2, ctx2);
|
||||
|
||||
printf("Running in store 2...\n");
|
||||
run_in_store(store2);
|
||||
|
||||
@@ -52,8 +52,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -31,9 +31,9 @@ void check(T actual, U expected) {
|
||||
}
|
||||
}
|
||||
|
||||
auto call(const wasm::Func* func) -> wasm::Value {
|
||||
auto args = wasm::vec<wasm::Value>::make();
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
auto call(const wasm::Func* func) -> wasm::Val {
|
||||
auto args = wasm::vec<wasm::Val>::make();
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -41,9 +41,9 @@ auto call(const wasm::Func* func) -> wasm::Value {
|
||||
return results[0].copy();
|
||||
}
|
||||
|
||||
void call(const wasm::Func* func, wasm::Value&& arg) {
|
||||
auto args = wasm::vec<wasm::Value>::make(std::move(arg));
|
||||
auto results = wasm::vec<wasm::Value>::make();
|
||||
void call(const wasm::Func* func, wasm::Val&& arg) {
|
||||
auto args = wasm::vec<wasm::Val>::make(std::move(arg));
|
||||
auto results = wasm::vec<wasm::Val>::make();
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -83,17 +83,17 @@ void run() {
|
||||
// Create external globals.
|
||||
std::cout << "Creating globals..." << std::endl;
|
||||
auto const_f32_type = wasm::GlobalType::make(
|
||||
wasm::ValueType::make(wasm::ValueKind::F32), wasm::Mutability::CONST);
|
||||
wasm::ValType::make(wasm::ValKind::F32), wasm::Mutability::CONST);
|
||||
auto const_i64_type = wasm::GlobalType::make(
|
||||
wasm::ValueType::make(wasm::ValueKind::I64), wasm::Mutability::CONST);
|
||||
wasm::ValType::make(wasm::ValKind::I64), wasm::Mutability::CONST);
|
||||
auto var_f32_type = wasm::GlobalType::make(
|
||||
wasm::ValueType::make(wasm::ValueKind::F32), wasm::Mutability::VAR);
|
||||
wasm::ValType::make(wasm::ValKind::F32), wasm::Mutability::VAR);
|
||||
auto var_i64_type = wasm::GlobalType::make(
|
||||
wasm::ValueType::make(wasm::ValueKind::I64), wasm::Mutability::VAR);
|
||||
auto const_f32_import = wasm::Global::make(store, const_f32_type.get(), wasm::Value::f32(1));
|
||||
auto const_i64_import = wasm::Global::make(store, const_i64_type.get(), wasm::Value::i64(2));
|
||||
auto var_f32_import = wasm::Global::make(store, var_f32_type.get(), wasm::Value::f32(3));
|
||||
auto var_i64_import = wasm::Global::make(store, var_i64_type.get(), wasm::Value::i64(4));
|
||||
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));
|
||||
auto var_i64_import = wasm::Global::make(store, var_i64_type.get(), wasm::Val::i64(4));
|
||||
|
||||
// Instantiate.
|
||||
std::cout << "Instantiating module..." << std::endl;
|
||||
@@ -154,10 +154,10 @@ void run() {
|
||||
check(call(get_var_i64_export).i64(), 8);
|
||||
|
||||
// Modify variables through API and check again.
|
||||
var_f32_import->set(wasm::Value::f32(33));
|
||||
var_i64_import->set(wasm::Value::i64(34));
|
||||
var_f32_export->set(wasm::Value::f32(37));
|
||||
var_i64_export->set(wasm::Value::i64(38));
|
||||
var_f32_import->set(wasm::Val::f32(33));
|
||||
var_i64_import->set(wasm::Val::i64(34));
|
||||
var_f32_export->set(wasm::Val::f32(37));
|
||||
var_i64_export->set(wasm::Val::i64(38));
|
||||
|
||||
check(var_f32_import->get().f32(), 33);
|
||||
check(var_i64_import->get().i64(), 34);
|
||||
@@ -170,10 +170,10 @@ void run() {
|
||||
check(call(get_var_i64_export).i64(), 38);
|
||||
|
||||
// Modify variables through calls and check again.
|
||||
call(set_var_f32_import, wasm::Value::f32(73));
|
||||
call(set_var_i64_import, wasm::Value::i64(74));
|
||||
call(set_var_f32_export, wasm::Value::f32(77));
|
||||
call(set_var_i64_export, wasm::Value::i64(78));
|
||||
call(set_var_f32_import, wasm::Val::f32(73));
|
||||
call(set_var_i64_import, wasm::Val::i64(74));
|
||||
call(set_var_f32_export, wasm::Val::f32(77));
|
||||
call(set_var_i64_export, wasm::Val::i64(78));
|
||||
|
||||
check(var_f32_import->get().f32(), 73);
|
||||
check(var_i64_import->get().i64(), 74);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* hello_callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n");
|
||||
printf("> Hello World!\n");
|
||||
@@ -22,8 +22,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
@@ -43,6 +41,13 @@ int main(int argc, const char* argv[]) {
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
// Validate.
|
||||
printf("Validating module...\n");
|
||||
if (!wasm_module_validate(store, &binary)) {
|
||||
printf("> Error validating module!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Compile.
|
||||
printf("Compiling module...\n");
|
||||
own wasm_module_t* module = wasm_module_new(store, &binary);
|
||||
|
||||
@@ -38,6 +38,13 @@ void run() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Validate.
|
||||
std::cout << "Validating module..." << std::endl;
|
||||
if (!wasm::Module::validate(store, binary)) {
|
||||
std::cout << "> Error validating module!" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Compile.
|
||||
std::cout << "Compiling module..." << std::endl;
|
||||
auto module = wasm::Module::make(store, binary);
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n> ");
|
||||
printf("> %p\n",
|
||||
@@ -127,8 +127,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
auto callback(
|
||||
const wasm::vec<wasm::Value>& args, wasm::vec<wasm::Value>& results
|
||||
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
|
||||
) -> wasm::own<wasm::Trap> {
|
||||
std::cout << "Calling back..." << std::endl;
|
||||
std::cout << "> " << (args[0].ref() ? args[0].ref()->get_host_info() : nullptr) << std::endl;
|
||||
@@ -45,8 +45,8 @@ auto get_export_table(wasm::ownvec<wasm::Extern>& exports, size_t i) -> wasm::Ta
|
||||
|
||||
void call_r_v(const wasm::Func* func, const wasm::Ref* ref) {
|
||||
std::cout << "call_r_v... " << std::flush;
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::ref(ref ? ref->copy() : wasm::own<wasm::Ref>()));
|
||||
auto results = wasm::vec<wasm::Value>::make();
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::ref(ref ? ref->copy() : wasm::own<wasm::Ref>()));
|
||||
auto results = wasm::vec<wasm::Val>::make();
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -56,8 +56,8 @@ void call_r_v(const wasm::Func* func, const wasm::Ref* ref) {
|
||||
|
||||
auto call_v_r(const wasm::Func* func) -> wasm::own<wasm::Ref> {
|
||||
std::cout << "call_v_r... " << std::flush;
|
||||
auto args = wasm::vec<wasm::Value>::make();
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
auto args = wasm::vec<wasm::Val>::make();
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -68,8 +68,8 @@ auto call_v_r(const wasm::Func* func) -> wasm::own<wasm::Ref> {
|
||||
|
||||
auto call_r_r(const wasm::Func* func, const wasm::Ref* ref) -> wasm::own<wasm::Ref> {
|
||||
std::cout << "call_r_r... " << std::flush;
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::ref(ref ? ref->copy() : wasm::own<wasm::Ref>()));
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::ref(ref ? ref->copy() : wasm::own<wasm::Ref>()));
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -80,9 +80,9 @@ auto call_r_r(const wasm::Func* func, const wasm::Ref* ref) -> wasm::own<wasm::R
|
||||
|
||||
void call_ir_v(const wasm::Func* func, int32_t i, const wasm::Ref* ref) {
|
||||
std::cout << "call_ir_v... " << std::flush;
|
||||
auto args = wasm::vec<wasm::Value>::make(
|
||||
wasm::Value::i32(i), wasm::Value::ref(ref ? ref->copy() : wasm::own<wasm::Ref>()));
|
||||
auto results = wasm::vec<wasm::Value>::make();
|
||||
auto args = wasm::vec<wasm::Val>::make(
|
||||
wasm::Val::i32(i), wasm::Val::ref(ref ? ref->copy() : wasm::own<wasm::Ref>()));
|
||||
auto results = wasm::vec<wasm::Val>::make();
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -92,8 +92,8 @@ void call_ir_v(const wasm::Func* func, int32_t i, const wasm::Ref* ref) {
|
||||
|
||||
auto call_i_r(const wasm::Func* func, int32_t i) -> wasm::own<wasm::Ref> {
|
||||
std::cout << "call_i_r... " << std::flush;
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::i32(i));
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(i));
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error calling function!" << std::endl;
|
||||
exit(1);
|
||||
@@ -144,8 +144,8 @@ void run() {
|
||||
// Create external callback function.
|
||||
std::cout << "Creating callback..." << std::endl;
|
||||
auto callback_type = wasm::FuncType::make(
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::ANYREF)),
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::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);
|
||||
|
||||
@@ -182,7 +182,7 @@ void run() {
|
||||
check(host1->copy(), host1.get());
|
||||
check(host2->copy(), host2.get());
|
||||
|
||||
wasm::Value val = wasm::Value::ref(host1->copy());
|
||||
wasm::Val val = wasm::Val::ref(host1->copy());
|
||||
check(val.ref()->copy(), host1.get());
|
||||
auto ref = val.release_ref();
|
||||
assert(val.ref() == nullptr);
|
||||
@@ -199,7 +199,7 @@ void run() {
|
||||
check(call_v_r(global_get), nullptr);
|
||||
|
||||
check(global->get().release_ref(), nullptr);
|
||||
global->set(wasm::Value(host2->copy()));
|
||||
global->set(wasm::Val(host2->copy()));
|
||||
check(call_v_r(global_get), host2.get());
|
||||
check(global->get().release_ref(), host2.get());
|
||||
|
||||
|
||||
@@ -98,8 +98,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -33,8 +33,8 @@ void check(T actual, U expected) {
|
||||
|
||||
template<class... Args>
|
||||
void check_ok(const wasm::Func* func, Args... xs) {
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::i32(xs)...);
|
||||
auto results = wasm::vec<wasm::Value>::make();
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(xs)...);
|
||||
auto results = wasm::vec<wasm::Val>::make();
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error on result, expected return" << std::endl;
|
||||
exit(1);
|
||||
@@ -43,8 +43,8 @@ void check_ok(const wasm::Func* func, Args... xs) {
|
||||
|
||||
template<class... Args>
|
||||
void check_trap(const wasm::Func* func, Args... xs) {
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::i32(xs)...);
|
||||
auto results = wasm::vec<wasm::Value>::make();
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(xs)...);
|
||||
auto results = wasm::vec<wasm::Val>::make();
|
||||
if (! func->call(args, results)) {
|
||||
std::cout << "> Error on result, expected trap" << std::endl;
|
||||
exit(1);
|
||||
@@ -53,8 +53,8 @@ void check_trap(const wasm::Func* func, Args... xs) {
|
||||
|
||||
template<class... Args>
|
||||
auto call(const wasm::Func* func, Args... xs) -> int32_t {
|
||||
auto args = wasm::vec<wasm::Value>::make(wasm::Value::i32(xs)...);
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(xs)...);
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error on result, expected return" << std::endl;
|
||||
exit(1);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n> ");
|
||||
printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
|
||||
@@ -44,8 +44,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
auto callback(
|
||||
const wasm::vec<wasm::Value>& args, wasm::vec<wasm::Value>& results
|
||||
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
|
||||
) -> wasm::own<wasm::Trap> {
|
||||
std::cout << "Calling back..." << std::endl;
|
||||
std::cout << "> " << args[0].i32();
|
||||
@@ -54,11 +54,11 @@ void run() {
|
||||
|
||||
// Create external print functions.
|
||||
std::cout << "Creating callback..." << std::endl;
|
||||
auto tuple = wasm::ownvec<wasm::ValueType>::make(
|
||||
wasm::ValueType::make(wasm::ValueKind::I32),
|
||||
wasm::ValueType::make(wasm::ValueKind::I64),
|
||||
wasm::ValueType::make(wasm::ValueKind::I64),
|
||||
wasm::ValueType::make(wasm::ValueKind::I32)
|
||||
auto tuple = wasm::ownvec<wasm::ValType>::make(
|
||||
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());
|
||||
@@ -84,10 +84,10 @@ void run() {
|
||||
|
||||
// Call.
|
||||
std::cout << "Calling export..." << std::endl;
|
||||
auto args = wasm::vec<wasm::Value>::make(
|
||||
wasm::Value::i32(1), wasm::Value::i64(2), wasm::Value::i64(3), wasm::Value::i32(4)
|
||||
auto args = wasm::vec<wasm::Val>::make(
|
||||
wasm::Val::i32(1), wasm::Val::i64(2), wasm::Val::i64(3), wasm::Val::i32(4)
|
||||
);
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(4);
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(4);
|
||||
if (wasm::own<wasm::Trap> trap = run_func->call(args, results)) {
|
||||
std::cout << "> Error calling function! " << trap->message().get() << std::endl;
|
||||
exit(1);
|
||||
|
||||
@@ -87,8 +87,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* hello_callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n");
|
||||
printf("> Hello World!\n");
|
||||
@@ -22,8 +22,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -23,8 +23,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* neg_callback(
|
||||
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n");
|
||||
results->data[0].kind = WASM_I32;
|
||||
@@ -78,8 +78,6 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
|
||||
@@ -9,10 +9,10 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
auto neg_callback(
|
||||
const wasm::vec<wasm::Value>& args, wasm::vec<wasm::Value>& results
|
||||
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
|
||||
) -> wasm::own<wasm::Trap> {
|
||||
std::cout << "Calling back..." << std::endl;
|
||||
results[0] = wasm::Value(-args[0].i32());
|
||||
results[0] = wasm::Val(-args[0].i32());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -49,10 +49,10 @@ void check(bool success) {
|
||||
}
|
||||
|
||||
auto call(
|
||||
const wasm::Func* func, wasm::Value&& arg1, wasm::Value&& arg2
|
||||
) -> wasm::Value {
|
||||
auto args = wasm::vec<wasm::Value>::make(std::move(arg1), std::move(arg2));
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
const wasm::Func* func, wasm::Val&& arg1, wasm::Val&& arg2
|
||||
) -> wasm::Val {
|
||||
auto args = wasm::vec<wasm::Val>::make(std::move(arg1), std::move(arg2));
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (func->call(args, results)) {
|
||||
std::cout << "> Error on result, expected return" << std::endl;
|
||||
exit(1);
|
||||
@@ -60,9 +60,9 @@ auto call(
|
||||
return results[0].copy();
|
||||
}
|
||||
|
||||
void check_trap(const wasm::Func* func, wasm::Value&& arg1, wasm::Value&& arg2) {
|
||||
auto args = wasm::vec<wasm::Value>::make(std::move(arg1), std::move(arg2));
|
||||
auto results = wasm::vec<wasm::Value>::make_uninitialized(1);
|
||||
void check_trap(const wasm::Func* func, wasm::Val&& arg1, wasm::Val&& arg2) {
|
||||
auto args = wasm::vec<wasm::Val>::make(std::move(arg1), std::move(arg2));
|
||||
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
|
||||
if (! func->call(args, results)) {
|
||||
std::cout << "> Error on result, expected trap" << std::endl;
|
||||
exit(1);
|
||||
@@ -119,8 +119,8 @@ void run() {
|
||||
// Create external function.
|
||||
std::cout << "Creating callback..." << std::endl;
|
||||
auto neg_type = wasm::FuncType::make(
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::I32)),
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::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);
|
||||
|
||||
@@ -132,9 +132,9 @@ void run() {
|
||||
check(table->size(), 2u);
|
||||
check(table->get(0) == nullptr);
|
||||
check(table->get(1) != nullptr);
|
||||
check_trap(call_indirect, wasm::Value::i32(0), wasm::Value::i32(0));
|
||||
check(call(call_indirect, wasm::Value::i32(7), wasm::Value::i32(1)).i32(), 7);
|
||||
check_trap(call_indirect, wasm::Value::i32(0), wasm::Value::i32(2));
|
||||
check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(0));
|
||||
check(call(call_indirect, wasm::Val::i32(7), wasm::Val::i32(1)).i32(), 7);
|
||||
check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(2));
|
||||
|
||||
// Mutate table.
|
||||
std::cout << "Mutating table..." << std::endl;
|
||||
@@ -143,9 +143,9 @@ void run() {
|
||||
check(! table->set(2, f));
|
||||
check(table->get(0) != nullptr);
|
||||
check(table->get(1) == nullptr);
|
||||
check(call(call_indirect, wasm::Value::i32(7), wasm::Value::i32(0)).i32(), 666);
|
||||
check_trap(call_indirect, wasm::Value::i32(0), wasm::Value::i32(1));
|
||||
check_trap(call_indirect, wasm::Value::i32(0), wasm::Value::i32(2));
|
||||
check(call(call_indirect, wasm::Val::i32(7), wasm::Val::i32(0)).i32(), 666);
|
||||
check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(1));
|
||||
check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(2));
|
||||
|
||||
// Grow table.
|
||||
std::cout << "Growing table..." << std::endl;
|
||||
@@ -157,10 +157,10 @@ void run() {
|
||||
check(table->get(2) != nullptr);
|
||||
check(table->get(3) != nullptr);
|
||||
check(table->get(4) == nullptr);
|
||||
check(call(call_indirect, wasm::Value::i32(5), wasm::Value::i32(2)).i32(), 5);
|
||||
check(call(call_indirect, wasm::Value::i32(6), wasm::Value::i32(3)).i32(), -6);
|
||||
check_trap(call_indirect, wasm::Value::i32(0), wasm::Value::i32(4));
|
||||
check_trap(call_indirect, wasm::Value::i32(0), wasm::Value::i32(5));
|
||||
check(call(call_indirect, wasm::Val::i32(5), wasm::Val::i32(2)).i32(), 5);
|
||||
check(call(call_indirect, wasm::Val::i32(6), wasm::Val::i32(3)).i32(), -6);
|
||||
check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(4));
|
||||
check_trap(call_indirect, wasm::Val::i32(0), wasm::Val::i32(5));
|
||||
|
||||
check(table->grow(2, f));
|
||||
check(table->size(), 7u);
|
||||
@@ -175,7 +175,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::ValueType::make(wasm::ValueKind::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));
|
||||
|
||||
@@ -13,7 +13,7 @@ const int N_THREADS = 10;
|
||||
const int N_REPS = 3;
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* callback(wasm_context_ref_mut_t* ctx, const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
||||
own wasm_trap_t* callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
||||
assert(args->data[0].kind == WASM_I32);
|
||||
printf("> Thread %d running\n", args->data[0].of.i32);
|
||||
return NULL;
|
||||
@@ -31,8 +31,6 @@ void* run(void* args_abs) {
|
||||
|
||||
// Rereate store and module.
|
||||
own wasm_store_t* store = wasm_store_new(args->engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
own wasm_module_t* module = wasm_module_obtain(store, args->module);
|
||||
|
||||
// Run the example N times.
|
||||
@@ -121,9 +119,6 @@ int main(int argc, const char *argv[]) {
|
||||
|
||||
// Compile and share.
|
||||
own wasm_store_t* store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
own wasm_module_t* module = wasm_module_new(store, &binary);
|
||||
if (!module) {
|
||||
printf("> Error compiling module!\n");
|
||||
|
||||
@@ -10,9 +10,9 @@ const int N_REPS = 3;
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
auto callback(
|
||||
void* env, const wasm::vec<wasm::Value>& args, wasm::vec<wasm::Value>& results
|
||||
void* env, const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
|
||||
) -> wasm::own<wasm::Trap> {
|
||||
assert(args[0].kind() == wasm::ValueKind::I32);
|
||||
assert(args[0].kind() == wasm::ValKind::I32);
|
||||
std::lock_guard<std::mutex> lock(*reinterpret_cast<std::mutex*>(env));
|
||||
std::cout << "Thread " << args[0].i32() << " running..." << std::endl;
|
||||
std::cout.flush();
|
||||
@@ -42,15 +42,15 @@ void run(
|
||||
|
||||
// Create imports.
|
||||
auto func_type = wasm::FuncType::make(
|
||||
wasm::ownvec<wasm::ValueType>::make(wasm::ValueType::make(wasm::ValueKind::I32)),
|
||||
wasm::ownvec<wasm::ValueType>::make()
|
||||
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::ValueType::make(wasm::ValueKind::I32), wasm::Mutability::CONST);
|
||||
wasm::ValType::make(wasm::ValKind::I32), wasm::Mutability::CONST);
|
||||
auto global = wasm::Global::make(
|
||||
store, global_type.get(), wasm::Value::i32(i));
|
||||
store, global_type.get(), wasm::Val::i32(i));
|
||||
|
||||
// Instantiate.
|
||||
auto imports = wasm::vec<wasm::Extern*>::make(func.get(), global.get());
|
||||
@@ -71,7 +71,7 @@ void run(
|
||||
auto run_func = exports[0]->func();
|
||||
|
||||
// Call.
|
||||
auto empty = wasm::vec<wasm::Value>::make();
|
||||
auto empty = wasm::vec<wasm::Val>::make();
|
||||
run_func->call(empty, empty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,16 +7,14 @@
|
||||
|
||||
#define own
|
||||
|
||||
wasm_store_t* store = NULL;
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* fail_callback(
|
||||
wasm_context_ref_mut_t* ctx, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n");
|
||||
own wasm_name_t message;
|
||||
wasm_name_new_from_string_nt(&message, "callback abort");
|
||||
own wasm_trap_t* trap = wasm_trap_new(store, &message);
|
||||
own wasm_trap_t* trap = wasm_trap_new((wasm_store_t*)env, &message);
|
||||
wasm_name_delete(&message);
|
||||
return trap;
|
||||
}
|
||||
@@ -36,9 +34,7 @@ int main(int argc, const char* argv[]) {
|
||||
// Initialize.
|
||||
printf("Initializing...\n");
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
store = wasm_store_new(engine);
|
||||
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||
wasm_store_context_set(store, ctx);
|
||||
wasm_store_t* store = wasm_store_new(engine);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
@@ -73,7 +69,12 @@ int main(int argc, const char* argv[]) {
|
||||
own wasm_functype_t* fail_type =
|
||||
wasm_functype_new_0_1(wasm_valtype_new_i32());
|
||||
own wasm_func_t* fail_func =
|
||||
wasm_func_new(store, fail_type, fail_callback);
|
||||
wasm_func_new_with_env(store, fail_type, fail_callback, store, NULL);
|
||||
|
||||
if (!fail_func) {
|
||||
printf("> Error compiling fail_func!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
wasm_functype_delete(fail_type);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user