Building a closure with format.abc

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Building a closure with format.abc

alstrup
Hi,

How do I reference a function to build an activation record and construct a closure object?

I.e. code like this:

ctx.ops([
ONewBlock,
ODup,
OScope,
OReg(0),
OSetProp(ctx.property("x")),
OFunction( /* WHAT GOES HERE? */ )
]);

Regards,
Asger
-- 
Asger Ottar Alstrup, SVP - Research & Development
Area9, Nyhavn 63d, 3, DK-1052 København K
Phone: <a href="tel:%2B45%2033%2011%2000%2090" value="+4533110090" target="_blank">+45 33 11 00 90, cell (DK): <a href="tel:%2B45%2026%2016%2079%2003" value="+4526167903" target="_blank">+45 26 16 79 03, cell (US): <a href="tel:630%20995%209277" value="+16309959277" target="_blank">630 995 9277


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

Jan_Flanders


On Thu, Aug 18, 2011 at 4:59 PM, Asger Ottar Alstrup <[hidden email]> wrote:
Hi,

How do I reference a function to build an activation record and construct a closure object?

I.e. code like this:

ctx.ops([
ONewBlock,
ODup,
OScope,
OReg(0),
OSetProp(ctx.property("x")),
OFunction( /* WHAT GOES HERE? */ )
]);

Regards,
Asger
-- 
Asger Ottar Alstrup, SVP - Research & Development
Area9, Nyhavn 63d, 3, DK-1052 København K
Phone: <a href="tel:%2B45%2033%2011%2000%2090" value="+4533110090" target="_blank">+45 33 11 00 90, cell (DK): <a href="tel:%2B45%2026%2016%2079%2003" value="+4526167903" target="_blank">+45 26 16 79 03, cell (US): <a href="tel:630%20995%209277" value="+16309959277" target="_blank">630 995 9277


--
haXe - an open source web programming language
http://haxe.org

Hi Asger,

hxswfml has a function (abc2hxm) that converts actionscript bytecode (inside a swf) to 'format.abc hx-source files'.
You can output the converted abc to one big .hx file or output each found class to a separate .hx class file.

Some remarks:
1)
It's needs a small fix since the haxe compiler changed recently and the document class name of each haxe-swf is no longer flash.Boot but a random name.
For now you'll have to change it manually in the output where the swf is generated.

2)
It's not optimized since it uses a function call for every op:
ctx.op(ONewBlock);
ctx.op(ODup);
instead of:
ctx.ops([ONewBlock,ODup]);
The reason for this is that at some points I need to call a jump function between ops, to calculate a bytes offset.(I have to find a better way yet)

3)
Since the output is hx source code it needs to be compiled by the haxe compiler and then run to let it generate a swf.
I added a 'generate swf' function which gets executed automatically when you compile with -x instead of -neko, so it's a 'compile-run-generate operation'
You could also compile to swf and play the swf and save the swf it generates with FileReference.save()

4)
It uses a slightly modified version of format.abc, so the generated output will not compile with the current version in haxelib. (you need the one include with hxswfml)

5)
Because it can output huge files which can be difficult to navigate through I added braces {} so you can use fold/collapse in your editor. (they serve no other purpose in this case)


So an easy way to write format.abc is writing it in plain haxe, compile it and then convert to format.abc:

build.hxml:

#compile original haxe source to swf
-main Main
-swf9 Main.swf
-debug

#convert to abc (3 options):

#1) convert abc inside swf to a zip containing one big .hx file + build.hxml file
-cmd hxswfml abc2hxm Main.swf Main1.zip -main flash.Boot -source

#2) convert abc inside swf to a zip containing .hx files in separate folders and files (packages/classes)
-cmd hxswfml abc2hxm Main.swf Main2.zip -main flash.Boot  -source -folders

#3) convert abc inside swf to xml
-cmd hxswfml abc2xml Main.swf Main.xml -source

# '-source' will weave the original .hx source lines through the bytecode as comments (to make it easier to follow/understand the bytecode)


There are some examples in: http://code.google.com/p/hxswfml/downloads/detail?name=examples.zip
You can find it in folder : examples\0Commandline\abc2hxm

hxswfml : http://code.google.com/p/hxswfml/

An example of what it outputs: Void_abc.hx: http://pastebin.com/B5J9hams


Good luck. :)

Jan


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

bubblebenj
*  Documentation proselytism ON *

Don't forget to complete the documentation http://haxe.org/com/libs/format/abc.
if you find the information useful.

*  Documentation proselytism OFF *



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

alstrup
The documentation is outdated, and does not cover the questions I ask.

In fact, it turns out that format.abc in haxelib does NOT support what I am asking, because the ctx.curFunction variable required to extract the id of a function is private.

So to answer my own question, the solution is this:

    ctx.op(OFunction(untyped ctx.curFunction.f.type));

Regards,
Asger


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

bubblebenj
My point was :
If the information is relevant, (and even more if the doc in outdated) update the doc (or at least link the thread).
Thus it's going to be useful again.

Maybe I'm wrong and it's totally off-topic regarding the original doc.

Ben

On Fri, Aug 19, 2011 at 1:48 PM, Asger Ottar Alstrup <[hidden email]> wrote:
The documentation is outdated, and does not cover the questions I ask.

In fact, it turns out that format.abc in haxelib does NOT support what I am asking, because the ctx.curFunction variable required to extract the id of a function is private.

So to answer my own question, the solution is this:

    ctx.op(OFunction(untyped ctx.curFunction.f.type));

Regards,
Asger


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

Jan_Flanders


On Fri, Aug 19, 2011 at 3:16 PM, benjamin Dubois <[hidden email]> wrote:
My point was :
If the information is relevant, (and even more if the doc in outdated) update the doc

I've updated the wiki for this fix: http://code.google.com/p/hxformat/source/detail?r=107

Jan


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

alstrup
Hi,

I've tried to switch to swfmlhx, because I can not get the "newBlock" flag set on a function using format.abc for some reason. It does not seem to work with the stock format.abc.

However, when I try with swfmlhx, I fail to make a SWF file completely:

neko test.n 
Could not produce SWF file: Invalid field access : index

Called from format/abc/Writer.hx line 79
Called from format/abc/Writer.hx line 289
Called from format/abc/Writer.hx line 70
Called from format/abc/Writer.hx line 329
Called from format/abc/Writer.hx line 341
Called from SwfWriter.hx line 90

My code basically does 

  var ctx = new format.abc.Context();
  ctx.beginClass("Swf");
  ctx.beginMethod("test", ...);
  ... produce code
  ctx.endMethod();
  ctx.endClass();

  var abcOutput = new haxe.io.BytesOutput();
  format.abc.Writer.write(abcOutput, ctx.getData());

and the last line is where it crashes.

Any ideas?

Thanks,
Asger


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

Jan_Flanders


On Sun, Aug 21, 2011 at 4:48 PM, Asger Ottar Alstrup <[hidden email]> wrote:
Hi,

I've tried to switch to swfmlhx, because I can not get the "newBlock" flag set on a function using format.abc for some reason. It does not seem to work with the stock format.abc.

However, when I try with swfmlhx, I fail to make a SWF file completely:

 
Works fine when I test it: http://www.haxer.be/zips/abctest/abcTest.zip

Used script and results:

Main.as: http://www.haxer.be/zips/abctest/Main.as
Main.hx: http://www.haxer.be/zips/abctest/GenSWF.hx

Main.swf: http://www.haxer.be/zips/abctest/Main.swf
Main_new.swf : http://www.haxer.be/zips/abctest/Main_new.swf

Jan


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Building a closure with format.abc

alstrup
Works fine when I test it: http://www.haxer.be/zips/abctest/abcTest.zip

This is an example where you reverse engineer an existing SWF and then reproduce it.

I am creating a SWF file from scratch.

Here is an example that crashes when run:

import format.abc.Data;
import format.swf.Data;

class Test {
public static function main() {
var ctx = new format.abc.Context();
var classDef = ctx.beginClass("Swf");
ctx.addClassSuper('Object');
var m = ctx.beginMethod("_init_globals", [], ctx.type("void"), false, false, true);
m.maxStack = 40;
m.initScope = 1;
m.maxScope = 2;
ctx.op(OpCode.OThis);
ctx.op(OpCode.OScope);
ctx.op( ORetVoid );

ctx.endClass();
var abcOutput = new haxe.io.BytesOutput();
format.abc.Writer.write(abcOutput, ctx.getData());
var swfFile = 
{
header: {version:10, compressed:false, width:800, height:600, fps:30, nframes:1},
tags:
[
TSandBox({useDirectBlit :false, useGPU:false, hasMetaData:false, actionscript3:true, useNetWork:false}),
TActionScript3(abcOutput.getBytes(), { id : 1, label : "Swf" } ),
TSymbolClass([{cid:0, className:"Swf"}]),
TShowFrame
]
};
var swfOutput:haxe.io.BytesOutput = new haxe.io.BytesOutput();
var writer = new format.swf.Writer(swfOutput);
writer.write(swfFile);
var bytes = swfOutput.getBytes();
}
}

Regards,
Asger

--
haXe - an open source web programming language
http://haxe.org