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

org.luaj.vm2.compiler.DumpState Maven / Gradle / Ivy

The 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.compiler;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LocVars;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaValue;


/** Class to dump a {@link Prototype} into an output stream, as part of compiling.
 * 

* Generally, this class is not used directly, but rather indirectly via a command * line interface tool such as {@link luac}. *

* A lua binary file is created via {@link DumpState#dump}: *

 {@code
 * Globals globals = JsePlatform.standardGlobals();
 * Prototype p = globals.compilePrototype(new StringReader("print('hello, world')"), "main.lua");
 * ByteArrayOutputStream o = new ByteArrayOutputStream();
 * DumpState.dump(p, o, false);
 * byte[] lua_binary_file_bytes = o.toByteArray();
 * } 
* * The {@link LoadState} may be used directly to undump these bytes: *
 {@code
 * Prototypep = LoadState.instance.undump(new ByteArrayInputStream(lua_binary_file_bytes), "main.lua");
 * LuaClosure c = new LuaClosure(p, globals);
 * c.call();
 * } 
* * * More commonly, the {@link Globals#undumper} may be used to undump them: *
 {@code
 * Prototype p = globals.loadPrototype(new ByteArrayInputStream(lua_binary_file_bytes), "main.lua", "b");
 * LuaClosure c = new LuaClosure(p, globals);
 * c.call();
 * } 
* * @see luac * @see LoadState * @see Globals * @see Prototype */ public class DumpState { /** set true to allow integer compilation */ public static boolean ALLOW_INTEGER_CASTING = false; /** format corresponding to non-number-patched lua, all numbers are floats or doubles */ public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLES = 0; /** format corresponding to non-number-patched lua, all numbers are ints */ public static final int NUMBER_FORMAT_INTS_ONLY = 1; /** format corresponding to number-patched lua, all numbers are 32-bit (4 byte) ints */ public static final int NUMBER_FORMAT_NUM_PATCH_INT32 = 4; /** default number format */ public static final int NUMBER_FORMAT_DEFAULT = NUMBER_FORMAT_FLOATS_OR_DOUBLES; // header fields private boolean IS_LITTLE_ENDIAN = false; private int NUMBER_FORMAT = NUMBER_FORMAT_DEFAULT; private int SIZEOF_LUA_NUMBER = 8; private static final int SIZEOF_INT = 4; private static final int SIZEOF_SIZET = 4; private static final int SIZEOF_INSTRUCTION = 4; DataOutputStream writer; boolean strip; int status; public DumpState(OutputStream w, boolean strip) { this.writer = new DataOutputStream( w ); this.strip = strip; this.status = 0; } void dumpBlock(final byte[] b, int size) throws IOException { writer.write(b, 0, size); } void dumpChar(int b) throws IOException { writer.write( b ); } void dumpInt(int x) throws IOException { if ( IS_LITTLE_ENDIAN ) { writer.writeByte(x&0xff); writer.writeByte((x>>8)&0xff); writer.writeByte((x>>16)&0xff); writer.writeByte((x>>24)&0xff); } else { writer.writeInt(x); } } void dumpString(LuaString s) throws IOException { final int len = s.len().toint(); dumpInt( len+1 ); s.write( writer, 0, len ); writer.write( 0 ); } void dumpDouble(double d) throws IOException { long l = Double.doubleToLongBits(d); if ( IS_LITTLE_ENDIAN ) { dumpInt( (int) l ); dumpInt( (int) (l>>32) ); } else { writer.writeLong(l); } } void dumpCode( final Prototype f ) throws IOException { final int[] code = f.code; int n = code.length; dumpInt( n ); for ( int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy