Skip to content

Commit 42dd9e5

Browse files
feat: CModule support
1 parent 5322529 commit 42dd9e5

11 files changed

Lines changed: 806 additions & 58 deletions

File tree

deps/libffi.lua

Lines changed: 0 additions & 57 deletions
This file was deleted.

deps/libtcc.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package("libtcc")
2+
set_homepage("https://bellard.org/tcc/")
3+
set_description("Tiny C Compiler")
4+
5+
set_urls("https://github.com/std-microblock/tinycc.git")
6+
add_versions("2026.03.30+1", "38caec5487b44da82ed9b25699e13cdab8845b77")
7+
8+
add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
9+
10+
on_install(function(package)
11+
import("package.tools.xmake").install(package)
12+
end)

src/core/bindings/binding_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
#include "native_memory_access_monitor.h"
1616
#include "native_process.h"
1717
#include "script_lifecycle.h"
18+
#include "native_cmodule.h"

src/core/bindings/generated_bindings/binding_qjs.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,30 @@ template<> struct js_bind<chromatic::js::ScriptLifecycle> {
836836
}
837837
};
838838

839+
template <> struct qjs::js_traits<chromatic::js::NativeCModule> {
840+
static chromatic::js::NativeCModule unwrap(JSContext *ctx, JSValueConst v) {
841+
chromatic::js::NativeCModule obj;
842+
843+
return obj;
844+
}
845+
846+
static JSValue wrap(JSContext *ctx, const chromatic::js::NativeCModule &val) noexcept {
847+
JSValue obj = JS_NewObject(ctx);
848+
849+
return obj;
850+
}
851+
};
852+
template<> struct js_bind<chromatic::js::NativeCModule> {
853+
static void bind(qjs::Context::Module &mod) {
854+
mod.class_<chromatic::js::NativeCModule>("NativeCModule")
855+
.constructor<std::string, std::vector<std::string>, std::vector<uint64_t>>()
856+
.fun<&chromatic::js::NativeCModule::getSymbol>("getSymbol")
857+
.fun<&chromatic::js::NativeCModule::listSymbols>("listSymbols")
858+
.fun<&chromatic::js::NativeCModule::dispose>("dispose")
859+
;
860+
}
861+
};
862+
839863
inline void chromatic_bindAll(qjs::Context::Module &mod) {
840864

841865
js_bind<chromatic::js::NativePointer>::bind(mod);
@@ -882,4 +906,6 @@ inline void chromatic_bindAll(qjs::Context::Module &mod) {
882906

883907
js_bind<chromatic::js::ScriptLifecycle>::bind(mod);
884908

909+
js_bind<chromatic::js::NativeCModule>::bind(mod);
910+
885911
}

src/core/bindings/generated_bindings/binding_types.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,5 +930,26 @@ export class ScriptLifecycle {
930930
*/
931931
static _callDisposeCallbacks(): void
932932
}
933+
export class NativeCModule {
934+
constructor(code: string, symbolNames: Array<string>, symbolAddresses: Array<number>);
935+
/**
936+
* Get the address of a named exported symbol. Returns null pointer if not
937+
* found.
938+
* @param name: string
939+
* @returns NativePointer
940+
*/
941+
getSymbol(name: string): NativePointer
942+
/**
943+
* Get all exported global symbol names.
944+
@returns Array<string>
945+
*/
946+
listSymbols(): Array<string>
947+
/**
948+
* Eagerly unmap the module from memory.
949+
* Calls void finalize(void) if defined, then frees the TCC state.
950+
@returns void
951+
*/
952+
dispose(): void
953+
}
933954
}
934955

0 commit comments

Comments
 (0)