Debian Codegraph @

src/core/ulib_machine.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: ulib_machine.cpp
28 // desc: class library for 'Machine'
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Spring 2004
32 //-----------------------------------------------------------------------------
33 #include "ulib_machine.h"
34 #include "chuck_type.h"
35 #include "chuck_vm.h"
36 #include "chuck_compile.h"
37 #include "chuck_errmsg.h"
38 #include "chuck_instr.h"
39 #include "chuck.h"
40
41 #include "util_string.h"
42 #include "util_platforms.h"
43
44 #include <string>
45 #include <algorithm>
46 using namespace std;
47
48
49
50
51 // exports
52 CK_DLL_SFUN( machine_crash_impl );
53 CK_DLL_SFUN( machine_add_impl );
54 CK_DLL_SFUN( machine_spork_impl );
55 CK_DLL_SFUN( machine_remove_impl );
56 CK_DLL_SFUN( machine_removeLast_impl );
57 CK_DLL_SFUN( machine_removeAll_impl );
58 CK_DLL_SFUN( machine_replace_impl );
59 CK_DLL_SFUN( machine_resetID_impl );
60 CK_DLL_SFUN( machine_clearVM_impl );
61 CK_DLL_SFUN( machine_printStatus_impl );
62 CK_DLL_SFUN( machine_printTimeCheck_impl );
63 CK_DLL_SFUN( machine_intsize_impl );
64 CK_DLL_SFUN( machine_shreds_impl );
65 CK_DLL_SFUN( machine_numshreds_impl );
66 CK_DLL_SFUN( machine_realtime_impl );
67 CK_DLL_SFUN( machine_silent_impl );
68 CK_DLL_SFUN( machine_eval_impl );
69 CK_DLL_SFUN( machine_eval2_impl );
70 CK_DLL_SFUN( machine_eval3_impl );
71 CK_DLL_SFUN( machine_version_impl );
72 CK_DLL_SFUN( machine_setloglevel_impl );
73 CK_DLL_SFUN( machine_getloglevel_impl );
74 CK_DLL_SFUN( machine_refcount_impl );
75 CK_DLL_SFUN( machine_regstack_impl );
76 CK_DLL_SFUN( machine_memstack_impl );
77 CK_DLL_SFUN( machine_platform_name_impl ); // 1.5.5.2 (azaday)
78 CK_DLL_SFUN( machine_timeofday_impl ); // 1.5.5.2 (azaday)
79 CK_DLL_SFUN( machine_timeofday_precise_impl ); // 1.5.5.2 (azaday)
80 CK_DLL_SFUN( machine_gc );
81 CK_DLL_SFUN( machine_opOverloadReset_impl );
82 // not used
83 //CK_DLL_SFUN( machine_opOverloadPush_impl );
84 //CK_DLL_SFUN( machine_opOverloadPop_impl );
85 //CK_DLL_SFUN( machine_opOverloadStackLevel_impl );
86 //CK_DLL_SFUN( machine_testSetup_impl );
87 //CK_DLL_SFUN( machine_testInvoke_impl );
88
89
90
91
92 //-----------------------------------------------------------------------------
93 // name: machine_query()
94 // desc: query entry point for Machine class library
95 //-----------------------------------------------------------------------------
96 DLL_QUERY machine_query( Chuck_DL_Query * QUERY )
97 {
98 // get global env instance
99 Chuck_Env * env = QUERY->env();
100 // set name of this query
101 QUERY->setname( QUERY, "Machine" );
102
103 /*! \nameinfo
104 Machine is the runtime interface to the ChucK Virtual Machine
105 (Ck.VM or simply VM). This interface can be used to manage shreds,
106 evaluate code, set log levels, etc. Machine's shred commands
107 (add, replace, remove, etc.) are similar to the
108 <a href="otfp.html">on-the-fly programming commands</a>,
109 except these are invoked from within a ChucK program, and benefit
110 from ChucK's strongly-timed mechanics.
111 */
112
113 // register deprecate
114 type_engine_register_deprecate( env, "machine", "Machine" );
115
116 // class
117 QUERY->begin_class( QUERY, "Machine", "Object" );
118 // documentation
119 QUERY->doc_class( QUERY, "Machine is the runtime interface to the ChucK Virtual Machine (Ck.VM or simply VM). This interface can be used to manage shreds, evaluate code, set log levels, etc. Machine's shred commands (add, replace, remove, etc.) are similar to the on-the-fly programming commands, except these are invoked from within a ChucK program, and benefit from ChucK's strongly-timed mechanics." );
120
121 // add add
122 //! compile and spork a new shred from file at 'path' into the VM now
123 //! returns the shred ID
124 //! (see example/machine.ck)
125 QUERY->add_sfun( QUERY, machine_add_impl, "int", "add" );
126 QUERY->add_arg( QUERY, "string", "path" );
127 QUERY->doc_func( QUERY, "Compile and spork a new shred from file at 'path' into the VM; returns the new shred ID. It is possible to include arguments with the file being added, e.g., `Machine.add( \"foo.ck:bar:3:5.0\" )`." );
128
129 // add replace
130 //! replace shred with new shred from file
131 //! returns shred ID , or 0 on error
132 QUERY->add_sfun( QUERY, machine_replace_impl, "int", "replace" );
133 QUERY->add_arg( QUERY, "int", "id" );
134 QUERY->add_arg( QUERY, "string", "path" );
135 QUERY->doc_func( QUERY, "Replace shred with new shred from file. Returns new shred ID, or 0 on error. It is possible to include arguments, e.g., `Machine.replace( outID, \"foo.ck:bar:3:5.0\" )`.");
136
137 // add remove
138 //! remove shred from VM by shred ID (returned by add/spork)
139 QUERY->add_sfun( QUERY, machine_remove_impl, "int", "remove" );
140 QUERY->add_arg( QUERY, "int", "id" );
141 QUERY->doc_func( QUERY, "Remove shred from VM by shred ID (returned by Machine.add).");
142
143 // add removeLastShred
144 QUERY->add_sfun( QUERY, machine_removeLast_impl, "int", "removeLastShred" );
145 QUERY->doc_func( QUERY, "Remove the most recently added shred in the VM (could be the shred calling this function); returns the ID of the removed shred.");
146
147 // add removeAllShreds
148 QUERY->add_sfun( QUERY, machine_removeAll_impl, "void", "removeAllShreds" );
149 QUERY->doc_func( QUERY, "Remove all shreds in the VM (including the shred calling this function).");
150
151 // add resetShredID
152 QUERY->add_sfun( QUERY, machine_resetID_impl, "int", "resetShredID" );
153 QUERY->doc_func( QUERY, "Reset shred IDs to 1 + the highest current shred ID in the VM; can be used as shred management to keep shred IDs low, after a lot of sporks; returns what the next shred ID would be.");
154
155 // add clearVM
156 QUERY->add_sfun( QUERY, machine_clearVM_impl, "void", "clearVM" );
157 QUERY->doc_func( QUERY, "Reset the type system, removing all user-defined types and all global variables; removes all shreds in the VM (including the shred calling this function); use with care.");
158
159 // add resetOperators
160 QUERY->add_sfun( QUERY, machine_opOverloadReset_impl, "void", "resetOperators" );
161 QUERY->doc_func( QUERY, "Reset operator overloading state to default startup state; removes all public @operator overloads; use with care.");
162
163 // comment out for now -- since local overloads don't persist, push/pop should probably affect public @operator overloads
164 // // add operatorsPush
165 // QUERY->add_sfun( QUERY, machine_opOverloadPush_impl, "void", "operatorsPush" );
166 // QUERY->doc_func( QUERY, "Push the operator overloading stack; use with care.");
167 //
168 // // add operatorsPop
169 // QUERY->add_sfun( QUERY, machine_opOverloadPop_impl, "void", "operatorsPop" );
170 // QUERY->doc_func( QUERY, "Pop the operator overloading stack; use with care.");
171 //
172 // // add operatorsStackLevel
173 // QUERY->add_sfun( QUERY, machine_opOverloadStackLevel_impl, "void", "operatorsStackLevel" );
174 // QUERY->doc_func( QUERY, "Get the current operator overloading stack level.");
175
176 // // add testSetup
177 // QUERY->add_sfun( QUERY, machine_testSetup_impl, "void", "testSetup" );
178 // QUERY->add_arg( QUERY, "Object", "obj" );
179 //
180 // // add testInvoke
181 // QUERY->add_sfun( QUERY, machine_testInvoke_impl, "void", "testInvoke" );
182 // QUERY->add_arg( QUERY, "Object", "obj" );
183 // QUERY->add_arg( QUERY, "float", "arg" );
184
185 // add status (legacy version of printStatus; has return value)
186 QUERY->add_sfun( QUERY, machine_printStatus_impl, "int", "status" );
187 QUERY->doc_func( QUERY, "Print the current status of the VM; legacy version of printStatus().");
188
189 // add printStatus
190 QUERY->add_sfun( QUERY, machine_printStatus_impl, "void", "printStatus" );
191 QUERY->doc_func( QUERY, "Print (to terminal or console) the current status of the VM.");
192
193 // add printTimeCheck
194 QUERY->add_sfun( QUERY, machine_printTimeCheck_impl, "void", "printTimeCheck" );
195 QUERY->doc_func( QUERY, "Print (to terminal or console) the current time information in the VM.");
196
197 // add crash
198 //! explicitly crash the virtual machine
199 QUERY->add_sfun( QUERY, machine_crash_impl, "void", "crash" );
200 QUERY->doc_func( QUERY, "Explicitly crash the virtual machine. The very last resort; or an emphatic gesture. Use with care." );
201
202 // add spork
203 //! same as add
204 // QUERY->add_sfun( QUERY, machine_add_impl, "int", "spork" );
205 // QUERY->add_arg( QUERY, "string", "path" );
206
207 // add shreds
208 //! get list of active shreds by id
209 QUERY->add_sfun( QUERY, machine_shreds_impl, "int[]", "shreds" );
210 QUERY->doc_func( QUERY, "Retrieve an array of active shred ids." );
211
212 // add numShreds
213 QUERY->add_sfun( QUERY, machine_numshreds_impl, "int", "numShreds" );
214 QUERY->doc_func( QUERY, "Get the number of shreds currently in the VM." );
215
216 // add eval
217 //! evaluate a string as ChucK code, compiling it and adding it to the the virtual machine
218 QUERY->add_sfun( QUERY, machine_eval_impl, "int", "eval" );
219 QUERY->doc_func( QUERY, "evaluate a string as ChucK code, compile it and immediately spork it as a new independent shred, and automatically yield the current shred to give the new shred a chance to run, without advancing time." );
220 QUERY->add_arg( QUERY, "string", "code" );
221
222 // add eval
223 //! evaluate a string as ChucK code, compiling it and adding it to the the virtual machine
224 QUERY->add_sfun( QUERY, machine_eval2_impl, "int", "eval" );
225 QUERY->doc_func( QUERY, "evaluate a string as ChucK code, with arguments (bundled in 'args' as \"ARG1:ARG2:...\", compile it and immediately spork it as a new independent shred, and automatically yield the current shred to give the new shred a chance to run, without advancing time." );
226 QUERY->add_arg( QUERY, "string", "code" );
227 QUERY->add_arg( QUERY, "string", "args" );
228
229 // add eval
230 //! evaluate a string as ChucK code, compiling it and adding it to the the virtual machine
231 QUERY->add_sfun( QUERY, machine_eval3_impl, "int", "eval" );
232 QUERY->doc_func( QUERY, "evaluate a string as ChucK code, with optional arguments (bundled in 'args' as \"ARG1:ARG2:...\", compile it and immediately spork 'count' independent shreds; and automatically yield the current shred to give all new shreds a chance to run, without advancing time." );
233 QUERY->add_arg( QUERY, "string", "code" );
234 QUERY->add_arg( QUERY, "string", "args" );
235 QUERY->add_arg( QUERY, "int", "count" );
236
237 // add get intsize (width)
238 //! get the intsize in bits (e.g., 32 or 64)
239 QUERY->add_sfun( QUERY, machine_intsize_impl, "int", "intsize" );
240 QUERY->doc_func( QUERY, "Return the bit size of an integer.");
241
242 // add check for silent (or realtime audio) | adapted from nshaheed PR #230
243 QUERY->add_sfun( QUERY, machine_silent_impl, "int", "silent" );
244 QUERY->doc_func( QUERY, "return false if the shred is in realtime mode, true if it's in silent mode (i.e. --silent is enabled)" );
245
246 // add check for realtime (always opposite of silent()) | adapted from nshaheed PR #230
247 QUERY->add_sfun( QUERY, machine_realtime_impl, "int", "realtime" );
248 QUERY->doc_func( QUERY, "return true if the shred is in realtime mode, false if it's in silent mode (i.e. --silent is enabled)" );
249
250 // get version string
251 QUERY->add_sfun( QUERY, machine_version_impl, "string", "version" );
252 QUERY->doc_func( QUERY, "return language and VM version string." );
253
254 // get set loglevel
255 QUERY->add_sfun( QUERY, machine_setloglevel_impl, "int", "loglevel" );
256 QUERY->add_arg( QUERY, "int", "level" );
257 QUERY->doc_func( QUERY, "set log level\n"
258 " |- 0: NONE\n"
259 " |- 1: CORE\n"
260 " |- 2: SYSTEM\n"
261 " |- 3: HERALD\n"
262 " |- 4: WARNING\n"
263 " |- 5: INFO\n"
264 " |- 6: DEBUG\n"
265 " |- 7: FINE\n"
266 " |- 8: FINER\n"
267 " |- 9: FINEST\n"
268 " |- 10: ALL" );
269
270 // get log level
271 QUERY->add_sfun( QUERY, machine_getloglevel_impl, "int", "loglevel" );
272 QUERY->doc_func( QUERY, "get log level." );
273
274 // get reference count for any object
275 QUERY->add_sfun( QUERY, machine_refcount_impl, "int", "refcount" );
276 QUERY->add_arg( QUERY, "Object", "obj" );
277 QUERY->doc_func( QUERY, "get an object's current internal reference count; this is intended for testing or curiosity; NOTE: this function intentionally does not take into account any reference counting related to the calling of this function (normally all functions increments the reference count for objects passed as arguments and decrements upon returning)" );
278
279 // explicitly trigger garbage collection pass | 1.5.2.0 (ge) added
280 // QUERY->add_sfun( QUERY, machine_gc, "void", "gc" );
281 // QUERY->doc_func( QUERY, "explicitly trigger a manual VM-level garbage collection pass; this will preemptively clean up eligible objects before automated garbage collection system does. Use with care." );
282
283 // get reg stack pointer
284 QUERY->add_sfun( QUERY, machine_regstack_impl, "int", "sp_reg" );
285 QUERY->doc_func( QUERY, "get the calling shred's operand (aka \"reg\"/register) stack pointer; intended for either debugging or curiosity." );
286
287 // get mem stack pointer
288 QUERY->add_sfun( QUERY, machine_memstack_impl, "int", "sp_mem" );
289 QUERY->doc_func( QUERY, "get the calling shred's memory (aka \"mem\") stack pointer; intended for either debugging or curiosity." );
290
291 // get the current platform
292 QUERY->add_sfun( QUERY, machine_platform_name_impl, "string", "os" );
293 QUERY->doc_func( QUERY, "get the underlying operating system name; possible values include \"mac\", \"linux\", \"windows\", \"web\", \"ios\", \"android\", \"unknown\"" );
294
295 // get the system time of day
296 QUERY->add_sfun( QUERY, machine_timeofday_impl, "float", "timeOfDay" );
297 QUERY->doc_func( QUERY,
298 "returns the time in seconds since the Epoch 1970-01-01 00:00:00 +0000 (UTC). "
299 "The returned time has microsecond resolution and could be used for e.g. synchronizing "
300 "events across machines without the use of networking. "
301 );
302
303 // get the system time of day as a vec2(seconds, microseconds)
304 QUERY->add_sfun( QUERY, machine_timeofday_precise_impl, "vec2", "timeOfDay2" );
305 QUERY->doc_func( QUERY,
306 "identical to Machine.timeOfDay(), only the return value is a vec2 where "
307 "x is time in seconds, and y is fractional time in microseconds. "
308 "y is guaranteed to be between 0 and 1,000,000. x + y yields the total time. "
309 "Useful for situations needing less than 64-bit precision e.g., floats in OSC are "
310 "32-bit and one must send the x and y values separately -- "
311 "otherwise a significant amount of timing resolution would be lost."
312 );
313
314 // add examples
315 QUERY->add_ex( QUERY, "machine/eval.ck" );
316 QUERY->add_ex( QUERY, "machine/eval-global.ck" );
317 QUERY->add_ex( QUERY, "machine/intsize.ck" );
318 QUERY->add_ex( QUERY, "machine/is-realtime.ck" );
319 QUERY->add_ex( QUERY, "machine/machine-help.ck" );
320 QUERY->add_ex( QUERY, "machine/machine-shred.ck" );
321 QUERY->add_ex( QUERY, "machine/platform.ck" );
322 QUERY->add_ex( QUERY, "machine/version.ck" );
323 QUERY->add_ex( QUERY, "book/digital-artists/chapter9/DrumMachine" );
324 QUERY->add_ex( QUERY, "book/digital-artists/chapter9/SmartMandolin/initialize.ck" );
325
326 // end class
327 QUERY->end_class( QUERY );
328
329 return TRUE;
330 }
331
332
333
334
335 //-----------------------------------------------------------------------------
336 // name: machine_intsize()
337 // desc: integer size in bits
338 //-----------------------------------------------------------------------------
339 t_CKUINT machine_intsize()
340 {
341 return sizeof(t_CKINT) * 8;
342 }
343
344
345
346
347 //-----------------------------------------------------------------------------
348 // name: machine_realtime()
349 // desc: return whether chuck is in realtime mode, or silent mode (--silent is enabled)
350 //-----------------------------------------------------------------------------
351 t_CKUINT machine_realtime( Chuck_VM * vm )
352 {
353 return vm->carrier()->hintIsRealtimeAudio();
354 }
355
356
357
358
359 //-----------------------------------------------------------------------------
360 // name: machine_eval()
361 // desc: evaluate a string as ChucK code, compiling it and adding it to the the virtual machine
362 //-----------------------------------------------------------------------------
363 t_CKUINT machine_eval( Chuck_String * codeStr, Chuck_String * argsTogether,
364 t_CKINT count, Chuck_VM_Shred * evaluator )
365 {
366 // check if null
367 if( codeStr == NULL ) return FALSE;
368
369 // get VM
370 Chuck_VM * vm = evaluator->vm_ref;
371 // get ChucK instance
372 ChucK * chuck = vm->carrier()->chuck;
373 // get code as string
374 string code = codeStr->str();
375 // get args, if there
376 string args = argsTogether ? argsTogether->str() : "";
377 // get filepath to use as optional filepath
378 // (e.g., for @import statements in the eval'ed code)
379 string optFilepath = evaluator->code ? evaluator->code->filename : "";
380
381 // compile and spork | 1.5.0.5 (ge) immediate=TRUE
382 t_CKUINT retval = chuck->compileCode( code, args, count, TRUE, NULL, optFilepath );
383 // automatically yield current shred to let new code run | 1.5.0.5 (ge)
384 evaluator->yield();
385
386 // return
387 return retval;
388 }
389
390 // add
391 CK_DLL_SFUN( machine_add_impl )
392 {
393 // get arg
394 Chuck_String * ckstr = GET_CK_STRING(ARGS);
395 // check arg; return (0 by default)
396 if( !ckstr ) return;
397
398 // get string
399 string path = ckstr->str();
400 // filename
401 string filename;
402 // arguments
403 string args;
404 // extract args FILE:arg1:arg2:arg3
405 extract_args( path, filename, args );
406
407 // compile but don't run yet (instance == 0)
408 if( !VM->carrier()->chuck->compileFile( filename, args, 0 ) ) return;
409
410 // construct chuck msg (must allocate on heap, as VM will clean up)
411 Chuck_Msg * msg = new Chuck_Msg();
412 // set type
413 msg->type = CK_MSG_ADD;
414 // set code
415 msg->code = VM->carrier()->compiler->output();
416 // create args array
417 msg->args = new vector<string>;
418 // extract args again but this time into vector
419 extract_args( path, filename, *(msg->args) );
420 // process ADD message, return new shred ID
421 RETURN->v_int = VM->process_msg( msg );
422 }
423
424 // remove
425 CK_DLL_SFUN( machine_remove_impl )
426 {
427 // get the shred ID to remove
428 t_CKINT v = GET_CK_INT(ARGS);
429
430 // construct chuck msg (must allocate on heap, as VM will clean up)
431 Chuck_Msg * msg = new Chuck_Msg();
432 // set type
433 msg->type = CK_MSG_REMOVE;
434 // set id to remove
435 msg->param = v;
436 // process the message
437 RETURN->v_int = VM->process_msg( msg );
438 }
439
440 // removeLast
441 CK_DLL_SFUN( machine_removeLast_impl )
442 {
443 // construct chuck msg (must allocate on heap, as VM will clean up)
444 Chuck_Msg * msg = new Chuck_Msg();
445 // set type
446 msg->type = CK_MSG_REMOVE;
447 // set special value to signify remove last shred
448 msg->param = CK_NO_VALUE;
449 // process the message
450 RETURN->v_int = VM->process_msg( msg );
451 }
452
453 // removeLast
454 CK_DLL_SFUN( machine_removeAll_impl )
455 {
456 // construct chuck msg (must allocate on heap, as VM will clean up)
457 Chuck_Msg * msg = new Chuck_Msg();
458 // set type
459 msg->type = CK_MSG_REMOVEALL;
460 // process the message
461 VM->process_msg( msg );
462 }
463
464 // replace
465 CK_DLL_SFUN( machine_replace_impl )
466 {
467 // shred ID to replace
468 t_CKINT v = GET_NEXT_INT(ARGS);
469 // incoming file
470 Chuck_String * ckstr = GET_NEXT_STRING(ARGS);
471 // check and if NULL return (0 by default)
472 if( !ckstr ) return;
473
474 // get string
475 string path = ckstr->str();
476 // filename
477 string filename;
478 // arguments
479 string args;
480 // extract args FILE:arg1:arg2:arg3
481 extract_args( path, filename, args );
482
483 // compile but don't run yet (instance == 0)
484 if( !VM->carrier()->chuck->compileFile( filename, args, 0 ) ) return;
485
486 // construct chuck msg (must allocate on heap, as VM will clean up)
487 Chuck_Msg * msg = new Chuck_Msg();
488 // set type
489 msg->type = CK_MSG_REPLACE;
490 // set shred id to replace
491 msg->param = v;
492 // set code for incoming shred
493 msg->code = VM->carrier()->compiler->output();
494 // create args array
495 msg->args = new vector<string>;
496 // extract args again but this time into vector
497 extract_args( path, filename, *(msg->args) );
498 // process ADD message, return new shred ID
499 RETURN->v_int = VM->process_msg( msg );
500 }
501
502 // printStatus (and also for status)
503 CK_DLL_SFUN( machine_printStatus_impl )
504 {
505 // construct chuck msg (must allocate on heap, as VM will clean up)
506 Chuck_Msg * msg = new Chuck_Msg();
507 // set type
508 msg->type = CK_MSG_STATUS;
509 // process the message; set return for status() which returns an int
510 RETURN->v_int = (VM->process_msg( msg ) != 0);
511 }
512
513 // printTimeCheck
514 CK_DLL_SFUN( machine_printTimeCheck_impl )
515 {
516 // construct chuck msg (must allocate on heap, as VM will clean up)
517 Chuck_Msg * msg = new Chuck_Msg();
518 // set type
519 msg->type = CK_MSG_TIME;
520 // process the message
521 VM->process_msg( msg );
522 }
523
524 // resetID
525 CK_DLL_SFUN( machine_resetID_impl )
526 {
527 // construct chuck msg (must allocate on heap, as VM will clean up)
528 Chuck_Msg * msg = new Chuck_Msg();
529 // set type
530 msg->type = CK_MSG_RESET_ID;
531 // process the message
532 RETURN->v_int = VM->process_msg( msg );
533 }
534
535 // removeLast
536 CK_DLL_SFUN( machine_clearVM_impl )
537 {
538 // construct chuck msg (must allocate on heap, as VM will clean up)
539 Chuck_Msg * msg = new Chuck_Msg();
540 // set type
541 msg->type = CK_MSG_CLEARVM;
542 // process the message
543 VM->process_msg( msg );
544 }
545
546 // intsize
547 CK_DLL_SFUN( machine_intsize_impl )
548 {
549 RETURN->v_int = machine_intsize();
550 }
551
552 // realtime
553 CK_DLL_SFUN( machine_realtime_impl )
554 {
555 RETURN->v_int = machine_realtime( SHRED->vm_ref );
556 }
557
558 // silent
559 CK_DLL_SFUN( machine_silent_impl )
560 {
561 RETURN->v_int = !machine_realtime( SHRED->vm_ref );
562 }
563
564 CK_DLL_SFUN( machine_shreds_impl )
565 {
566 Chuck_ArrayInt *array = new Chuck_ArrayInt(FALSE);
567 initialize_object(array, SHRED->vm_ref->env()->ckt_array, SHRED, VM);
568 array->clear();
569
570 Chuck_VM_Status status;
571 SHRED->vm_ref->shreduler()->status(&status);
572
573 for(int i = 0; i < status.list.size(); i++)
574 array->push_back(status.list[i]->xid);
575
576 status.clear();
577
578 RETURN->v_object = array;
579 }
580
581 CK_DLL_SFUN( machine_numshreds_impl )
582 {
583 // vector of shreds
584 vector<Chuck_VM_Shred *> shreds;
585 // get all shreds in the VM
586 SHRED->vm_ref->shreduler()->get_all_shreds( shreds );
587 // return the size
588 RETURN->v_int = shreds.size();
589 }
590
591 CK_DLL_SFUN( machine_eval_impl )
592 {
593 // get arguments
594 Chuck_String * code = GET_NEXT_STRING(ARGS);
595
596 RETURN->v_int = machine_eval( code, NULL, 1, SHRED );
597 }
598
599 CK_DLL_SFUN( machine_eval2_impl )
600 {
601 // get arguments
602 Chuck_String * code = GET_NEXT_STRING(ARGS);
603 Chuck_String * argsTogether = GET_NEXT_STRING(ARGS);
604
605 RETURN->v_int = machine_eval( code, argsTogether, 1, SHRED );
606 }
607
608 CK_DLL_SFUN( machine_eval3_impl )
609 {
610 // get arguments
611 Chuck_String * code = GET_NEXT_STRING(ARGS);
612 Chuck_String * argsTogether = GET_NEXT_STRING(ARGS);
613 t_CKINT count = GET_NEXT_INT(ARGS);
614
615 RETURN->v_int = machine_eval( code, argsTogether, count, SHRED );
616 }
617
618 CK_DLL_SFUN( machine_version_impl )
619 {
620 // get version string
621 string vs = VM->carrier()->chuck->version();
622 // make chuck string
623 Chuck_String * s = new Chuck_String( vs );
624 // initialize
625 initialize_object(s, VM->carrier()->env->ckt_string, SHRED, VM );
626 // return
627 RETURN->v_string = s;
628 }
629
630 CK_DLL_SFUN( machine_setloglevel_impl )
631 {
632 // arg
633 t_CKINT level = GET_NEXT_INT(ARGS);
634 // get current log
635 t_CKINT current = VM->carrier()->chuck->getLogLevel();
636 // clamp compare
637 if( level < CK_LOG_NONE ) level = CK_LOG_NONE;
638 else if( level > CK_LOG_ALL ) level = CK_LOG_ALL;
639 // compare
640 if( level != current )
641 {
642 EM_log( CK_LOG_NONE, "updating log level from %d to %d...", current, level );
643 VM->carrier()->chuck->setLogLevel( level );
644 }
645 // return
646 RETURN->v_int = level;
647 }
648
649 CK_DLL_SFUN( machine_getloglevel_impl )
650 {
651 // return
652 RETURN->v_int = VM->carrier()->chuck->getLogLevel();
653 }
654
655 CK_DLL_SFUN( machine_refcount_impl )
656 {
657 // get the argument
658 Chuck_Object * obj = (Chuck_Object *)GET_NEXT_OBJECT(ARGS);
659 // return (-1 to account for the extra refcount as part of calling this function!)
660 RETURN->v_int = obj != NULL ? obj->m_ref_count-1 : 0;
661 }
662
663 CK_DLL_SFUN( machine_regstack_impl )
664 {
665 // return reg stack pointer
666 RETURN->v_int = (t_CKINT)SHRED->reg->sp;
667 }
668
669 CK_DLL_SFUN( machine_memstack_impl )
670 {
671 // return mem stack pointer
672 RETURN->v_int = (t_CKINT)SHRED->mem->sp;
673 }
674
675 CK_DLL_SFUN( machine_platform_name_impl )
676 {
677 // make chuck string
678 Chuck_String * s = new Chuck_String( CHUCK_PLATFORM_STRING );
679 // initialize
680 initialize_object(s, VM->carrier()->env->ckt_string, SHRED, VM );
681 // return
682 RETURN->v_string = s;
683 }
684
685 CK_DLL_SFUN( machine_timeofday_impl )
686 {
687 long tv_sec = 0;
688 long tv_usec = 0;
689
690 // get time of day
691 if( ck_gettimeofday( &tv_sec, &tv_usec, NULL ) != 0 )
692 {
693 CK_FPRINTF_STDERR( "[chuck]: Machine.timeOfDay() failed...\n" );
694 RETURN->v_float = 0;
695 return;
696 }
697
698 // return value
699 RETURN->v_float = tv_sec + (tv_usec / 1000000.0);
700 }
701
702 CK_DLL_SFUN( machine_timeofday_precise_impl )
703 {
704 long tv_sec = 0;
705 long tv_usec = 0;
706
707 // get time of day
708 if( ck_gettimeofday( &tv_sec, &tv_usec, NULL ) != 0 )
709 {
710 CK_FPRINTF_STDERR( "[chuck]: Machine.timeOfDayPrecise() failed...\n" );
711 RETURN->v_vec2.x = 0;
712 RETURN->v_vec2.y = 0;
713 return;
714 }
715
716 // return value
717 RETURN->v_vec2.x = tv_sec;
718 RETURN->v_vec2.y = tv_usec;
719 }
720
721 // gc() | 1.5.2.0 (ge)
722 CK_DLL_SFUN( machine_gc )
723 {
724 // invoke
725 VM->gc();
726 }
727
728 CK_DLL_SFUN( machine_opOverloadPush_impl)
729 {
730 VM->env()->op_registry.push();
731 }
732
733 CK_DLL_SFUN( machine_opOverloadPop_impl)
734 {
735 VM->env()->op_registry.pop();
736 }
737
738 CK_DLL_SFUN( machine_opOverloadReset_impl)
739 {
740 VM->env()->op_registry.reset2public();
741 }
742
743 CK_DLL_SFUN( machine_opOverloadStackLevel_impl)
744 {
745 RETURN->v_int = VM->env()->op_registry.stackLevel();
746 }
747
748 // add
749 CK_DLL_SFUN( machine_crash_impl )
750 {
751 CK_FPRINTF_STDERR( "[chuck]: crashing...\n" );
752 *(volatile int *)0 = 0;
753 }
754
755
756 // TEST
757 //Chuck_VM_MFunInvoker * g_testInvoker;
758 //
759 //CK_DLL_SFUN( machine_testSetup_impl )
760 //{
761 // Chuck_Object * obj = GET_NEXT_OBJECT(ARGS);
762 //
763 // g_testInvoker = new Chuck_VM_MFunInvoker;
764 //
765 // t_CKINT func_vt_offset = -1;
766 // Chuck_Func * func = NULL;
767 //
768 // for(t_CKINT i = 0; i < obj->vtable->funcs.size(); i++)
769 // {
770 // func = obj->vtable->funcs[i];
771 // if(func->name.find("update") == 0 &&
772 // // ensure has one argument
773 // func->def()->arg_list != NULL &&
774 // // ensure first argument is float
775 // func->def()->arg_list->type == SHRED->vm_ref->env()->ckt_float &&
776 // // ensure has only one argument
777 // func->def()->arg_list->next == NULL &&
778 // // ensure returns float
779 // func->def()->ret_type == SHRED->vm_ref->env()->ckt_void )
780 // {
781 // func_vt_offset = i;
782 // break;
783 // }
784 // }
785 //
786 // // not found
787 // if( func_vt_offset < 0 )
788 // {
789 // EM_error3( "update() not found yo" );
790 // return;
791 // }
792 //
793 // // set up invoker
794 // g_testInvoker->setup( func, func_vt_offset, VM, SHRED );
795 //}
796 //
797 //CK_DLL_SFUN( machine_testInvoke_impl )
798 //{
799 // Chuck_Object * obj = GET_NEXT_OBJECT(ARGS);
800 // t_CKFLOAT v = GET_NEXT_FLOAT(ARGS);
801 //
802 // Chuck_DL_Arg arg;
803 // arg.kind = kindof_FLOAT;
804 // arg.value.v_float = v;
805 // vector<Chuck_DL_Arg> args;
806 // args.push_back(arg);
807 //
808 // g_testInvoker->invoke(obj, args );
809 //
810 // CK_SAFE_DELETE( g_testInvoker );
811 //}