All Projects → haxiomic → haxe-c-bridge

haxiomic / haxe-c-bridge

Licence: MIT license
Easily interact with haxe classes from C with an automatically generated C header

Programming Languages

haxe
709 projects
C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to haxe-c-bridge

Vime
Customizable, extensible, accessible and framework agnostic media player. Modern alternative to Video.js and Plyr. Supports HTML5, HLS, Dash, YouTube, Vimeo, Dailymotion...
Stars: ✭ 1,928 (+4091.3%)
Mutual labels:  embed
Gatsby Remark Embedder
Gatsby Remark plugin to embed well known services by their URL.
Stars: ✭ 245 (+432.61%)
Mutual labels:  embed
DotNET.jl
Julia ❤️ .NET
Stars: ✭ 75 (+63.04%)
Mutual labels:  interop
Media Embed
A PHP library to deal with all those media services around, parsing their URLs and displaying their audios/videos.
Stars: ✭ 143 (+210.87%)
Mutual labels:  embed
Unfurl
Scraper for oEmbed, Twitter Cards and Open Graph metadata - fast and Promise-based ⚡️
Stars: ✭ 193 (+319.57%)
Mutual labels:  embed
speckle-blender
speckle.systems/tag/blender/
Stars: ✭ 38 (-17.39%)
Mutual labels:  interop
Embed
Embed player for D.Tube
Stars: ✭ 65 (+41.3%)
Mutual labels:  embed
speckle-sharp
.NET SDK, Schema and Connectors: Revit, Rhino, Grasshopper, Dynamo, ETABS, AutoCAD, Civil3D & more.
Stars: ✭ 214 (+365.22%)
Mutual labels:  interop
Titan
Create Discord server widgets for websites of all sizes! A simple to setup process for end-users. Server members may view or send messages into an embedded Discord channel.
Stars: ✭ 221 (+380.43%)
Mutual labels:  embed
DotNetJS
Consume C# in JavaScript with comfort: single-file UMD library, auto-generated 2-way bindings and type definitions
Stars: ✭ 551 (+1097.83%)
Mutual labels:  interop
Skip Gram Pytorch
A complete pytorch implementation of skip-gram
Stars: ✭ 153 (+232.61%)
Mutual labels:  embed
Video Downloader Deploy
Video Downloaders (you-get, youtube-dl, annie) One-Click Deployment Batch. || 视频下载器 (you-get, youtube-dl, annie) 一键配置脚本。
Stars: ✭ 178 (+286.96%)
Mutual labels:  embed
MementoEmbed
A service that provides archive-aware oEmbed-compatible embeddable surrogates (social cards, thumbnails, etc.) for archived web pages (mementos).
Stars: ✭ 13 (-71.74%)
Mutual labels:  embed
Dhooks
A simple python Discord webhook API wrapper
Stars: ✭ 136 (+195.65%)
Mutual labels:  embed
embd-go
embd-go is an embeddable command-line tool for embedding data files in Go source code, specially crafted for easy use with `go generate`.
Stars: ✭ 24 (-47.83%)
Mutual labels:  embed
Gh Card
GitHub Repository Card for Any Web Site
Stars: ✭ 1,154 (+2408.7%)
Mutual labels:  embed
terrafx.interop.windows
Interop bindings for Windows.
Stars: ✭ 183 (+297.83%)
Mutual labels:  interop
ginadmin
基于Gin开发的后台管理系统,集成了、数据库操作、日志管理、权限分配管理、多模板页面、自动分页器、数据库迁移和填充、Docker集成部署等功能、静态资源打包
Stars: ✭ 149 (+223.91%)
Mutual labels:  embed
UnityNativeTool
Allows to unload native plugins in Unity3d editor
Stars: ✭ 147 (+219.57%)
Mutual labels:  interop
vue-rss-feed
Embed RSS Feeds in your Vue web app
Stars: ✭ 37 (-19.57%)
Mutual labels:  embed

Haxe C Bridge

HaxeCBridge is a @:build macro that enables calling haxe code from C by exposing classes via an automatically generated C header. This makes it possible to build your user interfaces using native platform tools (like Swift and Java) and call into haxe code for the main app work like rendering

Requires haxe 4.0 or newer and hxcpp

Quick Start

Install with haxelib install haxe-c-bridge (or simply copy the HaxeCBridge.hx file into your root class-path)

Haxe-side:

  • Add --library haxe-c-bridge to your hxml
  • Add -D dll_link or -D static_link to your hxml to compile your haxe program into a native library binary
  • Add @:build(HaxeCBridge.expose()) to classes that you want to expose to C (you can add this to as many classes as you like – all functions are combined into a single header file)
  • HaxeCBridge will then generate a header file in your build output directory named after your --main class (however a --main class is not required to use HaxeCBridge)

C-side:

  • #include the generated header and link with the hxcpp generated library binary
  • Before calling any haxe functions you must start the haxe thread: call YourLibName_initializeHaxeThread(onHaxeException)
  • Now interact with your haxe library thread by calling the exposed functions
  • When your program exits call YourLibName_stopHaxeThread(true)

See test/unit for a complete example

Minimal Example

Main.hx

class Main {
	static function main() {
		trace("haxe thread started!");
	}
}

@:build(HaxeCBridge.expose())
class UseMeFromC {

	final callback: cpp.Callable<(num: Int) -> Void>;

	public function new(nativeCallback: cpp.Callable<(num: Int) -> Void>) {
		this.callback = nativeCallback;
	}

	public function add(a: Int, b: Int) {
		var result = a + b;
		callback(result);
		return result;
	}

	static public function exampleStaticFunction() {
		return "here's a string from haxe! In C this will be represented as a const char*. When passing haxe object to C, the object will be retained so it's not garbage collected while it's being used in C. When finished with haxe objects, you can call releaseHaxeString() or releaseHaxeObject()";
	}

}

build.hxml

--main Main
--cpp bin
--dce full
-D dll_link

Then run haxe build.hxml to compile the haxe code into a native library binary

This will generate a header file: bin/Main.h with our haxe function exposed as:

HaxeObject Main_UseMeFromC_new(function_Int_Void exampleCallback);
int Main_UseMeFromC_add(HaxeObject instance, int a, int b);
HaxeString Main_UseMeFromC_exampleStaticFunction();

We can use our class from C like so:

void onHaxeException(const char* info) {
	printf("Haxe exception: \"%s\"\n", info);
	// stop the haxe thread immediately
	Main_stopHaxeThreadIfRunning(false);
}

void exampleCallback(int num) {
	printf("exampleCallback(%d)\n", num);
}

int main(void) {
	// start the haxe thread
	Main_initializeHaxeThread(onHaxeException);

	// create an instance of our haxe class
	HaxeObject instance = Main_UseMeFromC_new(exampleCallback);
	// to call members of instance, we pass the instance in as the first argument
	int result = Main_UseMeFromC_add(instance, 1, 2);
	// when we're done with our object we can tell the haxe-gc we're finished
	Main_releaseHaxeObject(instance);

	// call a static function
	HaxeString cStr = Main_UseMeFromC_exampleStaticFunction();
	printf("%s\n", cStr);
	Main_releaseHaxeString(cStr);

	// stop the haxe thread but wait for any scheduled events to complete
	Main_stopHaxeThreadIfRunning(true);

	return 0;
}

Background

C is a common language many platforms use to glue to one another. It's always been relatively easy to call C code from haxe using haxe C++ externs (or simply untyped __cpp__('c-code')) but it's much harder to call haxe code from C: while hxcpp can generate C++ declarations with @:nativeGen, you need to manually create adaptors for these to use with C. Additionally you have to take care to manage the haxe event loop and interaction with the haxe garbage collector.

This library plugs that gap by automatically generating safe function bindings, managing the event loop and taking care of converting exposed types to be C compatible and GC-safe.

A separate thread is used to host the haxe execution and the haxe event loop so events scheduled in haxe will continue running in parallel to the rest of your native app. When calling haxe functions from C, the haxe code will be executed synchronously on this haxe thread so it's safe for functions exposed to C to interact with the rest of your haxe code. You can disable haxe thread synchronization by adding @externalThread however this is less safe and you must then perform main thread synchronization yourself.

Meta

  • @HaxeCBridge.name – Can be used on functions and classes. On classes it sets the class prefix for each generated function and on functions it sets the complete function name (overriding prefixes)
  • @externalThread – Can be used on functions. When calling a haxe function with this metadata from C that function will be executed in the haxe calling thread, rather than the haxe main thread. This is faster but less safe – you cannot interact with any other haxe code without first synchronizing with the haxe main thread (or your app is likely to crash)

Compiler Defines

  • -D HaxeCBridge.name=YourLibName – Set the name of the generated header file as well as the prefix to all generated C types and functions
  • -D dll_link – A hxcpp define to compile your haxe code into a dynamic library (.dll, .dylib or .so on windows, mac and linux)
  • -D static_link – A hxcpp define to compile your haxe code into a static library (.lib on windows or .a on mac and linux)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].