Debian Codegraph @

src/core/chuck_dl.h

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.h
28 // desc: chuck dynamic linking header
29 //
30 // NOTE: this file is shared by two perspectives: 1) chuck hosts, as well
31 // as 2) chugins -- determined by the __CHUCK_CHUGIN__ macro:
32 // __CHUCK_CHUGIN__ is NOT defined: assume chuck host perspective
33 // __CHUCK_CHUGIN__ is defined: assume chugin perspective
34 //
35 // (ALL chugins must define __CHUCK_CHUGIN__
36 // conversely, NO host should define __CHUCK_CHUGIN__)
37 //
38 // authors: Ge Wang ([email protected] | [email protected])
39 // Ari Lazier ([email protected])
40 // Spencer Salazar ([email protected])
41 // macOS code based on apple's open source
42 //
43 // date: spring 2004 - 1.1 - internal modules interface
44 // spring 2005 - 1.2
45 // ...
46 // circa 2013 - support for chugins
47 // ...
48 // summer 2023 - chugins DL api v9
49 // fall 2023 - transition to single-header `chugin.h` | 1.5.2.0
50 // fall 2023 - chugins DL api v10
51 //-----------------------------------------------------------------------------
52 #ifndef __CHUCK_DL_H__
53 #define __CHUCK_DL_H__
54
55 #include "chuck_def.h"
56 #include "chuck_absyn.h"
57 #include <string>
58 #include <vector>
59 #include <map>
60
61
62 // major API version: significant semantic and/or API update
63 // major API version must be the same between chuck:chugin
64 #define CK_DLL_VERSION_MAJOR (10)
65 // minor API version: revisions
66 // minor API version of chuck must >= API version of chugin
67 #define CK_DLL_VERSION_MINOR (2)
68 #define CK_DLL_VERSION_MAKE(maj,min) ((t_CKUINT)(((maj) << 16) | (min)))
69 #define CK_DLL_VERSION_GETMAJOR(v) (((v) >> 16) & 0xFFFF)
70 #define CK_DLL_VERSION_GETMINOR(v) ((v) & 0xFFFF)
71 #define CK_DLL_VERSION (CK_DLL_VERSION_MAKE(CK_DLL_VERSION_MAJOR, CK_DLL_VERSION_MINOR))
72
73 // string literal containing default chugin paths (platform-specific)
74 extern char g_default_path_system[];
75 extern char g_default_path_packages[];
76 extern char g_default_path_user[];
77 // string literal containing environment variable names for import search path
78 extern char g_envvar_path_system[];
79 extern char g_envvar_path_packages[];
80 extern char g_envvar_path_user[];
81 // contains deprecated env var name
82 extern char g_envvar_path_deprecated[];
83
84
85 // DL forward references
86 struct Chuck_DL_Query;
87 struct Chuck_DL_Class;
88 struct Chuck_DL_Func;
89 struct Chuck_DL_Value;
90 struct Chuck_DL_Ctrl;
91 union Chuck_DL_Return;
92 struct Chuck_DL_MainThreadHook;
93 struct Chuck_DLL;
94 struct Chuck_DL_Api;
95 // un-namespaced Chuck_DL_Api | 1.5.1.5 (ge)
96 // namespace Chuck_DL_Api { struct Api; }
97
98 // object forward references
99 struct Chuck_VM;
100 struct Chuck_VM_Object;
101 struct Chuck_VM_Shred;
102 struct Chuck_Env;
103 struct Chuck_Compiler;
104 struct Chuck_Carrier;
105 struct Chuck_Object;
106 struct Chuck_Event;
107 struct Chuck_String;
108 struct Chuck_ArrayInt;
109 struct Chuck_ArrayFloat;
110 struct Chuck_ArrayVec2;
111 struct Chuck_ArrayVec3;
112 struct Chuck_ArrayVec4;
113 struct Chuck_UGen;
114 struct Chuck_UAna;
115 struct Chuck_UAnaBlobProxy;
116
117 // utility for references
118 class CBufferSimple;
119
120
121 // param conversion - to extract values from ARGS to functions
122 #define GET_CK_FLOAT(ptr) (*(t_CKFLOAT *)ptr)
123 #define GET_CK_SINGLE(ptr) (*(float *)ptr)
124 #define GET_CK_DOUBLE(ptr) (*(double *)ptr)
125 #define GET_CK_INT(ptr) (*(t_CKINT *)ptr)
126 #define GET_CK_UINT(ptr) (*(t_CKUINT *)ptr)
127 #define GET_CK_TIME(ptr) (*(t_CKTIME *)ptr)
128 #define GET_CK_DUR(ptr) (*(t_CKDUR *)ptr)
129 #define GET_CK_COMPLEX(ptr) (*(t_CKCOMPLEX *)ptr)
130 #define GET_CK_POLAR(ptr) (*(t_CKPOLAR *)ptr)
131 #define GET_CK_VEC2(ptr) (*(t_CKVEC2 *)ptr)
132 #define GET_CK_VEC3(ptr) (*(t_CKVEC3 *)ptr)
133 #define GET_CK_VEC4(ptr) (*(t_CKVEC4 *)ptr)
134 #define GET_CK_VECTOR(ptr) (*(t_CKVECTOR *)ptr)
135 #define GET_CK_OBJECT(ptr) (*(Chuck_Object **)ptr)
136 #define GET_CK_STRING(ptr) (*(Chuck_String **)ptr)
137
138 // param conversion with pointer advance
139 #define GET_NEXT_FLOAT(ptr) (*((t_CKFLOAT *&)ptr)++)
140 #define GET_NEXT_SINGLE(ptr) (*((float *&)ptr)++)
141 #define GET_NEXT_DOUBLE(ptr) (*((double *&)ptr)++)
142 #define GET_NEXT_INT(ptr) (*((t_CKINT *&)ptr)++)
143 #define GET_NEXT_UINT(ptr) (*((t_CKUINT *&)ptr)++)
144 #define GET_NEXT_TIME(ptr) (*((t_CKTIME *&)ptr)++)
145 #define GET_NEXT_DUR(ptr) (*((t_CKDUR *&)ptr)++)
146 #define GET_NEXT_COMPLEX(ptr) (*((t_CKCOMPLEX *&)ptr)++)
147 #define GET_NEXT_POLAR(ptr) (*((t_CKPOLAR *&)ptr)++)
148 #define GET_NEXT_VEC2(ptr) (*((t_CKVEC2 *&)ptr)++)
149 #define GET_NEXT_VEC3(ptr) (*((t_CKVEC3 *&)ptr)++)
150 #define GET_NEXT_VEC4(ptr) (*((t_CKVEC4 *&)ptr)++)
151 #define GET_NEXT_VECTOR(ptr) (*((t_CKVECTOR *&)ptr)++)
152 #define GET_NEXT_OBJECT(ptr) (*((Chuck_Object **&)ptr)++)
153 #define GET_NEXT_STRING(ptr) (*((Chuck_String **&)ptr)++)
154
155 // string-specific operations
156 #ifndef __CHUCK_CHUGIN__ // CHUGIN flag NOT present
157 // assume macro used from host
158 #define GET_CK_STRING_SAFE(ptr) std::string( GET_CK_STRING(ptr)->c_str() )
159 #define GET_NEXT_STRING_SAFE(ptr) std::string( GET_NEXT_STRING(ptr)->c_str() )
160 #else // CHUGIN flag is present
161 // assume macro used from chugin; use chugins runtime API for portability
162 #define GET_CK_STRING_SAFE(ptr) std::string( API->object->str((Chuck_String *)ptr) )
163 #define GET_NEXT_STRING_SAFE(ptr) std::string( API->object->str(GET_NEXT_STRING(ptr) ) )
164 #endif
165
166 // param conversion
167 #define SET_CK_FLOAT(ptr,v) (*(t_CKFLOAT *&)ptr=v)
168 #define SET_CK_SINGLE(ptr,v) (*(float *&)ptr=v)
169 #define SET_CK_DOUBLE(ptr,v) (*(double *&)ptr=v)
170 #define SET_CK_INT(ptr,v) (*(t_CKINT *&)ptr=v)
171 #define SET_CK_UINT(ptr,v) (*(t_CKUINT *&)ptr=v)
172 #define SET_CK_TIME(ptr,v) (*(t_CKTIME *&)ptr=v)
173 #define SET_CK_DUR(ptr,v) (*(t_CKDUR *&)ptr=v)
174 #define SET_CK_VEC2(ptr,v) (*(t_CKVEC2 *&)ptr=v)
175 #define SET_CK_VEC3(ptr,v) (*(t_CKVEC3 *&)ptr=v)
176 #define SET_CK_VEC4(ptr,v) (*(t_CKVEC4 *&)ptr=v)
177 #define SET_CK_VECTOR(ptr,v) (*(t_CKVECTOR *&)ptr=v)
178 #define SET_CK_STRING(ptr,v) (*(Chuck_String **&)ptr=v)
179
180 // param conversion with pointer advance
181 #define SET_NEXT_FLOAT(ptr,v) (*((t_CKFLOAT *&)ptr)++=v)
182 #define SET_NEXT_SINGLE(ptr,v) (*((float *&)ptr)++=v)
183 #define SET_NEXT_DOUBLE(ptr,v) (*((double *&)ptr)++=v)
184 #define SET_NEXT_INT(ptr,v) (*((t_CKINT *&)ptr)++=v)
185 #define SET_NEXT_UINT(ptr,v) (*((t_CKUINT *&)ptr)++=v)
186 #define SET_NEXT_TIME(ptr,v) (*((t_CKTIME *&)ptr)++=v)
187 #define SET_NEXT_DUR(ptr,v) (*((t_CKDUR *&)ptr)++=v)
188 #define SET_NEXT_VEC2(ptr,v) (*((t_CKVEC2 *&)ptr)++=v)
189 #define SET_NEXT_VEC3(ptr,v) (*((t_CKVEC3 *&)ptr)++=v)
190 #define SET_NEXT_VEC4(ptr,v) (*((t_CKVEC4 *&)ptr)++=v)
191 #define SET_NEXT_VECTOR(ptr,v) (*((t_CKVECTOR *&)ptr)++=v)
192 #define SET_NEXT_STRING(ptr,v) (*((Chuck_String **&)ptr)++=v)
193
194 // param conversion - to access values from an Object's data segment
195 //-----------------------------------------------------------------------------
196 // take action depending on where the macro is used, and as determined by the
197 // presence or absence of the __CHUCK_CHUGIN__ preprocessor macro:
198 // -- 1) macro NOT present: assume macro is used from within a chuck host
199 // (CLI chuck, miniAudicle, or any c++ system that integrates the ChucK system
200 // (compiler, VM, synthesis engine), then directly access obj->data
201 // -- 2) macro is present: assume macro is used from within a chugin;
202 // access Object data using the chugin runtime DL API
203 //-----------------------------------------------------------------------------
204 #ifndef __CHUCK_CHUGIN__ // CHUGIN flag NOT present
205 // assume macro used from chuck host
206 #define OBJ_MEMBER_DATA(obj,offset) (obj->data + offset)
207 #else // CHUGIN flag is present
208 // assume macro used from chugin
209 #define OBJ_MEMBER_DATA(obj,offset) (API->object->data(obj,offset))
210 #endif // #ifndef __CHUCK_CHUGIN__
211 //-----------------------------------------------------------------------------
212 // param conversion - to extract values from object's data segment
213 #define OBJ_MEMBER_FLOAT(obj,offset) (*(t_CKFLOAT *)OBJ_MEMBER_DATA(obj,offset))
214 #define OBJ_MEMBER_SINGLE(obj,offset) (*(float *)OBJ_MEMBER_DATA(obj,offset))
215 #define OBJ_MEMBER_DOUBLE(obj,offset) (*(double *)OBJ_MEMBER_DATA(obj,offset))
216 #define OBJ_MEMBER_INT(obj,offset) (*(t_CKINT *)OBJ_MEMBER_DATA(obj,offset))
217 #define OBJ_MEMBER_UINT(obj,offset) (*(t_CKUINT *)OBJ_MEMBER_DATA(obj,offset))
218 #define OBJ_MEMBER_TIME(obj,offset) (*(t_CKTIME *)OBJ_MEMBER_DATA(obj,offset))
219 #define OBJ_MEMBER_DUR(obj,offset) (*(t_CKDUR *)OBJ_MEMBER_DATA(obj,offset))
220 #define OBJ_MEMBER_VEC2(obj,offset) (*(t_CKVEC2 *)OBJ_MEMBER_DATA(obj,offset))
221 #define OBJ_MEMBER_VEC3(obj,offset) (*(t_CKVEC3 *)OBJ_MEMBER_DATA(obj,offset))
222 #define OBJ_MEMBER_VEC4(obj,offset) (*(t_CKVEC4 *)OBJ_MEMBER_DATA(obj,offset))
223 #define OBJ_MEMBER_VECTOR(obj,offset) (*(t_CKVECTOR *)OBJ_MEMBER_DATA(obj,offset))
224 #define OBJ_MEMBER_OBJECT(obj,offset) (*(Chuck_Object **)OBJ_MEMBER_DATA(obj,offset))
225 #define OBJ_MEMBER_STRING(obj,offset) (*(Chuck_String **)OBJ_MEMBER_DATA(obj,offset))
226
227
228 // chuck dll export linkage and calling convention
229 #if defined (__PLATFORM_WINDOWS__)
230 #define CK_DLL_LINKAGE extern "C" __declspec( dllexport )
231 #else
232 #define CK_DLL_LINKAGE extern "C"
233 #endif
234
235 // calling convention of functions provided by chuck to the dll
236 #if defined(__PLATFORM_WINDOWS__)
237 #define CK_DLL_CALL _cdecl
238 #else
239 #define CK_DLL_CALL
240 #endif
241
242 // DL api pointer
243 typedef const Chuck_DL_Api * CK_DL_API;
244
245 // macro for defining ChucK DLL export functions
246 // example: CK_DLL_EXPORT(int) foo() { return 1; }
247 #define CK_DLL_EXPORT(type) CK_DLL_LINKAGE type CK_DLL_CALL
248 // macro for declaring version of ChucK DL a given DLL links to
249 // example: CK_DLL_DECLVERSION
250 #define CK_DLL_DECLVERSION CK_DLL_EXPORT(t_CKUINT) ck_version() { return CK_DLL_VERSION; }
251 // macro for defining a DLL info func
252 #define CK_DLL_INFO(name) CK_DLL_EXPORT(void) ck_info( Chuck_DL_Query * QUERY )
253 // naming convention for static query functions
254 #define CK_DLL_QUERY_STATIC_NAME(name) ck_##name##_query
255 // macro for defining ChucK DLL export query-functions (static version)
256 #define CK_DLL_QUERY_STATIC(name) CK_DLL_EXPORT(t_CKBOOL) CK_DLL_QUERY_STATIC_NAME(name)( Chuck_DL_Query * QUERY )
257 // macro for defining ChucK DLL export query-functions | example: CK_DLL_QUERY(Foo) { ... }
258 #ifndef __CK_DLL_STATIC__
259 #define CK_DLL_QUERY(name) CK_DLL_DECLVERSION CK_DLL_EXPORT(t_CKBOOL) ck_query( Chuck_DL_Query * QUERY )
260 #else
261 #define CK_DLL_QUERY(name) CK_DLL_QUERY_STATIC(name)
262 #endif // __CK_DLL_STATIC__
263 // macro for defining ChucK DLL export allocator
264 // example: CK_DLL_ALLOC(foo)
265 #define CK_DLL_ALLOC(name) CK_DLL_EXPORT(Chuck_Object *) name( Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
266 // macro for defining ChucK DLL export constructors
267 // example: CK_DLL_CTOR(foo)
268 #define CK_DLL_CTOR(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
269 // macro for defining ChucK DLL export destructors
270 // example: CK_DLL_DTOR(foo)
271 #define CK_DLL_DTOR(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
272 // macro for defining ChucK DLL export member functions
273 // example: CK_DLL_MFUN(foo)
274 #define CK_DLL_MFUN(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
275 // macro for defining ChucK DLL export static functions
276 // example: CK_DLL_SFUN(foo) | 1.4.1.0 (ge) added TYPE to static prototype
277 #define CK_DLL_SFUN(name) CK_DLL_EXPORT(void) name( Chuck_Type * TYPE, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
278 // example: CK_DLL_GFUN(foo) | 1.5.1.5 (ge & andrew) added for global-scope function, e.g., for op overloads
279 #define CK_DLL_GFUN(name) CK_DLL_EXPORT(void) name( void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
280 // macro for defining ChucK DLL export ugen tick functions
281 // example: CK_DLL_TICK(foo)
282 #define CK_DLL_TICK(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, SAMPLE in, SAMPLE * out, CK_DL_API API )
283 // macro for defining ChucK DLL export ugen multi-channel tick functions
284 // example: CK_DLL_TICKF(foo)
285 #define CK_DLL_TICKF(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, SAMPLE * in, SAMPLE * out, t_CKUINT nframes, CK_DL_API API )
286 // macro for defining ChucK DLL export ugen ctrl functions
287 // example: CK_DLL_CTRL(foo)
288 #define CK_DLL_CTRL(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
289 // macro for defining ChucK DLL export ugen cget functions
290 // example: CK_DLL_CGET(foo)
291 #define CK_DLL_CGET(name) CK_DLL_EXPORT(void) name( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
292 // macro for defining ChucK DLL export ugen pmsg functions
293 // example: CK_DLL_PMSG(foo)
294 #define CK_DLL_PMSG(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, const char * MSG, void * ARGS, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API )
295 // macro for defining ChucK DLL export uana tock functions
296 // example: CK_DLL_TOCK(foo)
297 #define CK_DLL_TOCK(name) CK_DLL_EXPORT(t_CKBOOL) name( Chuck_Object * SELF, Chuck_UAna * UANA, Chuck_UAnaBlobProxy * BLOB, CK_DL_API API )
298 // macro for defining Chuck DLL export shreds watcher functions
299 // example: CK_DLL_SHREDS_WATCHER(foo)
300 #define CK_DLL_SHREDS_WATCHER(name) CK_DLL_EXPORT(void) name( Chuck_VM_Shred * SHRED, t_CKINT CODE, t_CKINT PARAM, Chuck_VM * VM, void * BINDLE )
301 // macro for defining Chuck DLL export type on instantiate functions
302 // example: CK_DLL_TYPE_ON_INSTANTIATE(foo)
303 #define CK_DLL_TYPE_ON_INSTANTIATE(name) CK_DLL_EXPORT(void) name( Chuck_Object * OBJECT, Chuck_Type * TYPE, Chuck_VM_Shred * SHRED, Chuck_VM * VM )
304
305 // macros for DLL exports
306 // example: DLL_QUERY query( Chuck_DL_Query * QUERY )
307 // example: DLL_FUNC foo( void * ARGS, Chuck_DL_Return * RETURN )
308 // example: DLL_UGEN_F foo_tick( Chuck_UGen * SELF, SAMPLE, SAMPLE * out );
309 #define DLL_QUERY CK_DLL_EXPORT(t_CKBOOL)
310 #define DLL_FUNC CK_DLL_EXPORT(void)
311 #define UGEN_CTOR CK_DLL_EXPORT(void *)
312 #define UGEN_DTOR CK_DLL_EXPORT(void)
313 #define UGEN_TICK CK_DLL_EXPORT(t_CKBOOL)
314 #define UGEN_PMSG CK_DLL_EXPORT(t_CKBOOL)
315 #define UGEN_CTRL CK_DLL_EXPORT(t_CKVOID)
316 #define UGEN_CGET CK_DLL_EXPORT(t_CKVOID)
317 #define UANA_TOCK CK_DLL_EXPORT(t_CKBOOL)
318
319
320 //-----------------------------------------------------------------------------
321 // dynamic linking class interface prototypes
322 //-----------------------------------------------------------------------------
323 extern "C" {
324 // query
325 typedef t_CKUINT (CK_DLL_CALL * f_ck_declversion)();
326 typedef t_CKVOID (CK_DLL_CALL * f_ck_info)( Chuck_DL_Query * QUERY );
327 typedef t_CKBOOL (CK_DLL_CALL * f_ck_query)( Chuck_DL_Query * QUERY );
328 // object
329 typedef Chuck_Object * (CK_DLL_CALL * f_alloc)( Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
330 typedef t_CKVOID (CK_DLL_CALL * f_ctor)( Chuck_Object * SELF, void * ARGS, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
331 typedef t_CKVOID (CK_DLL_CALL * f_dtor)( Chuck_Object * SELF, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
332 typedef t_CKVOID (CK_DLL_CALL * f_mfun)( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
333 // 1.4.1.0 (ge) added TYPE to static prototype
334 typedef t_CKVOID (CK_DLL_CALL * f_sfun)( Chuck_Type * TYPE, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
335 // 1.5.1.5 (ge & andrew) added for global-scope function, e.g., for op overloads
336 typedef t_CKVOID (CK_DLL_CALL * f_gfun)( void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
337 // ugen specific
338 typedef t_CKBOOL (CK_DLL_CALL * f_tick)( Chuck_Object * SELF, SAMPLE in, SAMPLE * out, CK_DL_API API );
339 typedef t_CKBOOL (CK_DLL_CALL * f_tickf)( Chuck_Object * SELF, SAMPLE * in, SAMPLE * out, t_CKUINT nframes, CK_DL_API API );
340 typedef t_CKVOID (CK_DLL_CALL * f_ctrl)( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
341 typedef t_CKVOID (CK_DLL_CALL * f_cget)( Chuck_Object * SELF, void * ARGS, Chuck_DL_Return * RETURN, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
342 typedef t_CKBOOL (CK_DLL_CALL * f_pmsg)( Chuck_Object * SELF, const char * MSG, void * ARGS, Chuck_VM * VM, Chuck_VM_Shred * SHRED, CK_DL_API API );
343 // uana specific
344 typedef t_CKBOOL (CK_DLL_CALL * f_tock)( Chuck_Object * SELF, Chuck_UAna * UANA, Chuck_UAnaBlobProxy * BLOB, CK_DL_API API );
345 // "main thread" hook
346 typedef t_CKBOOL (CK_DLL_CALL * f_mainthreadhook)( void * bindle );
347 // "main thread" quit (stop running hook)
348 typedef t_CKBOOL (CK_DLL_CALL * f_mainthreadquit)( void * bindle );
349 // callback function, called on host shutdown
350 typedef void (CK_DLL_CALL * f_callback_on_shutdown)( void * bindle );
351 // sample rate update callback | 1.5.4.2 (ge) added
352 typedef void (CK_DLL_CALL * f_callback_on_srate_update)( t_CKUINT srate, void * bindle );
353 // shreds watcher callback
354 typedef void (CK_DLL_CALL * f_shreds_watcher)( Chuck_VM_Shred * SHRED, t_CKINT CODE, t_CKINT PARAM, Chuck_VM * VM, void * BINDLE );
355 // type instantiation callback
356 typedef void (CK_DLL_CALL * f_callback_on_instantiate)( Chuck_Object * OBJECT, Chuck_Type * TYPE, Chuck_VM_Shred * originShred, Chuck_VM * VM );
357 }
358
359
360 // default name in DLL/ckx to look for host/chugin compatibility version func
361 #define CK_DECLVERSION_FUNC "ck_version"
362 // default name in DLL/ckx to look for info func
363 #define CK_INFO_FUNC "ck_info"
364 // default name in DLL/ckx to look for for query func
365 #define CK_QUERY_FUNC "ck_query"
366 // bad object data offset
367 #define CK_INVALID_OFFSET 0xffffffff
368
369
370 //-----------------------------------------------------------------------------
371 // chuck DLL query functions, implemented on chuck side for portability
372 //-----------------------------------------------------------------------------
373 extern "C" {
374 // get handles: opaque references to VM, API etc.
375 typedef Chuck_VM * (CK_DLL_CALL * f_get_vm)( Chuck_DL_Query * query );
376 typedef CK_DL_API (CK_DLL_CALL * f_get_api)( Chuck_DL_Query * query );
377 typedef Chuck_Env * (CK_DLL_CALL * f_get_env)( Chuck_DL_Query * query );
378 typedef Chuck_Compiler * (CK_DLL_CALL * f_get_compiler)( Chuck_DL_Query * query );
379 typedef Chuck_Carrier * (CK_DLL_CALL * f_get_carrier)( Chuck_DL_Query * query );
380
381 // set name of module / chugin
382 typedef void (CK_DLL_CALL * f_setname)( Chuck_DL_Query * query, const char * name );
383 // set info of module / chugin by (key,value)
384 typedef void (CK_DLL_CALL * f_setinfo)( Chuck_DL_Query * query, const char * key, const char * value );
385 // set info of module / chugin by (key,value)
386 typedef const char * (CK_DLL_CALL * f_getinfo)( Chuck_DL_Query * query, const char * key );
387
388 // begin class/namespace, can be nested
389 typedef void (CK_DLL_CALL * f_begin_class)( Chuck_DL_Query * query, const char * name, const char * parent );
390 // add constructor, must be betwen begin/end_class : use f_add_arg to add arguments immediately after
391 typedef void (CK_DLL_CALL * f_add_ctor)( Chuck_DL_Query * query, f_ctor ctor );
392 // add destructor - cannot have args
393 typedef void (CK_DLL_CALL * f_add_dtor)( Chuck_DL_Query * query, f_dtor dtor );
394 // add member function - args to follow
395 typedef void (CK_DLL_CALL * f_add_mfun)( Chuck_DL_Query * query, f_mfun mfun, const char * type, const char * name );
396 // add static function - args to follow
397 typedef void (CK_DLL_CALL * f_add_sfun)( Chuck_DL_Query * query, f_sfun sfun, const char * type, const char * name );
398 // add binary operator overload - args included
399 typedef void (CK_DLL_CALL * f_add_op_overload_binary)( Chuck_DL_Query * query, f_gfun gfun, const char * type, const char * op,
400 const char * lhsType, const char * lhsName, const char * rhsType, const char * rhsName );
401 // add unary (prefix) operator overload - arg included
402 typedef void (CK_DLL_CALL * f_add_op_overload_prefix)( Chuck_DL_Query * query, f_gfun gfun, const char * type, const char * op,
403 const char * argType, const char * argName );
404 // add unary (postfix) operator overload - args included
405 typedef void (CK_DLL_CALL * f_add_op_overload_postfix)( Chuck_DL_Query * query, f_gfun gfun, const char * type, const char * op,
406 const char * argType, const char * argName );
407 // add member variable
408 typedef t_CKUINT (CK_DLL_CALL * f_add_mvar)( Chuck_DL_Query * query,
409 const char * type, const char * name, t_CKBOOL is_const ); // TODO: public/protected/private
410 // add static variable
411 typedef void (CK_DLL_CALL * f_add_svar)( Chuck_DL_Query * query,
412 const char * type, const char * name,
413 t_CKBOOL is_const, void * static_addr ); // TODO: public/protected/private
414 // add arg - follows ctor mfun sfun
415 typedef void (CK_DLL_CALL * f_add_arg)( Chuck_DL_Query * query, const char * type, const char * name );
416 // ** functions for adding unit generators, must extend ugen
417 typedef void (CK_DLL_CALL * f_add_ugen_func)( Chuck_DL_Query * query, f_tick tick, f_pmsg pmsg, t_CKUINT num_in, t_CKUINT num_out );
418 typedef void (CK_DLL_CALL * f_add_ugen_funcf)( Chuck_DL_Query * query, f_tickf tickf, f_pmsg pmsg, t_CKUINT num_in, t_CKUINT num_out );
419 typedef void (CK_DLL_CALL * f_add_ugen_funcf_auto_num_channels)( Chuck_DL_Query * query, f_tickf tickf, f_pmsg psmg );
420 // ** add a ugen control (not used) | 1.4.1.0 removed
421 //typedef void (CK_DLL_CALL * f_add_ugen_ctrl)( Chuck_DL_Query * query, f_ctrl ctrl, f_cget cget,
422 // const char * type, const char * name );
423 // end class/namespace - must correspondent with begin_class. returns false on error
424 typedef t_CKBOOL (CK_DLL_CALL * f_end_class)( Chuck_DL_Query * query );
425 // create main thread hook- used for executing a "hook" function in the main thread of a primary chuck instance
426 typedef Chuck_DL_MainThreadHook * (CK_DLL_CALL * f_create_main_thread_hook)( Chuck_DL_Query * query, f_mainthreadhook hook, f_mainthreadquit quit, void * bindle );
427 // register a callback to be called on host shutdown, e.g., for chugin cleanup
428 typedef void (CK_DLL_CALL * f_register_callback_on_shutdown)( Chuck_DL_Query * query, f_callback_on_shutdown cb, void * bindle );
429 // register a callback to be called on sample rate change
430 typedef void (CK_DLL_CALL * f_register_callback_on_srate_update)( Chuck_DL_Query * query, f_callback_on_srate_update cb, void * bindle );
431 // register a callback function to receive notification from the VM about shreds (add, remove, etc.)
432 typedef void (CK_DLL_CALL * f_register_shreds_watcher)( Chuck_DL_Query * query, f_shreds_watcher cb, t_CKUINT options, void * bindle );
433 // unregister a shreds notification callback
434 typedef void (CK_DLL_CALL * f_unregister_shreds_watcher)( Chuck_DL_Query * query, f_shreds_watcher cb );
435
436 // documentation
437 // set current class documentation
438 typedef t_CKBOOL (CK_DLL_CALL * f_doc_class)( Chuck_DL_Query * query, const char * doc );
439 // add example of current class
440 typedef t_CKBOOL (CK_DLL_CALL * f_add_example)( Chuck_DL_Query * query, const char * ex );
441 // set current function documentation
442 typedef t_CKBOOL (CK_DLL_CALL * f_doc_func)( Chuck_DL_Query * query, const char * doc );
443 // set last mvar documentation
444 typedef t_CKBOOL (CK_DLL_CALL * f_doc_var)( Chuck_DL_Query * query, const char * doc );
445
446 } // end extern "C"
447
448
449
450
451 //-----------------------------------------------------------------------------
452 // name: enum ckvm_ShredsWatcherFlag | 1.5.1.5 (ge & andrew) added
453 // desc: shreds watcher flags; meant to bitwise-OR together in options
454 // these will also be passed back to the callback...
455 //-----------------------------------------------------------------------------
456 typedef enum {
457 ckvm_shreds_watch_NONE = 0,
458 ckvm_shreds_watch_SPORK = 1,
459 ckvm_shreds_watch_REMOVE = 2,
460 ckvm_shreds_watch_SUSPEND = 4,
461 ckvm_shreds_watch_ACTIVATE = 8,
462 ckvm_shreds_watch_ALL = 0x7fffffff
463 } ckvm_ShredsWatcherFlag;
464
465 //-----------------------------------------------------------------------------
466 // name: enum ckte_Origin | 1.5.0.0 (ge) added
467 // desc: where something (e.g., a Type) originates
468 //-----------------------------------------------------------------------------
469 typedef enum {
470 ckte_origin_UNKNOWN = 0,
471 ckte_origin_BUILTIN, // in core
472 ckte_origin_CHUGIN, // in imported chugin
473 ckte_origin_IMPORT, // library CK code
474 ckte_origin_USERDEFINED, // in user chuck code
475 ckte_origin_GENERATED // generated (e.g., array types like int[][][][])
476 } ckte_Origin;
477
478 //-----------------------------------------------------------------------------
479 // name: enum ckte_Op_OverloadKind | 1.5.1.5 (ge) added
480 // desc: enumeration for kinds of operator overload
481 //-----------------------------------------------------------------------------
482 typedef enum
483 {
484 ckte_op_overload_NONE,
485 ckte_op_overload_BINARY, // LHS op RHS
486 ckte_op_overload_UNARY_PRE, // op RHS
487 ckte_op_overload_UNARY_POST // LHS op
488 } ckte_Op_OverloadKind;
489
490 //-----------------------------------------------------------------------------
491 // key strings for QUERY->setname(...) and QUERY->getinfo(...)
492 //-----------------------------------------------------------------------------
493 #define CHUGIN_INFO_NAME "CHUGIN_INFO_NAME" // same as QUERY->setname()
494 #define CHUGIN_INFO_AUTHORS "CHUGIN_INFO_AUTHORS"
495 #define CHUGIN_INFO_CHUGIN_VERSION "CHUGIN_INFO_CHUGIN_VERSION"
496 #define CHUGIN_INFO_DESCRIPTION "CHUGIN_INFO_DESCRIPTION"
497 #define CHUGIN_INFO_URL "CHUGIN_INFO_URL"
498 #define CHUGIN_INFO_EMAIL "CHUGIN_INFO_EMAIL"
499 #define CHUGIN_INFO_ID "CHUGIN_INFO_ID"
500 // enclosing directory (without the filename) | 1.5.4.5 (ge)
501 #define CHUGIN_INFO_INSTALL_DIR "CHUGIN_INFO_INSTALL_DIR"
502 // filename (without the enclosing directory) | 1.5.4.5 (ge)
503 #define CHUGIN_INFO_INSTALL_FILE "CHUGIN_INFO_INSTALL_FILE"
504 // reserved
505 #define CHUGIN_INFO_EXTRA "CHUGIN_INFO_EXTRA"
506
507
508
509
510 //-----------------------------------------------------------------------------
511 // name: struct Chuck_DL_Query
512 // desc: data structure passed from chuck host to chugin to query its contents:
513 // include 1) chugin/module info; 2) classes and operator-overloadings
514 // to add to chuck's type system; 3) the chugin can also use the query
515 // to get data from from the host, including VM / host sample rate, and
516 // a Chuck_DL_Api handle for accessing the chugin-to-host runtime DL API
517 //-----------------------------------------------------------------------------
518 struct Chuck_DL_Query
519 {
520 //-------------------------------------------------------------------------
521 // function pointers: to be called from CHUGIN / module
522 //-------------------------------------------------------------------------
523 public:
524 // -------------
525 // functions for accessing useful host-side objects
526 // -------------
527 // get Chuck_DL_Api for accessing chugin DL runtime API
528 // CK_DL_API api = QUERY->ck_api( QUERY );
529 f_get_api ck_api;
530 // get a handle to host's VM
531 // Chuck_VM * vm = QUERY->ck_vm( QUERY );
532 f_get_vm ck_vm;
533
534 public:
535 // -------------
536 // functions for registering chugin info such as name, author, URL, etc.
537 // -------------
538 // set the name of the module, typically the name of the Chugin
539 // QUERY->setname( QUERY, "TheChuginName" );
540 f_setname setname;
541 // set additional info by key | 1.5.2.0 (ge) added
542 // see CHUGIN_INFO_* above for info keys
543 // QUERY->setinfo( QUERY, key_cstring, value_cstring );
544 f_setinfo setinfo;
545 // get info by key (do not keep returned pointer; always make a copy):
546 // see CHUGIN_INFO_* above for info keys
547 // std::string str = QUERY->setinfo( QUERY, key_cstring, value_cstring );
548 f_getinfo getinfo;
549
550 public:
551 // -------------
552 // functions for creating new types in chuck's type system, from a chugin
553 // -------------
554 // begin class/namespace, can be nested
555 f_begin_class begin_class;
556 // add constructor, can be followed by add_arg
557 f_add_ctor add_ctor;
558 // add destructor, no args allowed
559 f_add_dtor add_dtor;
560 // add member function, can be followed by add_arg
561 f_add_mfun add_mfun;
562 // add static function, can be followed by add_arg
563 f_add_sfun add_sfun;
564 // add member variable
565 f_add_mvar add_mvar;
566 // add static variable
567 f_add_svar add_svar;
568 // add argument to function
569 f_add_arg add_arg;
570 // (ugen only) add tick and pmsg functions
571 f_add_ugen_func add_ugen_func;
572 // (ugen only) add tick and pmsg functions
573 f_add_ugen_funcf add_ugen_funcf;
574 // (ugen only) add tick and pmsg functions, specify channels by vm
575 f_add_ugen_funcf_auto_num_channels add_ugen_funcf_auto_num_channels;
576 // (ugen only) add ctrl parameters
577 // f_add_ugen_ctrl add_ugen_ctrl; // not used but needed for import for now
578 // end class/namespace; compile it
579 f_end_class end_class;
580
581 public:
582 // -------------
583 // functions for overloading operators in the type system, from a chugin
584 // -------------
585 // add binary operator overload; args included | 1.5.1.5 (ge & andrew)
586 f_add_op_overload_binary add_op_overload_binary;
587 // add unary (prefix) operator overload; arg included
588 f_add_op_overload_prefix add_op_overload_prefix;
589 // add unary (postfix) operator overload; arg included
590 f_add_op_overload_postfix add_op_overload_postfix;
591
592 public:
593 // -------------
594 // these are used to document functions and variables added above
595 // the are used for ckdoc generations and .help runtime help
596 // -------------
597 f_doc_class doc_class;
598 f_doc_func doc_func;
599 f_doc_var doc_var;
600 f_add_example add_ex;
601
602 public:
603 // -------------
604 // register a function to be run on the main thread of chuck host
605 // * no more than ONE main thread hooks can be active
606 // * typically used by special chugins such as ChuGL or MAUI that
607 // deals with graphics or windowing | re-added 1.4.0.1
608 // -------------
609 f_create_main_thread_hook create_main_thread_hook;
610
611 public:
612 // -------------
613 // register a function to be run on host shutdown; this can be used
614 // for chugin cleanup when the host (chuck, miniAudicle, etc.) exits
615 // including on SIGINT (ctrl-c termination) | added 1.5.2.5 (ge)
616 // -------------
617 f_register_callback_on_shutdown register_callback_on_shutdown;
618
619 public:
620 // -------------
621 // register callback to be invoked by chuck host at various
622 // stages of a shred's operation | 1.5.1.5 (ge & andrew) added
623 // * see `ckvm_ShredsWatcherFlag` enums
624 // -------------
625 // register shred notifcations
626 f_register_shreds_watcher register_shreds_watcher;
627 // un-register shred notifcations
628 f_unregister_shreds_watcher unregister_shreds_watcher;
629
630 public:
631 // -------------
632 // register callback to be invoked by chuck host on
633 // sample rate changes | 1.5.4.2 (ge) added
634 // -------------
635 f_register_callback_on_srate_update register_callback_on_srate_update;
636
637
638
639
640 //-------------------------------------------------------------------------
641 // HOST ONLY beyond this point...
642 //-------------------------------------------------------------------------
643 public:
644 //-------------------------------------------------------------------------
645 // NOTE: everything below std::anything cannot be reliably accessed by
646 // offset across DLL/shared-library boundaries, since std::anything could
647 // be variable size; *** chugins should never access anything below ***
648 //-------------------------------------------------------------------------
649 // *** put everything to be accessed from chugins ABOVE this point! ***
650 //-------------------------------------------------------------------------
651 // * discovered by the vigilant and forever traumatized Jack Atherton,
652 // * fixed during REFACTOR-2017; warning by guilt-ridden Ge Wang
653 //-------------------------------------------------------------------------
654 // DLL reference
655 Chuck_DLL * dll_ref;
656 // name of dll
657 std::string dll_name;
658 // DL API reference | 1.5.1.5
659 CK_DL_API m_api;
660 // info map | 1.5.2.0
661 std::map<std::string, std::string> dll_info;
662
663 // current class
664 Chuck_DL_Class * curr_class;
665 // current function
666 Chuck_DL_Func * curr_func;
667 // current variable | added 1.3.5.0
668 Chuck_DL_Value * curr_var;
669 // collection of classes
670 std::vector<Chuck_DL_Class *> classes;
671 // stack of classes
672 std::vector<Chuck_DL_Class * > stack;
673 // collection of operator overloads
674 std::vector<Chuck_DL_Func *> op_overloads;
675
676 public: // host-side functions (not to be called from chugins)
677 // constructor
678 Chuck_DL_Query( Chuck_Carrier * the_carrier, Chuck_DLL * dll = NULL );
679 // desctructor
680 ~Chuck_DL_Query() { this->clear(); }
681 // clear
682 void clear();
683
684 public:
685 // access to various functions: called from host
686 Chuck_VM * vm() const;
687 CK_DL_API api() const;
688 Chuck_Env * env() const;
689 Chuck_Compiler * compiler() const;
690 Chuck_Carrier * carrier() const;
691
692 public:
693 // get sample rate; to be called by host only; (chugins use API->vm->srate)
694 t_CKUINT srate();
695 // flag any error encountered during the query | 1.5.0.5 (ge) added
696 t_CKBOOL errorEncountered;
697
698 protected:
699 // REFACTOR-2017: carrier ref
700 Chuck_Carrier * m_carrier;
701 // host sample rate
702 t_CKUINT m_srate;
703 };
704
705
706
707
708 //-----------------------------------------------------------------------------
709 // name: struct Chuck_DL_Class
710 // desc: class built from module
711 //-----------------------------------------------------------------------------
712 struct Chuck_DL_Class
713 {
714 // the name of the class
715 std::string name;
716 // the name of the parent
717 std::string parent;
718 // ctor
719 std::vector<Chuck_DL_Func *> ctors;
720 // dtor
721 Chuck_DL_Func * dtor;
722 // mfun
723 std::vector<Chuck_DL_Func *> mfuns;
724 // sfun
725 std::vector<Chuck_DL_Func *> sfuns;
726 // mdata
727 std::vector<Chuck_DL_Value *> mvars;
728 // sdata
729 std::vector<Chuck_DL_Value *> svars;
730 // ugen_tick
731 f_tick ugen_tick;
732 // ugen_tickf
733 f_tickf ugen_tickf;
734 // ugen_pmsg
735 f_pmsg ugen_pmsg;
736 // ugen_ctrl/cget
737 std::vector<Chuck_DL_Ctrl *> ugen_ctrl;
738 // uana_tock
739 f_tock uana_tock;
740 // collection of recursive classes
741 std::vector<Chuck_DL_Class *> classes;
742 // current mvar offset
743 t_CKUINT current_mvar_offset;
744 // # of ugen input and output channels
745 t_CKUINT ugen_num_in, ugen_num_out;
746 // ckdoc: class description
747 std::string doc;
748 // ckdoc: examples
749 std::vector<std::string> examples;
750 // origin string (e.g., filepath if coming from chugin DLL)
751 std::string hint_dll_filepath;
752
753 // constructor
754 Chuck_DL_Class() { dtor = NULL; ugen_tick = NULL; ugen_tickf = NULL; ugen_pmsg = NULL; uana_tock = NULL; ugen_pmsg = NULL; current_mvar_offset = 0; ugen_num_in = ugen_num_out = 0; }
755 // destructor
756 ~Chuck_DL_Class();
757 };
758
759
760
761
762 //-----------------------------------------------------------------------------
763 // name: struct Chuck_DL_Value
764 // desc: value from module
765 //-----------------------------------------------------------------------------
766 struct Chuck_DL_Value
767 {
768 // the name of the value
769 std::string name;
770 // the type of the value
771 std::string type;
772 // is const
773 t_CKBOOL is_const;
774 // addr static
775 void * static_addr;
776 // description
777 std::string doc;
778
779 // constructor
780 Chuck_DL_Value() { is_const = FALSE; static_addr = NULL; }
781 Chuck_DL_Value( const char * t, const char * n, t_CKBOOL c = FALSE, void * a = NULL )
782 { name = n; type = t; is_const = c; static_addr = a; }
783 };
784
785
786
787
788 //-----------------------------------------------------------------------------
789 // name: struct Chuck_DL_Func
790 // desc: function from module
791 //-----------------------------------------------------------------------------
792 struct Chuck_DL_Func
793 {
794 // the name of the function
795 std::string name;
796 // the return type
797 std::string type;
798 // the function pointer
799 union { f_ctor ctor; f_dtor dtor; f_mfun mfun; f_sfun sfun; f_gfun gfun; t_CKUINT addr; };
800 // the kind of the function pointer
801 ae_FuncPointerKind fpKind;
802 // arguments
803 std::vector<Chuck_DL_Value *> args;
804 // description
805 std::string doc;
806 // is this an operator overload? if so, which kind? | 1.5.1.5
807 ckte_Op_OverloadKind opOverloadKind;
808 // operator to overload | 1.5.1.5
809 ae_Operator op2overload;
810
811 // constructor
812 Chuck_DL_Func() { ctor = NULL; fpKind = ae_fp_unknown; opOverloadKind = ckte_op_overload_NONE; op2overload = ae_op_none; }
813 Chuck_DL_Func( const char * t, const char * n, t_CKUINT a, ae_FuncPointerKind kind )
814 { name = n?n:""; type = t?t:""; addr = a; fpKind = kind; opOverloadKind = ckte_op_overload_NONE; op2overload = ae_op_none; }
815 // destructor
816 ~Chuck_DL_Func();
817 // add arg
818 void add_arg( const char * t, const char * n )
819 { args.push_back( new Chuck_DL_Value( t, n ) ); }
820 };
821
822
823
824
825 //-----------------------------------------------------------------------------
826 // name: struct Chuck_DL_Ctrl
827 // desc: ctrl for ugen
828 //-----------------------------------------------------------------------------
829 struct Chuck_DL_Ctrl
830 {
831 // the name of the ctrl
832 std::string name;
833 // the first type
834 std::string type;
835 // the types of the value
836 std::vector<std::string> types;
837 // ctrl
838 f_ctrl ctrl;
839 // cget
840 f_cget cget;
841 };
842
843
844
845
846 //------------------------------------------------------------------------------
847 // name: union Chuck_DL_Return
848 // desc: dynamic link return function return struct
849 //------------------------------------------------------------------------------
850 union Chuck_DL_Return
851 {
852 t_CKINT v_int;
853 t_CKUINT v_uint;
854 t_CKFLOAT v_float;
855 t_CKDUR v_dur;
856 t_CKTIME v_time;
857 t_CKCOMPLEX v_complex;
858 t_CKPOLAR v_polar;
859 t_CKVEC2 v_vec2; // ge: added 1.5.1.7
860 t_CKVEC3 v_vec3; // ge: added 1.3.5.3
861 t_CKVEC4 v_vec4; // ge: added 1.3.5.3
862 Chuck_Object * v_object;
863 Chuck_String * v_string;
864
865 Chuck_DL_Return() { v_vec4.x = v_vec4.y = v_vec4.z = v_vec4.w = 0; }
866 };
867
868
869
870
871 //------------------------------------------------------------------------------
872 // name: struct Chuck_DL_Arg
873 // desc: import / dynamic link function argument | 1.5.1.5
874 //------------------------------------------------------------------------------
875 struct Chuck_DL_Arg
876 {
877 // which kind of data (e.g., int and object * are both kinds of ints)
878 te_KindOf kind;
879 // the data in a union; re-using DL_Return for this
880 Chuck_DL_Return value;
881
882 // constructor
883 Chuck_DL_Arg() { kind = kindof_VOID; }
884 // size in bytes
885 t_CKUINT sizeInBytes()
886 {
887 // check data kind
888 switch( kind )
889 {
890 case kindof_INT: return sz_INT;
891 case kindof_FLOAT: return sz_FLOAT;
892 case kindof_VEC2: return sz_VEC2;
893 case kindof_VEC3: return sz_VEC3;
894 case kindof_VEC4: return sz_VEC4;
895 case kindof_VOID: return sz_VOID;
896 }
897 // unhandled
898 return 0;
899 }
900 };
901
902
903
904
905 //------------------------------------------------------------------------------
906 // alternative functions to make stuff`
907 //------------------------------------------------------------------------------
908 Chuck_DL_Func * make_new_ctor( f_ctor ctor );
909 Chuck_DL_Func * make_new_mfun( const char * t, const char * n, f_mfun mfun );
910 Chuck_DL_Func * make_new_sfun( const char * t, const char * n, f_sfun sfun );
911 Chuck_DL_Func * make_new_op_binary( const char * t, ae_Operator op, f_gfun gfun );
912 Chuck_DL_Func * make_new_op_prefix( const char * t, ae_Operator op, f_gfun gfun );
913 Chuck_DL_Func * make_new_op_postfix( const char * t, ae_Operator op, f_gfun gfun );
914 Chuck_DL_Value * make_new_arg( const char * t, const char * n );
915 Chuck_DL_Value * make_new_mvar( const char * t, const char * n, t_CKBOOL c = FALSE );
916 Chuck_DL_Value * make_new_svar( const char * t, const char * n, t_CKBOOL c, void * a );
917
918
919
920
921 //------------------------------------------------------------------------------
922 // name: struct Chuck_DL_MainThreadHook
923 // desc: mechanism for chugins to provide a hook to run code on host main thread
924 //------------------------------------------------------------------------------
925 struct Chuck_DL_MainThreadHook
926 {
927 public:
928 Chuck_DL_MainThreadHook(f_mainthreadhook hook, f_mainthreadquit quit,
929 void * bindle, Chuck_Carrier * carrier);
930 t_CKBOOL (CK_DLL_CALL * const activate)(Chuck_DL_MainThreadHook *);
931 t_CKBOOL (CK_DLL_CALL * const deactivate)(Chuck_DL_MainThreadHook *);
932
933 public:
934 Chuck_Carrier * const m_carrier;
935 f_mainthreadhook const m_hook;
936 f_mainthreadquit const m_quit;
937 void * const m_bindle;
938 t_CKBOOL m_active;
939 };
940
941
942
943
944 //-----------------------------------------------------------------------------
945 // instantiating a chuck string
946 //-----------------------------------------------------------------------------
947 Chuck_String * CK_DLL_CALL ck_create_string( Chuck_VM * vm, const char * cstr, t_CKBOOL addRef );
948 //-----------------------------------------------------------------------------
949 // invoking chuck functions from c++
950 //-----------------------------------------------------------------------------
951 // directly invoke a chuck member function's native implementation from c++
952 // using object + vtable offset | 1.5.1.5 (ge & andrew)
953 // NOTE this will call the member function in IMMEDIATE MODE,
954 // marking it as a time-critical function when called in this manner;
955 // any time/event operations therein will throw an exception
956 Chuck_DL_Return CK_DLL_CALL ck_invoke_mfun_immediate_mode( Chuck_Object * obj, t_CKUINT func_vt_offset,
957 Chuck_VM * vm, Chuck_VM_Shred * shred,
958 Chuck_DL_Arg * ARGS, t_CKUINT numArgs );
959
960
961
962
963 //-----------------------------------------------------------------------------
964 // dynamic linking callable API to ChucK's innards
965 //-----------------------------------------------------------------------------
966 struct Chuck_DL_Api
967 {
968 typedef Chuck_Object * Object;
969 typedef Chuck_Type * Type;
970 typedef Chuck_String * String;
971 typedef Chuck_ArrayInt * ArrayInt; // 1.5.0.1 (ge) added
972 typedef Chuck_ArrayFloat * ArrayFloat; // 1.5.1.8 (nshaheed) added
973 typedef Chuck_ArrayVec2 * ArrayVec2; // 1.5.2.0 (ge) added
974 typedef Chuck_ArrayVec3 * ArrayVec3; // 1.5.2.0 (ge) added
975 typedef Chuck_ArrayVec4 * ArrayVec4; // 1.5.2.0 (ge) added
976
977 public:
978 static Chuck_DL_Api g_api;
979 static inline const Chuck_DL_Api * instance() { return &g_api; }
980
981 // api to access host-side ChucK virtual machine
982 struct VMApi
983 {
984 VMApi();
985 // get sample rate | 1.5.1.5
986 t_CKUINT (CK_DLL_CALL * const srate)( Chuck_VM * vm );
987 // get chuck now | 1.5.1.5
988 t_CKTIME (CK_DLL_CALL * const now)( Chuck_VM * vm );
989 // create a new lock-free one-producer, one-consumer buffer | 1.5.1.5
990 CBufferSimple * (CK_DLL_CALL * const create_event_buffer)( Chuck_VM * vm );
991 // queue an event; num_msg must be 1; buffer should be created using create_event_buffer() above | 1.5.1.5
992 t_CKBOOL (CK_DLL_CALL * const queue_event)( Chuck_VM * vm, Chuck_Event * event, t_CKINT num_msg, CBufferSimple * buffer );
993 // invoke Chuck_Object member function (defined either in chuck or c++) | 1.5.1.5 (ge & andrew)
994 // NOTE this will call the member function in IMMEDIATE MODE,
995 // marking it as a time-critical function when called in this manner;
996 // any time/event operations therein will throw an exception
997 Chuck_DL_Return (CK_DLL_CALL * const invoke_mfun_immediate_mode)( Chuck_Object * obj, t_CKUINT func_vt_offset,
998 Chuck_VM * vm, Chuck_VM_Shred * shred, Chuck_DL_Arg * ARGS, t_CKUINT numArgs );
999 // throw an exception; if shred is passed in, it will be halted
1000 void (CK_DLL_CALL * const throw_exception)( const char * exception, const char * desc, Chuck_VM_Shred * shred );
1001 // log a message in the chuck logging system
1002 void (CK_DLL_CALL * const em_log)( t_CKINT level, const char * text );
1003 // system function: remove all shreds in VM; use with care
1004 void (CK_DLL_CALL * const remove_all_shreds)( Chuck_VM * vm );
1005 } * const vm;
1006
1007 // api to access host-side ChucK objects
1008 struct ObjectApi
1009 {
1010 ObjectApi();
1011
1012 // 1.5.0.0 (nshaheed and ge and anonymous pr-lab member) | changed from private to public
1013 // also changed all std::string & in this section to const char *
1014 // intent: this allows for chugins to access member variables and create chuck strings
1015 public:
1016 // function pointer get_type()
1017 Type (CK_DLL_CALL * const get_type)( Object object );
1018 // add reference count
1019 void (CK_DLL_CALL * const add_ref)( Object object );
1020 // release reference count
1021 void (CK_DLL_CALL * const release)( Object object );
1022 // get reference count
1023 t_CKUINT (CK_DLL_CALL * const refcount)( Object object );
1024 // instantiating and initializing a ChucK object by type
1025 // if addRef == TRUE the newly created object will have a reference count of 1; otherwise 0
1026 // NOTE set addRef to TRUE if you intend to keep a reference of the newly created object around (e.g., in the chugin)
1027 // NOTE set addRef to FALSE if the created object is to be returned without keeping a reference around
1028 Object (CK_DLL_CALL * const create)( Chuck_VM_Shred *, Type type, t_CKBOOL addRef );
1029 // instantiating and initializing a ChucK object by type, using a VM instead of a shred
1030 Object (CK_DLL_CALL * const create_without_shred)( Chuck_VM *, Type type, t_CKBOOL addRef );
1031 // instantiate and initialize a ChucK string by type (without ref to a parent shred)
1032 String (CK_DLL_CALL * const create_string)( Chuck_VM *, const char * value, t_CKBOOL addRef );
1033 // compute pointer to data segment + offset; use for member variable access | 1.5.2.0
1034 void * (CK_DLL_CALL * const data)( Object object, t_CKUINT byteOffset );
1035 // get the origin shred
1036 Chuck_VM_Shred * (CK_DLL_CALL * const get_origin_shred)( Object object );
1037 // set the origin shred; this should only be invoked by system-level chugins; use with care
1038 void (CK_DLL_CALL * const set_origin_shred)( Object object, Chuck_VM_Shred * shred );
1039 // function pointers for get_mvar_*()
1040 t_CKBOOL (CK_DLL_CALL * const get_mvar_int)( Object object, const char * name, t_CKINT & value );
1041 t_CKBOOL (CK_DLL_CALL * const get_mvar_float)( Object object, const char * name, t_CKFLOAT & value );
1042 t_CKBOOL (CK_DLL_CALL * const get_mvar_dur)( Object object, const char * name, t_CKDUR & value );
1043 t_CKBOOL (CK_DLL_CALL * const get_mvar_time)( Object object, const char * name, t_CKTIME & value );
1044 t_CKBOOL (CK_DLL_CALL * const get_mvar_vec2)( Object object, const char * name, t_CKVEC2 & value ); // 1.5.2.0 | vec2/complex/polar
1045 t_CKBOOL (CK_DLL_CALL * const get_mvar_vec3)( Object object, const char * name, t_CKVEC3 & value ); // 1.5.2.0 | vec3
1046 t_CKBOOL (CK_DLL_CALL * const get_mvar_vec4)( Object object, const char * name, t_CKVEC4 & value ); // 1.5.2.0 | vec4
1047 t_CKBOOL (CK_DLL_CALL * const get_mvar_string)( Object object, const char * name, String & value );
1048 t_CKBOOL (CK_DLL_CALL * const get_mvar_object)( Object object, const char * name, Object & value );
1049 // function pointer for set_string()
1050 t_CKBOOL (CK_DLL_CALL * const set_string)( String string, const char * value );
1051 // get a chuck string's internal c-string (NOTE do not save a reference to the return value; make a copy if needed) | 1.5.2.0
1052 const char * (CK_DLL_CALL * const str)( Chuck_String * str );
1053 // array_int operations
1054 t_CKINT (CK_DLL_CALL * const array_int_size)( ArrayInt array );
1055 t_CKINT (CK_DLL_CALL * const array_int_get_idx)( ArrayInt array, t_CKINT idx );
1056 void (CK_DLL_CALL *const array_int_set_idx)( ArrayInt array, t_CKINT idx, t_CKINT value );
1057 t_CKBOOL (CK_DLL_CALL * const array_int_get_key)( ArrayInt array, const char * key, t_CKINT & value );
1058 void (CK_DLL_CALL *const array_int_set_key)( ArrayInt array, const char *key, t_CKINT value );
1059 t_CKBOOL (CK_DLL_CALL * const array_int_push_back)( ArrayInt array, t_CKINT value );
1060 void (CK_DLL_CALL * const array_int_clear)( ArrayInt array );
1061 // array_float operations
1062 t_CKINT (CK_DLL_CALL * const array_float_size)( ArrayFloat array );
1063 t_CKFLOAT (CK_DLL_CALL * const array_float_get_idx)( ArrayFloat array, t_CKINT idx );
1064 void (CK_DLL_CALL *const array_float_set_idx)( ArrayFloat array, t_CKINT idx, t_CKFLOAT value );
1065 t_CKBOOL (CK_DLL_CALL * const array_float_get_key)( ArrayFloat array, const char * key, t_CKFLOAT & value );
1066 void (CK_DLL_CALL *const array_float_set_key)( ArrayFloat array, const char *key, t_CKFLOAT value );
1067 t_CKBOOL (CK_DLL_CALL * const array_float_push_back)( ArrayFloat array, t_CKFLOAT value );
1068 void (CK_DLL_CALL * const array_float_clear)(ArrayFloat array);
1069 // array_vec2/complex/polar/16 operations | 1.5.2.0 (ge) added
1070 t_CKINT (CK_DLL_CALL * const array_vec2_size)( ArrayVec2 array );
1071 t_CKVEC2 (CK_DLL_CALL * const array_vec2_get_idx)( ArrayVec2 array, t_CKINT idx );
1072 void (CK_DLL_CALL *const array_vec2_set_idx)( ArrayVec2 array, t_CKINT idx, t_CKVEC2 value );
1073 t_CKBOOL (CK_DLL_CALL * const array_vec2_get_key)( ArrayVec2 array, const char * key, t_CKVEC2 & value );
1074 void (CK_DLL_CALL *const array_vec2_set_key)( ArrayVec2 array, const char *key, t_CKVEC2 value );
1075 t_CKBOOL (CK_DLL_CALL * const array_vec2_push_back)( ArrayVec2 array, const t_CKVEC2 & value );
1076 void (CK_DLL_CALL * const array_vec2_clear)(ArrayVec2 array);
1077 // array_vec3/24 operations | 1.5.2.0 (ge) added
1078 t_CKINT (CK_DLL_CALL * const array_vec3_size)( ArrayVec3 array );
1079 t_CKVEC3 (CK_DLL_CALL * const array_vec3_get_idx)( ArrayVec3 array, t_CKINT idx );
1080 void (CK_DLL_CALL *const array_vec3_set_idx)( ArrayVec3 array, t_CKINT idx, t_CKVEC3 value );
1081 t_CKBOOL (CK_DLL_CALL * const array_vec3_get_key)( ArrayVec3 array, const char * key, t_CKVEC3 & value );
1082 void (CK_DLL_CALL *const array_vec3_set_key)( ArrayVec3 array, const char *key, t_CKVEC3 value );
1083 t_CKBOOL (CK_DLL_CALL * const array_vec3_push_back)( ArrayVec3 array, const t_CKVEC3 & value );
1084 void (CK_DLL_CALL * const array_vec3_clear)(ArrayVec3 array);
1085 // array_vec4/32 operations | 1.5.2.0 (ge) added
1086 t_CKINT (CK_DLL_CALL * const array_vec4_size)( ArrayVec4 array );
1087 t_CKVEC4 (CK_DLL_CALL * const array_vec4_get_idx)( ArrayVec4 array, t_CKINT idx );
1088 void (CK_DLL_CALL *const array_vec4_set_idx)( ArrayVec4 array, t_CKINT idx, t_CKVEC4 value );
1089 t_CKBOOL (CK_DLL_CALL * const array_vec4_get_key)( ArrayVec4 array, const char * key, t_CKVEC4 & value );
1090 void (CK_DLL_CALL *const array_vec4_set_key)( ArrayVec4 array, const char *key, t_CKVEC4 value );
1091 t_CKBOOL (CK_DLL_CALL * const array_vec4_push_back)( ArrayVec4 array, const t_CKVEC4 & value );
1092 void (CK_DLL_CALL * const array_vec4_clear)(ArrayVec4 array);
1093 // (UNSAFE) get c++ vector pointers from chuck arrays | 1.5.2.0
1094 // std::vector<t_CKUINT> * (CK_DLL_CALL * const array_int_vector)( ArrayInt array );
1095 // std::vector<t_CKFLOAT> * (CK_DLL_CALL * const array_float_vector)( ArrayFloat array );
1096 // std::vector<t_CKVEC2> * (CK_DLL_CALL * const array_vec2_vector)( ArrayVec2 array );
1097 // std::vector<t_CKVEC3> * (CK_DLL_CALL * const array_vec3_vector)( ArrayVec3 array );
1098 // std::vector<t_CKVEC4> * (CK_DLL_CALL * const array_vec4_vector)( ArrayVec4 array );
1099 } * const object;
1100
1101 // access to host-side chuck types
1102 struct TypeApi
1103 {
1104 TypeApi();
1105 // look up type by name
1106 Type (CK_DLL_CALL * const lookup)( Chuck_VM *, const char * name );
1107 // get vtable offset for named function (if overloaded, returns first one); returns < 0 if not found
1108 t_CKINT (CK_DLL_CALL * const get_vtable_offset)( Chuck_VM *, Type type, const char * funcName );
1109 // test if two chuck types are equal
1110 t_CKBOOL (CK_DLL_CALL * const is_equal)(Type lhs, Type rhs);
1111 // test if lhs is a type of rhs (e.g., SinOsc is a type of UGen)
1112 t_CKBOOL (CK_DLL_CALL * const isa)(Type lhs, Type rhs);
1113 // register a callback to be invoked whenever a base-type (or its subclass) is instantiated, with option for type system to auto-set shred origin if available
1114 void (CK_DLL_CALL * const callback_on_instantiate)( f_callback_on_instantiate callback, Type base_type, Chuck_VM * vm, t_CKBOOL shouldSetShredOrigin );
1115 // get origin hint ("where did this type originate?")
1116 ckte_Origin (CK_DLL_CALL * const origin_hint)(Type type);
1117 // get type name (full, with decorations) (NOTE do not save a reference to the return value; make a copy if needed) | 1.5.2.0
1118 const char * (CK_DLL_CALL * const name)(Type type);
1119 // get type base name (no decorations) (NOTE do not save a reference to the return value; make a copy if needed) | 1.5.2.0
1120 const char * (CK_DLL_CALL * const base_name)(Type type);
1121 } * const type;
1122
1123 // api to access host-side shreds | 1.5.2.0
1124 struct ShredApi
1125 {
1126 ShredApi();
1127 // get shred parent
1128 Chuck_VM_Shred * (CK_DLL_CALL * const parent)( Chuck_VM_Shred * shred );
1129 // (NEW) get shred parent
1130 // Chuck_VM_Shred * (CK_DLL_CALL * const set_immediate_mode)( Chuck_VM_Shred * shred, t_CKBOOL value );
1131 } * const shred;
1132
1133 // constructor
1134 Chuck_DL_Api() :
1135 vm(new VMApi),
1136 object(new ObjectApi),
1137 type(new TypeApi),
1138 shred(new ShredApi)
1139 { }
1140
1141 private:
1142 // make this object un-copy-able
1143 Chuck_DL_Api( Chuck_DL_Api & a ) :
1144 vm(a.vm),
1145 object(a.object),
1146 type(a.type),
1147 shred(a.shred)
1148 { assert(0); };
1149 // make this object un-copy-able, part 2
1150 Chuck_DL_Api & operator=( Chuck_DL_Api & a ) { assert(0); return a; }
1151 };
1152
1153
1154
1155
1156 //-----------------------------------------------------------------------------
1157 // name: struct Chuck_DLL
1158 // desc: dynamic link library
1159 //-----------------------------------------------------------------------------
1160 struct Chuck_DLL /* : public Chuck_VM_Object */
1161 {
1162 public:
1163 // load module (chugin/dll) from filename
1164 t_CKBOOL load( const char * filename,
1165 const char * func = CK_QUERY_FUNC,
1166 t_CKBOOL lazy = FALSE );
1167 // load module (internal) from query func
1168 t_CKBOOL load( f_ck_query query_func, t_CKBOOL lazy = FALSE );
1169 // get address in loaded ckx
1170 void * get_addr( const char * symbol );
1171 // get last error
1172 const char * last_error() const;
1173 // unload the ckx
1174 t_CKBOOL unload();
1175 // query the content of the dll
1176 const Chuck_DL_Query * query();
1177 // probe information about dll without fully loading it
1178 t_CKBOOL probe();
1179 // is good
1180 t_CKBOOL good() const;
1181 // name
1182 const char * name() const;
1183 // full path
1184 const char * filepath() const;
1185 // get version major
1186 t_CKUINT versionMajor();
1187 // get version minor
1188 t_CKUINT versionMinor();
1189 // is version compatible between dll and host
1190 // major version must be same between chugin and host
1191 // chugin minor version must less than or equal host minor version
1192 t_CKBOOL compatible();
1193
1194 public:
1195 // get info from query
1196 std::string getinfo( const std::string & key );
1197
1198 public:
1199 // constructor
1200 Chuck_DLL( Chuck_Carrier * carrier, const char * xid = NULL )
1201 : m_handle(NULL), m_id(xid ? xid : ""),
1202 m_done_query(FALSE), m_api_version_func(NULL), m_info_func(NULL), m_query_func(NULL),
1203 m_query( carrier, this ), m_apiVersionMajor(0), m_apiVersionMinor(0)
1204 { }
1205 // destructor
1206 ~Chuck_DLL() { this->unload(); }
1207
1208 protected:
1209 // data
1210 void * m_handle;
1211 std::string m_last_error;
1212 std::string m_filename;
1213 std::string m_id;
1214 std::string m_func;
1215 t_CKBOOL m_done_query;
1216
1217 // host/client api version
1218 f_ck_declversion m_api_version_func;
1219 // chugin info func
1220 f_ck_info m_info_func;
1221 // the query func
1222 f_ck_query m_query_func;
1223 // the query shuttle object
1224 Chuck_DL_Query m_query;
1225
1226 protected: // addition info 1.5.0.4 (ge) added
1227 t_CKUINT m_apiVersionMajor;
1228 t_CKUINT m_apiVersionMinor;
1229 };
1230
1231
1232
1233
1234 // dlfcn interface
1235 #if defined(__PLATFORM_APPLE__)
1236 #include <AvailabilityMacros.h>
1237 #endif
1238
1239 // dlfcn interface, panther or below
1240 #if defined(__PLATFORM_APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1030
1241
1242 #error ChucK not support on Mac OS X 10.3 or lower
1243
1244 #elif defined(__PLATFORM_WINDOWS__)
1245
1246 #ifdef __cplusplus
1247 extern "C" {
1248 #endif
1249
1250 #define RTLD_LAZY 0x1
1251 #define RTLD_NOW 0x2
1252 #define RTLD_LOCAL 0x4
1253 #define RTLD_GLOBAL 0x8
1254 #define RTLD_NOLOAD 0x10
1255 #define RTLD_SHARED 0x20 /* not used, the default */
1256 #define RTLD_UNSHARED 0x40
1257 #define RTLD_NODELETE 0x80
1258 #define RTLD_LAZY_UNDEF 0x100
1259
1260 void * dlopen( const char * path, int mode );
1261 void * dlsym( void * handle, const char * symbol );
1262 const char * dlerror( void );
1263 int dlclose( void * handle );
1264 // 1.4.2.0 (ge) added DLERROR_BUFFER_LENGTH
1265 // 1.5.1.5 (ge) increased DLERROR_BUFFER_LENGTH from 128 to 512
1266 #define DLERROR_BUFFER_LENGTH 512
1267 static char dlerror_buffer[DLERROR_BUFFER_LENGTH];
1268
1269 #ifdef __cplusplus
1270 }
1271 #endif
1272
1273 #else
1274 #include "dlfcn.h"
1275 #endif
1276
1277
1278
1279 #endif