User Guide

98 Application Development Tips and Tricks
Simple-system call example
The following simple-system call example implements an ID generator. The generator must
generate an increasing number for each new ID. If you write the generator code in the
main.asc file, any part of the script could easily redefine the
nextID() function or directly
modify the
_nextID value, as in the following:
idGen = {};
idGen._nextID = 0;
idGen.nextID = function() {return this._nextID++;}
If you move the generator code to the secure.asc file, it loads before the main.asc file and isnt
accessible by code in the main.asc file. The following is the secure.asc file:
// Begin secure.asc.
trace("loading secure.asc");
var global = getGlobal(); // Grab the global object.
var idgen = {};
idgen._nextID = 0;
idgen.nextID = function() {return this._nextID++;}
// Create a protected object out of idgen and make it
// available globally as idGen.
global.idGen = protectObject(idgen);
// Make idGen non-enumerable, read-only, and permanent.
setAttributes(global, "idGen", false, true, true);
The following is the main.asc file that attempts to access the idGen variable that was protected
in the secure.asc file:
// main.asc
trace("Loading main.asc");
trace("idGen = " + idGen);
idGen = 50;
trace("idGen = " + idGen);
The following is the output from both files:
Loading secure.asc
Loading main.asc
idGen = [object Redirector]
idGen = [object Redirector]
Synchronous system calls
A synchronous call performs its action completely by the time the call returns. A synchronous
system call is a synchronous call that is protected and routed through a system object.