Can I script a C++ application with Ruby like with Lua? - c++

I've just started to read about Ruby, and I was wondering if it can be embedded in a C++ application like Lua which provides a library to parse a given script file: https://stackoverflow.com/a/5127294/399107

Yes, you can. You just need to embed a Ruby engine in your application.
Note that, unlike the main Lua engine, some Ruby engines aren't really that well suited to being embedded into other programs. But, for example, Rubinius, IronRuby and JRuby have been specifically designed with embedding in mind, and even though it isn't pretty, you can embed YARV or MRI as well, even though they are not designed for it.
There's also MRuby, but unlike the others, it doesn't implement the full Ruby Language Specification, it only implements a subset of the ISO Ruby Specification which itself is only a small subset of the intersection of Ruby 1.8 and Ruby 1.9. Plus, it hasn't been released yet, as is evidenced by the fact that not even its homepage exists yet. It is, however, specifically designed for embedding, in both senses of the word: being embedded into other programs, and being useful on an embedded device with very little RAM.
As you may have noticed, it is much easier to embed Ruby into your app if the app is running on the Java platform or the CLI. There are C++ compilers for both the Java platform and the CLI, so that option is not entirely out of the question. Otherwise, I'd say that Rubinius is easier to embed, but more people have tried embedding YARV, so there are more blog posts about how to do that. (Or maybe, embedding Rubinius is so trivial nobody needs to write blog posts about it.)
A long time ago, someone was working on an implementation of Ruby for the Lua VM, but that implementation never went anywhere. Would solve all your problems, though :-)

Sure you can. It's possible with with SWIG, or you can make your own bindings for it (or google to see if someone has already done the work). The big question is do you really want to? The ruby interpreter is pretty heavy, and the interface isn't very nice.
Matz is working on an embeddable version of Ruby called mruby, which strives to be as easy to embed and as light as Lua. But its still alpha quality.

Yes, it's possible. Most of the standard libraries types are written in C. And when you can use C, you can use C++ too. Use extern "C" declared functions to get the right binding. I had a lot of trouble, when using a C++ compiler that was different (different version) from the compiler that was used to compile the ruby interpreter.
Here is the part of the pick axt book, that covers the ruby extension library: http://media.pragprog.com/titles/ruby3/ext_ruby.pdf
In an open source C++ web server project, I wrote a ruby / rack adapter, to use the server with rails: https://github.com/TorstenRobitzki/Sioux/tree/master/source/rack

Related

Using google library in coldfusion without using Java library

Referring to the following library and my previous thread, I have two questions:
Question #1: I have decided not to mess up with Java Libraries and hence could anyone tell me if there is another way to figure out how to use the libphonenumber library in coldfusion?
Question #2: As discussed in my previous thread, that many people are porting it to different programming languages like JavaScript, Ruby, PHP as they are not written in Java. The google library libphonenumber is written in PHP and I am wondering why someone would port it to PHP language.
(This is more of a comment, but is a little too long)
Seems like this was already answered in the comments of your other thread, but to reiterate:
Is there another way to use a java library from CF?
No. There is basically only one way to use java libraries from CF. Add the jar(s) to the class path and use createObject. You could also use a dynamic class loader like Mark Mandel's JavaLoader.cfc (or the rip of that project baked into CF10+). However, ultimately they all do the same thing.
Since using java libraries is pretty simple in CF, I am curious why you do not want to use it. While I suppose you could rewrite it in pure CFML, I would ask why? The whole point of libraries is reuse, which saves development time. Since there already is a compatible library available to you, there is not much point in rewriting it. Not unless you are doing it as a learning exercise.
The google library libphonenumber is written in PHP
No. There is port that is written in PHP. The "official" project is "a Java, C++ and Javascript library.". So it sounds like your options are the java version (server side) or javascript version (client side use). That is it.
I am wondering why someone would port it to PHP language.
Because the java library is not compatible with every platform out there, PHP being one of them. If a developer on an unsupported platform wanted to use it, they have two choices: port it or write their own from scratch. Since the google project already did most of the heavy lifting, porting is simpler.

Wanted: Compiler Tool for Users of Software System

I am not sure if the title of this question gets to the point. I have written a large software system in C C++ for Windows, and want to give the users of this system the option to add compiled code to it. The user should be able to do basic stuff, and exchange data with my program.
Currently the implemented way is via DLLs. But for this, a grown up compiler is needed, and it is not as easy as I wished. Is there a tiny C compiler that can create Windows DLLs?
Another idea is the Java native interface. But this requires a complete Java system to run in the background, and it is not easy to run code in it.
Do you have any other ideas?
Any interpreted language? (TCL and Lua were designed as extension languages, but you can nearly as easily interface with any other).
How about python integration?
You could create an python interface that interfaces with your application. Python is rather easy to learn and should integrate easily with c/c++. The python documentation has an own chapter on that.
You could also use a tool like swig to generate the interface.
The advantage of this is that they wouldn't have to compile anything. They could just supply python files that could be loaded into your application and run within the software. This is a well known use for python, and something its simple syntax promotes.
As the other says you will be best of by providing an embedded language.
I would chip in for javascript and use the google v8 engine
By using javascript you get a language nearly everbody can use and program in.
There is other javascript engines you can embed like SpiderMonkey.
See this answer for what to choose.
An interpreted language is not good enough. I need speed. The software itself is an interpreted language. So I added support for the tiny C compiler. It is only C, and I do check mingw, which probably would not be as tiny as this. Thanks for all your hints.
Added after several months:
I have now two tools, actually: TinyC and Python. The speed difference between those is noticable (factor 5-10), but that usually does not matter too much. Python is much easier for the user, though I managed to integrate both into the Euler GUI quite nicely.
One of the ways is to add scripting. You application can host scripting environment and expose its services there. Users would be able to execute JScript/VBScript scripts and interact with your application. The advantage is that with reasonable effort you can get the power of well known and well documented scripting languages into your application (I suppose there is even debugger for scripting there). You will be required to shape your app services as COM interfaces and scripts will be able to access them automatically using method names you assigned on C++ side.
C++, Win32 and Scripting: Quick way to add Scripting support to your applications
MSDN Entry Point - IActiveScript interface

Something like unity but for C++ programmers

is there anything similar to unity but with the difference that instead of javascript I can use C++?
It is funny, I was looking to learn a little bit of unity as I seen it as requirement for some postings... and I found this question.
I think Ogre3D is great... it may be what you are looking for.
here is the link ogre3d.org
Ogre3D is what I used. http://www.ogre3d.org/
It is somewhat of a misconception that Unity uses Javascript. The so-called Javascript used in Unity is actually a dialect of Javascript that is CLI-compatible and is not exactly the same as the official Javascript. Also, you are not limited to using Javascript in Unity. You can also use C#, which is similar to C++ and Java in some ways or the less commonly used Boo script.
Personally, I would recommend using C# in Unity because it is very powerful and allows calling of native C/C++ functions via P/Invoke. Here is a link to the use of plugins:
http://unity3d.com/support/documentation/Manual/Plugins.html
Link to list of Game Engines. Please take your time to read and consider them:
http://en.wikipedia.org/wiki/List_of_game_engines
If you want accessibility offered by Unity, C++ is certainly wrong choice. Much better would be interpreted/script language, such as Python. They are more flexible in terms of working in web browser/many os-es.
If you're interested in targetting mobile platforms then Marmalade might be a good fit.
Panda3D It's not a think compared to Unity, but it can work as a web plugin, cross-platform. It has a lack of IDE, but it is opensource.
As far as I know, there is no need to use JavaScript or Boo with Unity, and there is nothing wrong with C# acting like script language ( which is really hard with C++ ).
as a unity developer, I liked to add c++ to by knowledge base as all AAA games are made by C++ and I was looking for something like unity but for CPP. the only good cpp engine is unreal that doesn't really support c++. the language is a dialect of c++ and there are big differences. like very
rare usages of pointer and references
more syntaxes like c# attributes(cpp has not attribute)
...
so you don't learn memory management. the nearest thing that I could find was COCOS2dx as the whole framework is written in pure standard c++ and many libraries like UI and Physics are already added. you can write your own engine and editor with it.
Unreal Engine uses C++
default standard is C++14
https://www.unrealengine.com

Is it possible to run C code directly in the browser?

Performance considerations aside, is there any known way to take existing C, C++, or Objective C code and run it directly in the browser? For example, a compiler that converts all the code into some interpreted language that can be run in the browser. Like Javascript, or Actionscript and the Flash player, or I suppose Java and the JVM.
I recognize there are higher level languages like Haxe that can be compiled to different targets. And on the other side there are projects like Cappuccino and GWT that attempt to make Javascript development more like traditional desktop development.
But I was wondering if you had an application that worked on a desktop or an existing code base done in C, C++, or Objective C could it easily be converted to a web based application?
Is there work being done on this front? Is there any practical reason to do this? Basically turn the browser into the OS?
Beside the performance issues, and the entrenchment of OS vendors, are there any technical reasons this couldn't be accomplished? Could this kind of C like code be shoehorned into a virtual machine hosted in a browser?
Google's Native Client (NaCl) uses a tweaked compiler to create x86 object code that can be verified by the browser and run in a sandbox, without a major performance hit - pretty cool stuff. They've compiled Quake under it.
This Matasano article has a good run-down on how it works.
Here is a C compiler which targets a number of other languages, including Javascript:
http://cowlark.com/clue/
Not sure what state it's in - last I spoke to the author, it handled pure C89 (subject to the limitations of the compiler frontend). AFAIK there are no plans for it ever to support a GUI.
I was wondering if you had an application that worked on a desktop or an existing code base done in C, C++, or Objective C could it easily be converted to a web based application?
That's sort of what Silverlight is for (C# rather than Objective-C, of course), since it makes the .NET runtime available. Porting a desktop app is usually as much about the GUI as it is about the language - if you have a Cocoa app and you want to port it to another environment (whether that's a browser or Windows), then you'd need more than just an Objective-C cross-compiler, you need the Application Kit and so on. WINE being a notable counter-example, it's pretty rare for these OS-specific libraries to be available at all on other platforms, let alone efficiently. And even where they are available, there are look-and-feel and usability problems when the conventions of one UI are bolted on top of another. So people tend to either use portable frameworks to start with, or else completely rewrite the presentation layer of the app.
Basically turn the browser into the OS?
There are several projects underway to turn the browser into a fully-featured environment for applications (not sure whether or not this is what you mean by "OS"). Flash and AIR, Silverlight, HTML 5. None of them plan to provide C as a programming language, as far as I'm aware.
Emscripten allows you to compile your code into javascript, which is then platform and browser independent.
I think the closest thing you are looking for is Google Native Client. It is still in early development stages though.
You may be interested in LLVM, the Low Level Virtual Machine. It would be possible to implement an LLVM inside a Java applet, Flash applet, or even in Javascript (I wouldn't be surprised if somebody hasn't already done some or all of the above).
Converting an existing application is a completely different kettle of fish, however. The paradigms of user interaction are so completely different between a "desktop" app and a "browser" app that a lot of it will have to be redesigned before a port is reasonable.
Check out Adobe Alchemy (formerly known as FlaCC), which uses LLVM to compile C/C++ to Flash.
This is possible with an ActiveX control, but this works only in microsoft internet explorer.
http://code.google.com/p/cibyl/wiki/Cibyl can make Java sources, so you could compile that for the Java plugin in a browser. Given that Java plugins are much less common nowadays though, you may be better off with a solution that compiles to Javascript.
It seems to me that the major challenges are not related to the language being used. I suspect C would be a very difficult language to implement in JavaScript, but it is possible.
It just seems like a bad idea to me.
First off, I would not write a desktop application in C, much less a web application. Second, web applications require a completely different architecture than desktop applications. Simply cross compiling a desktop application will not make it a web application. If it is portability you are looking for, I suggest using a high-level language targeting the JVM.
Maybe you should consider http://ideone.com for compiling c++ in the browser? You can also compile or interpret other languages, I personally use it rather for more exotic languages - I have c and c++ on my PC :)

How to embed Ruby in C++?

What's the best way to embed Ruby as a scripting language in C++? Using ruby.h? SWIG? Something else? What I need is to expose some C++ objects to Ruby and have the Ruby interpreter evaluate scripts that access these objects. I don't care about extending Ruby or accessing it in C++.
I've found this article on embedding Ruby in C++, and while it's very useful, it's kinda old and I was wondering if there are any other resources on the subject (or open source implementations of similar engines).
Rice is looking very promising.
Ruby provides a very helpful README.EXT file. It has lots of information about how to extend Ruby, and convert between C & Ruby types.
There is also this excerpt from the pick axe book which pretty much covers the same thing.
In my case, when I added Ruby scripting to my application I decided against using swig, because my needs were very simple, and I didn't want to add yet another build dependency.
swig is probablly the way to go..... but ruby doesnt embed too well......
if you want a language that embeds nicely into C++, try lua
You might wish to check out tinyrb.
I've been working on Rarity (https://github.com/Plaristote/Rarity), which does two things:
Generates Ruby bindings from a YML description of your C++ API
Allows interaction with Ruby script in the most C++ fashion there is
I've solved a good deal of question (exception handling, garbage collection)...
I haven't seen anywhere else the code generation that Rarity uses to make your bindings come to life. I also haven't seen any other lib that allows such an easy conversion between C++ and Ruby types.
I think Rarity's worth the shot ! And I'd be glad to have some feedback as well :) !