Aug 29, 2008

Introduction to redtamarin




first small release red v0.1.0.102
http://code.google.com/p/redtamarin/

download here
http://code.google.com/p/redtamarin/downloads/list

text of the video
http://docs.google.com/Doc?id=dcfgjs6p_215gbt7vwd8

Setup in Flex Builder 3
http://code.google.com/p/redtamarin/wiki/Main#Testing
http://code.google.com/p/redtamarin/wiki/Main#IDE

on top of that for your own executable you can use
http://code.google.com/p/maashaack/source/browse/#svn/trunk/AS3/src/system/cli

to parse your command line :)

see a basic example here
http://code.google.com/p/maashaack/source/browse/trunk/AS3/tests/framework/system/cli/SimpleArgumentsParser.as


read more ...

Sep 5, 2007

MMgc - Memory Manager and Garbage Collector

When Macromedia was working on the Flash player they implemented a memory manager, that we can be find now used also for garbage collection in the Tamarin project.

This GC is named MMgc, because it's also a memory manager, and in fact the project can substain by itself, Tamarin use it, but you could implement it for your own project.

You got it fully explained on the Mozilla dev wiki.

I didn't really looked into it to start delving into Tamarin (and this was a rookie error :p), but if you are really serious to understand how Tamarin internals are working, reading the doc on the wiki while reading the source code can really get you going, yes it is a must read.

An other interesting read is the description of thread safety in SpiderMonkey, even more interesting if you plan to get involved in ActionMonkey, as this project need at one moment or another to combine the two (MMgc and Thread Safety).

I must say that sometimes people criticize open source project for their lack of documentation, but when I read such great documentation we can surely not say that for those Mozilla projects :), even if you're a newbie (as me) you can learn a lot just by reading them.


read more ...

test MXR instance for Tamarin

Thanks to Benjamin Smedberg and a Mozilla admin, we can use the MXR tools on the Tamarin sources. the original post, and the MXR link.

note also that if you keep track of Tamarin, seeing the tips here, and the bugs list, that you can see a real nice increase of activity, so be prepared for a Tamarin new release some time in the futur :).


read more ...

May 20, 2007

get Tamarin using hg Mercurial

Few days ago Edwin Smith (Tamarin creator) announced on mozilla.dev.tech.js-engine that the Tamarin repository has moved to Mercurial.

Hi Folks,

As you may have been following, Mozilla is moving to Mercurial.
Tamarin has now moved over, the new repository is at
http://hg.mozilla.org/tamarin-central. Tamarin has been removed from
CVS (although it's history still lives on). The Tamarin project page
will soon reflect the change.

Happy updating, smooth sailing, and all that!

Edwin Smith
a Tamarin Guy
If you want to know why mozilla moved to Mercurial check out this post from preed (I love the pix :p).


Now let's install Mercurial on Windows, but before that just a little note, yes all this is a dumbed down tutorial to get the source code from Tamarin, not that I think dumb people read this blog but even if I love open source, mozilla, tamarin etc. I always think about the newbie, the student or other people feeling overwhelmed by a high barrier to entry to use something.

Sure, when you breath Mercurial, CVS, SVN everyday (heck even my blogger template is under SVN :p), you don't even think, you get the tool, you don't need tutorial, you just do it and boom it's done, well sorry to say that blokes but the "boom it's done" ain't gonna help any newbie wanting to have fun with Tamarin, so here you go an easy howto is following.

First thing first, get the Mercurial installer, either follow their home page or go directly here, and then install.
(don't mind the strange name as "Mercurial 0.9.3-10edaed7f909" for ex, just get the last one).

During the install if you get this error
C:\Windows\system32\msvcr71.dll
An error occured while trying to replace the existing file
...
ignore it and continue

basically, msvccr71 is the Microsoft C Runtime Library, and if you installed VSC++ express following this post you already have it.
(imho the Mercurial installer should not try to overwrite it)

now open a prompt and see if mercurial is correctly installed
(if you don't already have a "DOS prompt here" check this)

type
c:\>hg
and something should happen.
If not, then got to the directory where you installed Mercurial and use add_path (nice app from Marco Barisione)
c:\path\to\Mercurial>add_path c:\path\to\Mercurial
test again, for exemple here
D:\sources\opensource\Tamarin\tamarin-src>hg
and you should obtain
Mercurial Distributed SCM

basic commands:

 add        add the specified files on the next commit
 annotate   show changeset information per file line
 clone      make a copy of an existing repository
 commit     commit the specified files or all outstanding changes
 diff       diff repository (or selected files)
 export     dump the header and diffs for one or more changesets
 init       create a new repository in the given directory
 log        show revision history of entire repository or files
 merge      merge working directory with another revision
 parents    show the parents of the working dir or revision
 pull       pull changes from the specified source
 push       push changes to the specified destination
 remove     remove the specified files on the next commit
 revert     revert files or dirs to their states as of some revision
 serve      export the repository via HTTP
 status     show changed files in the working directory
 update     update working directory

use "hg help" for the full list of commands or "hg -v" for details
Now that hg is fully installed, let's checkout the source code of tamarin, the repository is at this new URL http://hg.mozilla.org/tamarin-central/.
When you access it using a browser you got a simple yet very usefull interface (love the RSS feed :)), but this same URL is what we gonna use to fetch the code with hg.

It's even simpler than CVS, just type this
C:\>hg clone http://hg.mozilla.org/tamarin-central/ tamarin
here this command will do a checkout of the code into the "c:\tamarin\" directory, but depending on where you executed the command it could be anyother path and/or directory, the result will look like this
requesting all changes
adding changesets
adding manifests
adding file changes
added 86 changesets with 7586 changes to 3189 files
3165 files updated, 0 files merged, 0 files removed, 0 files unresolved
later if you want to update those sources, you will just need to go back to that same path and execute that command
C:\tamarin>hg pull
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
that's all folks.


read more ...

May 18, 2007

Tamarin gotchas

If you have used the Flex SDK and want to use Tamarin, you will soon realize that there are some bits of differences in the way you can import libraries (*.abc files) and compile your custom code under Tamarin.

When you're compiling avmplus, by default the resulting binary will contain the core builtins as Object, String, Array, etc. what you may call the global library, and will also contain avmplus dedicated classes as avmplus.System, avmplus.File, etc. what you may call the shell library.

Now let create our first little program, let's say we are tired to use asc.jar and we would like to have a nice asc.exe.

asc.as


package
    {
    
    import avmplus.System;
    
    var cmdline = "java -jar asc.jar";
    
    if( System.argv.length > 0 )
        {
        cmdline += " " + System.argv.join( " " );
        }
    
    System.exec( cmdline );
    
    }

let's compile it
c:\>java -jar asc.jar asc.as
asc.abc, 205 bytes written
let's run it
c:\>avmplus asc.abc
ActionScript 3.0 for AVM+
version 1.0 build d628
Copyright (c) 2003-2004 Macromedia, Inc.
Copyright (c) 1998-2003 Mountain View Compiler Company
All rights reserved
...
cool it seems to work, now let's make that exe.
c:\>java -jar asc.jar -exe avmplus.exe asc.as
asc.abc, 205 bytes written
asc.exe, 467157 bytes written
and run it
c:\>asc
ActionScript 3.0 for AVM+
version 1.0 build d628
Copyright (c) 2003-2004 Macromedia, Inc.
Copyright (c) 1998-2003 Mountain View Compiler Company
All rights reserved
...
great it's working.


But what if we would like to compile asc.as in strict mode ?
c:\>java -jar asc.jar -strict asc.as
[Compiler] Error #1172: Definition avmplus:System could not be found.
   asc.as, Ln 5, Col 20:
       import avmplus.System;
   ...................^

[Compiler] Error #1120: Access of undefined property System.
   asc.as, Ln 9, Col 9:
       if( System.argv.length > 0 )
   ........^

[Compiler] Error #1120: Access of undefined property System.
   asc.as, Ln 11, Col 26:
           cmdline += " " + System.argv.join( " " );
   .........................^

[Compiler] Error #1120: Access of undefined property System.
   asc.as, Ln 14, Col 5:
       System.exec( cmdline );
   ....^

4 errors found
Ok there are errors but this is still good news, now we know that we can compile a source code that use imports without using strict mode and the bytecode keep the reference to the import and so if the import is correct in the end the compiled program will work.
The other good news is if we use strict mode the compiler tell us about that missing import and give us a pretty detailled list of where the errors are.

So here the first gotcha, even if the avmplus.exe contain the bytecode for the class avmplus.System, if you want to compile in strict mode and import that class you will need to compile the shell library and use it as an import when compiling your source code.

The shell library are all the *.as files than can be found in \js\tamarin\shell\, to compile them in one library just define your own shell.as that will include those files.

shell.as

include "shell\\ByteArray.as"
include "shell\\Domain.as"

include "shell\\Endian.as"

include "shell\\StringBuilder.as"

include "shell\\UIntArray.as"
include "shell\\UShortArray.as"
include "shell\\ShortArray.as"
include "shell\\IntArray.as"
include "shell\\FloatArray.as"
include "shell\\DoubleArray.as"

include "shell\\Java.as"

include "shell\\Dictionary.as"

include "shell\\toplevel.as"

You can do it anyway you want, me to keep it simple, I copied all the \js\tamarin\shell\*.as files to \sandbox\shell\*.as and I compile the lib from \sandbox\shell.as.
And you compile it like that
c:\>java -jar asc.jar shell.as
But here again you will have another gotcha, it does not compile (yep even in non-strict mode), you will obtain these errors
[Compiler] Error #1017: The definition of base class Object was not found.
   D:\sources\opensource\Tamarin\sandbox\shell\ByteArray.as, Ln 1, Col 1:
   /* ***** BEGIN LICENSE BLOCK *****
   ^
...
Here the compiler want the base class Object, and this one you can only have it by compiling the global library.

To compile the global library, you take all the files from \js\tamarin\core\*.as and you copy them to \sandbox\core\*.as, and you compile it from \sandbox\global.as.

shell.as

include "core\\builtin.as"

include "core\\ErrorConstants.as"
include "core\\Error.as"

include "core\\Math.as"

include "core\\Date.as"
include "core\\RegExp.as"
include "core\\XML.as"

Take into account that builtin.as already include those files
include "Object.as"
include "Class.as"
include "Function.as"
include "Namespace.as"
include "Boolean.as"
include "Number.as"
include "String.as"
include "Array.as"
include "actionscript.lang.as"
Don't change the order of the includes, it's important (and logic, Object should always be the first class defined for ex).

Ok, let's compile global.as
c:\>java -jar asc.jar global.as
global.abc, 31729 bytes written
now we can compile shell.as
c:\>java -jar asc.jar -import global.abc shell.as
shell.abc, 3864 bytes written
and finally we can compile our asc.as in strict mode
c:\>java -jar asc.jar -import shell.abc -strict asc.as
[Compiler] Error #1046: Type was not found or was not a compile-time constant:
 String.

[Compiler] Error #1046: Type was not found or was not a compile-time constant:
 String.

[Compiler] Error #1046: Type was not found or was not a compile-time constant:
 uint.

[Compiler] Error #1046: Type was not found or was not a compile-time constant:
 int.
...
ahem... stuck again. Let's explain why.
You need the shell.abc library to be able to reference avmplus.System, but as soon as you use this library you will also need the global.abc library that define the base classes as String, int, uint, etc.

When earlier we compiled shell.abc importing global.abc, the final shell.abc file contained only the code defined in shell.as, not the codes defined in global.as, so you see now our problem is to be able to use the 2 libraries at the same time, and yes it is a problem because asc.jar only allow you to import 1 library, not 2.

So in fact what we need is a tamarin library that contain the bytecodes of shell.as and global.as.

Tamarin.as

include "global.as"

include "shell.as"


c:\>java -jar asc.jar Tamarin.as
Tamarin.abc, 35787 bytes written
And now we can compile asc.as in strict mode
c:\>java -jar asc.jar -import Tamarin.abc -strict asc.as
asc.abc, 200 bytes written
but as ultimately we want an exe
c:\>java -jar asc.jar -exe avmplus.exe -import Tamarin.abc -strict asc.as
asc.abc, 200 bytes written
asc.exe, 467152 bytes written
Now we are ready to write full blown command line tools in ES4 :), but that will be in another post.

ps: asc.exe wil work fine as long as you keep asc.jar in the same directory


read more ...

ECMAScript 4 specification

For some time you could access the ES4 specification, but as this was an export of a wiki some links were not working.

Especially the links for doc and pdf files in the resources.

I don't know when this was released (maybe it's an old news), but now you can download the ECMAScript 4 specification as PDF, this is the spec from November 1 2006.
If the link for some reason go dead, note also that I keep a copy of the file here.


read more ...

Apr 29, 2007

avmplus 101

Now that we have an avmplus executable, let's see how we can use it.

First let's define what is the avmplus,
the name stand for ActionScript Virtual Machine +,
so basically it's a virtual machine that can execute actionscript bytecode.

That's it, you can not feed the avmplus source code directly, but only bytecode, and as a normal human sorry I do not write bytecode by hand, at most I can barely write readable source code.

So even if we got the avmplus we're missing one piece of the puzzle, the way to generate bytecode (*.abc file) from source code (*.as file).

Download the Flex SDK, and install it.

It's not open source yet, but it will be in few months, just to say that we gonna use it (or at least a part of it) in an unconventionnal way but not that much to bring us the thunder of Adobe ;).

What interest us in that Flex SDK is a library named asc.jar, yep it is the shortname for actionscript compiler, and this little gem can generate *.abc files from *.as sources.

In your Flex SDK installation dir you should find asc.jar, copy and paste it in a working directory, I named mine tamarin/sandbox :p.

Let's run it using Java.
c:\>java -jar asc.jar
And you will obtain the default options for the actionscript compiler
--------------------------------------------------------------
ActionScript 3.0 for AVM+
version 1.0 build d628
Copyright (c) 2003-2004 Macromedia, Inc.
Copyright (c) 1998-2003 Mountain View Compiler Company
All rights reserved

Usage:
  asc {-AS3|-ES|-d|-f|-h|-i|-import |-in |-m|-p}* filespec
  -AS3 = use the AS3 class based object model for greater performance and better error reporting
  -ES = use the ECMAScript edition 3 prototype based object model to allow dynamic overriding of prototype properties
  -d = emit debug info into the bytecode
  -f = print the flow graph to standard out
  -h = print this message
  -i = write intermediate code to the .il file
  -import  = make the packages in the
       specified file available for import
  -in  = include the specified filename
       (multiple -in arguments allowed)
  -m = write the avm+ assembly code to the .il file
  -p = write parse tree to the .p file
  -md = emit metadata information into the bytecode
  -warnings = warn on common actionscript mistakes
  -strict = treat undeclared variable and method access as errors
  -sanity = system-independent error/warning output -- appropriate for sanity testing
  -log = redirect all error output to a logfile
  -exe  = emit an EXE file (projector)
  -swf classname,width,height[,fps] = emit a SWF file
  -language = set the language for output strings {EN|FR|DE|IT|ES|JP|KR|CN|TW}
  -optimize = produced an optimized abc file
--------------------------------------------------------------
Now let's try to run our first example "hello world" using avmplus.

test.as

package
    {
    
    print( "hello world" );
    
    }


Let's see first what happen if we try to run an *.as file directly in avmplus
c:\>avmplus_sd test.as
VerifyError: Error #1042: Not an ABC file.  major_version=24944 minor_version=2573.
You see, avmplus need an *.abc file.

but as we can run asc to compile an *.as file to *.abc, we should be OK.

so first we compile an abc file
c:\>java -jar asc.jar test.as
test.abc, 87 bytes written
then we run it using avmplus
c:\>avmplus test.abc
hello world
Voila we run our "hello world" example with avmplus :).

But wait, there is more to it, if you look closely at the asc options you will see that little gem
-exe  = emit an EXE file (projector)
Yes, you can also use asc to compile an abc file and combine it with the avmplus to compile your own executable file.
c:\>java -jar asc.jar -exe avmplus.exe test.as
test.abc, 87 bytes written
test.exe, 467039 bytes written
and off course you can run that exe
c:\>test.exe
hello world
And if a 400KB executable bother you for a simple hello world, you can still use UPX (the Ultimate Packer for eXecutable) and that will size down that exe almost by half.
And if you're not convinced by a simple hello world, look here for an abc decompiler, that simply reuse abcdump.as.

You can not do much with avmplus for now, look at toplevel.as, you will see that there is only basic file IO and basic command line interaction (readline, writeline,etc.), but this will evolve.


read more ...

How to build Tamarin on Windows

The Tamarin project is an open source ECMAScript Virtual Machine, contributed by Adobe to the Mozilla fondation.
This VM is already running inside the Flash player 9, and is planed later to be integrated in SpiderMonkey (the engine running JavaScript in Mozilla, FireFox, etc.).
Here I gonna explain step by step how to compile it under Windows (win32).

01. Download a C++ compiler
I set my choice on Visual C++ 2005 Express Edition, first because if you're new to compiling on windows this is less hassle to set up, second it's free to use and third it can build Windows native executable and it's what we want here.

Download and Install vcsetup.exe (3MB), and don't forget to register (well yaknow that MS thing, don't worry it's free).

Well now you will have to install the Platform SDK (well I said "less hassle", not "no hassle"), you need the PSDK for thing like windows.h, but anyway as you're into compiling in C/C++ having the PSDK could be usefull later, whatch out it can be a big download (around 300MB and more).

But basically follow these steps:
Using Visual C++ 2005 Express Edition with the Microsoft Platform SDK.

02. Download Tamarin sources from the CVS repository
You gonna need CVS, simpler thing to do is to install TortoiseCVS, and then add the path to the TortoiseCVS install to your environment variables.

That way you will be able to use CVS from the command line, and use the following to get Tamarin sources.

cvs -d :pserver:anonymous:anonymous@cvs-mirror.mozilla.org:/cvsroot
 export -r HEAD mozilla/js/tamarin
To compile Tamarin you will also need the zlib module, that you can get also from CVS.
cvs -d :pserver:anonymous:anonymous@cvs-mirror.mozilla.org:/cvsroot
 export -r HEAD mozilla/modules/zlib
You should write these all in one line, you can download that zip file for some little batch files that will do that for you ;).


03. Compiling Tamarin with VC++ express
After the CVS checkout you should have the Tamarin directories all set up, now we need to open avmplus_8.vcproj.
Now, right click solution 'avmplus_8' and select build solution.

If all went OK, you should obtain this line at the end.
========== Build: 4 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
By default the project compile the debugger version of avmplus, that can be found here.
..mozilla\js\tamarin\platform\win32\obj_8\shell\Debug_Debugger\avmplus_sd.exe
Here the options for the avmplus (debugger).
--------------------------------------------------------------
avmplus shell 1.0 build d684

usage: avmplus
          [-d]          enter debugger on start
          [-Ddprofile]  dynamic instruction stats
          [-Dsprofile]  show static instruction stats
          [-Dgreedy]    collect before every allocation
          [-Dnogc]      don't collect
          [-Dgcstats]   generate statistics on gc
          [-Dnoincgc]   don't use incremental collection
          [-Dastrace N] display AS execution information, where N is [1..4]
          [-Dinterp]    do not generate machine code, interpret instead
          [-Dverbose]   trace every instruction (verbose!)
          [-Dbbgraph]   output MIR basic block graphs for use with Graphviz
          [-Dforcemir]  use MIR always, never interp
          [-Dnodce]     disable DCE optimization
          [-Dnocse]     disable CSE optimization
          [-Dnosse]     use FPU stack instead of SSE2 instructions
          [-Dverifyall] verify greedily instead of lazily
          [-Dtimeout]   enforce maximum 15 seconds execution
          [-error]      crash opens debug dialog, instead of dumping
          [-log]
          [-- args]     args passed to AS3 program
          [-jargs ... ;] args passed to Java runtime
          filename.abc ...
          [--] application args
--------------------------------------------------------------


04. Compile the Release version of Tamarin
Right click solution 'avmplus_8' and select configuration manager....

At the top left in active solution configuration, select Release, close the window, clear the output (right click clear all), and compile again.

Again if all goes well you should obtain this line at the end.
========== Build: 4 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
The compiled exe can be found here
..mozilla\js\tamarin\platform\win32\obj_8\shell\Release\avmplus.exe
Here the option for the avmplus (release).
--------------------------------------------------------------
avmplus shell 1.0 build d684

usage: avmplus
          [-Dinterp]    do not generate machine code, interpret instead
          [-Dforcemir]  use MIR always, never interp
          [-Dnodce]     disable DCE optimization
          [-Dnocse]     disable CSE optimization
          [-Dnosse]     use FPU stack instead of SSE2 instructions
          [-Dtimeout]   enforce maximum 15 seconds execution
          [-error]      crash opens debug dialog, instead of dumping
          [-log]
          [-- args]     args passed to AS3 program
          [-jargs ... ;] args passed to Java runtime
          filename.abc ...
          [--] application args
--------------------------------------------------------------


Voila, you should now be able to compile Tamarin on Win32 :).
The next post will be about how to use that's shiny avmplus to compile ECMAScript 4 source.


read more ...