[Swfdec] variable scope

Benjamin Otte otte at gnome.org
Mon Jun 11 01:13:02 PDT 2007


On 6/11/07, Nguyen Thai Ngoc Duy <pclouds at gmail.com> wrote:
> caveat: I haven't either read actionscript language spec nor really
> got through swfdec context, frame and such. This is fully based on
> blocky.swf code inspection.
>
> thanks to flare, i can extract the below beautiful code. You don't
> need to understand the whole thing but there is one thing to notice:
> border[XY]start are initialized in on(press) and get used in
> on(release). Currently swfdec makes those variable in on(press)'s
> local scope or something. When on(release) is run, it cannot find
> border[XY]current and hence fail to run properly.
>
I just looked at the button movie code, and it looks like I didn't
touch it in a few months. Seems that shows now that you have a Flash
that is using buttons.

Anyway, the scope chain (inside Swfdec) for a normal script looks like this:
global <= target <= frame [<= With [ <= With ...] ]
The frame only contains the "this" variable. The target is the object
the script was called on. It is the object passed to
swfdec_event_list_execute() or in turn swfdec_as_object_run(). In the
button case, we might even be passing the wrong object (we're passsing
the parent?!).
As a side note, in older movies (think Flash 4/5) it is common to not
use WIth but instead modify the target via SetTarget actions. This
modifies the target in the scope chain.

The scope chain in a script-defined function looks like this:
Scope_the_function_was_defined_in <= frame [<= With [ <= With ...] ]
So, if you define a function, you inherit all the With and Frames of
where the function was defined. This is completely weird and really
requires wrapping one's mind around (unless you happen to already have
done this as a Javascript coder). Once you've managed to do that, you
can write callbacks like this to get around it:
function createCallback (object, fun)
{
  With (object) {
    fun.apply (o, arguments.slice (2));
  };
};
netstream.onStatus = createCallback (some_weird_object, some_function);

Now, all this code is only relevant for getting variables. When
setting variables that are not defined yet (where not defined means
not in the scope not including the global object), it depends on if
the variable is set via DefineLocal or SetVariable. I've never really
checked this, but I think SetVariable sets on the current target and
DefineLocal sets on the current target unless in a user-defined
function, where it sets in the current function's frame.

So, after telling you all of this, I'd like to note that this is oly
how Swfdec works, but I have no clue if it's correct. So if you manage
to create an SWF (like the one you just posted) where this doesn't
work, we have to fix Swfdec. And probably rewrite all the knowledge I
just posted above...

Cheers,
Benjamin,
who sometimes feels like the Winston Smith of Flash


More information about the Swfdec mailing list