v.0.3
Seed.import_namespace(namespace, version)

Imports functions and constructors from the given gobject-introspection namespace. The optional version parameter forces a particular version, and will throw an exception if the typelib for that version is not installed; if it is omitted, the latest version is loaded.

Seed.import_namespace("Gtk", "2.0");
Seed.include(file)

Evaluates a Javascript file as if it were included in the file at the point include is called.

Seed.include("tabview.js");
Seed.print(value)

Prints, to standard output, a representation of value. Number types are printed as floating-point values (with 6 decimal places); strings are printed as-is; objects are printed as [object type].

Seed.print(5);
Seed.print("This is a test!".replace(" is ", " was "));

var win = new Gtk.Window();
Seed.print(win);
Seed.printf(format, ...)
Seed.sprintf(format, ...)

printf prints, to standard output, a string formatted as specified by format. Following format should be values to substitute, as in C's printf. Most standard printf format strings should work.

sprintf returns the string, instead of printing it.

Seed.printf("A number: %d\n", 5);
Seed.printf("One third is approximately %.3f\n", 1/3);
Seed.printf("%d %s %d\n", 2, " is not ", 5);

var my_string = Seed.sprintf("%d + %d = %d", 2, 3, 2+3);
var my_name = Seed.printf("[%s] is %d characters long!\n",
                          my_string, my_string.length);
Seed.check_syntax(code)

Examines a segment of Javascript, looking for syntax errors. If errors are found, an exception is thrown, which can be caught with a try/catch block. You can examine the location of the syntax error with the line property of the returned exception.

try
{
    Seed.check_syntax("234[asdf");
}
catch(e)
{
    Seed.print("Something horrible happened on line " + e.line);
}
Seed.fork()

Returns: pid of child (to parent); 0 (to child)

Creates a new process which is an exact copy of the current one, starting from the next instruction in both processes. It works just as POSIX fork should.

var pid = Seed.fork();

if(pid)
{
    // Parent
    
    while(1)
        Seed.print("From Parent");
}
else
{
    // Child

    while(1)
        Seed.print("From Child");
}
Seed.introspect(function)

Returns an object containing information about the function, its arguments, etc. This will eventually support introspection of a wider variety of Javascript types.

proto = Seed.prototype(Gtk.Window);
method = Seed.introspect(proto.translate_coordinates);

for(i in method.args)
{
    Seed.print(method.args[i].type)
}
Seed.stringify(object)

Returns a string representing the entire contents of object in a pretty-printed fashion, like that of JSON.

proto = Seed.prototype(Gtk.Window);
method = Seed.introspect(proto.translate_coordinates);
Seed.print(Seed.stringify(method));
Seed.argv

An array representing the arguments passed to the seed interpreter.

Seed.quit(exitcode)

Terminates the execution of the Seed interpreter, returning exitcode as the exit value of the program.

object.signal.signame.connect(function, user_data)
object.connect(signame, function, user_data)

Connects function to the signal, signame, on object. Any GObject signal will work. If present, user_data is passed as the last argument to the callback.

function button_clicked()
{
    Seed.print("You pushed me!!");
}

var button = new Gtk.Button();
button.signal.clicked.connect(button_clicked);

The second form is useful if you want to connect to detailed signals; for example, notify:: signals on an object's properties:

function handle_opacity_change(obj, gobject, user_data)
{
    Seed.print("Window " + obj + "'s opacity was changed!");
}

win = new Gtk.Window();
win.signal.connect("notify::opacity", handle_opacity_change);
Exceptions

Seed throws Javascript exceptions for errors in the GObject layer; our custom exception types are as follows:

Exceptions are caught with the try/catch construct:

try
{
    var window = new Gtk.Window();
    window.opacity = "hello!";
}
catch(e)
{
    Seed.print("An exception occurred!");
}

e is the name we've given the Exception object in this examle. The Exception object has a handful of properties which provide more information about the exception:

Just as in Javascript, you can throw an exception manually with the throw function, passing it an object - either a new object, with the properties listed above (for consistency), or an arbitrary object:

try
{
    if(!http.connect("http://google.com"))
        throw { name: "HTTPConnectionError", message: "404 File Not Found" }
}
catch(e)
{
    // e.message = "404 File Not Found"
}
Inheritance

JavaScript, being a prototypal language, rather than a class based language, has no strict inheritance model. A plethora of documentation can be found on the internet for implementing various inheritance models inside your program. However, a clear and common use case is to subclass GObjects, and Seed provides an interface to define and implement new GTypes.

Type Objects

To implement a new GType, an object describing the type is required.

NewType = {
    parent: ParentTypeConstructor,
    name: "NewTypeName",
    class_init: function(klass, prototype)
    {
    },
    instance_init: function(klass)
    {
    }
}

Indicates that the new type derives from ParentType, i.e. Gtk.Window, with name "NewTypeName". The class_init function is called when the class comes in to existence, and allows you to add to the prototype of objects constructed by the type. The instance_init function is called on the creation of each instance, with the "this" variable set to the new instance. An example type:

HelloLabel = new GType({
    parent: Gtk.Label,
    name: "HelloLabel",
    class_init: function(klass, prototype)
    {
        prototype.say_goodbye = 
                             function()
                             {
                                 this.label = "Goodbye";
                             }
    },
    instance_init: function(klass)
    {
        this.label = "Hello"; // Hello Labels Always Say Hello.
    }
});

Now to create an instance of the object:

label = new HelloLabel();
box.pack_start(label);
label.show();
label.say_goodbye();

The label inherits all the methods, signals, and properties of the Gtk.Label class and its parents, and internally has its own GType.

signal.emit(...)

emit provides the ability to arbitrarily emit any GObject signal, thus calling all of the functions which are connected to it. Any arguments passed to emit are passed on to the callback function.

win = new Gtk.Window();
win.signal.close.connect(Gtk.main_quit);
win.signal.close.emit();
class.install_signal(signal_descriptor)

When creating a new GObject type within Seed, install_signal provides the ability to install new signals, which you can later emit with emit and can be connected to in any of the usual ways.

signal_descriptor is a Javascript object describing the signal. Important properties of signal_descriptor are:

For example:

HelloWindow = new GType({       
    parent: Gtk.Window.type,
    name: "HelloWindow",
    class_init: function(klass, prototype)
    {
        var HelloSignalDefinition = {name: "hello",
                                     parameters: [GObject.TYPE_INT,
                                                  GObject.TYPE_STRING],
                                     return_type: Gtk.Window.type};

        hello_signal_id = klass.install_signal(HelloSignalDefinition);
    },
});

w = new HelloWindow();

w.signal.hello.connect(function(object, number, string)
                       {
                           Seed.print(number + " " + string);
                           return new Gtk.Window()
                       });

Seed.print(w.signal.hello.emit(2, "Test"));