Package org.luaj.vm2

Examples of org.luaj.vm2.LuaValue


    }
    return NIL;
  }

  static LuaValue _setupvalue(Varargs args) {
    LuaValue func = args.checkfunction(1);
    int up = args.checkint(2);
    LuaValue value = args.arg(3);
    if ( func instanceof LuaClosure ) {
      LuaClosure c = (LuaClosure) func;
      LuaString name = findupvalue(c, up);
      if ( name != null ) {
        c.upValues[up-1].setValue(value);
View Full Code Here


      return arg;
    int level = arg.optint(1);
      arg.argcheck(level>=0, 1, "level must be non-negative");
    if ( level == 0 )
      return LuaThread.getRunning();
    LuaValue f = LuaThread.getCallstackFunction(level);
      arg.argcheck(f != null, 1, "invalid level");
      return f;
  }
View Full Code Here

  }

  public static Varargs pcall(LuaValue func, Varargs args, LuaValue errfunc) {
    try {
      LuaThread thread = LuaThread.getRunning();
      LuaValue olderr = thread.err;
      try {
        thread.err = errfunc;
        return varargsOf(LuaValue.TRUE, func.invoke(args));
      } finally {
        thread.err = olderr;
View Full Code Here

        return NIL;
      case 1: // "error", // ( message [,level] ) -> ERR
        throw new LuaError( arg1.isnil()? null: arg1.tojstring(), arg2.optint(1) );
      case 2: { // "setfenv", // (f, table) -> void
        LuaTable t = arg2.checktable();
        LuaValue f = getfenvobj(arg1);
        if ( ! f.isfunction() && ! f.isclosure() )
          error("'setfenv' cannot change environment of given object");
          f.setfenv(t);
          return f.isthread()? NONE: f;
      }
      }
      return NIL;
    }
View Full Code Here

            BaseLib.loadFile( args.checkjstring(1) );
        return v.isnil(1)? error(v.tojstring(2)): v.arg1().invoke();
      }
      case 2: // "getfenv", // ( [f] ) -> env
      {
        LuaValue f = getfenvobj(args.arg1());
          LuaValue e = f.getfenv();
        return e!=null? e: NIL;
      }
      case 3: // "getmetatable", // ( object ) -> table
      {
        LuaValue mt = args.checkvalue(1).getmetatable();
        return mt!=null? mt.rawget(METATABLE).optvalue(mt): NIL;
      }
      case 4: // "load", // ( func [,chunkname] ) -> chunk | nil, msg
      {
        LuaValue func = args.checkfunction(1);
        String chunkname = args.optjstring(2, "function");
        return BaseLib.loadStream(new StringInputStream(func), chunkname);
      }
      case 5: // "loadfile", // ( [filename] ) -> chunk | nil, msg
      {
        return args.isnil(1)?
          BaseLib.loadStream( baselib.STDIN, "stdin" ):
          BaseLib.loadFile( args.checkjstring(1) );
      }
      case 6: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg
      {
        LuaString script = args.checkstring(1);
        String chunkname = args.optjstring(2, "string");
        return BaseLib.loadStream(script.toInputStream(),chunkname);
      }
      case 7: // "pcall", // (f, arg1, ...) -> status, result1, ...
      {
        LuaValue func = args.checkvalue(1);
        LuaThread.onCall(this);
        try {
          return pcall(func,args.subargs(2),null);
        } finally {
          LuaThread.onReturn();
        }
      }
      case 8: // "xpcall", // (f, err) -> result1, ...       
      {
        LuaThread.onCall(this);
        try {
          return pcall(args.arg1(),NONE,args.checkvalue(2));
        } finally {
          LuaThread.onReturn();
        }
      }
      case 9: // "print", // (...) -> void
      {
        LuaValue tostring = LuaThread.getGlobals().get("tostring");
        for ( int i=1, n=args.narg(); i<=n; i++ ) {
          if ( i>1 ) baselib.STDOUT.write( '\t' );
          LuaString s = tostring.call( args.arg(i) ).strvalue();
          int z = s.indexOf((byte)0, 0);
          baselib.STDOUT.write( s.m_bytes, s.m_offset, z>=0? z: s.m_length );
        }
        baselib.STDOUT.println();
        return NONE;
      }
      case 10: // "select", // (f, ...) -> value1, ...
      {
        int n = args.narg()-1;        
        if ( args.arg1().equals(valueOf("#")) )
          return valueOf(n);
        int i = args.checkint(1);
        if ( i == 0 || i < -n )
          argerror(1,"index out of range");
        return args.subargs(i<0? n+i+2: i+1);
      }
      case 11: // "unpack", // (list [,i [,j]]) -> result1, ...
      {
        int na = args.narg();
        LuaTable t = args.checktable(1);
        int n = t.length();
        int i = na>=2? args.checkint(2): 1;
        int j = na>=3? args.checkint(3): n;
        n = j-i+1;
        if ( n<0 ) return NONE;
        if ( n==1 ) return t.get(i);
        if ( n==2 ) return varargsOf(t.get(i),t.get(j));
        LuaValue[] v = new LuaValue[n];
        for ( int k=0; k<n; k++ )
          v[k] = t.get(i+k);
        return varargsOf(v);
      }
      case 12: // "type",  // (v) -> value
        return valueOf(args.checkvalue(1).typename());
      case 13: // "rawequal", // (v1, v2) -> boolean
        return valueOf(args.checkvalue(1) == args.checkvalue(2));
      case 14: // "rawget", // (table, index) -> value
        return args.checktable(1).rawget(args.checkvalue(2));
      case 15: { // "rawset", // (table, index, value) -> table
        LuaTable t = args.checktable(1);
        t.rawset(args.checknotnil(2), args.checkvalue(3));
        return t;
      }
      case 16: { // "setmetatable", // (table, metatable) -> table
        final LuaValue t = args.arg1();
        final LuaValue mt0 = t.getmetatable();
        if ( mt0!=null && !mt0.rawget(METATABLE).isnil() )
          error("cannot change a protected metatable");
        final LuaValue mt = args.checkvalue(2);
        return t.setmetatable(mt.isnil()? null: mt.checktable());
      }
      case 17: { // "tostring", // (e) -> value
        LuaValue arg = args.checkvalue(1);
        LuaValue h = arg.metatag(TOSTRING);
        if ( ! h.isnil() )
          return h.call(arg);
        LuaValue v = arg.tostring();
        if ( ! v.isnil() )
          return v;
        return valueOf(arg.tojstring());
      }
      case 18: { // "tonumber", // (e [,base]) -> value
        LuaValue arg1 = args.checkvalue(1);
        final int base = args.optint(2,10);
        if (base == 10) {  /* standard conversion */
          return arg1.tonumber();
        } else {
          if ( base < 2 || base > 36 )
            argerror(2, "base out of range");
          return arg1.checkstring().tonumber(base);
        }
      }
      case 19: // "pairs" (t) -> iter-func, t, nil
        return varargsOf( baselib.next, args.checktable(1), NIL );
      case 20: // "ipairs", // (t) -> iter-func, t, 0
View Full Code Here

      this.func = func;
    }
    public int read() throws IOException {
      if ( func == null ) return -1;
      if ( bytes == null ) {
        LuaValue s = func.call();
        if ( s.isnil() ) {
          func = null;
          bytes = null;
          return -1;
        }
        bytes = s.tojstring().getBytes();
        offset = 0;
      }
      if ( offset >= bytes.length )
        return -1;
      return bytes[offset++];
View Full Code Here

            args.isvalue(4)? args.checkint(4): table.length() );
      }
      case 2: { // "insert" (table, [pos,] value) -> prev-ele
        final LuaTable table = args.checktable(1);
        final int pos = args.narg()>2? args.checkint(2): 0;
        final LuaValue value = args.arg( args.narg()>2? 3: 2 );
        table.insert( pos, value );
        return NONE;
      }
      case 3: { // "sort" (table [, comp]) -> void
        LuaTable table = args.checktable(1);
        LuaValue compare = (args.isnoneornil(2)? NIL: args.checkfunction(2));
        table.sort( compare );
        return NONE;
      }
      case 4: { // (table, func) -> void
        return args.checktable(1).foreach( args.checkfunction(2) );
View Full Code Here

public class JseMathLib extends org.luaj.vm2.lib.MathLib {
 
  public JseMathLib() {}

  public LuaValue call(LuaValue arg) {
    LuaValue t = super.call(arg);
    bind( t, JseMathLib1.class, new String[] {
      "acos", "asin", "atan", "cosh"
      "exp", "log", "log10", "sinh"
      "tanh" } );
    bind( t, JseMathLib2.class, new String[] {
View Full Code Here

   * each option is a function to be applied over the module.
   */
  public Varargs module(Varargs args) {
    LuaString modname = args.checkstring(1);
    int n = args.narg();
    LuaValue value = LOADED.get(modname);
    LuaValue module;
    if ( ! value.istable() ) { /* not found? */
     
        /* try global variable (and create one if it does not exist) */
      LuaValue globals = LuaThread.getGlobals();
      module = findtable( globals, modname );
      if ( module == null )
        error( "name conflict for module '"+modname+"'" );
      LOADED.set(modname, module);
    } else {
      module = (LuaTable) value;
    }
   
   
    /* check whether table already has a _NAME field */
    LuaValue name = module.get(_NAME);
    if ( name.isnil() ) {
      modinit( module, modname );
    }
   
    // set the environment of the current function
    LuaFunction f = LuaThread.getCallstackFunction(1);
View Full Code Here

        return JavaClass.forClass(clazz);
      }
      case NEWINSTANCE:
      case NEW: {
        // get constructor
        final LuaValue c = args.checkvalue(1);
        final Class clazz = (opcode==NEWINSTANCE? classForName(c.tojstring()): (Class) c.checkuserdata(Class.class));
        final Varargs consargs = args.subargs(2);
        return JavaClass.forClass(clazz).getConstructor().invoke(consargs);
      }
       
      case CREATEPROXY: {       
        final int niface = args.narg()-1;
        if ( niface <= 0 )
          throw new LuaError("no interfaces");
        final LuaValue lobj = args.checktable(niface+1);
       
        // get the interfaces
        final Class[] ifaces = new Class[niface];
        for ( int i=0; i<niface; i++ )
          ifaces[i] = classForName(args.checkjstring(i+1));
       
        // create the invocation handler
        InvocationHandler handler = new InvocationHandler() {
          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            String name = method.getName();
            LuaValue func = lobj.get(name);
            if ( func.isnil() )
              return null;
            boolean isvarargs = ((method.getModifiers() & METHOD_MODIFIERS_VARARGS) != 0);
            int n = args!=null? args.length: 0;
            LuaValue[] v;
            if ( isvarargs ) {               
              Object o = args[--n];
              int m = Array.getLength( o );
              v = new LuaValue[n+m];
              for ( int i=0; i<n; i++ )
                v[i] = CoerceJavaToLua.coerce(args[i]);
              for ( int i=0; i<m; i++ )
                v[i+n] = CoerceJavaToLua.coerce(Array.get(o,i));               
            } else {
              v = new LuaValue[n];
              for ( int i=0; i<n; i++ )
                v[i] = CoerceJavaToLua.coerce(args[i]);
            }
            LuaValue result = func.invoke(v).arg1();
            return CoerceLuaToJava.coerce(result, method.getReturnType());
          }
        };
       
        // create the proxy object
View Full Code Here

TOP

Related Classes of org.luaj.vm2.LuaValue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.