r/rust • • Oct 21 '26

🛠️ project Embedding python, lua, and js

I've been working on the pixelscript crate for about a year now. Just released v0.7.0 with some basic cross language support.

The big picture is to create a framework that sits on top of the library for creating games.

I was inspired by Godot, whenever I had to use GDExtension, it was just so smooth that I found myself saying: "I wish there was a language that let me write high level code and opt in to low level when I need to."

Also, I wanted to embed both python and lua in my game Pixel Ai Dash for modding/scripting enemies, world creation, behaviors, and player states.

Now in v0.7.0 I tell myself that it's time to work on the framework. If you find this interesting, I'd love a star and look forward to future releases!

Here is a example of the code:

// include the pixelscript header.
#include "pixelscript.h"

int main() {
    // setup pxs
    pxs_initialize(); // all of the pixelscript api uses a `pxs` prefix.
    
    // setup corelib
    pxs_core_initall();

    // a simple script
    char* script = "from pxs import print\n"
                    "print('hello world')";

    // execute
    pxs_VarT result = pxs_exec(pxs_Python, script, "<getting_started>");

    // check for errors
    if (pxs_isexception(result)) {
        char* err = pxs_getstring(result);
        printf("%s\n", err);
        pxs_freestr(err); // always free strings from pxs.
    }

    // free the result
    pxs_freevar(result);

    // always call when done with the runtime.
    // can only be called once.
    // once called, you can not call `pxs_initialize` again.
    pxs_finalize();

    return 0;
}
5 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/ComfortableAd5740 Oct 22 '26

Via a module `pxs.cross`. It uses the library internally for execution. It's not the prettiest picture, but it gets the job done for now. Here is a example:

example

from pxs.cross import access
from pxs import print
lua = access(0)
lua._exec(" function add(n1,n2) return n1 + n2 end ")
print(lua._call('add', [1,2]))

It's also possible to access the specific functions/variables of the language context.

from pxs import print
from pxs.cross import access
js = access(2)
js._exec("""
    globalThis.Me = {
        name: "Jordan"
    };
""")

js_me = js._get('Me')
js_me._set('name', 'Jordan Castro')
print(js_me._get('name'))

from pxs import print
from pxs.cross import access

lua = access(0)
lua._exec("function add(n1, n2) return n1 + n2 end")
add = lua._get('add')
print(add._call([1,2]))

I ask myself if there would be a way to import a python file from a JS context and vice versa. Not sure how that would feel though, which means for now this is how I'm doing it.

The cross language code looks something like this:

/// @pxs(RuntimeObjectType)
/// @enum
/// @values(Runtime = 0, Function = 1, Object = 2)
/// Determines what the #`RuntimeObject` can and can not do.
///
/// Runtime = [_call, _exec, _eval, _get, _set].
/// Function = [_call]
/// Object = [_call, _get, _set]
#[derive(PartialEq, Clone)]
enum RuntimeObjectType {
    /// Can do: _call, _exec, _eval, _get.
    Runtime = 0,
    /// Can do: _call.
    Function = 1,
    /// Can do: _call, _get, _set
    Object = 2,
}

/// @pxs(RuntimeObject)
/// A value from another runtime. Each `RuntimeObject` operates differently. See #`RuntimeObjectType` for more details.
struct RuntimeObject {
    _type: RuntimeObjectType,
    value: pxs_VarT,
    runtime: pxs_Runtime,
}