• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

mherkender/lua.js: An ECMAscript framework to compile and run Lua code, allowing ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

mherkender/lua.js

开源软件地址(OpenSource Url):

https://github.com/mherkender/lua.js

开源编程语言(OpenSource Language):

JavaScript 51.9%

开源软件介绍(OpenSource Introduction):

Download

Click here to download precompiled versions of lua.js

To build these files yourself, use make in the root of the project, for example...

make CLOSURE_COMPILER=/path/to/closure/compiler.jar

(the CLOSURE_COMPILER variable only needs to be set if you're building the minified versions of the scripts)

Usage

You can use the lua2js tool to translate Lua code into Javascript.

./lua2js input.lua output.js

(node is required to run lua2js)

lua(.min).js must also be embedded on the same page as generated Javascript code. It contains all the functions necessary to keep the code working.

When a script generated by lua2js is run, the variable lua_script will be set to the global variable (_G) of the script. You can use this variable to interact with the script using the functions in lua.js/lua.as.

<script type="text/javascript" src="lua.min.js">
<script type="text/javascript" src="generated_code.js">
<script type="test/javascript">

  // this is equivalent to: init(1, 2, 3)
  lua_call(lua_tableget(lua_script, "init"), [1, 2, 3]);

  // this is equivalent to: local numActive = activeChars.count
  var numActive = lua_tableget(lua_tableget(lua_script, "activeChars"), "count")[0];
  
  // this is equivalent to: local distFromCenter = get_distance({x=1, y=2})
  var distFromCenter = lua_call(lua_tableget(lua_script, "get_distance"), [lua_newtable(null, "x", 1, "y", 2)])[0];

</script>

If you are dealing with multiple Lua scripts, modules should be used. They can be used normally for inter-Lua interactions, and the lua_module() function can be used by Javascript to retrieve modules.

lua2js also generates valid ActionScript code as well. Include lua.as (not lua.js) and the generated file to make it work.

You can also use the function lua_load() to parse Lua code in the browser. lua_load() is only available when the lua+parser.js version of lua.js is used. lua_load() will parse the script, and return a function that will execute the script when called and returning the global variable of that script.

Known issues

  • There are missing library functions and incomplete implementations of some functions. They should all be marked with TODO comments, or throw "Not supported" errors.
  • The global table does not currently support metatables.
  • Some Lua functions do not handle error conditions in the same way as standard Lua does.
  • Nested comments (e.g. --[=[ ]=]--) are not supported
  • __mode and __gc metamethods are not supported

Some implementation details

  • This project works by converting Lua code directly to ECMAscript. An alternative solution would be to emulate a Lua VM to better maintain compatibility, but this would be much slower.
  • Lua functions in ECMAscript always return arrays. This is to support the multiple return values that Lua uses.
  • Tables are normal objects with sub-objects (str, uints, etc) that seperate different kinds of keys from each other.
  • The uints key in an object can be an array or an object, depending on how it is used. If table.insert is used for example, then it will be converted to an array if it is not already one. To force uints to be one thing or another, use ensure_arraymode() or ensure_notarraymode() to require it be available in a certain form.
  • lua.js can be compiled using Closure Compiler's ADVANCED_OPTIMIZATIONS mode for a dramatically smaller total file size. This is more complicated than traditional minifying so you'll need to use variable maps or combine Lua-related scripts together.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap