Swfdec Reference Manual | ||||
---|---|---|---|---|
Internals and garbage collectionInternals and garbage collection — understanding internals such as garbage collection |
#include <swfdec/swfdec.h> void swfdec_as_string_mark (const char *string); void swfdec_as_value_mark (SwfdecAsValue *value);
This section deals with the internals of the Swfdec Actionscript engine. You should probably read this first when trying to write code with it. If you're just trying to use Swfdec for creating Flash content, you can probably skip this section.
First, I'd like to note that the Swfdec script engine has to be modeled very closely after the existing Flash players. So if there are some behaviours that seem stupid at first sight, it might very well be that it was chosen for a very particular reason. Now on to the features.
The Swfdec script engine tries to imitate Actionscript as good as possible. Actionscript is similar to Javascript, but not equal. Depending on the version of the script executed it might be more or less similar. An important example is that Flash in versions up to 6 did case-insensitive variable lookups.
The script engine starts its life when it is initialized via
swfdec_as_context_startup()
. At that point, the basic objects are created.
After this function has been called, you can start executing code. All code
execution happens by creating a new SwfdecAsFrame and then calling
swfdec_as_context_run()
to execute it. This function is the single entry
point for code execution. Convenience functions exist that make executing
code easy, most notably swfdec_as_object_run()
and
swfdec_as_object_call()
.
It is also easily possible to extend the environment by adding new objects.
In fact, without doing so, the environment is pretty bare as it just contains
the basic Math, String, Number, Array, Date and Boolean objects. This is done
by adding SwfdecAsNative functions to the environment. The easy way
to do this is via swfdec_as_object_add_function()
.
The Swfdec script engine is dynamically typed and knows about different types
of values. See SwfdecAsValue for the different values. Memory management is
done using a mark and sweep garbage collector. You can initiate a garbage
collection cycle by calling swfdec_as_context_gc()
or
swfdec_as_context_maybe_gc()
. You should do this regularly to avoid excessive
memory use. The SwfdecAsContext will then collect the objects and strings it
is keeping track of. If you want to use an object or string in the script
engine, you'll have to add it first via swfdec_as_object_add()
or
swfdec_as_context_get_string()
and swfdec_as_context_give_string()
,
respectively.
Garbage-collected strings are special in Swfdec in that they are unique. This means the same string exists exactly once. Because of this you can do equality comparisons using == instead of strcmp. It also means that if you forget to add a string to the context before using it, your app will most likely not work, since the string will not compare equal to any other string.
When a garbage collection cycle happens, all reachable objects and strings are marked and all unreachable ones are freed. This is done by calling the context class's mark function which will mark all reachable objects. This is usually called the "root set". For any reachable object, the object's mark function is called so that the object in turn can mark all objects it can reach itself. Marking is done via functions described below.
void swfdec_as_string_mark (const char *string);
Mark string
as being in use. Calling this function is only valid during
the marking phase of garbage collection.
|
a garbage-collected string |
void swfdec_as_value_mark (SwfdecAsValue *value);
Mark value
as being in use. This is just a convenience function that calls
the right marking function depending on the value's type. Calling this
function is only valid during the marking phase of garbage collection.
|
a SwfdecAsValue |