Skip to content

Instantly share code, notes, and snippets.

@tong
Last active January 27, 2020 11:49
Show Gist options
  • Save tong/5061441 to your computer and use it in GitHub Desktop.
Save tong/5061441 to your computer and use it in GitHub Desktop.
Haxe port of nekoboot.neko for creating executables from bytecode. Original: http://code.google.com/p/nekovm/source/browse/trunk/src/tools/nekoboot.neko Usage : nekoboot <file.n>
import sys.FileSystem;
import sys.io.File;
/**
Haxe port of nekoboot.neko for creating executables from neko bytecode.
Original version: https://github.com/HaxeFoundation/neko/blob/master/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] );
}
}
@tong
Copy link
Author

tong commented May 14, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment