Debian Codegraph @

src/core/chuck_dl.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_dl.cpp
28 // desc: chuck dynamic linking header
29 //
30 // authors: Ge Wang ([email protected] | [email protected])
31 // Ari Lazier ([email protected])
32 // Spencer Salazar ([email protected])
33 // mac os code based on apple's open source
34 //
35 // date: spring 2004 - 1.1
36 // spring 2005 - 1.2
37 //-----------------------------------------------------------------------------
38 #include "chuck_dl.h"
39 #include "chuck_errmsg.h"
40 #include "chuck_type.h"
41 #include "chuck_instr.h"
42 #include "chuck_compile.h"
43 #include "chuck_vm.h"
44 #include "chuck.h"
45 #include <sstream>
46 using namespace std;
47
48
49
50
51 //-----------------------------------------------------------------------------
52 // three types of import search paths
53 //-----------------------------------------------------------------------------
54 // * system paths: for loading chugins/ck files on VM startup
55 // contents persists across clearVM
56 // * user paths: user .ck and .chug files; not automatically loaded on startup;
57 // @import on demand to use; contents removed by clearVM
58 // * packages path: packages installed and managed by ChuMP | 1.5.4.0 (ge & nshaheed)
59 // @import on demand to use; contents removed by clearVM
60 // (=v ChuMP =v ChucK Manager of Packages)
61 //-----------------------------------------------------------------------------
62 #if defined(__PLATFORM_APPLE__)
63 char g_default_path_system[] = "/usr/lib/chuck:/usr/local/lib/chuck:/Library/Application Support/ChucK/chugins";
64 char g_default_path_packages[] = "~/.chuck/packages";
65 char g_default_path_user[] = "~/Library/Application Support/ChucK/chugins:~/.chuck/lib";
66 #elif defined(__PLATFORM_WINDOWS__)
67 char g_default_path_system[] = "C:\\Windows\\system32\\ChucK;C:\\Program Files\\ChucK\\chugins;C:\\Program Files (x86)\\ChucK\\chugins";
68 char g_default_path_packages[] = "C:\\Users\\%USERNAME%\\Documents\\ChucK\\packages";
69 char g_default_path_user[] = "C:\\Users\\%USERNAME%\\Documents\\ChucK\\chugins";
70 #else // Linux / Cygwin
71 char g_default_path_system[] = "/usr/lib/chuck:/usr/local/lib/chuck";
72 char g_default_path_packages[] = "~/.chuck/packages";
73 char g_default_path_user[] = "~/.chuck/lib";
74 #endif
75
76 // environment variables
77 char g_envvar_path_system[] = "CHUCK_IMPORT_PATH_SYSTEM";
78 char g_envvar_path_packages[] = "CHUCK_IMPORT_PATH_PACKAGES";
79 char g_envvar_path_user[] = "CHUCK_IMPORT_PATH_USER";
80 // deprecated; should use the above
81 char g_envvar_path_deprecated[] = "CHUCK_CHUGIN_PATH";
82
83
84
85
86 //-----------------------------------------------------------------------------
87 // function prototypes
88 //-----------------------------------------------------------------------------
89
90 //-----------------------------------------------------------------------------
91 // Chuck_DL_Query implementation for chugin-side interface
92 //-----------------------------------------------------------------------------
93 Chuck_VM * CK_DLL_CALL ck_get_vm( Chuck_DL_Query * query );
94 CK_DL_API CK_DLL_CALL ck_get_api( Chuck_DL_Query * query );
95 Chuck_Env * CK_DLL_CALL ck_get_env( Chuck_DL_Query * query );
96 Chuck_Compiler * CK_DLL_CALL ck_get_compiler( Chuck_DL_Query * query );
97 Chuck_Carrier * CK_DLL_CALL ck_get_carrier( Chuck_DL_Query * query );
98 void CK_DLL_CALL ck_setname( Chuck_DL_Query * query, const char * name, t_CKBOOL attendSpecial );
99 void CK_DLL_CALL ck_setinfo( Chuck_DL_Query * query, const char * key, const char * value, t_CKBOOL attendSpecial );
100 const char * CK_DLL_CALL ck_getinfo( Chuck_DL_Query * query, const char * key );
101
102 void CK_DLL_CALL ck_add_arg( Chuck_DL_Query * query, const char * type, const char * name );
103 void CK_DLL_CALL ck_throw_exception( const char * exception, const char * desc, Chuck_VM_Shred * shred );
104 void CK_DLL_CALL ck_em_log( t_CKINT level, const char * text );
105 void CK_DLL_CALL ck_remove_all_shreds( Chuck_VM * vm );
106 Chuck_DL_Api::Type CK_DLL_CALL ck_type_lookup( Chuck_VM * vm, const char * name );
107 t_CKBOOL CK_DLL_CALL ck_type_isequal( Chuck_Type * lhs, Chuck_Type * rhs );
108 t_CKBOOL CK_DLL_CALL ck_type_isa( Chuck_Type * lhs, Chuck_Type * rhs );
109 void CK_DLL_CALL ck_callback_on_instantiate( f_callback_on_instantiate callback, Chuck_Type * base_type, Chuck_VM * vm, t_CKBOOL shouldSetShredOrigin );
110 ckte_Origin ck_origin_hint( Chuck_Type * type );
111 const char * CK_DLL_CALL ck_type_name( Chuck_Type * type );
112 const char * CK_DLL_CALL ck_type_base_name( Chuck_Type * type );
113 Chuck_VM_Shred * CK_DLL_CALL ck_get_origin_shred( Chuck_Object * object );
114 void CK_DLL_CALL ck_set_origin_shred( Chuck_Object * object, Chuck_VM_Shred * shred );
115 Chuck_VM_Shred * CK_DLL_CALL ck_shred_parent( Chuck_VM_Shred * shred );
116
117
118
119
120 //-----------------------------------------------------------------------------
121 // internal implementation of query functions
122 //-----------------------------------------------------------------------------
123 t_CKUINT CK_DLL_CALL ck_builtin_declversion() { return CK_DLL_VERSION; }
124
125
126
127
128 //-----------------------------------------------------------------------------
129 // name: ck_builtin_info()
130 // desc: builtin info func for internal modules
131 //-----------------------------------------------------------------------------
132 void CK_DLL_CALL ck_builtin_info( Chuck_DL_Query * query )
133 {
134 // set nothing here
135 }
136
137
138
139
140 //-----------------------------------------------------------------------------
141 // name: ck_setname()
142 // desc: set the name of the module/chugin
143 //-----------------------------------------------------------------------------
144 void CK_DLL_CALL ck_setname( Chuck_DL_Query * query, const char * name, t_CKBOOL attendSpecial )
145 {
146 // set the name
147 query->dll_name = name ? name : "[no name]";
148 // also set in info, but attendSpecial=FALSE to avoid infinite recursion
149 if( attendSpecial ) ck_setinfo( query, CHUGIN_INFO_NAME, name, FALSE );
150 }
151 // overload to get around function pointer business for f_setname
152 void CK_DLL_CALL ck_setname( Chuck_DL_Query * query, const char * name )
153 { ck_setname( query, name, TRUE ); }
154
155
156
157
158 //-----------------------------------------------------------------------------
159 // name: ck_setinfo() | 1.5.2.0
160 // desc: set info (by key) of the module/chugin
161 //-----------------------------------------------------------------------------
162 void CK_DLL_CALL ck_setinfo( Chuck_DL_Query * query, const char * key, const char * value, t_CKBOOL attendSpecial )
163 {
164 // check for null key
165 if( !key ) return;
166
167 // check for null value
168 if( !value )
169 {
170 // check for presence of entry
171 if( query->dll_info.find( key ) != query->dll_info.end() )
172 {
173 // erase the entry
174 query->dll_info.erase( key );
175 }
176 }
177 else
178 {
179 // add or update it
180 query->dll_info[key] = value;
181 }
182
183 // check for special keys
184 if( attendSpecial )
185 {
186 // also set name, but attendSpecial=FALSE to avoid infinite recursion
187 if( string(key) == CHUGIN_INFO_NAME ) ck_setname( query, value, FALSE );
188 }
189 }
190 // overload to get around function pointer business for f_setinfo
191 void CK_DLL_CALL ck_setinfo( Chuck_DL_Query * query, const char * key, const char * value )
192 { ck_setinfo( query, key, value, TRUE ); }
193
194
195
196
197 //-----------------------------------------------------------------------------
198 // name: ck_geinfo() | 1.5.2.0
199 // desc: get info (by key) of the module/chugin
200 //-----------------------------------------------------------------------------
201 const char * CK_DLL_CALL ck_getinfo( Chuck_DL_Query * query, const char * key )
202 {
203 // check for null key
204 if( !key ) return "";
205 // check for presence of entry
206 if( query->dll_info.find( key ) == query->dll_info.end() ) return "";
207 // return
208 return query->dll_info[key].c_str();
209 }
210
211
212
213
214 //-----------------------------------------------------------------------------
215 // name: ck_begin_class()
216 // desc: begin class/namespace, can be nexted
217 //-----------------------------------------------------------------------------
218 void CK_DLL_CALL ck_begin_class( Chuck_DL_Query * query, const char * name, const char * parent )
219 {
220 // find parent mvar offset
221 t_CKINT parent_offset = 0;
222 if(parent && parent[0] != '\0')
223 {
224 a_Id_List parent_path = str2list( parent );
225 if( !parent_path )
226 {
227 // error
228 EM_error2( 0, "class import: ck_begin_class: unknown parent type '%s'",
229 query->curr_class->name.c_str(), name, parent );
230 return;
231 }
232
233 // find parent type
234 Chuck_Type * ck_parent_type = type_engine_find_type( query->env(), parent_path );
235 // clean up locally created id list
236 delete_id_list( parent_path );
237 // not found
238 if( !ck_parent_type )
239 {
240 // error
241 EM_error2( 0, "class import: ck_begin_class: unable to find parent type '%s'", ck_parent_type );
242 return;
243 }
244
245 parent_offset = ck_parent_type->obj_size;
246 }
247
248 // push
249 query->stack.push_back( query->curr_class );
250 // allocate
251 Chuck_DL_Class * c = new Chuck_DL_Class;
252
253 // add class
254 if( query->curr_class )
255 {
256 // recursive
257 query->curr_class->classes.push_back( c );
258 }
259 // 1.3.2.0: do not save class for later import (will import it on class close)
260 // else
261 // // first level
262 // query->classes.push_back( c );
263
264 // remember info
265 c->name = name ? name : "";
266 c->parent = parent ? parent : "";
267 c->current_mvar_offset = parent_offset;
268
269 // if more info is available | 1.5.1.3 (ge)
270 if( query->dll_ref && string(query->dll_ref->filepath()) != "" ) {
271 // set chugin file path for error reporting
272 c->hint_dll_filepath = query->dll_ref->filepath();
273 } else {
274 // set DLL name for error reporting
275 c->hint_dll_filepath = query->dll_name;
276 }
277
278 // curr
279 query->curr_class = c;
280 query->curr_func = NULL;
281 query->curr_var = NULL; // 1.5.2.0
282 }
283
284
285
286
287 //-----------------------------------------------------------------------------
288 // name: ck_add_ctor()
289 // desc: add constructor, can be followed by add_arg
290 //-----------------------------------------------------------------------------
291 void CK_DLL_CALL ck_add_ctor( Chuck_DL_Query * query, f_ctor ctor )
292 {
293 // make sure there is class
294 if( !query->curr_class )
295 {
296 // error
297 EM_error2( 0, "class import: add_ctor invoked without begin_class..." );
298 return;
299 }
300
301 // allocate
302 Chuck_DL_Func * f = new Chuck_DL_Func;
303 f->name = "@construct";
304 f->type = "void";
305 f->ctor = ctor;
306 f->fpKind = ae_fp_ctor; // 1.5.2.0
307
308 // add
309 query->curr_class->ctors.push_back( f );
310 query->curr_func = f;
311 query->curr_var = NULL; // 1.5.2.0
312 }
313
314
315
316
317 //-----------------------------------------------------------------------------
318 // name: ck_add_dtor()
319 // add destructor, no args allowed
320 //-----------------------------------------------------------------------------
321 void CK_DLL_CALL ck_add_dtor( Chuck_DL_Query * query, f_dtor dtor )
322 {
323 // make sure there is class
324 if( !query->curr_class )
325 {
326 // error
327 EM_error2( 0, "class import: add_dtor invoked without begin_class..." );
328 return;
329 }
330
331 // make sure there are no duplicates
332 if( query->curr_class->dtor )
333 {
334 // error
335 EM_error2( 0, "class import: multiple dtor added ..." );
336 return;
337 }
338
339 // allocate
340 Chuck_DL_Func * f = new Chuck_DL_Func;
341 f->name = "@destruct";
342 f->type = "void";
343 f->dtor = dtor;
344 f->fpKind = ae_fp_dtor; // 1.5.2.0
345
346 // add
347 query->curr_class->dtor = f;
348 query->curr_func = NULL;
349 query->curr_var = NULL; // 1.5.2.0
350 }
351
352
353
354
355 //-----------------------------------------------------------------------------
356 // name: ck_add_mfun()
357 // desc: add member function, can be followed by add_arg
358 //-----------------------------------------------------------------------------
359 void CK_DLL_CALL ck_add_mfun( Chuck_DL_Query * query, f_mfun addr,
360 const char * type, const char * name )
361 {
362 // make sure there is class
363 if( !query->curr_class )
364 {
365 // error
366 EM_error2( 0, "class import: add_mfun invoked without begin_class..." );
367 return;
368 }
369
370 // allocate
371 Chuck_DL_Func * f = new Chuck_DL_Func;
372 f->name = name;
373 f->type = type;
374 f->mfun = addr;
375 f->fpKind = ae_fp_mfun; // 1.5.2.0
376
377 // add
378 query->curr_class->mfuns.push_back( f );
379 query->curr_func = f;
380 query->curr_var = NULL; // 1.5.2.0
381 }
382
383
384
385
386 //-----------------------------------------------------------------------------
387 // name: ck_add_sfun()
388 // desc: add static function, can be followed by add_arg
389 //-----------------------------------------------------------------------------
390 void CK_DLL_CALL ck_add_sfun( Chuck_DL_Query * query, f_sfun addr,
391 const char * type, const char * name )
392 {
393 // make sure there is class
394 if( !query->curr_class )
395 {
396 // error
397 EM_error2( 0, "class import: add_sfun invoked without begin_class..." );
398 return;
399 }
400
401 // allocate
402 Chuck_DL_Func * f = new Chuck_DL_Func;
403 f->name = name;
404 f->type = type;
405 f->sfun = addr;
406 f->fpKind = ae_fp_sfun; // 1.5.2.0
407
408 // add
409 query->curr_class->sfuns.push_back( f );
410 query->curr_func = f;
411 query->curr_var = NULL; // 1.5.2.0
412 }
413
414
415
416
417 //-----------------------------------------------------------------------------
418 // name: ck_add_op_overload_binary()
419 // desc: add binary operator overload; args included
420 //-----------------------------------------------------------------------------
421 void CK_DLL_CALL ck_add_op_overload_binary( Chuck_DL_Query * query, f_gfun addr,
422 const char * type, const char * opName,
423 const char * lhsType, const char * lhsName,
424 const char * rhsType, const char * rhsName )
425 {
426 // look up operator
427 ae_Operator op = str2op(opName);
428 if( op == ae_op_none )
429 {
430 // error
431 EM_error2( 0, "class import: add_op_overload_binary invoked with unsupported operator '%s'...", opName );
432 return;
433 }
434
435 // allocate
436 Chuck_DL_Func * f = new Chuck_DL_Func;
437 f->name = string("@operator") + opName;
438 f->type = type;
439 f->gfun = addr;
440 f->fpKind = ae_fp_gfun; // 1.5.2.0
441 f->opOverloadKind = ckte_op_overload_BINARY;
442 f->op2overload = op;
443
444 // add
445 query->op_overloads.push_back( f );
446 query->curr_func = NULL;
447
448 // add arg LHS
449 Chuck_DL_Value * v = new Chuck_DL_Value;
450 v->type = lhsType; v->name = lhsName;
451 f->args.push_back( v );
452
453 // add arg RHS
454 v = new Chuck_DL_Value;
455 v->type = rhsType; v->name = rhsName;
456 f->args.push_back( v );
457 }
458
459
460
461
462 //-----------------------------------------------------------------------------
463 // name: ck_add_op_overload_prefix()
464 // desc: add unary (prefix) operator overload; args included
465 //-----------------------------------------------------------------------------
466 void CK_DLL_CALL ck_add_op_overload_prefix( Chuck_DL_Query * query, f_gfun addr,
467 const char * type, const char * opName,
468 const char * argType, const char * argName )
469 {
470 // look up operator
471 ae_Operator op = str2op(opName);
472 if( op == ae_op_none )
473 {
474 // error
475 EM_error2( 0, "class import: add_op_overload_prefix invoked with unsupported operator '%s'...", opName );
476 return;
477 }
478
479 // allocate
480 Chuck_DL_Func * f = new Chuck_DL_Func;
481 f->name = string("@operator") + opName;
482 f->type = type;
483 f->gfun = addr;
484 f->fpKind = ae_fp_gfun; // 1.5.2.0
485 f->opOverloadKind = ckte_op_overload_UNARY_PRE;
486 f->op2overload = op;
487
488 // add
489 query->op_overloads.push_back( f );
490 query->curr_func = NULL;
491
492 // add arg
493 Chuck_DL_Value * v = new Chuck_DL_Value;
494 v->type = argType; v->name = argName;
495 f->args.push_back( v );
496 }
497
498
499
500
501 //-----------------------------------------------------------------------------
502 // name: ck_add_op_overload_postfix()
503 // desc: add unary (postfix) operator overload; args included
504 //-----------------------------------------------------------------------------
505 void CK_DLL_CALL ck_add_op_overload_postfix( Chuck_DL_Query * query, f_gfun addr,
506 const char * type, const char * opName,
507 const char * argType, const char * argName )
508 {
509 // look up operator
510 ae_Operator op = str2op(opName);
511 if( op == ae_op_none )
512 {
513 // error
514 EM_error2( 0, "class import: add_op_overload_postfix invoked with unsupported operator '%s'...", opName );
515 return;
516 }
517
518 // allocate
519 Chuck_DL_Func * f = new Chuck_DL_Func;
520 f->name = string("@operator") + opName;
521 f->type = type;
522 f->gfun = addr;
523 f->fpKind = ae_fp_gfun; // 1.5.2.0
524 f->opOverloadKind = ckte_op_overload_UNARY_POST;
525 f->op2overload = op;
526
527 // add
528 query->op_overloads.push_back( f );
529 query->curr_func = NULL;
530
531 // add arg
532 Chuck_DL_Value * v = new Chuck_DL_Value;
533 v->type = argType; v->name = argName;
534 f->args.push_back( v );
535 }
536
537
538
539
540 //-----------------------------------------------------------------------------
541 // name: ck_add_mvar()
542 // desc: add member var
543 //-----------------------------------------------------------------------------
544 t_CKUINT CK_DLL_CALL ck_add_mvar( Chuck_DL_Query * query,
545 const char * type, const char * name,
546 t_CKBOOL is_const )
547 {
548 // make sure there is class
549 if( !query->curr_class )
550 {
551 // error
552 EM_error2( 0, "class import: add_mvar invoked without begin_class..." );
553 return CK_INVALID_OFFSET;
554 }
555
556 a_Id_List path = str2list( type );
557 if( !path )
558 {
559 // error
560 EM_error2( 0, "class import: add_mvar: mvar %s.%s has unknown type '%s'",
561 query->curr_class->name.c_str(), name, type );
562 return CK_INVALID_OFFSET;
563 }
564
565 // find type
566 Chuck_Type * ck_type = type_engine_find_type( query->env(), path );
567 // clean up locally created id list
568 delete_id_list( path );
569 // not found
570 if( !ck_type )
571 {
572 // error
573 EM_error2( 0, "class import: add_mvar: unable to find type '%s'", type );
574 return CK_INVALID_OFFSET;
575 }
576
577 // allocate
578 Chuck_DL_Value * v = new Chuck_DL_Value;
579 v->name = name;
580 v->type = type;
581 v->is_const = is_const;
582
583 // add
584 query->curr_class->mvars.push_back( v );
585 query->curr_func = NULL;
586 query->curr_var = v;
587
588 t_CKUINT offset = query->curr_class->current_mvar_offset;
589 query->curr_class->current_mvar_offset = type_engine_next_offset( query->curr_class->current_mvar_offset,
590 ck_type );
591
592 return offset;
593 }
594
595
596
597
598 //-----------------------------------------------------------------------------
599 // name: ck_add_svar()
600 // desc: add static var
601 //-----------------------------------------------------------------------------
602 void CK_DLL_CALL ck_add_svar( Chuck_DL_Query * query, const char * type, const char * name,
603 t_CKBOOL is_const, void * addr )
604 {
605 // make sure there is class
606 if( !query->curr_class )
607 {
608 // error
609 EM_error2( 0, "class import: add_svar invoked without begin_class..." );
610 return;
611 }
612
613 // allocate
614 Chuck_DL_Value * v = new Chuck_DL_Value;
615 v->name = name;
616 v->type = type;
617 v->is_const = is_const;
618 v->static_addr = addr;
619
620 // add
621 query->curr_class->svars.push_back( v );
622 query->curr_func = NULL;
623 query->curr_var = v;
624 }
625
626
627
628
629 //-----------------------------------------------------------------------------
630 // name: ck_add_arg()
631 // desc: add argument to function
632 //-----------------------------------------------------------------------------
633 void CK_DLL_CALL ck_add_arg( Chuck_DL_Query * query, const char * type, const char * name )
634 {
635 // make sure there is class
636 if( !query->curr_class )
637 {
638 // error
639 EM_error2( 0, "class import: add_arg invoked without begin_class..." );
640 return;
641 }
642
643 // make sure there is function
644 if( !query->curr_func )
645 {
646 // error
647 EM_error2( 0, "class import: add_arg can only follow 'ctor', 'mfun', 'sfun', 'arg'..." );
648 return;
649 }
650
651 // allocate
652 Chuck_DL_Value * v = new Chuck_DL_Value;
653 v->name = name;
654 v->type = type;
655
656 // add
657 query->curr_func->args.push_back( v );
658 }
659
660
661
662
663 //-----------------------------------------------------------------------------
664 // name: ck_add_ugen_func()
665 // desc: (ugen only) add tick and pmsg functions
666 //-----------------------------------------------------------------------------
667 void CK_DLL_CALL ck_add_ugen_func( Chuck_DL_Query * query, f_tick ugen_tick, f_pmsg ugen_pmsg, t_CKUINT num_in, t_CKUINT num_out )
668 {
669 // make sure there is class
670 if( !query->curr_class )
671 {
672 // error
673 EM_error2( 0, "class import: add_ugen_func invoked without begin_class..." );
674 return;
675 }
676
677 // make sure tick not defined already
678 if( query->curr_class->ugen_tick && ugen_tick )
679 {
680 // error
681 EM_error2( 0, "class import: ugen_tick already defined..." );
682 return;
683 }
684
685 // make sure pmsg not defined already
686 if( query->curr_class->ugen_pmsg && ugen_pmsg )
687 {
688 // error
689 EM_error2( 0, "class import: ugen_pmsg already defined..." );
690 return;
691 }
692
693 // set
694 if( ugen_tick ) query->curr_class->ugen_tick = ugen_tick;
695 if( ugen_pmsg ) query->curr_class->ugen_pmsg = ugen_pmsg;
696 query->curr_class->ugen_num_in = num_in;
697 query->curr_class->ugen_num_out = num_out;
698 query->curr_func = NULL;
699 }
700
701
702
703
704 //-----------------------------------------------------------------------------
705 // name: ck_add_ugen_funcf()
706 // desc: (ugen only) add tick and pmsg functions
707 //-----------------------------------------------------------------------------
708 void CK_DLL_CALL ck_add_ugen_funcf( Chuck_DL_Query * query, f_tickf ugen_tickf, f_pmsg ugen_pmsg, t_CKUINT num_in, t_CKUINT num_out )
709 {
710 // make sure there is class
711 if( !query->curr_class )
712 {
713 // error
714 EM_error2( 0, "class import: add_ugen_func invoked without begin_class..." );
715 return;
716 }
717
718 // make sure tick not defined already
719 if( query->curr_class->ugen_tickf && ugen_tickf )
720 {
721 // error
722 EM_error2( 0, "class import: ugen_tick already defined..." );
723 return;
724 }
725
726 // make sure pmsg not defined already
727 if( query->curr_class->ugen_pmsg && ugen_pmsg )
728 {
729 // error
730 EM_error2( 0, "class import: ugen_pmsg already defined..." );
731 return;
732 }
733
734 // set
735 if( ugen_tickf ) query->curr_class->ugen_tickf = ugen_tickf;
736 if( ugen_pmsg ) query->curr_class->ugen_pmsg = ugen_pmsg;
737 query->curr_class->ugen_num_in = num_in;
738 query->curr_class->ugen_num_out = num_out;
739 query->curr_func = NULL;
740 }
741
742
743
744
745 //-----------------------------------------------------------------------------
746 // name: ck_add_ugen_funcf_auto_num_channels()
747 // desc: (ugen only) add tick and pmsg functions. specify num channels by vm.
748 //-----------------------------------------------------------------------------
749 void CK_DLL_CALL ck_add_ugen_funcf_auto_num_channels( Chuck_DL_Query * query,
750 f_tickf ugen_tickf, f_pmsg ugen_pmsg )
751 {
752 ck_add_ugen_funcf( query, ugen_tickf, ugen_pmsg,
753 query->vm()->m_num_adc_channels,
754 query->vm()->m_num_dac_channels
755 );
756 }
757
758
759
760
761 //-----------------------------------------------------------------------------
762 // name: ck_add_ugen_ctrl()
763 // desc: (ugen only) add ctrl parameters
764 //-----------------------------------------------------------------------------
765 //void CK_DLL_CALL ck_add_ugen_ctrl( Chuck_DL_Query * query, f_ctrl ugen_ctrl, f_cget ugen_cget,
766 // const char * type, const char * name )
767 //{
768 // // this is no longer used
769 // EM_error2( 0, "class import: obselete ck_add_ugen_ctrl invoked..." );
770 // return;
771 //
772 // // make sure there is class
773 // if( !query->curr_class )
774 // {
775 // // error
776 // EM_error2( 0, "class import: add_ugen_func invoked without begin_class..." );
777 // return;
778 // }
779 //
780 // // allocate
781 // Chuck_DL_Ctrl * c = new Chuck_DL_Ctrl;
782 // c->name = name;
783 // c->type = type;
784 // c->ctrl = ugen_ctrl;
785 // c->cget = ugen_cget;
786 //
787 // // set
788 // query->curr_func = NULL;
789 //}
790
791
792
793
794 //-----------------------------------------------------------------------------
795 // name: ck_end_class()
796 // desc: end class/namespace, compile it
797 //-----------------------------------------------------------------------------
798 t_CKBOOL CK_DLL_CALL ck_end_class( Chuck_DL_Query * query )
799 {
800 // make sure there is class
801 if( !query->curr_class )
802 {
803 // error
804 EM_error2( 0, "class import: end_class invoked without begin_class..." );
805 return FALSE;
806 }
807
808 // type check the class?
809 // 1.3.2.0: import class into type engine if at top level
810 if( query->stack.size() == 1 ) // top level class
811 {
812 if( !type_engine_add_class_from_dl( query->env(), query->curr_class ) )
813 {
814 // should already be message
815 //EM_log(CK_LOG_HERALD, "error importing class '%s' into type engine",
816 // query->curr_class->name.c_str());
817
818 // pop
819 assert( query->stack.size() );
820 query->curr_class = query->stack.back();
821 query->stack.pop_back();
822
823 // flag the query with error
824 query->errorEncountered = TRUE;
825
826 return FALSE;
827 }
828 }
829
830 // pop
831 assert( query->stack.size() );
832 query->curr_class = query->stack.back();
833 query->stack.pop_back();
834
835 return TRUE;
836 }
837
838
839 //-----------------------------------------------------------------------------
840 // name: ck_create_main_thread_hook()
841 // desc: create a main thread hook data structure
842 //-----------------------------------------------------------------------------
843 Chuck_DL_MainThreadHook * CK_DLL_CALL ck_create_main_thread_hook( Chuck_DL_Query * query,
844 f_mainthreadhook hook,
845 f_mainthreadquit quit,
846 void * bindle )
847 {
848 return new Chuck_DL_MainThreadHook( hook, quit, bindle, query->carrier() );
849 }
850
851 //-----------------------------------------------------------------------------
852 // name: ck_register_callback_on_shutdown() | 1.5.2.5 (ge) added
853 // desc: register a callback function to be called on host exit
854 // (both natural or SIGINT)
855 //-----------------------------------------------------------------------------
856 void CK_DLL_CALL ck_register_callback_on_shutdown( Chuck_DL_Query * query, f_callback_on_shutdown cb, void * bindle )
857 {
858 // register
859 query->vm()->register_callback_on_shutdown( cb, bindle );
860 }
861
862 //-----------------------------------------------------------------------------
863 // name: ck_register_callback_on_srate_update() | 1.5.4.2 (ge) added
864 // desc: register a callback function to be called on sample rate change
865 //-----------------------------------------------------------------------------
866 void CK_DLL_CALL ck_register_callback_on_srate_update( Chuck_DL_Query * query, f_callback_on_srate_update cb, void * bindle )
867 {
868 // register
869 query->vm()->register_callback_on_srate_update( cb, bindle );
870 }
871
872 //-----------------------------------------------------------------------------
873 // name: ck_register_shreds_watcher()
874 // desc: register a callback function to receive notifications
875 // from the VM about shreds (add, remove, etc.)
876 // `options` is a bit-wised OR of ckvmShredsWatcherFlag
877 //-----------------------------------------------------------------------------
878 void CK_DLL_CALL ck_register_shreds_watcher( Chuck_DL_Query * query, f_shreds_watcher cb, t_CKUINT options, void * bindle )
879 {
880 // subscribe
881 query->vm()->subscribe_watcher( cb, options, bindle );
882 }
883
884 //-----------------------------------------------------------------------------
885 // unregister a shreds notification callback
886 //-----------------------------------------------------------------------------
887 void CK_DLL_CALL ck_unregister_shreds_watcher( Chuck_DL_Query * query, f_shreds_watcher cb )
888 {
889 query->vm()->remove_watcher( cb );
890 }
891
892
893 //-----------------------------------------------------------------------------
894 // name: ck_doc_class()
895 // desc: set current class documentation
896 //-----------------------------------------------------------------------------
897 t_CKBOOL CK_DLL_CALL ck_doc_class( Chuck_DL_Query * query, const char * doc )
898 {
899 // if no current class
900 if( !query->curr_class ) return FALSE;
901 // set it
902 query->curr_class->doc = doc;
903 // done
904 return TRUE;
905 }
906
907
908 //-----------------------------------------------------------------------------
909 // name: ck_add_example()
910 // desc: set current class documentation
911 //-----------------------------------------------------------------------------
912 t_CKBOOL CK_DLL_CALL ck_add_example( Chuck_DL_Query * query, const char * ex )
913 {
914 // if no current class
915 if( !query->curr_class ) return FALSE;
916 // append it
917 query->curr_class->examples.push_back(ex);
918 // done
919 return TRUE;
920 }
921
922
923 // set current function documentation
924 t_CKBOOL CK_DLL_CALL ck_doc_func( Chuck_DL_Query * query, const char * doc )
925 {
926 // if no current function
927 if( !query->curr_func ) return FALSE;
928 // set it
929 query->curr_func->doc = doc;
930 // done
931 return TRUE;
932 }
933
934
935 // set last mvar documentation
936 t_CKBOOL CK_DLL_CALL ck_doc_var( Chuck_DL_Query * query, const char * doc )
937 {
938 // if no current var
939 if( !query->curr_var ) return FALSE;
940 // set it
941 query->curr_var->doc = doc;
942 // done
943 return TRUE;
944 }
945
946
947
948
949 //------------------------------------------------------------------------------
950 // alternative functions to make stuff
951 //------------------------------------------------------------------------------
952 Chuck_DL_Func * CK_DLL_CALL make_new_ctor( f_ctor ctor )
953 { return new Chuck_DL_Func( "void", "@construct", (t_CKUINT)ctor, ae_fp_ctor ); }
954 Chuck_DL_Func * CK_DLL_CALL make_new_mfun( const char * t, const char * n, f_mfun mfun )
955 { return new Chuck_DL_Func( t, n, (t_CKUINT)mfun, ae_fp_mfun ); }
956 Chuck_DL_Func * CK_DLL_CALL make_new_sfun( const char * t, const char * n, f_sfun sfun )
957 { return new Chuck_DL_Func( t, n, (t_CKUINT)sfun, ae_fp_sfun ); }
958 Chuck_DL_Value * CK_DLL_CALL make_new_arg( const char * t, const char * n )
959 { return new Chuck_DL_Value( t, n ); }
960 Chuck_DL_Value * CK_DLL_CALL make_new_mvar( const char * t, const char * n, t_CKBOOL c )
961 { return new Chuck_DL_Value( t, n, c ); }
962 Chuck_DL_Value * CK_DLL_CALL make_new_svar( const char * t, const char * n, t_CKBOOL c, void * a )
963 { return new Chuck_DL_Value( t, n, c, a ); }
964 Chuck_DL_Func * make_new_op_binary( const char * t, ae_Operator op, f_gfun gfun )
965 {
966 // allocate
967 Chuck_DL_Func * f = new Chuck_DL_Func( t, (string("@operator")+op2str(op)).c_str(), (t_CKUINT)gfun, ae_fp_gfun );
968 // set operator overload kind
969 f->opOverloadKind = ckte_op_overload_BINARY;
970 // set the op
971 f->op2overload = op;
972 // return
973 return f;
974 }
975 Chuck_DL_Func * make_new_op_prefix( const char * t, ae_Operator op, f_gfun gfun )
976 {
977 // allocate
978 Chuck_DL_Func * f = new Chuck_DL_Func( t, (string("@operator")+op2str(op)).c_str(), (t_CKUINT)gfun, ae_fp_gfun );
979 // set operator overload kind
980 f->opOverloadKind = ckte_op_overload_UNARY_PRE;
981 // set the op
982 f->op2overload = op;
983 // return
984 return f;
985 }
986 Chuck_DL_Func * make_new_op_postfix( const char * t, ae_Operator op, f_gfun gfun )
987 {
988 // allocate
989 Chuck_DL_Func * f = new Chuck_DL_Func( t, (string("@operator")+op2str(op)).c_str(), (t_CKUINT)gfun, ae_fp_gfun );
990 // set operator overload kind
991 f->opOverloadKind = ckte_op_overload_UNARY_POST;
992 // set the op
993 f->op2overload = op;
994 // return
995 return f;
996 }
997
998
999
1000
1001 //-----------------------------------------------------------------------------
1002 // Chuck_DL_Query implementation for chugin-side interface
1003 //-----------------------------------------------------------------------------
1004 Chuck_VM * CK_DLL_CALL ck_get_vm( Chuck_DL_Query * query ) { return query->vm(); }
1005 CK_DL_API CK_DLL_CALL ck_get_api( Chuck_DL_Query * query ) { return query->api(); }
1006 Chuck_Env * CK_DLL_CALL ck_get_env( Chuck_DL_Query * query ) { return query->env(); }
1007 Chuck_Compiler * CK_DLL_CALL ck_get_compiler( Chuck_DL_Query * query ) { return query->compiler(); }
1008 Chuck_Carrier * CK_DLL_CALL ck_get_carrier( Chuck_DL_Query * query ) { return query->carrier(); }
1009
1010
1011
1012 //-----------------------------------------------------------------------------
1013 // Chuck_DL_Query host-side functions
1014 //-----------------------------------------------------------------------------
1015 Chuck_VM * Chuck_DL_Query::vm() const { return m_carrier->vm; }
1016 CK_DL_API Chuck_DL_Query::api() const { return m_api; } // 1.5.1.5
1017 Chuck_Env * Chuck_DL_Query::env() const { return m_carrier->env; }
1018 Chuck_Compiler * Chuck_DL_Query::compiler() const { return m_carrier->compiler; }
1019 Chuck_Carrier * Chuck_DL_Query::carrier() const { return m_carrier; }
1020
1021
1022
1023
1024 //-----------------------------------------------------------------------------
1025 // name: load()
1026 // desc: load module (chugin/dll) from filename
1027 //-----------------------------------------------------------------------------
1028 t_CKBOOL Chuck_DLL::load( const char * filename, const char * func, t_CKBOOL lazy )
1029 {
1030 // return value
1031 t_CKBOOL ret = TRUE;
1032
1033 // open
1034 m_handle = dlopen( filename, lazy ? RTLD_LAZY : RTLD_NOW );
1035
1036 // still not there
1037 if( !m_handle )
1038 {
1039 m_last_error = dlerror();
1040 ret = FALSE;
1041 goto cleanup;
1042 }
1043
1044 // save the filename
1045 m_filename = normalize_directory_separator(filename);
1046 m_done_query = FALSE;
1047 m_func = func;
1048
1049 // set the enclosing directory (without the filename)
1050 ck_setinfo( &m_query, CHUGIN_INFO_INSTALL_DIR, extract_filepath_dir( m_filename ).c_str() );
1051 // set the filename (without the dir)
1052 ck_setinfo( &m_query, CHUGIN_INFO_INSTALL_FILE, extract_filepath_file( m_filename ).c_str() );
1053
1054 // if not lazy, do it
1055 if( !lazy && !this->query() )
1056 {
1057 // clean up the handle | 1.5.4.1 (ge) added here
1058 dlclose( m_handle );
1059 m_handle = NULL;
1060
1061 // FYI last error should be set from within query()
1062 ret = FALSE;
1063 goto cleanup;
1064 }
1065
1066 cleanup:
1067
1068 // return
1069 return ret;
1070 }
1071
1072
1073
1074
1075 //-----------------------------------------------------------------------------
1076 // name: load()
1077 // desc: load module (internal) from query func
1078 //-----------------------------------------------------------------------------
1079 t_CKBOOL Chuck_DLL::load( f_ck_query query_func, t_CKBOOL lazy )
1080 {
1081 m_query_func = query_func;
1082 m_api_version_func = ck_builtin_declversion;
1083 m_info_func = ck_builtin_info;
1084 m_done_query = FALSE;
1085 m_func = "ck_query";
1086
1087 // if not lazy, do it
1088 if( !lazy && !this->query() )
1089 return FALSE;
1090
1091 return TRUE;
1092 }
1093
1094
1095
1096
1097 //-----------------------------------------------------------------------------
1098 // name: good()
1099 // desc: ...
1100 //-----------------------------------------------------------------------------
1101 t_CKBOOL Chuck_DLL::good() const
1102 {
1103 return ( m_handle != NULL || m_query_func != NULL );
1104 }
1105
1106
1107
1108
1109 //-----------------------------------------------------------------------------
1110 // name: query()
1111 // desc: ...
1112 //-----------------------------------------------------------------------------
1113 const Chuck_DL_Query * Chuck_DLL::query()
1114 {
1115 // return if there already
1116 if( m_done_query )
1117 return &m_query;
1118
1119 // probe DLL, for version and info
1120 if( !probe() )
1121 return NULL;
1122
1123 // is version ok
1124 t_CKBOOL version_ok = FALSE;
1125 // chugin major version must == host major version
1126 // chugin minor version must <= host minor version
1127 if( m_apiVersionMajor == CK_DLL_VERSION_MAJOR &&
1128 m_apiVersionMinor <= CK_DLL_VERSION_MINOR)
1129 version_ok = TRUE;
1130
1131 // if version not okay
1132 if( !version_ok )
1133 {
1134 // construct error string
1135 ostringstream oss;
1136 oss << "chugin API version incompatible..." << endl
1137 << "chugin path: '" << m_filename << "'" << endl
1138 << "chugin API version: " << m_apiVersionMajor << "." << m_apiVersionMinor
1139 << " VS host API version: " << CK_DLL_VERSION_MAJOR << "." << CK_DLL_VERSION_MINOR;
1140 m_last_error = oss.str();
1141 return NULL;
1142 }
1143
1144 // get the address of the query function from the DLL
1145 if( !m_query_func )
1146 m_query_func = (f_ck_query)this->get_addr( m_func.c_str() );
1147 if( !m_query_func )
1148 m_query_func = (f_ck_query)this->get_addr( (string("_")+m_func).c_str() );
1149 if( !m_query_func )
1150 {
1151 m_last_error = string("no query function found in dll '") + m_filename + string("'");
1152 return NULL;
1153 }
1154
1155 // get the address of the attach function from the DLL
1156 /* if( !m_attach_func )
1157 m_attach_func = (f_ck_attach)this->get_addr( "ck_attach" );
1158 if( !m_attach_func )
1159 m_attach_func = (f_ck_attach)this->get_addr( "_ck_attach" );
1160
1161 // get the address of the detach function from the DLL
1162 if( !m_detach_func )
1163 m_detach_func = (f_ck_detach)this->get_addr( "ck_detach" );
1164 if( !m_detach_func )
1165 m_detach_func = (f_ck_detach)this->get_addr( "_ck_detach" ); */
1166
1167 // do the query
1168 m_query.clear();
1169 if( !m_query_func( &m_query ) || m_query.errorEncountered )
1170 {
1171 m_last_error = string("unsuccessful query in dll/module '") + (m_filename.length() ? m_filename : m_id)
1172 + string("'");
1173 return NULL;
1174 }
1175
1176 // set flag
1177 m_done_query = TRUE;
1178
1179 // process any operator overloads | 1.5.1.5 (ge & andrew) chaos^2
1180 if( m_query.op_overloads.size() )
1181 {
1182 // log
1183 EM_log( CK_LOG_FINER, "processing operator overload in chugin '%s'", m_filename.c_str() );
1184 // push log
1185 EM_pushlog();
1186 // iterate over overloads
1187 for( t_CKUINT i = 0; i < m_query.op_overloads.size(); i++ )
1188 {
1189 // get the dl func def
1190 Chuck_DL_Func * f = m_query.op_overloads[i];
1191 // log
1192 EM_log( CK_LOG_FINER, "processing '%s'", f->name.c_str() );
1193 // try to import
1194 if( !type_engine_import_op_overload( m_query.env(), f ) )
1195 {
1196 m_last_error = string("unsuccessful operator overload '") + f->name +
1197 string("' in dll '") + m_filename + string("'");
1198 return NULL;
1199 }
1200 // delete entry (and zero out the array element)
1201 CK_SAFE_DELETE( m_query.op_overloads[i] );
1202 }
1203 // clear
1204 m_query.op_overloads.clear();
1205 // pop log
1206 EM_poplog();
1207 }
1208
1209 // call attach
1210 // if( m_attach_func ) m_attach_func( 0, NULL );
1211
1212 return &m_query;
1213 }
1214
1215
1216
1217
1218 //-----------------------------------------------------------------------------
1219 // name: probe()
1220 // desc: probe the dll without fully loading its content
1221 //-----------------------------------------------------------------------------
1222 t_CKBOOL Chuck_DLL::probe()
1223 {
1224 if( !m_handle && !m_query_func )
1225 {
1226 m_last_error = "dynamic link library not loaded for query...";
1227 return FALSE;
1228 }
1229
1230 // get the address of the DL version function from the DLL
1231 if( !m_api_version_func )
1232 m_api_version_func = (f_ck_declversion)this->get_addr( CK_DECLVERSION_FUNC );
1233 if( !m_api_version_func )
1234 m_api_version_func = (f_ck_declversion)this->get_addr( (string("_")+CK_DECLVERSION_FUNC).c_str() );
1235 if( !m_api_version_func )
1236 {
1237 m_last_error = string("no version function found in dll '") + m_filename + string("'");
1238 return FALSE;
1239 }
1240
1241 // check version
1242 t_CKUINT dll_version = m_api_version_func();
1243 // get major and minor version numbers
1244 m_apiVersionMajor = CK_DLL_VERSION_GETMAJOR(dll_version);
1245 m_apiVersionMinor = CK_DLL_VERSION_GETMINOR(dll_version);
1246
1247 // get the address of the DL info function from the DLL
1248 if( !m_info_func )
1249 m_info_func = (f_ck_info)this->get_addr( CK_INFO_FUNC );
1250 if( !m_info_func )
1251 m_info_func = (f_ck_info)this->get_addr( (string("_")+CK_INFO_FUNC).c_str() );
1252
1253 // if info func found
1254 if( m_info_func )
1255 {
1256 // query the module for info
1257 m_info_func( &m_query );
1258 }
1259
1260 return TRUE;
1261 }
1262
1263
1264
1265
1266 //-----------------------------------------------------------------------------
1267 // name: unload()
1268 // desc: ...
1269 //-----------------------------------------------------------------------------
1270 t_CKBOOL Chuck_DLL::unload()
1271 {
1272 if( !m_handle && !m_query_func )
1273 {
1274 m_last_error = "cannot unload dynamic library - nothing open...";
1275 return FALSE;
1276 }
1277
1278 // if( m_detach_func ) m_detach_func( 0, NULL );
1279
1280 if( m_handle )
1281 {
1282 dlclose( m_handle );
1283 m_handle = NULL;
1284 }
1285 else
1286 m_query_func = NULL;
1287
1288 return TRUE;
1289 }
1290
1291
1292
1293
1294 //-----------------------------------------------------------------------------
1295 // name: get_addr()
1296 // desc: ...
1297 //-----------------------------------------------------------------------------
1298 void * Chuck_DLL::get_addr( const char * symbol )
1299 {
1300 if( !m_handle )
1301 {
1302 m_last_error = "cannot find addr from dynamic library - nothing open...";
1303 return FALSE;
1304 }
1305
1306 return dlsym( m_handle, symbol );
1307 }
1308
1309
1310
1311
1312 //-----------------------------------------------------------------------------
1313 // name: last_error()
1314 // desc: ...
1315 //-----------------------------------------------------------------------------
1316 const char * Chuck_DLL::last_error() const
1317 {
1318 return m_last_error.c_str();
1319 }
1320
1321
1322
1323
1324 //-----------------------------------------------------------------------------
1325 // name: name()
1326 // desc: return name given to dll
1327 //-----------------------------------------------------------------------------
1328 const char * Chuck_DLL::name() const
1329 {
1330 return m_id.c_str();
1331 }
1332
1333
1334
1335
1336 //-----------------------------------------------------------------------------
1337 // name: filepath()
1338 // desc: return the file path
1339 //-----------------------------------------------------------------------------
1340 const char * Chuck_DLL::filepath() const
1341 {
1342 return m_filename.c_str();
1343 }
1344
1345
1346
1347
1348 //-----------------------------------------------------------------------------
1349 // name: getinfo()
1350 // desc: get query info
1351 //-----------------------------------------------------------------------------
1352 string Chuck_DLL::getinfo( const string & key )
1353 {
1354 return ck_getinfo( &m_query, key.c_str() );
1355 }
1356
1357
1358
1359
1360 //-----------------------------------------------------------------------------
1361 // name: versionMajor()
1362 // desc: get version major
1363 //-----------------------------------------------------------------------------
1364 t_CKUINT Chuck_DLL::versionMajor()
1365 {
1366 // probe dll
1367 if( !this->probe() ) return 0;
1368 // return
1369 return m_apiVersionMajor;
1370 }
1371
1372
1373
1374
1375 //-----------------------------------------------------------------------------
1376 // name: versionMinor()
1377 // desc: get version minor
1378 //-----------------------------------------------------------------------------
1379 t_CKUINT Chuck_DLL::versionMinor()
1380 {
1381 // probe dll
1382 if( !this->probe() ) return 0;
1383 // return
1384 return m_apiVersionMinor;
1385 }
1386
1387
1388
1389
1390 //-----------------------------------------------------------------------------
1391 // name: compatible()
1392 // desc: if version compatible between chugin and host
1393 //-----------------------------------------------------------------------------
1394 t_CKBOOL Chuck_DLL::compatible()
1395 {
1396 // probe dll
1397 if( !this->probe() ) return FALSE;
1398 // major AND minor version must match | 1.5.2.0 (ge)
1399 // (was: major version must be same between chugin and host, and
1400 // chugin minor version must less than or equal host minor version)
1401 if( m_apiVersionMajor == CK_DLL_VERSION_MAJOR &&
1402 m_apiVersionMinor == CK_DLL_VERSION_MINOR ) return TRUE;
1403
1404 // error string
1405 m_last_error = string("incompatible API version: chugin (")
1406 + ck_itoa(m_apiVersionMajor) + "." + ck_itoa(m_apiVersionMinor)
1407 + string(") vs. host (")
1408 + ck_itoa(CK_DLL_VERSION_MAJOR) + "." + ck_itoa(CK_DLL_VERSION_MINOR)
1409 + ")";
1410 // done
1411 return FALSE;
1412 }
1413
1414
1415
1416
1417 //-----------------------------------------------------------------------------
1418 // name: Chuck_DL_Query
1419 // desc: ...
1420 //-----------------------------------------------------------------------------
1421 Chuck_DL_Query::Chuck_DL_Query( Chuck_Carrier * carrier, Chuck_DLL * dll )
1422 {
1423 // set the pointers to functions so the module can call
1424 ck_vm = ck_get_vm;
1425 ck_api = ck_get_api;
1426 setname = ck_setname;
1427 setinfo = ck_setinfo;
1428 getinfo = ck_getinfo;
1429 begin_class = ck_begin_class;
1430 add_ctor = ck_add_ctor;
1431 add_dtor = ck_add_dtor;
1432 add_mfun = ck_add_mfun;
1433 add_sfun = ck_add_sfun;
1434 add_mvar = ck_add_mvar;
1435 add_svar = ck_add_svar;
1436 add_arg = ck_add_arg;
1437 add_op_overload_binary = ck_add_op_overload_binary;
1438 add_op_overload_prefix = ck_add_op_overload_prefix;
1439 add_op_overload_postfix = ck_add_op_overload_postfix;
1440 add_ugen_func = ck_add_ugen_func;
1441 add_ugen_funcf = ck_add_ugen_funcf;
1442 add_ugen_funcf_auto_num_channels = ck_add_ugen_funcf_auto_num_channels;
1443 // add_ugen_ctrl = ck_add_ugen_ctrl; // not used
1444 end_class = ck_end_class;
1445 doc_class = ck_doc_class;
1446 doc_func = ck_doc_func;
1447 doc_var = ck_doc_var;
1448 add_ex = ck_add_example; // 1.5.0.0 (ge) added
1449 create_main_thread_hook = ck_create_main_thread_hook;
1450 register_shreds_watcher = ck_register_shreds_watcher; // 1.5.1.5 (ge & andrew)
1451 unregister_shreds_watcher = ck_unregister_shreds_watcher; // 1.5.1.5 (ge & andrew)
1452 register_callback_on_shutdown = ck_register_callback_on_shutdown; // 1.5.2.5 (ge)
1453 register_callback_on_srate_update = ck_register_callback_on_srate_update; // 1.5.4.2 (ge)
1454 m_carrier = carrier;
1455 dll_ref = dll; // 1.5.1.3 (ge) added
1456
1457 dll_name = "[no name]";
1458 curr_class = NULL;
1459 curr_func = NULL;
1460 // memset(reserved2, NULL, sizeof(void*)*RESERVED_SIZE);
1461
1462 // 1.5.0.4 (ge) allow carrier to be NULL (for query purposes)
1463 if( m_carrier != NULL )
1464 {
1465 // get the actual runtime sample rate
1466 m_srate = m_carrier->vm->srate();
1467 }
1468 else
1469 {
1470 // set to something invalid...
1471 // (instead of default sample rate, which could be harder to notice / debug)
1472 m_srate = 0;
1473 }
1474 // get DL API reference | 1.5.1.5
1475 m_api = Chuck_DL_Api::instance();
1476
1477 // clear error flag
1478 errorEncountered = FALSE;
1479 }
1480
1481
1482
1483
1484 //-----------------------------------------------------------------------------
1485 // name: clear()
1486 // desc: clear the query
1487 //-----------------------------------------------------------------------------
1488 void Chuck_DL_Query::clear()
1489 {
1490 // set to no name
1491 dll_name = "[no name]";
1492
1493 // delete classes
1494 for( t_CKUINT i = 0; i < classes.size(); i++ ) CK_SAFE_DELETE( classes[i] );
1495 // delete operator overloads
1496 for( t_CKUINT i = 0; i < op_overloads.size(); i++ ) CK_SAFE_DELETE( op_overloads[i] );
1497
1498 // clear
1499 classes.clear();
1500 op_overloads.clear();
1501 }
1502
1503
1504
1505
1506 //-----------------------------------------------------------------------------
1507 // name: srate()
1508 // desc: get sample rate (to be called by host only; chugins use API->vm->srate)
1509 //-----------------------------------------------------------------------------
1510 t_CKUINT Chuck_DL_Query::srate()
1511 {
1512 return m_srate;
1513 }
1514
1515
1516
1517
1518
1519 //-----------------------------------------------------------------------------
1520 // name: ~Chuck_DL_Class()
1521 // desc: ...
1522 //-----------------------------------------------------------------------------
1523 Chuck_DL_Class::~Chuck_DL_Class()
1524 {
1525 t_CKUINT i;
1526
1527 // delete mfuns
1528 for( i = 0; i < mfuns.size(); i++ ) delete mfuns[i];
1529 // delete sfuns
1530 for( i = 0; i < sfuns.size(); i++ ) delete sfuns[i];
1531 // delete mvars
1532 for( i = 0; i < mvars.size(); i++ ) delete mvars[i];
1533 // delete svars
1534 for( i = 0; i < svars.size(); i++ ) delete svars[i];
1535 // delete classes
1536 for( i = 0; i < classes.size(); i++ ) delete classes[i];
1537 }
1538
1539
1540
1541
1542 //-----------------------------------------------------------------------------
1543 // name: ~Chuck_DL_Func()
1544 // desc: ...
1545 //-----------------------------------------------------------------------------
1546 Chuck_DL_Func::~Chuck_DL_Func()
1547 {
1548 // clean up
1549 for( t_CKUINT i = 0; i < args.size(); i++ )
1550 CK_SAFE_DELETE( args[i] );
1551 // clear
1552 args.clear();
1553 // zero
1554 opOverloadKind = ckte_op_overload_NONE;
1555 op2overload = ae_op_none;
1556 }
1557
1558
1559
1560
1561 //-----------------------------------------------------------------------------
1562 // Main Thread Hook stuff
1563 //-----------------------------------------------------------------------------
1564 t_CKBOOL ck_mthook_activate(Chuck_DL_MainThreadHook *hook)
1565 {
1566 hook->m_carrier->chuck->setMainThreadHook(hook);
1567 hook->m_active = true;
1568 return hook->m_active;
1569 }
1570
1571 t_CKBOOL ck_mthook_deactivate(Chuck_DL_MainThreadHook *hook)
1572 {
1573 hook->m_carrier->chuck->setMainThreadHook(NULL);
1574 hook->m_active = false;
1575 return hook->m_active;
1576 }
1577
1578 //-----------------------------------------------------------------------------
1579 // name: Chuck_DL_MainThreadHook()
1580 // desc: ...
1581 //-----------------------------------------------------------------------------
1582 Chuck_DL_MainThreadHook::Chuck_DL_MainThreadHook( f_mainthreadhook hook, f_mainthreadquit quit,
1583 void * bindle, Chuck_Carrier * carrier )
1584 : activate(ck_mthook_activate),
1585 deactivate(ck_mthook_deactivate),
1586 m_carrier(carrier),
1587 m_hook(hook),
1588 m_quit(quit),
1589 m_bindle(bindle),
1590 m_active(FALSE)
1591 { }
1592
1593
1594
1595
1596 //-----------------------------------------------------------------------------
1597 // Chuck_DL_Api stuff
1598 //-----------------------------------------------------------------------------
1599 // note on function hooks, e.g., create_string:
1600 // the implementation is always to be compiled as part of the host
1601 // (e.g., chuck or miniAudicle), and never as part of the client
1602 // (e.g., a chugin). The latter is given a function pointer (hook) at
1603 // module load-time; this ensures that the actual instantiation is in
1604 // alignment with whatever host is loading the module.
1605 //-----------------------------------------------------------------------------
1606 Chuck_DL_Api Chuck_DL_Api::g_api;
1607
1608
1609 //-----------------------------------------------------------------------------
1610 // name: ck_srate() | add 1.5.1.5
1611 // desc: host-side hook implementation for getting system srate
1612 //-----------------------------------------------------------------------------
1613 static t_CKUINT CK_DLL_CALL ck_srate( Chuck_VM * vm )
1614 {
1615 return vm->srate();
1616 }
1617
1618
1619 //-----------------------------------------------------------------------------
1620 // name: ck_now() | add 1.5.1.5
1621 // desc: host-side hook implementation for getting chuck system now
1622 //-----------------------------------------------------------------------------
1623 static t_CKTIME CK_DLL_CALL ck_now( Chuck_VM * vm )
1624 {
1625 return vm->now();
1626 }
1627
1628
1629 //-----------------------------------------------------------------------------
1630 // name: create_event_buffer() | 1.5.1.5 (ge, andrew) added
1631 // desc: host-side hoook implemenation for
1632 // creatinga new lock-free one-producer, one-consumer buffer
1633 //-----------------------------------------------------------------------------
1634 static CBufferSimple * CK_DLL_CALL ck_create_event_buffer( Chuck_VM * vm )
1635 {
1636 return vm->create_event_buffer();
1637 }
1638
1639
1640 //-----------------------------------------------------------------------------
1641 // name: queue_event() | 1.5.1.5 (ge, andrew) added
1642 // desc: host-side hoook implemenation for queuing an event
1643 // NOTE num_msg must be 1; buffer created using create_event_buffer()
1644 //-----------------------------------------------------------------------------
1645 static t_CKBOOL CK_DLL_CALL ck_queue_event( Chuck_VM * vm, Chuck_Event * event, t_CKINT num_msg, CBufferSimple * buffer )
1646 {
1647 return vm->queue_event( event, num_msg, buffer );
1648 }
1649
1650
1651 //-----------------------------------------------------------------------------
1652 // name: ck_get_type()
1653 // desc: host-side hook implementation for retrieving a type by name
1654 //-----------------------------------------------------------------------------
1655 static Chuck_DL_Api::Type CK_DLL_CALL ck_get_type( Chuck_Object * object )
1656 {
1657 return object->type_ref;
1658 }
1659
1660
1661 //-----------------------------------------------------------------------------
1662 // name: ck_get_vtable_offset()
1663 // desc: function pointer get_type()
1664 //-----------------------------------------------------------------------------
1665 t_CKINT CK_DLL_CALL ck_get_vtable_offset( Chuck_VM * vm, Chuck_Type * t, const char * valueName )
1666 {
1667 // find the offset for value by name
1668 Chuck_Value * value = type_engine_find_value( t, valueName );
1669 // value not found
1670 if( !value || !value->func_ref ) return -1;
1671 // return it
1672 return value->func_ref->vt_index;
1673 }
1674
1675
1676 //-----------------------------------------------------------------------------
1677 // add reference count
1678 //-----------------------------------------------------------------------------
1679 void CK_DLL_CALL ck_add_ref( Chuck_DL_Api::Object object )
1680 {
1681 Chuck_Object * obj = (Chuck_Object *)object;
1682 CK_SAFE_ADD_REF(obj);
1683 }
1684
1685
1686 //-----------------------------------------------------------------------------
1687 // release reference count
1688 //-----------------------------------------------------------------------------
1689 void CK_DLL_CALL ck_release( Chuck_DL_Api::Object object )
1690 {
1691 Chuck_Object * obj = (Chuck_Object *)object;
1692 CK_SAFE_RELEASE(obj);
1693 }
1694
1695
1696 //-----------------------------------------------------------------------------
1697 // get reference count
1698 //-----------------------------------------------------------------------------
1699 t_CKUINT CK_DLL_CALL ck_refcount( Chuck_DL_Api::Object object )
1700 {
1701 Chuck_Object * obj = (Chuck_Object *)object;
1702 if( !obj ) return 0;
1703 return obj->refcount();
1704 }
1705
1706
1707
1708
1709 //-----------------------------------------------------------------------------
1710 // helper function
1711 //-----------------------------------------------------------------------------
1712 Chuck_Object * do_ck_create( Chuck_VM_Shred * shred, Chuck_VM * vm, Chuck_DL_Api::Type t, vector<t_CKINT> & caps, t_CKBOOL addRef )
1713 {
1714 // type
1715 Chuck_Type * type = (Chuck_Type *)t;
1716 // object
1717 Chuck_Object * object = NULL;
1718
1719 // check if type is some kind of array
1720 if( isa(type, vm->env()->ckt_array) )
1721 {
1722 // 1.5.2.0 (nshaheed) added
1723 t_CKINT index = 0;
1724 t_CKBOOL is_object = isa(type->array_type, vm->env()->ckt_object);
1725 // get array depth
1726 t_CKUINT depth = type->array_depth;
1727 // create capacity
1728 t_CKINT * capacity = new t_CKINT[depth];
1729 // make sure
1730 assert( depth == caps.size() );
1731 // iterate and copy
1732 for( t_CKUINT i = 0; i < depth; i++ ) capacity[i] = caps[i];
1733 // allocate array
1734 object = do_alloc_array( vm, shred,
1735 &capacity[depth - 1], capacity,
1736 getkindof(vm->env(), type->array_type),
1737 is_object, NULL, index, type );
1738 // clean up
1739 CK_SAFE_DELETE_ARRAY( capacity );
1740 }
1741 else
1742 {
1743 // instantiate and initialize
1744 if( shred ) object = instantiate_and_initialize_object( type, shred, vm );
1745 else object = instantiate_and_initialize_object( type, vm );
1746 }
1747
1748 // if requested to add ref
1749 if( object && addRef ) CK_SAFE_ADD_REF(object);
1750
1751 // done
1752 return object;
1753 }
1754
1755
1756
1757
1758 //-----------------------------------------------------------------------------
1759 // name: ck_create_with_shred()
1760 // desc: host-side hook implementation for instantiating and initializing
1761 // a ChucK object by type, with reference to a parent shred;
1762 // the shred reference is useful in certain scenarios:
1763 // if 1) create a chuck-side class 2) with a member function that
1764 // is then invoked from c++ (e.g., using ck_invoke_mfun_immediate_mode)
1765 // and 3) that member function references global scope variables
1766 //-----------------------------------------------------------------------------
1767 static Chuck_DL_Api::Object CK_DLL_CALL ck_create_with_shred( Chuck_VM_Shred * shred, Chuck_DL_Api::Type t, t_CKBOOL addRef )
1768 {
1769 // check | 1.5.0.1 (ge) changed; used to be assert t != NULL
1770 if( t == NULL )
1771 {
1772 // print error message
1773 EM_error2( 0, "DL_Api:object:ck_create_with_shred: NULL object reference." );
1774 // done
1775 return NULL;
1776 }
1777
1778 // type
1779 Chuck_Type * type = (Chuck_Type *)t;
1780 // do_alloc_array() expects a stack of intial capacities for
1781 // each dimension of the array. Because this is only expected
1782 // to be used through the chuck_dl API, which does not support
1783 // setting the capacity explicitly, only empty arrays are
1784 // initialized | 1.5.2.0
1785 vector<t_CKINT> caps;
1786 // check for array type
1787 if( type->array_depth )
1788 for( t_CKUINT i = 0; i < type->array_depth; i++ )
1789 caps.push_back(0);
1790
1791 // process and return
1792 return (Chuck_DL_Api::Object)do_ck_create( shred, shred->vm_ref, t, caps, addRef );
1793 }
1794
1795
1796
1797
1798 //-----------------------------------------------------------------------------
1799 // name: ck_create_without_shred()
1800 // desc: host-side hook implementation for instantiating and initializing
1801 // a ChucK object by type, without a parent shred; see ck_create_with_shred()
1802 // for more details
1803 //-----------------------------------------------------------------------------
1804 static Chuck_DL_Api::Object CK_DLL_CALL ck_create_without_shred( Chuck_VM * vm, Chuck_DL_Api::Type t, t_CKBOOL addRef )
1805 {
1806 // check | 1.5.0.1 (ge) changed; used to be assert t != NULL
1807 if( t == NULL )
1808 {
1809 // print error message
1810 EM_error2( 0, "DL_Api:object:ck_create_without_shred: NULL object reference." );
1811 // done
1812 return NULL;
1813 }
1814
1815 // type
1816 Chuck_Type * type = (Chuck_Type *)t;
1817 // do_alloc_array() expects a stack of intial capacities for
1818 // each dimension of the array. Because this is only expected
1819 // to be used through the chuck_dl API, which does not support
1820 // setting the capacity explicitly, only empty arrays are
1821 // initialized | 1.5.2.0
1822 vector<t_CKINT> caps;
1823 // check for array type
1824 if( type->array_depth )
1825 for( t_CKUINT i = 0; i < type->array_depth; i++ )
1826 caps.push_back(0);
1827
1828 // process and return
1829 return (Chuck_DL_Api::Object)do_ck_create( NULL, vm, t, caps, addRef );
1830 }
1831
1832
1833
1834
1835 //-----------------------------------------------------------------------------
1836 // name: ck_create_string()
1837 // desc: host-side hook implementation for creating a chuck string
1838 //-----------------------------------------------------------------------------
1839 Chuck_String * CK_DLL_CALL ck_create_string( Chuck_VM * vm, const char * cstr, t_CKBOOL addRef )
1840 {
1841 // instantiate and initalize object
1842 Chuck_String * ckstr = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, vm );
1843 // if successfully instantiated
1844 if( ckstr )
1845 {
1846 // set the value
1847 ckstr->set( cstr ? cstr : "" );
1848 // if requested to add ref
1849 if( addRef ) CK_SAFE_ADD_REF(ckstr);
1850 }
1851 // return reference
1852 return ckstr;
1853 }
1854
1855
1856
1857
1858 //-----------------------------------------------------------------------------
1859 // name: ck_obj_data() | 1.5.2.0 (ge) added
1860 // desc: compute pointer to data segment + offset; use for member variable access
1861 //-----------------------------------------------------------------------------
1862 void * CK_DLL_CALL ck_obj_data( Chuck_Object * object, t_CKUINT byteOffset )
1863 {
1864 // chuck for NULL
1865 if( !object ) return NULL;
1866 // return pointer
1867 return ((t_CKBYTE *)(object->data)) + byteOffset;
1868 }
1869
1870
1871
1872
1873 //-----------------------------------------------------------------------------
1874 // name: ck_get_origin_shred()
1875 // desc: get origin shred
1876 //-----------------------------------------------------------------------------
1877 Chuck_VM_Shred * CK_DLL_CALL ck_get_origin_shred( Chuck_Object * object )
1878 {
1879 // check for NULL
1880 if( !object ) return NULL;
1881 // get it
1882 return object->originShred();
1883 }
1884
1885
1886
1887
1888 //-----------------------------------------------------------------------------
1889 // name: ck_set_origin_shred()
1890 // desc: set origin shred
1891 //-----------------------------------------------------------------------------
1892 void CK_DLL_CALL ck_set_origin_shred( Chuck_Object * object, Chuck_VM_Shred * shred )
1893 {
1894 // check for NULL
1895 if( !object ) return;
1896 // set it
1897 object->setOriginShred( shred );
1898 }
1899
1900
1901
1902
1903 //-----------------------------------------------------------------------------
1904 // name: ck_get_mvar()
1905 // desc: retrieve a class's member variable offset
1906 //-----------------------------------------------------------------------------
1907 static t_CKBOOL CK_DLL_CALL ck_get_mvar(Chuck_DL_Api::Object o, const char * name, te_Type basic_type, t_CKINT & offset)
1908 {
1909 // check | 1.5.0.1 (ge) added
1910 if( o == NULL )
1911 {
1912 // put error here
1913 EM_error2(0, "get mvar: ck_get_mvar: NULL object reference.");
1914 return FALSE;
1915 }
1916
1917 std::string mvar(name);
1918 Chuck_Object* obj = (Chuck_Object*)o;
1919 Chuck_Type* type = obj->type_ref;
1920 // check type
1921 if (type->nspc == NULL)
1922 {
1923 // put error here
1924 EM_error2(0, "get mvar: ck_get_mvar: object has no type info");
1925 return FALSE;
1926 }
1927
1928 vector<Chuck_Value*> vars;
1929 Chuck_Value* var = NULL;
1930 type->nspc->get_values(vars);
1931 // iterate over retrieved functions
1932 for (vector<Chuck_Value*>::iterator v = vars.begin(); v != vars.end(); v++)
1933 {
1934 // get pointer to chuck value
1935 Chuck_Value* value = *v;
1936 // check for NULL
1937 if (value == NULL) continue;
1938 // see if name matches
1939 if (value->name != mvar) continue;
1940 // see if name is correct type
1941 if (value->type->xid != basic_type) continue;
1942 // see if var is a member var
1943 if (!value->is_instance_member) continue;
1944
1945 // ladies and gentlemen, we've got it
1946 var = value;
1947 // done
1948 break;
1949 }
1950
1951 // make error
1952 if( !var )
1953 {
1954 EM_error2(0, "get_mvar(): member variable %s not found", mvar.c_str());
1955 return FALSE;
1956 }
1957 // return offset of var
1958 offset = var->offset;
1959
1960 return TRUE;
1961 }
1962
1963
1964
1965
1966 //-----------------------------------------------------------------------------
1967 // name: ck_get_mvar_int()
1968 // desc: retrieve a class's member variable of type int
1969 //-----------------------------------------------------------------------------
1970 static t_CKBOOL CK_DLL_CALL ck_get_mvar_int( Chuck_DL_Api::Object obj, const char * name, t_CKINT & value )
1971 {
1972 // default value
1973 value = 0;
1974 // check | 1.5.0.1 (ge) changed from assert obj != NULL
1975 if( obj == NULL ) return FALSE;
1976
1977 t_CKINT offset = -1;
1978 // get mvar
1979 t_CKBOOL success = ck_get_mvar(obj, name, te_int, offset);
1980 // check get int at offset
1981 if( success ) value = OBJ_MEMBER_INT(obj, offset); // get offset
1982
1983 // done
1984 return success;
1985 }
1986
1987
1988
1989
1990 //-----------------------------------------------------------------------------
1991 // name: ck_get_mvar_float()
1992 // desc: retrieve a class's member variable of type float
1993 //-----------------------------------------------------------------------------
1994 static t_CKBOOL CK_DLL_CALL ck_get_mvar_float( Chuck_DL_Api::Object obj, const char* name, t_CKFLOAT & value )
1995 {
1996 // default value
1997 value = 0.0;
1998 // check | 1.5.0.1 (ge) changed from assert obj != NULL
1999 if( obj == NULL ) return FALSE;
2000
2001 t_CKINT offset = -1;
2002 // get mvar
2003 t_CKBOOL success = ck_get_mvar(obj, name, te_float, offset);
2004 // check, get float at offset
2005 if( success ) value = OBJ_MEMBER_FLOAT(obj, offset);
2006
2007 // done
2008 return success;
2009 }
2010
2011
2012
2013
2014 //-----------------------------------------------------------------------------
2015 // name: ck_get_mvar_dur()
2016 // desc: retrieve a class's member variable of type dur
2017 //-----------------------------------------------------------------------------
2018 static t_CKBOOL CK_DLL_CALL ck_get_mvar_dur( Chuck_DL_Api::Object obj, const char * name, t_CKDUR & value )
2019 {
2020 // default value
2021 value = 0.0;
2022 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2023 if( obj == NULL ) return FALSE;
2024
2025 t_CKINT offset = -1;
2026 // get mvar
2027 t_CKBOOL success = ck_get_mvar(obj, name, te_dur, offset);
2028 // check, get dur at offset
2029 if( success ) value = OBJ_MEMBER_DUR(obj, offset);
2030
2031 // done
2032 return success;
2033 }
2034
2035
2036
2037
2038 //-----------------------------------------------------------------------------
2039 // name: ck_get_mvar_time()
2040 // desc: retrieve a class's member variable of type time
2041 //-----------------------------------------------------------------------------
2042 static t_CKBOOL CK_DLL_CALL ck_get_mvar_time( Chuck_DL_Api::Object obj, const char * name, t_CKTIME & value )
2043 {
2044 // default value
2045 value = 0.0;
2046 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2047 if( obj == NULL ) return FALSE;
2048
2049 t_CKINT offset = -1;
2050 // get mvar
2051 t_CKBOOL success = ck_get_mvar(obj, name, te_time, offset);
2052 // check, get time value at offset
2053 if( success ) value = OBJ_MEMBER_TIME(obj, offset);
2054
2055 // done
2056 return success;
2057 }
2058
2059
2060
2061
2062 //-----------------------------------------------------------------------------
2063 // name: ck_get_mvar_vec2()
2064 // desc: retrieve a class's member variable of type vec2/complex/polar
2065 //-----------------------------------------------------------------------------
2066 static t_CKBOOL CK_DLL_CALL ck_get_mvar_vec2( Chuck_DL_Api::Object obj, const char * name, t_CKVEC2 & value )
2067 {
2068 // default value
2069 value.x = value.y = 0.0;
2070 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2071 if( obj == NULL ) return FALSE;
2072
2073 t_CKINT offset = -1;
2074 // get mvar
2075 t_CKBOOL success = ck_get_mvar(obj, name, te_vec2, offset);
2076 // check, get vec2 value at offset
2077 if( success ) value = OBJ_MEMBER_VEC2(obj, offset);
2078
2079 // done
2080 return success;
2081 }
2082
2083
2084
2085
2086 //-----------------------------------------------------------------------------
2087 // name: ck_get_mvar_vec3()
2088 // desc: retrieve a class's member variable of type vec3
2089 //-----------------------------------------------------------------------------
2090 static t_CKBOOL CK_DLL_CALL ck_get_mvar_vec3( Chuck_DL_Api::Object obj, const char * name, t_CKVEC3 & value )
2091 {
2092 // default value
2093 value.x = value.y = value.z = 0.0;
2094 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2095 if( obj == NULL ) return FALSE;
2096
2097 t_CKINT offset = -1;
2098 // get mvar
2099 t_CKBOOL success = ck_get_mvar(obj, name, te_vec3, offset);
2100 // check, get vec3 value at offset
2101 if( success ) value = OBJ_MEMBER_VEC3(obj, offset);
2102
2103 // done
2104 return success;
2105 }
2106
2107
2108
2109
2110 //-----------------------------------------------------------------------------
2111 // name: ck_get_mvar_vec4()
2112 // desc: retrieve a class's member variable of type vec4
2113 //-----------------------------------------------------------------------------
2114 static t_CKBOOL CK_DLL_CALL ck_get_mvar_vec4( Chuck_DL_Api::Object obj, const char * name, t_CKVEC4 & value )
2115 {
2116 // default value
2117 value.x = value.y = value.z = value.w = 0.0;
2118 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2119 if( obj == NULL ) return FALSE;
2120
2121 t_CKINT offset = -1;
2122 // get mvar
2123 t_CKBOOL success = ck_get_mvar(obj, name, te_vec4, offset);
2124 // check, get vec4 value at offset
2125 if( success ) value = OBJ_MEMBER_VEC4(obj, offset);
2126
2127 // done
2128 return success;
2129 }
2130
2131
2132
2133
2134 //-----------------------------------------------------------------------------
2135 // name: ck_get_mvar_object()
2136 // desc: retrieve a class's member variable of type object
2137 //-----------------------------------------------------------------------------
2138 static t_CKBOOL CK_DLL_CALL ck_get_mvar_object( Chuck_DL_Api::Object obj, const char * name, Chuck_DL_Api::Object & object )
2139 {
2140 // default
2141 object = NULL;
2142 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2143 if( obj == NULL ) return FALSE;
2144
2145 t_CKINT offset = -1;
2146 // TODO how to do this?
2147 t_CKBOOL success = ck_get_mvar(obj, name, te_object, offset);
2148 // check, get object at offset
2149 if( success ) object = OBJ_MEMBER_OBJECT(obj, offset);
2150
2151 // done
2152 return success;
2153 }
2154
2155
2156
2157
2158 //-----------------------------------------------------------------------------
2159 // name: ck_get_mvar_string()
2160 // desc: retrieve a class's member variable of type string
2161 //-----------------------------------------------------------------------------
2162 static t_CKBOOL CK_DLL_CALL ck_get_mvar_string( Chuck_DL_Api::Object obj, const char * name, Chuck_DL_Api::String & str )
2163 {
2164 // default value
2165 str = NULL;
2166 // check | 1.5.0.1 (ge) changed from assert obj != NULL
2167 if( obj == NULL ) return FALSE;
2168
2169 t_CKINT offset = -1;
2170 // get mvar
2171 t_CKBOOL success = ck_get_mvar(obj, name, te_string, offset);
2172 // check, get string at offset
2173 if( success ) str = OBJ_MEMBER_STRING(obj, offset);
2174
2175 // done
2176 return success;
2177 }
2178
2179
2180
2181
2182 //-----------------------------------------------------------------------------
2183 // name: ck_string_set()
2184 // desc: set a chuck string
2185 //-----------------------------------------------------------------------------
2186 static t_CKBOOL CK_DLL_CALL ck_string_set( Chuck_DL_Api::String s, const char * str )
2187 {
2188 // check | 1.5.0.1 (ge) changed from assert s != NULL
2189 if( s == NULL ) return FALSE;
2190 // cast to string
2191 Chuck_String * string = (Chuck_String *) s;
2192 // action
2193 string->set( str != NULL ? str : "" );
2194 // done
2195 return TRUE;
2196 }
2197
2198
2199
2200
2201 //-----------------------------------------------------------------------------
2202 // name: ck_string_get()
2203 // desc: get a chuck string's internal c-string
2204 //-----------------------------------------------------------------------------
2205 static const char * CK_DLL_CALL ck_string_get( Chuck_String * str )
2206 {
2207 // check
2208 if( str == NULL ) return NULL;
2209 // get the internal c string
2210 return str->str().c_str();
2211 }
2212
2213
2214
2215
2216 //-----------------------------------------------------------------------------
2217 // name: ck_array_int_size()
2218 // desc: get size of an array | 1.5.1.3 (nshaheed) added
2219 //-----------------------------------------------------------------------------
2220 static t_CKINT CK_DLL_CALL ck_array_int_size( Chuck_DL_Api::ArrayInt array )
2221 {
2222 // check
2223 if( array == NULL ) return 0;
2224 // return size
2225 return array->size();
2226 }
2227
2228
2229
2230
2231 //-----------------------------------------------------------------------------
2232 // name: ck_array_int_get_idx()
2233 // desc: get an indexed element from an array | 1.5.1.3 (nshaheed) added
2234 //-----------------------------------------------------------------------------
2235 static t_CKINT CK_DLL_CALL ck_array_int_get_idx( Chuck_DL_Api::ArrayInt array, t_CKINT idx )
2236 {
2237 // value
2238 t_CKINT value = 0;
2239 // try to retrive value by index
2240 if( array ) array->get( idx, &value );
2241 // return value
2242 return value;
2243 }
2244
2245
2246
2247
2248 //-----------------------------------------------------------------------------
2249 // name: ck_array_int_set_idx()
2250 // desc: set an indexed element in an array | 1.5.2.5 (azaday) added
2251 //-----------------------------------------------------------------------------
2252 static void CK_DLL_CALL ck_array_int_set_idx( Chuck_DL_Api::ArrayInt array, t_CKINT idx, t_CKINT value )
2253 {
2254 if( array ) array->set( idx, value );
2255 }
2256
2257
2258
2259
2260 //-----------------------------------------------------------------------------
2261 // name: ck_array_int_get_key()
2262 // desc: get a keyed element from an array | 1.5.1.3 (nshaheed) added
2263 //-----------------------------------------------------------------------------
2264 static t_CKBOOL CK_DLL_CALL ck_array_int_get_key( Chuck_DL_Api::ArrayInt array, const char * key, t_CKINT & value )
2265 {
2266 // check
2267 if( array == NULL ) return FALSE;
2268 // action
2269 return array->get( key, &value );
2270 }
2271
2272
2273
2274
2275 //-----------------------------------------------------------------------------
2276 // name: ck_array_int_set_key()
2277 // desc: set a keyed element in an array | 1.5.2.5 (azaday) added
2278 //-----------------------------------------------------------------------------
2279 static void CK_DLL_CALL ck_array_int_set_key( Chuck_DL_Api::ArrayInt array, const char * key, t_CKINT value )
2280 {
2281 if( array ) array->set( key, value );
2282 }
2283
2284
2285
2286
2287 //-----------------------------------------------------------------------------
2288 // name: ck_array_int_push_back()
2289 // desc: push back an element into an array | 1.5.0.1 (ge) added
2290 //-----------------------------------------------------------------------------
2291 static t_CKBOOL CK_DLL_CALL ck_array_int_push_back( Chuck_DL_Api::ArrayInt array, t_CKINT value )
2292 {
2293 // check
2294 if( array == NULL ) return FALSE;
2295 // action
2296 array->push_back( (t_CKUINT)value );
2297 // done
2298 return TRUE;
2299 }
2300
2301
2302
2303
2304 //-----------------------------------------------------------------------------
2305 // name: ck_array_int_clear()
2306 // desc: clear array | 1.5.2.0 (ge) added
2307 //-----------------------------------------------------------------------------
2308 static void CK_DLL_CALL ck_array_int_clear( Chuck_DL_Api::ArrayInt array )
2309 {
2310 // check
2311 if( array == NULL ) return;
2312 // clear
2313 array->clear();
2314 }
2315
2316
2317
2318
2319 //-----------------------------------------------------------------------------
2320 // name: ck_array_float_size()
2321 // desc: get size of an array | 1.5.1.8 (nshaheed) added
2322 //-----------------------------------------------------------------------------
2323 static t_CKINT CK_DLL_CALL ck_array_float_size( Chuck_DL_Api::ArrayFloat array )
2324 {
2325 // check
2326 if( array == NULL ) return 0;
2327 // done
2328 return array->size();
2329 }
2330
2331
2332
2333
2334 //-----------------------------------------------------------------------------
2335 // name: ck_array_float_get_idx()
2336 // desc: get an indexed element from an array | 1.5.1.8 (nshaheed) added
2337 //-----------------------------------------------------------------------------
2338 static t_CKFLOAT CK_DLL_CALL ck_array_float_get_idx( Chuck_DL_Api::ArrayFloat array, t_CKINT idx )
2339 {
2340 // value
2341 t_CKFLOAT value = 0;
2342 // attempt to retrieve value
2343 if( array ) array->get( idx, &value );
2344 // action
2345 return value;
2346 }
2347
2348
2349
2350
2351 //-----------------------------------------------------------------------------
2352 // name: ck_array_float_set_idx()
2353 // desc: set an indexed element in an array | 1.5.2.5 (azaday) added
2354 //-----------------------------------------------------------------------------
2355 static void CK_DLL_CALL ck_array_float_set_idx( Chuck_DL_Api::ArrayFloat array, t_CKINT idx, t_CKFLOAT value )
2356 {
2357 if( array ) array->set( idx, value );
2358 }
2359
2360
2361
2362
2363 //-----------------------------------------------------------------------------
2364 // name: ck_array_float_get_key()
2365 // desc: get a keyed element from an array | 1.5.1.8 (nshaheed) added
2366 //-----------------------------------------------------------------------------
2367 static t_CKBOOL CK_DLL_CALL ck_array_float_get_key( Chuck_DL_Api::ArrayFloat array, const char * key, t_CKFLOAT & value )
2368 {
2369 // check
2370 if( array == NULL ) return FALSE;
2371 // action
2372 return array->get( key, &value );
2373 }
2374
2375
2376
2377
2378 //-----------------------------------------------------------------------------
2379 // name: ck_array_float_set_key()
2380 // desc: set a keyed element in an array | 1.5.2.5 (azaday) added
2381 //-----------------------------------------------------------------------------
2382 static void CK_DLL_CALL ck_array_float_set_key( Chuck_DL_Api::ArrayFloat array, const char * key, t_CKFLOAT value )
2383 {
2384 if( array ) array->set( key, value );
2385 }
2386
2387
2388
2389
2390 //-----------------------------------------------------------------------------
2391 // name: ck_array_float_push_back()
2392 // desc: push back an element into an array | 1.5.1.8 (nshaheed) added
2393 //-----------------------------------------------------------------------------
2394 static t_CKBOOL CK_DLL_CALL ck_array_float_push_back( Chuck_DL_Api::ArrayFloat a, t_CKFLOAT value )
2395 {
2396 // check
2397 if( a == NULL ) return FALSE;
2398 // cast to array_float
2399 Chuck_ArrayFloat * array = (Chuck_ArrayFloat *)a;
2400 // action
2401 array->push_back( value );
2402 // done
2403 return TRUE;
2404 }
2405
2406
2407
2408
2409 //-----------------------------------------------------------------------------
2410 // name: ck_array_float_clear()
2411 // desc: clear array | 1.5.2.0 (ge) added
2412 //-----------------------------------------------------------------------------
2413 static void CK_DLL_CALL ck_array_float_clear( Chuck_DL_Api::ArrayFloat array )
2414 {
2415 // check
2416 if( array == NULL ) return;
2417 // clear
2418 array->clear();
2419 }
2420
2421
2422
2423
2424 //-----------------------------------------------------------------------------
2425 // name: ck_array_vec2_size()
2426 // desc: get size of an array | 1.5.2.0 (ge) added
2427 //-----------------------------------------------------------------------------
2428 static t_CKINT CK_DLL_CALL ck_array_vec2_size( Chuck_DL_Api::ArrayVec2 array )
2429 {
2430 // check
2431 if( array == NULL ) return 0;
2432 // return size
2433 return array->size();
2434 }
2435
2436
2437
2438
2439 //-----------------------------------------------------------------------------
2440 // name: ck_array_vec2_get_idx()
2441 // desc: get indexed element from array | 1.5.2.0 (ge) added
2442 //-----------------------------------------------------------------------------
2443 static t_CKVEC2 CK_DLL_CALL ck_array_vec2_get_idx( Chuck_DL_Api::ArrayVec2 array, t_CKINT idx )
2444 {
2445 // value
2446 t_CKVEC2 value; value.x = value.y = 0;
2447 // try to retrive value by index
2448 if( array ) array->get( idx, &value );
2449 // return value
2450 return value;
2451 }
2452
2453
2454
2455
2456 //-----------------------------------------------------------------------------
2457 // name: ck_array_vec2_set_idx()
2458 // desc: set indexed element in array | 1.5.2.5 (azaday) added
2459 //-----------------------------------------------------------------------------
2460 static void CK_DLL_CALL ck_array_vec2_set_idx( Chuck_DL_Api::ArrayVec2 array, t_CKINT idx, t_CKVEC2 value)
2461 {
2462 if( array ) array->set( idx, value );
2463 }
2464
2465
2466
2467
2468 //-----------------------------------------------------------------------------
2469 // name: ck_array_vec2_get_key()
2470 // desc: get keyed element from array | 1.5.2.0 (ge) added
2471 //-----------------------------------------------------------------------------
2472 static t_CKBOOL CK_DLL_CALL ck_array_vec2_get_key( Chuck_DL_Api::ArrayVec2 array, const char * key, t_CKVEC2 & value )
2473 {
2474 // check
2475 if( array == NULL ) return FALSE;
2476 // action
2477 return array->get( key, &value );
2478 }
2479
2480
2481
2482
2483 //-----------------------------------------------------------------------------
2484 // name: ck_array_vec2_set_key()
2485 // desc: set keyed element in array | 1.5.2.5 (azady) added
2486 //-----------------------------------------------------------------------------
2487 static void CK_DLL_CALL ck_array_vec2_set_key( Chuck_DL_Api::ArrayVec2 array, const char * key, t_CKVEC2 value )
2488 {
2489 if( array ) array->set( key, value );
2490 }
2491
2492
2493
2494
2495 //-----------------------------------------------------------------------------
2496 // name: ck_array_vec2_push_back()
2497 // desc: push back an element into an array | 1.5.0.1 (ge) added
2498 //-----------------------------------------------------------------------------
2499 static t_CKBOOL CK_DLL_CALL ck_array_vec2_push_back( Chuck_DL_Api::ArrayVec2 array, const t_CKVEC2 & value )
2500 {
2501 // check
2502 if( array == NULL ) return FALSE;
2503 // action
2504 array->push_back( value );
2505 // done
2506 return TRUE;
2507 }
2508
2509
2510
2511
2512 //-----------------------------------------------------------------------------
2513 // name: ck_array_vec2clear()
2514 // desc: clear array | 1.5.2.0 (ge) added
2515 //-----------------------------------------------------------------------------
2516 static void CK_DLL_CALL ck_array_vec2_clear( Chuck_DL_Api::ArrayVec2 array )
2517 {
2518 // check
2519 if( array == NULL ) return;
2520 // clear
2521 array->clear();
2522 }
2523
2524
2525
2526
2527 //-----------------------------------------------------------------------------
2528 // name: ck_array_vec3_size()
2529 // desc: get size of an array | 1.5.2.0 (ge) added
2530 //-----------------------------------------------------------------------------
2531 static t_CKINT CK_DLL_CALL ck_array_vec3_size( Chuck_DL_Api::ArrayVec3 array )
2532 {
2533 // check
2534 if( array == NULL ) return 0;
2535 // return size
2536 return array->size();
2537 }
2538
2539
2540
2541
2542 //-----------------------------------------------------------------------------
2543 // name: ck_array_vec3_get_idx()
2544 // desc: get indexed element from array | 1.5.2.0 (ge) added
2545 //-----------------------------------------------------------------------------
2546 static t_CKVEC3 CK_DLL_CALL ck_array_vec3_get_idx( Chuck_DL_Api::ArrayVec3 array, t_CKINT idx )
2547 {
2548 // value
2549 t_CKVEC3 value; value.x = value.y = value.z = 0;
2550 // try to retrive value by index
2551 if( array ) array->get( idx, &value );
2552 // return value
2553 return value;
2554 }
2555
2556
2557
2558
2559 //-----------------------------------------------------------------------------
2560 // name: ck_array_vec3_set_idx()
2561 // desc: set indexed element in array | 1.5.2.5 (azaday) added
2562 //-----------------------------------------------------------------------------
2563 static void CK_DLL_CALL ck_array_vec3_set_idx( Chuck_DL_Api::ArrayVec3 array, t_CKINT idx, t_CKVEC3 value )
2564 {
2565 if( array ) array->set( idx, value );
2566 }
2567
2568
2569
2570
2571 //-----------------------------------------------------------------------------
2572 // name: ck_array_vec3_get_key()
2573 // desc: get keyed element from array | 1.5.2.0 (ge) added
2574 //-----------------------------------------------------------------------------
2575 static t_CKBOOL CK_DLL_CALL ck_array_vec3_get_key( Chuck_DL_Api::ArrayVec3 array, const char * key, t_CKVEC3 & value )
2576 {
2577 // check
2578 if( array == NULL ) return FALSE;
2579 // action
2580 return array->get( key, &value );
2581 }
2582
2583
2584
2585
2586 //-----------------------------------------------------------------------------
2587 // name: ck_array_vec3_set_key()
2588 // desc: set keyed element in array | 1.5.2.5 (azaday) added
2589 //-----------------------------------------------------------------------------
2590 static void CK_DLL_CALL ck_array_vec3_set_key( Chuck_DL_Api::ArrayVec3 array, const char * key, t_CKVEC3 value )
2591 {
2592 if( array ) array->set( key, value );
2593 }
2594
2595
2596
2597
2598 //-----------------------------------------------------------------------------
2599 // name: ck_array_vec3_push_back()
2600 // desc: push back an element into an array | 1.5.0.1 (ge) added
2601 //-----------------------------------------------------------------------------
2602 static t_CKBOOL CK_DLL_CALL ck_array_vec3_push_back( Chuck_DL_Api::ArrayVec3 array, const t_CKVEC3 & value )
2603 {
2604 // check
2605 if( array == NULL ) return FALSE;
2606 // action
2607 array->push_back( value );
2608 // done
2609 return TRUE;
2610 }
2611
2612
2613
2614
2615 //-----------------------------------------------------------------------------
2616 // name: ck_array_vec3clear()
2617 // desc: clear array | 1.5.2.0 (ge) added
2618 //-----------------------------------------------------------------------------
2619 static void CK_DLL_CALL ck_array_vec3_clear( Chuck_DL_Api::ArrayVec3 array )
2620 {
2621 // check
2622 if( array == NULL ) return;
2623 // clear
2624 array->clear();
2625 }
2626
2627
2628
2629
2630 //-----------------------------------------------------------------------------
2631 // name: ck_array_vec4_size()
2632 // desc: get size of an array | 1.5.2.0 (ge) added
2633 //-----------------------------------------------------------------------------
2634 static t_CKINT CK_DLL_CALL ck_array_vec4_size( Chuck_DL_Api::ArrayVec4 array )
2635 {
2636 // check
2637 if( array == NULL ) return 0;
2638 // return size
2639 return array->size();
2640 }
2641
2642
2643
2644
2645 //-----------------------------------------------------------------------------
2646 // name: ck_array_vec4_get_idx()
2647 // desc: get indexed element from array | 1.5.2.0 (ge) added
2648 //-----------------------------------------------------------------------------
2649 static t_CKVEC4 CK_DLL_CALL ck_array_vec4_get_idx( Chuck_DL_Api::ArrayVec4 array, t_CKINT idx )
2650 {
2651 // value
2652 t_CKVEC4 value; value.x = value.y = value.z = value.w = 0;
2653 // try to retrive value by index
2654 if( array ) array->get( idx, &value );
2655 // return value
2656 return value;
2657 }
2658
2659
2660
2661
2662 //-----------------------------------------------------------------------------
2663 // name: ck_array_vec4_set_idx()
2664 // desc: set indexed element in array | 1.5.2.5 (azaday) added
2665 //-----------------------------------------------------------------------------
2666 static void CK_DLL_CALL ck_array_vec4_set_idx( Chuck_DL_Api::ArrayVec4 array, t_CKINT idx, t_CKVEC4 value )
2667 {
2668 if( array ) array->set( idx, value );
2669 }
2670
2671
2672
2673
2674 //-----------------------------------------------------------------------------
2675 // name: ck_array_vec4_get_key()
2676 // desc: get keyed element from array | 1.5.2.0 (ge) added
2677 //-----------------------------------------------------------------------------
2678 static t_CKBOOL CK_DLL_CALL ck_array_vec4_get_key( Chuck_DL_Api::ArrayVec4 array, const char * key, t_CKVEC4 & value )
2679 {
2680 // check
2681 if( array == NULL ) return FALSE;
2682 // action
2683 return array->get( key, &value );
2684 }
2685
2686
2687
2688
2689 //-----------------------------------------------------------------------------
2690 // name: ck_array_vec4_set_key()
2691 // desc: set keyed element in array | 1.5.2.5 (azaday) added
2692 //-----------------------------------------------------------------------------
2693 static void CK_DLL_CALL ck_array_vec4_set_key( Chuck_DL_Api::ArrayVec4 array, const char * key, t_CKVEC4 value )
2694 {
2695 if( array ) array->set( key, value );
2696 }
2697
2698
2699
2700
2701 //-----------------------------------------------------------------------------
2702 // name: ck_array_vec4_push_back()
2703 // desc: push back an element into an array | 1.5.0.1 (ge) added
2704 //-----------------------------------------------------------------------------
2705 static t_CKBOOL CK_DLL_CALL ck_array_vec4_push_back( Chuck_DL_Api::ArrayVec4 array, const t_CKVEC4 & value )
2706 {
2707 // check
2708 if( array == NULL ) return FALSE;
2709 // action
2710 array->push_back( value );
2711 // done
2712 return TRUE;
2713 }
2714
2715
2716
2717
2718 //-----------------------------------------------------------------------------
2719 // name: ck_array_vec4clear()
2720 // desc: clear array | 1.5.2.0 (ge) added
2721 //-----------------------------------------------------------------------------
2722 static void CK_DLL_CALL ck_array_vec4_clear( Chuck_DL_Api::ArrayVec4 array )
2723 {
2724 // check
2725 if( array == NULL ) return;
2726 // clear
2727 array->clear();
2728 }
2729
2730
2731
2732
2733 //-----------------------------------------------------------------------------
2734 // constructor for the VMApi; connects function pointers to host-side impl
2735 //-----------------------------------------------------------------------------
2736 Chuck_DL_Api::VMApi::VMApi() :
2737 srate(ck_srate),
2738 now(ck_now),
2739 create_event_buffer(ck_create_event_buffer),
2740 queue_event(ck_queue_event),
2741 invoke_mfun_immediate_mode(ck_invoke_mfun_immediate_mode),
2742 throw_exception(ck_throw_exception),
2743 em_log(ck_em_log),
2744 remove_all_shreds(ck_remove_all_shreds)
2745 { }
2746
2747
2748
2749
2750 //-----------------------------------------------------------------------------
2751 // constructor for the ObjectApi; connects function pointers to host-side impl
2752 //-----------------------------------------------------------------------------
2753 Chuck_DL_Api::ObjectApi::ObjectApi() :
2754 get_type(ck_get_type),
2755 add_ref(ck_add_ref),
2756 release(ck_release),
2757 refcount(ck_refcount),
2758 create(ck_create_with_shred),
2759 create_without_shred(ck_create_without_shred),
2760 create_string(ck_create_string),
2761 data(ck_obj_data),
2762 get_origin_shred(ck_get_origin_shred),
2763 set_origin_shred(ck_set_origin_shred),
2764 get_mvar_int(ck_get_mvar_int),
2765 get_mvar_float(ck_get_mvar_float),
2766 get_mvar_dur(ck_get_mvar_dur),
2767 get_mvar_time(ck_get_mvar_time),
2768 get_mvar_vec2(ck_get_mvar_vec2),
2769 get_mvar_vec3(ck_get_mvar_vec3),
2770 get_mvar_vec4(ck_get_mvar_vec4),
2771 get_mvar_string(ck_get_mvar_string),
2772 get_mvar_object(ck_get_mvar_object),
2773 set_string(ck_string_set),
2774 str(ck_string_get),
2775 array_int_size(ck_array_int_size),
2776 array_int_get_idx(ck_array_int_get_idx),
2777 array_int_set_idx(ck_array_int_set_idx),
2778 array_int_get_key(ck_array_int_get_key),
2779 array_int_set_key(ck_array_int_set_key),
2780 array_int_push_back(ck_array_int_push_back),
2781 array_int_clear(ck_array_int_clear),
2782 array_float_size(ck_array_float_size),
2783 array_float_get_idx(ck_array_float_get_idx),
2784 array_float_set_idx(ck_array_float_set_idx),
2785 array_float_get_key(ck_array_float_get_key),
2786 array_float_set_key(ck_array_float_set_key),
2787 array_float_push_back(ck_array_float_push_back),
2788 array_float_clear( ck_array_float_clear ),
2789 array_vec2_size(ck_array_vec2_size),
2790 array_vec2_get_idx(ck_array_vec2_get_idx),
2791 array_vec2_set_idx(ck_array_vec2_set_idx),
2792 array_vec2_get_key(ck_array_vec2_get_key),
2793 array_vec2_set_key(ck_array_vec2_set_key),
2794 array_vec2_push_back(ck_array_vec2_push_back),
2795 array_vec2_clear( ck_array_vec2_clear ),
2796 array_vec3_size(ck_array_vec3_size),
2797 array_vec3_get_idx(ck_array_vec3_get_idx),
2798 array_vec3_set_idx(ck_array_vec3_set_idx),
2799 array_vec3_get_key(ck_array_vec3_get_key),
2800 array_vec3_set_key(ck_array_vec3_set_key),
2801 array_vec3_push_back(ck_array_vec3_push_back),
2802 array_vec3_clear( ck_array_vec3_clear ),
2803 array_vec4_size(ck_array_vec4_size),
2804 array_vec4_get_idx(ck_array_vec4_get_idx),
2805 array_vec4_set_idx(ck_array_vec4_set_idx),
2806 array_vec4_get_key(ck_array_vec4_get_key),
2807 array_vec4_set_key(ck_array_vec4_set_key),
2808 array_vec4_push_back(ck_array_vec4_push_back),
2809 array_vec4_clear( ck_array_vec4_clear )
2810 { }
2811
2812
2813
2814
2815 //-----------------------------------------------------------------------------
2816 // constructor for the TypeApi; connects function pointers to host-side impl
2817 //-----------------------------------------------------------------------------
2818 Chuck_DL_Api::TypeApi::TypeApi() :
2819 lookup(ck_type_lookup),
2820 get_vtable_offset(ck_get_vtable_offset),
2821 is_equal(ck_type_isequal),
2822 isa(ck_type_isa),
2823 callback_on_instantiate(ck_callback_on_instantiate),
2824 origin_hint(ck_origin_hint),
2825 name(ck_type_name),
2826 base_name(ck_type_base_name)
2827 { }
2828
2829
2830
2831
2832 //-----------------------------------------------------------------------------
2833 // constructor for the ShredApi; connects function pointers to host-side impl
2834 //-----------------------------------------------------------------------------
2835 Chuck_DL_Api::ShredApi::ShredApi() :
2836 parent(ck_shred_parent)
2837 { }
2838
2839
2840
2841 //-----------------------------------------------------------------------------
2842 // name: ck_invoke_mfun_immediate_mode()
2843 // desc: directly call a chuck member function
2844 // (supports both native (c++) and user (chuck)
2845 //-----------------------------------------------------------------------------
2846 Chuck_DL_Return CK_DLL_CALL ck_invoke_mfun_immediate_mode( Chuck_Object * obj, t_CKUINT func_vt_offset, Chuck_VM * vm, Chuck_VM_Shred * caller_shred, Chuck_DL_Arg * args_list, t_CKUINT numArgs )
2847 {
2848 Chuck_DL_Return RETURN;
2849 // check objt
2850 if( !obj ) return RETURN;
2851 // verify bounds
2852 if( func_vt_offset >= obj->vtable->funcs.size() )
2853 {
2854 EM_error3( "(internal error) ck_invoke_mfun_immediate_mode() encountered invalid virtual function index: %lu", func_vt_offset );
2855 return RETURN;
2856 }
2857
2858 // get the member function
2859 Chuck_Func * func = obj->vtable->funcs[func_vt_offset];
2860 // get the code for the function
2861 Chuck_VM_Code * code = func->code;
2862 // check whether native or user
2863 if( code->native_func )
2864 {
2865 // native func (defined in c++)
2866 f_mfun f = (f_mfun)code->native_func;
2867 // pack the args_list into memory
2868 func->pack_cache( args_list, numArgs );
2869 // call the function | added 1.3.0.0: the DL API instance
2870 f( obj, func->args_cache, &RETURN, vm, caller_shred, Chuck_DL_Api::instance() );
2871 }
2872 else
2873 {
2874 // user func (defined in chuck)
2875 // attach invoker, if needed
2876 if( !func->setup_invoker(func_vt_offset, vm, caller_shred ) )
2877 {
2878 // error
2879 EM_error3( "ck_invoke_mfun_immediate_mode() cannot set up invoker for: %s", func->signature(FALSE,TRUE).c_str() );
2880 return RETURN;
2881 }
2882 // pack the args_list into vector
2883 vector<Chuck_DL_Arg> args_vector;
2884 // iterate over c-style array
2885 for( t_CKUINT i = 0; i < numArgs; i++ ) args_vector.push_back(args_list[i]);
2886 // invoke the invoker
2887 func->invoker_mfun->invoke( obj, args_vector, caller_shred );
2888 }
2889
2890 // return it
2891 return RETURN;
2892 }
2893
2894
2895
2896
2897 //-----------------------------------------------------------------------------
2898 // name: ck_throw_exception()
2899 // desc: throw an exception, if shred is passed it, it will be halted
2900 //-----------------------------------------------------------------------------
2901 void CK_DLL_CALL ck_throw_exception( const char * exception, const char * desc,
2902 Chuck_VM_Shred * shred )
2903 {
2904 // constructor string
2905 string e = exception ? string(exception) : "[unnamed]";
2906 if( desc ) e += ": " + string(desc);
2907
2908 // display
2909 EM_exception( e.c_str() );
2910 // if shred passed in
2911 if( shred )
2912 {
2913 // halt shred
2914 shred->is_running = FALSE;
2915 // mark as done
2916 shred->is_done = TRUE;
2917 }
2918 }
2919
2920
2921
2922
2923 //-----------------------------------------------------------------------------
2924 // name: ck_em_log()
2925 // desc: host impl: print to chuck logger
2926 //-----------------------------------------------------------------------------
2927 void CK_DLL_CALL ck_em_log( t_CKINT level, const char * text )
2928 {
2929 // check
2930 if( !text ) return;
2931 // display
2932 EM_log( level, text );
2933 }
2934
2935
2936
2937
2938 //-----------------------------------------------------------------------------
2939 // name: ck_remove_all_shreds()
2940 // desc: host impl for system function: remove all shreds in VM; use with care
2941 //-----------------------------------------------------------------------------
2942 void CK_DLL_CALL ck_remove_all_shreds( Chuck_VM * vm )
2943 {
2944 // construct chuck msg (must allocate on heap, as VM will clean up)
2945 Chuck_Msg * msg = new Chuck_Msg();
2946 // set type
2947 msg->type = CK_MSG_REMOVEALL;
2948 // remove all shreds
2949 vm->queue_msg( msg );
2950 }
2951
2952
2953
2954
2955 //-----------------------------------------------------------------------------
2956 // name: ck_type_lookup()
2957 // desc: host-side hook implementation for retrieving a type by name
2958 //-----------------------------------------------------------------------------
2959 Chuck_DL_Api::Type CK_DLL_CALL ck_type_lookup( Chuck_VM * vm, const char * name )
2960 {
2961 // get the type
2962 Chuck_Env * env = vm->env();
2963 a_Id_List list = new_id_list( name, 0, 0 /*, NULL*/ ); // TODO: nested types
2964 Chuck_Type * t = type_engine_find_type( env, list );
2965 delete_id_list( list );
2966 // return
2967 return (Chuck_DL_Api::Type)t;
2968 }
2969
2970
2971
2972
2973 //-----------------------------------------------------------------------------
2974 // name: ck_type_isequal()
2975 // desc: test if two types are equivalent
2976 //-----------------------------------------------------------------------------
2977 t_CKBOOL CK_DLL_CALL ck_type_isequal( Chuck_Type * lhs, Chuck_Type * rhs )
2978 {
2979 if( !lhs || !rhs ) return FALSE;
2980 return *lhs == *rhs;
2981 }
2982
2983
2984
2985
2986 //-----------------------------------------------------------------------------
2987 // name: ck_type_isa()
2988 // desc: test if lhs is a type of rhs
2989 //-----------------------------------------------------------------------------
2990 t_CKBOOL CK_DLL_CALL ck_type_isa( Chuck_Type * lhs, Chuck_Type * rhs )
2991 {
2992 if( !lhs || !rhs ) return FALSE;
2993 return ::isa( lhs, rhs );
2994 }
2995
2996
2997
2998
2999 //-----------------------------------------------------------------------------
3000 // name: ck_callback_on_instantiate()
3001 // desc: register a callback to be invoked whenever a base-type (or its subclass)
3002 // is instantiated, with option for type system to auto-set shred origin if available
3003 //-----------------------------------------------------------------------------
3004 void CK_DLL_CALL ck_callback_on_instantiate( f_callback_on_instantiate callback,
3005 Chuck_Type * base_type, Chuck_VM * vm, t_CKBOOL shouldSetShredOrigin )
3006 {
3007 // register the callback with chuck type
3008 base_type->add_instantiate_cb( callback, shouldSetShredOrigin );
3009 }
3010
3011
3012
3013
3014 //-----------------------------------------------------------------------------
3015 // name: ck_origin_hint()
3016 // desc: where did the type originate? e.g., chugin, builtin, user-defined, etc.
3017 //-----------------------------------------------------------------------------
3018 ckte_Origin CK_DLL_CALL ck_origin_hint( Chuck_Type * type )
3019 {
3020 // null
3021 if( !type ) return ckte_origin_UNKNOWN;
3022 // return origin hint
3023 return type->originHint;
3024 }
3025
3026
3027
3028
3029 //-----------------------------------------------------------------------------
3030 // name: ck_type_name()
3031 // desc: get type name (full, with decorations such as [])
3032 //-----------------------------------------------------------------------------
3033 const char * CK_DLL_CALL ck_type_name( Chuck_Type * type )
3034 {
3035 // null
3036 if( !type ) return "";
3037 // return type name
3038 return type->c_name();
3039 }
3040
3041
3042
3043
3044 //-----------------------------------------------------------------------------
3045 // name: ck_type_base_name()
3046 // desc: get type base name (no [] decorations)
3047 //-----------------------------------------------------------------------------
3048 const char * CK_DLL_CALL ck_type_base_name( Chuck_Type * type )
3049 {
3050 // null
3051 if( !type ) return "";
3052 // return origin hint
3053 return type->base_name.c_str();
3054 }
3055
3056
3057
3058
3059 //-----------------------------------------------------------------------------
3060 // name: ck_shred_parent()
3061 // desc: where did the type originate? e.g., chugin, builtin, user-defined, etc.
3062 //-----------------------------------------------------------------------------
3063 Chuck_VM_Shred * CK_DLL_CALL ck_shred_parent( Chuck_VM_Shred * shred )
3064 {
3065 // null
3066 if( !shred ) return NULL;
3067 // return origin hint
3068 return shred->parent;
3069 }
3070
3071
3072
3073
3074 //-----------------------------------------------------------------------------
3075 // windows translation
3076 //-----------------------------------------------------------------------------
3077 #if defined(__PLATFORM_WINDOWS__)
3078 #include <system_error> // std::system_category() | 1.5.1.5
3079 #include <algorithm> // std::replace() | 1.5.4.5
3080
3081 extern "C"
3082 {
3083
3084 #ifndef __CHUNREAL_ENGINE__
3085 #include <windows.h>
3086 #else
3087 // 1.5.0.0 (ge) | #chunreal
3088 // unreal engine on windows disallows including windows.h
3089 #include "Windows/MinWindows.h"
3090 #endif // #ifndef __CHUNREAL_ENGINE__
3091
3092 void * dlopen( const char * path, int mode )
3093 {
3094 // return
3095 void * retval = NULL;
3096 // copy into string
3097 std::string platformPath = path;
3098
3099 // add DLL search path | 1.5.4.0 (ge & nshaheed) added
3100 // (e.g., for chugins that have DLL dependencies)
3101 // replace '/' with '\\'
3102 std::replace( platformPath.begin(), platformPath.end(), '/', '\\' );
3103 // the dll search path to add
3104 string dll_path = extract_filepath_dir( platformPath );
3105 // if empty string, use current directory
3106 if( dll_path == "" ) dll_path = ".\\";
3107 // AddDllDirectory expects only fullpaths
3108 dll_path = normalize_filepath( dll_path, TRUE );
3109 // the relateive _deps directory
3110 string dll_deps_path = dll_path + "_deps\\";
3111 // convert to wchar
3112 wstring dll_pathw = wstring( dll_path.begin(), dll_path.end() );
3113 wstring dll_deps_pathw = wstring( dll_deps_path.begin(), dll_deps_path.end() );
3114 // add to the dll search path, for resolving the chugin's own DLL dependencies
3115 DLL_DIRECTORY_COOKIE cookie_path = AddDllDirectory( dll_pathw.c_str() );
3116 // add the relateive _deps directory to the search path as well
3117 DLL_DIRECTORY_COOKIE cookie_deps_path = AddDllDirectory( dll_deps_pathw.c_str() );
3118
3119 #ifndef __CHUNREAL_ENGINE__
3120 // 1.5.4.0 (ge) change from LoadLibrary to LoadLibraryEx to specific
3121 // LOAD_LIBRARY_SEARCH_DEFAULT_DIRS -- this is needed, apparently,
3122 // to take directories added by AddDllDirectory() into account, which
3123 // is needed to load DLL dependencies of chugins...
3124 // https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-adddlldirectory
3125 retval = (void *)LoadLibraryEx( platformPath.c_str(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
3126 #else
3127 // 1.5.0.0 (ge) | #chunreal; explicitly call ASCII version
3128 // the build envirnment seems to force UNICODE
3129 retval = (void *)LoadLibraryExA( platformPath.c_str(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
3130 #endif
3131
3132 // undo the AddDllDirectory()
3133 if( cookie_path ) RemoveDllDirectory( cookie_path );
3134 if( cookie_deps_path ) RemoveDllDirectory( cookie_deps_path );
3135
3136 // return
3137 return retval;
3138 }
3139
3140 int dlclose( void *handle )
3141 {
3142 FreeLibrary((HMODULE)handle);
3143 return 1;
3144 }
3145
3146 void *dlsym( void * handle, const char *symbol )
3147 {
3148 return (void *)GetProcAddress((HMODULE)handle, symbol);
3149 }
3150
3151 const char * dlerror( void )
3152 {
3153 // get windows error code
3154 int error = GetLastError();
3155 // no error
3156 if( error == 0 ) return NULL;
3157 // 1.5.1.5 (azaday) convert error code to system message
3158 std::string error_msg = std::system_category().message( error );
3159 // 1.4.2.0 (ge) changed to snprintf
3160 snprintf( dlerror_buffer, DLERROR_BUFFER_LENGTH, "(%i) %s", error, error_msg.c_str() );
3161 // return error buffer
3162 return dlerror_buffer;
3163 }
3164
3165 } // extern "C"
3166 #endif