Some questions about Redtamarin


#1

My point is that I’ve seen for example you mentioned about w reimplementing Events system and I recently thought about doing just that because I’m not completely satisfied with native one. Like, I don’t understand why for 2 listeners there are created 2 separate instances of the same event. I have some event driven app where I simulate gravity by Newtonian equation. With few particles I’m able to calculate about 2000steps/s, where for each step, new event is dispatched + ~70/s from Timer event, and another 60/s for rendering… Well, the app wast not suppose to run massive simulation but Thinking creating all those object make me want to improve it a bit. I heard about Signals but I’m sure how this will work with current events code scheme I would really like to keep it as it is. I even did something like this recently, just to get code completion for events in FD (I don’t like to extend event dispatcher) :

package adnss.projects.sedit 
{
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	/**
	 * Digital Signal sample.
	 * @author Audionysos
	 */
	public class SSample {
		
		private var _events:SSampleEventDisp = new SSampleEventDisp();
		
		public function SSample(sample:*, fps:Number = 44100, bDepth:uint = 4, nCh:uint = 2) {
			...
		}
		
		public function get source():SSample { return _s; }
		/**!!TODO: validate that given source do not lead to circular reference.
		 * This can couse program crash!**/
		public function set source(value:SSample):void {
			if (_s) {
				_s.events.removeEventListener(SSampleEvent.REGION_ADDED, onRegionAdded);
				_s.events.removeEventListener(SSampleEvent.MARKER_ADDED, onMarkerAdded);
			}
			_s = value;
			for each (var m:TimeMarker in _s.markers) { addMarker(m);}
			_s.events.addEventListener(SSampleEvent.REGION_ADDED, onRegionAdded);
			_s.events.addEventListener(SSampleEvent.MARKER_ADDED, onMarkerAdded);
			_rB.x = _s.rootBounds.x; _rB.y = _s.rootBounds.y;
		}
		
		/**Dispatches SSampleEvent events for this sample instance.**/
		public function get events():SSampleEventDisp{return _events;}
		
	}

}
import flash.events.EventDispatcher;

/**
 * Dispatched after region was added to sample.
 * @eventType	adnss.projects.sedit.SSampleEvent
 */
[Event(name = "regionAdded", type = "adnss.projects.sedit.SSampleEvent")]
/**
 * Dispatched after time marker was assigned to sample.
 * @eventType	adnss.projects.sedit.SSampleEvent
 */
[Event(name = "markerAdded", type = "adnss.projects.sedit.SSampleEvent")]
/**
 * Dispatched after postion of marker associated with this sample was changed.
 * @eventType	adnss.projects.sedit.TMarkerEvent
 */
[Event(name="markerMoved", type="adnss.projects.sedit.TMarkerEvent")] 
class SSampleEventDisp extends EventDispatcher { }

…So If I would write my own event system I don’t see any reason to not share it so you wont waste time on this again, unless you really want to write everything by yourself :stuck_out_tongue: I perfectly understand “stelth mode” and not publishing unfinished painting but with few people actually developing AS3 it would be sad make to code overlaps so maybe it would be good to clarify what are we doing :wink:


Anyway, I found your post because I’m new to Redtamarin and I have a little isse. It’s not to complicated so I hope it’s fine I would ask about this here.

So, I’m sucesfully passing .as files to redbeans or use -repl and everything works fine but when I try to follow your video tutorial something is wrong in that my test.as file trace("hello word"); outputs null in console. Another strange thing is that when I try to compile the file to .abc file, I don’t have any errors but I also don’t see any file been created anywere. In fact what I’m having now is this:

//compile(“src/TamTest.as”);
shell(“src/TamTest.abc”);

and output gives just null (again “TamTest.abc” don’t even exist):

[redbean 1.0.0]
[exe] C:\Users\Suchy\Documents\flash\TotalWar\utils\redtamarin-sdk-windows\bin\redbean.exe
[arg]
[cwd] C:\Users\Suchy\Documents\eclipse\workspace\NewEclipse
[config]
var REDTAMARIN_SDK = “C:\Users\Suchy\Documents\flash\TotalWar\utils\redtamarin-sdk-windows”;
var showHelp = false
var showInformations = false;
var showEnvironment = false;
var quietMode = false;
var verboseMode = true;
var buildfile = “build.as3”;
var configfile = “redbean.cfg”;
var useDebugger = true;
[run] build.as3

[shell] src/TamTest.abc
[redshell] C:\Users\Suchy\Documents\flash\TotalWar\utils\redtamarin-sdk-windows\runtimes\redshell\windows\32\redshell.exe src/TamTest.abc

null

[done] in 37ms

I don’t actually have Flash Builder and I set it up in completely blank Eclipse Lite installation but as far as I understand this, I should only have to set up my custom builder to redbeans and SWCs are only used for code completion? Shouldn’t I get the same result as calling redbeans from cmd?
Please answer quick as I’m so hyped after reading your post I can’t wait long. Thank you :slight_smile:


**admin edit:** moved from other topic. reason: too many different questions/subjects into one post

Example event system in AS3
Cracking Up The Event Loop
#2

The thing is implementing an event system natively in C/C++ can be complicated
look at issue #82 flash.events package and the subtopics, it can get very complicated real fast.

first problem
“as is” the AVM2 or redtamarin code context does not have a mechanism to handle frames,
there are some mention of it in the source code, but right now we have only one code context
which we can consider global or “only 1 frame”.

Flash/AIR can have different code context when moving from one frame to another,
that’s the Event.ENTER_FRAME, Events.EXIT_FRAME, etc.

second problem
by default in redtamarin we are synchronous, eg. we can block code execution forever,
with either Program.sleep() or in C.unistd with the function sleep() we can basically block as much as we want without triggering a max-execution-time or other default-script-limits (usually 15 seconds in Flash).

third problem
because AVM2 have workers that use pthread we need to be careful how we intermix
events and threads.
simply put, events and threads do not play well with each others (I’m talking about how you implement events in C/C++).

fourth problem
when you are programming “socket server”, even if you are blocking and synchronous, you do implement a “server loop” to be able to serve all the clients, mixing a “server loop” with an “event loop” is also something that can get complicated.

So to be able to have AS3 events like in Flash/AIR we need to be able to go asynchronous,
but we do not want to go “all in” with one (synchronous) or the other (asynchronous), we want to be able to do have an option to use events if we need to.

Node.js is asynchronous by default and you need a trick to make it synchronous, we don’t want to do that because we are dealing with the command-line and on the CLI things are synchronous by default.

Node.js to implement its “event loop” use something like libuv, see An Introduction to libuv.

so yeah it is a complicated problem, again read the notes in the issue tracker
in particular #84 test a worker event loop,
because we already have workers we could implement the event loop in AS3 instead of doing it natively in C/C++, so far it is the direction taken.


humm you need to uncomment the line that actually compiling the *.as source

compile("src/TamTest.as"); // here
shell("src/TamTest.abc");

also look at Compiling Programs

when you use compile() with redbean it basically execute
$ java -jar asc.jar [options] myfile.as

with the verbose mode
$ redbean -v

you should see something like that

[redbean 1.0.0]
[arg] -v
[cwd] /Volumes/prototype/platform/redshell-tests/proto042
[run] build.as3

[compile] proto042lib.as
[asc1] java -jar /usr/lib/redtamarin/support/asc.jar -AS3 -d -import /Volumes/prototype/platform/redtamarin/bin-release/components/redtamarin.abc proto042lib.as

proto042lib.abc, 13304 bytes written


[done] in 97ms

if you don’t see “something.abc, 123 bytes written” then you are not compiling the ABC
so when you call shell() the file is not there to be executed


By installing the redtamarin-sdk you get asc.jar, so the only dependency you need to compile is Java.

Yes, the redtamarin.swc is only there for syntax completion in IDE like Flash Builder.

You can also look at small projects like as3hash to see an example of how a project can be organised.

Documentation is not there yet but I’m working on it.


Example event system in AS3
#3

Yes, I was actually thinking about implementing this in AS3 - to be honest my experience with C is as little as few complies for Arduino. Also I’m not sure how you exactly glue C with AS3 in Redtamarin (I’ve downloaded it just yesterday) but as Event suppose to be expendable to custom classes I see these as something to implement in AS3… (unless you can write something like base class in C that could be extended with AS?)

And I’m not sure why you are talking about “event loop” and “go asynchronous” as core Event system is synchronous. You can design interrupts to work with them and stuff like frame events, it can look like you get call any time, but events itself are completely synchronous and unless there is some user input involved or “physically” separated threads they are completely predictable.

Things like frame rendering (Event.EnterFrame or Event.ExiFrame) are another thing. Frame events are synchronised with graphic rendering engine. You don’t need to listen for those events in order to get frames rendered but this doesn’t mean that rendering engine have to run without sync to the program. Rendering engine itself could just create interrupts to dispatched events.
It would be nice be able to start dispatching events at C point but this could be done really at any custom level of your app. Once you have Event dispatching system deployed you could just create simple interrupts to simulate stage rendering and dispatch your own Event.ENTER_FRAME. You could just incorporate something like setIterval to have some consistent frame rate. And creating interrupts is not really that hard. Here for example I did something like that to run my algorithm “independently” of rest of the program - you can adjust frequency to any value you like and it’s not locked to flash frame rate, you can control state of the algorithm from any point which have access to object which dispatches events which drive that algorithm. If thee is easy way to set some interval in C without blocking CPU then it should be easy to simulate frame events…

And also question is how external swf suppose to be loaded? Because generally frame events are listened through display list (DisplayObject). How stage concept looks in Redtamarin?

About a second problem I don’t actually see what is the problem here. You don’t have max-execution-time as with flash but how is it a problem? When you go to infinite loop you are going to hang the program anyway. Isn’t only benefit of that is that flash would exit such program automatically?
From what you now wrote I understand that there is not way of setting no-blocking interval in e

Using multiple threads can be tricky but end developer should be aware of this. You can’t really do anything to ensure he is not making any mistakes. I’m not sure how Your Worker class is different from flash one but I imagine Workers as two separate program communicating similarly to any other two programs communicating with sockets.
By the way, I’m not very exited about whole Worker concept. It’s ok if you simply want to have communication between swfs and occasionally exchange data but If I have lot of data that is updating constantly, the idea of converting it back and forward to ByteArrray or simply having it in two different places on the same machine doesn’t seem right. But changing that would require modification in code that is running virtual machines (If I remember correctly, shared memory was possible in first version of Worker)…

About fourth problem, Again I don’t understand what you mean by “event loop”. I would imagine something like using timer events to create server loop and then from that loop dispatch socket event to get something similar to current flash Socket class.
Btw. I’ve just recently wrote socket communication between Blender and flash app.
For my need it’s only maintaining single connection but I understand the trick… :slight_smile:
And If I would have to rate hardness of each side:
Python - 8/10 (done with blocking socket on separate thread)
AS3 - 2/10 (Socket class)
Though it’s not completely fair as I’m not very familiar with python, on the other hand, python docs suck so no reason to defend it. :stuck_out_tongue:

Anyway, whether you will involve C into that or not, the Event system on AS3 side would be needed and I could do it :slight_smile:
PS. After looking at issues you linked I’m still not sure how you connect concept of loop with events. Do you want to create separate code context (VM instance) for every event? This is not how Event class works as far as any given test I made tells me. Events are just simply a way to organize function callbacks and Event instances don’t have separate contexts by default. Concept of events seem clear to me and If you want, I can give you example implementation like tomorrow, so maybe we could better understand each other :wink:


Sorry for been unclear. Of course it was uncommented in first place. My point was to show you that it does noting and even shell is not complaining about not existing file or anything. The console output just looks like the "Hellow world" string is evaluating to null but yes, the file is not compiling :

[compile] src/TamTest.as
[asc1] java -jar C:…\redtamarin-sdk-windows\lib\asc.jar -AS3 -md -strict -optimize -import C:…\redtamarin-sdk-windows\lib-abc\redtamarin.abc src/TamTest.as
null
[shell] src/TamTest.abc
[redshell] C:…\redtamarin-sdk-windows\runtimes\redshell\windows\32\redshell.exe src/TamTest.abc

null

[done] in 079ms

The longer I’m checkin it, the more confusion I get. :stuck_out_tongue: Lets list steps that suppose to happen.

  1. I’m choosing Project>Build Project / Build All
  2. Eclipse is starting *.exe defined in builder configuration “Location:” (in our case redbean.exe)
    • the working directory is directory for that process - this is the same as specifying “Start in:” in windows shortcut properties.
  3. Redbean is looking for redbean.cfg and loads it’s setting from it.
  4. Redbean is looking for build.as3 file in that working directory and execute script defined in it.
    1. import readbean.*; (to be able to call next functions)
    2. complie(".as"); (complie the file WorkingDirector+givenPah)
      • A “twin” .abc file is created in the same directory. (this fails as far as I can see)
    3. shell(".abc") (pass code that was just compiled to execute by redshell and save output to log)
  5. Redbean is returing output log to caller - Eclipse.
  6. Eclipse is displaying the output of Redbean in Console view.

And also just after I feelt a little more comfortable with command-line, this leads me again to conclusion that I totally don’t get it…
I mean how entering this to cmd:

C:\U\S\Documents\f\TW\u\r-s-w\bin> redbean
You must provide input files, -repl, or -Dselftest, or the executable must be a projector file.
avmplus shell 2.1 debug-debugger build cyclone

usage: avmplus
[-d] enter debugger on start

is different then entering this one:

C:\U\S\Documents\f\TW\u\r-s-w\bin> redbean.exe
[redbean 1.0.0]
[error] “build.as3” is not a valid file

?

I hope the post is not to long. :slight_smile:


#4

man … how about to combine 4 different subjects in one massive message

please keep one subject per thread, split your different subjects in different messages
well… you know, a forum is good for that


#5

I’m sorry, but I see only two subjects there - event system and my question about using Redbean in eclipse builder. As you already splinted my fist post I thought you are ok. with having two subject here :confused:


#6

so I gonna answer inline there, but that’s it … stop pilling up different subjects altogether
it is simple as create another thread, and then link to the thread in your answer

answer here mainly about events

I disagree with the part “core Event system is synchronous”, not the first time I see that,
I got the same argument with a whole engineering team at some company long time ago,
but no, I can assure you, events are asynchronous.

Check AsyncErrorEvent

An object dispatches an AsyncErrorEvent when an exception is thrown from native asynchronous code, which could be from, for example, LocalConnection, NetConnection, SharedObject, or NetStream.

So it’s not all white or black, the main event loop of Flash/AIR is probably implemented with 2 things

  • a sort of sleep() function
  • an intercept external event function

Many things are made to be predictable like the duration of frames, the rendering etc.
but there are many other things that are not predictable.

The “I can not predict or assume in which order the events occurs” is what make me say that the main event loop is asynchronous.

Another more in depth example is if you look into CrossBridge you will find an API call AS3_GoAsync() see the doc

When breaking a C/C++ run-loop so that Crossbridge code is suitable for use without using multi-threading you need to execute main, so the static initializers for your code are run, but you want to prevent the static destructors from running so it is important that main does not return. By throwing an AS3 exception and preserving the stack we can effectively interrupt the execution of main.

which lead to that great explanation Sample 4: Animation

The last sample showed how to inter-operate with the stage during main(), but what if you want to drive an animation from C++? Because AS3 is single threaded, you can’t simply put a while(true) loop in main as that would block the main Flash player thread and block screen updates, input events, and sound.

On my side I don’t understand why you keep using the word “interrupts” in relation to events
for me an interrupts is something related to C signals, it is a kind of low-level kernel events
but nothing in related to AS3 events.

Anyway, even if you don’t use pthreads (workers) and use a simple synchronous loop on the C front-end to implements events in the AS3 front-end, you still gonna have to mix it with that famous “intercept external event function” which will make then the behaviour of AS3 asynchronous.

My guess is you did not read the notes here
implement events (google code Issue 4) #46.

Read that then we can talk more.


Another subject for another thread.


It’s not my worker class, it is the one from the tamarin/avmplus project, so it is the same as the one used in the Flash Player or Adobe AIR.

It is implemented cross-platform using pthreads
see Multithreaded C programming with the Adobe Flash C Compiler (FlasCC)


See how you are adding more and more different subjects ?

Anyway, to understand what I mean by “event loop”, please read
flash.events package #82
followed by


I’m not completely convinced here.


Look … I can point into the right direction, but imho you are lacking many concepts about the AVM2 runtime and how the C/C++ part interact with AS3 part, and that’s OK it is a complicated subject.

The thing is I can not transfer the 10 years I spent working on redtamarin just by answering questions, so if you want to figure out how things work, well … you gonna have to put some work into it.

So again keep a thread about a single subject if you need to ask questions, ideally if the questions are about bugs/issues with redtamarin itself you should post that on the github issue tracker.



OK so let’s start with the basic

  • which version of Windows ?
  • how did you install redtamarin-sdk ?
  • which version of the redtamarin-sdk did you install ?
    redtamarin-sdk_0.4.1_win64.deb ?
  • Do you use ConEmu or other POSIX shell ?
    no, Windows cmd.exe is not supported
  • how is the structure of your project ?
    • from which directory you try to run what ?
    • what is the content of TamTest.as ?

Now from what you answer I would say

  • don’t run the commands from Eclipse use a proper Terminal/Console first
    and once you see how it works then you can adapt to Eclipse with builders and other commands
  • if your console prompte starts with C:\> you are using cmd.exe, it will not work
    use a POSIX console/terminal like the Cygwin console, ConEmu, etc.
    eg. the prompt should starts with $
  • my guess is that the way the “Build All” builder is setup in Eclipse the executable
    can not find the path of the *.as file because of the “current working directory”

but “as is” I can not be sure that is your exact problem,


I’m used to trolling behaviours …