All Projects → ejulien → FABGen

ejulien / FABGen

Licence: GPL-3.0 License
C++ binding generator for CPython 3.x (x>=2), Lua 5.3 and Go

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to FABGen

idajava
Java integration for Hex-Rays IDA Pro
Stars: ✭ 25 (-3.85%)
Mutual labels:  swig
numpyeigen
Fast zero-overhead bindings between NumPy and Eigen
Stars: ✭ 75 (+188.46%)
Mutual labels:  binding
emscripten-sys
Emscripten API bindings for Rust
Stars: ✭ 18 (-30.77%)
Mutual labels:  binding
ruby-tree-sitter
Ruby bindings to tree-sitter
Stars: ✭ 48 (+84.62%)
Mutual labels:  binding
openHAB-Simatic
openHAB binding for Siemens Simatic S7 PLC
Stars: ✭ 15 (-42.31%)
Mutual labels:  binding
simplesquirrel
Yet another simple binding in C++11 for Squirrel scripting language
Stars: ✭ 37 (+42.31%)
Mutual labels:  binding
Binder
🦁"Hello World" <-> [🏷, 🏷, 🏷, 🏷]
Stars: ✭ 37 (+42.31%)
Mutual labels:  binding
croatoan
Common Lisp bindings for the ncurses terminal library.
Stars: ✭ 111 (+326.92%)
Mutual labels:  binding
UIControlBinding
一种半自动控件绑定方案
Stars: ✭ 110 (+323.08%)
Mutual labels:  binding
nimffmpeg
Nim FFMpeg binding
Stars: ✭ 29 (+11.54%)
Mutual labels:  binding
xsdata
Naive XML & JSON Bindings for python
Stars: ✭ 144 (+453.85%)
Mutual labels:  binding
hibpwned
Python API wrapper for haveibeenpwned.com (API v3)
Stars: ✭ 21 (-19.23%)
Mutual labels:  binding
UIFramework
VBM UI Framework For Unity3D
Stars: ✭ 25 (-3.85%)
Mutual labels:  binding
rust-tree-sitter
Rust bindings to Tree-sitter
Stars: ✭ 29 (+11.54%)
Mutual labels:  binding
raylib-nelua
Raylib wrapper to nelua language
Stars: ✭ 27 (+3.85%)
Mutual labels:  binding
node-libzim
Binding to libzim, read/write ZIM files in Javascript
Stars: ✭ 23 (-11.54%)
Mutual labels:  binding
observable ish
Observable state and events for browser and Flutter.
Stars: ✭ 26 (+0%)
Mutual labels:  binding
phper
A library that allows us to write PHP extensions using pure Rust and using safe Rust whenever possible.
Stars: ✭ 24 (-7.69%)
Mutual labels:  binding
PyMFEM
Python wrapper for MFEM
Stars: ✭ 91 (+250%)
Mutual labels:  swig
webview
Nim bindings for https://github.com/zserge/webview
Stars: ✭ 91 (+250%)
Mutual labels:  binding

Fabgen - The fabulous generator

Build Status Coverage Status

Fabgen is a set of Python scripts to generate C++ binding code to different languages.
It was written as a SWIG replacement for the Harfang Multimedia Framework (http://www.harfang3d.com).

Authors

Fabgen is written and maintained by Emmanuel Julien for the Harfang Multimedia Framework (http://www.harfang3d.com).
Fabgen GO is written and maintained by Thomas Simonnet for the Harfang Multimedia Framework (http://www.harfang3d.com).

License

Fabgen is licensed under the GPLv3.

Fabgen output does not fall under the GPLv3, you are free to license it as you please.

Goals

  1. Support multiple target language.
  2. Bidirectional binding. Bind C++ functions to target language and target language functions to C++.
  3. Provide an API for embedding (runtime C type name to target name query, human-readable type conversion functions, etc...).
  4. Full feature support for all target language unless technicaly impossible in which case a sensible fallback should be provided.
  5. Provide fast binding

Philosophy

  1. Keep as much code as possible on the generic part of the generator (gen.py).
  2. Any feature that can be done using what's available should be culled (aka. no feature creep).
  3. Output library must feel as native as possible to the target language.

Features

  • Generated code has no dependencies and is human readable.
  • Generator input is a Python script.
  • Customizable type conversion from C++ and back.
  • Can bind many C++ construct (function/data members, static function/data members, exceptions, etc...).
  • User specifiable bound name.
  • Types can be hidden from the generated binding interface.
  • Feature mechanism to extend types and prototypes such as:
    • arg_out, arg_in_out to support output arguments.
    • route to route methods to a customizable expression.
    • proxy to support wrapper types such as std::shared_ptr.
  • Extern type support to "link" C++ types shared by different bindings.
  • Simple and hopefully easy to dive into codebase.

Supported target language

  • CPython 3.2+ using the CPython limited API (Py_Limited_API) (generated modules can be used on all CPython version >=3.2)
  • Lua 5.3+
  • Go 1.11+ (use of go module)

Contributions

Contributions are welcome. Please submit merge requests with working unit test to the project Github directly.

Basic usage

Generate binding for CPython 3

bind.py api_binding_script.py --cpython --out d:\

Generate binding for Lua 5.3

bind.py api_binding_script.py --lua --out d:\

Generate binding for GO
Installation:

bind.py api_binding_script.py --go --out d:\

Refer to the provided example and the tests for how to write your own API binding script.

Extending through feature

Type converters and function prototypes all accept a list or dictionary of features when declared.

Extending type converter with feature

  • 'proxy': class ProxyFeature

Types using this feature are declared as wrapper for another underlying type.
This feature is used to unwrap arguments to a call and wrap its return value.

Note: A type converter proxy feature is consumed by prototypes proxy feature, see the prototype feature documentation.

Support for std::shared_ptr is done with the proxy feature.

  • 'rval_transform': def transform(gen, conv, expr, var_out, ownership):

Allow type conversion when returned from a function.

class StdSharedPtrProxyFeature:
    def __init__(self, wrapped_conv):
        self.wrapped_conv = wrapped_conv

    def init_type_converter(self, gen, conv):
        # declare shared_ptr<T> to T cast support
        gen.add_cast(conv, self.wrapped_conv, lambda in_var, out_var: '%s = ((%s *)%s)->get();\n' % (out_var, conv.ctype, in_var))

    def unwrap(self, in_var, out_var):
        return '%s = %s->get();\n' % (out_var, in_var)

    def wrap(self, in_var, out_var):
        return '%s = new std::shared_ptr<%s>(%s);\n' % (out_var, self.wrapped_conv.ctype, in_var)
  • 'sequence': Class

Provide native access for the target language to values stored in a container type.

  • 'repr': def get_repr(var_self, var_repr)

Provide a custom representation for a type.

def get_repr(var_self, var_repr):
    return '%s = "custom_repr";\n' % var_repr  # C++ code to evaluate at runtime and assign to the repr string

Extending function prototype with feature

  • 'route': def router(var, args)

This feature redirects an object method (static or not) to a function with a compatible signature. It can be used to extend classes with additional methods while not modifying the existing C++ class.

  • 'arg_out': []

Dictionary of argument names to consider as output to the bound function. Output arguments do not take part in overload resolution and are not provided by the function caller.

Output arguments are appended to the function return value.

  • 'arg_in_out': []

Dictionary of argument names to consider as input-output to the bound function. Input-output arguments take part in overload resolution and are provided by the function caller.

Input-output arguments are appended to the function return value.

  • 'proxy': None

Route a method call to the type wrapped by the 'proxy' feature of the converter used of the declaration. This feature does not provide any value. This feature can be used to wrap constructors.

# With shared_ptr_to_obj_conv being a TypeConverter with a proxy feature,
# call the PrintState method through the shared pointer.
gen.bind_method(shared_ptr_to_obj_conv, 'PrintState', 'void', [], features=['proxy'])
  • 'new_obj': None

By default, values returned by reference or pointer are assumed C++ ownership. This feature transfers the returned object ownership to the target language.

  • 'copy_obj': None

By default, values returned by reference or pointer are assumed C++ ownership. This feature makes a copy of the returned object, the target language then has full ownership.

  • 'check_rval': def check(rvals):

Insert return value checking code right after the native call.

  • 'exception': str

Wrap the native function call with a try{} block and raises an exception in the target language with the specified string as the exception message.

Performance notes

Beware of silent conversions when providing advanced interoperability with a target language

Consider the following example scenario when binding std::vector<int> to CPython:

  1. The bound type will implement the Sequence protocol (if you use the standard StdVector type converter which implements this feature).
  2. We define a function with two equivalent prototypes taking one argument. One accepts std::vector<int>, the other one a PySequence of int as a convenience to the user.
  3. If the PySequence prototype is listed first CPython will accept std::vector<int> as PySequence during dynamic dispatch since it implements the Sequence protocol.

This can have serious performance implications as the PySequence always need to be extracted to a temporary std::vector<int> before the native call is made.
A wrapped std::vector<int> object does not undergo any transformation and is passed right away to the native layer.

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].