Tiny C Compiler plugin

I’ve run across a number of open source libraries in C that I thought looked interesting, but would often get deterred by the hassle of setting up the projects and such. I was aware of tcc and some of the Lua efforts using it, but that also fell victim to the same hesitation.

The idea has germinated for a while, though, and since some stuff has landed in 3713 (and gotten some minor C fixes in 3719), it seemed like it would be extra-handy, given that some of the useful bits weren’t a good fit for Lua.


I took a small existing plugin and added Solar support around it. So far these are the Mac bits, but @Kan98 has also got lots of stuff working on Windows. (Both are still WIP, though.)

That’s using a build of the mob branch of TinyCC, via the libtcc.a and the zip, in the mac folder, although I’d like to try using the ONE_SOURCE approach to (I hope) avoid the extra build step.

This is currently only useful on desktop, e.g. for rapid testing of libraries or plugin development. (It is actually quite common as the “debug” / development compiler in a lot of projects.) I’m hoping some practical workflows will emerge over time.


In addition to using C strings or code, existing dynamic libraries seem to work well, so it can also be used as an FFI.


(Also, I would like to test something if somebody on Mac doesn’t have Xcode or Developer Tools installed.)


Here’s my current copy of the tests (Kan might have some stuff to add):

solar2c_tests.zip (7.8 MB)

Assuming I uploaded the plugin properly, you can try them by going to main.lua and setting Choice to the desired example. (Yes, these are tests. :grinning_face_with_smiling_eyes:)


The prints, color_mask, stuff_3, and stuff_3D are the graphics examples from the above “experimental” link, ported to C. (There’s also a quick-and-dirty z-order test in the last folder.)

bitmap_test was, as the name sort of suggests, used to test the screwy packing behavior that got fixed in 3721. (It should look odd or crash pre-3721, assuming you don’t have a rather forgiving driver.)


The other stuff (a lot of it still in idea form, or needing some porting, etc.) come from random interesting C libraries. (The licenses are all pretty lenient and probably in most of the source files, but I expect I’ll make a proper list before adding the examples to the repository itself.)


The js_and_wasm still have problems. (I might have flushed out an actual code generation bug, although it’s odd all the other stuff seems to be fine. Haven’t reached out about this yet.) Anyhow, those use a Javascript and WASM library (i.e. they run such code, not that they’re written in them) to use some music libraries. These are ports of some code I wrote a couple years back (which does work), investigating how to bring in non-Lua libraries. (Related to mod support in SoLoud.)


The flecs folder has a couple tests from the MANY in the ECS library of the same name. I’m curious about some of its search features.


microui is a port of the demo for the immediate mode GUI library of the same name. Kan added input to it, though we haven’t synced it here.


jar_libs and TinySoundFont are some little tests for mod, xm, and soundfont audio. These don’t yet have Windows support, since we haven’t figured out some file IO stuff there. They do have support for loading from memory, so might “work”, but of course these are tests and meant to bring in all features possible. :slightly_smiling_face:


Anything else is, as mentioned above, not yet ready.

Plenty of other ideas floating around. Random shortlist in some notes:

tbox
chips
tg (and other tidwall libraries)
More of flecs
blis, blosc2, flite
textedit, tileeditor (from stb's nothings)
Connected components, iji2grid
stencil-based z-order
stencil-based curve?
scalable-font2, wamain (from bzt on Gitlab)
RISC-V?
arc-length parametrization? (maybe not much use in C)
texture stuff
memory stuff
image processing stuff? (content-aware fill; wfc or graphcut)
other plugins… (3D: shape generator, e.g. prideout's stuff)
hash, RNG, compression tests
LuaAutoC
Nelua; vlang; Nim?

The audio examples use a little bit of raw OpenAL to do the audio, since Solar doesn’t yet have any audio integration support. (I have played with some things, but don’t have any final ideas in mind.)

Also, none of the tests really do much shutdown, so could possibly get work if your relaunch a lot. :smiley:

7 Likes

Nice work!

1 Like

Heyyooo!

This is a very interesting and highly useful plugin, we can call C from Lua via Lua C API, there have been many discussions about the performance of Lua 5.1, I think that we can call C without building a plugin will also be very useful in this matter.

@StarCrunch has presented this plugin very clearly, I will give some examples so that everyone can touch it more easily.


With the above example that StarCrunch has provided you can create a new folder in examples and create a file start.lua and change the choice value to your folder name in main.lua, here my folder is test

Then paste the code below.

This is a small example of calling C from Lua.

-- examples/test/start.lua
local setup = require("setup")
local context = setup(...)

context:compile([[
   #include <CoronaLua.h>
    int sum(lua_State * L)
    {
        if (lua_gettop(L) != 2) {
            lua_pushstring(L, "Exactly two arguments required");
            lua_error(L);
        }

        if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
            lua_pushstring(L, "Both arguments must be numbers");
            lua_error(L);
        }

        int a = lua_tointeger(L, 1);
        int b = lua_tointeger(L, 2);
        lua_pushinteger(L, a + b);
        return 1;
    }
]])

context:relocate()

local sumFunc = context:get_symbol("sum")

local result = sumFunc(5, 10)

print("Result of sum:", result)

Moreover, you can implement most C libraries directly.

In the test project we have an example called microui, this example allows us to build a GUI quickly suitable for developing debugging/testing tools.

You can add the following code to make it work (maybe you need change orientation to landscape).

local r = display.newImage(bitmap.filename, bitmap.baseDir)

r.x, r.y = display.contentCenterX, display.contentCenterY

r.fill.effect = "filter.custom.microui"

local flag = 0
Runtime:addEventListener("mouse", function(event)
	r.fill.effect.mouseMove = { event.x, event.y }

	if event.isPrimaryButtonDown then
		flag = 1
	elseif event.isMiddleButtonDown then
		flag = 2
	elseif event.isSecondaryButtonDown then
		flag = 3
	end

	if event.type == "down" then
		r.fill.effect.mouseDown = { event.x, event.y, flag }
	elseif event.type == "up" then
		r.fill.effect.mouseUp = { event.x, event.y, flag }
		flag = 0
	elseif event.type == "scroll" then
		r.fill.effect.scroll = { event.scrollX, event.scrollY }
	end
end)

Runtime:addEventListener("key", function(event)
	if event.phase == "down" then
		r.fill.effect.text = event.keyName
	end
	return false
end)


Hope this is impressive for you guys, it’s still in development, need everyone’s testing.

9 Likes

Congrats to both of you for the hard work.

I see that’s pretty new and still a WiP but would you like me to include it in awesome-solar2d?

Sounds good. :+1:

1 Like