0

I'm using the example listed here:

http://www.luaj.org/luaj/3.0/README.html#5

It works fine, but instead of using inside the Lua script:

require 'hyperbolic'

I would like to use this or something similar in the java code

_G.set("hyperbolic", new hyperbolic());

Mostly to pass initial arguments to hyperbolic (like new hyperbolic(2.4, 1.67) when initializing it, so the Lua script is simple and "kid" friendly.

Any ideas or suggestions? Google isn't helping, possibly because I'm searching for the wrong thing..

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Air In
  • 945
  • 1
  • 6
  • 11

1 Answers1

0

By convention, instances of Java classes that implement lua libraries need to be called once with arguments (modulename, environment), and they set up the library for the supplied environment.

As coded, the hyperbolic library ignores the module name, and puts its functions in globals.hyperbolic

Globals globals = JsePlatform.standardGlobals();

hyperbolic module = new hyperbolic();
module.call(LuaValue.valueOf("hyperbolic"), globals);

This loads the library so you can use the function from scripts that have those globals as their environment. For example,

LuaValue chunk = globals.load(
    "print( 'sinh(0.5)', hyperbolic.sinh(0.5) )");
chunk.call();

will then output

sinh(0.5)   0.5210953

Unlike require(), this example does not populate the package.loaded table, so if you go on to require('hyperbolic'), it may be loaded a second time.

Jim Roseborough
  • 201
  • 1
  • 4