Skip to content

Instantly share code, notes, and snippets.

@jcward
Created April 14, 2017 15:49
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 jcward/26895ebbe7ffcc0e8d4d0659e5d133df to your computer and use it in GitHub Desktop.
Save jcward/26895ebbe7ffcc0e8d4d0659e5d133df to your computer and use it in GitHub Desktop.
Generating AS3 from Haxe, and post-processing it to work in an AS3 project
#!/usr/bin/ruby
require "open3"
require 'json'
require 'pathname'
require 'yaml'
# no trace TextField on the stage, please
defines = "-D native_trace"
# Haxe build, e.g.
cmd = "haxe #{defines} -swf-version 11 -cp #{ class_paths.join(' -cp ') } -as3 generated_as3 #{ classes.join(' ') }"
puts "Compiling..."
puts cmd
Open3.popen2e(cmd) do |stdin, stdout_and_err, wait_thr|
pid = wait_thr.pid
exit_status = wait_thr.value # Process::Status object returned
puts " - - - - - "
puts stdout_and_err.read
puts " - - - - - "
unless wait_thr.value.success?
puts "Compile failed!"
exit 1
end
end
puts "Post processing AS3..."
# - - Post process: __main__.as --> HxLangPatches.as - -
`mv generated_as3/__main__.as generated_as3/HxLangPatches.as`
`sed -ri 's,__main__,HxLangPatches,' generated_as3/HxLangPatches.as`
`sed -ri 's,extends flash,{ //extends flash,' generated_as3/HxLangPatches.as`
`sed -ri 's,super\\(\\),//super(),' generated_as3/HxLangPatches.as`
`sed -ri 's,flash.Lib.current = this,// no flash.Lib.current,' generated_as3/HxLangPatches.as`
`sed -ri 's,public function HxLangPatches,public static function patch,' generated_as3/HxLangPatches.as`
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - Post process: typing errors in Boot.as - - - - - - - - - - - - - - - -
`sed -ri 's,arr : Array = this,arr:Array = this as Array,' generated_as3/flash/Boot.as`
`sed -ri 's,s : String = this,s:String = this as String,' generated_as3/flash/Boot.as`
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - Post process: missing haxe package in Lib.as - - - - - - - - - - - -
`sed -ri 's,if\\(flash.external.*haxe.Log,//,' generated_as3/flash/Lib.as` if File.exist?("generated_as3/flash/Lib.as")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Post process haxe.Log.trace -> native trace
`find generated_as3 -type f -print0 | xargs -0 sed -ri 's,\\(haxe.Log._trace\\),hxputs,'`
# generated_as3/hscript/HScript.as:69
# Error: Ambiguous reference to Error
# -- just throw strings
`sed -ri 's,throw new Error,throw ,' generated_as3/hscript/HScript.as` if File.exist?("generated_as3/hscript/HScript.as")
# Now in your AS3 build, add a -source-path+=../path/to/generated_as3
# And in your Main.as file, call HxLangPatches.patch() fairly early in the boot process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment