Coder Social home page Coder Social logo

bondage's Introduction

bondage Code Climate Build Status

bondage is a bindings generator. It uses clang and ruby to process C++ classes into runtime reflectable methods.

for example:

namespace Meat
{

/// \expose
class Pork
{
public:
  void saltiness();
};
}

Allows class reflection:

// Prints "Pork"
for(const bondage::WrappedClass *cls : bondage::ClassWalker(Meat::bindings()))
  {
  std::cout << cls->type().name();
  }

And function reflection:

// Prints "saltiness"
const bondage::WrappedClass *pork;
for (std::size_t i = 0; i < pork->functionCount(); ++i)
  {
  std::cout << stringClass->function(i).name();
  }

bondage uses Reflect to wrap functions, and requires some kind of wrapping backend (a script engine, for example) to provide function invocation.

Example

A library for bondage processing can defined with:

require "parser/Library.rb"

# Create a library called [Gen], in the relative folder [data/Gen]
lib = Library.new("Gen", "data/Gen")

# Include files for this library are located in the library folder [data/Gen], or [.]
lib.addIncludePath(".")

# The only file in the library is the [Generator.h] file.
lib.addFile("Generator.h")

# We could also add other exposed dependencies to the library here.

C++ bindings can then be generated using:

# Parse the library. This fills visitor with the data from our C++ files.
visitor = ParsedLibrary.parse(lib)

# Create an exposer, this visits all classes in the C++, checks 
# if they should be exposed (either implicitly, or if asked - using the /// \expose command)
exposer = ClassExposer.new(visitor)

# Now we have all the data to write the bindings
libGen = CPP::LibraryGenerator.new()
libGen.generate(lib, exposer)

# libGen.header and libGen.source can now be used to write to file.

bondage also has a generator for Lua, which will generate class bindings on the lua side, which interacts with the bindings above. Lua bindings are generated using a very similar method:

# first create a lua library generator
libGen = Lua::LibraryGenerator.new(
  Lua::DEFAULT_PLUGINS,     # plugins are used to generate extra class data - properties by default
  Lua::DEFAULT_CLASSIFIERS, # classifiers can process arguments passed to functions - converting indices
  "getFunction",            # a global lua function to get a native function
  TestPathResolver.new)     # a helper class to turn a class into a file path for requiring

# Generate the bindings
libGen.generate(lib, exposer)

# write the bindings (writes a file per class, and a library file)
libGen.write("c:\somedir\")

Written with StackEdit.

bondage's People

Contributors

0x55555555 avatar

Watchers

James Cloos avatar  avatar

Forkers

m0tive

bondage's Issues

IndexClassifier is called on all overloaded calls when only one contains an index

The class

/// \brief
/// \expose unmanaged
class MyClass
{
  public:

  /// \brief  Find the pork
  const std::string& findPork(const std::string& country, const std::string& town);

  /// \brief  Find the pork
  /// \param  porkUID [index] the unique identification number for the pork
  const std::string& findPork(int porkUID);
}

generates the following wrapper in lua:

local MyClass_findPork_wrapper_fwd = getNative("prk", "MyClass", "findPork")
local MyClass_findPork_wrapper = function(...)
  local argCount = select("#", ...)
  if 3 == argCount then
    return MyClass_findPork_wrapper_fwd(select(1, ...), to_native(select(2, ...)), select(3, ...))
  end
  if 2 == argCount then
    return MyClass_findPork_wrapper_fwd(select(1, ...), (select(2, ...)-1))
  end
end

The overload with two arguments (string, string) is having the first argument treated as a non-integer index, even though it isn't marked as such

"RuntimeError: non equal return types" when exposing overload that takes an index

When exposing

void remove(const std::string& name);
/// \param itemIndex [index]
void remove(int itemIndex);

I get an error in: https://github.com/jorj1988/bondage/blob/master/generators/Lua/Function/WrapperGenerator.rb#L130

RuntimeError: non equal return types
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:143:in `block (2 levels) in getCommonTypeArray'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:141:in `each_index'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:141:in `block in getCommonTypeArray'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:138:in `times'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:138:in `getCommonTypeArray'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:57:in `generateOverloadCall'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:40:in `block in generate'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:38:in `each'
D:/bondage/generators/Lua/Function/WrapperGenerator.rb:38:in `generate'
D:/bondage/generators/Lua/Function/Generator.rb:57:in `generate'
D:/bondage/generators/Lua/ClassGenerator.rb:112:in `generateFunction'
D:/bondage/generators/Lua/ClassGenerator.rb:154:in `blockin generateFunctions'
D:/bondage/generators/Lua/ClassGenerator.rb:153:in `each' 
D:/bondage/generators/Lua/ClassGenerator.rb:153:in `generateFunctions'
D:/bondage/generators/Lua/ClassGenerator.rb:38:in `generate'
D:/bondage/generators/Lua/LibraryGenerator.rb:33:in `block in generate'
D:/bondage/generators/Lua/LibraryGenerator.rb:31:in `each'
D:/bondage/generators/Lua/LibraryGenerator.rb:31:in `generate'

which I think is due to the wrapper generator not handling the overload with the same number of arguments (other cases with differing number of arguments work)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.