extensions
Steven Massey 5 years ago
parent afa8478e00
commit ae705f0316

@ -227,7 +227,7 @@ typedef int64_t (* M3Callback) (IM3Function i_currentFunction, void * i_ref);
// const char * const i_functionName, const char * const i_signature);
M3Result m3_Call (IM3Function i_function);
M3Result m3_CallWithArgs (IM3Function i_function, int32_t i_argc, const char * const * i_argv);
// void * /* return */ m3_Call (IM3Function i_function, M3Result * o_result);

@ -133,8 +133,11 @@ void ReleaseRuntime (IM3Runtime i_runtime)
void m3_FreeRuntime (IM3Runtime i_runtime)
{
ReleaseRuntime (i_runtime);
free (i_runtime);
if (i_runtime)
{
ReleaseRuntime (i_runtime);
free (i_runtime);
}
}
@ -385,19 +388,52 @@ M3Result m3_FindFunction (IM3Function * o_function, IM3Runtime i_runtime, cons
M3Result m3_Call (IM3Function i_function)
{
return m3_CallWithArgs (i_function, 0, NULL);
}
M3Result m3_CallWithArgs (IM3Function i_function, i32 i_argc, ccstr_t * i_argv)
{
M3Result result = c_m3Err_none;
if (i_function->compiled)
{
IM3Runtime env = i_function->module->runtime;
IM3Module module = i_function->module;
m3stack_t stack = env->stack;
stack += i_function->numLocals;
IM3Runtime env = module->runtime;
_ (Module_EnsureMemorySize (i_function->module, & i_function->module->memory, 3000000));
_ (Module_EnsureMemorySize (module, & i_function->module->memory, 3000000));
u8 * linearMemory = i_function->module->memory.wasmPages;
u8 * linearMemory = module->memory.wasmPages;
m3stack_t stack = env->stack;
if (i_argc)
{
IM3Memory memory = & module->memory;
// FIX: memory allocation in general
i32 offset = AllocateHeap (memory, sizeof (i32) * i_argc);
i32 * pointers = (i32 *) (memory->wasmPages + offset);
for (i32 i = 0; i < i_argc; ++i)
{
size_t argLength = strlen (i_argv [i]) + 1;
if (argLength < 4000)
{
i32 o = AllocateHeap (memory, (i32) argLength);
memcpy (memory->wasmPages + o, i_argv [i], argLength);
* pointers++ = o;
}
else throw ("insane argument string length");
}
stack [0] = i_argc;
stack [1] = offset;
}
_ (Call (i_function->compiled, stack, linearMemory, d_m3OpDefaultArgs));

@ -82,6 +82,8 @@ typedef struct M3Memory
}
M3Memory;
typedef M3Memory * IM3Memory;
//---------------------------------------------------------------------------------------------------------------------------------

@ -68,18 +68,6 @@ void m3_abort (i32 i_dunno)
}
i32 AllocateHeap (M3Memory * io_memory, i32 i_size)
{
i_size = (i_size + 7) & ~7;
size_t ptrOffset = io_memory->heapOffset + (io_memory->heapAllocated += i_size);
size_t size = (u8 *) io_memory->mallocated->end - io_memory->wasmPages;
assert (ptrOffset < size);
return (i32) ptrOffset;
}
i32 m3_malloc (IM3Module i_module, i32 i_size)
{
@ -187,11 +175,13 @@ void m3Out_f64 (double i_value)
printf ("%lf\n", i_value);
}
void m3Out_i32 (i32 i_value)
{
printf ("m3_out: %d %u\n", i_value, (u32) i_value);
}
void m3TestOut (int32_t i_int0, double i_double, int32_t i_int1)
{
printf ("0: %d, 1: %lf, 2: %d\n", i_int0, i_double, i_int1);
@ -214,13 +204,15 @@ void m3Export (const void * i_data, i32 i_size)
fclose (f);
}
m3ret_t m3_exit (i32 i_code)
{
printf ("exit: %d\n", i_code);
printf ("exit (%d)\n", i_code);
return c_m3Err_trapExit;
}
M3Result SuppressLookupFailure (M3Result i_result)
{
if (i_result == c_m3Err_functionLookupFailed)
@ -229,6 +221,7 @@ M3Result SuppressLookupFailure (M3Result i_result)
return i_result;
}
M3Result m3_LinkCStd (IM3Module io_module)
{
M3Result result = c_m3Err_none;

@ -8,7 +8,7 @@
#include "m3_module.h"
#include <assert.h>
void m3_FreeModule (IM3Module i_module)
{
@ -156,4 +156,16 @@ M3Result Module_EnsureMemorySize (IM3Module i_module, M3Memory * io_memory, m3
}
i32 AllocateHeap (M3Memory * io_memory, i32 i_size)
{
i_size = (i_size + 7) & ~7;
size_t ptrOffset = io_memory->heapOffset + (io_memory->heapAllocated += i_size);
size_t size = (u8 *) io_memory->mallocated->end - io_memory->wasmPages;
assert (ptrOffset < size);
return (i32) ptrOffset;
}

@ -11,6 +11,9 @@
#include "m3_env.h"
M3Result Module_EnsureMemorySize (IM3Module i_module, M3Memory * io_memory, m3word_t i_memorySize);
M3Result Module_EnsureMemorySize (IM3Module i_module, IM3Memory io_memory, m3word_t i_memorySize);
i32 AllocateHeap (M3Memory * io_memory, i32 i_size);
#endif /* m3_module_h */

@ -92,19 +92,19 @@ int main (int i_argc, const char * i_argv [])
clock_t start = clock ();
result = m3_Call (main);
// result = m3_Call (main);
result = m3_CallWithArgs (main, i_argc, i_argv);
clock_t end = clock ();
double elapsed_time = (end - start) / (double) CLOCKS_PER_SEC ;
printf("%lf\n", elapsed_time);
printf ("call: %s\n", result);
// printf ("call: %s\n", result);
m3_PrintProfilerInfo ();
}
m3_FreeRuntime (env);
env = nullptr;
}
catch (const M3Result & r) {}
@ -117,9 +117,10 @@ int main (int i_argc, const char * i_argv [])
{
M3ErrorInfo info = m3_GetErrorInfo (env);
printf ("%s\n", info.message);
m3_FreeRuntime (env);
}
}
m3_FreeRuntime (env);
}
free (wasm);

Loading…
Cancel
Save