Package org.luaj.vm2

Examples of org.luaj.vm2.LuaValue


    do {
      e = fname.indexOf(_DOT, b=e+1 );
      if ( e < 0 )
        e = fname.m_length;
      LuaString key = fname.substring(b, e);
      LuaValue val = table.rawget(key);
      if ( val.isnil() ) { /* no such field? */
        LuaTable field = new LuaTable(); /* new table for field */
        table.set(key, field);
        table = field;
      } else if ( ! val.istable() ) {  /* field has a non-table value? */
        return null;
      } else {
        table = val;
      }
    } while ( e < fname.m_length );
View Full Code Here


   * If there is any error loading or running the module, or if it cannot find any loader for
   * the module, then require signals an error.
   */ 
  public LuaValue require( LuaValue arg ) {
    LuaString name = arg.checkstring();
    LuaValue loaded = LOADED.get(name);
    if ( loaded.toboolean() ) {
      if ( loaded == _SENTINEL )
        error("loop or previous error loading module '"+name+"'");
      return loaded;
    }

    /* else must load it; iterate over available loaders */
    LuaTable tbl = PACKAGE.get(_LOADERS).checktable();
    StringBuffer sb = new StringBuffer();
    LuaValue chunk = null;
    for ( int i=1; true; i++ ) {
      LuaValue loader = tbl.get(i);
      if ( loader.isnil() ) {
        error( "module '"+name+"' not found: "+name+sb );       
        }
           
        /* call loader with module name as argument */
      chunk = loader.call(name);
      if ( chunk.isfunction() )
        break;
      if ( chunk.isstring() )
        sb.append( chunk.tojstring() );
    }

    // load the module using the loader
    LOADED.set(name, _SENTINEL);
    LuaValue result = chunk.call(name);
    if ( ! result.isnil() )
      LOADED.set( name, result );
    else if ( (result = LOADED.get(name)) == _SENTINEL )
      LOADED.set( name, result = LuaValue.TRUE );
    return result;
  }
View Full Code Here

    return varargsOf(NIL, valueOf("dynamic libraries not enabled"), valueOf("absent"));
  }

  LuaValue loader_preload( Varargs args ) {
    LuaString name = args.checkstring(1);
    LuaValue preload = PACKAGE.get(_PRELOAD).checktable();
    LuaValue val = preload.get(name);
    return val.isnil()?
      valueOf("\n\tno field package.preload['"+name+"']"):
      val;
  }
View Full Code Here

    String name = args.checkjstring(1);
    InputStream is = null;
   
   
    // get package path
    LuaValue pp = PACKAGE.get(_PATH);
    if ( ! pp.isstring() )
      return valueOf("package.path is not a string");
    String path = pp.tojstring();
   
    // check the path elements
    int e = -1;
    int n = path.length();
    StringBuffer sb = null;
View Full Code Here

 
  LuaValue loader_Java( Varargs args ) {
    String name = args.checkjstring(1);
    String classname = toClassname( name );
    Class c = null;
    LuaValue v = null;
    try {
      c = Class.forName(classname);
      v = (LuaValue) c.newInstance();
      v.setfenv(env);
      return v;
    } catch ( ClassNotFoundException  cnfe ) {
      return valueOf("\n\tno class '"+classname+"'" );
    } catch ( Exception e ) {
      return valueOf("\n\tjava load failed on '"+classname+"', "+e );
View Full Code Here

      switch ( opcode ) {
      case OP_REQUIRE:
        return lib.require(arg);
      case OP_SEEALL: {
        LuaTable t = arg.checktable();
        LuaValue m = t.getmetatable();
        if ( m == null )
          t.setmetatable(m=tableOf());
        m.set( INDEX, LuaThread.getGlobals() );
        return NONE;
      }
      }
      return NIL;
    }
View Full Code Here

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream( outputStream );
    try {
      org.luaj.vm2.LuaTable _G = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
      LuaThread.getRunning().setfenv(_G);
      LuaValue loader = new org.luaj.vm2.lib.OneArgFunction(_G) {
        public LuaValue call(LuaValue arg) {
          String name = arg.tojstring();
          String file = name + ".lua";
          InputStream is = open( file );
          if ( is == null )
            return LuaValue.valueOf("not found: "+file);
          try {
            return org.luaj.vm2.LoadState.load(is, "@stdin", env);
          } catch (IOException e) {
            return LuaValue.valueOf(e.toString());
          } finally {
            try { is.close(); } catch ( IOException ioe ) {}
          }
        }
      };
      LuaTable loaders = _G.get("package").get("loaders").checktable();
      loaders.insert(1, loader);
      org.luaj.vm2.lib.BaseLib.instance.STDOUT = printStream;
      loader.call(LuaValue.valueOf(test)).invoke(LuaValue.valueOf(test));
    } finally {
      printStream.close();
    }
    return outputStream.toString();
  }
View Full Code Here

   * function must be a Lua function without upvalues.
   * 
   * TODO: port dumping code as optional add-on
   */
  static LuaValue dump( LuaValue arg ) {
    LuaValue f = arg.checkfunction();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      DumpState.dump( ((LuaClosure)f).p, baos, true );
      return LuaString.valueOf(baos.toByteArray());
    } catch (IOException e) {
View Full Code Here

   */
  static Varargs gsub( Varargs args ) {
    LuaString src = args.checkstring( 1 );
    final int srclen = src.length();
    LuaString p = args.checkstring( 2 );
    LuaValue repl = args.arg( 3 );
    int max_s = args.optint( 4, srclen + 1 );
    final boolean anchor = p.length() > 0 && p.charAt( 0 ) == '^';
   
    Buffer lbuf = new Buffer( srclen );
    MatchState ms = new MatchState( args, src, p );
View Full Code Here

      try {
        return CoerceJavaToLua.coerce(f.get(m_instance));
      } catch (Exception e) {
        throw new LuaError(e);
      }
    LuaValue m = jclass.getMethod(key);
    if ( m != null )
      return m;
    return super.get(key);
  }
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.