<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog @ SIB Visions &#187; Lua</title>
	<atom:link href="http://blog.sibvisions.com/tag/lua/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sibvisions.com</link>
	<description>Blog @ SIB Visions</description>
	<lastBuildDate>Mon, 13 Apr 2026 09:47:01 +0000</lastBuildDate>
		<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JVx and Lua, a proof of concept</title>
		<link>https://blog.sibvisions.com/2017/09/25/jvx-lua-proof-of-concept/</link>
		<comments>https://blog.sibvisions.com/2017/09/25/jvx-lua-proof-of-concept/#comments</comments>
		<pubDate>Mon, 25 Sep 2017 19:57:03 +0000</pubDate>
		<dc:creator>rzenz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JVx]]></category>
		<category><![CDATA[Lua]]></category>

		<guid isPermaLink="false">https://blog.sibvisions.com/?p=7221</guid>
		<description><![CDATA[We've found the time to look at something that was floating around the office for quite some time. A few of us had previous experiences with Lua, a simple scripting language, but nothing too concrete and while doing a prototype a question popped up: Would it be easy to create a JVx GUI in Lua? [...]]]></description>
			<content:encoded><![CDATA[<p>We've found the time to look at something that was floating around the office for quite some time. A few of us had previous experiences with <a href="https://www.lua.org/">Lua</a>, a simple scripting language, but nothing too concrete and while doing a prototype a question popped up: Would it be easy to create a JVx GUI in Lua? As it turns out, the answer is "yes".</p>
<h3 style="padding-top: 10px">Lua, a short tour</h3>
<blockquote><p>Lua is a lightweight, multi-paradigm programming language designed primarily for embedded systems and clients.</p>
<p>Lua was originally designed in 1993 as a language for extending software applications to meet the increasing demand for customization at the time. It provided the basic facilities of most procedural programming languages, but more complicated or domain-specific features were not included; rather, it included mechanisms for extending the language, allowing programmers to implement such features. As Lua was intended to be a general embeddable extension language, the designers of Lua focused on improving its speed, portability, extensibility, and ease-of-use in development.</p></blockquote>
<p>That is what <a href="https://en.wikipedia.org/wiki/Lua_%28programming_language%29">Wikipedia</a> has to say about Lua, but personally I like to think about it as "Basic done right", no insults intended. Lua is easy to write, easy to read and allows to quickly write and edit scripts.  There are <a href="http://lua-users.org/wiki/LuaImplementations">quite a few implementations for different languages and systems available</a> which makes it very versatile and usable from in nearly every environment.</p>
<p>The most simple Lua script is one that prints "Hello World":</p>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">print</span><span class="br0">&#40;</span><span class="st0">&quot;Hello World&quot;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<p>Because it is a prototype based language, functions are first-class citizens, which can be easily created, passed around and invoked:</p>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">local</span> <span class="kw1">call</span> <span class="sy0">=</span> <span class="kw1">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">print</span><span class="br0">&#40;</span><span class="st0">&quot;Hello World&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">end</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">call</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<p>Additionally, we can use tables to store state. They work like a simple key/value store:</p>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">local</span> operation <span class="sy0">=</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; method <span class="sy0">=</span> <span class="kw1">function</span><span class="br0">&#40;</span><span class="kw1">string</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span><span class="br0">&#40;</span><span class="kw1">string</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">end</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; value <span class="sy0">=</span> <span class="st0">&quot;Hello World&quot;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">operation.method<span class="br0">&#40;</span>operation.value<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">local</span> operation <span class="sy0">=</span> <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">operation.method <span class="sy0">=</span> <span class="kw1">function</span><span class="br0">&#40;</span><span class="kw1">string</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">print</span><span class="br0">&#40;</span><span class="kw1">string</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">end</span></div>
</li>
<li class="li2">
<div class="de2">operation.value <span class="sy0">=</span> <span class="st0">&quot;Hello World&quot;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">operation.method<span class="br0">&#40;</span>operation.value<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<p>Additionally, with some syntactic sugar, we can even emulate "real" objects. This is done by using a colon for invoking functions, which means that the table on which the function is invoked from will be provided as first parameter:</p>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">local</span> operation <span class="sy0">=</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; method <span class="sy0">=</span> <span class="kw1">function</span><span class="br0">&#40;</span>valueContainer, <span class="kw1">string</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span><span class="br0">&#40;</span>valueContainer.value .. <span class="st0">&quot; &quot;</span> .. <span class="kw1">string</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">end</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; value <span class="sy0">=</span> <span class="st0">&quot;Hello World&quot;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">operation:method<span class="br0">&#40;</span><span class="st0">&quot;and others&quot;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<p>Last but not least, the rules about "new lines" and "end of statements" are very relaxed in Lua, we can either write everything on one line or use semicolons as statement end:</p>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">local</span> <span class="kw1">call</span> <span class="sy0">=</span> <span class="kw1">function</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span> <span class="kw1">return</span> value + 5 <span class="kw1">end</span> <span class="kw1">print</span><span class="br0">&#40;</span><span class="kw1">call</span><span class="br0">&#40;</span>10<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">local</span> <span class="kw1">call</span> <span class="sy0">=</span> <span class="kw1">function</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> value + <span class="nu0">5</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">end</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">print</span><span class="br0">&#40;</span><span class="kw1">call</span><span class="br0">&#40;</span>10<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
</div>
<p>But enough of the simple things, let's jump right to the case.</p>
<h3 style="padding-top: 10px">World, meet JVx.Lua</h3>
<p><a href="https://github.com/sibvisions/jvx.lua">JVx.Lua</a> is a proof of concept Java/Lua bridge, which allows to use the JVx classes in Lua scripts. Additionally, we've created a short demo application, <a href="https://github.com/sibvisions/jvx.lua-live">JVx.Lua Live</a>, which allows to directly write Lua code and see the output live in the application.</p>
<p style="text-align: center"><a href="https://blog.sibvisions.com/wp-content/uploads/2017/09/jvxlualive1.png" rel="lightbox[7221]"><img src="https://blog.sibvisions.com/wp-content/uploads/2017/09/jvxlualive1.png" alt="JVx/Lua live demo" width="400" style="border: 1px solid #cccccc" /></a></p>
<p>The example code should be self-explanatory and the API is as close to the Java one as is possible. If an exception is thrown by the Lua environment it will be displayed in the live preview.</p>
<p style="text-align: center"><a href="https://blog.sibvisions.com/wp-content/uploads/2017/09/jvxlualive2.png" rel="lightbox[7221]"><img src="https://blog.sibvisions.com/wp-content/uploads/2017/09/jvxlualive2.png" alt="JVx/Lua live demo" width="400" style="border: 1px solid #cccccc" /></a></p>
<p>This allows to quickly test out the Lua bindings and create a simple GUI in no time. But note that this simple demo application does not store what you've created, when you close it, it will be gone.</p>
<h3 style="padding-top: 10px">How does it work?</h3>
<p>Glad you asked! The demo application is, of course, a simple GUI build with JVx, there are two core components which make it possible:</p>
<ol>
<li><a href="https://github.com/bobbylight/RSyntaxTextArea">RSyntaxTextArea</a>, a Swing component for displaying and editing code.</li>
<li><a href="http://www.luaj.org/luaj.html">LuaJ</a>, a Lua interpreter and compiler which allows to compile Lua directly to Java bytecode.</li>
</ol>
<p><a href="https://github.com/bobbylight/RSyntaxTextArea">RSyntaxTextArea</a> does not need to be further explained, it just works, and working very well it does. So does <a href="http://www.luaj.org/luaj.html">LuaJ</a>, but that one has to be explained.</p>
<p>To create a new "Lua environment" one has to instance a new set of Globals and install the Lua-to-Lua-Bytecode and Lua-Bytecode-to-Java-Bytecode compilers into it.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2">Globals globals <span class="sy0">=</span> <span class="kw1">new</span> Globals<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">LuaC.<span class="me1">install</span><span class="br0">&#40;</span>globals<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">LuaJC.<span class="me1">install</span><span class="br0">&#40;</span>globals<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">globals.<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">&quot;print(<span class="es0">\&quot;</span>Hello World<span class="es0">\&quot;</span>)&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
</div>
<p>And that's it! With this we can already execute Lua code directly in Java, and most importantly, at runtime.</p>
<p>By default, LuaJ does provide <em>nothing</em> for the Lua environment, which means that it is sandboxed by default. If we want to add functionality and libraries, we'll have to load it into the Globals as so called "libs". For example if we want to provide all functions which can be found inside the <a href="http://lua-users.org/wiki/StringLibraryTutorial">string table</a>, we'll have to load the StringLib:</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2">Globals globals <span class="sy0">=</span> <span class="kw1">new</span> Globals<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">LuaC.<span class="me1">install</span><span class="br0">&#40;</span>globals<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">LuaJC.<span class="me1">install</span><span class="br0">&#40;</span>globals<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">globals.<span class="me1">load</span><span class="br0">&#40;</span><span class="kw1">new</span> StringLib<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">globals.<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">&quot;print(string.sub(<span class="es0">\&quot;</span>Hello World<span class="es0">\&quot;</span>, 7))&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
</div>
<p>There are multiple libs provided with LuaJ which contain the standard library functions of Lua or provide access directly into the Java environment. For example we can coerce Java objects directly into Lua ones:</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw3">BigDecimal</span> value <span class="sy0">=</span> <span class="kw1">new</span> <span class="kw3">BigDecimal</span><span class="br0">&#40;</span><span class="st0">&quot;-5.1234&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">globals.<span class="me1">set</span><span class="br0">&#40;</span><span class="st0">&quot;value&quot;</span>, CoerceJavaToLua.<span class="me1">coerce</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
</div>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2"><span class="kw1">local</span> absoluteValue <span class="sy0">=</span> value:<span class="kw1">abs</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">local</span> squaredValue <span class="sy0">=</span> absoluteValue:pow<span class="br0">&#40;</span>2<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">print</span><span class="br0">&#40;</span>squaredValue:toString<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<p>Which gives us all the power of Java at our fingertips in Lua.</p>
<h3 style="padding-top: 10px">JVx bindings for Lua</h3>
<p>Armed with that knowledge, we can have a look at the bindings which make it possible to use the JVx classes. JVxLib and LuaUtil are the main classes which coerce a single class to be used by Lua, the procedure looks as follows:</p>
<ol>
<li>Create a LuaTable to hold the class and register it globally.</li>
<li>Add all public static fields (constants) to it.</li>
<li>Add all static methods to it.</li>
<li>Add a single constructor with a vararg argument to it.</li>
</ol>
<p>The most interesting point is the constructor call, we simply register a method called "new" on the table and give it a vararg argument, which means that it can be called with any number of arguments. When this function is invoked the arguments are processed and a fitting constructor for the object is chosen. The found constructor is invoked and the created object is coerced to a Lua object, that created Lua object is returned.</p>
<p>This allows us to use a clean syntax when it comes to accessing the static or instance state of the objects. Static methods and constants, including constructors, are always accessed using the "dot" notation, while everything related to the instance is accessed using the "colon" notation.</p>
<h3 style="padding-top: 10px">Downside, binding events</h3>
<p>For events and event handlers we had to dig a little deeper into LuaJ. The main problem with our EventHandler is that it has two methods: addListener(L) and addListener(IRunnable), at runtime the first one is reduced to addListener(Object). Let's assume the following code:</p>
<div class="codesnip-container" >
<div class="lua codesnip" style="font-family:monospace;">
<ol>
<li class="li2">
<div class="de2">button:eventAction<span class="br0">&#40;</span><span class="br0">&#41;</span>:addListener<span class="br0">&#40;</span>listener<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
</div>
<p>With such a construct LuaJ had a hard time finding the correct overload to use even when listener was a coerced IRunnable. This turned out to be undefined behavior, because the order of methods returned by a class object during runtime is undefined, sometimes LuaJ would choose the correct method and all other times it would use the addListener(Object) method. Which had "interesting" side effects, obviously, because an IRunnable object ended up in a list which should only hold objects of type L.</p>
<p>We've added a workaround so that functions with no parameters can be easily used, but for "full blown listener" support we'd have to invest quite some time. Which we might do at some point, but currently this is alright for a proof of concept.</p>
<h3 style="padding-top: 10px">Conclusion</h3>
<p>Using Lua from Java is easy thanks to <a href="http://www.luaj.org/luaj.html">LuaJ</a>, another possible option is <a href="https://github.com/mjanicek/rembulan/">Rembulan</a>, which can not go unmentioned when one talks about Java and Lua. It does not only allow to quickly and easily write logic, but with the right bindings one can even create complete GUIs and applications in it and thanks to the ability to compile it directly to Java bytecode it is nearly as fast as the Java code. But, with the big upside that it can be easily changed at runtime, even by users.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.sibvisions.com/2017/09/25/jvx-lua-proof-of-concept/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
