Skip to content

Instantly share code, notes, and snippets.

@bradparks
Forked from tong/NekoBoot.hx
Created March 4, 2013 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradparks/5083427 to your computer and use it in GitHub Desktop.
Save bradparks/5083427 to your computer and use it in GitHub Desktop.
import sys.FileSystem;
import sys.io.File;
/**
Haxe port of nekoboot.neko for creating executables from neko bytecode.
Original: http://code.google.com/p/nekovm/source/browse/trunk/src/tools/nekoboot.neko
Usage : nekoboot <file.n>
*/
class NekoBoot {
static function exit( m : String ) {
Sys.println( m );
Sys.exit( 0 );
}
static function main() {
var system = Sys.systemName();
var args = Sys.args();
var exe_ext = ( system == 'Windows' ) ? '.exe' : null;
var boot_exe = 'neko';
if( exe_ext != null )
boot_exe += exe_ext;
if( args[0] == "-b" ) {
boot_exe = args[1];
args.shift();
}
if( args.length != 1 )
exit( 'Need bytecode argument' );
var file = args[0];
var bytecode = File.getBytes( file );
// --- find bootable, load it
var boot : haxe.io.Bytes = null;
for( p in neko.vm.Loader.local().getPath() ) {
var path = p + boot_exe;
if( !FileSystem.exists( path ) )
continue;
boot = File.getBytes( path );
if( boot.get( 0 ) == 35 ) // '#' It's a script
boot = null;
}
if( boot == null )
exit( 'The bootable executable file was not found' );
var boot_size = boot.length;
var dot_pos = file.indexOf(".");
if( dot_pos != -1 )
file = file.substr( 0, dot_pos );
// --- create executable file :
// --- --- content of neko(.exe)
// --- --- + neko bytecode followed by 'NEKO' and the original exe size
var out_name = file;
var fo = File.write( out_name );
fo.write( boot );
fo.write( bytecode );
fo.writeString( 'NEKO' );
fo.writeByte( boot_size & 0xFF );
fo.writeByte( ( boot_size >> 8 ) & 0xFF );
fo.writeByte( ( boot_size >> 16 ) & 0xFF );
fo.writeByte( ( boot_size >> 24 ) & 0xFF );
fo.close();
// --- set execution rights
if( system != "Windows" )
Sys.command( "chmod", ["755",out_name] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment