Globals
Ringo adds a few functions to the global object beyond those defined by ECMAScript. Some of those are adopted from the Rhino shell, some are defined CommonJS proposals, while others are proprietary to RingoJS.
CommonJS Functions
require(moduleName)
The require function provides the functionality defined in the CommonJS Modules proposal. It tries to locate a module in the module path, loads it and returns its exports object.
require.mainholds the class of the main script executed. The array of module paths searched can be read and manipulated viarequire.paths.
RingoJS Functions
include(moduleName)
The include function builds on top of require, additionally copying all exported properties of the loaded module to the calling module scope.
export(propertyName[, ...])
The export function provides an alternative method to the exports object to define exported properties in a module by passing the names of exported properties as arguments.
addToClasspath(pathName)
This function adds a jar file or directory to the classpath. By default, all jar files in the RingoJS lib directory are included in the classpath.
getResource(pathName)
This looks for a file resource with the given path name in the module path and returns a resource object. This can be used to load resources other than JavaScript files using the same lookup rules as the module loader.
getRepository(pathName)
Looks for a directory with the given path name in the module path and returns a repository object.
Rhino functions
defineClass(javaClass)
Defines a JavaScript host object class from a Java class implementing
org.mozilla.javascript.Scriptable. While any public Java class can be scripted in Rhino, Java classes must follow specific naming conventions or use special annotations in order to qualify as host classes.
sync(func, [syncObject])
Creates a wrapper function that synchronizes invocation of
funcon the this-object orsyncObject, if defined. This means a thread calling the function acquires an exclusive lock on the object. Other threads calling the same function have to wait until the lock is released, guaranteeing that only one thread executes the function at any given time.
print(string)
Prints one or more arguments to stdout, followed by a newline character. Multiple arguments are separated by a single space character.
importClass(javaClass)
Imports the Java class into the current scope. Note that it needs to be a fully qualified class name and the "Packages" prefix needs to be used for classes outside java.lang package.
importClass(java.io.File);Note that you can do the same and maybe more elegantly with destructuring assignments in RingoJS:
var {File} = java.io;
importPackage(javaPackage)
Imports all the classes in the Java package into the current scope. Use this one with care as java packages often contain dozens of classes which may slow down your program.
Usually it is preferable to just import the classes you really need using destructuring assignment syntax:
var {HashMap, ArrayList} = java.util;
TODO this page is still incomplete. See http://github.com/ringo/ringojs/blob/master/src/org/ringojs/engine/RingoGlobal.java for more.
