1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
27 // desc: chuck dynamic linking header
29 // authors: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Ari Lazier (alazier@cs.princeton.edu)
32 // mac os code based on apple's open source
34 // date: spring 2004 - 1.1
36 //-----------------------------------------------------------------------------
37 #ifndef __CHUCK_DL_H__
38 #define __CHUCK_DL_H__
40 #include "chuck_def.h"
48 struct Chuck_DL_Query
;
49 struct Chuck_DL_Class
;
51 struct Chuck_DL_Value
;
53 union Chuck_DL_Return
;
57 struct Chuck_UAnaBlobProxy
;
60 // param conversion - to extract values from ARGS to functions
61 #define GET_CK_FLOAT(ptr) (*(t_CKFLOAT *)ptr)
62 #define GET_CK_SINGLE(ptr) (*(float *)ptr)
63 #define GET_CK_DOUBLE(ptr) (*(double *)ptr)
64 #define GET_CK_INT(ptr) (*(t_CKINT *)ptr)
65 #define GET_CK_UINT(ptr) (*(t_CKUINT *)ptr)
66 #define GET_CK_TIME(ptr) (*(t_CKTIME *)ptr)
67 #define GET_CK_DUR(ptr) (*(t_CKDUR *)ptr)
68 #define GET_CK_COMPLEX(ptr) (*(t_CKCOMPLEX *)ptr)
69 #define GET_CK_POLAR(ptr) (*(t_CKPOLAR *)ptr)
70 #define GET_CK_OBJECT(ptr) (*(Chuck_Object **)ptr)
71 #define GET_CK_STRING(ptr) (*(Chuck_String **)ptr)
73 // param conversion with pointer advance
74 #define GET_NEXT_FLOAT(ptr) (*((t_CKFLOAT *&)ptr)++)
75 #define GET_NEXT_SINGLE(ptr) (*((float *&)ptr)++)
76 #define GET_NEXT_DOUBLE(ptr) (*((double *&)ptr)++)
77 #define GET_NEXT_INT(ptr) (*((t_CKINT *&)ptr)++)
78 #define GET_NEXT_UINT(ptr) (*((t_CKUINT *&)ptr)++)
79 #define GET_NEXT_TIME(ptr) (*((t_CKTIME *&)ptr)++)
80 #define GET_NEXT_DUR(ptr) (*((t_CKDUR *&)ptr)++)
81 #define GET_NEXT_COMPLEX(ptr) (*((t_CKCOMPLEX *&)ptr)++)
82 #define GET_NEXT_POLAR(ptr) (*((t_CKPOLAR *&)ptr)++)
83 #define GET_NEXT_OBJECT(ptr) (*((Chuck_Object **&)ptr)++)
84 #define GET_NEXT_STRING(ptr) (*((Chuck_String **&)ptr)++)
87 #define SET_CK_FLOAT(ptr,v) (*(t_CKFLOAT *&)ptr=v)
88 #define SET_CK_SINGLE(ptr,v) (*(float *&)ptr=v)
89 #define SET_CK_DOUBLE(ptr,v) (*(double *&)ptr=v)
90 #define SET_CK_INT(ptr,v) (*(t_CKINT *&)ptr=v)
91 #define SET_CK_UINT(ptr,v) (*(t_CKUINT *&)ptr=v)
92 #define SET_CK_TIME(ptr,v) (*(t_CKTIME *&)ptr=v)
93 #define SET_CK_DUR(ptr,v) (*(t_CKDUR *&)ptr=v)
94 #define SET_CK_STRING(ptr,v) (*(Chuck_String **&)ptr=v)
96 // param conversion with pointer advance
97 #define SET_NEXT_FLOAT(ptr,v) (*((t_CKFLOAT *&)ptr)++=v)
98 #define SET_NEXT_SINGLE(ptr,v) (*((float *&)ptr)++=v)
99 #define SET_NEXT_DOUBLE(ptr,v) (*((double *&)ptr)++=v)
100 #define SET_NEXT_INT(ptr,v) (*((t_CKINT *&)ptr)++=v)
101 #define SET_NEXT_UINT(ptr,v) (*((t_CKUINT *&)ptr)++=v)
102 #define SET_NEXT_TIME(ptr,v) (*((t_CKTIME *&)ptr)++=v)
103 #define SET_NEXT_DUR(ptr,v) (*((t_CKDUR *&)ptr)++=v)
104 #define SET_NEXT_STRING(ptr,v) (*((Chuck_String **&)ptr)++=v)
106 // param conversion - to extract values from object's data segment
107 #define OBJ_MEMBER_DATA(obj,offset) (obj->data + offset)
108 #define OBJ_MEMBER_FLOAT(obj,offset) (*(t_CKFLOAT *)OBJ_MEMBER_DATA(obj,offset))
109 #define OBJ_MEMBER_SINGLE(obj,offset) (*(float *)OBJ_MEMBER_DATA(obj,offset))
110 #define OBJ_MEMBER_DOUBLE(obj,offset) (*(double *)OBJ_MEMBER_DATA(obj,offset))
111 #define OBJ_MEMBER_INT(obj,offset) (*(t_CKINT *)OBJ_MEMBER_DATA(obj,offset))
112 #define OBJ_MEMBER_UINT(obj,offset) (*(t_CKUINT *)OBJ_MEMBER_DATA(obj,offset))
113 #define OBJ_MEMBER_TIME(obj,offset) (*(t_CKTIME *)OBJ_MEMBER_DATA(obj,offset))
114 #define OBJ_MEMBER_DUR(obj,offset) (*(t_CKDUR *)OBJ_MEMBER_DATA(obj,offset))
115 #define OBJ_MEMBER_OBJECT(obj,offset) (*(Chuck_Object **)OBJ_MEMBER_DATA(obj,offset))
116 #define OBJ_MEMBER_STRING(obj,offset) (*(Chuck_String **)OBJ_MEMBER_DATA(obj,offset))
119 // chuck dll export linkage and calling convention
120 #if defined (__PLATFORM_WIN32__)
121 #define CK_DLL_LINKAGE extern "C" __declspec( dllexport )
123 #define CK_DLL_LINKAGE extern "C"
126 // calling convention of functions provided by chuck to the dll
127 #if defined(__WINDOWS_DS__)
128 #define CK_DLL_CALL _cdecl
133 // macro for defining ChucK DLL export functions
134 // example: CK_DLL_EXPORT(int) foo() { return 1; }
135 #define CK_DLL_EXPORT(type) CK_DLL_LINKAGE type CK_DLL_CALL
136 // macro for defining ChucK DLL export query-functions
137 // example: CK_DLL_QUERY
138 #define CK_DLL_QUERY CK_DLL_EXPORT(t_CKBOOL) ck_query( Chuck_DL_Query * QUERY )
139 // macro for defining ChucK DLL export constructors
140 // example: CK_DLL_CTOR(foo)
141 #define CK_DLL_CTOR(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_VM_Shred * SHRED )
142 // macro for defining ChucK DLL export destructors
143 // example: CK_DLL_DTOR(foo)
144 #define CK_DLL_DTOR(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, Chuck_VM_Shred * SHRED )
145 // macro for defining ChucK DLL export member functions
146 // example: CK_DLL_MFUN(foo)
147 #define CK_DLL_MFUN(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM_Shred * SHRED )
148 // macro for defining ChucK DLL export static functions
149 // example: CK_DLL_SFUN(foo)
150 #define CK_DLL_SFUN(name) CK_DLL_EXPORT(void) name( void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM_Shred * SHRED )
151 // macro for defining ChucK DLL export ugen tick functions
152 // example: CK_DLL_TICK(foo)
153 #define CK_DLL_TICK(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, SAMPLE in, SAMPLE * out, Chuck_VM_Shred * SHRED )
154 // macro for defining ChucK DLL export ugen ctrl functions
155 // example: CK_DLL_CTRL(foo)
156 #define CK_DLL_CTRL(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM_Shred * SHRED )
157 // macro for defining ChucK DLL export ugen cget functions
158 // example: CK_DLL_CGET(foo)
159 #define CK_DLL_CGET(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM_Shred * SHRED )
160 // macro for defining ChucK DLL export ugen pmsg functions
161 // example: CK_DLL_PMSG(foo)
162 #define CK_DLL_PMSG(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, const char * MSG, void * ARGS, Chuck_VM_Shred * SHRED )
163 // macro for defining ChucK DLL export uana tock functions
164 // example: CK_DLL_TOCK(foo)
165 #define CK_DLL_TOCK(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, Chuck_UAna * UANA, Chuck_UAnaBlobProxy * BLOB, Chuck_VM_Shred * SHRED )
168 // macros for DLL exports
169 // example: DLL_QUERY query( Chuck_DL_Query * QUERY )
170 // example: DLL_FUNC foo( void * ARGS, Chuck_DL_Return * RETURN )
171 // example: DLL_UGEN_F foo_tick( Chuck_UGen * SELF, SAMPLE, SAMPLE * out );
172 #define DLL_QUERY CK_DLL_EXPORT(t_CKBOOL)
173 #define DLL_FUNC CK_DLL_EXPORT(void)
174 #define UGEN_CTOR CK_DLL_EXPORT(void *)
175 #define UGEN_DTOR CK_DLL_EXPORT(void)
176 #define UGEN_TICK CK_DLL_EXPORT(t_CKBOOL)
177 #define UGEN_PMSG CK_DLL_EXPORT(t_CKBOOL)
178 #define UGEN_CTRL CK_DLL_EXPORT(t_CKVOID)
179 #define UGEN_CGET CK_DLL_EXPORT(t_CKVOID)
180 #define UANA_TOCK CK_DLL_EXPORT(t_CKBOOL)
183 //-----------------------------------------------------------------------------
184 // dynamic linking class interface prototypes
185 //-----------------------------------------------------------------------------
188 typedef t_CKBOOL (CK_DLL_CALL
* f_ck_query
)( Chuck_DL_Query
* QUERY
);
190 typedef t_CKVOID (CK_DLL_CALL
* f_ctor
)( Chuck_Object
* SELF
, void * ARGS
, Chuck_VM_Shred
* SHRED
);
191 typedef t_CKVOID (CK_DLL_CALL
* f_dtor
)( Chuck_Object
* SELF
, Chuck_VM_Shred
* SHRED
);
192 typedef t_CKVOID (CK_DLL_CALL
* f_mfun
)( Chuck_Object
* SELF
, void * ARGS
, Chuck_DL_Return
* RETURN
, Chuck_VM_Shred
* SHRED
);
193 typedef t_CKVOID (CK_DLL_CALL
* f_sfun
)( void * ARGS
, Chuck_DL_Return
* RETURN
, Chuck_VM_Shred
* SHRED
);
195 typedef t_CKBOOL (CK_DLL_CALL
* f_tick
)( Chuck_Object
* SELF
, SAMPLE in
, SAMPLE
* out
, Chuck_VM_Shred
* SHRED
);
196 typedef t_CKVOID (CK_DLL_CALL
* f_ctrl
)( Chuck_Object
* SELF
, void * ARGS
, Chuck_DL_Return
* RETURN
, Chuck_VM_Shred
* SHRED
);
197 typedef t_CKVOID (CK_DLL_CALL
* f_cget
)( Chuck_Object
* SELF
, void * ARGS
, Chuck_DL_Return
* RETURN
, Chuck_VM_Shred
* SHRED
);
198 typedef t_CKBOOL (CK_DLL_CALL
* f_pmsg
)( Chuck_Object
* SELF
, const char * MSG
, void * ARGS
, Chuck_VM_Shred
* SHRED
);
200 typedef t_CKBOOL (CK_DLL_CALL
* f_tock
)( Chuck_Object
* SELF
, Chuck_UAna
* UANA
, Chuck_UAnaBlobProxy
* BLOB
, Chuck_VM_Shred
* SHRED
);
204 // default name in DLL/ckx to look for
205 #define CK_QUERY_FUNC "ck_query"
206 // bad object data offset
207 #define CK_INVALID_OFFSET 0xffffffff
210 //-----------------------------------------------------------------------------
211 // chuck DLL query functions, implemented on chuck side for portability
212 //-----------------------------------------------------------------------------
215 typedef void ( CK_DLL_CALL
* f_setname
)( Chuck_DL_Query
* query
, const char * name
);
217 // begin class/namespace, can be nested
218 typedef void (CK_DLL_CALL
* f_begin_class
)( Chuck_DL_Query
* query
, const char * name
, const char * parent
);
219 // add constructor, must be betwen begin/end_class : use f_add_arg to add arguments immediately after
220 typedef void (CK_DLL_CALL
* f_add_ctor
)( Chuck_DL_Query
* query
, f_ctor ctor
);
221 // add destructor - cannot have args
222 typedef void (CK_DLL_CALL
* f_add_dtor
)( Chuck_DL_Query
* query
, f_dtor dtor
);
223 // add member function - args to follow
224 typedef void (CK_DLL_CALL
* f_add_mfun
)( Chuck_DL_Query
* query
, f_mfun mfun
, const char * type
, const char * name
);
225 // add static function - args to follow
226 typedef void (CK_DLL_CALL
* f_add_sfun
)( Chuck_DL_Query
* query
, f_sfun sfun
, const char * type
, const char * name
);
227 // add member variable
228 typedef void (CK_DLL_CALL
* f_add_mvar
)( Chuck_DL_Query
* query
,
229 const char * type
, const char * name
, t_CKBOOL is_const
); // TODO: public/protected/private
230 // add static variable
231 typedef void (CK_DLL_CALL
* f_add_svar
)( Chuck_DL_Query
* query
,
232 const char * type
, const char * name
,
233 t_CKBOOL is_const
, void * static_addr
); // TODO: public/protected/private
234 // add arg - follows ctor mfun sfun
235 typedef void (CK_DLL_CALL
* f_add_arg
)( Chuck_DL_Query
* query
, const char * type
, const char * name
);
236 // ** functions for adding unit generators, must extend ugen
237 typedef void (CK_DLL_CALL
* f_add_ugen_func
)( Chuck_DL_Query
* query
, f_tick tick
, f_pmsg pmsg
);
238 // ** add a ugen control
239 typedef void (CK_DLL_CALL
* f_add_ugen_ctrl
)( Chuck_DL_Query
* query
, f_ctrl ctrl
, f_cget cget
,
240 const char * type
, const char * name
);
241 // end class/namespace - must correspondent with begin_class. returns false on error
242 typedef t_CKBOOL (CK_DLL_CALL
* f_end_class
)( Chuck_DL_Query
* query
);
246 //-----------------------------------------------------------------------------
247 // name: struct Chuck_DL_Query
248 // desc: passed to module
249 //-----------------------------------------------------------------------------
250 struct Chuck_DL_Query
252 // function pointers - to be called from client module
253 // QUERY->setname( QUERY, ... );
255 // set the name of the module
257 // begin class/namespace, can be nexted
258 f_begin_class begin_class
;
259 // add constructor, can be followed by add_arg
261 // add destructor, no args allowed
263 // add member function, can be followed by add_arg
265 // add static function, can be followed by add_arg
267 // add member variable
269 // add static variable
271 // add argument to function
273 // (ugen only) add tick and pmsg functions
274 f_add_ugen_func add_ugen_func
;
275 // (ugen only) add ctrl parameters
276 f_add_ugen_ctrl add_ugen_ctrl
;
277 // end class/namespace, compile it
278 f_end_class end_class
;
283 Chuck_DL_Class
* curr_class
;
285 Chuck_DL_Func
* curr_func
;
286 // collection of class
287 std::vector
<Chuck_DL_Class
*> classes
;
289 std::vector
<Chuck_DL_Class
* >stack
;
292 std::string dll_name
;
307 ~Chuck_DL_Query() { this->clear(); }
313 //-----------------------------------------------------------------------------
314 // name: struct Chuck_DL_Class
315 // desc: class built from module
316 //-----------------------------------------------------------------------------
317 struct Chuck_DL_Class
319 // the name of the class
321 // the name of the parent
324 std::vector
<Chuck_DL_Func
*> ctors
;
326 Chuck_DL_Func
* dtor
;
328 std::vector
<Chuck_DL_Func
*> mfuns
;
330 std::vector
<Chuck_DL_Func
*> sfuns
;
332 std::vector
<Chuck_DL_Value
*> mvars
;
334 std::vector
<Chuck_DL_Value
*> svars
;
340 std::vector
<Chuck_DL_Ctrl
*> ugen_ctrl
;
343 // collection of recursive classes
344 std::vector
<Chuck_DL_Class
*> classes
;
347 Chuck_DL_Class() { dtor
= NULL
; ugen_tick
= NULL
; ugen_pmsg
= NULL
; uana_tock
= NULL
; ugen_pmsg
= NULL
; }
353 //-----------------------------------------------------------------------------
354 // name: struct Chuck_DL_Value
355 // desc: value from module
356 //-----------------------------------------------------------------------------
357 struct Chuck_DL_Value
359 // the name of the value
361 // the type of the value
369 Chuck_DL_Value() { is_const
= FALSE
; static_addr
= NULL
; }
370 Chuck_DL_Value( const char * t
, const char * n
, t_CKBOOL c
= FALSE
, void * a
= NULL
)
371 { name
= n
; type
= t
; is_const
= c
; static_addr
= a
; }
375 //-----------------------------------------------------------------------------
376 // name: struct Chuck_DL_Func
377 // desc: function from module
378 //-----------------------------------------------------------------------------
381 // the name of the function
386 union { f_ctor ctor
; f_dtor dtor
; f_mfun mfun
; f_sfun sfun
; t_CKUINT addr
; };
388 std::vector
<Chuck_DL_Value
*> args
;
391 Chuck_DL_Func() { ctor
= NULL
; }
392 Chuck_DL_Func( const char * t
, const char * n
, t_CKUINT a
)
393 { name
= n
; type
= t
; addr
= a
; }
397 void add_arg( const char * t
, const char * n
)
398 { args
.push_back( new Chuck_DL_Value( t
, n
) ); }
402 //-----------------------------------------------------------------------------
403 // name: struct Chuck_DL_Ctrl
404 // desc: ctrl for ugen
405 //-----------------------------------------------------------------------------
408 // the name of the ctrl
412 // the types of the value
413 std::vector
<std::string
> types
;
421 //------------------------------------------------------------------------------
422 // alternative functions to make stuff
423 //------------------------------------------------------------------------------
424 Chuck_DL_Func
* make_new_mfun( const char * t
, const char * n
, f_mfun mfun
);
425 Chuck_DL_Func
* make_new_sfun( const char * t
, const char * n
, f_sfun sfun
);
426 Chuck_DL_Value
* make_new_arg( const char * t
, const char * n
);
427 Chuck_DL_Value
* make_new_mvar( const char * t
, const char * n
, t_CKBOOL c
= FALSE
);
428 Chuck_DL_Value
* make_new_svar( const char * t
, const char * n
, t_CKBOOL c
, void * a
);
431 //------------------------------------------------------------------------------
432 // name: union Chuck_DL_Return
433 // desc: dynamic link return function return struct
434 //------------------------------------------------------------------------------
435 union Chuck_DL_Return
442 t_CKCOMPLEX v_complex
;
444 Chuck_Object
* v_object
;
445 Chuck_String
* v_string
;
447 Chuck_DL_Return() { v_complex
.re
= 0.0; v_complex
.im
= 0.0; }
451 //-----------------------------------------------------------------------------
452 // name: struct Chuck_DLL
453 // desc: dynamic link library
454 //-----------------------------------------------------------------------------
455 struct Chuck_DLL
: public Chuck_VM_Object
458 // load dynamic ckx/dll from filename
459 t_CKBOOL
load( const char * filename
,
460 const char * func
= CK_QUERY_FUNC
,
461 t_CKBOOL lazy
= FALSE
);
462 t_CKBOOL
load( f_ck_query query_func
, t_CKBOOL lazy
= FALSE
);
463 // get address in loaded ckx
464 void * get_addr( const char * symbol
);
466 const char * last_error() const;
469 // query the content of the dll
470 const Chuck_DL_Query
* query( );
472 t_CKBOOL
good() const;
474 const char * name() const;
478 Chuck_DLL( const char * xid
= NULL
)
479 : m_handle(NULL
), m_id(xid
? xid
: ""),
480 m_done_query(FALSE
), m_query_func(NULL
)
483 ~Chuck_DLL() { this->unload(); }
488 std::string m_last_error
;
489 std::string m_filename
;
492 t_CKBOOL m_done_query
;
494 f_ck_query m_query_func
;
495 Chuck_DL_Query m_query
;
502 #if defined(__MACOSX_CORE__)
503 #include <AvailabilityMacros.h>
506 // dlfcn interface, panther or below
507 #if defined(__MACOSX_CORE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1030
513 void * dlopen( const char * path
, int mode
);
514 void * dlsym( void * handle
, const char * symbol
);
515 const char * dlerror( void );
516 int dlclose( void * handle
);
518 #define RTLD_LAZY 0x1
520 #define RTLD_LOCAL 0x4
521 #define RTLD_GLOBAL 0x8
522 #define RTLD_NOLOAD 0x10
523 #define RTLD_SHARED 0x20 /* not used, the default */
524 #define RTLD_UNSHARED 0x40
525 #define RTLD_NODELETE 0x80
526 #define RTLD_LAZY_UNDEF 0x100
532 #elif defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)
538 #define RTLD_LAZY 0x1
540 #define RTLD_LOCAL 0x4
541 #define RTLD_GLOBAL 0x8
542 #define RTLD_NOLOAD 0x10
543 #define RTLD_SHARED 0x20 /* not used, the default */
544 #define RTLD_UNSHARED 0x40
545 #define RTLD_NODELETE 0x80
546 #define RTLD_LAZY_UNDEF 0x100
548 void * dlopen( const char * path
, int mode
);
549 void * dlsym( void * handle
, const char * symbol
);
550 const char * dlerror( void );
551 int dlclose( void * handle
);
552 static char dlerror_buffer
[128];