Java Code Examples for org.luaj.vm2.compiler.LuaC

The following examples show how to use org.luaj.vm2.compiler.LuaC. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: JsePlatform.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
     * Create a standard set of globals for JSE including all the libraries.
     *
     * @return Table of globals initialized with the standard JSE libraries
     * @see #debugGlobals()
     * @see JsePlatform
     * @see JmePlatform
     */
    public static Globals standardGlobals(Globals globals) {
        globals.load(new JseBaseLib());
        globals.load(new PackageLib());
        globals.load(new Bit32Lib());
        globals.load(new TableLib());
        globals.load(new StringLib());
        globals.load(new CoroutineLib());
        globals.load(new JseMathLib());
        globals.load(new JseOsLib());
//		globals.load(new JseIoLib());//安全考虑,删除for LuaView
//		globals.load(new LuajavaLib());//安全考虑,删除for LuaView
        LoadState.install(globals);
        LuaC.install(globals);
        return globals;
    }
 
Example #2
Source Project: XPrivacyLua   Author: M66B   File: JsePlatform.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a standard set of globals for JSE including all the libraries.
 * 
 * @return Table of globals initialized with the standard JSE libraries
 * @see #debugGlobals()
 * @see org.luaj.vm2.lib.jse.JsePlatform
 * @see org.luaj.vm2.lib.jme.JmePlatform
 */
public static Globals standardGlobals() {
	Globals globals = new Globals();
	globals.load(new JseBaseLib());
	globals.load(new PackageLib());
	globals.load(new Bit32Lib());
	globals.load(new TableLib());
	globals.load(new StringLib());
	globals.load(new CoroutineLib());
	globals.load(new JseMathLib());
	globals.load(new JseIoLib());
	globals.load(new JseOsLib());
	globals.load(new LuajavaLib());
	LoadState.install(globals);
	LuaC.install(globals);
	return globals;		
}
 
Example #3
Source Project: luaj   Author: luaj   File: JsePlatform.java    License: MIT License 6 votes vote down vote up
/**
 * Create a standard set of globals for JSE including all the libraries.
 * 
 * @return Table of globals initialized with the standard JSE libraries
 * @see #debugGlobals()
 * @see org.luaj.vm2.lib.jse.JsePlatform
 * @see org.luaj.vm2.lib.jme.JmePlatform
 */
public static Globals standardGlobals() {
	Globals globals = new Globals();
	globals.load(new JseBaseLib());
	globals.load(new PackageLib());
	globals.load(new Bit32Lib());
	globals.load(new TableLib());
	globals.load(new JseStringLib());
	globals.load(new CoroutineLib());
	globals.load(new JseMathLib());
	globals.load(new JseIoLib());
	globals.load(new JseOsLib());
	globals.load(new LuajavaLib());
	LoadState.install(globals);
	LuaC.install(globals);
	return globals;
}
 
Example #4
Source Project: luaj   Author: luaj   File: JmePlatform.java    License: MIT License 6 votes vote down vote up
/**
 * Create a standard set of globals for JME including all the libraries.
 * 
 * @return Table of globals initialized with the standard JME libraries
 * @see #debugGlobals()
 * @see org.luaj.vm2.lib.jse.JsePlatform
 * @see org.luaj.vm2.lib.jme.JmePlatform
 */
public static Globals standardGlobals() {
	Globals globals = new Globals();
	globals.load(new BaseLib());
	globals.load(new PackageLib());
	globals.load(new Bit32Lib());
	globals.load(new OsLib());
	globals.load(new MathLib());
	globals.load(new TableLib());
	globals.load(new StringLib());
	globals.load(new CoroutineLib());
	globals.load(new JmeIoLib());
	LoadState.install(globals);
	LuaC.install(globals);
	return globals;		
}
 
Example #5
Source Project: remixed-dungeon   Author: NYRDS   File: LuaEngine.java    License: GNU General Public License v3.0 6 votes vote down vote up
private LuaEngine() {
	globals = new Globals();
	globals.load(new JseBaseLib());
	globals.load(new PackageLib());
	globals.load(new Bit32Lib());
	globals.load(new TableLib());
	globals.load(new StringLib());
	globals.load(new CoroutineLib());
	globals.load(new JseMathLib());
	globals.load(new JseIoLib());
	globals.load(new JseOsLib());
	globals.load(new MultiDexLuajavaLib());
	LoadState.install(globals);
	LuaC.install(globals);

	globals.finder = this;
	globals.set("loadResource", new resLoader());
}
 
Example #6
Source Project: HtmlNative   Author: hsllany   File: JsePlatform.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a standard set of globals for JSE including all the libraries.
 *
 * @return Table of globals initialized with the standard JSE libraries
 * @see #debugGlobals()
 * @see org.luaj.vm2.lib.jse.JsePlatform
 * @see org.luaj.vm2.lib.jme.JmePlatform
 */
public static Globals standardGlobals() {
    Globals globals = new Globals();
    globals.load(new JseBaseLib());
    globals.load(new PackageLib());
    globals.load(new Bit32Lib());
    globals.load(new TableLib());
    globals.load(new StringLib());
    globals.load(new JseMathLib());
    globals.load(new JseOsLib());
    LoadState.install(globals);
    LuaC.install(globals);
    return globals;
}
 
Example #7
Source Project: Bytecoder   Author: mirkosertic   File: LuaDemo.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
    globals = new Globals();
    LuaC.install(globals);
    final Window window = Window.window();
    final Document document = window.document();
    inputCode = document.getElementById("luainput");
    output = document.getElementById("luaoutput");
    button = document.getElementById("runlua");
    button.addEventListener("click", new EventListener<Event>() {
        @Override
        public void run(final Event aEvent) {
            runLua();
        }
    });
}
 
Example #8
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaReturnString() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaValue chunk = theGlobals.load("return 'hello, world'");
    final LuaString theResult = chunk.call().strvalue();
    Assert.assertEquals("hello, world", theResult.tojstring());
}
 
Example #9
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaReturnInteger() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaValue chunk = theGlobals.load("return 123");
    final LuaString theResult = chunk.call().strvalue();
    Assert.assertEquals("123", theResult.tojstring());
}
 
Example #10
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaReturnIntegerAdd() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaValue chunk = theGlobals.load("return 123 + 231");
    final LuaString theResult = chunk.call().strvalue();
    Assert.assertEquals("354", theResult.tojstring());
}
 
Example #11
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaFunction() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaValue chunk = theGlobals.load("function add(a,b)\nreturn a+b\nend\nreturn add(1,2)");
    Assert.assertFalse(chunk.isnil());
    Assert.assertTrue(chunk instanceof LuaClosure);
    final LuaClosure luaClosure = (LuaClosure) chunk;
    final LuaString theResult = luaClosure.call().strvalue();
    Assert.assertEquals("3", theResult.tojstring());
}
 
Example #12
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testVarArgs() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[]{
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
}
 
Example #13
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCall() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return a + b end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final LuaValue theFunction = theGlobals.get("add");
    Assert.assertFalse(theFunction.isnil());
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaInteger theResult = (LuaInteger) theFunction.invoke(theArguments);
    Assert.assertEquals("300", theResult.tojstring());
}
 
Example #14
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCallStringResult() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return 'hello' end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaValue theFunction = theGlobals.get("add");
    final LuaValue theValue = (LuaValue) theFunction.invoke(theArguments);
    Assert.assertTrue(theValue.isstring());
    Assert.assertEquals("hello", theValue.tojstring());
}
 
Example #15
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaTable() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaTable theTable = new LuaTable();
    theTable.set("20", 200);
    Assert.assertEquals(200, theTable.get("20").toint(), 0);
}
 
Example #16
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaConversion() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    theGlobals.set("key", 10);
    Assert.assertEquals(10, theGlobals.get("key").toint());
    Assert.assertEquals(10, theGlobals.get(LuaString.valueOf("key")).toint());

    final LuaValue chunk = theGlobals.load("return key").call();
    Assert.assertEquals(10, chunk.toint());
}
 
Example #17
Source Project: luaj   Author: luaj   File: SampleApplet.java    License: MIT License 5 votes vote down vote up
public void init() {
	// Find the script to load from the applet parameters.
	String script = getParameter(script_parameter);
	if (script == null)
		script = default_script;
	System.out.println("Loading " + script);

	// Construct globals specific to the applet platform.
	globals = new Globals();
	globals.load(new JseBaseLib());
	globals.load(new PackageLib());
	globals.load(new Bit32Lib());
	globals.load(new TableLib());
	globals.load(new JseStringLib());
	globals.load(new CoroutineLib());
	globals.load(new JseMathLib());
	globals.load(new JseIoLib());
	globals.load(new JseOsLib());
	globals.load(new AppletLuajavaLib());
	LoadState.install(globals);
	LuaC.install(globals);

	// Use custom resource finder.
	globals.finder = this;

	// Look up and save the handy pcall method.
	pcall = globals.get("pcall");

	// Load and execute the script, supplying this Applet as the only
	// argument.
	globals.loadfile(script).call(CoerceJavaToLua.coerce(this));
}
 
Example #18
Source Project: luaj   Author: luaj   File: SampleSandboxed.java    License: MIT License 4 votes vote down vote up
public static void main(String[] args) {
	// Create server globals with just enough library support to compile user scripts.
	server_globals = new Globals();
	server_globals.load(new JseBaseLib());
	server_globals.load(new PackageLib());
	server_globals.load(new JseStringLib());

	// To load scripts, we occasionally need a math library in addition to compiler support.
	// To limit scripts using the debug library, they must be closures, so we only install LuaC.
	server_globals.load(new JseMathLib());
	LoadState.install(server_globals);
	LuaC.install(server_globals);

	// Set up the LuaString metatable to be read-only since it is shared across all scripts.
	LuaString.s_metatable = new ReadOnlyLuaTable(LuaString.s_metatable);

	// Example normal scripts that behave as expected.
	runScriptInSandbox( "return 'foo'" );
	runScriptInSandbox( "return ('abc'):len()" );
	runScriptInSandbox( "return getmetatable('abc')" );
	runScriptInSandbox( "return getmetatable('abc').len" );
	runScriptInSandbox( "return getmetatable('abc').__index" );

	// Example user scripts that attempt rogue operations, and will fail.
	runScriptInSandbox( "return setmetatable('abc', {})" );
	runScriptInSandbox( "getmetatable('abc').len = function() end" );
	runScriptInSandbox( "getmetatable('abc').__index = {}" );
	runScriptInSandbox( "getmetatable('abc').__index.x = 1" );
	runScriptInSandbox( "while true do print('loop') end" );
	
	// Example use of other shared metatables, which should also be made read-only.
	// This toy example allows booleans to be added to numbers.
	runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
	LuaBoolean.s_metatable = new ReadOnlyLuaTable(LuaValue.tableOf(new LuaValue[] {
			LuaValue.ADD, new TwoArgFunction() {
				public LuaValue call(LuaValue x, LuaValue y) {
					return LuaValue.valueOf(
							(x == TRUE ? 1.0 : x.todouble()) +
							(y == TRUE ? 1.0 : y.todouble()) );
				}
			},
	}));
	runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
}