GithubHelp home page GithubHelp logo

Comments (4)

ichenhe avatar ichenhe commented on July 17, 2024

README 写了

注入 Lua 表

类似注入普通变量。

java:

lua.newTable(); // 新建一个table,并压入栈。
lua.pushString( "from" );
lua.pushString( "java" );
lua.setTable( -3 ); // 设置一个元素
lua.pushString( "value" );
lua.pushString( "Hello lua" );
lua.setTable( -3 );
lua.setGlobal( "table" );

首先需要创建一个 table。接着将 key、value 依次入栈,随后调用 lua.setTable() 设置一个元素。它接受一个参数指明要设置的 table 在栈中的位置。因为我们已经压入了两个值,因此 table 的位置是-3。它会弹出栈顶的两个值分别作为 value、key。最后使用 lua.setGlobal() 弹出 table 本身并注入为变量。

上面代码运行后,等价于下面的 Lua table 定义:

table = {}
table["from"] = "java"
table["value"] = "Hello lua"

from android-lua.

milesleefj avatar milesleefj commented on July 17, 2024

谢谢回答。这样是可以作为全局变量传,我想问问能不能方法传参的方式调用呢。比如lua中有个方法是需要传局部变量。我试过构造一个map,然后调用pushJavaObject方式
Map map = new HashMap<>;
map.put(xx,xx);
lua.pushJavaObject(map)
但这个方法行不通。或者说pushJavaObject支持传递哪些java类型呢

from android-lua.

ichenhe avatar ichenhe commented on July 17, 2024

我觉得你的用法有问题。

pushJavaObject 是把 java 对象传给 lua,仅仅是传递一个引用,它仍然是 java 对象。因此 lua 拿到后要按照处理 obj 的方式来使用。这种做法也不推荐。

你的需求更像是先把 java map 转为 lua table,然后传给 lua 函数。此时 lua 接收到的就是一个原生的 lua table。要想这么做,只要把文档里「注入 Lua 表」部分和「Java 调用 Lua 函数」结合起来使用就行了。

  1. 获取 lua 函数入栈。
  2. 构造 lua 表
  3. 此时栈中有一个函数和一个 lua table。
  4. 调用 pcall 执行函数,指明有1个参数,lua 虚拟机会自动把栈中的那个元素作为参数取出来。

pushJavaObject支持传递哪些java类型呢

理论上支持全部类型,因为传的只是引用而已。

from android-lua.

milesleefj avatar milesleefj commented on July 17, 2024

原来如此,多谢

from android-lua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.