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");
Evaluates a Javascript file as if it were included in the file at the point include is called.
Seed.include("tabview.js");
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);
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);
Returns: user input
Uses the GNU Readline library to display a prompt (using the prompt argument) and then wait for input from the user. The readline prompt provides history (using the up and down arrow keys) within a single Seed process.
var input = Seed.readline("prompt> "); Seed.print(input);
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.
try { Seed.check_syntax("234[asdf"); } catch(e) { Seed.print("Something horrible happened!"); }
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"); }
Evaluates a given segment of Javascript after timeout milliseconds. Keep in mind that this is evaluated in the global context, so local variables surrounding the call to setTimeout will not be available! Also, setTimeout will only work while a GLib main loop is running (after you've called Gtk.main(), etc.).
Returns the prototype for an object created with constructor. This can be used to add methods to all future objects of a particular class.
Seed.prototype(Gio.FileInputStream).get_contents = function() { var stream = Gio.DataInputStream._new(this); var line = stream.read_until("", 0); return line; }
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) }
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));
An array representing the arguments passed to the seed
interpreter.
Terminates the execution of the Seed interpreter, returning exitcode as the exit value of the program.
Connects function to the signal, signame, on object. Any GObject signal will work. context is passed to the signal handler as the this
object; if omitted, the global context is used. 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);
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:
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:
HelloLabelType = { 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 a constructor, and instance of the object:
HelloLabel = new GType(HelloLabelType); 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 it's parents, and internally has it's own GType.