Bring back exports
This commit is contained in:
		
							parent
							
								
									6c91caddc0
								
							
						
					
					
						commit
						d9ab264f81
					
				
							
								
								
									
										48
									
								
								src/main.c
								
								
								
								
							
							
						
						
									
										48
									
								
								src/main.c
								
								
								
								
							| 
						 | 
					@ -382,9 +382,9 @@ void* orca_runloop(void* user)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//NOTE: Find and type check event handlers.
 | 
						//NOTE: Find and type check event handlers.
 | 
				
			||||||
	for(int i=0; i<G_EVENT_COUNT; i++)
 | 
						for(int i=0; i<G_EXPORT_COUNT; i++)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		const g_event_handler_desc* desc = &G_EVENT_HANDLER_DESC[i];
 | 
							const g_export_desc* desc = &G_EXPORT_DESC[i];
 | 
				
			||||||
		IM3Function handler = 0;
 | 
							IM3Function handler = 0;
 | 
				
			||||||
		m3_FindFunction(&handler, app->runtime.m3Runtime, desc->name.ptr);
 | 
							m3_FindFunction(&handler, app->runtime.m3Runtime, desc->name.ptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -427,7 +427,7 @@ void* orca_runloop(void* user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(checked)
 | 
								if(checked)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				app->runtime.eventHandlers[i] = handler;
 | 
									app->runtime.exports[i] = handler;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
| 
						 | 
					@ -455,12 +455,12 @@ void* orca_runloop(void* user)
 | 
				
			||||||
	//NOTE: prepare GL surface
 | 
						//NOTE: prepare GL surface
 | 
				
			||||||
	mg_surface_prepare(app->surface);
 | 
						mg_surface_prepare(app->surface);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IM3Function* eventHandlers = app->runtime.eventHandlers;
 | 
						IM3Function* exports = app->runtime.exports;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//NOTE: call init handler
 | 
						//NOTE: call init handler
 | 
				
			||||||
	if(eventHandlers[G_EVENT_START])
 | 
						if(exports[G_EXPORT_ON_INIT])
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		M3Result err = m3_Call(eventHandlers[G_EVENT_START], 0, 0);
 | 
							M3Result err = m3_Call(exports[G_EXPORT_ON_INIT], 0, 0);
 | 
				
			||||||
		if(err != NULL)
 | 
							if(err != NULL)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			log_error("runtime error: %s\n", err);
 | 
								log_error("runtime error: %s\n", err);
 | 
				
			||||||
| 
						 | 
					@ -477,13 +477,13 @@ void* orca_runloop(void* user)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if(eventHandlers[G_EVENT_FRAME_RESIZE])
 | 
						if(exports[G_EXPORT_FRAME_RESIZE])
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		mp_rect frame = mg_surface_get_frame(app->surface);
 | 
							mp_rect frame = mg_surface_get_frame(app->surface);
 | 
				
			||||||
		u32 width = (u32)frame.w;
 | 
							u32 width = (u32)frame.w;
 | 
				
			||||||
		u32 height = (u32)frame.h;
 | 
							u32 height = (u32)frame.h;
 | 
				
			||||||
		const void* args[2] = {&width, &height};
 | 
							const void* args[2] = {&width, &height};
 | 
				
			||||||
		m3_Call(eventHandlers[G_EVENT_FRAME_RESIZE], 2, args);
 | 
							m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ui_set_context(&app->debugOverlay.ui);
 | 
						ui_set_context(&app->debugOverlay.ui);
 | 
				
			||||||
| 
						 | 
					@ -499,14 +499,14 @@ void* orca_runloop(void* user)
 | 
				
			||||||
				ui_process_event(event);
 | 
									ui_process_event(event);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(eventHandlers[G_EVENT_RAW_EVENT])
 | 
								if(exports[G_EXPORT_RAW_EVENT])
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				#ifndef M3_BIG_ENDIAN
 | 
									#ifndef M3_BIG_ENDIAN
 | 
				
			||||||
				mp_event* eventPtr = (mp_event*)wasm_memory_offset_to_ptr(&app->runtime.wasmMemory, app->runtime.rawEventOffset);
 | 
									mp_event* eventPtr = (mp_event*)wasm_memory_offset_to_ptr(&app->runtime.wasmMemory, app->runtime.rawEventOffset);
 | 
				
			||||||
				memcpy(eventPtr, event, sizeof(*event));
 | 
									memcpy(eventPtr, event, sizeof(*event));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				const void* args[1] = {&app->runtime.rawEventOffset};
 | 
									const void* args[1] = {&app->runtime.rawEventOffset};
 | 
				
			||||||
				m3_Call(eventHandlers[G_EVENT_RAW_EVENT], 1, args);
 | 
									m3_Call(exports[G_EXPORT_RAW_EVENT], 1, args);
 | 
				
			||||||
				#else
 | 
									#else
 | 
				
			||||||
				log_error("OnRawEvent() is not supported on big endian platforms");
 | 
									log_error("OnRawEvent() is not supported on big endian platforms");
 | 
				
			||||||
				#endif
 | 
									#endif
 | 
				
			||||||
| 
						 | 
					@ -526,12 +526,12 @@ void* orca_runloop(void* user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//					mg_surface_set_frame(app->debugOverlay.surface, frame);
 | 
					//					mg_surface_set_frame(app->debugOverlay.surface, frame);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					if(eventHandlers[G_EVENT_FRAME_RESIZE])
 | 
										if(exports[G_EXPORT_FRAME_RESIZE])
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						u32 width = (u32)event->frame.rect.w;
 | 
											u32 width = (u32)event->frame.rect.w;
 | 
				
			||||||
						u32 height = (u32)event->frame.rect.h;
 | 
											u32 height = (u32)event->frame.rect.h;
 | 
				
			||||||
						const void* args[2] = {&width, &height};
 | 
											const void* args[2] = {&width, &height};
 | 
				
			||||||
						m3_Call(eventHandlers[G_EVENT_FRAME_RESIZE], 2, args);
 | 
											m3_Call(exports[G_EXPORT_FRAME_RESIZE], 2, args);
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				} break;
 | 
									} break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -539,30 +539,30 @@ void* orca_runloop(void* user)
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					if(event->key.action == MP_KEY_PRESS)
 | 
										if(event->key.action == MP_KEY_PRESS)
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						if(eventHandlers[G_EVENT_MOUSE_DOWN])
 | 
											if(exports[G_EXPORT_MOUSE_DOWN])
 | 
				
			||||||
						{
 | 
											{
 | 
				
			||||||
							int key = event->key.code;
 | 
												int key = event->key.code;
 | 
				
			||||||
							const void* args[1] = {&key};
 | 
												const void* args[1] = {&key};
 | 
				
			||||||
							m3_Call(eventHandlers[G_EVENT_MOUSE_DOWN], 1, args);
 | 
												m3_Call(exports[G_EXPORT_MOUSE_DOWN], 1, args);
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					else
 | 
										else
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						if(eventHandlers[G_EVENT_MOUSE_UP])
 | 
											if(exports[G_EXPORT_MOUSE_UP])
 | 
				
			||||||
						{
 | 
											{
 | 
				
			||||||
							int key = event->key.code;
 | 
												int key = event->key.code;
 | 
				
			||||||
							const void* args[1] = {&key};
 | 
												const void* args[1] = {&key};
 | 
				
			||||||
							m3_Call(eventHandlers[G_EVENT_MOUSE_UP], 1, args);
 | 
												m3_Call(exports[G_EXPORT_MOUSE_UP], 1, args);
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				} break;
 | 
									} break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				case MP_EVENT_MOUSE_MOVE:
 | 
									case MP_EVENT_MOUSE_MOVE:
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					if(eventHandlers[G_EVENT_MOUSE_MOVE])
 | 
										if(exports[G_EXPORT_MOUSE_MOVE])
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						const void* args[4] = {&event->move.x, &event->move.y, &event->move.deltaX, &event->move.deltaY};
 | 
											const void* args[4] = {&event->move.x, &event->move.y, &event->move.deltaX, &event->move.deltaY};
 | 
				
			||||||
						m3_Call(eventHandlers[G_EVENT_MOUSE_MOVE], 4, args);
 | 
											m3_Call(exports[G_EXPORT_MOUSE_MOVE], 4, args);
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				} break;
 | 
									} break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -577,18 +577,18 @@ void* orca_runloop(void* user)
 | 
				
			||||||
						#endif
 | 
											#endif
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
						if(eventHandlers[G_EVENT_KEY_DOWN])
 | 
											if(exports[G_EXPORT_KEY_DOWN])
 | 
				
			||||||
						{
 | 
											{
 | 
				
			||||||
							const void* args[1] = {&event->key.code};
 | 
												const void* args[1] = {&event->key.code};
 | 
				
			||||||
							m3_Call(eventHandlers[G_EVENT_KEY_DOWN], 1, args);
 | 
												m3_Call(exports[G_EXPORT_KEY_DOWN], 1, args);
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					else if(event->key.action == MP_KEY_RELEASE)
 | 
										else if(event->key.action == MP_KEY_RELEASE)
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						if(eventHandlers[G_EVENT_KEY_UP])
 | 
											if(exports[G_EXPORT_KEY_UP])
 | 
				
			||||||
						{
 | 
											{
 | 
				
			||||||
							const void* args[1] = {&event->key.code};
 | 
												const void* args[1] = {&event->key.code};
 | 
				
			||||||
							m3_Call(eventHandlers[G_EVENT_KEY_UP], 1, args);
 | 
												m3_Call(exports[G_EXPORT_KEY_UP], 1, args);
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				} break;
 | 
									} break;
 | 
				
			||||||
| 
						 | 
					@ -735,10 +735,10 @@ void* orca_runloop(void* user)
 | 
				
			||||||
			mg_render(app->debugOverlay.surface, app->debugOverlay.canvas);
 | 
								mg_render(app->debugOverlay.surface, app->debugOverlay.canvas);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(eventHandlers[G_EVENT_FRAME_REFRESH])
 | 
							if(exports[G_EXPORT_FRAME_REFRESH])
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			mg_surface_prepare(app->surface);
 | 
								mg_surface_prepare(app->surface);
 | 
				
			||||||
			m3_Call(eventHandlers[G_EVENT_FRAME_REFRESH], 0, 0);
 | 
								m3_Call(exports[G_EXPORT_FRAME_REFRESH], 0, 0);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(app->debugOverlay.show)
 | 
							if(app->debugOverlay.show)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,41 +14,41 @@
 | 
				
			||||||
#include"m3_env.h"
 | 
					#include"m3_env.h"
 | 
				
			||||||
#include"m3_compile.h"
 | 
					#include"m3_compile.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define G_EVENTS(X) \
 | 
					#define G_EXPORTS(X) \
 | 
				
			||||||
	X(G_EVENT_START, "OnInit", "", "") \
 | 
						X(G_EXPORT_ON_INIT, "OnInit", "", "") \
 | 
				
			||||||
	X(G_EVENT_MOUSE_DOWN, "OnMouseDown", "", "i") \
 | 
						X(G_EXPORT_MOUSE_DOWN, "OnMouseDown", "", "i") \
 | 
				
			||||||
	X(G_EVENT_MOUSE_UP, "OnMouseUp", "", "i") \
 | 
						X(G_EXPORT_MOUSE_UP, "OnMouseUp", "", "i") \
 | 
				
			||||||
	X(G_EVENT_MOUSE_ENTER, "OnMouseEnter", "", "") \
 | 
						X(G_EXPORT_MOUSE_ENTER, "OnMouseEnter", "", "") \
 | 
				
			||||||
	X(G_EVENT_MOUSE_LEAVE, "OnMouseLeave", "", "") \
 | 
						X(G_EXPORT_MOUSE_LEAVE, "OnMouseLeave", "", "") \
 | 
				
			||||||
	X(G_EVENT_MOUSE_MOVE, "OnMouseMove", "", "ffff") \
 | 
						X(G_EXPORT_MOUSE_MOVE, "OnMouseMove", "", "ffff") \
 | 
				
			||||||
	X(G_EVENT_MOUSE_WHEEL, "OnMouseWheel", "", "ff") \
 | 
						X(G_EXPORT_MOUSE_WHEEL, "OnMouseWheel", "", "ff") \
 | 
				
			||||||
	X(G_EVENT_KEY_DOWN, "OnKeyDown", "", "i") \
 | 
						X(G_EXPORT_KEY_DOWN, "OnKeyDown", "", "i") \
 | 
				
			||||||
	X(G_EVENT_KEY_UP, "OnKeyUp", "", "i") \
 | 
						X(G_EXPORT_KEY_UP, "OnKeyUp", "", "i") \
 | 
				
			||||||
	X(G_EVENT_FRAME_REFRESH, "OnFrameRefresh", "", "") \
 | 
						X(G_EXPORT_FRAME_REFRESH, "OnFrameRefresh", "", "") \
 | 
				
			||||||
	X(G_EVENT_FRAME_RESIZE, "OnFrameResize", "", "ii") \
 | 
						X(G_EXPORT_FRAME_RESIZE, "OnFrameResize", "", "ii") \
 | 
				
			||||||
	X(G_EVENT_RAW_EVENT, "OnRawEvent", "", "i") \
 | 
						X(G_EXPORT_RAW_EVENT, "OnRawEvent", "", "i") \
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef enum {
 | 
					typedef enum {
 | 
				
			||||||
	#define G_EVENT_KIND(kind, ...) kind,
 | 
						#define G_EXPORT_KIND(kind, ...) kind,
 | 
				
			||||||
	G_EVENTS(G_EVENT_KIND)
 | 
						G_EXPORTS(G_EXPORT_KIND)
 | 
				
			||||||
	G_EVENT_COUNT
 | 
						G_EXPORT_COUNT
 | 
				
			||||||
} guest_event_kind;
 | 
					} guest_export_kind;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct g_event_handler_desc
 | 
					typedef struct g_export_desc
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	str8 name;
 | 
						str8 name;
 | 
				
			||||||
	str8 retTags;
 | 
						str8 retTags;
 | 
				
			||||||
	str8 argTags;
 | 
						str8 argTags;
 | 
				
			||||||
} g_event_handler_desc;
 | 
					} g_export_desc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const g_event_handler_desc G_EVENT_HANDLER_DESC[] = {
 | 
					const g_export_desc G_EXPORT_DESC[] = {
 | 
				
			||||||
	#define STR8LIT(s) {sizeof(s)-1, s} //NOTE: msvc doesn't accept STR8(s) as compile-time constant...
 | 
						#define STR8LIT(s) {sizeof(s)-1, s} //NOTE: msvc doesn't accept STR8(s) as compile-time constant...
 | 
				
			||||||
	#define G_EVENT_HANDLER_DESC_ENTRY(kind, name, rets, args) {STR8LIT(name), STR8LIT(rets), STR8LIT(args)},
 | 
						#define G_EXPORT_DESC_ENTRY(kind, name, rets, args) {STR8LIT(name), STR8LIT(rets), STR8LIT(args)},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	G_EVENTS(G_EVENT_HANDLER_DESC_ENTRY)
 | 
						G_EXPORTS(G_EXPORT_DESC_ENTRY)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#undef G_EVENT_HANDLER_DESC_ENTRY
 | 
						#undef G_EXPORT_DESC_ENTRY
 | 
				
			||||||
	#undef STR8LIT
 | 
						#undef STR8LIT
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,7 +69,7 @@ typedef struct orca_runtime
 | 
				
			||||||
	IM3Environment m3Env;
 | 
						IM3Environment m3Env;
 | 
				
			||||||
	IM3Runtime m3Runtime;
 | 
						IM3Runtime m3Runtime;
 | 
				
			||||||
	IM3Module m3Module;
 | 
						IM3Module m3Module;
 | 
				
			||||||
	IM3Function eventHandlers[G_EVENT_COUNT];
 | 
						IM3Function exports[G_EXPORT_COUNT];
 | 
				
			||||||
	u32 rawEventOffset;
 | 
						u32 rawEventOffset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} orca_runtime;
 | 
					} orca_runtime;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue