Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Publish lua.vm.js to npm #26

Merged
merged 3 commits into from Jun 19, 2015
Merged

Conversation

statico
Copy link
Contributor

@statico statico commented Jun 19, 2015

Hi Alon,

This pull request lets this useful module be used as a NodeJS/ioj.s and published to npmjs.org. All you'll need to do is run npm publish. (More docs on publishing npm modules here.)

Also -- is there a better way of calling Lua functions? I'm currently creating a Lua state, executing a modified JSON.lua, executing my script, capturing stdout, printing the JSON-encoded result of the Lua function from within Lua, and then parsing that captured output:

var luavm = require('lua.vm.js');
var intercept = require('intercept-stdout');
var fs = require('fs');

function getResult() {
  var state = new luavm.Lua.State();
  state.execute(fs.readFileSync('JSON-modified.lua', 'utf8'));
  state.execute(fs.readFileSync('foo.lua', 'utf8')); // Defines function bar()
  var buf = '', unhook = intercept(function(chunk) { buf += chunk; });
  state.execute('print(JSON:encode(bar()))')
  unhook();
  return JSON.parse(buf);
}

Thanks for your great work here and elsewhere.

kripken added a commit that referenced this pull request Jun 19, 2015
@kripken kripken merged commit a641419 into daurnimator:master Jun 19, 2015
@kripken
Copy link
Collaborator

kripken commented Jun 19, 2015

Thanks!

About the question, I haven't had time for this project in a while, so I would open an issue where other people that are more current would be more likely to notice. But I do think what you are doing there is the right thing, nothing obviously unoptimal there.

@statico statico mentioned this pull request Jun 19, 2015
@daurnimator
Copy link
Owner

is there a better way of calling Lua functions?

The whole lua C api is available.
Then you can marshall back to javascript with lua_to_js
e.g.

L.getglobal('math'); /* push 'math' table onto lua stack */
math = L.lua_to_js(-1);
L.pop(1); /* pop 'math' off the lua stack */
math_floor = math.get('floor')
console.log(math_floor.call(2.2)) // prints 2

modified JSON.lua

I recommend dkjson ( http://dkolf.de/src/dkjson-lua.fsl/home ) as a nicer pure lua json library

executing my script, capturing stdout, printing the JSON-encoded result of the Lua function from within Lua, and then parsing that captured output:

ew! just use the return value from execute:

return state.execute("return bar()")`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants