V8 Javascript API

Module System

[Architecture]

Description

The V8 API is an entirely module based system. There is no 'main' JavaScript file which is loaded and executed, instead individual commands are implemented as modules and these modules are executed when their command is called. Command modules can call RealityServer commands directly via the RS global object or can require other modules to access scene elements in an object oriented manner.

In addition to the core RealityServer Globals modules provide four additional variables and one function.

‎ module 
The Module object referencing the current module. Contains the module id and the exports object which exposes the module to other modules. The module implementation should either populate object with it's exports or replace it if it want's to export something other than an object.

‎ exports 
A shortcut to module.exports. Should only be used if we are populating the export object opposed to replacing it.

‎ __filename 
Absolute path to the module filename.

‎ __dirname 
Absolute path to the module directory.

require(id) 

The require function is used to import other modules into the current module.

id specifies either a JSON file or a module and must resolve to a filename that can be loaded from disk. JSON files are identified by the extension .json in the ID, all others are interpreted as module IDs. Mapping of ID to filename occurs as follows:

  • filename = id

  • if filename extension is not .json then append .js

  • if filename starts with ./ then load filename from same directory as the file that required it.

  • else search all include and command paths for filename and return the first path found. Paths are searched in the order they are defined in the configuration file.

Once the filename has been resolved it is loaded and returned. JSON files are simply parsed and their contents returned, modules are loaded and their exports returned.

Note that id can be a path like construct. EG:

require('path/file') 

will search for path/file.js in the configured directories.

Classes

class 
The module class. More...

Functions

varying require( String id)
Requires other JavaScript modules This function is used to pull in external JavaScript modules and JSON files. More...

Functions

varying require( String id)

Requires other JavaScript modules This function is used to pull in external JavaScript modules and JSON files. See Module System for details on how modules are discovered and loaded.

Parameters

id
the ID of the module to require or the path to a JSON file to import.

Returns

the module exports or the parsed JSON file.