weimingtom / jnlua

Automatically exported from code.google.com/p/jnlua
0 stars 0 forks source link

Alternatives to using lua_load() for loading file based chunks? #10

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

I just started using JNLua in my Java project (Lua in general for that matter). 
Basically, I am calling into Lua scripts to run some functions from my Java 
code. Everything works great, but the problems I am having are more related to 
the debug environment.

I have been trying to setup my debug environment by attaching to the running 
Java process. (I am currently using Eclipse/LDT 1.0.1). The issue I have been 
having is that my breakpoints are never hit. It appears the reason for this is 
how I am loading the Lua script file from Java (using lua_load()):

import com.naef.jnlua.LuaState;
...
    luaState.openLibs();
    luaState.load(
        Gdx.files.internal(scriptFileName).read(), //read() returns InputStream
        Gdx.files.internal(scriptFileName).file().getName(), //getName() returns filename, "atm.lua"
        "bt"); //read as binary or text
    // Evaluate the chunk, thus defining the function 
    luaState.call(0, 0); // No arguments, no returns 
...

Then when called later:
...
        luaState.getGlobal("create");
    luaState.pushJavaObject(entity); // Push argument #1 
    luaState.call(1, 0); // 1 argument, 0 return 
...

In looking through the Eclipse Koneki forums, it appears that Lua files loaded 
using lua_load() are considered "dynamic code" which was defined in the 
documentation as code "compiled with load or loadstring and is not handled (the 
debugger will step over it, like C code)"

There was one little excerpt in the forum that caught my attention in how the 
developer solved his problem:
"To use an embedded script you need to load it in the style of luaL_loadfile, 
pushing the filename onto the stack first with lua_pushfstring(L, "@%s", 
filename); and removing it after calling luaL_loadbuffer."

I looked through the JNI bridge source (jnlua.c) and did not see any of these 
functions available. 

My question is whether exposing lua_pushfstring() and luaL_loadbuffer() even 
works within the model of JNLua, and if so, could this be added as a feature 
request? If this doesn't fit within the design of JNLua, are there any 
alternatives to using lua_load() for files?

Any help would be appreciated.

Original issue reported on code.google.com by patrick....@gmail.com on 10 Dec 2013 at 9:49

GoogleCodeExporter commented 9 years ago
Your code should work if your prefix the "@" character in front of the file 
name (2nd argument).

Original comment by an...@naef.com on 15 Dec 2013 at 6:48