Links

Categories

Tags


« | Main | »

Built-in binding code generator

By Jewe | January 31, 2010

I added an integrated C++ binding code generator to JewelScript.

Why I did it

The redesign of the reference handling in JewelScript is almost done, apart from finding and fixing the bugs that this change undoubtedly has introduced into the compiler. So I thought why not test-compile one of my applications that makes use of the library. While trying to do that, I once again realized one of the big weaknesses of the library: I needed to bind a new native class to that application, however, without a binding code generator, this can be quite an effort.

Well, I had always planned to integrate a binding code generator into the library at some point in the future, I even reserved some keywords for it in the language, but I never really got to it. So while I was sitting there, disappointed that I couldn’t test my application and pondering whether or not I would once again write the binding code by hand, I decided to finally start the integrated binding code generator.

Funny thing is, now that I’m at it, I wonder what took me so long to decide to do it, it’s kind of fun generating C++ code instead of virtual machine code for a change.

A brief glance at the new feature

So the good news is that JewelScript v1.0 will have an integrated C++ binding code generator. The current version I have now is by far not finished, but it can already handle most of the ordinary stuff, so I’d guess that you could use it for 75% of the native code you’d probably want to bind to the script language. I expect that by the time 1.0 gets released, it should enable developers to also do the more advanced stuff.

Since the binding code generator has direct access to the object model of the script program from the compiler, no extra parser or INI file has to be written, so using it is freakingly easy.

Working principle

The basic idea is that you either already have a C++ class that you want to bind to JewelScript, or want to start one. Here’s a mocked up CColor class as an example: CColor.cpp.

To create a binding file for this class, you write a JewelScript code file and declare the class using the native keyword to signal to the compiler, that you don’t want to implement this class in script, but want to have C++ binding code generated for it. Here’s the example JewelScript class: Color.jc.

Next, you just compile the JewelScript file, using your own application or the JILRunOnly command-line tool, and you’ll receive a C++ source file that you can add to your application. Here’s the example: bind_Color.cpp.

Of course this is a very simple example, in it’s current state, the code generator can already handle much more complex classes, but I wanted to keep it simple for this demonstration.

Tomorrow I’ll put it to good use and have it create the missing bindings for me, so I can test-compile that application I was talking about earlier.

Topics: docs, news | Comments Off on Built-in binding code generator

Comments are closed.