public static Object spawn ( Context cx , Scriptable thisObj , Object [ ] args , Function funObj ) { Scriptable scope = funObj . getParentScope ( ) ; Runner runner ; if ( args . length != 0 && args [ 0 ] instanceof Function ) { Object [ ] newArgs = null ; if ( args . length > 1 && args [ 1 ] instanceof Scriptable ) { newArgs = cx . getElements ( ( Scriptable ) args [ 1 ] ) ; } if ( newArgs == null ) { newArgs = ScriptRuntime . emptyArgs ; } runner = new Runner ( scope , ( Function ) args [ 0 ] , newArgs ) ; } else if ( args . length != 0 && args [ 0 ] instanceof Script ) { runner = new Runner ( scope , ( Script ) args [ 0 ] ) ; } else { throw reportRuntimeError ( "msg.spawn.args" ) ; } runner . factory = cx . getFactory ( ) ; Thread thread = new Thread ( runner ) ; thread . start ( ) ; return thread ; }
The spawn function runs a given function or script in a different thread .