Preprocessor definitions to build with/without lua/moon/js/wren #529
This commit is contained in:
		
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							| @@ -1,5 +1,5 @@ | ||||
| CC=gcc | ||||
| OPT=-O3 -Wall -std=c99 | ||||
| OPT=-O3 -Wall -std=gnu99 | ||||
| OPT_PRO=-DTIC80_PRO | ||||
| BIN_NAME= bin/tic80 | ||||
|  | ||||
| @@ -57,7 +57,6 @@ LINUX_ARM_LIBS= \ | ||||
| 	-L$(PRE_BUILT)/arm | ||||
|  | ||||
| LINUX_LINKER_LTO_FLAGS= \ | ||||
| 	-D_GNU_SOURCE \ | ||||
| 	-lSDL2 \ | ||||
| 	-llua \ | ||||
| 	-lwren \ | ||||
| @@ -69,7 +68,6 @@ LINUX_LINKER_LTO_FLAGS= \ | ||||
| 	-lz | ||||
|  | ||||
| LINUX_LINKER_FLAGS= \ | ||||
| 	-D_GNU_SOURCE \ | ||||
| 	-llua5.3 \ | ||||
| 	-lwren \ | ||||
| 	-ldl \ | ||||
| @@ -82,7 +80,6 @@ MINGW_OUTPUT=$(BIN_NAME).exe | ||||
|  | ||||
| EMS_CC=emcc | ||||
| EMS_OPT= \ | ||||
| 	-D_GNU_SOURCE \ | ||||
| 	-Wno-typedef-redefinition \ | ||||
| 	-s USE_SDL=2 \ | ||||
| 	-s TOTAL_MEMORY=67108864 \ | ||||
| @@ -101,8 +98,7 @@ EMS_LINKER_FLAGS= \ | ||||
| MACOSX_OPT= \ | ||||
| 	-mmacosx-version-min=10.6 \ | ||||
| 	-Wno-typedef-redefinition \ | ||||
| 	-D_THREAD_SAFE \ | ||||
| 	-D_GNU_SOURCE | ||||
| 	-D_THREAD_SAFE | ||||
|  | ||||
| MACOSX_LIBS= \ | ||||
| 	-L$(PRE_BUILT)/macos \ | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| LOCAL_PATH := $(call my-dir) | ||||
|  | ||||
| include $(CLEAR_VARS) | ||||
| LOCAL_CFLAGS += -O3 -Wall -std=c99 -D"log2(x)=(log(x)/log(2))" | ||||
| LOCAL_CFLAGS += -O3 -Wall -std=gnu99 -D"log2(x)=(log(x)/log(2))" | ||||
| LOCAL_MODULE := main | ||||
|  | ||||
| SRC_PATH := ../../../../src | ||||
|   | ||||
| @@ -22,46 +22,44 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #define TIC_BUILD_WITH_LUA 		1 | ||||
| #define TIC_BUILD_WITH_MOON 	1 | ||||
| #define TIC_BUILD_WITH_JS 		1 | ||||
| #define TIC_BUILD_WITH_WREN 	1 | ||||
|  | ||||
| #if defined(__APPLE__) | ||||
| /* lets us know what version of Mac OS X we're compiling on */ | ||||
| #include "AvailabilityMacros.h" | ||||
| #include "TargetConditionals.h" | ||||
|  | ||||
| 	#ifndef TARGET_OS_IPHONE | ||||
| 		#undef __TIC_MACOSX__ | ||||
| 		#define __TIC_MACOSX__  1 | ||||
| 		#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 | ||||
| 			# error SDL for Mac OS X only supports deploying on 10.6 and above. | ||||
| 		#endif /* MAC_OS_X_VERSION_MIN_REQUIRED < 1060 */ | ||||
| 	#endif /* TARGET_OS_IPHONE */ | ||||
|  | ||||
| #	include "AvailabilityMacros.h" | ||||
| #	include "TargetConditionals.h" | ||||
| #	ifndef TARGET_OS_IPHONE | ||||
| #		undef __TIC_MACOSX__ | ||||
| #		define __TIC_MACOSX__ 1 | ||||
| #		if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 | ||||
| #			error SDL for Mac OS X only supports deploying on 10.6 and above. | ||||
| #		endif /* MAC_OS_X_VERSION_MIN_REQUIRED < 1060 */ | ||||
| #	endif /* TARGET_OS_IPHONE */ | ||||
| #endif /* defined(__APPLE__) */ | ||||
|  | ||||
| #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) | ||||
| /* Try to find out if we're compiling for WinRT or non-WinRT */ | ||||
| #if defined(_MSC_VER) && defined(__has_include) | ||||
| #define HAVE_WINAPIFAMILY_H __has_include(<winapifamily.h>) | ||||
| /* If _USING_V110_SDK71_ is defined it means we are using the Windows XP toolset. */ | ||||
| #elif defined(_MSC_VER) && (_MSC_VER >= 1700 && !_USING_V110_SDK71_)    /* _MSC_VER == 1700 for Visual Studio 2012 */ | ||||
| #define HAVE_WINAPIFAMILY_H 1 | ||||
| #else | ||||
| #define HAVE_WINAPIFAMILY_H 0 | ||||
| #endif | ||||
|  | ||||
| #if HAVE_WINAPIFAMILY_H | ||||
| #include <winapifamily.h> | ||||
| #define WINAPI_FAMILY_WINRT (!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)) | ||||
| #else | ||||
| #define WINAPI_FAMILY_WINRT 0 | ||||
| #endif /* HAVE_WINAPIFAMILY_H */ | ||||
|  | ||||
| #if WINAPI_FAMILY_WINRT | ||||
| #undef __TIC_WINRT__ | ||||
| #define __TIC_WINRT__ 1 | ||||
| #else | ||||
| #undef __TIC_WINDOWS__ | ||||
| #define __TIC_WINDOWS__ 1 | ||||
| #endif | ||||
| #	if defined(_MSC_VER) && defined(__has_include) | ||||
| #		define HAVE_WINAPIFAMILY_H __has_include(<winapifamily.h>) | ||||
| #	elif defined(_MSC_VER) && (_MSC_VER >= 1700 && !_USING_V110_SDK71_) | ||||
| #		define HAVE_WINAPIFAMILY_H 1 | ||||
| #	else | ||||
| #		define HAVE_WINAPIFAMILY_H 0 | ||||
| #	endif | ||||
| #	if HAVE_WINAPIFAMILY_H | ||||
| #		include <winapifamily.h> | ||||
| #		define WINAPI_FAMILY_WINRT (!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)) | ||||
| #	else | ||||
| #		define WINAPI_FAMILY_WINRT 0 | ||||
| #	endif /* HAVE_WINAPIFAMILY_H */ | ||||
| #	if WINAPI_FAMILY_WINRT | ||||
| #		undef __TIC_WINRT__ | ||||
| #		define __TIC_WINRT__ 1 | ||||
| #	else | ||||
| #		undef __TIC_WINDOWS__ | ||||
| #		define __TIC_WINDOWS__ 1 | ||||
| #	endif | ||||
| #endif  | ||||
|  | ||||
| #if (defined(linux) || defined(__linux) || defined(__linux__)) | ||||
|   | ||||
							
								
								
									
										197
									
								
								src/console.c
									
									
									
									
									
								
							
							
						
						
									
										197
									
								
								src/console.c
									
									
									
									
									
								
							| @@ -52,10 +52,23 @@ | ||||
|  | ||||
| typedef enum | ||||
| { | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	LuaScript,	 | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 	MoonScript,	 | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 	JavaScript,	 | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 	WrenScript,	 | ||||
| #endif | ||||
|  | ||||
| } ScriptLang; | ||||
|  | ||||
| #if defined(__TIC_WINDOWS__) || defined(__TIC_LINUX__) || defined(__TIC_MACOSX__) | ||||
| @@ -80,10 +93,23 @@ typedef struct | ||||
| static const char* ExeExt = ".exe"; | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| static const char DefaultLuaTicPath[] = TIC_LOCAL "default.tic"; | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| static const char DefaultMoonTicPath[] = TIC_LOCAL "default_moon.tic"; | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| static const char DefaultJSTicPath[] = TIC_LOCAL "default_js.tic"; | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| static const char DefaultWrenTicPath[] = TIC_LOCAL "default_wren.tic"; | ||||
| #endif | ||||
| 	 | ||||
|  | ||||
| static const char* getName(const char* name, const char* ext) | ||||
| { | ||||
| @@ -421,18 +447,22 @@ static void* getDemoCart(Console* console, ScriptLang script, s32* size) | ||||
| 	{ | ||||
| 		switch(script) | ||||
| 		{ | ||||
| 		case LuaScript: | ||||
| 			strcpy(path, DefaultLuaTicPath); | ||||
| 			break; | ||||
| 		case MoonScript: | ||||
| 			strcpy(path, DefaultMoonTicPath); | ||||
| 			break; | ||||
| 		case JavaScript: | ||||
| 			strcpy(path, DefaultJSTicPath); | ||||
| 			break; | ||||
| 		case WrenScript: | ||||
| 			strcpy(path, DefaultWrenTicPath); | ||||
| 			break; | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 		case LuaScript: strcpy(path, DefaultLuaTicPath); break; | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 		case MoonScript: strcpy(path, DefaultMoonTicPath); break; | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 		case JavaScript: strcpy(path, DefaultJSTicPath); break; | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 		case WrenScript: strcpy(path, DefaultWrenTicPath); break; | ||||
| #endif			 | ||||
| 		} | ||||
|  | ||||
| 		void* data = fsLoadRootFile(console->fs, path, size); | ||||
| @@ -441,47 +471,68 @@ static void* getDemoCart(Console* console, ScriptLang script, s32* size) | ||||
| 			return data; | ||||
| 	} | ||||
|  | ||||
| 	static const u8 LuaDemoRom[] = | ||||
| 	{ | ||||
| 		#include "../bin/assets/luademo.tic.dat" | ||||
| 	}; | ||||
|  | ||||
| 	static const u8 JsDemoRom[] = | ||||
| 	{ | ||||
| 		#include "../bin/assets/jsdemo.tic.dat" | ||||
| 	}; | ||||
|  | ||||
| 	static const u8 MoonDemoRom[] = | ||||
| 	{ | ||||
| 		#include "../bin/assets/moondemo.tic.dat" | ||||
| 	}; | ||||
|  | ||||
| 	static const u8 WrenDemoRom[] = | ||||
| 	{ | ||||
| 		#include "../bin/assets/wrendemo.tic.dat" | ||||
| 	}; | ||||
|  | ||||
| 	const u8* demo = NULL; | ||||
| 	s32 romSize = 0; | ||||
|  | ||||
| 	switch(script) | ||||
| 	{ | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	case LuaScript: | ||||
| 		{ | ||||
| 			static const u8 LuaDemoRom[] = | ||||
| 			{ | ||||
| 				#include "../bin/assets/luademo.tic.dat" | ||||
| 			}; | ||||
|  | ||||
| 			demo = LuaDemoRom; | ||||
| 			romSize = sizeof LuaDemoRom;			 | ||||
| 		} | ||||
| 		break; | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 	case MoonScript: | ||||
| 		{ | ||||
| 			static const u8 MoonDemoRom[] = | ||||
| 			{ | ||||
| 				#include "../bin/assets/moondemo.tic.dat" | ||||
| 			}; | ||||
|  | ||||
| 			demo = MoonDemoRom; | ||||
| 			romSize = sizeof MoonDemoRom;			 | ||||
| 		} | ||||
| 		break; | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 	case JavaScript: | ||||
| 		{ | ||||
| 			static const u8 JsDemoRom[] = | ||||
| 			{ | ||||
| 				#include "../bin/assets/jsdemo.tic.dat" | ||||
| 			}; | ||||
|  | ||||
| 			demo = JsDemoRom; | ||||
| 			romSize = sizeof JsDemoRom; | ||||
| 		} | ||||
| 		break; | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 	case WrenScript: | ||||
| 		{ | ||||
| 			static const u8 WrenDemoRom[] = | ||||
| 			{ | ||||
| 				#include "../bin/assets/wrendemo.tic.dat" | ||||
| 			}; | ||||
|  | ||||
| 			demo = WrenDemoRom; | ||||
| 			romSize = sizeof WrenDemoRom;			 | ||||
| 		} | ||||
| 		break; | ||||
| #endif		 | ||||
| 	} | ||||
|  | ||||
| 	u8* data = NULL; | ||||
| @@ -508,14 +559,26 @@ static void onConsoleLoadDemoCommandConfirmed(Console* console, const char* para | ||||
|  | ||||
| 	console->showGameMenu = false; | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	if(strcmp(param, DefaultLuaTicPath) == 0) | ||||
| 		data = getDemoCart(console, LuaScript, &size); | ||||
| 	else if(strcmp(param, DefaultMoonTicPath) == 0) | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 	if(strcmp(param, DefaultMoonTicPath) == 0) | ||||
| 		data = getDemoCart(console, MoonScript, &size); | ||||
| 	else if(strcmp(param, DefaultJSTicPath) == 0) | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 	if(strcmp(param, DefaultJSTicPath) == 0) | ||||
| 		data = getDemoCart(console, JavaScript, &size); | ||||
| 	else if(strcmp(param, DefaultWrenTicPath) == 0) | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 	if(strcmp(param, DefaultWrenTicPath) == 0) | ||||
| 		data = getDemoCart(console, WrenScript, &size); | ||||
| #endif | ||||
|  | ||||
| 	const char* name = getCartName(param); | ||||
|  | ||||
| @@ -1073,17 +1136,44 @@ static void loadDemo(Console* console, ScriptLang script) | ||||
|  | ||||
| static void onConsoleNewCommandConfirmed(Console* console, const char* param) | ||||
| { | ||||
| 	bool done = false; | ||||
|  | ||||
| 	if(param && strlen(param)) | ||||
| 	{ | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 		if(strcmp(param, "lua") == 0) | ||||
| 		{ | ||||
| 			loadDemo(console, LuaScript); | ||||
| 		else if(strcmp(param, "moon") == 0 || strcmp(param, "moonscript") == 0) | ||||
| 			done = true; | ||||
| 		} | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 		if(strcmp(param, "moon") == 0 || strcmp(param, "moonscript") == 0) | ||||
| 		{ | ||||
| 			loadDemo(console, MoonScript); | ||||
| 		else if(strcmp(param, "js") == 0 || strcmp(param, "javascript") == 0) | ||||
| 			done = true; | ||||
| 		} | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 		if(strcmp(param, "js") == 0 || strcmp(param, "javascript") == 0) | ||||
| 		{ | ||||
| 			loadDemo(console, JavaScript); | ||||
| 		else if(strcmp(param, "wren") == 0) | ||||
| 			done = true; | ||||
| 		} | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 		if(strcmp(param, "wren") == 0) | ||||
| 		{ | ||||
| 			loadDemo(console, WrenScript); | ||||
| 		else | ||||
| 			done = true; | ||||
| 		} | ||||
| #endif			 | ||||
|  | ||||
| 		if(!done) | ||||
| 		{ | ||||
| 			printError(console, "\nunknown parameter: "); | ||||
| 			printError(console, param); | ||||
| @@ -1091,9 +1181,14 @@ static void onConsoleNewCommandConfirmed(Console* console, const char* param) | ||||
| 			return; | ||||
| 		} | ||||
| 	} | ||||
| 	else loadDemo(console, LuaScript); | ||||
|  | ||||
| 	printBack(console, "\nnew cart is created"); | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	else loadDemo(console, LuaScript); | ||||
| #endif | ||||
|  | ||||
| 	if(done) printBack(console, "\nnew cart is created"); | ||||
| 	else printError(console, "\ncart not created"); | ||||
|  | ||||
| 	commandDone(console); | ||||
| } | ||||
|  | ||||
| @@ -1335,22 +1430,36 @@ static void onConsoleConfigCommand(Console* console, const char* param) | ||||
| 		console->config->reset(console->config); | ||||
| 		printBack(console, "\nconfiguration reset :)"); | ||||
| 	} | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	else if(strcmp(param, "default") == 0 || strcmp(param, "default lua") == 0) | ||||
| 	{ | ||||
| 		onConsoleLoadDemoCommand(console, DefaultLuaTicPath); | ||||
| 	} | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 	else if(strcmp(param, "default moon") == 0 || strcmp(param, "default moonscript") == 0) | ||||
| 	{ | ||||
| 		onConsoleLoadDemoCommand(console, DefaultMoonTicPath); | ||||
| 	} | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 	else if(strcmp(param, "default js") == 0) | ||||
| 	{ | ||||
| 		onConsoleLoadDemoCommand(console, DefaultJSTicPath); | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 	else if(strcmp(param, "default wren") == 0) | ||||
| 	{ | ||||
| 		onConsoleLoadDemoCommand(console, DefaultWrenTicPath); | ||||
| 	} | ||||
| #endif | ||||
| 	 | ||||
| 	else | ||||
| 	{ | ||||
| 		printError(console, "\nunknown parameter: "); | ||||
| @@ -2660,7 +2769,13 @@ static void tick(Console* console) | ||||
| 	{ | ||||
| 		if(!console->embed.yes) | ||||
| 		{ | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 			loadDemo(console, LuaScript); | ||||
| #elif defined(TIC_BUILD_WITH_JS) | ||||
| 			loadDemo(console, JavaScript); | ||||
| #elif defined(TIC_BUILD_WITH_WREN) | ||||
| 			loadDemo(console, WrenScript); | ||||
| #endif			 | ||||
|  | ||||
| 			printBack(console, "\n hello! type "); | ||||
| 			printFront(console, "help"); | ||||
|   | ||||
| @@ -21,6 +21,9 @@ | ||||
| // SOFTWARE. | ||||
|  | ||||
| #include "machine.h" | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
|  | ||||
| #include "tools.h" | ||||
|  | ||||
| #include <ctype.h> | ||||
| @@ -1026,3 +1029,9 @@ const tic_script_config* getJsScriptConfig() | ||||
| { | ||||
| 	return &JsSyntaxConfig; | ||||
| } | ||||
|  | ||||
| #else | ||||
|  | ||||
| s32 duk_timeout_check(void* udata){return 0;} | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_JS) */ | ||||
							
								
								
									
										198
									
								
								src/luaapi.c
									
									
									
									
									
								
							
							
						
						
									
										198
									
								
								src/luaapi.c
									
									
									
									
									
								
							| @@ -20,6 +20,10 @@ | ||||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
| // SOFTWARE. | ||||
|  | ||||
| #include "machine.h" | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
|  | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <lua.h> | ||||
| @@ -27,9 +31,6 @@ | ||||
| #include <lualib.h> | ||||
| #include <ctype.h> | ||||
|  | ||||
| #include "machine.h" | ||||
| #include "moonscript.h" | ||||
|  | ||||
| #define LUA_LOC_STACK 1E8 // 100.000.000 | ||||
|  | ||||
| static const char TicMachine[] = "_TIC80"; | ||||
| @@ -1130,22 +1131,6 @@ static s32 lua_loadfile(lua_State *lua) | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static void setloaded(lua_State* l, char* name) | ||||
| { | ||||
| 	s32 top = lua_gettop(l); | ||||
| 	lua_getglobal(l, "package"); | ||||
| 	lua_getfield(l, -1, "loaded"); | ||||
| 	lua_getfield(l, -1, name); | ||||
| 	if (lua_isnil(l, -1)) { | ||||
| 		lua_pop(l, 1); | ||||
| 		lua_pushvalue(l, top); | ||||
| 		lua_setfield(l, -2, name); | ||||
| 	} | ||||
|  | ||||
| 	lua_settop(l, top); | ||||
| } | ||||
|  | ||||
| static const char* const ApiKeywords[] = API_KEYWORDS; | ||||
| static const lua_CFunction ApiFunc[] =  | ||||
| { | ||||
| @@ -1237,82 +1222,6 @@ static bool initLua(tic_mem* tic, const char* code) | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| #define MOON_CODE(...) #__VA_ARGS__ | ||||
|  | ||||
| static const char* execute_moonscript_src = MOON_CODE( | ||||
| 	local fn, err = require('moonscript.base').loadstring(...) | ||||
|  | ||||
| 	if not fn then | ||||
| 		return err | ||||
| 	end | ||||
| 	return fn() | ||||
| ); | ||||
|  | ||||
| static bool initMoonscript(tic_mem* tic, const char* code) | ||||
| { | ||||
| 	tic_machine* machine = (tic_machine*)tic; | ||||
| 	closeLua(tic); | ||||
|  | ||||
| 	lua_State* lua = machine->lua = luaL_newstate(); | ||||
|  | ||||
| 	static const luaL_Reg loadedlibs[] = | ||||
| 	{ | ||||
| 		{ "_G", luaopen_base }, | ||||
| 		{ LUA_LOADLIBNAME, luaopen_package }, | ||||
| 		{ LUA_COLIBNAME, luaopen_coroutine }, | ||||
| 		{ LUA_TABLIBNAME, luaopen_table }, | ||||
| 		{ LUA_STRLIBNAME, luaopen_string }, | ||||
| 		{ LUA_MATHLIBNAME, luaopen_math }, | ||||
| 		{ LUA_DBLIBNAME, luaopen_debug }, | ||||
| 		{ NULL, NULL } | ||||
| 	}; | ||||
|  | ||||
| 	for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) | ||||
| 	{ | ||||
| 		luaL_requiref(lua, lib->name, lib->func, 1); | ||||
| 		lua_pop(lua, 1); | ||||
| 	} | ||||
|  | ||||
| 	luaopen_lpeg(lua); | ||||
| 	setloaded(lua, "lpeg"); | ||||
|  | ||||
| 	initAPI(machine); | ||||
|  | ||||
| 	{ | ||||
| 		lua_State* moon = machine->lua; | ||||
|  | ||||
| 		lua_settop(moon, 0); | ||||
|  | ||||
| 		if (luaL_loadbuffer(moon, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") != LUA_OK) | ||||
| 		{ | ||||
| 			machine->data->error(machine->data->data, "failed to load moonscript.lua"); | ||||
| 			return false; | ||||
| 		} | ||||
|  | ||||
| 		lua_call(moon, 0, 0); | ||||
|  | ||||
| 		if (luaL_loadbuffer(moon, execute_moonscript_src, strlen(execute_moonscript_src), "execute_moonscript") != LUA_OK) | ||||
| 		{ | ||||
| 			machine->data->error(machine->data->data, "failed to load moonscript compiler"); | ||||
| 			return false; | ||||
| 		} | ||||
|  | ||||
| 		lua_pushstring(moon, code); | ||||
| 		if (lua_pcall(moon, 1, 1, 0) != LUA_OK) | ||||
| 		{ | ||||
| 			const char* msg = lua_tostring(moon, -1); | ||||
|  | ||||
| 			if (msg) | ||||
| 			{ | ||||
| 				machine->data->error(machine->data->data, msg); | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| static void callLuaTick(tic_mem* tic) | ||||
| { | ||||
| 	tic_machine* machine = (tic_machine*)tic; | ||||
| @@ -1482,6 +1391,101 @@ const tic_script_config* getLuaScriptConfig() | ||||
| 	return &LuaSyntaxConfig; | ||||
| } | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_MOON) | ||||
|  | ||||
| #include "moonscript.h" | ||||
|  | ||||
| #define MOON_CODE(...) #__VA_ARGS__ | ||||
|  | ||||
| static const char* execute_moonscript_src = MOON_CODE( | ||||
| 	local fn, err = require('moonscript.base').loadstring(...) | ||||
|  | ||||
| 	if not fn then | ||||
| 		return err | ||||
| 	end | ||||
| 	return fn() | ||||
| ); | ||||
|  | ||||
| static void setloaded(lua_State* l, char* name) | ||||
| { | ||||
| 	s32 top = lua_gettop(l); | ||||
| 	lua_getglobal(l, "package"); | ||||
| 	lua_getfield(l, -1, "loaded"); | ||||
| 	lua_getfield(l, -1, name); | ||||
| 	if (lua_isnil(l, -1)) { | ||||
| 		lua_pop(l, 1); | ||||
| 		lua_pushvalue(l, top); | ||||
| 		lua_setfield(l, -2, name); | ||||
| 	} | ||||
|  | ||||
| 	lua_settop(l, top); | ||||
| } | ||||
|  | ||||
| static bool initMoonscript(tic_mem* tic, const char* code) | ||||
| { | ||||
| 	tic_machine* machine = (tic_machine*)tic; | ||||
| 	closeLua(tic); | ||||
|  | ||||
| 	lua_State* lua = machine->lua = luaL_newstate(); | ||||
|  | ||||
| 	static const luaL_Reg loadedlibs[] = | ||||
| 	{ | ||||
| 		{ "_G", luaopen_base }, | ||||
| 		{ LUA_LOADLIBNAME, luaopen_package }, | ||||
| 		{ LUA_COLIBNAME, luaopen_coroutine }, | ||||
| 		{ LUA_TABLIBNAME, luaopen_table }, | ||||
| 		{ LUA_STRLIBNAME, luaopen_string }, | ||||
| 		{ LUA_MATHLIBNAME, luaopen_math }, | ||||
| 		{ LUA_DBLIBNAME, luaopen_debug }, | ||||
| 		{ NULL, NULL } | ||||
| 	}; | ||||
|  | ||||
| 	for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) | ||||
| 	{ | ||||
| 		luaL_requiref(lua, lib->name, lib->func, 1); | ||||
| 		lua_pop(lua, 1); | ||||
| 	} | ||||
|  | ||||
| 	luaopen_lpeg(lua); | ||||
| 	setloaded(lua, "lpeg"); | ||||
|  | ||||
| 	initAPI(machine); | ||||
|  | ||||
| 	{ | ||||
| 		lua_State* moon = machine->lua; | ||||
|  | ||||
| 		lua_settop(moon, 0); | ||||
|  | ||||
| 		if (luaL_loadbuffer(moon, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") != LUA_OK) | ||||
| 		{ | ||||
| 			machine->data->error(machine->data->data, "failed to load moonscript.lua"); | ||||
| 			return false; | ||||
| 		} | ||||
|  | ||||
| 		lua_call(moon, 0, 0); | ||||
|  | ||||
| 		if (luaL_loadbuffer(moon, execute_moonscript_src, strlen(execute_moonscript_src), "execute_moonscript") != LUA_OK) | ||||
| 		{ | ||||
| 			machine->data->error(machine->data->data, "failed to load moonscript compiler"); | ||||
| 			return false; | ||||
| 		} | ||||
|  | ||||
| 		lua_pushstring(moon, code); | ||||
| 		if (lua_pcall(moon, 1, 1, 0) != LUA_OK) | ||||
| 		{ | ||||
| 			const char* msg = lua_tostring(moon, -1); | ||||
|  | ||||
| 			if (msg) | ||||
| 			{ | ||||
| 				machine->data->error(machine->data->data, msg); | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| static const char* const MoonKeywords [] = | ||||
| { | ||||
| 	"false", "true", "nil", "return", | ||||
| @@ -1571,3 +1575,7 @@ const tic_script_config* getMoonScriptConfig() | ||||
| { | ||||
| 	return &MoonSyntaxConfig; | ||||
| } | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_MOON) */ | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|   | ||||
| @@ -123,9 +123,17 @@ typedef struct | ||||
|  | ||||
| 	struct | ||||
| 	{ | ||||
| 		struct duk_hthread* js; | ||||
| #if defined(TIC_BUILD_WITH_LUA) || defined(TIC_BUILD_WITH_MOON) | ||||
| 		struct lua_State* lua; | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 		struct duk_hthread* js; | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 		struct WrenVM* wren; | ||||
| #endif	 | ||||
| 	}; | ||||
|  | ||||
| 	blip_buffer_t* blip; | ||||
| @@ -161,7 +169,20 @@ s32 drawSpriteFont(tic_mem* memory, u8 symbol, s32 x, s32 y, s32 width, s32 heig | ||||
| s32 drawFixedSpriteFont(tic_mem* memory, u8 index, s32 x, s32 y, s32 width, s32 height, u8 chromakey, s32 scale); | ||||
| void parseCode(const tic_script_config* config, const char* start, u8* color, const tic_code_theme* theme); | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| const tic_script_config* getLuaScriptConfig(); | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| const tic_script_config* getMoonScriptConfig(); | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| const tic_script_config* getJsScriptConfig(); | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| const tic_script_config* getWrenScriptConfig(); | ||||
| #endif | ||||
|   | ||||
							
								
								
									
										57
									
								
								src/tic.c
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								src/tic.c
									
									
									
									
									
								
							| @@ -558,10 +558,23 @@ void tic_close(tic_mem* memory) | ||||
|  | ||||
| 	machine->state.initialized = false; | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	getLuaScriptConfig()->close(memory); | ||||
|  | ||||
| #	if defined(TIC_BUILD_WITH_MOON) | ||||
| 	getMoonScriptConfig()->close(memory); | ||||
| #	endif | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_LUA) */ | ||||
|  | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 	getJsScriptConfig()->close(memory); | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 	getWrenScriptConfig()->close(memory); | ||||
| #endif | ||||
|  | ||||
| 	blip_delete(machine->blip); | ||||
|  | ||||
| @@ -1517,29 +1530,32 @@ static bool compareMetatag(const char* code, const char* tag, const char* value, | ||||
| 	return result; | ||||
| } | ||||
|  | ||||
| static bool isMoonscript(const char* code) | ||||
| { | ||||
| 	return compareMetatag(code, "script", "moon", getMoonScriptConfig()->singleComment)  | ||||
| 		|| compareMetatag(code, "script", "moonscript", getMoonScriptConfig()->singleComment); | ||||
| } | ||||
|  | ||||
| static bool isJavascript(const char* code) | ||||
| { | ||||
| 	return compareMetatag(code, "script", "js", getJsScriptConfig()->singleComment)  | ||||
| 		|| compareMetatag(code, "script", "javascript", getJsScriptConfig()->singleComment); | ||||
| } | ||||
|  | ||||
| static bool isWren(const char* code) | ||||
| { | ||||
| 	return compareMetatag(code, "script", "wren", getWrenScriptConfig()->singleComment); | ||||
| } | ||||
|  | ||||
| static const tic_script_config* getScriptConfig(const char* code) | ||||
| { | ||||
| 	if(isMoonscript(code)) return getMoonScriptConfig(); | ||||
| 	if(isJavascript(code)) return getJsScriptConfig(); | ||||
| 	if(isWren(code)) return getWrenScriptConfig(); | ||||
| #if defined(TIC_BUILD_WITH_MOON) | ||||
| 	if(compareMetatag(code, "script", "moon", getMoonScriptConfig()->singleComment) || | ||||
| 		compareMetatag(code, "script", "moonscript", getMoonScriptConfig()->singleComment))  | ||||
| 		return getMoonScriptConfig(); | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_JS) | ||||
| 	if(compareMetatag(code, "script", "js", getJsScriptConfig()->singleComment) || | ||||
| 		compareMetatag(code, "script", "javascript", getJsScriptConfig()->singleComment))  | ||||
| 		return getJsScriptConfig(); | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
| 	if(compareMetatag(code, "script", "wren", getWrenScriptConfig()->singleComment))  | ||||
| 		return getWrenScriptConfig(); | ||||
| #endif | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_LUA) | ||||
| 	return getLuaScriptConfig(); | ||||
| #elif defined(TIC_BUILD_WITH_JS) | ||||
| 	return getJsScriptConfig(); | ||||
| #elif defined(TIC_BUILD_WITH_WREN) | ||||
| 	return getWrenScriptConfig(); | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static const tic_script_config* api_get_script_config(tic_mem* memory) | ||||
| @@ -1592,6 +1608,7 @@ static void api_tick(tic_mem* tic, tic_tick_data* data) | ||||
| 				config = getScriptConfig(code); | ||||
| 				cart2ram(tic); | ||||
|  | ||||
| 				machine->state.synced = 0; | ||||
| 				tic->input.data = 0; | ||||
| 				 | ||||
| 				if(compareMetatag(code, "input", "mouse", config->singleComment)) | ||||
|   | ||||
| @@ -20,12 +20,15 @@ | ||||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
| // SOFTWARE. | ||||
|  | ||||
| #include "machine.h" | ||||
|  | ||||
| #if defined(TIC_BUILD_WITH_WREN) | ||||
|  | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <stdio.h> | ||||
| #include <ctype.h> | ||||
|  | ||||
| #include "machine.h" | ||||
| #include "tools.h" | ||||
| #include "wren.h" | ||||
|  | ||||
| @@ -1369,3 +1372,5 @@ const tic_script_config* getWrenScriptConfig() | ||||
| { | ||||
| 	return &WrenSyntaxConfig; | ||||
| } | ||||
|  | ||||
| #endif /* defined(TIC_BUILD_WITH_WREN) */ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 BADIM-PC\Vadim
					BADIM-PC\Vadim