| 1 |
// usage examples for a number of Machine functions |
| 2 |
// (requires chuck-1.5.1.3 or higher) |
| 3 |
// |
| 4 |
// see ChucK API Reference on Machine |
| 5 |
// https://chuck.stanford.edu/doc/reference/base.html#Machine |
| 6 |
// |
| 7 |
// or uncomment the next line to print Machine usage |
| 8 |
// Machine.help() |
| 9 |
|
| 10 |
// print language version |
| 11 |
<<< "chuck version:", Machine.version() >>>; |
| 12 |
|
| 13 |
// keep informed |
| 14 |
<<< "\nMachine.eval() example...", "" >>>; |
| 15 |
|
| 16 |
// compile the string as code and spork it as a new shred |
| 17 |
// also see eval.ck for more on Machine.eval() |
| 18 |
Machine.eval( "repeat(5) { <<< Math.random2f(0,1) >>>; }" ); |
| 19 |
|
| 20 |
// keep informed |
| 21 |
<<< "\nwaiting for half a second...", "" >>>; |
| 22 |
|
| 23 |
// advance time |
| 24 |
.5::second => now; |
| 25 |
|
| 26 |
// keep informed |
| 27 |
<<< "\ntrying some shred management...", "" >>>; |
| 28 |
|
| 29 |
// add |
| 30 |
Machine.add( me.dir() + "test1.ck:foo" ) => int id; |
| 31 |
// yield to give new shred a chance to run |
| 32 |
me.yield(); |
| 33 |
|
| 34 |
// replace |
| 35 |
Machine.replace( id, me.dir() + "test2.ck:bar:5" ) => id; |
| 36 |
// yield to give new shred a chance to run |
| 37 |
me.yield(); |
| 38 |
|
| 39 |
// print current VM status (list of shreds) |
| 40 |
Machine.printStatus(); |
| 41 |
// remove shred |
| 42 |
Machine.remove( id ); |
| 43 |
|
| 44 |
// keep informed |
| 45 |
<<< "\ntrying more Machine functions...", "" >>>; |
| 46 |
// print current VM time |
| 47 |
Machine.printTimeCheck(); |
| 48 |
// reset shred IDs to current highest + 1 |
| 49 |
Machine.resetShredID(); |
| 50 |
// remove all shreds (including this one!) |
| 51 |
Machine.removeAllShreds(); |
| 52 |
// reset type system and all global variables; remove all shreds |
| 53 |
// Machine.clearVM(); |
| 54 |
|
| 55 |
// unreachable code |
| 56 |
<<< "shouldn't get here" >>>; |