Embedding Lua, in Scala, using Java(转)

LuaJ

I was reading over the list of features that CurioDB lacks compared to Redis , that I’d previously documented. It contained the item “no Lua scripting”, which I’d written confidently at the time, certain that I wouldn’t possibly be able to implement something so serious in my toy project. But then I thought to myself, “why not?”, and after a bit of research, I discovered the fantastic LuaJ library , which transformed the task from a significant engineering feat, into yet another case of merely gluing some libraries together.

LuaJ provides a complete API for compiling and running Lua scripts, and exposing JVM code to them as Lua functions. LuaJ also makes a complete range of Lua data types available to your JVM code. I purposely refer to the JVM ( Java Virtual Machine ) here rather than Java the language, as in my case, it’s actually Scala being used to code against LuaJ, which makes the overall accomplishment seem even more death-defying.

Obligatory Blog Snippets

Let’s take a look. First up, we’ll compile and run a snippet of Lua (sans imports and class scaffolding, which you can pick up from the final result in CurioDB if you wish):

// The global namespace we'll run the script in, which we could preload
// variables into if needed.
val globals = JsePlatform.standardGlobals()

// A tiny string of Lua code - it returns the second string from a
// table called "names" (Lua tables use 1-based indexes).
val script = "names = {'foo', 'bar', 'baz'}; return names[2]"

// Running the script returns a LuaValue object, which from our Lua
// code above, we know to be a string, so we cast it to one and print
// the value, which should be "bar".
val result = globals.load(script).call()
println(result.asjstring)

As simple as that. Now let’s look at passing Lua variables from JVM land into a Lua script, and pulling Lua variables back out as JVM objects:

// The same table declared above in Lua, but this time in Scala.
val names = LuaValue.listOf(Array("foo", "bar", "baz").map(LuaValue.valueOf))

// Create the global namespace again, and pass the Lua table into it.
val globals = JsePlatform.standardGlobals()
globals.set("names", names)

// Our Lua code is inline here, and just returns the variable "names".
val result = globals.load("return names").call()

// The result is our original table back in Scala, where we can access
// items by index, just as we previously did in Lua.
println(result.get(2).asjstring)

There you have it - JVM objects representing Lua variables, moving into and back out of Lua code. Now let’s look at the reverse, calling JVM code from inside Lua. To do this, we implement a class representing a Lua function:

// Simple function that takes a string, and prefixes it with "Hello ".
class SayHelloFunction extends OneArgFunction {
  override def call(name: LuaValue) =
    LuaValue.valueOf("Hello " + name.tojstring)
}

// Again, build the global namespace, this time adding the function
// object to it with a given name, and calling it from within Lua.
val globals = JsePlatform.standardGlobals()
globals.set("say_hello", new SayHelloFunction())
val result = globals.load("return say_hello('grandma')").call()
println(result.asjstring)  // prints "Hello grandma".

That’s it, the full round trip - Lua calling JVM code, and vice versa. With that working, the rest is up to your imagination. I’ve only scratched the surface of what LuaJ provides - all of the data types found in Lua, its standard library, and much more.

The final result was a little more involved than the above implies. CurioDB is carefully designed to be non-blocking, using tell rather than ask , where actors only ever send messages forwards, without expectation of a reply. The challenge here was introducing the synchronous redis.call API into a fundamentally asynchronous system. The solution involved modelling the scripting API as a client actor, with a controlled amount of blocking, much like the way TCP and HTTP connections are managed in the system.

Call-ception

A really fun and whacky side effect of implementing this possibly a little too correctly(for lack of a better term), is that it unintentionally allows the Lua API to recursively call itself:

$ redis-cli EVAL "return redis.call('EVAL', 'return redis.call(\'EVAL\', \'return redis.call(\\\\\'TIME\\\\\')\', 0)', 0)" 0
1) (integer) 227734
2) (integer) 541653

Is this a feature, or a bug? The scripting API in Redis specifically disallows this, most likely for good reason.

http://www.tuicool.com/articles/M36byiy

 http://stackoverflow.com/questions/32573748/multiple-return-values-in-luaj

http://levelup.sinaapp.com/

http://www.luaj.org/luaj/3.0/README.html

http://luaj.org/luaj/3.0/api/org/luaj/vm2/LuaValue.html

http://stackoverflow.com/questions/12358047/how-can-i-pass-objects-to-an-exposed-luaj-function

时间: 2024-11-02 02:36:48

Embedding Lua, in Scala, using Java(转)的相关文章

机器学习新星:Scala 优于 Java 的五大理由!

Java 在程序猿江湖的霸主地位已经很多年没有受到挑战了. 作为一门主流编程语言,在所有领域的普及率.职业选择.业界倾向榜单中,Java 即使不在榜首,也位于前列.即便是 Python 为王的机器学习领域,Java 的普及程度也稳居第二.三位,仅有 R 语言可与之相比.Java 能有如此巨大的成功,跟它的灵活多功能,以及处理复杂任务的能力是分不开的.但有没有比 Java 更好的选择呢? 就雷锋网所知,在资深程序猿眼中,能替代 Java.并且能做得比它更好的只有 Scala.这篇文章并不是对 Sc

编译Lambda表达式: Scala和Java 8

原文地址,译文地址,译者:梁海舰, 校对:丁一 最近几年Lambda表达式风靡于编程界. 很多现代编程语言都把它作为函数式编程的基本组成部分. 基于JVM的编程语言如Scala,Groovy还有Clojure把它们作为关键部分集成在语言中.现在Java8也加入了它们的行列. 有趣的是,对于JVM来说,Lambda表达式是完全不可见的,并没有匿名函数和Lamada表达式的概念,它只知道字节码是严格面向对象规范的.它取决于语言的作者和它的编译器在规范限制内创造出更新,更高级的语言元素. 我们第一次接

Scala入门到精通——第二十八节 Scala与JAVA互操作

本节主要内容 JAVA中调用Scala类 Scala中调用JAVA类 Scala类型参数与JAVA泛型互操作 Scala与Java间的异常处理互操作 1. JAVA中调用Scala类 Java可以直接操作纵Scala类,如同Scala直接使用Java中的类一样,例如: //在Person.scala文件中定义Scala语法的Person类 package cn.scala.xtwy.scalaToJava class Person(val name:String,val age:Int) //伴

Scala 比 Java 还快? 【已翻译100%】

通常Scala被认为比Java要慢,特别是用于函数式编程时.本文会解释为什么这个被广泛接受的假设是错误的. 数据验证 编程中一个常见的问题是数据验证.即我们要确保所有得到的数据处于正确的结构中.我们需要从安全的,编译器验证的数据中找到不安全的外部输入.在一个典型的WEB应用中,你需要验证每个请求.很明显这会影响你的应用的性能.在本文中我将会比较处理这个问题的两种极不相同的解决方案.Java的Bean验证API和来自play的统一验证API.后者是一种更为函数式的方法,它具有不变性和类型安全的特性

R、Python、Scala 和 Java,到底该使用哪一种大数据编程语言?

有一个大数据项目,你知道问题领域(problem domain),也知道使用什么基础设施,甚至可能已决定使用哪种框架来处理所有这些数据,但是有一个决定迟迟未能做出:我该选择哪种语言?(或者可能更有针对性的问题是,我该迫使我的所有开发人员和数据科学家非要用哪种语言?)这个问题不会推迟太久,迟早要定夺. 当然,没有什么阻止得了你使用其他机制(比如XSLT转换)来处理大数据工作.但通常来说,如今大数据方面有三种语言可以选择:R.Python和Scala,外加一直以来屹立于企业界的Java.那么,你该选

写给Java老司机的Scala教程——Scala Fast Track

引子 如果说有什么编程语言让我觉得收获颇大的话,我想除了 Java 那么另一个就是 Scala,Java 教会了我工程和严谨,而 Scala 则进一步的给了我耳目一新的思维模式,并提高了我对OOP的认识,反过来,Scala的习得,也让我成为了一个更好的Java程序员. 背景 我写这个系列教程,除了分享我自己学习Scala的一些心得体会之外,并不是要特别的安利大家Scala.而我其实比较愚钝,所以我学习Scala 的时候走了不少弯路,记得当时还是 Scala 2.10,然后各种学习资料不是特别完善

Java 下一代: Groovy、Scala 和 Clojure 中的共同点(二)

了解Java 下一代语言如何减少样板代码和降低复杂性 Java 编程语言诞生时所面临的限制与如今的开发人员所面临的条件有所不同.具体来讲,由于上世纪 90 年代中期的硬 件的性能和内存限制,Java 语言中存在原语类型.从那时起,Java 语言不断在演化,通过自动装箱(autobox)消除了许 多麻烦操作,而下一代语言(Groovy.Scala 和 Clojure)更进一步,消除了每种语言中的不一致性和冲突. 在这 一期的文章中,我将展示下一代语言如何消除一些常见的 Java 限制,无论是语法上

给Java开发者的Scala教程

author:Michel Schinz,Philipp Haller 1. 简介 本文将该要的介绍Scala语言和其编译.这里假设读者已经有一定的java开发经验,需要概要的了解他们可以用Scala 做些什么. 2. 第一个例子 我们用全世界最著名的代码来作为开始.虽然没什么用,但是可以很好地直观的了解Scala: object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, world!") 

JVM 并发性: Java 和 Scala 并发性基础

Java 并发性支持 在 Java 平台诞生之初,并发性支持就是它的一个特性,线程和同步的实现为它提供了超越其他竞争语言的优势.Scala 基于 Java 并在 JVM 上运行,能够直接访问所有 Java 运行时(包括所有并发性支持).所以在分析 Scala 特性之前,我首先会快速回顾一下 Java 语言已经提供的功能. Java 线程基础 在 Java 编程过程中创建和使用线程非常容易.它们由 java.lang.Thread 类表示,线程要执行的代码为 java.lang.Runnable