Debian Codegraph @

src/core/chuck_lang.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 // name: chuck_lang.cpp
28 // desc: chuck class library base
29 //
30 // authors: Ge Wang ([email protected] | [email protected])
31 // Spencer Salazar ([email protected])
32 // Ananya Misra ([email protected])
33 // Andrew Schran ([email protected])
34 // date: spring 2005
35 //-----------------------------------------------------------------------------
36 #include "chuck_lang.h"
37 #include "chuck_type.h"
38 #include "chuck_instr.h"
39 #include "chuck_vm.h"
40 #include "chuck_errmsg.h"
41 #include "chuck_ugen.h"
42 #include "util_string.h"
43
44 #include <math.h>
45 #include <iostream>
46 #include <iomanip>
47 #include <sstream>
48 using namespace std;
49
50 // dac tick
51 CK_DLL_TICK(__ugen_tick) { *out = in; return TRUE; }
52 // object string offset
53 static t_CKUINT Object_offset_string = 0;
54
55
56
57
58 //-----------------------------------------------------------------------------
59 // name: init_class_object()
60 // desc: initialize the base Object class
61 //-----------------------------------------------------------------------------
62 t_CKBOOL init_class_object( Chuck_Env * env, Chuck_Type * type )
63 {
64 Chuck_DL_Func * func = NULL;
65 Chuck_Value * value = NULL;
66
67 // log
68 EM_log( CK_LOG_HERALD, "class 'Object'" );
69
70 const char * doc = "base class for all class types in ChucK.";
71
72 // init as base class
73 if( !type_engine_import_class_begin( env, type, env->global(), object_ctor, object_dtor, doc ) )
74 return FALSE;
75
76 // add member to hold string
77 Object_offset_string = type_engine_import_mvar( env, "string", "@string", FALSE );
78 if( Object_offset_string == CK_INVALID_OFFSET ) goto error;
79
80 // add toString()
81 func = make_new_mfun( "string", "toString", object_toString );
82 func->doc = "get a textual description of this object.";
83 if( !type_engine_import_mfun( env, func ) ) goto error;
84
85 // add help()
86 func = make_new_sfun( "void", "help", object_help );
87 func->doc = "output helpful information about a class or an instance thereof.";
88 if( !type_engine_import_sfun( env, func ) ) goto error;
89
90 // add typeOf() // 1.5.0.0
91 func = make_new_sfun( "Type", "typeOf", object_typeInfo );
92 func->doc = "get the type of this object (or class).";
93 if( !type_engine_import_sfun( env, func ) ) goto error;
94
95 // add typeOfInstance() // 1.5.4.0 (nshaheed, ge) added
96 func = make_new_mfun( "Type", "typeOfInstance", object_typeInstanceInfo );
97 func->doc = "get the instanced type of this object.";
98 if( !type_engine_import_mfun( env, func ) ) goto error;
99
100 // // add dump()
101 // func = make_new_mfun( "void", "dump", object_dump );
102 // func->doc = "output current state of the object.";
103 // if( !type_engine_import_mfun( env, func ) ) goto error;
104
105 // end the class import
106 type_engine_import_class_end( env );
107
108 // find the offset for can_wait
109 value = type_engine_find_value( type, "toString" );
110 assert( value != NULL );
111 assert( value->func_ref != NULL );
112 // remember it
113 Chuck_Event::our_vt_toString = value->func_ref->vt_index;
114
115 return TRUE;
116
117 error:
118
119 // end the class import
120 type_engine_import_class_end( env );
121
122 return FALSE;
123 }
124
125
126
127
128 //-----------------------------------------------------------------------------
129 // name: init_class_ugen()
130 // desc: ...
131 //-----------------------------------------------------------------------------
132 t_CKBOOL init_class_ugen( Chuck_Env * env, Chuck_Type * type )
133 {
134 Chuck_DL_Func * func = NULL;
135
136 EM_log( CK_LOG_HERALD, "class 'UGen'" );
137
138 // add ugen info
139 type->ugen_info = new Chuck_UGen_Info;
140 type->ugen_info->add_ref();
141 type->ugen_info->tick = __ugen_tick;
142 type->ugen_info->num_ins = 1;
143 type->ugen_info->num_outs = 1;
144 // documentation text
145 const char * doc = "base class for all unit generator (UGen) types.";
146
147 // init as base class
148 // TODO: ctor/dtor, ugen's sometimes created internally?
149 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
150 return FALSE;
151
152 // gain
153 func = make_new_mfun( "float", "gain", ugen_gain );
154 func->add_arg( "float", "val" );
155 func->doc = "set the gain of the unit generator.";
156 if( !type_engine_import_mfun( env, func ) ) goto error;
157 func = make_new_mfun( "float", "gain", ugen_cget_gain );
158 func->doc = "get the gain of the unit generator.";
159 if( !type_engine_import_mfun( env, func ) ) goto error;
160
161 /* // next
162 func = make_new_mfun( "float", "next", ugen_next );
163 func->add_arg( "float", "val" );
164 if( !type_engine_import_mfun( env, func ) ) goto error;
165 func = make_new_mfun( "float", "next", ugen_cget_next );
166 if( !type_engine_import_mfun( env, func ) ) goto error;
167 */
168 // last
169 func = make_new_mfun( "float", "last", ugen_last );
170 func->doc = "get the last sample value of the unit generator.";
171 if( !type_engine_import_mfun( env, func ) ) goto error;
172
173 // op
174 func = make_new_mfun( "int", "op", ugen_op );
175 func->add_arg( "int", "val" );
176 func->doc = "set the unit generator's operation mode. Accepted values are: 1 (sum inputs), 2 (take difference between first input and subsequent inputs), 3 (multiply inputs), 4 (divide first input by subsequent inputs), 0 (do not synthesize audio, output 0) or -1 (passthrough inputs to output).";
177 if( !type_engine_import_mfun( env, func ) ) goto error;
178 func = make_new_mfun( "int", "op", ugen_cget_op );
179 func->doc = "get the unit generator's operation mode.";
180 if( !type_engine_import_mfun( env, func ) ) goto error;
181
182 // add numChannels
183 func = make_new_mfun( "int", "channels", ugen_numChannels );
184 func->add_arg( "int", "num" );
185 func->doc = "set number of channels. (currently NOT supported)";
186 if( !type_engine_import_mfun( env, func ) ) goto error;
187 func = make_new_mfun( "int", "channels", ugen_cget_numChannels );
188 func->doc = "get number of channels.";
189 if( !type_engine_import_mfun( env, func ) ) goto error;
190
191 // add chan
192 func = make_new_mfun( "UGen", "chan", ugen_chan );
193 func->add_arg( "int", "num" );
194 func->doc = "get channel (as a UGen) at specified index.";
195 if( !type_engine_import_mfun( env, func ) ) goto error;
196
197 // add isConnectedTo
198 func = make_new_mfun( "int", "isConnectedTo", ugen_connected );
199 func->add_arg( "UGen", "rhs" );
200 func->doc = "return true if this UGen's output is connected to the input of rhs; if either this UGen or rhs has more than one channel, this function returns true if any connections exist between the channels; return false if there are no connections.";
201 if( !type_engine_import_mfun( env, func ) ) goto error;
202
203 // add buffered
204 func = make_new_mfun( "int", "buffered", ugen_buffered );
205 func->add_arg( "int", "val" );
206 func->doc = "set the unit generator's buffered operation mode, typically used externally from hosts that embed ChucK as a component. If true, the UGen stores a buffer of its most recent samples, which can be fetched using global variables in the host language.";
207 if( !type_engine_import_mfun( env, func ) ) goto error;
208 func = make_new_mfun( "int", "buffered", ugen_cget_buffered );
209 func->doc = "get the ugen's buffered operation mode.";
210 if( !type_engine_import_mfun( env, func ) ) goto error;
211
212 // // add attachToOriginShred
213 // func = make_new_mfun( "Shred", "attachToOriginShred", ugen_originShred );
214 // func->add_arg( "Shred", "shred" );
215 // func->doc = "(Use with care) by default, a UGen is attached to its origin Shred (the Shred the UGen was created on); if that Shred is removed (e.g., by Machine.remove()), it will disconnect its UGens' audio connections. This methods makes it possible for UGen to be attached to either a different Shred or to no Shred if `null` is passed in. The latter makes this UGen a kind of \"free agent\" that is subject to the garbage collector as usual, but no longer to a origin Shred. This is useful ";
216 // if( !type_engine_import_mfun( env, func ) ) goto error;
217
218 // end
219 type_engine_import_class_end( env );
220
221 return TRUE;
222
223 error:
224
225 // end the class import
226 type_engine_import_class_end( env );
227
228 return FALSE;
229 }
230
231
232
233
234 // virtual table offset
235 static t_CKINT uana_offset_blob = 0;
236 //-----------------------------------------------------------------------------
237 // name: init_class_uana()
238 // desc: ...
239 //-----------------------------------------------------------------------------
240 t_CKBOOL init_class_uana( Chuck_Env * env, Chuck_Type * type )
241 {
242 Chuck_DL_Func * func = NULL;
243
244 EM_log( CK_LOG_HERALD, "class 'UAna'" );
245
246 // add uana info
247 type->ugen_info = new Chuck_UGen_Info;
248 type->ugen_info->add_ref();
249 type->ugen_info->tick = __ugen_tick;
250 type->ugen_info->num_ins = 1;
251 type->ugen_info->num_outs = 1;
252
253 const char * doc = "base class from which all unit analyzer (UAna) types inherit; UAnae (note plural form) can be interconnected using => (chuck operator for synthesis; all UAnae are also UGens) or using =^ (upchuck operator for analysis) -- the operator used will determine how data is passed. When .upchuck() is invoked on a given UAna, the UAna-chain (i.e., UAnae connected via =^) is traversed upstream from the upchucked UAna, and analysis is performed at each UAna along the chain; the analysis results are returned in UAnaBlobs.";
254
255 // init as base class, type should already know the parent type
256 // TODO: ctor/dtor, ugen's sometimes created internally?
257 if( !type_engine_import_class_begin( env, type, env->global(), uana_ctor, uana_dtor, doc ) )
258 return FALSE;
259
260 // add variables
261 uana_offset_blob = type_engine_import_mvar( env, "int", "@m_blobproxy", FALSE );
262 if( uana_offset_blob == CK_INVALID_OFFSET ) goto error;
263
264 // add upchuck
265 func = make_new_mfun( "UAnaBlob", "upchuck", uana_upchuck );
266 func->doc = "initiate analysis at the UAna and return result.";
267 if( !type_engine_import_mfun( env, func ) ) goto error;
268
269 // add fvals
270 func = make_new_mfun( "float[]", "fvals", uana_fvals );
271 func->doc = "get blob's float array.";
272 if( !type_engine_import_mfun( env, func ) ) goto error;
273
274 // add cvals
275 func = make_new_mfun( "complex[]", "cvals", uana_cvals );
276 func->doc = "get blob's complex array.";
277 if( !type_engine_import_mfun( env, func ) ) goto error;
278
279 // add fval
280 func = make_new_mfun( "float", "fval", uana_fval );
281 func->add_arg( "int", "index" );
282 func->doc = "get blob's float value at index.";
283 if( !type_engine_import_mfun( env, func ) ) goto error;
284
285 // add cval
286 func = make_new_mfun( "complex", "cval", uana_cval );
287 func->add_arg( "int", "index" );
288 func->doc = "get blob's complex value at index.";
289 if( !type_engine_import_mfun( env, func ) ) goto error;
290
291 // add isUpConnectedTo
292 func = make_new_mfun( "int", "isUpConnectedTo", uana_connected );
293 func->add_arg( "UAna", "right" );
294 func->doc = "is connected to another uana via =^?";
295 if( !type_engine_import_mfun( env, func ) ) goto error;
296
297 // TODO: add nonchuck
298 // func = make_new_mfun( "void", "nonchuck", uana_nonchuck );
299 // if( !type_engine_import_mfun( env, func ) ) goto error;
300
301 // TODO: add event
302 // func = make_new_mfun( "Event", "event", uana_event );
303 // if( !type_engine_import_mfun( env, func ) ) goto error;
304
305 // end
306 type_engine_import_class_end( env );
307
308 return TRUE;
309
310 error:
311
312 // end the class import
313 type_engine_import_class_end( env );
314
315 return FALSE;
316 }
317
318
319
320
321 // virtual table offset
322 static t_CKINT uanablob_offset_when = 0;
323 static t_CKINT uanablob_offset_fvals = 0;
324 static t_CKINT uanablob_offset_cvals = 0;
325 //-----------------------------------------------------------------------------
326 // name: init_class_blob()
327 // desc: ...
328 //-----------------------------------------------------------------------------
329 t_CKBOOL init_class_blob( Chuck_Env * env, Chuck_Type * type )
330 {
331 Chuck_DL_Func * func = NULL;
332 // Chuck_Value * value = NULL;
333
334 // log
335 EM_log( CK_LOG_HERALD, "class 'UAnaBlob'" );
336
337 const char * doc = "a data structure that contains results associated with UAna analysis. There is a UAnaBlob associated with every UAna. As a UAna is upchucked (using .upchuck()), the result is stored in the UAnaBlob's floating point vector and/or complex vector. The interpretation of the results depends on the specific UAna.";
338
339 // init class
340 // TODO: ctor/dtor
341 if( !type_engine_import_class_begin( env, type, env->global(), uanablob_ctor, uanablob_dtor, doc ) )
342 return FALSE;
343
344 // add variables
345 uanablob_offset_when = type_engine_import_mvar( env, "time", "m_when", FALSE );
346 if( uanablob_offset_when == CK_INVALID_OFFSET ) goto error;
347 uanablob_offset_fvals = type_engine_import_mvar( env, "float[]", "m_fvals", FALSE );
348 if( uanablob_offset_fvals == CK_INVALID_OFFSET ) goto error;
349 uanablob_offset_cvals = type_engine_import_mvar( env, "complex[]", "m_cvals", FALSE );
350 if( uanablob_offset_cvals == CK_INVALID_OFFSET ) goto error;
351
352 // add when
353 func = make_new_mfun( "time", "when", uanablob_when );
354 func->doc = "get the time when blob was last upchucked.";
355 if( !type_engine_import_mfun( env, func ) ) goto error;
356
357 // add fvals
358 func = make_new_mfun( "float[]", "fvals", uanablob_fvals );
359 func->doc = "get blob's float array.";
360 if( !type_engine_import_mfun( env, func ) ) goto error;
361
362 // add cvals
363 func = make_new_mfun( "complex[]", "cvals", uanablob_cvals );
364 func->doc = "get blob's complex array.";
365 if( !type_engine_import_mfun( env, func ) ) goto error;
366
367 // add fval
368 func = make_new_mfun( "float", "fval", uanablob_fval );
369 func->add_arg( "int", "index" );
370 func->doc = "get blob's float value at index.";
371 if( !type_engine_import_mfun( env, func ) ) goto error;
372
373 // add cval
374 func = make_new_mfun( "complex", "cval", uanablob_cval );
375 func->add_arg( "int", "index" );
376 func->doc = "get blob's complex value at index.";
377 if( !type_engine_import_mfun( env, func ) ) goto error;
378
379 // end class import
380 type_engine_import_class_end( env );
381
382 // done
383 return TRUE;
384
385 error:
386
387 // end class import
388 type_engine_import_class_end( env );
389
390 // error
391 return FALSE;
392 }
393
394
395
396
397 // static t_CKUINT event_offset_data = 0;
398 //-----------------------------------------------------------------------------
399 // name: init_class_event()
400 // desc: ...
401 //-----------------------------------------------------------------------------
402 t_CKBOOL init_class_event( Chuck_Env * env, Chuck_Type * type )
403 {
404 Chuck_DL_Func * func = NULL;
405 Chuck_Value * value = NULL;
406
407 // log
408 EM_log( CK_LOG_HERALD, "class 'Event'" );
409
410 const char *doc = "a mechanism for precise synchronization across shreds.";
411
412 // init as base class
413 // TODO: ctor/dtor?
414 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
415 return FALSE;
416
417 // add signal()
418 func = make_new_mfun( "void", "signal", event_signal );
419 func->doc = "signal one shred that is waiting on this event.";
420 if( !type_engine_import_mfun( env, func ) ) goto error;
421
422 // add broadcast()
423 func = make_new_mfun( "void", "broadcast", event_broadcast );
424 func->doc = "signal all shreds that are waiting on this event.";
425 if( !type_engine_import_mfun( env, func ) ) goto error;
426
427 // add wait() | commented out 1.4.1.0 (ge) -- it doesn't DO anything!!!
428 // func = make_new_mfun( "void", "wait", event_wait );
429 // func->add_arg( "Shred", "me" );
430 // func->doc = "(currently unsupported; use Event => now to wait on event)";
431 // if( !type_engine_import_mfun( env, func ) ) goto error;
432
433 // add can_wait()
434 func = make_new_mfun( "int", "can_wait", event_can_wait );
435 func->doc = "can the event can be waited on? (internal) used by virtual machine for synchronization.";
436 if( !type_engine_import_mfun( env, func ) ) goto error;
437
438 // add waiting_on()
439 func = make_new_mfun( "void", "waiting_on", event_waiting_on );
440 func->doc = "(internal) used by virtual machine to be notified when a shred starts waiting on this Event.";
441 if( !type_engine_import_mfun( env, func ) ) goto error;
442
443 // add examples
444 if( !type_engine_import_add_ex( env, "event/broadcast.ck" ) ) goto error;
445 if( !type_engine_import_add_ex( env, "event/signal.ck" ) ) goto error;
446 if( !type_engine_import_add_ex( env, "event/signal4.ck" ) ) goto error;
447 if( !type_engine_import_add_ex( env, "event/event-extend.ck" ) ) goto error;
448 if( !type_engine_import_add_ex( env, "event/event-extend2.ck" ) ) goto error;
449 if( !type_engine_import_add_ex( env, "event/event-x-bpm-1.ck" ) ) goto error;
450 if( !type_engine_import_add_ex( env, "event/event-x-bpm-2.ck" ) ) goto error;
451
452 // if( !type_engine_import_add_ex( env, "hid/joy.ck" ) ) goto error;
453 // if( !type_engine_import_add_ex( env, "hid/kb.ck" ) ) goto error;
454 // if( !type_engine_import_add_ex( env, "midi/gomidi.ck" ) ) goto error;
455 // if( !type_engine_import_add_ex( env, "midi/polyfony.ck" ) ) goto error;
456 // if( !type_engine_import_add_ex( env, "midi/polyfony2.ck" ) ) goto error;
457 // if( !type_engine_import_add_ex( env, "osc/r.ck" ) ) goto error;
458
459 // end the class import
460 type_engine_import_class_end( env );
461
462 // find the offset for can_wait
463 value = type_engine_find_value( type, "can_wait" );
464 assert( value != NULL );
465 assert( value->func_ref != NULL );
466 // remember it
467 Chuck_Event::our_can_wait = value->func_ref->vt_index;
468
469 // find the offset for waiting_on
470 value = type_engine_find_value( type, "waiting_on" );
471 assert( value != NULL );
472 assert( value->func_ref != NULL );
473 // remember it
474 Chuck_Event::our_waiting_on = value->func_ref->vt_index;
475
476 return TRUE;
477
478 error:
479
480 // end the class import
481 type_engine_import_class_end( env );
482
483 return FALSE;
484 }
485
486
487
488
489 // static t_CKUINT shred_offset_args = 0;
490 //-----------------------------------------------------------------------------
491 // name: init_class_shred()
492 // desc: ...
493 //-----------------------------------------------------------------------------
494 t_CKBOOL init_class_shred( Chuck_Env * env, Chuck_Type * type )
495 {
496 // init as base class
497 Chuck_DL_Func * func = NULL;
498
499 // log
500 EM_log( CK_LOG_HERALD, "class 'Shred'" );
501
502 const char *doc = "a strongly-timed ChucK thread of execution.";
503
504 // init as base class
505 // TODO: ctor/dtor?
506 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
507 return FALSE;
508
509 // add dtor
510 // not
511
512 // add fromId()
513 func = make_new_sfun( "Shred", "fromId", shred_fromId );
514 func->add_arg( "int", "id" );
515 func->doc = "get Shred corresponding to a Shred ID.";
516 if( !type_engine_import_sfun( env, func ) ) goto error;
517
518 // add clone()
519 // func = make_new_mfun( "void", "clone", shred_clone );
520 // if( !type_engine_import_mfun( env, func ) ) goto error;
521
522 // add exit()
523 func = make_new_mfun( "void", "exit", shred_exit );
524 func->doc = "halt the shred's operation and remove it from the virtual machine.";
525 if( !type_engine_import_mfun( env, func ) ) goto error;
526
527 // add id()
528 func = make_new_mfun( "int", "id", shred_id );
529 func->doc = "get the unique numeric id of the Shred.";
530 if( !type_engine_import_mfun( env, func ) ) goto error;
531
532 // add yield()
533 func = make_new_mfun( "void", "yield", shred_yield );
534 func->doc = "cause the current Shred to temporarily suspend without advancing time, allowing other simultaneously schreduled shreds to run as needed. NOTE: yield() is equivalent to '0::second +=> now;'";
535 if( !type_engine_import_mfun( env, func ) ) goto error;
536
537 // add running()
538 func = make_new_mfun( "int", "running", shred_running );
539 func->doc = "is the Shred currently actively running in the VM?";
540 if( !type_engine_import_mfun( env, func ) ) goto error;
541
542 // add done()
543 func = make_new_mfun( "int", "done", shred_done );
544 func->doc = "has the Shred reached the end of its execution?";
545 if( !type_engine_import_mfun( env, func ) ) goto error;
546
547 // add nargs()
548 // func = make_new_mfun( "int", "numArgs", shred_numArgs );
549 // if( !type_engine_import_mfun( env, func ) ) goto error;
550
551 // add nargs()
552 func = make_new_mfun( "int", "args", shred_numArgs );
553 func->doc = "get the number of arguments provided to the Shred.";
554 if( !type_engine_import_mfun( env, func ) ) goto error;
555
556 // add arg()
557 // func = make_new_mfun( "string", "getArg", shred_getArg );
558 // func->add_arg( "int", "index" );
559 // if( !type_engine_import_mfun( env, func ) ) goto error;
560
561 // add arg()
562 func = make_new_mfun( "string", "arg", shred_getArg );
563 func->add_arg( "int", "index" );
564 func->doc = "get the Shred argument at the specified index.";
565 if( !type_engine_import_mfun( env, func ) ) goto error;
566
567 // add sourcePath() (added 1.3.0.0)
568 func = make_new_mfun( "string", "sourcePath", shred_sourcePath );
569 func->doc = "get the path of the source file from which this Shred's code is derived (same as .path()).";
570 if( !type_engine_import_mfun( env, func ) ) goto error;
571
572 // add path() (added 1.3.2.0)
573 func = make_new_mfun( "string", "path", shred_sourcePath );
574 func->doc = "get the path of the source file from which this Shred's code is derived (same as .sourcePath()).";
575 if( !type_engine_import_mfun( env, func ) ) goto error;
576
577 // add sourceDir() (added 1.3.0.0)
578 func = make_new_mfun( "string", "sourceDir", shred_sourceDir );
579 func->doc = "get the enclosing directory of the source file from which this Shred's code is derived (same as .dir()).";
580 if( !type_engine_import_mfun( env, func ) ) goto error;
581
582 // add dir() (added 1.3.2.0)
583 func = make_new_mfun( "string", "dir", shred_sourceDir );
584 func->doc = "get the enclosing directory of the source file from which this Shred's code is derived (same as .sourceDir()).";
585 if( !type_engine_import_mfun( env, func ) ) goto error;
586
587 // add dir() (added 1.3.2.0, ge)
588 func = make_new_mfun( "string", "dir", shred_sourceDir2 );
589 func->add_arg( "int", "levelsUp" );
590 func->doc = "get the enclosing directory, the specified number of parent directories up.";
591 if( !type_engine_import_mfun( env, func ) ) goto error;
592
593 // add childMemSize() | 1.5.1.5
594 func = make_new_mfun( "int", "childMemSize", shred_ctrl_hintChildMemSize );
595 func->add_arg( "int", "sizeInBytes" );
596 func->doc = "set size hint of per-shred call stack (\"mem\") for children shreds subsequently sporked from the calling shred (NOTE this size hint does not affect the calling shred--only its descendants); if sizeInBytes <= 0, the size hint is set to the VM default. (FYI This is an arcane functionality that most programmers never need to worry about. Advanced usage: set size hint to small values (e.g., 1K) to support A LOT (e.g., >10000) of simultaneous shreds; set size hint to large values (e.g., >65K) to spork functions with extremely deep recursion, or to support A LOT (>10000) of declared local variables. Use with care.)";
597 if( !type_engine_import_mfun( env, func ) ) goto error;
598
599 // add childMemSize() | 1.5.1.5
600 func = make_new_mfun( "int", "childMemSize", shred_cget_hintChildMemSize );
601 func->doc = "get the memory stack size hint (in bytes) for shreds sporked from this one.";
602 if( !type_engine_import_mfun( env, func ) ) goto error;
603
604 // add childRegSize() | 1.5.1.5
605 func = make_new_mfun( "int", "childRegSize", shred_ctrl_hintChildRegSize );
606 func->add_arg( "int", "sizeInBytes" );
607 func->doc = "set size hint of per-shred operand stack (\"reg\") for children shreds subsequently sporked from the calling shred (NOTE this size hint does not affect the calling shred--only its descendants); if sizeInBytes <= 0, the size hint is set to the VM default. (FYI This is an arcane functionality that most programmers never need to worry about. Advanced usage: set size hint to small values (e.g., 256 bytes) to support A LOT (>10000) of simultaneous shreds; set size hint to large values (e.g., >20K) to spork functions with extremely lengthy (>10000) statements, including array initializer lists. Use with care.)";
608 if( !type_engine_import_mfun( env, func ) ) goto error;
609
610 // add childRegSize() | 1.5.1.5
611 func = make_new_mfun( "int", "childRegSize", shred_cget_hintChildRegSize );
612 func->doc = "get the operand stack size hint (in bytes) for shreds sporked from this one.";
613 if( !type_engine_import_mfun( env, func ) ) goto error;
614
615 // add parent() | 1.5.2.0 (nshaheed) | 1.5.4.2 (ge) sfun => mfun
616 func = make_new_mfun( "Shred", "parent", shred_parent );
617 func->doc = "get the calling shred's parent shred (i.e., the shred that sporked the calling shred). Returns null if there is no parent Shred. (Related: see Shred.ancestor())";
618 if( !type_engine_import_mfun( env, func ) ) goto error;
619
620 // add ancestor() | 1.5.2.0 (nshaheed) | 1.5.4.2 (ge) sfun => mfun
621 func = make_new_mfun( "Shred", "ancestor", shred_ancestor );
622 func->doc = "get the calling shred's \"ancestor\" shred (i.e., the top-level shred). Returns itself if the calling shred is the top-level shred. (Related: see Shred.parent())";
623 if( !type_engine_import_mfun( env, func ) ) goto error;
624
625 // add gc() | 1.5.2.0 (ge) added
626 // func = make_new_mfun( "void", "gc", shred_gc );
627 // func->doc = "manually trigger a per-shred garbage collection pass; can be used to clean up certain UGens/objects without waiting for the shred to complete; use with care.";
628 // if( !type_engine_import_mfun( env, func ) ) goto error;
629
630 // add pc() | 1.5.4.2 (ge) | 1.5.4.2 (ge) sfun => mfun
631 func = make_new_mfun( "int", "pc", shred_pc );
632 func->doc = "get the calling shred's current program counter (CP); useful for debugging, general information, or amusement.";
633 if( !type_engine_import_mfun( env, func ) ) goto error;
634
635 // add pc() | 1.5.4.2 (ge) | 1.5.4.2 (ge) sfun => mfun
636 func = make_new_mfun( "int", "regSP", shred_reg_stack_sp );
637 func->doc = "get the calling shred's current register/operand stack pointer; useful for debugging, general information, or amusement.";
638 if( !type_engine_import_mfun( env, func ) ) goto error;
639
640 // add pc() | 1.5.4.2 (ge) | 1.5.4.2 (ge) sfun => mfun
641 func = make_new_mfun( "int", "memSP", shred_mem_stack_sp );
642 func->doc = "get the calling shred's current memory/call stack pointer; useful for debugging, general information, or amusement.";
643 if( !type_engine_import_mfun( env, func ) ) goto error;
644
645 // add examples
646 if( !type_engine_import_add_ex( env, "shred/spork.ck" ) ) goto error;
647 if( !type_engine_import_add_ex( env, "shred/spork2.ck" ) ) goto error;
648 if( !type_engine_import_add_ex( env, "shred/spork2-exit.ck" ) ) goto error;
649 if( !type_engine_import_add_ex( env, "shred/spork2-remove.ck" ) ) goto error;
650 if( !type_engine_import_add_ex( env, "shred/powerup.ck" ) ) goto error;
651 if( !type_engine_import_add_ex( env, "event/broadcast.ck" ) ) goto error;
652 if( !type_engine_import_add_ex( env, "event/signal.ck" ) ) goto error;
653 if( !type_engine_import_add_ex( env, "event/signal4.ck" ) ) goto error;
654 if( !type_engine_import_add_ex( env, "shred/parent.ck" ) ) goto error;
655 if( !type_engine_import_add_ex( env, "shred/ancestor.ck" ) ) goto error;
656
657 // end the class import
658 type_engine_import_class_end( env );
659
660 return TRUE;
661
662 error:
663
664 // end the class import
665 type_engine_import_class_end( env );
666
667 return FALSE;
668 }
669
670
671
672
673 //-----------------------------------------------------------------------------
674 // name: init_class_vec2()
675 // desc: initialize primitive type vec2
676 //-----------------------------------------------------------------------------
677 t_CKBOOL init_class_vec2( Chuck_Env * env, Chuck_Type * type )
678 {
679 // init as base class
680 Chuck_DL_Func * func = NULL;
681
682 // log
683 EM_log( CK_LOG_HERALD, "class 'vec2' (primitive)" );
684
685 // document
686 const char * doc = "a primitive type for a 2-dimensional vector; potentially useful for 2D and UV coordinates.";
687
688 // init as base class
689 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
690 return FALSE;
691
692 // add set(float,float,float)
693 func = make_new_mfun( "void", "set", vec2_set );
694 func->add_arg( "float", "x" );
695 func->add_arg( "float", "y" );
696 if( !type_engine_import_mfun( env, func ) ) goto error;
697
698 // add setAll(float)
699 func = make_new_mfun( "void", "setAll", vec2_setAll );
700 func->add_arg( "float", "value" );
701 if( !type_engine_import_mfun( env, func ) ) goto error;
702
703 // add magnitude()
704 func = make_new_mfun( "float", "magnitude", vec2_magnitude );
705 if( !type_engine_import_mfun( env, func ) ) goto error;
706
707 // add normalize()
708 func = make_new_mfun( "void", "normalize", vec2_normalize );
709 if( !type_engine_import_mfun( env, func ) ) goto error;
710
711 // add dot() product | 1.5.4.2 (ge & azaday) added
712 func = make_new_mfun( "float", "dot", vec2_dot );
713 func->add_arg( "vec2", "rhs" );
714 if( !type_engine_import_mfun( env, func ) ) goto error;
715
716 // end the class import
717 type_engine_import_class_end( env );
718
719 return TRUE;
720
721 error:
722 // end the class import
723 type_engine_import_class_end( env );
724
725 return FALSE;
726 }
727
728
729
730
731 //-----------------------------------------------------------------------------
732 // name: init_class_vec3()
733 // desc: initialize primitive type vec3
734 //-----------------------------------------------------------------------------
735 t_CKBOOL init_class_vec3( Chuck_Env * env, Chuck_Type * type )
736 {
737 // init as base class
738 Chuck_DL_Func * func = NULL;
739
740 // log
741 EM_log( CK_LOG_HERALD, "class 'vec3' (primitive)" );
742
743 // document
744 const char *doc = "a primitive type for a 3-dimensional vector; potentially useful for 3D coordinate, RGB color, or as a value/goal/slew interpolator.";
745
746 // init as base class
747 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
748 return FALSE;
749
750 // add set(float,float,float)
751 func = make_new_mfun( "void", "set", vec3_set );
752 func->add_arg( "float", "x" );
753 func->add_arg( "float", "y" );
754 func->add_arg( "float", "z" );
755 if( !type_engine_import_mfun( env, func ) ) goto error;
756
757 // add setAll(float)
758 func = make_new_mfun( "void", "setAll", vec3_setAll );
759 func->add_arg( "float", "value" );
760 if( !type_engine_import_mfun( env, func ) ) goto error;
761
762 // add magnitude()
763 func = make_new_mfun( "float", "magnitude", vec3_magnitude );
764 if( !type_engine_import_mfun( env, func ) ) goto error;
765
766 // add normalize()
767 func = make_new_mfun( "void", "normalize", vec3_normalize );
768 if( !type_engine_import_mfun( env, func ) ) goto error;
769
770 // add dot() product | 1.5.4.2 (ge & azaday) added
771 func = make_new_mfun( "float", "dot", vec3_dot );
772 func->add_arg( "vec3", "rhs" );
773 if( !type_engine_import_mfun( env, func ) ) goto error;
774
775 // add cross() product | same as vec3 * vec3
776 func = make_new_mfun( "vec3", "cross", vec3_cross );
777 func->add_arg( "vec3", "rhs" );
778 if( !type_engine_import_mfun( env, func ) ) goto error;
779
780 // add interp()
781 func = make_new_mfun( "float", "interp", vec3_interp );
782 if( !type_engine_import_mfun( env, func ) ) goto error;
783
784 // add interp( float )
785 func = make_new_mfun( "float", "interp", vec3_interp_delta_float );
786 func->add_arg( "float", "delta" );
787 if( !type_engine_import_mfun( env, func ) ) goto error;
788
789 // add interp( dur )
790 func = make_new_mfun( "float", "interp", vec3_interp_delta_dur );
791 func->add_arg( "dur", "delta" );
792 if( !type_engine_import_mfun( env, func ) ) goto error;
793
794 // add update( float )
795 func = make_new_mfun( "void", "update", vec3_update_goal );
796 func->add_arg( "float", "goal" );
797 if( !type_engine_import_mfun( env, func ) ) goto error;
798
799 // add update( float, float )
800 func = make_new_mfun( "void", "update", vec3_update_goal_slew );
801 func->add_arg( "float", "goal" );
802 func->add_arg( "float", "slew" );
803 if( !type_engine_import_mfun( env, func ) ) goto error;
804
805 // add update( float )
806 func = make_new_mfun( "void", "updateSet", vec3_updateSet_goalAndValue );
807 func->add_arg( "float", "goalAndValue" );
808 if( !type_engine_import_mfun( env, func ) ) goto error;
809
810 // add update( float, float )
811 func = make_new_mfun( "void", "updateSet", vec3_updateSet_goalAndValue_slew );
812 func->add_arg( "float", "goal" );
813 func->add_arg( "float", "slew" );
814 if( !type_engine_import_mfun( env, func ) ) goto error;
815
816 // end the class import
817 type_engine_import_class_end( env );
818
819 return TRUE;
820
821 error:
822 // end the class import
823 type_engine_import_class_end( env );
824
825 return FALSE;
826 }
827
828
829
830
831 //-----------------------------------------------------------------------------
832 // name: init_class_vec4()
833 // desc: initialize primitive type vec4
834 //-----------------------------------------------------------------------------
835 t_CKBOOL init_class_vec4( Chuck_Env * env, Chuck_Type * type )
836 {
837 // init as base class
838 Chuck_DL_Func * func = NULL;
839
840 // log
841 EM_log( CK_LOG_HERALD, "class 'vec4' (primitive)" );
842
843 // document
844 const char *doc = "a primitive type for a 4-dimensional vector; potentially useful for 4D coordinate and RGBA color.";
845
846 // init as base class
847 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
848 return FALSE;
849
850 // add set(float,float,float)
851 func = make_new_mfun( "void", "set", vec4_set );
852 func->add_arg( "float", "x" );
853 func->add_arg( "float", "y" );
854 func->add_arg( "float", "z" );
855 func->add_arg( "float", "w" );
856 if( !type_engine_import_mfun( env, func ) ) goto error;
857
858 // add setAll(float)
859 func = make_new_mfun( "void", "setAll", vec4_setAll );
860 func->add_arg( "float", "value" );
861 if( !type_engine_import_mfun( env, func ) ) goto error;
862
863 // add magnitude()
864 func = make_new_mfun( "float", "magnitude", vec4_magnitude );
865 if( !type_engine_import_mfun( env, func ) ) goto error;
866
867 // add normalize()
868 func = make_new_mfun( "void", "normalize", vec4_normalize );
869 if( !type_engine_import_mfun( env, func ) ) goto error;
870
871 // add dot() product | 1.5.4.2 (ge & azaday) added
872 func = make_new_mfun( "float", "dot", vec4_dot );
873 func->add_arg( "vec4", "rhs" );
874 if( !type_engine_import_mfun( env, func ) ) goto error;
875
876 // add cross() product | same as vec4 * vec4
877 // (which ignores w and computes a 3d cross product)
878 func = make_new_mfun( "vec4", "cross", vec4_cross );
879 func->add_arg( "vec4", "rhs" );
880 if( !type_engine_import_mfun( env, func ) ) goto error;
881
882 // end the class import
883 type_engine_import_class_end( env );
884
885 return TRUE;
886
887 error:
888 // end the class import
889 type_engine_import_class_end( env );
890
891 return FALSE;
892 }
893
894
895
896
897 //-----------------------------------------------------------------------------
898 // name: init_class_string()
899 // desc: ...
900 //-----------------------------------------------------------------------------
901 t_CKBOOL init_class_string( Chuck_Env * env, Chuck_Type * type )
902 {
903 // init as base class
904 Chuck_DL_Func * func = NULL;
905
906 // log
907 EM_log( CK_LOG_HERALD, "class 'string'" );
908
909 const char * doc = "textual data as a sequence of characters, along with functions for manipulating text.";
910
911 // init as base class
912 // NOTE: explicitly instantiated in instantiate_and_initialize_object()
913 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
914 return FALSE;
915
916 // add contructor
917 func = make_new_ctor( string_ctor_str );
918 func->add_arg( "string", "str" );
919 func->doc = "construct a string as a copy of another string.";
920 if( !type_engine_import_ctor( env, func ) ) goto error;
921
922 // add length()
923 func = make_new_mfun( "int", "length", string_length );
924 func->doc = "get the number of characters of the string.";
925 if( !type_engine_import_mfun( env, func ) ) goto error;
926
927 // add upper()
928 func = make_new_mfun( "string", "upper", string_upper );
929 func->doc = "get a new string in which the lowercase characters of the original string have been converted to uppercase.";
930 if( !type_engine_import_mfun( env, func ) ) goto error;
931
932 // add lower()
933 func = make_new_mfun( "string", "lower", string_lower );
934 func->doc = "get a new string in which the uppercase characters of the original string have been converted to lowercase.";
935 if( !type_engine_import_mfun( env, func ) ) goto error;
936
937 // add ltrim()
938 func = make_new_mfun( "string", "ltrim", string_ltrim );
939 func->doc = "get a new string in which leading whitespace has been removed.";
940 if( !type_engine_import_mfun( env, func ) ) goto error;
941
942 // add rtrim()
943 func = make_new_mfun( "string", "rtrim", string_rtrim );
944 func->doc = "get a new string in which trailing whitespace has been removed.";
945 if( !type_engine_import_mfun( env, func ) ) goto error;
946
947 // add trim()
948 func = make_new_mfun( "string", "trim", string_trim );
949 func->doc = "get a new string in which leading and trailing whitespace has been removed.";
950 if( !type_engine_import_mfun( env, func ) ) goto error;
951
952 // add toString()
953 func = make_new_mfun( "string", "toString", string_toString );
954 func->doc = "return the reference of calling string.";
955 if( !type_engine_import_mfun( env, func ) ) goto error;
956
957 // add charAt()
958 func = make_new_mfun( "int", "charAt", string_charAt );
959 func->add_arg( "int", "index" );
960 func->doc = "get the character at the specified index.";
961 if( !type_engine_import_mfun( env, func ) ) goto error;
962
963 // add charAt2()
964 func = make_new_mfun( "string", "charAt2", string_charAt2 );
965 func->add_arg( "int", "index" );
966 func->doc = "get the character at the specified index, and return it as a string";
967 if( !type_engine_import_mfun( env, func ) ) goto error;
968
969 // add setCharAt()
970 func = make_new_mfun( "int", "setCharAt", string_setCharAt );
971 func->add_arg("int", "index");
972 func->add_arg("int", "theChar");
973 func->doc = "set the character at the specified index.";
974 if( !type_engine_import_mfun( env, func ) ) goto error;
975
976 // add setCharAt2()
977 func = make_new_mfun( "string", "setCharAt2", string_setCharAt2 );
978 func->add_arg("int", "index");
979 func->add_arg("string", "theChar");
980 func->doc = "set the character (as a string) at the specified index; if `theChar` is an empty string, the character in question is erased from the string; if `theChar` is longer than 1 character, only its first character will be used";
981 if( !type_engine_import_mfun( env, func ) ) goto error;
982
983 // add append()
984 func = make_new_mfun( "string", "appendChar", string_appendChar );
985 func->add_arg( "int", "theChar" );
986 func->doc = "append a character by its ASCII code and return a reference to itself; same as using the << operator `STR << INT`";
987 if( !type_engine_import_mfun( env, func ) ) goto error;
988
989 // add substring()
990 func = make_new_mfun( "string", "substring", string_substring );
991 func->add_arg("int", "start");
992 func->doc = "get a new string containing the substring from the start index to the end of the string.";
993 if( !type_engine_import_mfun( env, func ) ) goto error;
994
995 // add substring()
996 func = make_new_mfun( "string", "substring", string_substringN );
997 func->add_arg("int", "start");
998 func->add_arg("int", "length");
999 func->doc = "get a new string containing the substring from the start index of the specified length.";
1000 if( !type_engine_import_mfun( env, func ) ) goto error;
1001
1002 // add insert()
1003 func = make_new_mfun( "void", "insert", string_insert );
1004 func->add_arg("int", "position");
1005 func->add_arg("string", "str");
1006 func->doc = "insert a string at the specified position.";
1007 if( !type_engine_import_mfun( env, func ) ) goto error;
1008
1009 // add replace()
1010 func = make_new_mfun( "void", "replace", string_replace );
1011 func->add_arg( "int", "position" );
1012 func->add_arg( "string", "str" );
1013 func->doc = "replace characters from 'position' with contents of 'str'.";
1014 if( !type_engine_import_mfun( env, func ) ) goto error;
1015
1016 // add replace()
1017 func = make_new_mfun( "void", "replace", string_replaceN );
1018 func->add_arg( "int", "position" );
1019 func->add_arg( "int", "length" );
1020 func->add_arg( "string", "str" );
1021 func->doc = "replace 'length' characters from 'position' with contents of 'str'.";
1022 if( !type_engine_import_mfun( env, func ) ) goto error;
1023
1024 // substring replace() | 1.5.1.3 (nshaheed) added
1025 func = make_new_mfun( "void", "replace", string_replace_str );
1026 func->add_arg( "string", "from" );
1027 func->add_arg( "string", "to" );
1028 func->doc = "replace all instances of 'from' in the string with 'to'.";
1029 if( !type_engine_import_mfun( env, func ) ) goto error;
1030
1031 // add find()
1032 func = make_new_mfun( "int", "find", string_find );
1033 func->add_arg( "int", "theChar" );
1034 func->doc = "get the index of the first occurrence of theChar, or -1 if theChar is not found.";
1035 if( !type_engine_import_mfun( env, func ) ) goto error;
1036
1037 // add find()
1038 func = make_new_mfun( "int", "find", string_findStart );
1039 func->add_arg( "int", "theChar" );
1040 func->add_arg( "int", "start" );
1041 func->doc = "get the index of the first occurrence of theChar at or after the start position, or -1 if theChar is not found.";
1042 if( !type_engine_import_mfun( env, func ) ) goto error;
1043
1044 // add find()
1045 func = make_new_mfun( "int", "find", string_findStr );
1046 func->add_arg( "string", "str" );
1047 func->doc = "get the index of the first occurrence of str, or -1 if str is not found.";
1048 if( !type_engine_import_mfun( env, func ) ) goto error;
1049
1050 // add find()
1051 func = make_new_mfun( "int", "find", string_findStrStart );
1052 func->add_arg( "string", "str" );
1053 func->add_arg( "int", "start" );
1054 func->doc = "get the index of the first occurrence of str at or after the start position, or -1 if str is not found.";
1055 if( !type_engine_import_mfun( env, func ) ) goto error;
1056
1057 // add rfind()
1058 func = make_new_mfun( "int", "rfind", string_rfind );
1059 func->add_arg( "int", "theChar" );
1060 func->doc = "get the index of the last occurrence of theChar, or -1 if theChar is not found.";
1061 if( !type_engine_import_mfun( env, func ) ) goto error;
1062
1063 // add rfind()
1064 func = make_new_mfun( "int", "rfind", string_rfindStart );
1065 func->add_arg( "int", "theChar" );
1066 func->add_arg( "int", "start" );
1067 func->doc = "get the index of the last occurrence of theChar at or before the start position, or -1 if theChar is not found.";
1068 if( !type_engine_import_mfun( env, func ) ) goto error;
1069
1070 // add rfind()
1071 func = make_new_mfun( "int", "rfind", string_rfindStr );
1072 func->add_arg( "string", "str" );
1073 func->doc = "get the index of the last occurrence of str, or -1 if str is not found.";
1074 if( !type_engine_import_mfun( env, func ) ) goto error;
1075
1076 // add rfind()
1077 func = make_new_mfun( "int", "rfind", string_rfindStrStart );
1078 func->add_arg( "string", "str" );
1079 func->add_arg( "int", "start" );
1080 func->doc = "get the index of the last occurrence of str at or before the start position, or -1 if str is not found.";
1081 if( !type_engine_import_mfun( env, func ) ) goto error;
1082
1083 // add erase()
1084 func = make_new_mfun( "void", "erase", string_erase );
1085 func->add_arg( "int", "start" );
1086 func->add_arg( "int", "length" );
1087 func->doc = "erase length characters of the string from start position.";
1088 if( !type_engine_import_mfun( env, func ) ) goto error;
1089
1090 // add toInt()
1091 func = make_new_mfun( "int", "toInt", string_toInt );
1092 func->doc = "attempt to convert the contents of the string to an integer and return the result, or 0 if conversion failed.";
1093 if( !type_engine_import_mfun( env, func ) ) goto error;
1094
1095 // add toFloat()
1096 func = make_new_mfun( "float", "toFloat", string_toFloat );
1097 func->doc = "attempt to convert the contents of the string to an float and return the result, or 0 if conversion failed.";
1098 if( !type_engine_import_mfun( env, func ) ) goto error;
1099
1100 // add parent()
1101 // disable for now
1102 // func = make_new_mfun( "string", "parent", string_parent );
1103 // if( !type_engine_import_mfun( env, func ) ) goto error;
1104
1105 // add toTime()
1106 // func = make_new_mfun( "float", "toTime", string_toFloat );
1107 // if( !type_engine_import_mfun( env, func ) ) goto error;
1108
1109 // add toDur()
1110 // func = make_new_mfun( "float", "toDur", string_toFloat );
1111 // if( !type_engine_import_mfun( env, func ) ) goto error;
1112
1113 // add at()
1114 // func = make_new_mfun( "int", "ch", string_set_at );
1115 // func->add_arg( "int", "index" );
1116 // func->add_arg( "int", "val" );
1117 // if( !type_engine_import_mfun( env, func ) ) goto error;
1118 // func = make_new_mfun( "int", "ch", string_get_at );
1119 // func->add_arg( "int", "index" );
1120 // if( !type_engine_import_mfun( env, func ) ) goto error;
1121
1122 // add examples | 1.5.0.0 (ge) added
1123 if( !type_engine_import_add_ex( env, "string/strops.ck" ) ) goto error;
1124 if( !type_engine_import_add_ex( env, "string/strops2.ck" ) ) goto error;
1125 if( !type_engine_import_add_ex( env, "string/replace.ck" ) ) goto error;
1126 if( !type_engine_import_add_ex( env, "string/token.ck" ) ) goto error;
1127 if( !type_engine_import_add_ex( env, "string/escape.ck" ) ) goto error;
1128 if( !type_engine_import_add_ex( env, "string/tostr.ck" ) ) goto error;
1129
1130 // end the class import
1131 type_engine_import_class_end( env );
1132
1133 // add operator overload: string+string | 1.5.4.2 (ge)
1134 func = make_new_op_binary( "string", ae_op_plus, string_op_string_plus_string );
1135 func->add_arg( "string", "obj" );
1136 func->add_arg( "string", "str" );
1137 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1138
1139 // add operator overload: int+string | 1.5.4.2 (ge)
1140 func = make_new_op_binary( "string", ae_op_plus, string_op_int_plus_string );
1141 func->add_arg( "int", "ival" );
1142 func->add_arg( "string", "str" );
1143 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1144 // add operator overload: string+int | 1.5.4.2 (ge)
1145 func = make_new_op_binary( "string", ae_op_plus, string_op_string_plus_int );
1146 func->add_arg( "string", "obj" );
1147 func->add_arg( "int", "ival" );
1148 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1149
1150 // add operator overload: int+string | 1.5.4.2 (ge)
1151 func = make_new_op_binary( "string", ae_op_plus, string_op_float_plus_string );
1152 func->add_arg( "float", "fval" );
1153 func->add_arg( "string", "str" );
1154 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1155 // add operator overload: string+int | 1.5.4.2 (ge)
1156 func = make_new_op_binary( "string", ae_op_plus, string_op_string_plus_float );
1157 func->add_arg( "string", "obj" );
1158 func->add_arg( "float", "fval" );
1159 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1160
1161 // add operator overload: string == string | 1.5.4.2 (ge)
1162 func = make_new_op_binary( "int", ae_op_eq, string_op_string_eq_string );
1163 func->add_arg( "string", "obj" );
1164 func->add_arg( "string", "str" );
1165 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1166
1167 // add operator overload: string != string | 1.5.4.2 (ge)
1168 func = make_new_op_binary( "int", ae_op_neq, string_op_string_neq_string );
1169 func->add_arg( "string", "obj" );
1170 func->add_arg( "string", "str" );
1171 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1172
1173 // add operator overload: string < string | 1.5.4.2 (ge)
1174 func = make_new_op_binary( "int", ae_op_lt, string_op_string_lt_string );
1175 func->add_arg( "string", "obj" );
1176 func->add_arg( "string", "str" );
1177 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1178
1179 // add operator overload: string <= string | 1.5.4.2 (ge)
1180 func = make_new_op_binary( "int", ae_op_le, string_op_string_le_string );
1181 func->add_arg( "string", "obj" );
1182 func->add_arg( "string", "str" );
1183 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1184
1185 // add operator overload: string > string | 1.5.4.2 (ge)
1186 func = make_new_op_binary( "int", ae_op_gt, string_op_string_gt_string );
1187 func->add_arg( "string", "obj" );
1188 func->add_arg( "string", "str" );
1189 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1190
1191 // add operator overload: string >= string | 1.5.4.2 (ge)
1192 func = make_new_op_binary( "int", ae_op_ge, string_op_string_ge_string );
1193 func->add_arg( "string", "obj" );
1194 func->add_arg( "string", "str" );
1195 if( !type_engine_import_op_overload( env, func ) ) goto error_post_class;
1196
1197 return TRUE;
1198
1199 error:
1200
1201 // end the class import
1202 type_engine_import_class_end( env );
1203
1204 error_post_class:
1205
1206 return FALSE;
1207 }
1208
1209
1210
1211
1212 //-----------------------------------------------------------------------------
1213 // name: init_class_array()
1214 // desc: ...
1215 //-----------------------------------------------------------------------------
1216 t_CKBOOL init_class_array( Chuck_Env * env, Chuck_Type * type )
1217 {
1218 // init as base class
1219 Chuck_DL_Func * func = NULL;
1220
1221 // log
1222 EM_log( CK_LOG_HERALD, "class 'array'" );
1223
1224 // init as base class
1225 // TODO: ctor/dtor?
1226 string doc = "storage construct for sequential data of the same type; can also be used as an associative map data structure; also can be used as a stack with << operator to append/push and popBack to pop.";
1227 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc.c_str() ) )
1228 return FALSE;
1229
1230 // add clear()
1231 func = make_new_mfun( "void", "clear", array_clear );
1232 func->doc = "clear the contents of the array.";
1233 if( !type_engine_import_mfun( env, func ) ) goto error;
1234
1235 // add zero() | 1.5.0.0 (ge) added
1236 func = make_new_mfun( "void", "zero", array_zero );
1237 func->doc = "zero out the contents of the array; size is unchanged.";
1238 if( !type_engine_import_mfun( env, func ) ) goto error;
1239
1240 // add reset()
1241 func = make_new_mfun( "void", "reset", array_reset );
1242 func->doc = "reset array size to 0, set capacity to (at least) 8.";
1243 if( !type_engine_import_mfun( env, func ) ) goto error;
1244
1245 // // add insert()
1246 // func = make_new_mfun( "void", "insert", array_insert );
1247 // func->add_arg( "int", "position" );
1248 // func->add_arg( "auto", "value" );
1249 // func->doc = "insert value before the specific position.";
1250 // if( !type_engine_import_mfun( env, func ) ) goto error;
1251
1252 // // add pushBack()
1253 // func = make_new_mfun( "void", "pushBack", array_push_back );
1254 // func->add_arg( "auto", "value" );
1255 // func->doc = "append value to array.";
1256 // if( !type_engine_import_mfun( env, func ) ) goto error;
1257
1258 // // add pushFront()
1259 // func = make_new_mfun( "void", "pushFront", array_push_front );
1260 // func->add_arg( "auto", "value" );
1261 // func->doc = "prepend value to array.";
1262 // if( !type_engine_import_mfun( env, func ) ) goto error;
1263
1264 // add popBack()
1265 func = make_new_mfun( "void", "popBack", array_pop_back );
1266 func->doc = "remove the last element of the array.";
1267 if( !type_engine_import_mfun( env, func ) ) goto error;
1268
1269 // add popFront()
1270 func = make_new_mfun( "void", "popFront", array_pop_front );
1271 func->doc = "remove the first element of the array.";
1272 if( !type_engine_import_mfun( env, func ) ) goto error;
1273
1274 // add popOut()
1275 func = make_new_mfun( "void", "popOut", array_erase );
1276 func->add_arg( "int", "position" );
1277 func->doc = "remove the element at 'position' from the array (same as erase(int)).";
1278 if( !type_engine_import_mfun( env, func ) ) goto error;
1279
1280 // add erase()
1281 func = make_new_mfun( "int", "erase", array_erase );
1282 func->add_arg( "int", "position" );
1283 func->doc = "remove element at 'position' from the array (same as popOut(int)).";
1284 if( !type_engine_import_mfun( env, func ) ) goto error;
1285
1286 // add erase()
1287 func = make_new_mfun( "int", "erase", array_erase2 );
1288 func->add_arg( "int", "begin" );
1289 func->add_arg( "int", "end" );
1290 func->doc = "remove element(s) in the range [begin,end).";
1291 if( !type_engine_import_mfun( env, func ) ) goto error;
1292
1293 // add size()
1294 func = make_new_mfun( "int", "size", array_get_size );
1295 func->doc = "get the number of elements in the array.";
1296 if( !type_engine_import_mfun( env, func ) ) goto error;
1297 // add size()
1298 func = make_new_mfun( "int", "size", array_set_size );
1299 func->add_arg( "int", "newSize" );
1300 func->doc = "set the size of the array. If the new size is less than the current size, elements will be deleted from the end; if the new size is larger than the current size, 0 or null elements will be added to the end.";
1301 if( !type_engine_import_mfun( env, func ) ) goto error;
1302
1303 // add getKeys() | (1.4.1.1) nshaheed
1304 func = make_new_mfun( "void", "getKeys", array_get_keys );
1305 func->add_arg( "string[]", "keys" );
1306 func->doc = "return all keys found in associative array in keys";
1307 if( !type_engine_import_mfun( env, func ) ) goto error;
1308
1309 // note 1.4.1.0: in the future, should deprecate and encourage programmer
1310 // to explicitly use .size() OR .capacity(), depending on the context
1311 // ---------
1312 // add cap()
1313 func = make_new_mfun( "int", "cap", array_get_capacity_hack );
1314 func->doc = "(deprecated) For historical/compatibilty reasons, .cap() is always equal to .size(); instead of using .cap(), it is recommended to explicitly use .size() or .capacity().";
1315 if( !type_engine_import_mfun( env, func ) ) goto error;
1316
1317 // add capacity() // 1.4.1.0
1318 func = make_new_mfun( "int", "capacity", array_set_capacity );
1319 func->doc = "ensure mininum capacity of the array.";
1320 func->add_arg( "int", "val" );
1321 if( !type_engine_import_mfun( env, func ) ) goto error;
1322 func = make_new_mfun( "int", "capacity", array_get_capacity );
1323 func->doc = "get current capacity of the array.";
1324 if( !type_engine_import_mfun( env, func ) ) goto error;
1325
1326 // add isInMap() | 1.5.0.8 previously was find()
1327 func = make_new_mfun( "int", "isInMap", array_map_find );
1328 func->add_arg( "string", "key" );
1329 func->doc = "(map only) test if 'key' is in the map; (historical) this was renamed from .find() to avoid confusion with the vector part of array.";
1330 if( !type_engine_import_mfun( env, func ) ) goto error;
1331
1332 // add erase()
1333 func = make_new_mfun( "int", "erase", array_map_erase );
1334 func->add_arg( "string", "key" );
1335 func->doc = "(map only) Erase all elements with the specified key.";
1336 if( !type_engine_import_mfun( env, func ) ) goto error;
1337
1338 // add reverse() | (1.5.0.0) azaday
1339 func = make_new_mfun( "void", "reverse", array_reverse );
1340 func->doc = "reverses the array in-place";
1341 if( !type_engine_import_mfun( env, func ) ) goto error;
1342
1343 // add shuffle()
1344 func = make_new_mfun( "void", "shuffle", array_shuffle );
1345 func->doc = "shuffle the contents of the array.";
1346 if( !type_engine_import_mfun( env, func ) ) goto error;
1347
1348 // add sort()
1349 func = make_new_mfun( "void", "sort", array_sort );
1350 func->doc = "sort the contents of the array in ascending order.";
1351 if( !type_engine_import_mfun( env, func ) ) goto error;
1352
1353 // add examples
1354 if( !type_engine_import_add_ex( env, "array/array_append.ck" ) ) goto error;
1355 if( !type_engine_import_add_ex( env, "array/array_argument.ck" ) ) goto error;
1356 if( !type_engine_import_add_ex( env, "array/array_assign.ck" ) ) goto error;
1357 if( !type_engine_import_add_ex( env, "array/array_associative.ck" ) ) goto error;
1358 if( !type_engine_import_add_ex( env, "array/array_capacity.ck" ) ) goto error;
1359 if( !type_engine_import_add_ex( env, "array/array_dynamic.ck" ) ) goto error;
1360 if( !type_engine_import_add_ex( env, "array/array_erase.ck" ) ) goto error;
1361 if( !type_engine_import_add_ex( env, "array/array_erase2.ck" ) ) goto error;
1362 if( !type_engine_import_add_ex( env, "array/array_mdim.ck" ) ) goto error;
1363 if( !type_engine_import_add_ex( env, "array/array_mmixed.ck" ) ) goto error;
1364 if( !type_engine_import_add_ex( env, "array/array_negative.ck" ) ) goto error;
1365 if( !type_engine_import_add_ex( env, "array/array_range.ck" ) ) goto error;
1366 if( !type_engine_import_add_ex( env, "array/array_resize.ck" ) ) goto error;
1367 if( !type_engine_import_add_ex( env, "array/array_reverse.ck" ) ) goto error;
1368 if( !type_engine_import_add_ex( env, "array/array_shuffle.ck" ) ) goto error;
1369 if( !type_engine_import_add_ex( env, "array/array_sort.ck" ) ) goto error;
1370 if( !type_engine_import_add_ex( env, "array/array_storage.ck" ) ) goto error;
1371 if( !type_engine_import_add_ex( env, "array/array_sub_assign.ck" ) ) goto error;
1372 if( !type_engine_import_add_ex( env, "array/array_zero.ck" ) ) goto error;
1373
1374 // end import
1375 type_engine_import_class_end( env );
1376
1377 return TRUE;
1378
1379 error:
1380
1381 // end the class import
1382 type_engine_import_class_end( env );
1383
1384 return FALSE;
1385 }
1386
1387
1388
1389
1390 //-----------------------------------------------------------------------------
1391 // name: init_class_function()
1392 // desc: initialize the @fuction class | 1.5.0.0 (ge) added
1393 //-----------------------------------------------------------------------------
1394 t_CKBOOL init_class_function( Chuck_Env * env, Chuck_Type * type )
1395 {
1396 // log
1397 EM_log( CK_LOG_HERALD, "class '@function'" );
1398 const char * doc = "the base function Type.";
1399
1400 // init as base class
1401 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc ) )
1402 return FALSE;
1403 // end the class import
1404 type_engine_import_class_end( env );
1405
1406 return TRUE;
1407 }
1408
1409
1410
1411
1412 // global initialization
1413 const t_CKINT TYPE_ATTRIB_OBJECT = 0x1;
1414 const t_CKINT TYPE_ATTRIB_PRIMITIVE = 0x2;
1415 const t_CKINT TYPE_ATTRIB_SPECIAL = 0x4;
1416 const t_CKINT TYPE_ORIGIN_BUILTIN = 0x8;
1417 const t_CKINT TYPE_ORIGIN_CHUGIN = 0x10;
1418 const t_CKINT TYPE_ORIGIN_CKLIB = 0x20;
1419 const t_CKINT TYPE_ORIGIN_USER = 0x40;
1420
1421
1422 //-----------------------------------------------------------------------------
1423 // name: init_class_type()
1424 // desc: initialize the Type class | 1.5.0.0 (ge) added
1425 //-----------------------------------------------------------------------------
1426 t_CKBOOL init_class_type( Chuck_Env * env, Chuck_Type * type )
1427 {
1428 Chuck_DL_Func * func = NULL;
1429
1430 // log
1431 EM_log( CK_LOG_HERALD, "class 'Type'" );
1432 const char * doc = "a representation of a ChucK type.";
1433
1434 // init as base class
1435 if( !type_engine_import_class_begin( env, type, env->global(), type_ctor, type_dtor, doc ) )
1436 return FALSE;
1437
1438 // add relevant examples
1439 if( !type_engine_import_add_ex( env, "type/type_type.ck" ) ) goto error;
1440 if( !type_engine_import_add_ex( env, "type/type_query.ck" ) ) goto error;
1441
1442 // add static variables
1443 if( !type_engine_import_svar( env, "int", "OBJECT", TRUE, (t_CKUINT)(&TYPE_ATTRIB_OBJECT), "attribute bit for non-primitive Object types.") ) goto error;
1444 if( !type_engine_import_svar( env, "int", "PRIMITIVE", TRUE, (t_CKUINT)(&TYPE_ATTRIB_PRIMITIVE), "attribute bit for primitive types.") ) goto error;
1445 if( !type_engine_import_svar( env, "int", "SPECIAL", TRUE, (t_CKUINT)(&TYPE_ATTRIB_SPECIAL), "attribute bit for special types (e.g., @array and @function).") ) goto error;
1446 if( !type_engine_import_svar( env, "int", "BUILTIN", TRUE, (t_CKUINT)(&TYPE_ORIGIN_BUILTIN), "origin bit for \"builtin\".") ) goto error;
1447 if( !type_engine_import_svar( env, "int", "CHUGIN", TRUE, (t_CKUINT)(&TYPE_ORIGIN_CHUGIN), "origin bit for \"chugin\".") ) goto error;
1448 if( !type_engine_import_svar( env, "int", "CKLIB", TRUE, (t_CKUINT)(&TYPE_ORIGIN_BUILTIN), "origin bit for \"cklib\".") ) goto error;
1449 if( !type_engine_import_svar( env, "int", "USER", TRUE, (t_CKUINT)(&TYPE_ORIGIN_BUILTIN), "origin bit for \"user\".") ) goto error;
1450
1451 // add equals()
1452 func = make_new_mfun( "int", "equals", type_equals );
1453 func->add_arg( "Type", "another" );
1454 func->doc = "return whether this Type is same as 'another'.";
1455 if( !type_engine_import_mfun( env, func ) ) goto error;
1456
1457 // add isa()
1458 func = make_new_mfun( "int", "isa", type_isa );
1459 func->add_arg( "Type", "another" );
1460 func->doc = "return whether this Type is a kind of 'another'.";
1461 if( !type_engine_import_mfun( env, func ) ) goto error;
1462
1463 // add isa()
1464 func = make_new_mfun( "int", "isa", type_isa_str );
1465 func->add_arg( "string", "another" );
1466 func->doc = "return whether this Type is a kind of 'another'.";
1467 if( !type_engine_import_mfun( env, func ) ) goto error;
1468
1469 // add baseName()
1470 func = make_new_mfun( "string", "baseName", type_basename );
1471 func->doc = "return the base name of this Type. The base of name of an array Type is the type without the array dimensions (e.g., base name of 'int[][]' is 'int')";
1472 if( !type_engine_import_mfun( env, func ) ) goto error;
1473
1474 // add name()
1475 func = make_new_mfun( "string", "name", type_name );
1476 func->doc = "return the name of this Type.";
1477 if( !type_engine_import_mfun( env, func ) ) goto error;
1478
1479 // add parent()
1480 func = make_new_mfun( "Type", "parent", type_parent );
1481 func->doc = "return this Type's parent Type; returns null if this Type is 'Object'.";
1482 if( !type_engine_import_mfun( env, func ) ) goto error;
1483
1484 // add kids()
1485 func = make_new_mfun( "Type[]", "children", type_children );
1486 func->doc = "retrieve this Type's children Types.";
1487 if( !type_engine_import_mfun( env, func ) ) goto error;
1488
1489 // add isPrimitive()
1490 func = make_new_mfun( "int", "isPrimitive", type_isPrimitive );
1491 func->doc = "return whether this is a primitive Type (e.g., 'int' and 'dur' are primitives types; 'Object' and its children Types are not).";
1492 if( !type_engine_import_mfun( env, func ) ) goto error;
1493
1494 // add isArray()
1495 func = make_new_mfun( "int", "isArray", type_isArray );
1496 func->doc = "return whether this Type is some kind of an array.";
1497 if( !type_engine_import_mfun( env, func ) ) goto error;
1498
1499 // add arrayDims()
1500 func = make_new_mfun( "int", "arrayDepth", type_arrayDims );
1501 func->doc = "return the number of array dimensions associated with this Type (e.g., 'int[][]' has 2; 'int' has 0).";
1502 if( !type_engine_import_mfun( env, func ) ) goto error;
1503
1504 // add origin()
1505 func = make_new_mfun( "string", "origin", type_origin );
1506 func->doc = "return a string describing where this Type was defined (e.g., \"builtin\", \"chugin\", \"cklib\", \"user\").";
1507 if( !type_engine_import_mfun( env, func ) ) goto error;
1508
1509 // add find()
1510 func = make_new_sfun( "Type", "find", type_findString );
1511 func->add_arg( "string", "typeName" );
1512 func->doc = "find and return the Type associated with 'typeName'; returns null if no Types currently in the VM with that name.";
1513 if( !type_engine_import_sfun( env, func ) ) goto error;
1514
1515 // add typeOf()
1516 func = make_new_sfun( "Type", "of", type_typeOf_obj );
1517 func->add_arg( "Object", "obj" );
1518 func->doc = "return the Type of 'obj'";
1519 if( !type_engine_import_sfun( env, func ) ) goto error;
1520
1521 // add typeOf()
1522 func = make_new_sfun( "Type", "of", type_typeOf_int );
1523 func->add_arg( "int", "val" );
1524 func->doc = "return the Type associated with 'int'.";
1525 if( !type_engine_import_sfun( env, func ) ) goto error;
1526
1527 // add typeOf()
1528 func = make_new_sfun( "Type", "of", type_typeOf_float );
1529 func->add_arg( "float", "val" );
1530 func->doc = "return the Type associated with 'float'.";
1531 if( !type_engine_import_sfun( env, func ) ) goto error;
1532
1533 // add typeOf()
1534 func = make_new_sfun( "Type", "of", type_typeOf_time );
1535 func->add_arg( "time", "val" );
1536 func->doc = "return the Type associated with 'time'.";
1537 if( !type_engine_import_sfun( env, func ) ) goto error;
1538
1539 // add typeOf()
1540 func = make_new_sfun( "Type", "of", type_typeOf_dur );
1541 func->add_arg( "dur", "val" );
1542 func->doc = "return the Type associated with 'dur'.";
1543 if( !type_engine_import_sfun( env, func ) ) goto error;
1544
1545 // add typeOf()
1546 func = make_new_sfun( "Type", "of", type_typeOf_complex );
1547 func->add_arg( "complex", "val" );
1548 func->doc = "return the Type associated with 'complex'.";
1549 if( !type_engine_import_sfun( env, func ) ) goto error;
1550
1551 // add typeOf()
1552 func = make_new_sfun( "Type", "of", type_typeOf_polar );
1553 func->add_arg( "polar", "val" );
1554 func->doc = "return the Type associated with 'polar'.";
1555 if( !type_engine_import_sfun( env, func ) ) goto error;
1556
1557 // add typeOf()
1558 func = make_new_sfun( "Type", "of", type_typeOf_vec2 );
1559 func->add_arg( "vec2", "val" );
1560 func->doc = "return the Type associated with 'vec2'.";
1561 if( !type_engine_import_sfun( env, func ) ) goto error;
1562
1563 // add typeOf()
1564 func = make_new_sfun( "Type", "of", type_typeOf_vec3 );
1565 func->add_arg( "vec3", "val" );
1566 func->doc = "return the Type associated with 'vec3'.";
1567 if( !type_engine_import_sfun( env, func ) ) goto error;
1568
1569 // add typeOf()
1570 func = make_new_sfun( "Type", "of", type_typeOf_vec4 );
1571 func->add_arg( "vec4", "val" );
1572 func->doc = "return the Type associated with 'vec4'.";
1573 if( !type_engine_import_sfun( env, func ) ) goto error;
1574
1575 // add getTypes()
1576 func = make_new_sfun( "Type[]", "getTypes", type_getTypes );
1577 func->add_arg( "int", "attributes" );
1578 func->add_arg( "int", "origins" );
1579 func->doc = "retrieve all top-level Types in the ChucK runtime type system that fit the attributes and origins flags. Flags that can bitwise-OR'ed for attributes: Type.ATTRIB_OBJECT, Type.ATTRIB_PRIMITIVE, TYPE_SPECIAL -- and for origins: Type.ORIGIN_BUILTIN, Type.ORIGIN_CHUGIN, Type.ORIGIN_CKLIB, Type.ORIGIN_USER.";
1580 if( !type_engine_import_sfun( env, func ) ) goto error;
1581
1582 // add getTypes()
1583 // func = make_new_sfun( "Type[]", "getTypes", type_getTypes2 );
1584 // func->add_arg( "int", "includeObjects" );
1585 // func->add_arg( "int", "includePrimitives" );
1586 // func->add_arg( "int", "includeSpecial" );
1587 // func->add_arg( "int", "includeBuiltin" );
1588 // func->add_arg( "int", "includeChugins" );
1589 // func->add_arg( "int", "includeImports" );
1590 // func->add_arg( "int", "includeUserDefined" );
1591 // func->doc = "retrieves in the array 'types' all top-level Types currently in the ChucK runtime type system; 'includeObjects'--include all Objects? 'includePrimitives'--include primitive types suchs as 'int' and 'dur'? 'includeSpecial'--include special types such as '@array' and '@function'? The results are further filtered by 'includeBuiltin'--include builtin types? 'includeChugins'--include types imported from chugins? 'includeImports'--include types imported from CK files in library path? 'includeUserDefined'--include types defined in ChucK code?";
1592 // if( !type_engine_import_sfun( env, func ) ) goto error;
1593
1594 // add getTypesAll()
1595 func = make_new_sfun( "Type[]", "getTypes", type_getTypesAll );
1596 func->doc = "retrieves all top-level Types currently in the type system.";
1597 if( !type_engine_import_sfun( env, func ) ) goto error;
1598
1599 // end the class import
1600 type_engine_import_class_end( env );
1601
1602 return TRUE;
1603
1604 error:
1605
1606 // end the class import
1607 type_engine_import_class_end( env );
1608
1609 return FALSE;
1610 }
1611
1612
1613
1614
1615
1616 //-----------------------------------------------------------------------------
1617 // name: init_primitive_types() | 1.5.0.0 (ge)
1618 // desc: initialize all primitive Type types
1619 //-----------------------------------------------------------------------------
1620 t_CKBOOL init_primitive_types( Chuck_Env * env )
1621 {
1622 //-----------------------------------------------------------------------------
1623 // init void
1624 //-----------------------------------------------------------------------------
1625 if( !type_engine_import_class_begin( env, env->ckt_void, env->global(), NULL, NULL,
1626 "the type of nothingness, or the lack of a type.") ) return FALSE;
1627 // end the class import
1628 type_engine_import_class_end( env );
1629
1630 //-----------------------------------------------------------------------------
1631 // init int
1632 //-----------------------------------------------------------------------------
1633 if( !type_engine_import_class_begin( env, env->ckt_int, env->global(), NULL, NULL,
1634 "the primitive integer type.") ) return FALSE;
1635 // end the class import
1636 type_engine_import_class_end( env );
1637
1638 //-----------------------------------------------------------------------------
1639 // init float
1640 //-----------------------------------------------------------------------------
1641 if( !type_engine_import_class_begin( env, env->ckt_float, env->global(), NULL, NULL,
1642 "the primitive floating point type.") ) return FALSE;
1643 // end the class import
1644 type_engine_import_class_end( env );
1645
1646 //-----------------------------------------------------------------------------
1647 // init time
1648 //-----------------------------------------------------------------------------
1649 if( !type_engine_import_class_begin( env, env->ckt_time, env->global(), NULL, NULL,
1650 "the primitive type of a point in time.") ) return FALSE;
1651 // end the class import
1652 type_engine_import_class_end( env );
1653
1654 //-----------------------------------------------------------------------------
1655 // init dur
1656 //-----------------------------------------------------------------------------
1657 if( !type_engine_import_class_begin( env, env->ckt_dur, env->global(), NULL, NULL,
1658 "the primitive type of a length of time.") ) return FALSE;
1659 // end the class import
1660 type_engine_import_class_end( env );
1661
1662 //-----------------------------------------------------------------------------
1663 // init complex
1664 //-----------------------------------------------------------------------------
1665 if( !type_engine_import_class_begin( env, env->ckt_complex, env->global(), NULL, NULL,
1666 "the primitive type of a complex pair #(re,im).") ) return FALSE;
1667 // end the class import
1668 type_engine_import_class_end( env );
1669
1670 //-----------------------------------------------------------------------------
1671 // init polar
1672 //-----------------------------------------------------------------------------
1673 if( !type_engine_import_class_begin( env, env->ckt_polar, env->global(), NULL, NULL,
1674 "the primitive type of a polar value %(mag,phase)") ) return FALSE;
1675 // end the class import
1676 type_engine_import_class_end( env );
1677
1678 return TRUE;
1679 }
1680
1681 // Object ctor
1682 CK_DLL_CTOR( object_ctor )
1683 {
1684 // log
1685 EM_log( CK_LOG_ALL, "Object constructor..." );
1686
1687 // initialize
1688 OBJ_MEMBER_UINT(SELF, Object_offset_string) = 0;
1689 }
1690
1691
1692 // Object dtor
1693 CK_DLL_DTOR( object_dtor )
1694 {
1695 // log
1696 EM_log( CK_LOG_ALL, "Object destructor..." );
1697
1698 // NOTE the garbage collector should take care of
1699 // string reference at offset Object_offset_string
1700 }
1701
1702
1703 // toString
1704 CK_DLL_MFUN( object_toString )
1705 {
1706 // get the string
1707 Chuck_String * str = (Chuck_String *)OBJ_MEMBER_UINT(SELF, Object_offset_string);
1708 // allocate
1709 if( !str )
1710 {
1711 // new it
1712 str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
1713 // check it
1714 if( !str )
1715 {
1716 // TODO: make this exception
1717 CK_FPRINTF_STDERR( "[chuck]: Object.toString() out of memory!\n" );
1718 RETURN->v_object = NULL;
1719 return;
1720 }
1721 // 1.5.0.0 (ge) assign to object
1722 OBJ_MEMBER_UINT(SELF, Object_offset_string) = (t_CKUINT)str;
1723 // 1.5.0.0 (ge) | add ref
1724 str->add_ref();
1725
1726 // set it
1727 ostringstream strout( ostringstream::out );
1728 // get the type
1729 Chuck_Type * type = SELF->type_ref;
1730 // write
1731 strout.setf( ios::hex, ios::basefield );
1732 strout << ((type != NULL) ? type->c_name() : "[VOID]") << ":" << (t_CKUINT)SELF << " (refcount=" << SELF->m_ref_count << ")";
1733 strout.flush();
1734
1735 // done
1736 str->set( strout.str() );
1737 }
1738
1739 // set return
1740 RETURN->v_object = str;
1741 }
1742
1743 // dump
1744 CK_DLL_MFUN( object_dump )
1745 {
1746 // get object
1747 Chuck_Object * me = SELF;
1748 // call dump
1749 me->dump();
1750 }
1751
1752 // help
1753 CK_DLL_SFUN( object_help )
1754 {
1755 TYPE->apropos();
1756 // get object
1757 // Chuck_Object * me = SELF;
1758 // call dump
1759 // me->apropos();
1760 }
1761
1762 // get the type info
1763 CK_DLL_SFUN( object_typeInfo )
1764 {
1765 // default is to return the type...
1766 RETURN->v_object = TYPE;
1767
1768 // if actual type is indicated...
1769 if( TYPE->actual_type )
1770 {
1771 // what we return depends on if TYPE is an array type or not
1772 if( TYPE->array_depth == 0 )
1773 {
1774 // for cases like X.typeInfo() where X is a class
1775 RETURN->v_object = TYPE->actual_type;
1776 }
1777 }
1778 }
1779
1780 // get the instanced type info
1781 CK_DLL_MFUN( object_typeInstanceInfo )
1782 {
1783 // get the object reference
1784 Chuck_Object * self = SELF;
1785
1786 // if null, return null
1787 if( !self ) return;
1788
1789 // return the type
1790 RETURN->v_object = self->type_ref;
1791 }
1792
1793 // ctor
1794 CK_DLL_CTOR( ugen_ctor )
1795 {
1796 }
1797
1798 CK_DLL_DTOR( ugen_dtor )
1799 {
1800 // get as ugen
1801 // Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1802 }
1803
1804 CK_DLL_MFUN( ugen_op )
1805 {
1806 // get as ugen
1807 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1808 // get arg
1809 t_CKINT op = GET_CK_INT( ARGS );
1810 // set op
1811 ugen->m_op = op;
1812 // set return
1813 RETURN->v_int = ugen->m_op;
1814
1815 // for multiple channels
1816 Chuck_DL_Return ret;
1817 // added 1.3.0.0 -- Chuck_DL_Api::instance()
1818 for( t_CKUINT i = 0; i < ugen->m_multi_chan_size; i++ )
1819 ugen_op( ugen->m_multi_chan[i], ARGS, &ret, VM, SHRED, API );
1820 // added 1.3.0.2 -- apply op to subgraph outlet
1821 if( ugen->inlet() )
1822 ugen->inlet()->m_op = op;
1823 if( ugen->outlet() )
1824 ugen->outlet()->m_op = op;
1825 }
1826
1827 CK_DLL_MFUN( ugen_cget_op )
1828 {
1829 // get as ugen
1830 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1831 // set return
1832 RETURN->v_int = ugen->m_op;
1833 }
1834
1835 CK_DLL_MFUN( ugen_last )
1836 {
1837 // get as ugen
1838 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1839 // set return
1840 // added 1.3.0.2 -- ugen outlet() last if subgraph
1841 if( ugen->outlet() )
1842 RETURN->v_float = (t_CKFLOAT) ugen->outlet()->m_last;
1843 else
1844 RETURN->v_float = (t_CKFLOAT)ugen->m_last;
1845 }
1846
1847 CK_DLL_MFUN( ugen_next )
1848 {
1849 // get as ugen
1850 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1851 // get arg
1852 t_CKFLOAT next = GET_CK_FLOAT( ARGS );
1853 // set op
1854 ugen->m_next = (SAMPLE)next;
1855 ugen->m_use_next = TRUE;
1856 // set return
1857 RETURN->v_float = (t_CKFLOAT)ugen->m_next;
1858
1859 // for multiple channels
1860 Chuck_DL_Return ret;
1861 // added 1.3.0.0 -- Chuck_DL_Api::instance()
1862 for( t_CKUINT i = 0; i < ugen->m_multi_chan_size; i++ )
1863 ugen_next( ugen->m_multi_chan[i], ARGS, &ret, VM, SHRED, API );
1864 // added 1.3.0.2 -- apply op to subgraph outlet
1865 if( ugen->outlet() )
1866 {
1867 ugen->outlet()->m_next = (SAMPLE) next;
1868 ugen->outlet()->m_use_next = TRUE;
1869 }
1870 }
1871
1872 CK_DLL_MFUN( ugen_cget_next )
1873 {
1874 // get as ugen
1875 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1876 // set return
1877 RETURN->v_float = ugen->m_use_next ? (t_CKFLOAT)ugen->m_next : 0.0f;
1878 }
1879
1880 CK_DLL_MFUN( ugen_gain )
1881 {
1882 // get as ugen
1883 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1884 // get arg
1885 t_CKFLOAT gain = GET_CK_FLOAT( ARGS );
1886 // set op
1887 ugen->m_gain = (SAMPLE)gain;
1888 // set return
1889 RETURN->v_float = (t_CKFLOAT)ugen->m_gain;
1890
1891 // for multiple channels
1892 Chuck_DL_Return ret;
1893 // added 1.3.0.0 -- Chuck_DL_Api::instance()
1894 for( t_CKUINT i = 0; i < ugen->m_multi_chan_size; i++ )
1895 ugen_gain( ugen->m_multi_chan[i], ARGS, &ret, VM, SHRED, API );
1896 // added 1.3.0.2 -- apply gain to subgraph outlet
1897 if( ugen->outlet() )
1898 ugen->outlet()->m_gain = (SAMPLE) gain;
1899 }
1900
1901 CK_DLL_MFUN( ugen_cget_gain )
1902 {
1903 // get as ugen
1904 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1905 // set return
1906 RETURN->v_float = (t_CKFLOAT)ugen->m_gain;
1907 }
1908
1909 CK_DLL_CTRL( ugen_numChannels )
1910 {
1911 // get ugen
1912 //Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1913 // error
1914 EM_error3( "setting .numChannels is not yet supported (use the command line)..." );
1915 }
1916
1917 CK_DLL_CGET( ugen_cget_numChannels )
1918 {
1919 // get ugen
1920 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1921 // return
1922 RETURN->v_int = ugen->m_multi_chan_size == 0 ? 1 : ugen->m_multi_chan_size;
1923 }
1924
1925 CK_DLL_CTRL( ugen_chan )
1926 {
1927 // get ugen
1928 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1929 t_CKINT num = GET_NEXT_INT(ARGS);
1930 // check
1931 if( !ugen->m_multi_chan_size && num == 0 )
1932 RETURN->v_object = ugen;
1933 else if( num >= 0 && num < ugen->m_multi_chan_size )
1934 RETURN->v_object = ugen->m_multi_chan[num];
1935 else
1936 RETURN->v_object = NULL;
1937 }
1938
1939 CK_DLL_CTRL( ugen_connected )
1940 {
1941 // get ugen
1942 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1943 Chuck_UGen * right = (Chuck_UGen *)GET_NEXT_OBJECT(ARGS);
1944
1945 // sanity
1946 t_CKINT ret = FALSE;
1947 if( !right )
1948 {
1949 ret = FALSE;
1950 }
1951 else
1952 {
1953 ret = right->is_connected_from( ugen );
1954 }
1955
1956 RETURN->v_int = ret;
1957 }
1958
1959 // 1.4.1.0 (jack): "buffered" flag -- necessary for getUGenSamples()
1960 // for global UGens
1961 CK_DLL_MFUN( ugen_buffered )
1962 {
1963 // get as ugen
1964 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1965 // get arg
1966 t_CKINT buffered = GET_CK_INT( ARGS );
1967 // set op
1968 ugen->set_is_buffered( buffered );
1969 // set return
1970 RETURN->v_int = ugen->m_is_buffered;
1971
1972 // for multiple channels
1973 Chuck_DL_Return ret;
1974 for( t_CKUINT i = 0; i < ugen->m_multi_chan_size; i++ )
1975 ugen_buffered( ugen->m_multi_chan[i], ARGS, &ret, VM, SHRED, API );
1976 }
1977
1978 CK_DLL_MFUN( ugen_cget_buffered )
1979 {
1980 // get as ugen
1981 Chuck_UGen * ugen = (Chuck_UGen *)SELF;
1982 // set return
1983 RETURN->v_int = ugen->m_is_buffered;
1984 }
1985
1986
1987 // ctor
1988 CK_DLL_CTOR( uana_ctor )
1989 {
1990 // make an actual blob
1991 Chuck_Object * blob = instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_uanablob, SHRED );
1992 // check
1993 if( blob == NULL )
1994 {
1995 // error message
1996 EM_error3( "UAna constructor unable to instantiate UAnaBlob; bailing out!" );
1997 // everything has gone to poop
1998 assert( FALSE );
1999 // not even gonna get here
2000 return;
2001 }
2002 // make a blob proxy
2003 Chuck_UAnaBlobProxy * proxy = new Chuck_UAnaBlobProxy( blob );
2004 // remember it
2005 OBJ_MEMBER_INT(SELF, uana_offset_blob) = (t_CKINT)proxy;
2006 // HACK: DANGER: manually call blob's ctor (added 1.3.0.0 -- Chuck_DL_Api::instance())
2007 uanablob_ctor( blob, NULL, VM, SHRED, API );
2008 }
2009
2010 CK_DLL_DTOR( uana_dtor )
2011 {
2012 // get the blob
2013 Chuck_UAnaBlobProxy * blob = (Chuck_UAnaBlobProxy *)OBJ_MEMBER_INT(SELF, uana_offset_blob);
2014 // delete the blob proxy | 1.5.0.0 (ge) added
2015 CK_SAFE_DELETE( blob ); // this should also clean up actual blob reference
2016 // zero out
2017 OBJ_MEMBER_INT(SELF, uana_offset_blob) = 0;
2018 }
2019
2020 CK_DLL_MFUN( uana_upchuck )
2021 {
2022 // get as uana
2023 Chuck_UAna * uana = (Chuck_UAna *)SELF;
2024 // get shred
2025 Chuck_VM_Shred * derhs = SHRED;
2026 // make sure it's not NULL
2027 if( !derhs )
2028 {
2029 EM_error3( "UAna.upchuck() encountered NULL shred; operation aborting!" );
2030 return;
2031 }
2032
2033 // get VM
2034 Chuck_VM * vm = derhs->vm_ref;
2035 // ensure
2036 if( !vm )
2037 {
2038 EM_error3( "UAna.upchuck() encountered NULL VM ref; operation aborting!" );
2039 return;
2040 }
2041
2042 // check if time
2043 if( uana->m_uana_time < vm->shreduler()->now_system )
2044 {
2045 // for multiple channels
2046 Chuck_DL_Return ret;
2047 // added 1.3.0.0 -- Chuck_DL_Api::instance()
2048 for( t_CKUINT i = 0; i < uana->m_multi_chan_size; i++ )
2049 uana_upchuck( uana->m_multi_chan[i], ARGS, &ret, VM, SHRED, API );
2050
2051 // tock it (TODO: order relative to multiple channels?)
2052 uana->system_tock( vm->shreduler()->now_system );
2053 }
2054
2055 // return
2056 RETURN->v_object = uana->blobProxy()->realblob();
2057 }
2058
2059 /* CK_DLL_MFUN( uana_blob )
2060 {
2061 // get as uana
2062 Chuck_UAna * uana = (Chuck_UAna *)SELF;
2063
2064 // TODO: return
2065 RETURN->v_object = NULL;
2066 } */
2067
2068 CK_DLL_MFUN( uana_fvals )
2069 {
2070 // get the fvals array
2071 Chuck_UAnaBlobProxy * blob = (Chuck_UAnaBlobProxy *)OBJ_MEMBER_INT(SELF, uana_offset_blob);
2072 RETURN->v_object = &blob->fvals();
2073 }
2074
2075 CK_DLL_MFUN( uana_cvals )
2076 {
2077 // get the fvals array
2078 Chuck_UAnaBlobProxy * blob = (Chuck_UAnaBlobProxy *)OBJ_MEMBER_INT(SELF, uana_offset_blob);
2079 RETURN->v_object = &blob->cvals();
2080 }
2081
2082 CK_DLL_MFUN( uana_fval )
2083 {
2084 // get index
2085 t_CKINT i = GET_NEXT_INT(ARGS);
2086 // get the fvals array
2087 Chuck_UAnaBlobProxy * blob = (Chuck_UAnaBlobProxy *)OBJ_MEMBER_INT(SELF, uana_offset_blob);
2088 Chuck_ArrayFloat & fvals = blob->fvals();
2089 // check caps
2090 if( i < 0 || fvals.size() <= i ) RETURN->v_float = 0;
2091 else
2092 {
2093 // get
2094 t_CKFLOAT val;
2095 fvals.get( i, &val );
2096 RETURN->v_float = val;
2097 }
2098 }
2099
2100 CK_DLL_MFUN( uana_cval )
2101 {
2102 // get index
2103 t_CKINT i = GET_NEXT_INT(ARGS);
2104 // get the fvals array
2105 Chuck_UAnaBlobProxy * blob = (Chuck_UAnaBlobProxy *)OBJ_MEMBER_INT(SELF, uana_offset_blob);
2106 Chuck_ArrayVec2 & cvals = blob->cvals();
2107 // check caps
2108 if( i < 0 || cvals.size() <= i ) RETURN->v_complex.re = RETURN->v_complex.im = 0;
2109 else
2110 {
2111 // get as vec2
2112 t_CKVEC2 val;
2113 cvals.get( i, &val );
2114 RETURN->v_vec2 = val;
2115 }
2116 }
2117
2118 CK_DLL_MFUN( uana_connected )
2119 {
2120 // get ugen
2121 Chuck_UAna * uana = (Chuck_UAna *)SELF;
2122 Chuck_UAna * right = (Chuck_UAna *)GET_NEXT_OBJECT(ARGS);
2123
2124 // sanity
2125 t_CKINT ret = FALSE;
2126 if( !right )
2127 {
2128 ret = FALSE;
2129 }
2130 else
2131 {
2132 ret = right->is_up_connected_from( uana );
2133 }
2134
2135 RETURN->v_int = ret;
2136 }
2137
2138
2139 // blob proxy implementation
2140 Chuck_UAnaBlobProxy::Chuck_UAnaBlobProxy( Chuck_Object * blob )
2141 {
2142 m_blob = blob;
2143 assert( m_blob != NULL );
2144 // add reference
2145 m_blob->add_ref();
2146 }
2147
2148 Chuck_UAnaBlobProxy::~Chuck_UAnaBlobProxy()
2149 {
2150 // release
2151 CK_SAFE_RELEASE( m_blob );
2152 }
2153
2154 t_CKTIME & Chuck_UAnaBlobProxy::when()
2155 {
2156 // TODO: DANGER: is this actually returning correct reference?!
2157 return OBJ_MEMBER_TIME(m_blob, uanablob_offset_when);
2158 }
2159
2160 Chuck_ArrayFloat & Chuck_UAnaBlobProxy::fvals()
2161 {
2162 // TODO: DANGER: is this actually returning correct reference?!
2163 Chuck_ArrayFloat * arr8 = (Chuck_ArrayFloat *)OBJ_MEMBER_INT(m_blob, uanablob_offset_fvals);
2164 assert( arr8 != NULL );
2165 return *arr8;
2166 }
2167
2168 Chuck_ArrayVec2 & Chuck_UAnaBlobProxy::cvals()
2169 {
2170 // TODO: DANGER: is this actually returning correct reference?!
2171 Chuck_ArrayVec2 * arr16 = (Chuck_ArrayVec2 *)OBJ_MEMBER_INT(m_blob, uanablob_offset_cvals);
2172 assert( arr16 != NULL );
2173 return *arr16;
2174 }
2175
2176 // get proxy
2177 Chuck_UAnaBlobProxy * getBlobProxy( const Chuck_UAna * uana )
2178 {
2179 return (Chuck_UAnaBlobProxy *)OBJ_MEMBER_INT(uana, uana_offset_blob);
2180 }
2181
2182 // ctor
2183 CK_DLL_CTOR( uanablob_ctor )
2184 {
2185 // when
2186 OBJ_MEMBER_TIME(SELF, uanablob_offset_when) = 0;
2187
2188 // fvals
2189 Chuck_ArrayFloat * arrF = new Chuck_ArrayFloat( 8 );
2190 initialize_object( arrF, SHRED->vm_ref->env()->ckt_array, SHRED, VM );
2191 CK_SAFE_ADD_REF( arrF );
2192 OBJ_MEMBER_INT(SELF, uanablob_offset_fvals) = (t_CKINT)arrF;
2193
2194 // cvals (complex)
2195 Chuck_ArrayVec2 * arrC = new Chuck_ArrayVec2( 8 );
2196 initialize_object( arrC, SHRED->vm_ref->env()->ckt_array, SHRED, VM );
2197 CK_SAFE_ADD_REF( arrC );
2198 OBJ_MEMBER_INT(SELF, uanablob_offset_cvals) = (t_CKINT)arrC;
2199 }
2200
2201 // dtor
2202 CK_DLL_DTOR( uanablob_dtor )
2203 {
2204 // set when to 0 for good measure
2205 OBJ_MEMBER_TIME(SELF, uanablob_offset_when) = 0;
2206
2207 // typed object mvars are auto-released | 1.5.2.0
2208 // include uanablob_offset_fvals and uanablob_offset_cvals
2209 }
2210
2211 CK_DLL_MFUN( uanablob_when )
2212 {
2213 // set return
2214 RETURN->v_time = OBJ_MEMBER_TIME(SELF, uanablob_offset_when);
2215 }
2216
2217 CK_DLL_MFUN( uanablob_fvals )
2218 {
2219 // set return
2220 RETURN->v_object = (Chuck_ArrayFloat *)OBJ_MEMBER_INT(SELF, uanablob_offset_fvals);
2221 }
2222
2223 CK_DLL_MFUN( uanablob_fval )
2224 {
2225 // get index
2226 t_CKINT i = GET_NEXT_INT(ARGS);
2227 // get the fvals array
2228 Chuck_ArrayFloat * fvals = (Chuck_ArrayFloat *)OBJ_MEMBER_INT(SELF, uanablob_offset_fvals);
2229 // check caps
2230 if( i < 0 || fvals->size() <= i ) RETURN->v_float = 0;
2231 else
2232 {
2233 // get
2234 t_CKFLOAT val;
2235 fvals->get( i, &val );
2236 RETURN->v_float = val;
2237 }
2238 }
2239
2240 CK_DLL_MFUN( uanablob_cval )
2241 {
2242 // get index
2243 t_CKINT i = GET_NEXT_INT(ARGS);
2244 // get the fvals array
2245 Chuck_ArrayVec2 * cvals = (Chuck_ArrayVec2 *)OBJ_MEMBER_INT(SELF, uanablob_offset_cvals);
2246 // check caps
2247 if( i < 0 || cvals->size() <= i ) RETURN->v_complex.re = RETURN->v_complex.im = 0;
2248 else
2249 {
2250 // get
2251 t_CKVEC2 val;
2252 cvals->get( i, &val );
2253 RETURN->v_vec2 = val;
2254 }
2255 }
2256
2257 CK_DLL_MFUN( uanablob_cvals )
2258 {
2259 // set return
2260 RETURN->v_object = (Chuck_ArrayVec2 *)OBJ_MEMBER_INT(SELF, uanablob_offset_cvals);
2261 }
2262
2263 // ctor
2264 CK_DLL_CTOR( event_ctor )
2265 {
2266 // OBJ_MEMBER_INT(SELF, event_offset_data) = (t_CKUINT)new Data_Event;
2267 // Chuck_Event * event = (Chuck_Event *)SELF;
2268 }
2269
2270
2271 CK_DLL_DTOR( event_dtor )
2272 {
2273 // delete (Data_Event *)OBJ_MEMBER_INT(SELF, event_offset_data);
2274 }
2275
2276 CK_DLL_MFUN( event_signal )
2277 {
2278 Chuck_Event * d = (Chuck_Event *)SELF;
2279 d->signal_local();
2280 d->signal_global();
2281 }
2282
2283 CK_DLL_MFUN( event_broadcast )
2284 {
2285 Chuck_Event * d = (Chuck_Event *)SELF;
2286 d->broadcast_local();
2287 d->broadcast_global();
2288 }
2289
2290 CK_DLL_MFUN( event_wait )
2291 {
2292 // Chuck_Event * d = (Chuck_Event *)SELF;
2293 assert( FALSE );
2294 }
2295
2296 // default implementation
2297 CK_DLL_MFUN( event_can_wait )
2298 {
2299 RETURN->v_int = TRUE;
2300 }
2301
2302 // 1.5.1.5 (ge/andrew) added; default implementation
2303 CK_DLL_MFUN( event_waiting_on )
2304 {
2305 // do nothing here; this function could be overridden as needed
2306 // FYI get the calling shred with 'SHRED' parameter to this function
2307 }
2308
2309
2310
2311
2312 //-----------------------------------------------------------------------------
2313 // vec2 API
2314 //-----------------------------------------------------------------------------
2315 CK_DLL_MFUN( vec2_set )
2316 {
2317 // HACK: this is a particularly horrid cast from (Chuck_Object *)
2318 // t_CKVEC3 is neither a super- or sub-class of Chuck_Object...
2319 t_CKVEC2 * vec2 = (t_CKVEC2 *)SELF;
2320 // get index
2321 vec2->x = GET_NEXT_FLOAT(ARGS);
2322 vec2->y = GET_NEXT_FLOAT(ARGS);
2323 }
2324
2325 CK_DLL_MFUN( vec2_setAll )
2326 {
2327 // get data pointer
2328 t_CKVEC2 * vec2 = (t_CKVEC2 *)SELF;
2329 // get index
2330 vec2->x = vec2->y = GET_NEXT_FLOAT(ARGS);
2331 }
2332
2333 CK_DLL_MFUN( vec2_magnitude )
2334 {
2335 // get data pointer
2336 t_CKVEC2 * vec2 = (t_CKVEC2 *)SELF;
2337 // compute magnitude
2338 t_CKFLOAT mag = ::sqrt( vec2->x*vec2->x + vec2->y*vec2->y );
2339 // return
2340 RETURN->v_float = mag;
2341 }
2342
2343 CK_DLL_MFUN( vec2_normalize )
2344 {
2345 // get data pointer
2346 t_CKVEC2 * vec2 = (t_CKVEC2 *)SELF;
2347 // compute magnitude
2348 t_CKFLOAT mag = ::sqrt( vec2->x*vec2->x + vec2->y*vec2->y );
2349 // check for zero
2350 if( mag > 0 )
2351 {
2352 vec2->x /= mag;
2353 vec2->y /= mag;
2354 }
2355 }
2356
2357 CK_DLL_MFUN( vec2_dot ) // 1.5.4.2 (ge & azaday) added
2358 {
2359 // get lhs value
2360 t_CKVEC2 lhs = *((t_CKVEC2 *)SELF);
2361 // get rhs from argument
2362 t_CKVEC2 rhs = GET_NEXT_VEC2(ARGS);
2363 // compute dot product and set as return value
2364 RETURN->v_float = lhs.x*rhs.x + lhs.y*rhs.y;
2365 }
2366
2367
2368 //-----------------------------------------------------------------------------
2369 // vec3 API
2370 //-----------------------------------------------------------------------------
2371 CK_DLL_MFUN( vec3_set )
2372 {
2373 // HACK: this is a particularly horrid cast from (Chuck_Object *)
2374 // t_CKVEC3 is neither a super- or sub-class of Chuck_Object...
2375 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2376 // get index
2377 vec3->x = GET_NEXT_FLOAT(ARGS);
2378 vec3->y = GET_NEXT_FLOAT(ARGS);
2379 vec3->z = GET_NEXT_FLOAT(ARGS);
2380 }
2381
2382 CK_DLL_MFUN( vec3_setAll )
2383 {
2384 // get data pointer
2385 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2386 // get index
2387 vec3->x = vec3->y = vec3->z = GET_NEXT_FLOAT(ARGS);
2388 }
2389
2390 CK_DLL_MFUN( vec3_magnitude )
2391 {
2392 // get data pointer
2393 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2394 // compute magnitude
2395 t_CKFLOAT mag = ::sqrt( vec3->x*vec3->x + vec3->y*vec3->y + vec3->z*vec3->z );
2396 // return
2397 RETURN->v_float = mag;
2398 }
2399
2400 CK_DLL_MFUN( vec3_normalize )
2401 {
2402 // get data pointer
2403 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2404 // compute magnitude
2405 t_CKFLOAT mag = ::sqrt( vec3->x*vec3->x + vec3->y*vec3->y + vec3->z*vec3->z );
2406 // check for zero
2407 if( mag > 0 )
2408 {
2409 vec3->x /= mag;
2410 vec3->y /= mag;
2411 vec3->z /= mag;
2412 }
2413 }
2414
2415 CK_DLL_MFUN( vec3_dot ) // 1.5.4.2 (ge & azaday) added
2416 {
2417 // get lhs value
2418 t_CKVEC3 lhs = *((t_CKVEC3 *)SELF);
2419 // get rhs from argument
2420 t_CKVEC3 rhs = GET_NEXT_VEC3(ARGS);
2421 // compute dot product and set as return value
2422 RETURN->v_float = lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
2423 }
2424
2425 CK_DLL_MFUN( vec3_cross ) // 1.5.4.2 (ge & azaday) added
2426 {
2427 // get lhs value
2428 t_CKVEC3 lhs = *((t_CKVEC3 *)SELF);
2429 // get rhs from argument
2430 t_CKVEC3 rhs = GET_NEXT_VEC3(ARGS);
2431 // compute cross product and set as return value
2432 RETURN->v_vec3.x = lhs.y*rhs.z - lhs.z*rhs.y;
2433 RETURN->v_vec3.y = lhs.z*rhs.x - lhs.x*rhs.z;
2434 RETURN->v_vec3.z = lhs.x*rhs.y - lhs.y*rhs.x;
2435 }
2436
2437 CK_DLL_MFUN( vec3_interp )
2438 {
2439 // get data pointer
2440 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2441 // update value based on goal and slew
2442 vec3->x = (vec3->y - vec3->x) * vec3->z + vec3->x;
2443 // set return to value so it can used right after if desired
2444 RETURN->v_float = vec3->x;
2445 }
2446
2447 CK_DLL_MFUN( vec3_interp_delta_float )
2448 {
2449 // get data pointer
2450 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2451 // get index
2452 t_CKFLOAT delta = GET_NEXT_FLOAT(ARGS);
2453 // update value based on goal and slew
2454 vec3->x = (vec3->y - vec3->x) * vec3->z * delta + vec3->x;
2455 // set return to value so it can used right after if desired
2456 RETURN->v_float = vec3->x;
2457 }
2458
2459 CK_DLL_MFUN( vec3_interp_delta_dur )
2460 {
2461 // get data pointer
2462 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2463 // get index
2464 t_CKDUR delta = GET_NEXT_DUR(ARGS);
2465 // get srate
2466 t_CKFLOAT srate = SHRED->vm_ref->srate();
2467 // update value based on goal and slew
2468 vec3->x = (vec3->y - vec3->x) * vec3->z * (delta / srate) + vec3->x;
2469 // set return to value so it can used right after if desired
2470 RETURN->v_float = vec3->x;
2471 }
2472
2473 CK_DLL_MFUN( vec3_update_goal )
2474 {
2475 // get data pointer
2476 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2477 // get index
2478 t_CKDUR goal = GET_NEXT_DUR(ARGS);
2479 // set goal
2480 vec3->y = goal;
2481 }
2482
2483 CK_DLL_MFUN( vec3_update_goal_slew )
2484 {
2485 // get data pointer
2486 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2487 // get goal
2488 t_CKDUR goal = GET_NEXT_DUR(ARGS);
2489 // get slew
2490 t_CKDUR slew = GET_NEXT_DUR(ARGS);
2491 // set goal
2492 vec3->y = goal;
2493 // set slew
2494 vec3->z = slew;
2495 }
2496
2497 CK_DLL_MFUN( vec3_updateSet_goalAndValue )
2498 {
2499 // get data pointer
2500 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2501 // get goal
2502 t_CKDUR goalAndValue = GET_NEXT_DUR(ARGS);
2503 // set goal and value
2504 vec3->y = vec3->x = goalAndValue;
2505 }
2506
2507 CK_DLL_MFUN( vec3_updateSet_goalAndValue_slew )
2508 {
2509 // get data pointer
2510 t_CKVEC3 * vec3 = (t_CKVEC3 *)SELF;
2511 // get goal
2512 t_CKDUR goalAndValue = GET_NEXT_DUR(ARGS);
2513 // get slew
2514 t_CKDUR slew = GET_NEXT_DUR(ARGS);
2515 // set goal and value
2516 vec3->y = vec3->x = goalAndValue;
2517 // set slew
2518 vec3->z = slew;
2519 }
2520
2521
2522 //-----------------------------------------------------------------------------
2523 // vec4 API
2524 //-----------------------------------------------------------------------------
2525 CK_DLL_MFUN( vec4_set )
2526 {
2527 // HACK: this is a particularly horrid cast from (Chuck_Object *)
2528 // t_CKVEC3 is neither a super- or sub-class of Chuck_Object...
2529 t_CKVEC4 * vec4 = (t_CKVEC4 *)SELF;
2530 // get index
2531 vec4->x = GET_NEXT_FLOAT(ARGS);
2532 vec4->y = GET_NEXT_FLOAT(ARGS);
2533 vec4->z = GET_NEXT_FLOAT(ARGS);
2534 vec4->w = GET_NEXT_FLOAT(ARGS);
2535 }
2536
2537 CK_DLL_MFUN( vec4_setAll )
2538 {
2539 // get data pointer
2540 t_CKVEC4 * vec4 = (t_CKVEC4 *)SELF;
2541 // get index
2542 vec4->x = vec4->y = vec4->z = vec4->w = GET_NEXT_FLOAT(ARGS);
2543 }
2544
2545 CK_DLL_MFUN( vec4_magnitude )
2546 {
2547 // get data pointer
2548 t_CKVEC4 * vec4 = (t_CKVEC4 *)SELF;
2549 // compute magnitude
2550 t_CKFLOAT mag = ::sqrt( vec4->x*vec4->x + vec4->y*vec4->y
2551 + vec4->z*vec4->z + vec4->w*vec4->w );
2552 // return
2553 RETURN->v_float = mag;
2554 }
2555
2556 CK_DLL_MFUN( vec4_normalize )
2557 {
2558 // get data pointer
2559 t_CKVEC4 * vec4 = (t_CKVEC4 *)SELF;
2560 // compute magnitude
2561 t_CKFLOAT mag = ::sqrt( vec4->x*vec4->x + vec4->y*vec4->y
2562 + vec4->z*vec4->z + vec4->w*vec4->w );
2563 // check for zero
2564 if( mag > 0 )
2565 {
2566 vec4->x /= mag;
2567 vec4->y /= mag;
2568 vec4->z /= mag;
2569 vec4->w /= mag;
2570 }
2571 }
2572
2573 CK_DLL_MFUN( vec4_dot ) // 1.5.4.2 (ge & azaday) added
2574 {
2575 // get lhs value
2576 t_CKVEC4 lhs = *((t_CKVEC4 *)SELF);
2577 // get rhs from argument
2578 t_CKVEC4 rhs = GET_NEXT_VEC4(ARGS);
2579 // compute dot product and set as return value
2580 RETURN->v_float = lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
2581 }
2582
2583 CK_DLL_MFUN( vec4_cross ) // 1.5.4.2 (ge & azaday) added
2584 {
2585 // get lhs value
2586 t_CKVEC3 lhs = *((t_CKVEC3 *)SELF);
2587 // get rhs from argument
2588 t_CKVEC3 rhs = GET_NEXT_VEC3(ARGS);
2589 // compute cross product and set as return value
2590 // 4D cross product is undefined; as a shorthand we do a 3D cross product
2591 // w is ignored in the calcluation, and the resulting w component is set to 0
2592 RETURN->v_vec4.x = lhs.y*rhs.z - lhs.z*rhs.y;
2593 RETURN->v_vec4.y = lhs.z*rhs.x - lhs.x*rhs.z;
2594 RETURN->v_vec4.z = lhs.x*rhs.y - lhs.y*rhs.x;
2595 RETURN->v_vec4.w = 0;
2596 }
2597
2598
2599
2600
2601 //-----------------------------------------------------------------------------
2602 // Shred API
2603 //-----------------------------------------------------------------------------
2604
2605 //CK_DLL_CTOR( shred_ctor );
2606 //CK_DLL_DTOR( shred_dtor );
2607
2608 CK_DLL_MFUN( shred_exit )
2609 {
2610 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2611 // end the shred
2612 derhs->is_done = TRUE;
2613 derhs->is_running = FALSE;
2614 }
2615
2616 CK_DLL_MFUN( shred_running )
2617 {
2618 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2619 RETURN->v_int = derhs->is_running;
2620 }
2621
2622 CK_DLL_MFUN( shred_done )
2623 {
2624 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2625 RETURN->v_int = derhs->is_done;
2626 }
2627
2628 CK_DLL_MFUN( shred_clone )
2629 {
2630 }
2631
2632 CK_DLL_MFUN( shred_id )
2633 {
2634 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2635 // return the id
2636 RETURN->v_int = derhs->xid;
2637 }
2638
2639 CK_DLL_MFUN( shred_yield )
2640 {
2641 // get this shred pointer
2642 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2643 // yield the shred in question | 1.5.0.5 (ge) refactored to call yield()
2644 derhs->yield();
2645
2646 // WAS:
2647 // Chuck_VM * vm = derhs->vm_ref;
2648 // suspend
2649 // derhs->is_running = FALSE;
2650 // reshredule
2651 // vm->shreduler()->shredule( derhs, derhs->now );
2652 }
2653
2654 CK_DLL_MFUN( shred_numArgs )
2655 {
2656 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2657
2658 // get the number of arguments
2659 RETURN->v_int = derhs->args.size();
2660 }
2661
2662 CK_DLL_MFUN( shred_getArg )
2663 {
2664 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2665
2666 // get index
2667 t_CKINT i = GET_NEXT_INT(ARGS);
2668 // total
2669 t_CKINT num = derhs->args.size();
2670
2671 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2672 str->set( ( i < num ? derhs->args[i] : "" ) );
2673 RETURN->v_string = str;
2674 }
2675
2676 CK_DLL_MFUN( shred_sourcePath ) // added 1.3.0.0
2677 {
2678 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2679
2680 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2681 str->set( derhs->code->filename );
2682 RETURN->v_string = str;
2683 }
2684
2685 CK_DLL_MFUN( shred_sourceDir ) // added 1.3.0.0
2686 {
2687 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2688
2689 // new chuck string
2690 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2691 // set the content
2692 str->set( extract_filepath_dir(derhs->code->filename) );
2693
2694 RETURN->v_string = str;
2695 }
2696
2697 CK_DLL_MFUN( shred_sourceDir2 ) // added 1.3.2.0
2698 {
2699 Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF;
2700 // get num up
2701 t_CKINT i = GET_NEXT_INT(ARGS);
2702 // abs
2703 if( i < 0 ) i = -i;
2704
2705 // new chuck string
2706 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2707 // set the content
2708 str->set( extract_filepath_dir(derhs->code->filename) );
2709 // up
2710 str->set( dir_go_up( str->str(), i ) );
2711
2712 RETURN->v_string = str;
2713 }
2714
2715
2716 CK_DLL_SFUN( shred_fromId ) // added 1.3.2.0
2717 {
2718 t_CKINT shred_id = GET_NEXT_INT(ARGS);
2719
2720 Chuck_VM_Shred * derhs = SHRED->vm_ref->shreduler()->lookup(shred_id);
2721
2722 RETURN->v_object = derhs;
2723 }
2724
2725
2726 CK_DLL_MFUN( shred_ctrl_hintChildMemSize ) // 1.5.1.5
2727 {
2728 // get this pointer
2729 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2730 // get arg
2731 t_CKINT sizeInBytes = GET_NEXT_INT(ARGS);
2732 // set
2733 RETURN->v_int = shred->childSetMemSize( sizeInBytes );
2734 }
2735
2736
2737 CK_DLL_MFUN( shred_cget_hintChildMemSize ) // 1.5.1.5
2738 {
2739 // get this pointer
2740 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2741 // set
2742 RETURN->v_int = shred->childGetMemSize();
2743 }
2744
2745
2746 CK_DLL_MFUN( shred_ctrl_hintChildRegSize ) // 1.5.1.5
2747 {
2748 // get this pointer
2749 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2750 // get arg
2751 t_CKINT sizeInBytes = GET_NEXT_INT(ARGS);
2752 // set
2753 RETURN->v_int = shred->childSetRegSize( sizeInBytes );
2754 }
2755
2756
2757 CK_DLL_MFUN( shred_cget_hintChildRegSize ) // 1.5.1.5
2758 {
2759 // get this pointer
2760 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2761 // set
2762 RETURN->v_int = shred->childGetRegSize();
2763 }
2764
2765
2766 CK_DLL_MFUN( shred_parent ) // added 1.5.2.0 (nshaheed)
2767 {
2768 // get this pointer
2769 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2770 // get the parent
2771 Chuck_VM_Shred * parent = shred->parent;
2772 // set return value
2773 RETURN->v_object = parent;
2774 }
2775
2776
2777 CK_DLL_MFUN( shred_ancestor ) // added 1.5.2.0 (nshaheed)
2778 {
2779 // get this pointer
2780 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2781
2782 // iterate up until parent is null; it's possible that
2783 // ancestor() returns the calling shred, if called on
2784 // the top-level "ancestor" shred
2785 while( shred->parent != NULL )
2786 {
2787 // set curr as parent
2788 shred = shred->parent;
2789 }
2790
2791 // set return value
2792 RETURN->v_object = shred;
2793 }
2794
2795 CK_DLL_MFUN( shred_pc ) // added 1.5.4.2 (ge)
2796 {
2797 // get this pointer
2798 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2799 // set return value to shred program counter
2800 RETURN->v_int = shred->pc;
2801 }
2802
2803 CK_DLL_MFUN( shred_reg_stack_sp ) // added 1.5.4.2 (ge)
2804 {
2805 // get this pointer
2806 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2807 // set return value to shred register/operand stack pointer
2808 RETURN->v_int = (t_CKINT)shred->reg->sp;
2809 }
2810
2811 CK_DLL_MFUN( shred_mem_stack_sp ) // added 1.5.4.2 (ge)
2812 {
2813 // get this pointer
2814 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2815 // set return value to shred register/operand stack pointer
2816 RETURN->v_int = (t_CKINT)shred->mem->sp;
2817 }
2818
2819 CK_DLL_MFUN( shred_gc ) // added 1.5.2.0 (ge)
2820 {
2821 // get this pointer
2822 Chuck_VM_Shred * shred = (Chuck_VM_Shred *)SELF;
2823 // invoke manual per-shred GC pass
2824 shred->gc();
2825 }
2826
2827
2828
2829
2830 //-----------------------------------------------------------------------------
2831 // string API
2832 //-----------------------------------------------------------------------------
2833 CK_DLL_CTOR( string_ctor_str )
2834 {
2835 // self (the string being constructed)
2836 Chuck_String * self = (Chuck_String *)SELF;
2837 // get arg
2838 Chuck_String * rhs = GET_NEXT_STRING(ARGS);
2839 // set it
2840 self->set( rhs ? rhs->str() : "" );
2841 }
2842
2843 CK_DLL_MFUN( string_length )
2844 {
2845 Chuck_String * s = (Chuck_String *)SELF;
2846 RETURN->v_int = s->str().length();
2847 }
2848
2849 CK_DLL_MFUN( string_upper )
2850 {
2851 Chuck_String * s = (Chuck_String *)SELF;
2852 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2853 str->set( toupper( s->str() ) );
2854 RETURN->v_string = str;
2855 }
2856
2857 CK_DLL_MFUN( string_lower )
2858 {
2859 Chuck_String * s = (Chuck_String *)SELF;
2860 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2861 str->set( tolower( s->str() ) );
2862 RETURN->v_string = str;
2863 }
2864
2865 CK_DLL_MFUN( string_ltrim )
2866 {
2867 Chuck_String * s = (Chuck_String *)SELF;
2868 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2869 str->set( ltrim( s->str() ) );
2870 RETURN->v_string = str;
2871 }
2872
2873 CK_DLL_MFUN( string_rtrim )
2874 {
2875 Chuck_String * s = (Chuck_String *)SELF;
2876 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2877 str->set( rtrim( s->str() ) );
2878 RETURN->v_string = str;
2879 }
2880
2881 CK_DLL_MFUN( string_trim )
2882 {
2883 Chuck_String * s = (Chuck_String *)SELF;
2884 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2885 str->set( trim( s->str() ) );
2886 RETURN->v_string = str;
2887 }
2888
2889 CK_DLL_MFUN( string_toString )
2890 {
2891 Chuck_String * s = (Chuck_String *)SELF;
2892 RETURN->v_string = s;
2893 }
2894
2895 CK_DLL_MFUN(string_charAt)
2896 {
2897 Chuck_String * str = (Chuck_String *) SELF;
2898 t_CKINT index = GET_NEXT_INT(ARGS);
2899
2900 if(index < 0 || index >= str->str().length())
2901 {
2902 ck_throw_exception(SHRED, "IndexOutOfBounds", index);
2903 RETURN->v_int = -1;
2904 return;
2905 }
2906
2907 RETURN->v_int = str->str().at(index);
2908 }
2909
2910 CK_DLL_MFUN(string_charAt2)
2911 {
2912 Chuck_String * str = (Chuck_String *) SELF;
2913 t_CKINT index = GET_NEXT_INT(ARGS);
2914
2915 // NOTE: no checking for bounds; will return "" if index out of bounds
2916
2917 // the string to be changed
2918 std::string s = str->str();
2919 // make a new string for the return value
2920 Chuck_String * rs = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2921
2922 // check index
2923 if( index < 0 || index >= s.length() ) rs->set( "" );
2924 else rs->set( std::string( 1, s[index] ) );
2925
2926 // return value;
2927 RETURN->v_string = rs;
2928 }
2929
2930 CK_DLL_MFUN(string_setCharAt)
2931 {
2932 Chuck_String * str = (Chuck_String *) SELF;
2933 t_CKINT index = GET_NEXT_INT(ARGS);
2934 t_CKINT the_char = GET_NEXT_INT(ARGS);
2935
2936 // check bounds
2937 if( index < 0 || index >= str->str().length() )
2938 {
2939 ck_throw_exception(SHRED, "IndexOutOfBounds", index);
2940 RETURN->v_int = -1;
2941 return;
2942 }
2943
2944 // str->str.at(index) = the_char;
2945 std::string s = str->str();
2946 s.at(index) = the_char;
2947 str->set( s );
2948 RETURN->v_int = str->str().at(index);
2949 }
2950
2951 CK_DLL_MFUN(string_setCharAt2)
2952 {
2953 Chuck_String * str = (Chuck_String *) SELF;
2954 t_CKINT index = GET_NEXT_INT(ARGS);
2955 Chuck_String * charAsStr = GET_NEXT_STRING(ARGS);
2956
2957 // check for bounds
2958 if( index < 0 || index >= str->str().length() )
2959 {
2960 ck_throw_exception(SHRED, "IndexOutOfBounds", index);
2961 return;
2962 }
2963
2964 // the string to be changed
2965 std::string s = str->str();
2966 // check if empty string
2967 if( charAsStr == NULL )
2968 {
2969 ck_throw_exception(SHRED, "NullPointer");
2970 return;
2971 }
2972
2973 // the char as string
2974 std::string cs = charAsStr->str();
2975 // empty string
2976 if( cs == "" )
2977 {
2978 // erase the character
2979 s.erase( index, 1 );
2980 }
2981 else
2982 {
2983 // replace character with first character
2984 s.at(index) = cs[0];
2985 }
2986
2987 // copy into chuck string
2988 str->set( s );
2989
2990 // make a new string for the return value
2991 Chuck_String * rs = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2992 // set it with what was replaced
2993 rs->set( cs == "" ? "" : std::string( 1, cs[0]) );
2994 // return
2995 RETURN->v_string = rs;
2996 }
2997
2998
2999 CK_DLL_MFUN(string_appendChar)
3000 {
3001 Chuck_String * str = (Chuck_String *)SELF;
3002 t_CKINT the_char = GET_NEXT_INT(ARGS);
3003 char c = (char)the_char;
3004
3005 // get the internal string representation
3006 std::string s = str->str();
3007 // append the character
3008 s += c;
3009 // set it back
3010 str->set( s );
3011 // return the string itself
3012 RETURN->v_string = str;
3013 }
3014
3015 CK_DLL_MFUN(string_substring)
3016 {
3017 Chuck_String * str = (Chuck_String *) SELF;
3018 t_CKINT start = GET_NEXT_INT(ARGS);
3019
3020 if(start < 0 || start >= str->str().length())
3021 {
3022 ck_throw_exception(SHRED, "IndexOutOfBounds", start);
3023 RETURN->v_string = NULL;
3024 return;
3025 }
3026
3027 Chuck_String * ss = (Chuck_String *) instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3028 ss->set( str->str().substr(start) );
3029
3030 RETURN->v_string = ss;
3031 }
3032
3033 CK_DLL_MFUN(string_substringN)
3034 {
3035 Chuck_String * str = (Chuck_String *) SELF;
3036 t_CKINT start = GET_NEXT_INT(ARGS);
3037 t_CKINT length = GET_NEXT_INT(ARGS);
3038
3039 if(start < 0 || start >= str->str().length())
3040 {
3041 ck_throw_exception(SHRED, "IndexOutOfBounds", start);
3042 RETURN->v_string = NULL;
3043 return;
3044 }
3045
3046 if(length < 0 || start+length > str->str().length())
3047 {
3048 ck_throw_exception(SHRED, "IndexOutOfBounds", length);
3049 RETURN->v_string = NULL;
3050 return;
3051 }
3052
3053 Chuck_String * ss = (Chuck_String *) instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3054 ss->set( str->str().substr(start, length) );
3055
3056 RETURN->v_string = ss;
3057 }
3058
3059 CK_DLL_MFUN(string_insert)
3060 {
3061 Chuck_String * str = (Chuck_String *) SELF;
3062 t_CKINT position = GET_NEXT_INT(ARGS);
3063 Chuck_String * str2 = GET_NEXT_STRING(ARGS);
3064
3065 if(position < 0 || position >= str->str().length())
3066 {
3067 ck_throw_exception(SHRED, "IndexOutOfBounds", position);
3068 return;
3069 }
3070 if(str2 == NULL)
3071 {
3072 ck_throw_exception(SHRED, "NullPointer");
3073 return;
3074 }
3075
3076 // str->str.insert(position, str2->str);
3077 std::string s = str->str();
3078 s.insert( position, str2->str() );
3079 str->set( s );
3080 }
3081
3082 CK_DLL_MFUN(string_replace)
3083 {
3084 Chuck_String * str = (Chuck_String *) SELF;
3085 t_CKINT position = GET_NEXT_INT(ARGS);
3086 Chuck_String * str2 = GET_NEXT_STRING(ARGS);
3087
3088 if(position < 0 || position >= str->str().length())
3089 {
3090 ck_throw_exception(SHRED, "IndexOutOfBounds", position);
3091 return;
3092 }
3093 if(str2 == NULL)
3094 {
3095 ck_throw_exception(SHRED, "NullPointer");
3096 return;
3097 }
3098
3099 string::size_type length;
3100 if(position + str2->str().length() > str->str().length())
3101 length = str->str().length() - position;
3102 else
3103 length = str2->str().length();
3104
3105 // str->str.replace(position, length, str2->str);
3106 std::string s = str->str();
3107 s.replace( position, length, str2->str() );
3108 str->set( s );
3109 }
3110
3111 CK_DLL_MFUN(string_replaceN)
3112 {
3113 Chuck_String * str = (Chuck_String *) SELF;
3114 t_CKINT position = GET_NEXT_INT(ARGS);
3115 t_CKINT length = GET_NEXT_INT(ARGS);
3116 Chuck_String * str2 = GET_NEXT_STRING(ARGS);
3117
3118 if(position < 0 || position >= str->str().length())
3119 {
3120 ck_throw_exception(SHRED, "IndexOutOfBounds", position);
3121 return;
3122 }
3123
3124 if(length < 0 || position+length > str->str().length())
3125 {
3126 ck_throw_exception(SHRED, "IndexOutOfBounds", length);
3127 return;
3128 }
3129
3130 if(str2 == NULL)
3131 {
3132 ck_throw_exception(SHRED, "NullPointer");
3133 return;
3134 }
3135
3136 // str->str.replace(position, length, str2->str);
3137 std::string s = str->str();
3138 s.replace( position, length, str2->str() );
3139 str->set( s );
3140 }
3141
3142 // string.replace(old, new) | 1.5.1.3 (nshaheed) added
3143 CK_DLL_MFUN(string_replace_str)
3144 {
3145 // this is just a wrapper around std::replace
3146 Chuck_String * str = (Chuck_String *) SELF;
3147 std::string from = GET_NEXT_STRING_SAFE(ARGS);
3148 std::string to = GET_NEXT_STRING_SAFE(ARGS);
3149
3150 std::string s = str->str();
3151
3152 // Taken from https://stackoverflow.com/a/3418285/20065423
3153 if(from.empty())
3154 return;
3155 size_t start_pos = 0;
3156
3157 while((start_pos = s.find(from, start_pos)) != std::string::npos) {
3158 s.replace(start_pos, from.length(), to);
3159 start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
3160 }
3161
3162 str->set(s);
3163 }
3164
3165 CK_DLL_MFUN(string_find)
3166 {
3167 Chuck_String * str = (Chuck_String *) SELF;
3168 t_CKINT the_char = GET_NEXT_INT(ARGS);
3169
3170 string::size_type index = str->str().find(the_char);
3171
3172 if(index == string::npos)
3173 RETURN->v_int = -1;
3174 else
3175 RETURN->v_int = index;
3176 }
3177
3178 CK_DLL_MFUN(string_findStart)
3179 {
3180 Chuck_String * str = (Chuck_String *) SELF;
3181 t_CKINT the_char = GET_NEXT_INT(ARGS);
3182 t_CKINT start = GET_NEXT_INT(ARGS);
3183
3184 if(start < 0 || start >= str->str().length())
3185 {
3186 ck_throw_exception(SHRED, "IndexOutOfBounds", start);
3187 RETURN->v_int = -1;
3188 return;
3189 }
3190
3191 string::size_type index = str->str().find(the_char, start);
3192
3193 if(index == string::npos)
3194 RETURN->v_int = -1;
3195 else
3196 RETURN->v_int = index;
3197 }
3198
3199 CK_DLL_MFUN(string_findStr)
3200 {
3201 Chuck_String * str = (Chuck_String *) SELF;
3202 Chuck_String * the_str = GET_NEXT_STRING(ARGS);
3203
3204 if( the_str == NULL ) // 1.5.4.2 (ge) added
3205 {
3206 ck_throw_exception( SHRED, "NullPointer", "string argument in .find()" );
3207 return;
3208 }
3209
3210 string::size_type index = str->str().find(the_str->str());
3211
3212 if(index == string::npos)
3213 RETURN->v_int = -1;
3214 else
3215 RETURN->v_int = index;
3216 }
3217
3218 CK_DLL_MFUN(string_findStrStart)
3219 {
3220 Chuck_String * str = (Chuck_String *) SELF;
3221 Chuck_String * the_str = GET_NEXT_STRING(ARGS);
3222 t_CKINT start = GET_NEXT_INT(ARGS);
3223
3224 if( the_str == NULL ) // 1.5.4.2 (ge) added
3225 {
3226 ck_throw_exception( SHRED, "NullPointer", "string argument in .find()" );
3227 return;
3228 }
3229
3230 if(start < 0 || start >= str->str().length())
3231 {
3232 ck_throw_exception( SHRED, "IndexOutOfBounds", start );
3233 RETURN->v_int = -1;
3234 return;
3235 }
3236
3237 string::size_type index = str->str().find(the_str->str(), start);
3238
3239 if(index == string::npos)
3240 RETURN->v_int = -1;
3241 else
3242 RETURN->v_int = index;
3243 }
3244
3245 CK_DLL_MFUN(string_rfind)
3246 {
3247 Chuck_String * str = (Chuck_String *) SELF;
3248 t_CKINT the_char = GET_NEXT_INT(ARGS);
3249
3250 string::size_type index = str->str().rfind(the_char);
3251
3252 if(index == string::npos)
3253 RETURN->v_int = -1;
3254 else
3255 RETURN->v_int = index;
3256 }
3257
3258 CK_DLL_MFUN(string_rfindStart)
3259 {
3260 Chuck_String * str = (Chuck_String *) SELF;
3261 t_CKINT the_char = GET_NEXT_INT(ARGS);
3262 t_CKINT start = GET_NEXT_INT(ARGS);
3263
3264 if(start < 0 || start >= str->str().length())
3265 {
3266 ck_throw_exception(SHRED, "IndexOutOfBounds", start);
3267 RETURN->v_int = -1;
3268 return;
3269 }
3270
3271 string::size_type index = str->str().rfind(the_char, start);
3272
3273 if(index == string::npos)
3274 RETURN->v_int = -1;
3275 else
3276 RETURN->v_int = index;
3277 }
3278
3279 CK_DLL_MFUN(string_rfindStr)
3280 {
3281 Chuck_String * str = (Chuck_String *) SELF;
3282 Chuck_String * the_str = GET_NEXT_STRING(ARGS);
3283
3284 if( the_str == NULL ) // 1.5.4.2 (ge) added
3285 {
3286 ck_throw_exception( SHRED, "NullPointer", "string argument in .find()" );
3287 return;
3288 }
3289
3290 string::size_type index = str->str().rfind(the_str->str());
3291
3292 if(index == string::npos)
3293 RETURN->v_int = -1;
3294 else
3295 RETURN->v_int = index;
3296 }
3297
3298 CK_DLL_MFUN(string_rfindStrStart)
3299 {
3300 Chuck_String * str = (Chuck_String *) SELF;
3301 Chuck_String * the_str = GET_NEXT_STRING(ARGS);
3302 t_CKINT start = GET_NEXT_INT(ARGS);
3303
3304 if( the_str == NULL ) // 1.5.4.2 (ge) added
3305 {
3306 ck_throw_exception( SHRED, "NullPointer", "string argument in .find()" );
3307 return;
3308 }
3309
3310 if(start < 0 || start >= str->str().length())
3311 {
3312 ck_throw_exception(SHRED, "IndexOutOfBounds", start);
3313 RETURN->v_int = -1;
3314 return;
3315 }
3316
3317 string::size_type index = str->str().rfind(the_str->str(), start);
3318
3319 if(index == string::npos)
3320 RETURN->v_int = -1;
3321 else
3322 RETURN->v_int = index;
3323 }
3324
3325 CK_DLL_MFUN(string_erase)
3326 {
3327 Chuck_String * str = (Chuck_String *) SELF;
3328 t_CKINT start = GET_NEXT_INT(ARGS);
3329 t_CKINT length = GET_NEXT_INT(ARGS);
3330
3331 if(start < 0 || start >= str->str().length())
3332 {
3333 ck_throw_exception(SHRED, "IndexOutOfBounds", start);
3334 return;
3335 }
3336
3337 if(length < 0 || start+length > str->str().length())
3338 {
3339 ck_throw_exception(SHRED, "IndexOutOfBounds", length);
3340 return;
3341 }
3342
3343 // str->str.erase(start, length);
3344 std::string s = str->str();
3345 s.erase( start, length );
3346 str->set( s );
3347 }
3348
3349 CK_DLL_MFUN( string_toInt )
3350 {
3351 // get pointer to self
3352 Chuck_String * str = (Chuck_String *)SELF;
3353 // convert to int
3354 RETURN->v_int = ::atoi( str->str().c_str() );
3355 }
3356
3357 CK_DLL_MFUN( string_toFloat )
3358 {
3359 // get pointer to self
3360 Chuck_String * str = (Chuck_String *)SELF;
3361 // convert to int
3362 RETURN->v_float = (t_CKFLOAT)::atof( str->str().c_str() );
3363 }
3364
3365 CK_DLL_MFUN( string_parent )
3366 {
3367 Chuck_String * str = (Chuck_String *) SELF;
3368
3369 string::size_type i = str->str().rfind('/', str->str().length()-2);
3370 #ifdef __PLATFORM_WINDOWS__
3371 // SPENCERTODO: make this legit on windows
3372 if(i == string::npos)
3373 i = str->str().rfind('\\', str->str().length()-2);
3374 #endif // __PLATFORM_WINDOWS__
3375
3376 Chuck_String * parent = (Chuck_String *) instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3377
3378 if(i != string::npos)
3379 {
3380 if(i == 0)
3381 parent->set( "/" );
3382 else
3383 parent->set( str->str().substr(0, i) );
3384 }
3385
3386 RETURN->v_string = parent;
3387 }
3388
3389 /*
3390 CK_DLL_MFUN( string_set_at )
3391 {
3392 Chuck_String * s = (Chuck_String *)SELF;
3393 t_CKINT c = GET_CK_INT(
3394 RETURN->v_int = s->str.length();
3395 }
3396
3397 CK_DLL_MFUN( string_get_at )
3398 {
3399 Chuck_String * s = (Chuck_String *)SELF;
3400 RETURN->v_int = s->str.length();
3401 }
3402 */
3403
3404 // 1.5.4.2 (ge) added
3405 CK_DLL_GFUN( string_op_string_plus_string )
3406 {
3407 Chuck_String * left = GET_NEXT_STRING(ARGS);
3408 Chuck_String * right = GET_NEXT_STRING(ARGS);
3409
3410 if( left == NULL || right == NULL ) // 1.5.4.2 (ge) added
3411 {
3412 ck_throw_exception( SHRED, "NullPointer", "argument(s) to @operator+(string,string)" );
3413 return;
3414 }
3415
3416 string lhs = left->str();
3417 string rhs = right->str();
3418
3419 // return value
3420 RETURN->v_string = (Chuck_String *)instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3421 RETURN->v_string->set( lhs + rhs );
3422 }
3423
3424 CK_DLL_GFUN( string_op_int_plus_string )
3425 {
3426 t_CKINT left = GET_NEXT_INT(ARGS);
3427 Chuck_String * right = GET_NEXT_STRING(ARGS);
3428
3429 if( right == NULL ) // 1.5.4.2 (ge) added
3430 {
3431 ck_throw_exception( SHRED, "NullPointer", "argument(s) to @operator+(int,string)" );
3432 return;
3433 }
3434
3435 string rhs = right->str();
3436
3437 // return value
3438 RETURN->v_string = (Chuck_String *)instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3439 RETURN->v_string->set( std::to_string(left) + rhs );
3440 }
3441
3442 CK_DLL_GFUN( string_op_string_plus_int )
3443 {
3444 Chuck_String * left = GET_NEXT_STRING(ARGS);
3445 t_CKINT right = GET_NEXT_INT(ARGS);
3446
3447 if( left == NULL ) // 1.5.4.2 (ge) added
3448 {
3449 ck_throw_exception( SHRED, "NullPointer", "argument(s) to @operator+(string,int)" );
3450 return;
3451 }
3452
3453 string lhs = left->str();
3454
3455 // return value
3456 RETURN->v_string = (Chuck_String *)instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3457 RETURN->v_string->set( lhs + std::to_string(right) );
3458 }
3459
3460 CK_DLL_GFUN( string_op_float_plus_string )
3461 {
3462 t_CKFLOAT left = GET_NEXT_FLOAT(ARGS);
3463 Chuck_String * right = GET_NEXT_STRING(ARGS);
3464
3465 if( right == NULL ) // 1.5.4.2 (ge) added
3466 {
3467 ck_throw_exception( SHRED, "NullPointer", "argument(s) to @operator+(float,string)" );
3468 return;
3469 }
3470
3471 string rhs = right->str();
3472
3473 // return value
3474 RETURN->v_string = (Chuck_String *)instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3475 RETURN->v_string->set( std::to_string(left) + rhs );
3476 }
3477
3478 CK_DLL_GFUN( string_op_string_plus_float )
3479 {
3480 Chuck_String * left = GET_NEXT_STRING(ARGS);
3481 t_CKFLOAT right = GET_NEXT_FLOAT(ARGS);
3482
3483 if( left == NULL ) // 1.5.4.2 (ge) added
3484 {
3485 ck_throw_exception( SHRED, "NullPointer", "argument(s) to @operator+(string,float)" );
3486 return;
3487 }
3488
3489 string lhs = left->str();
3490
3491 // return value
3492 RETURN->v_string = (Chuck_String *)instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3493 RETURN->v_string->set( lhs + std::to_string(right) );
3494 }
3495
3496 CK_DLL_GFUN( string_op_string_eq_string )
3497 {
3498 Chuck_String * left = GET_NEXT_STRING(ARGS);
3499 Chuck_String * right = GET_NEXT_STRING(ARGS);
3500
3501 // if both non-null
3502 if( left && right ) RETURN->v_int = (left->str() == right->str());
3503 // only one is null
3504 else if( (left && !right) || (!left && right) ) RETURN->v_int = FALSE;
3505 // both null
3506 else RETURN->v_int = TRUE;
3507 }
3508
3509 CK_DLL_GFUN( string_op_string_neq_string )
3510 {
3511 Chuck_String * left = GET_NEXT_STRING(ARGS);
3512 Chuck_String * right = GET_NEXT_STRING(ARGS);
3513
3514 // if both non-null
3515 if( left && right ) RETURN->v_int = (left->str() != right->str());
3516 // if at least one is null
3517 else RETURN->v_int = FALSE;
3518 }
3519
3520 CK_DLL_GFUN( string_op_string_lt_string )
3521 {
3522 Chuck_String * left = GET_NEXT_STRING(ARGS);
3523 Chuck_String * right = GET_NEXT_STRING(ARGS);
3524
3525 // if both non-null
3526 if( left && right ) RETURN->v_int = (left->str() < right->str());
3527 // if at least one is null
3528 else RETURN->v_int = FALSE;
3529 }
3530
3531 CK_DLL_GFUN( string_op_string_le_string )
3532 {
3533 Chuck_String * left = GET_NEXT_STRING(ARGS);
3534 Chuck_String * right = GET_NEXT_STRING(ARGS);
3535
3536 // if both non-null
3537 if( left && right ) RETURN->v_int = (left->str() <= right->str());
3538 // if at least one is null
3539 else RETURN->v_int = FALSE;
3540 }
3541
3542 CK_DLL_GFUN( string_op_string_gt_string )
3543 {
3544 Chuck_String * left = GET_NEXT_STRING(ARGS);
3545 Chuck_String * right = GET_NEXT_STRING(ARGS);
3546
3547 // if both non-null
3548 if( left && right ) RETURN->v_int = (left->str() > right->str());
3549 // if at least one is null
3550 else RETURN->v_int = FALSE;
3551 }
3552
3553 CK_DLL_GFUN( string_op_string_ge_string )
3554 {
3555 Chuck_String * left = GET_NEXT_STRING(ARGS);
3556 Chuck_String * right = GET_NEXT_STRING(ARGS);
3557
3558 // if both non-null
3559 if( left && right ) RETURN->v_int = (left->str() >= right->str());
3560 // if at least one is null
3561 else RETURN->v_int = FALSE;
3562 }
3563
3564
3565 //-----------------------------------------------------------------------------
3566 // array API
3567 //-----------------------------------------------------------------------------
3568 CK_DLL_MFUN( array_get_size )
3569 {
3570 Chuck_Array * array = (Chuck_Array *)SELF;
3571 RETURN->v_int = array->size();
3572 }
3573
3574 // array.size()
3575 CK_DLL_MFUN( array_set_size )
3576 {
3577 Chuck_Array * array = (Chuck_Array *)SELF;
3578 t_CKINT size = GET_NEXT_INT(ARGS);
3579 if( size < 0 )
3580 {
3581 // TODO: make this exception
3582 CK_FPRINTF_STDERR( "[chuck](via array): attempt to set negative array size!\n" );
3583 RETURN->v_int = 0;
3584 }
3585 else
3586 {
3587 array->set_size( size );
3588 RETURN->v_int = array->size();
3589 }
3590 RETURN->v_int = array->size();
3591 }
3592
3593 // array.clear()
3594 CK_DLL_MFUN( array_clear )
3595 {
3596 Chuck_Array * array = (Chuck_Array *)SELF;
3597 array->clear();
3598 }
3599
3600 // array.reset()
3601 CK_DLL_MFUN( array_reset )
3602 {
3603 Chuck_Array * array = (Chuck_Array *)SELF;
3604 // default capacity
3605 array->set_size( 8 );
3606 // clear the array
3607 array->clear();
3608 }
3609
3610 // array.zero() | 1.5.0.0 (ge) added
3611 CK_DLL_MFUN( array_zero )
3612 {
3613 Chuck_Array * array = (Chuck_Array *)SELF;
3614 array->zero();
3615 }
3616
3617 // array.cap()
3618 CK_DLL_MFUN( array_set_capacity )
3619 {
3620 Chuck_Array * array = (Chuck_Array *)SELF;
3621 t_CKINT capacity = GET_NEXT_INT(ARGS);
3622 if( capacity < 0 )
3623 {
3624 // TODO: make this exception
3625 CK_FPRINTF_STDERR( "[chuck](via array): attempt to set negative array capacity!\n" );
3626 RETURN->v_int = 0;
3627 }
3628 else
3629 {
3630 array->set_capacity( capacity );
3631 RETURN->v_int = array->capacity();
3632 }
3633 }
3634
3635 // array.cap()
3636 CK_DLL_MFUN( array_get_capacity_hack )
3637 {
3638 Chuck_Array * array = (Chuck_Array *)SELF;
3639 RETURN->v_int = array->size();
3640 }
3641
3642 // array.capacity()
3643 CK_DLL_MFUN( array_get_capacity )
3644 {
3645 Chuck_Array * array = (Chuck_Array *)SELF;
3646 RETURN->v_int = array->capacity();
3647 }
3648
3649 // array.find()
3650 CK_DLL_MFUN( array_map_find )
3651 {
3652 Chuck_Array * array = (Chuck_Array *)SELF;
3653 Chuck_String * name = GET_NEXT_STRING( ARGS );
3654 RETURN->v_int = array->map_find( name->str() );
3655 }
3656
3657 // array.erase()
3658 CK_DLL_MFUN( array_map_erase )
3659 {
3660 Chuck_Array * array = (Chuck_Array *)SELF;
3661 Chuck_String * name = GET_NEXT_STRING( ARGS );
3662 RETURN->v_int = array->map_erase( name->str() );
3663 }
3664
3665 // array.shuffle()
3666 CK_DLL_MFUN( array_shuffle )
3667 {
3668 Chuck_Array * array = (Chuck_Array *)SELF;
3669 array->shuffle();
3670 }
3671
3672 // array.sort()
3673 CK_DLL_MFUN( array_sort )
3674 {
3675 Chuck_Array * array = (Chuck_Array *)SELF;
3676 array->sort();
3677 }
3678
3679 // array.push_back()
3680 CK_DLL_MFUN( array_push_back )
3681 {
3682 Chuck_Array * array = (Chuck_Array *)SELF;
3683 // ISSUE: 64-bit (fixed 1.3.1.0 using data kind)
3684 if( array->data_type_kind() == CHUCK_ARRAYINT_DATAKIND )
3685 RETURN->v_int = ((Chuck_ArrayInt *)array)->push_back( GET_NEXT_UINT( ARGS ) );
3686 else if( array->data_type_kind() == CHUCK_ARRAYFLOAT_DATAKIND )
3687 RETURN->v_int = ((Chuck_ArrayFloat *)array)->push_back( GET_NEXT_FLOAT( ARGS ) );
3688 else if( array->data_type_kind() == CHUCK_ARRAYVEC2_DATAKIND )
3689 RETURN->v_int = ((Chuck_ArrayVec2 *)array)->push_back( GET_NEXT_VEC2( ARGS ) );
3690 else if( array->data_type_kind() == CHUCK_ARRAYVEC3_DATAKIND )
3691 RETURN->v_int = ((Chuck_ArrayVec3 *)array)->push_back( GET_NEXT_VEC3( ARGS ) );
3692 else if( array->data_type_kind() == CHUCK_ARRAYVEC4_DATAKIND )
3693 RETURN->v_int = ((Chuck_ArrayVec4 *)array)->push_back( GET_NEXT_VEC4( ARGS ) );
3694 else
3695 assert( FALSE );
3696 }
3697
3698
3699 // array.insert()
3700 CK_DLL_MFUN( array_insert )
3701 {
3702 Chuck_Array * array = (Chuck_Array *)SELF;
3703 t_CKINT position = GET_NEXT_INT( ARGS );
3704
3705 // ISSUE: 64-bit (fixed 1.3.1.0 using data kind)
3706 if( array->data_type_kind() == CHUCK_ARRAYINT_DATAKIND )
3707 RETURN->v_int = ((Chuck_ArrayInt *)array)->insert( position, GET_NEXT_UINT( ARGS ) );
3708 else if( array->data_type_kind() == CHUCK_ARRAYFLOAT_DATAKIND )
3709 RETURN->v_int = ((Chuck_ArrayFloat *)array)->insert( position, GET_NEXT_FLOAT( ARGS ) );
3710 else if( array->data_type_kind() == CHUCK_ARRAYVEC2_DATAKIND )
3711 RETURN->v_int = ((Chuck_ArrayVec2 *)array)->insert( position, GET_NEXT_VEC2( ARGS ) );
3712 else if( array->data_type_kind() == CHUCK_ARRAYVEC3_DATAKIND )
3713 RETURN->v_int = ((Chuck_ArrayVec3 *)array)->insert( position, GET_NEXT_VEC3( ARGS ) );
3714 else if( array->data_type_kind() == CHUCK_ARRAYVEC4_DATAKIND )
3715 RETURN->v_int = ((Chuck_ArrayVec4 *)array)->insert( position, GET_NEXT_VEC4( ARGS ) );
3716 else
3717 assert( FALSE );
3718 }
3719
3720
3721 // array.pop_back()
3722 CK_DLL_MFUN( array_pop_back )
3723 {
3724 Chuck_Array * array = (Chuck_Array *)SELF;
3725 RETURN->v_int = array->pop_back();
3726 }
3727
3728 // array.push_front()
3729 CK_DLL_MFUN( array_push_front )
3730 {
3731 Chuck_Array * array = (Chuck_Array *)SELF;
3732 // ISSUE: 64-bit (fixed 1.3.1.0 using data kind)
3733 if( array->data_type_kind() == CHUCK_ARRAYINT_DATAKIND )
3734 RETURN->v_int = ((Chuck_ArrayInt *)array)->push_front( GET_NEXT_UINT( ARGS ) );
3735 else if( array->data_type_kind() == CHUCK_ARRAYFLOAT_DATAKIND )
3736 RETURN->v_int = ((Chuck_ArrayFloat *)array)->push_front( GET_NEXT_FLOAT( ARGS ) );
3737 else if( array->data_type_kind() == CHUCK_ARRAYVEC2_DATAKIND )
3738 RETURN->v_int = ((Chuck_ArrayVec2 *)array)->push_front( GET_NEXT_VEC2( ARGS ) );
3739 else if( array->data_type_kind() == CHUCK_ARRAYVEC3_DATAKIND )
3740 RETURN->v_int = ((Chuck_ArrayVec3 *)array)->push_front( GET_NEXT_VEC3( ARGS ) );
3741 else if( array->data_type_kind() == CHUCK_ARRAYVEC4_DATAKIND )
3742 RETURN->v_int = ((Chuck_ArrayVec4 *)array)->push_front( GET_NEXT_VEC4( ARGS ) );
3743 else
3744 assert( FALSE );
3745 }
3746
3747 // array.pop_front()
3748 CK_DLL_MFUN( array_pop_front )
3749 {
3750 Chuck_Array * array = (Chuck_Array *)SELF;
3751 RETURN->v_int = array->pop_front();
3752 }
3753
3754 // array.erase
3755 CK_DLL_MFUN( array_erase )
3756 {
3757 Chuck_Array * array = (Chuck_Array *)SELF;
3758 t_CKINT position = GET_NEXT_INT(ARGS);
3759 RETURN->v_int = array->erase( position );
3760 }
3761
3762 // array.erase
3763 CK_DLL_MFUN( array_erase2 )
3764 {
3765 Chuck_Array * array = (Chuck_Array *)SELF;
3766 t_CKINT begin = GET_NEXT_INT(ARGS);
3767 t_CKINT end = GET_NEXT_INT(ARGS);
3768 RETURN->v_int = array->erase( begin, end );
3769 }
3770
3771 // 1.4.1.1 nshaheed (added) array.getKeys()
3772 CK_DLL_MFUN( array_get_keys )
3773 {
3774 Chuck_Array * array = (Chuck_Array *)SELF;
3775 Chuck_ArrayInt * returned_keys = (Chuck_ArrayInt *) GET_NEXT_OBJECT(ARGS);
3776
3777 // clear return array
3778 returned_keys->set_size(0);
3779 // local keys array
3780 std::vector<std::string> array_keys;
3781 // get the keys
3782 array->get_keys( array_keys );
3783
3784 // copy array keys into the provided string array
3785 for (t_CKUINT i = 0; i < array_keys.size(); i++ ) {
3786 Chuck_String * key = (Chuck_String *)instantiate_and_initialize_object(SHRED->vm_ref->env()->ckt_string, SHRED);
3787 key->set(array_keys[i]);
3788 returned_keys->push_back((t_CKUINT) key);
3789 }
3790 }
3791
3792 // 1.5.0.0 azaday (added) array.reverse()
3793 CK_DLL_MFUN( array_reverse )
3794 {
3795 Chuck_Array * array = (Chuck_Array *)SELF;
3796 array->reverse();
3797 }
3798
3799
3800
3801 //-----------------------------------------------------------------------------
3802 // Type implementation
3803 // 1.5.0.0 (ge) added
3804 //-----------------------------------------------------------------------------
3805 static void typeGetTypes(
3806 Chuck_VM * vm,
3807 Chuck_ArrayInt * ret,
3808 t_CKBOOL isObj,
3809 t_CKBOOL isPrim,
3810 t_CKBOOL isSpecial,
3811 t_CKBOOL isBuiltin,
3812 t_CKBOOL isChug,
3813 t_CKBOOL isImport,
3814 t_CKBOOL isUser )
3815 {
3816 // results
3817 vector<Chuck_Type *> types;
3818 // get types
3819 vm->env()->nspc_top()->get_types( types );
3820 // clear
3821 ret->m_vector.clear();
3822 // iterate
3823 for( t_CKINT i = 0; i < types.size(); i++ )
3824 {
3825 // check special
3826 // 1.5.5.8 (ge) account for "auto"
3827 t_CKBOOL special = (types[i]->base_name.length()>0 &&
3828 (types[i]->base_name[0] == '@' ||
3829 types[i]->xid == te_auto) );
3830 // if not requesting special types
3831 if( !isSpecial && (special == TRUE) ) continue;
3832 // check origin
3833 ckte_Origin origin = types[i]->originHint;
3834
3835 // filter level 1
3836 if( (isObj && isobj(vm->env(), types[i])) ||
3837 (isPrim && isprim(vm->env(), types[i])) )
3838 {
3839 // filter level 2
3840 if( (isBuiltin && (origin == ckte_origin_BUILTIN)) ||
3841 (isChug && (origin == ckte_origin_CHUGIN)) ||
3842 (isImport && (origin == ckte_origin_IMPORT)) ||
3843 (isUser && (origin == ckte_origin_USERDEFINED)) )
3844 {
3845 // copy
3846 ret->m_vector.push_back( (t_CKINT)types[i] );
3847 // add reference
3848 CK_SAFE_ADD_REF( types[i] );
3849 }
3850 }
3851 }
3852 }
3853
3854 CK_DLL_CTOR( type_ctor )
3855 {
3856 }
3857
3858 CK_DLL_DTOR( type_dtor )
3859 {
3860 }
3861
3862 CK_DLL_MFUN( type_equals )
3863 {
3864 // get self as type
3865 Chuck_Type * lhs = (Chuck_Type *)SELF;
3866 // get arg
3867 Chuck_Type * rhs = (Chuck_Type *)GET_NEXT_OBJECT(ARGS);
3868 // check
3869 if( rhs == NULL )
3870 {
3871 RETURN->v_int = FALSE;
3872 return;
3873 }
3874 // query
3875 RETURN->v_int = equals( lhs, rhs );
3876 }
3877
3878 CK_DLL_MFUN( type_isa )
3879 {
3880 // get self as type
3881 Chuck_Type * lhs = (Chuck_Type *)SELF;
3882 // get arg
3883 Chuck_Type * rhs = (Chuck_Type *)GET_NEXT_OBJECT(ARGS);
3884 // check
3885 if( rhs == NULL )
3886 {
3887 RETURN->v_int = FALSE;
3888 return;
3889 }
3890 // query
3891 RETURN->v_int = isa( lhs, rhs );
3892 }
3893
3894 CK_DLL_MFUN( type_isa_str )
3895 {
3896 // get self as type
3897 Chuck_Type * lhs = (Chuck_Type *)SELF;
3898 // get arg
3899 Chuck_String * another = GET_NEXT_STRING(ARGS);
3900 // default
3901 RETURN->v_int = FALSE;
3902
3903 // check
3904 if( another == NULL ) return;
3905 // get the type
3906 Chuck_Type * rhs = type_engine_find_type( VM->env(), another->str() );
3907 // check
3908 if( rhs == NULL ) return;
3909 // check
3910 RETURN->v_int = isa( lhs, rhs );
3911 }
3912
3913 CK_DLL_MFUN( type_basename )
3914 {
3915 // get self as type
3916 Chuck_Type * type = (Chuck_Type *)SELF;
3917 // new it
3918 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
3919 // set
3920 str->set( type->base_name );
3921 // return
3922 RETURN->v_object = str;
3923 }
3924
3925 CK_DLL_MFUN( type_name )
3926 {
3927 // get self as type
3928 Chuck_Type * type = (Chuck_Type *)SELF;
3929 // new it
3930 Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
3931 // construct the full type name (arrays included)
3932 string name = type->base_name;
3933 // append array depth
3934 for( t_CKINT i = 0; i < type->array_depth; i++ ) name += "[]";
3935 // set
3936 str->set( name );
3937 // return
3938 RETURN->v_object = str;
3939 }
3940
3941 CK_DLL_MFUN( type_parent )
3942 {
3943 // get self as type
3944 Chuck_Type * type = (Chuck_Type *)SELF;
3945 // get parent type
3946 RETURN->v_object = type->parent_type;
3947 }
3948
3949 CK_DLL_MFUN( type_children )
3950 {
3951 // get me
3952 Chuck_Type * me = (Chuck_Type *)SELF;
3953 // name
3954 string name = me->base_name;
3955
3956 // instantiate
3957 Chuck_ArrayInt * ret = new Chuck_ArrayInt( TRUE );
3958 // initialize as chuck object
3959 initialize_object( ret, VM->env()->ckt_array, SHRED, VM );
3960 // clear the vector
3961 ret->m_vector.clear();
3962
3963 // get the current top-most namespace
3964 Chuck_Namespace * nspc = VM->env()->nspc_top();
3965 // while we have a namespace
3966 while( nspc != NULL )
3967 {
3968 // results
3969 vector<Chuck_Type *> types;
3970 // get types
3971 nspc->get_types( types );
3972 // iterate
3973 for( t_CKINT i = 0; i < types.size(); i++ )
3974 {
3975 // skip me
3976 if( equals( types[i], me ) ) continue;
3977 // skip if not a subclass of me
3978 if( !isa( types[i], me ) ) continue;
3979 // append to vector
3980 ret->m_vector.push_back( (t_CKINT)types[i] );
3981 // add reference
3982 CK_SAFE_ADD_REF( types[i] );
3983 }
3984 // go up the namespace, e.g., from [user] to [global]
3985 nspc = nspc->parent;
3986 }
3987
3988 // return
3989 RETURN->v_object = ret;
3990 }
3991
3992 CK_DLL_MFUN( type_origin )
3993 {
3994 // me, the type
3995 Chuck_Type * type = (Chuck_Type *)SELF;
3996 // return type
3997 Chuck_String * origin = (Chuck_String *)instantiate_and_initialize_object( VM->env()->ckt_string, VM );
3998 // string
3999 string s = "";
4000 // check origin hint
4001 switch( type->originHint )
4002 {
4003 case ckte_origin_BUILTIN:
4004 s = "builtin";
4005 break;
4006 case ckte_origin_CHUGIN:
4007 s = "chugin";
4008 break;
4009 case ckte_origin_IMPORT:
4010 s = "cklib";
4011 break;
4012 case ckte_origin_USERDEFINED:
4013 s = "user";
4014 break;
4015 case ckte_origin_GENERATED:
4016 s = "generated";
4017 break;
4018
4019 case ckte_origin_UNKNOWN:
4020 default:
4021 s = "[unknown origin]";
4022 break;
4023 }
4024
4025 // set
4026 origin->set( s );
4027 // return
4028 RETURN->v_object = origin;
4029 }
4030
4031 CK_DLL_MFUN( type_isPrimitive )
4032 {
4033 Chuck_Type * type = (Chuck_Type *)SELF;
4034 RETURN->v_int = isprim( VM->env(), type );
4035 }
4036
4037 CK_DLL_MFUN( type_isObject )
4038 {
4039 Chuck_Type * type = (Chuck_Type *)SELF;
4040 RETURN->v_int = isobj( VM->env(), type );
4041 }
4042
4043 CK_DLL_MFUN( type_isArray )
4044 {
4045 Chuck_Type * type = (Chuck_Type *)SELF;
4046 // test for arrayhood -- either array depth > 0 or type could be "@array" | 1.5.2.0
4047 RETURN->v_int = type->array_depth > 0 || isa(type, type->env()->ckt_array);
4048 }
4049
4050 CK_DLL_MFUN( type_arrayDims )
4051 {
4052 Chuck_Type * type = (Chuck_Type *)SELF;
4053 RETURN->v_int = type->array_depth;
4054 }
4055
4056 CK_DLL_SFUN( type_findString )
4057 {
4058 // get arg
4059 Chuck_String * another = GET_NEXT_STRING(ARGS);
4060 // check
4061 if( another == NULL )
4062 {
4063 RETURN->v_object = NULL;
4064 return;
4065 }
4066 // get the type
4067 RETURN->v_object = type_engine_find_type( VM->env(), another->str() );
4068 }
4069
4070 CK_DLL_SFUN( type_typeOf_obj )
4071 {
4072 // get arg
4073 Chuck_Object * obj = GET_NEXT_OBJECT(ARGS);
4074 // check
4075 if( obj == NULL )
4076 {
4077 RETURN->v_object = NULL;
4078 return;
4079 }
4080
4081 // get type
4082 // NOTE: Chuck_Object->type_ref may have different semantics for typeOf() and Type.of()
4083 // [1.0] @=> float array[];
4084 // <<< array.typeOf().isArray() >>>;
4085 // <<< Type.of(array).isArray() >>>;
4086 // TODO: do these need to be unified?
4087 RETURN->v_object = obj->type_ref;
4088 }
4089
4090 CK_DLL_SFUN( type_typeOf_int )
4091 {
4092 RETURN->v_object = VM->env()->ckt_int;
4093 }
4094
4095 CK_DLL_SFUN( type_typeOf_float )
4096 {
4097 RETURN->v_object = VM->env()->ckt_float;
4098 }
4099
4100 CK_DLL_SFUN( type_typeOf_time )
4101 {
4102 RETURN->v_object = VM->env()->ckt_time;
4103 }
4104
4105 CK_DLL_SFUN( type_typeOf_dur )
4106 {
4107 RETURN->v_object = VM->env()->ckt_dur;
4108 }
4109
4110 CK_DLL_SFUN( type_typeOf_complex )
4111 {
4112 RETURN->v_object = VM->env()->ckt_complex;
4113 }
4114
4115 CK_DLL_SFUN( type_typeOf_polar )
4116 {
4117 RETURN->v_object = VM->env()->ckt_polar;
4118 }
4119
4120 CK_DLL_SFUN( type_typeOf_vec2 )
4121 {
4122 RETURN->v_object = VM->env()->ckt_vec2;
4123 }
4124
4125 CK_DLL_SFUN( type_typeOf_vec3 )
4126 {
4127 RETURN->v_object = VM->env()->ckt_vec3;
4128 }
4129
4130 CK_DLL_SFUN( type_typeOf_vec4 )
4131 {
4132 RETURN->v_object = VM->env()->ckt_vec4;
4133 }
4134
4135 CK_DLL_SFUN( type_getTypes )
4136 {
4137 // instantiate
4138 Chuck_ArrayInt * array = new Chuck_ArrayInt( TRUE );
4139 initialize_object( array, VM->env()->ckt_array, SHRED, VM );
4140
4141 // get args
4142 t_CKINT attributes = GET_NEXT_INT(ARGS);
4143 t_CKINT origins = GET_NEXT_INT(ARGS);
4144
4145 // get args
4146 t_CKBOOL objs = (attributes & TYPE_ATTRIB_OBJECT) != 0;
4147 t_CKBOOL prim = (attributes & TYPE_ATTRIB_PRIMITIVE) != 0;
4148 t_CKBOOL special = (attributes & TYPE_ATTRIB_SPECIAL) != 0;
4149 t_CKBOOL builtin = (origins & TYPE_ORIGIN_BUILTIN) != 0;
4150 t_CKBOOL chugins = (origins & TYPE_ORIGIN_CHUGIN) != 0;
4151 t_CKBOOL imports = (origins & TYPE_ORIGIN_CKLIB) != 0;
4152 t_CKBOOL user = (origins & TYPE_ORIGIN_USER) != 0;
4153
4154 // get types with the flags
4155 typeGetTypes( VM, array, objs, prim, special, builtin, chugins, imports, user );
4156 // return
4157 RETURN->v_object = array;
4158 }
4159
4160 CK_DLL_SFUN( type_getTypes2 )
4161 {
4162 // instantiate
4163 Chuck_ArrayInt * array = new Chuck_ArrayInt( TRUE );
4164 initialize_object( array, VM->env()->ckt_array, SHRED, VM );
4165
4166 // get args
4167 t_CKBOOL objs = GET_NEXT_INT(ARGS);
4168 t_CKBOOL prim = GET_NEXT_INT(ARGS);
4169 t_CKBOOL special = GET_NEXT_INT(ARGS);
4170 t_CKBOOL builtin = GET_NEXT_INT(ARGS);
4171 t_CKBOOL chugins = GET_NEXT_INT(ARGS);
4172 t_CKBOOL imports = GET_NEXT_INT(ARGS);
4173 t_CKBOOL user = GET_NEXT_INT(ARGS);
4174
4175 // get types with the flags
4176 typeGetTypes( VM, array, objs, prim, special, builtin, chugins, imports, user );
4177 // return
4178 RETURN->v_object = array;
4179 }
4180
4181 CK_DLL_SFUN( type_getTypesAll )
4182 {
4183 // instantiate
4184 Chuck_ArrayInt * array = new Chuck_ArrayInt( TRUE );
4185 initialize_object( array, VM->env()->ckt_array, SHRED, VM );
4186
4187 // get all types
4188 typeGetTypes( VM, array, true, true, true, true, true, true, true );
4189 // return
4190 RETURN->v_object = array;
4191 }