v.0.6
imports

An imports object is defined globally in every Seed context. This provides access to GObject Introspection namespaces, C extension modules, and other JavaScript files, as explained in the next three sections.

The default path to search for native modules and JavaScript files to be imported can be set as an array of strings on imports.searchPath:

imports.searchPath.push("/opt/javascript");

This will add /opt/javascript as the last location to search when looking for native Seed modules and JavaScript files. The default search path includes the current directory and the directory into which Seed's default native modules are installed.

imports.gi

Provides access to all installed gobject-introspection namespaces. imports.gi.namespace will import functions and constructors from the given namespace and return an object providing them.

Importing is done once per process, and any subsequent accesses to the same namespace return the same object, and are very cheap as a result.

Gtk = imports.gi.Gtk;
Gtk.init(null, null);

A particular version of a namespace can be loaded by setting before it is first requested, by setting the imports.gi.versions.namespace object to a string representing the version number to load:

imports.gi.versions.Clutter = "0.8";
Clutter = imports.gi.Clutter; // The returned object represents clutter-0.8
Importing modules and JavaScript files

Native C modules and JavaScript files can be imported in a similar fashion, by accessing imports.file. Notice that the suffix (most likely .so or .js, respectively) is not included in the file name when requesting it, and keep this in mind when naming files.

First, if the file name is actually a directory, an object is returned that represents the contents of that directory, which behaves exactly as imports does in regards to importing native modules and JavaScript files. For example, say you have the directory js, which contains score.js:

score = imports.js.score;

This will import score.js, just as described below.

If the file is not a directory, but happens to have your system's shared library suffix, the native module is loaded, and the module object is returned, similar to GObject Introspection namespace imports:

readline = imports.readline;
readline.readline(">");

Otherwise, Seed assumes that the file is a JavaScript file. If the file is found in the current path, it is evaluated (in a separate Seed context, so any state in the file it is imported from is not accessible), and the global object is returned. Keep in mind that it is possible to accidentally import a non-JavaScript file, as the extension is not taken into account.

Imagine we have the file test_file.js:

test_string = "Hello, world!";

And another file, which we evaluate with seed:

test_file = imports.test_file;
Seed.print(test_file.test_string);

This will print "Hello, world!", as expected. Notice how, unlike in versions of Seed prior to 0.5, the file is not actually evaluated in the context of the importing file, so its toplevel objects are not globally available.

Seed.import_namespace(namespace, version)

Deprecated. Do not use in new code. Will be removed in a future 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");

Behaviour when loading a namespace which has already been loaded is undefined and most likely unwanted. To work around this, a try/catch block can be used:

try{
    Clutter;
}
catch(e){
    Seed.import_namespace("Clutter");
}
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.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(){
    }
}

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(){
        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",
    signals: [{name: "hello",
               parameters: [GObject.TYPE_INT,
                            GObject.TYPE_STRING],
               return_type: Gtk.Window.type}];
});

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"));