Switch to using C in main apps

extensions
Volodymyr Shymanskyy 5 years ago
parent c749c3f4b3
commit 474f4cf40c

@ -51,7 +51,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
file(GLOB M3_SRC "source/*.c" "source/*.cpp" "${APP_DIR}/*.cpp")
file(GLOB M3_SRC "source/*.c" "source/*.cpp" "${APP_DIR}/*.c")
include_directories("./source/")

@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "m3.h"
#include "m3_host.h"
#define FATAL(msg, ...) { printf("Fatal: " msg "\n", ##__VA_ARGS__); return 1; }
int main (int i_argc, const char * i_argv [])
{
M3Result result = c_m3Err_none;
if (i_argc < 3) FATAL("not enough arguments");
u8* wasm = NULL;
u32 fsize = 0;
//printf("Loading WebAssembly...\n");
FILE* f = fopen (i_argv[1], "rb");
if (f)
{
fseek (f, 0, SEEK_END);
fsize = ftell(f);
fseek (f, 0, SEEK_SET);
if (fsize > 1000000) FATAL("file is too big");
wasm = (u8*) malloc(fsize);
fread (wasm, 1, fsize, f);
fclose (f);
}
IM3Module module;
result = m3_ParseModule (& module, wasm, fsize);
if (result) FATAL("m3_ParseModule: %s", result);
IM3Runtime env = m3_NewRuntime (4096);
if (!env) FATAL("m3_NewRuntime");
result = m3_LoadModule (env, module);
if (result) FATAL("m3_LoadModule: %s", result);
/*
m3_LinkFunction (module, "_m3TestOut", "v(iFi)", (void *) m3TestOut);
m3_LinkFunction (module, "_m3StdOut", "v(*)", (void *) m3Output);
m3_LinkFunction (module, "_m3Export", "v(*i)", (void *) m3Export);
m3_LinkFunction (module, "_m3Out_f64", "v(F)", (void *) m3Out_f64);
m3_LinkFunction (module, "_m3Out_i32", "v(i)", (void *) m3Out_i32);
m3_LinkFunction (module, "_TestReturn", "F(i)", (void *) TestReturn);
m3_LinkFunction (module, "abortStackOverflow", "v(i)", (void *) m3_abort);
result = m3_LinkCStd (module); if (result) FATAL("m3_LinkCStd: %s", result);
m3_PrintRuntimeInfo (env);
*/
IM3Function func;
result = m3_FindFunction (& func, env, "__post_instantiate");
if (not result)
{
result = m3_Call (func);
if (result) FATAL("__post_instantiate failed: %s", result);
}
const char* fname = i_argv[2];
result = m3_FindFunction (& func, env, fname);
if (result) FATAL("m3_FindFunction: %s", result);
//printf("Running...\n");
if (!strcmp(fname, "_main") || !strcmp(fname, "main")) {
i_argc -= 2;
i_argv += 2;
result = m3_CallMain (func, i_argc, i_argv);
} else {
i_argc -= 3;
i_argv += 3;
result = m3_CallWithArgs (func, i_argc, i_argv);
}
m3_FreeRuntime (env);
free (wasm);
if (result) FATAL("m3_Call: %s", result);
printf ("\n");
return 0;
}

@ -1,150 +0,0 @@
//
//
// Created by Steven Massey on 4/15/19.
// Copyright © 2019 Steven Massey. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "m3.hpp"
extern "C"
{
# include "m3_core.h"
}
#include "m3_host.h"
void m3Output (const char * i_string)
{
printf ("%s\n", i_string);
}
int main (int i_argc, const char * i_argv [])
{
M3Result result = c_m3Err_none;
if (i_argc >= 2)
{
FILE * f = fopen (i_argv [1], "rb");
if (f)
{
fseek (f, 0, SEEK_END);
size_t fsize = ftell (f);
fseek (f, 0, SEEK_SET);
if (fsize < 100000)
{
u8 * wasm = (u8 *) malloc (fsize);
if (wasm)
{
IM3Runtime env = nullptr;
try
{
fread (wasm, 1, fsize, f);
fclose (f);
IM3Module module;
result = m3_ParseModule (& module, wasm, (u32) fsize); if (result) throw result;
env = m3_NewRuntime (32768);
result = m3_LoadModule (env, module); if (result) throw result;
m3_LinkFunction (module, "_m3TestOut", "v(iFi)", (void *) m3TestOut);
m3_LinkFunction (module, "_m3StdOut", "v(*)", (void *) m3Output);
m3_LinkFunction (module, "_m3Export", "v(*i)", (void *) m3Export);
m3_LinkFunction (module, "_m3Out_f64", "v(F)", (void *) m3Out_f64);
m3_LinkFunction (module, "_m3Out_i32", "v(i)", (void *) m3Out_i32);
m3_LinkFunction (module, "_TestReturn", "F(i)", (void *) TestReturn);
m3_LinkFunction (module, "abortStackOverflow", "v(i)", (void *) m3_abort);
result = m3_LinkCStd (module); if (result) throw result;
//m3_PrintRuntimeInfo (env);
IM3Function f;
result = m3_FindFunction (& f, env, "__post_instantiate");
if (not result)
{
result = m3_Call (f); if (result) throw result;
}
const char* fname = i_argv[2];
IM3Function main;
result = m3_FindFunction (& main, env, fname); if (result) throw result;
if (main)
{
clock_t start = clock ();
if (!strcmp(fname, "_main") || !strcmp(fname, "main")) {
if (i_argc)
{
i_argc -= 2;
i_argv += 2;
}
result = m3_CallMain (main, i_argc, i_argv);
} else {
if (i_argc)
{
i_argc -= 3;
i_argv += 3;
}
result = m3_CallWithArgs (main, i_argc, i_argv);
}
#ifdef M3_PRINT_TIME
clock_t end = clock ();
double elapsed_time = (end - start) / (double) CLOCKS_PER_SEC ;
printf("Time: %.3lf s\n", elapsed_time);
#endif
// printf ("call: %s\n", result);
//m3_PrintProfilerInfo ();
} else {
printf ("%s not found\n", fname);
}
}
catch (const M3Result & r) {}
if (result)
{
printf ("Error: %s", result);
if (env)
{
M3ErrorInfo info = m3_GetErrorInfo (env);
printf (" (%s)", info.message);
}
printf ("\n");
return 1;
}
m3_FreeRuntime (env);
}
free (wasm);
}
}
else printf ("couldn't open '%s'\n", i_argv [1]);
} else printf ("not enough arguments\n");
printf ("\n");
return 0;
}

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <time.h>
#include "m3.hpp"
#include "m3.h"
#include "extra/fib32.wasm.h"
@ -12,8 +12,8 @@ void run_wasm()
{
M3Result result = c_m3Err_none;
u8* wasm = (u8*)fib32_wasm;
u32 fsize = fib32_wasm_len-1;
uint8_t* wasm = (uint8_t*)fib32_wasm;
size_t fsize = fib32_wasm_len-1;
printf("Loading WebAssembly...\n");
Loading…
Cancel
Save