Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Chromium .clang-format file, and apply it #115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BasedOnStyle: Chromium
33 changes: 16 additions & 17 deletions example/callback.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "wasm.h"

Expand Down Expand Up @@ -34,9 +34,7 @@ void wasm_val_print(wasm_val_t val) {
}

// A function to be called from Wasm code.
own wasm_trap_t* print_callback(
const wasm_val_t args[], wasm_val_t results[]
) {
own wasm_trap_t* print_callback(const wasm_val_t args[], wasm_val_t results[]) {
printf("Calling back...\n> ");
wasm_val_print(args[0]);
printf("\n");
Expand All @@ -45,11 +43,10 @@ own wasm_trap_t* print_callback(
return NULL;
}


// A function closure.
own wasm_trap_t* closure_callback(
void* env, const wasm_val_t args[], wasm_val_t results[]
) {
own wasm_trap_t* closure_callback(void* env,
const wasm_val_t args[],
wasm_val_t results[]) {
int i = *(int*)env;
printf("Calling back closure...\n");
printf("> %d\n", i);
Expand All @@ -59,7 +56,6 @@ own wasm_trap_t* closure_callback(
return NULL;
}


int main(int argc, const char* argv[]) {
// Initialize.
printf("Initializing...\n");
Expand Down Expand Up @@ -96,23 +92,26 @@ int main(int argc, const char* argv[]) {

// Create external print functions.
printf("Creating callback...\n");
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);
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_with_env(store, closure_type, closure_callback, &i, NULL);
own wasm_functype_t* closure_type =
wasm_functype_new_0_1(wasm_valtype_new_i32());
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);

// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {
wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
};
const wasm_extern_t* imports[] = {wasm_func_as_extern(print_func),
wasm_func_as_extern(closure_func)};
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
Expand Down
36 changes: 15 additions & 21 deletions example/callback.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include <fstream>
#include <cinttypes>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <cinttypes>

#include "wasm.hh"

Expand Down Expand Up @@ -34,27 +34,23 @@ auto operator<<(std::ostream& out, const wasm::Val& val) -> std::ostream& {
}

// A function to be called from Wasm code.
auto print_callback(
const wasm::Val args[], wasm::Val results[]
) -> wasm::own<wasm::Trap> {
auto print_callback(const wasm::Val args[], wasm::Val results[])
-> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl << "> " << args[0] << std::endl;
results[0] = args[0].copy();
return nullptr;
}


// A function closure.
auto closure_callback(
void* env, const wasm::Val args[], wasm::Val results[]
) -> wasm::own<wasm::Trap> {
auto closure_callback(void* env, const wasm::Val args[], 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::Val::i32(static_cast<int32_t>(i));
return nullptr;
}


void run() {
// Initialize.
std::cout << "Initializing..." << std::endl;
Expand Down Expand Up @@ -87,19 +83,18 @@ 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::I32)),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32)));
auto print_func = wasm::Func::make(store, print_type.get(), print_callback);

// Creating closure.
std::cout << "Creating closure..." << std::endl;
int i = 42;
auto closure_type = wasm::FuncType::make(
wasm::ownvec<wasm::ValType>::make(),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32))
);
auto closure_func = wasm::Func::make(store, closure_type.get(), closure_callback, &i);
wasm::ownvec<wasm::ValType>::make(),
wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::I32)));
auto closure_func =
wasm::Func::make(store, closure_type.get(), closure_callback, &i);

// Instantiate.
std::cout << "Instantiating module..." << std::endl;
Expand All @@ -113,7 +108,8 @@ 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::EXTERN_FUNC ||
!exports[0]->func()) {
std::cout << "> Error accessing export!" << std::endl;
exit(1);
}
Expand All @@ -136,10 +132,8 @@ void run() {
std::cout << "Shutting down..." << std::endl;
}


int main(int argc, const char* argv[]) {
run();
std::cout << "Done." << std::endl;
return 0;
}

10 changes: 6 additions & 4 deletions example/finalize.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "wasm.h"

Expand All @@ -13,7 +13,8 @@ int live_count = 0;

void finalize(void* data) {
int i = (int)data;
if (i % (iterations / 10) == 0) printf("Finalizing #%d...\n", i);
if (i % (iterations / 10) == 0)
printf("Finalizing #%d...\n", i);
--live_count;
}

Expand Down Expand Up @@ -49,9 +50,10 @@ void run_in_store(wasm_store_t* store) {
// Instantiate.
printf("Instantiating modules...\n");
for (int i = 0; i <= iterations; ++i) {
if (i % (iterations / 10) == 0) printf("%d\n", i);
if (i % (iterations / 10) == 0)
printf("%d\n", i);
own wasm_instance_t* instance =
wasm_instance_new(store, module, NULL, NULL);
wasm_instance_new(store, module, NULL, NULL);
if (!instance) {
printf("> Error instantiating module %d!\n", i);
exit(1);
Expand Down
13 changes: 5 additions & 8 deletions example/finalize.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <iostream>
#include <fstream>
#include <cinttypes>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <cinttypes>

#include "wasm.hh"


const int iterations = 100000;

int live_count = 0;
Expand Down Expand Up @@ -45,7 +44,8 @@ void run_in_store(wasm::Store* store) {
// Instantiate.
std::cout << "Instantiating modules..." << std::endl;
for (int i = 0; i <= iterations; ++i) {
if (i % (iterations / 10) == 0) std::cout << i << std::endl;
if (i % (iterations / 10) == 0)
std::cout << i << std::endl;
auto instance = wasm::Instance::make(store, module.get(), nullptr);
if (!instance) {
std::cout << "> Error instantiating module " << i << "!" << std::endl;
Expand All @@ -59,7 +59,6 @@ void run_in_store(wasm::Store* store) {
std::cout << "Shutting down..." << std::endl;
}


void run() {
// Initialize.
std::cout << "Initializing..." << std::endl;
Expand Down Expand Up @@ -92,12 +91,10 @@ void run() {
std::cout << "Deleting store 1..." << std::endl;
}


int main(int argc, const char* argv[]) {
run();
std::cout << "Live count " << live_count << std::endl;
assert(live_count == 0);
std::cout << "Done." << std::endl;
return 0;
}

68 changes: 32 additions & 36 deletions example/global.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "wasm.h"

Expand All @@ -23,28 +23,26 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
return wasm_extern_as_func(exports->data[i]);
}


#define check(val, type, expected) \
if (val.of.type != expected) { \
#define check(val, type, expected) \
if (val.of.type != expected) { \
printf("> Error reading value\n"); \
exit(1); \
exit(1); \
}

#define check_global(global, type, expected) \
{ \
wasm_val_t val; \
wasm_global_get(global, &val); \
check(val, type, expected); \
{ \
wasm_val_t val; \
wasm_global_get(global, &val); \
check(val, type, expected); \
}

#define check_call(func, type, expected) \
{ \
wasm_val_t results[1]; \
{ \
wasm_val_t results[1]; \
wasm_func_call(func, NULL, results); \
check(results[0], type, expected); \
check(results[0], type, expected); \
}


int main(int argc, const char* argv[]) {
// Initialize.
printf("Initializing...\n");
Expand Down Expand Up @@ -81,27 +79,27 @@ int main(int argc, const char* argv[]) {

// Create external globals.
printf("Creating globals...\n");
own wasm_globaltype_t* const_f32_type = wasm_globaltype_new(
wasm_valtype_new(WASM_F32), WASM_CONST);
own wasm_globaltype_t* const_i64_type = wasm_globaltype_new(
wasm_valtype_new(WASM_I64), WASM_CONST);
own wasm_globaltype_t* var_f32_type = wasm_globaltype_new(
wasm_valtype_new(WASM_F32), WASM_VAR);
own wasm_globaltype_t* var_i64_type = wasm_globaltype_new(
wasm_valtype_new(WASM_I64), WASM_VAR);
own wasm_globaltype_t* const_f32_type =
wasm_globaltype_new(wasm_valtype_new(WASM_F32), WASM_CONST);
own wasm_globaltype_t* const_i64_type =
wasm_globaltype_new(wasm_valtype_new(WASM_I64), WASM_CONST);
own wasm_globaltype_t* var_f32_type =
wasm_globaltype_new(wasm_valtype_new(WASM_F32), WASM_VAR);
own wasm_globaltype_t* var_i64_type =
wasm_globaltype_new(wasm_valtype_new(WASM_I64), WASM_VAR);

wasm_val_t val_f32_1 = {.kind = WASM_F32, .of = {.f32 = 1}};
own wasm_global_t* const_f32_import =
wasm_global_new(store, const_f32_type, &val_f32_1);
wasm_global_new(store, const_f32_type, &val_f32_1);
wasm_val_t val_i64_2 = {.kind = WASM_I64, .of = {.i64 = 2}};
own wasm_global_t* const_i64_import =
wasm_global_new(store, const_i64_type, &val_i64_2);
wasm_global_new(store, const_i64_type, &val_i64_2);
wasm_val_t val_f32_3 = {.kind = WASM_F32, .of = {.f32 = 3}};
own wasm_global_t* var_f32_import =
wasm_global_new(store, var_f32_type, &val_f32_3);
wasm_global_new(store, var_f32_type, &val_f32_3);
wasm_val_t val_i64_4 = {.kind = WASM_I64, .of = {.i64 = 4}};
own wasm_global_t* var_i64_import =
wasm_global_new(store, var_i64_type, &val_i64_4);
wasm_global_new(store, var_i64_type, &val_i64_4);

wasm_globaltype_delete(const_f32_type);
wasm_globaltype_delete(const_i64_type);
Expand All @@ -110,14 +108,12 @@ int main(int argc, const char* argv[]) {

// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {
wasm_global_as_extern(const_f32_import),
wasm_global_as_extern(const_i64_import),
wasm_global_as_extern(var_f32_import),
wasm_global_as_extern(var_i64_import)
};
const wasm_extern_t* imports[] = {wasm_global_as_extern(const_f32_import),
wasm_global_as_extern(const_i64_import),
wasm_global_as_extern(var_f32_import),
wasm_global_as_extern(var_i64_import)};
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
Expand Down Expand Up @@ -195,13 +191,13 @@ int main(int argc, const char* argv[]) {
check_call(get_var_i64_export, i64, 38);

// Modify variables through calls and check again.
wasm_val_t args73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} };
wasm_val_t args73[] = {{.kind = WASM_F32, .of = {.f32 = 73}}};
wasm_func_call(set_var_f32_import, args73, NULL);
wasm_val_t args74[] = { {.kind = WASM_I64, .of = {.i64 = 74}} };
wasm_val_t args74[] = {{.kind = WASM_I64, .of = {.i64 = 74}}};
wasm_func_call(set_var_i64_import, args74, NULL);
wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77}} };
wasm_val_t args77[] = {{.kind = WASM_F32, .of = {.f32 = 77}}};
wasm_func_call(set_var_f32_export, args77, NULL);
wasm_val_t args78[] = { {.kind = WASM_I64, .of = {.i64 = 78}} };
wasm_val_t args78[] = {{.kind = WASM_I64, .of = {.i64 = 78}}};
wasm_func_call(set_var_i64_export, args78, NULL);

check_global(var_f32_import, f32, 73);
Expand Down
Loading