Skip to content

Commit

Permalink
lang loading moved to core
Browse files Browse the repository at this point in the history
  • Loading branch information
nesbox committed Apr 28, 2024
1 parent 518d7b2 commit 7c98dcb
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 216 deletions.
30 changes: 17 additions & 13 deletions cmake/core.cmake
Expand Up @@ -7,7 +7,6 @@ macro(MACRO_CORE SCRIPT DEFINE BUILD_DEPRECATED)
set(TIC80CORE_DIR ${CMAKE_SOURCE_DIR}/src)
set(TIC80CORE_SRC
${TIC80CORE_DIR}/core/core.c
${TIC80CORE_DIR}/core/languages.c
${TIC80CORE_DIR}/core/draw.c
${TIC80CORE_DIR}/core/io.c
${TIC80CORE_DIR}/core/sound.c
Expand Down Expand Up @@ -38,56 +37,61 @@ macro(MACRO_CORE SCRIPT DEFINE BUILD_DEPRECATED)
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src)

target_link_libraries(tic80core${SCRIPT} blipbuf zlib)
target_link_libraries(tic80core${SCRIPT} PRIVATE blipbuf zlib)


if(BUILD_STATIC)
if(BUILD_WITH_LUA)
target_link_libraries(tic80core${SCRIPT} lua)
target_link_libraries(tic80core${SCRIPT} PRIVATE lua)
endif()

if(BUILD_WITH_JS)
target_link_libraries(tic80core${SCRIPT} js)
target_link_libraries(tic80core${SCRIPT} PRIVATE js)
endif()

if(BUILD_WITH_SCHEME)
target_link_libraries(tic80core${SCRIPT} scheme)
target_link_libraries(tic80core${SCRIPT} PRIVATE scheme)
endif()

if(BUILD_WITH_SQUIRREL)
target_link_libraries(tic80core${SCRIPT} squirrel)
target_link_libraries(tic80core${SCRIPT} PRIVATE squirrel)
endif()

if(BUILD_WITH_PYTHON)
target_link_libraries(tic80core${SCRIPT} python)
target_link_libraries(tic80core${SCRIPT} PRIVATE python)
endif()

if(BUILD_WITH_WREN)
target_link_libraries(tic80core${SCRIPT} wren)
target_link_libraries(tic80core${SCRIPT} PRIVATE wren)
endif()

if(BUILD_WITH_MRUBY)
target_link_libraries(tic80core${SCRIPT} ruby)
target_link_libraries(tic80core${SCRIPT} PRIVATE ruby)
endif()

if(BUILD_WITH_JANET)
target_link_libraries(tic80core${SCRIPT} janet)
target_link_libraries(tic80core${SCRIPT} PRIVATE janet)
endif()

if(BUILD_WITH_WASM)
target_link_libraries(tic80core${SCRIPT} wasm)
target_link_libraries(tic80core${SCRIPT} PRIVATE wasm)
endif()

target_compile_definitions(tic80core${SCRIPT} PUBLIC TIC_RUNTIME_STATIC)

elseif(WIN32)
target_include_directories(tic80core${SCRIPT} PRIVATE ${THIRDPARTY_DIR}/dlfcn/src)
add_library(dlfcn STATIC ${THIRDPARTY_DIR}/dlfcn/src/dlfcn.c)
target_link_libraries(tic80core${SCRIPT} PRIVATE dlfcn)
endif()

if(${BUILD_DEPRECATED})
target_compile_definitions(tic80core${SCRIPT} PRIVATE BUILD_DEPRECATED)
target_link_libraries(tic80core${SCRIPT} giflib)
target_link_libraries(tic80core${SCRIPT} PRIVATE giflib)
endif()

if(LINUX)
target_link_libraries(tic80core${SCRIPT} m)
target_link_libraries(tic80core${SCRIPT} PRIVATE m)
endif()

target_compile_definitions(tic80core${SCRIPT} PUBLIC ${DEFINE})
Expand Down
10 changes: 1 addition & 9 deletions cmake/studio.cmake
Expand Up @@ -39,15 +39,7 @@ add_library(tic80studio STATIC
${CMAKE_SOURCE_DIR}/build/assets/cart.png.dat)

if(WIN32)
add_library(dlfcn STATIC ${THIRDPARTY_DIR}/dlfcn/src/dlfcn.c)

target_link_libraries(tic80studio PRIVATE dlfcn)

target_include_directories(tic80studio
PRIVATE
${THIRDPARTY_DIR}/dirent/include
${THIRDPARTY_DIR}/dlfcn/src
)
target_include_directories(tic80studio PRIVATE ${THIRDPARTY_DIR}/dirent/include)
endif()

target_include_directories(tic80studio PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
4 changes: 2 additions & 2 deletions src/api.h
Expand Up @@ -29,7 +29,7 @@
#define MAX_SUPPORTED_LANGS (16)

// convenience macros to loop languages
#define FOREACH_LANG(lang) for(const tic_script_config **conf = Languages, *lang = *conf; *conf != NULL; lang = *++conf)
#define FOREACH_LANG(lang) for(const tic_script_config *lang = Languages, *end = lang + MAX_SUPPORTED_LANGS; lang != end && lang->id; lang++)

typedef struct { u8 index; tic_flip flip; tic_rotate rotate; } RemapResult;
typedef void(*RemapFunc)(void*, s32 x, s32 y, RemapResult* result);
Expand Down Expand Up @@ -122,7 +122,7 @@ typedef struct

} tic_script_config;

extern const tic_script_config* Languages[];
extern tic_script_config Languages[];

typedef enum
{
Expand Down
193 changes: 189 additions & 4 deletions src/core/core.c
Expand Up @@ -32,6 +32,10 @@
#include <stddef.h>
#include <time.h>

#if !defined(TIC_RUNTIME_STATIC)
#include <dlfcn.h>
#endif

#include "tic_assert.h"

#ifdef _3DS
Expand All @@ -42,12 +46,14 @@

static_assert(TIC_BANK_BITS == 3, "tic_bank_bits");
static_assert(sizeof(tic_map) < 1024 * 32, "tic_map");
static_assert(sizeof(tic_rgb) == 3, "tic_rgb");
static_assert(sizeof(tic_palette) == 48, "tic_palette");
static_assert(sizeof(((tic_vram *)0)->vars) == 4, "tic_vram vars");
static_assert(sizeof(tic_rgb) == 3, "tic_rgb");
static_assert(sizeof(tic_palette) == 48, "tic_palette");
static_assert(sizeof(((tic_vram *)0)->vars) == 4, "tic_vram vars");
static_assert(sizeof(tic_vram) == TIC_VRAM_SIZE, "tic_vram");
static_assert(sizeof(tic_ram) == TIC_RAM_SIZE, "tic_ram");

tic_script_config Languages[MAX_SUPPORTED_LANGS] = {};

u8 tic_api_peek(tic_mem* memory, s32 address, s32 bits)
{
if (address < 0)
Expand Down Expand Up @@ -267,7 +273,7 @@ const tic_script_config* tic_core_script_config(tic_mem* memory)
return it;
}

return Languages[0];
return Languages->id ? Languages : NULL;
}

static void updateSaveid(tic_mem* memory)
Expand Down Expand Up @@ -544,6 +550,10 @@ void tic_core_resume(tic_mem* memory)
}
}

#if !defined(TIC_RUNTIME_STATIC)
static void* LoadedModules[MAX_SUPPORTED_LANGS] = {};
#endif

void tic_core_close(tic_mem* memory)
{
tic_core* core = (tic_core*)memory;
Expand All @@ -561,6 +571,14 @@ void tic_core_close(tic_mem* memory)
free(memory->product.screen);
#endif
free(memory->product.samples.buffer);

#if !defined(TIC_RUNTIME_STATIC)
for(void **it = LoadedModules, **end = it + COUNT_OF(LoadedModules); it != end; ++it)
{
dlclose(*it);
}
#endif

free(core);
}

Expand Down Expand Up @@ -733,6 +751,171 @@ void tic_core_blit(tic_mem* tic)
tic_core_blit_ex(tic, (tic_blit_callback){scanline, border, NULL});
}

#if defined(TIC_RUNTIME_STATIC)

#if defined (TIC_BUILD_WITH_LUA)
extern tic_script_config LuaSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_MRUBY)
extern tic_script_config RubySyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_JS)
extern tic_script_config JsSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_MOON)
extern tic_script_config MoonSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_FENNEL)
extern tic_script_config FennelSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_SQUIRREL)
extern tic_script_config SquirrelSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_SCHEME)
extern tic_script_config SchemeSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_WREN)
extern tic_script_config WrenSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_WASM)
extern tic_script_config WasmSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_JANET)
extern tic_script_config JanetSyntaxConfig;
#endif

#if defined(TIC_BUILD_WITH_PYTHON)
extern tic_script_config PythonSyntaxConfig;
#endif

static void loadLangs()
{
static const tic_script_config *LocalLanguages[] =
{
#if defined (TIC_BUILD_WITH_LUA)
&LuaSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_MRUBY)
&RubySyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_JS)
&JsSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_MOON)
&MoonSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_FENNEL)
&FennelSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_SCHEME)
&SchemeSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_SQUIRREL)
&SquirrelSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_WREN)
&WrenSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_WASM)
&WasmSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_JANET)
&JanetSyntaxConfig,
#endif

#if defined(TIC_BUILD_WITH_PYTHON)
&PythonSyntaxConfig,
#endif

};

s32 i = 0;
for(const tic_script_config **it = LocalLanguages, **end = it + COUNT_OF(LocalLanguages); it != end; it++, i++)
{
Languages[i] = **it;
}
}

#else

#if defined(__TIC_WINDOWS__)
#define MODULE_EXT ".dll"
#elif defined(__TIC_MACOSX__)
#define MODULE_EXT ".dylib"
#elif defined(__TIC_LINUX__) || defined(__TIC_ANDROID__)
#define MODULE_EXT ".so"
#endif

#define CONFIG_SUFFUX "SyntaxConfig"

static void loadModule(const char *module_name, const char *config_name)
{
void *module = dlopen(module_name, RTLD_NOW | RTLD_LOCAL);

if(module)
{
const tic_script_config *config = dlsym(module, config_name);

if(config)
{
printf("config is loaded: %s\n", config->name);

s32 count = 0;
FOREACH_LANG(_) count++;

if(count < MAX_SUPPORTED_LANGS)
{
Languages[count] = *config;
LoadedModules[count] = module;
}
}
}
}

static void loadLangs()
{
// !TODO: scan working directory for all dlls
static const struct Module {const char *config; const char* file;} Modules[] =
{
{"Lua" CONFIG_SUFFUX, "lua" MODULE_EXT},
{"Moon" CONFIG_SUFFUX, "lua" MODULE_EXT},
{"Fennel" CONFIG_SUFFUX, "lua" MODULE_EXT},
{"Ruby" CONFIG_SUFFUX, "ruby" MODULE_EXT},
{"Js" CONFIG_SUFFUX, "js" MODULE_EXT},
{"Scheme" CONFIG_SUFFUX, "scheme" MODULE_EXT},
{"Squirrel" CONFIG_SUFFUX, "squirrel" MODULE_EXT},
{"Wren" CONFIG_SUFFUX, "wren" MODULE_EXT},
{"Wasm" CONFIG_SUFFUX, "wasm" MODULE_EXT},
{"Janet" CONFIG_SUFFUX, "janet" MODULE_EXT},
{"Python" CONFIG_SUFFUX, "python" MODULE_EXT},
};

FOR(const struct Module*, it, Modules)
{
loadModule(it->file, it->config);
}
}

#endif

tic_mem* tic_core_create(s32 samplerate, tic80_pixel_color_format format)
{
tic_core* core = (tic_core*)malloc(sizeof(tic_core));
Expand Down Expand Up @@ -763,6 +946,8 @@ tic_mem* tic_core_create(s32 samplerate, tic80_pixel_color_format format)
blip_set_rates(core->blip.left, CLOCKRATE, samplerate);
blip_set_rates(core->blip.right, CLOCKRATE, samplerate);

loadLangs();

{
#define API_FUNC_DEF(name, ...) core->api.name = tic_api_ ## name;
TIC_API_LIST(API_FUNC_DEF)
Expand Down

0 comments on commit 7c98dcb

Please sign in to comment.