All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.luaj.vm2.lib.TableLib Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/*******************************************************************************
* Copyright (c) 2009 Luaj.org. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.vm2.lib;

import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

/** 
 * Subclass of {@link LibFunction} which implements the lua standard {@code table} 
 * library. 
 * 
 * 

* Typically, this library is included as part of a call to either * {@link JsePlatform#standardGlobals()} or {@link JmePlatform#standardGlobals()} *

* To instantiate and use it directly, * link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as: *

 {@code
 * LuaTable _G = new LuaTable();
 * LuaThread.setGlobals(_G);
 * _G.load(new BaseLib());
 * _G.load(new PackageLib());
 * _G.load(new TableLib());
 * LuaValue tbl = LuaValue.listOf( new LuaValue[] {
 * 		LuaValue.valueOf( "abc" ),
 * 		LuaValue.valueOf( "def" ) } );
 * LuaValue sep = LuaValue.valueOf( "-" );
 * System.out.println( _G.get("table").get("concat").call( tbl, sep ) );
 * } 
* Doing so will ensure the library is properly initialized * and loaded into the globals table. *

* This has been implemented to match as closely as possible the behavior in the corresponding library in C. * @see LibFunction * @see JsePlatform * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.5 */ public class TableLib extends OneArgFunction { public LuaValue call(LuaValue env) { LuaTable table = new LuaTable(); table.set("concat", new concat()); table.set("insert", new insert()); table.set("pack", new pack()); table.set("remove", new remove()); table.set("sort", new sort()); table.set("unpack", new unpack()); env.set("table", table); return NIL; } static class TableLibFunction extends LibFunction { public LuaValue call() { return argerror(1, "table expected, got no value"); } } // "concat" (table [, sep [, i [, j]]]) -> string static class concat extends TableLibFunction { public LuaValue call(LuaValue list) { return list.checktable().concat(EMPTYSTRING,1,list.length()); } public LuaValue call(LuaValue list, LuaValue sep) { return list.checktable().concat(sep.checkstring(),1,list.length()); } public LuaValue call(LuaValue list, LuaValue sep, LuaValue i) { return list.checktable().concat(sep.checkstring(),i.checkint(),list.length()); } public LuaValue call(LuaValue list, LuaValue sep, LuaValue i, LuaValue j) { return list.checktable().concat(sep.checkstring(),i.checkint(),j.checkint()); } } // "insert" (table, [pos,] value) -> prev-ele static class insert extends TableLibFunction { public LuaValue call(LuaValue list) { return argerror(2, "value expected"); } public LuaValue call(LuaValue table, LuaValue value) { table.checktable().insert(table.length()+1,value); return NONE; } public LuaValue call(LuaValue table, LuaValue pos, LuaValue value) { table.checktable().insert(pos.checkint(),value); return NONE; } } // "pack" (...) -> table static class pack extends VarArgFunction { public Varargs invoke(Varargs args) { LuaValue t = tableOf(args, 1); t.set("n", args.narg()); return t; } } // "remove" (table [, pos]) -> removed-ele static class remove extends TableLibFunction { public LuaValue call(LuaValue list) { return list.checktable().remove(0); } public LuaValue call(LuaValue list, LuaValue pos) { return list.checktable().remove(pos.checkint()); } } // "sort" (table [, comp]) static class sort extends TwoArgFunction { public LuaValue call(LuaValue table, LuaValue compare) { table.checktable().sort(compare.isnil()? NIL: compare.checkfunction()); return NONE; } } // "unpack", // (list [,i [,j]]) -> result1, ... static class unpack extends VarArgFunction { public Varargs invoke(Varargs args) { LuaTable t = args.checktable(1); switch (args.narg()) { case 1: return t.unpack(); case 2: return t.unpack(args.checkint(2)); default: return t.unpack(args.checkint(2), args.checkint(3)); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy