gamesys / moonshine

A lightweight Lua VM for the browser
http://moonshinejs.org
MIT License
501 stars 35 forks source link

Passing JSON Directly Yields Error #43

Closed Rybadour closed 7 years ago

Rybadour commented 7 years ago

I'm trying to use Moonshine in react-native and as such the only way I think I can get this to work is by passing the distilled JSON directly to the VM. However when I try this I get a strange error:

attempt to perform arithmetic on a string value
    @src/lua/test.lua [(compiled code):2]

My code is pretty straight forward:

var luaVm = new shine.VM({
    global: 42,
    speak: console.log,
});
var luaSource = require('../../test.lua.json');
luaVm.load(luaSource);

The distilled file is:

{"sourceName":"@src/lua/test.lua","lineDefined":0,"lastLineDefined":0,"upvalueCount":0,"paramCount":0,"is_vararg":2,"maxStackSize":3,"instructions":[1,0,0,0,5,1,1,0,5,2,2,0,12,2,2,259,12,2,2,0,12,2,2,260,28,1,2,1,30,0,1,0],"constants":["dog","speak","global",": "," says woof!"],"functions":[],"linePositions":[1,2,2,2,2,2,2,2],"locals":[{"varname":"animal","startpc":1,"endpc":7}],"upvalues":[],"sourcePath":"src/lua/test.lua"}

I couldn't find any documentation about this use case but while reading the source code this seems like the way to do this.

Thank you.

paulcuth commented 7 years ago

Hi,

I've not heard of anyone attempting to use Moonshine with React Native before, so I'm rathing interested in your results.

From what you say, it looks like the JSON file is being loaded and executed fine. The error is from the runtime, which usually means there's an error in your Lua code. Could you attach the original Lua code that you're "distilling"?

From what I can read from the bytecode, I'm seeing arithmetic ADD operations and a number of strings. Could it be that you are performing maths operations on strings? ie. something like print('a' + 'b' + 'c'), instead of print('a'..'b'..'c')?

Also, in your call to initialise the VM, browsers (at least) would have an issue with the fact that the log() function is not bound to the console object. I don't know if that's an issue in React Native, but you may the following more helpful:

var luaVm = new shine.VM({
    global: 42,
    speak: console.log.bind(console),
});
paulcuth commented 7 years ago

Also, as there's not much development going on Moonshine these days, depending on what you're doing you may like to checkout Starlight too:

http://starlight.paulcuth.me.uk

Rybadour commented 7 years ago

Ah that makes more sense now. I was looking at the JSON and thought it meant it had trouble with the first line of the JSON since it mentions "@src/lua/test.lua". Everything seems to working fine now!

I took a look Starlight and Moonshine but Moonshine seemed like a simpler solution.