git » swiftpm » main » tree

[main] / Sources / c_snikket / iinclude / hx / CFFI.h

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#ifndef HX_CFFI_H
#define HX_CFFI_H

// 410 - adds gc_try_unblocking
#define HX_CFFI_API_VERSION 410

#ifdef HXCPP_JS_PRIME
#include <emscripten/bind.h>
using namespace emscripten;

typedef struct emscripten::val value;
typedef struct _vkind  *vkind;
typedef struct _buffer  *buffer;
#define HAVE_NEKO_TYPES 1
#endif

#include "OS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(BLACKBERRY)
using namespace std;
#endif
// --- Register functions (primitives) ----

#ifdef STATIC_LINK

#define DEFINE_PRIM_MULT(func) \
int __reg_##func = hx_register_prim(#func "__MULT",(void *)(&func)); \

#define DEFINE_PRIM(func,nargs) \
int __reg_##func = hx_register_prim(#func "__" #nargs,(void *)(&func)); \


#define DEFINE_PRIM_MULT_NATIVE(func,ext) \
int __reg_##func = hx_register_prim(#func "__MULT",(void *)(&func)) + \
                   hx_register_prim(#func "__"  #ext,(void *)(&func##_##ext)) ; 

#define DEFINE_PRIM_NATIVE(func,nargs,ext) \
int __reg_##func = hx_register_prim(#func "__" #nargs,(void *)(&func)) + \
                   hx_register_prim(#func "__" #ext,(void *)(&func##_##ext)) ; 


#define DEFINE_LIB_PRIM_MULT(lib,func) \
int __reg_##func = hx_register_prim(lib "_" #func "__MULT",(void *)(&func)); \

#define DEFINE_LIB_PRIM(lib,func,nargs) \
int __reg_##func = hx_register_prim(lib "_" #func "__" #nargs,(void *)(&func)); \


#elif defined(HXCPP_JS_PRIME)

//#define DEFINE_PRIM_MULT(func) EMSCRIPTEN_BINDINGS(func) { function(#func, &func); }
//TODO
#define DEFINE_PRIM_MULT(func)

#define DEFINE_PRIM(func,nargs) EMSCRIPTEN_BINDINGS(func) { function(#func, &func); }


#else


#define DEFINE_PRIM_MULT(func) extern "C" { \
  EXPORT void *func##__MULT() { return (void*)(&func); } \
}


#define DEFINE_PRIM(func,nargs) extern "C" { \
  EXPORT void *func##__##nargs() { return (void*)(&func); } \
}


#define DEFINE_PRIM_MULT_NATIVE(func,ext) extern "C" { \
  EXPORT void *func##__MULT() { return (void*)(&func); } \
  EXPORT void *func##__##ext() { return (void*)(&func##_##ext); } \
}


#define DEFINE_PRIM_NATIVE(func,nargs,ext) extern "C" { \
  EXPORT void *func##__##nargs() { return (void*)(&func); } \
  EXPORT void *func##__##ext() { return (void*)(&func##_##ext); } \
}


#define DEFINE_LIB_PRIM_MULT(lib,func) extern "C" { \
  EXPORT void *func##__MULT() { return (void*)(&func); } \
}


#define DEFINE_LIB_PRIM(lib,func,nargs) extern "C" { \
  EXPORT void *func##__##nargs() { return (void*)(&func); } \
}



#endif // !STATIC_LINK




 
#define DEFFUNC_0(ret,name) DEFFUNC(name,ret, (), ())
#define DEFFUNC_1(ret,name,t1) DEFFUNC(name,ret, (t1 a1), (a1))
#define DEFFUNC_2(ret,name,t1,t2) DEFFUNC(name,ret, (t1 a1, t2 a2), (a1,a2))
#define DEFFUNC_3(ret,name,t1,t2,t3) DEFFUNC(name,ret, (t1 a1, t2 a2, t3 a3), (a1,a2,a3))
#define DEFFUNC_4(ret,name,t1,t2,t3,t4) DEFFUNC(name,ret, (t1 a1, t2 a2, t3 a3, t4 a4), (a1,a2,a3,a4))
#define DEFFUNC_5(ret,name,t1,t2,t3,t4,t5) DEFFUNC(name,ret, (t1 a1, t2 a2, t3 a3, t4 a4,t5 a5), (a1,a2,a3,a4,a5))
 

enum hxValueType
{
   valtUnknown = -1,
   valtInt = 0xff,
   valtNull = 0,
   valtFloat = 1,
   valtBool = 2,
   valtString = 3,
   valtObject = 4,
   valtArray = 5,
   valtFunction = 6,
   valtEnum,
   valtClass,
   valtRoot = 0xff,
   valtAbstractBase = 0x100,
};

namespace hx
{
enum StringEncoding
{
   StringAscii,
   StringUtf8,
   StringUtf16
};
}

// val_fun_nargs may return a special value
enum { faNotFunction = -2, faVarArgs=-1, faArgs0=0 /* ... */ };

typedef int field;



#ifdef IMPLEMENT_API
#include "CFFILoader.h"
#endif


#if !defined(HAVE_NEKO_TYPES)
#ifdef HXCPP_NATIVE_CFFI_VALUE
namespace hx { class Object; }
typedef hx::Object _value;
#else
struct _value;
#endif
typedef _value *value;
typedef struct _vkind  *vkind;
typedef struct _buffer  *buffer;
#endif

typedef buffer cffiByteBuffer;

typedef struct _gcroot  *gcroot;

typedef void (*hxFinalizer)(value v);
typedef void (*hxPtrFinalizer)(void *v);

typedef void (__hx_field_iter)(value v,field f,void *);

#define hx_failure(msg)		hx_fail(msg,__FILE__,__LINE__)

#ifndef IGNORE_CFFI_API_H


#ifndef IMPLEMENT_API
 
#if defined(STATIC_LINK) || defined(HXCPP_JS_PRIME)

#define DEFFUNC(name,ret,def_args,call_args) \
extern "C" ret name def_args;


#else

#define DEFFUNC(name,ret,def_args,call_args) \
typedef ret (*FUNC_##name) def_args; \
extern FUNC_##name name;

#endif

#endif
 

#include "CFFIAPI.h"


#ifdef WANT_DYNALLOC_ALLOC_BYTES
void *DynAlloc::allocBytes(size_t n)
{
   return hx_alloc((int)n);
}
#endif



#define DEFINE_KIND(name) extern "C" { vkind name = 0; }

#ifdef STATIC_LINK
#	define DEFINE_ENTRY_POINT(name)
#else
#	define DEFINE_ENTRY_POINT(name) extern "C" {  void name(); EXPORT void *__neko_entry_point() { return (void *)&name; } }
#endif

#ifdef HEADER_IMPORTS
#	define H_EXTERN IMPORT
#else
#	define H_EXTERN EXPORT
#endif

#define DECLARE_PRIM(func,nargs) extern "C" {  H_EXTERN void *func##__##nargs(); }
#define DECLARE_KIND(name) extern "C" {  H_EXTERN extern vkind name; }



// --- Helpers ----------------------------------------------------------------

// Check type...
inline bool val_is_null(value inVal) { return val_type(inVal)==valtNull; }
inline bool val_is_int(value inVal) { return val_type(inVal)==valtInt; }
inline bool val_is_bool(value inVal) { return val_type(inVal)==valtBool; }
inline bool val_is_float(value inVal) { return val_type(inVal)==valtFloat; }
inline bool val_is_string(value inVal) { return val_type(inVal)==valtString; }
inline bool val_is_function(value inVal) { return val_type(inVal)==valtFunction; }
inline bool val_is_array(value inVal) { return val_type(inVal)==valtArray; }
inline bool val_is_abstract(value inVal) { return val_type(inVal)>=valtAbstractBase; }
inline bool val_is_kind(value inVal,vkind inKind) { return val_to_kind(inVal,inKind)!=0; }

inline bool val_is_number(value inVal)
{
	int t = val_type(inVal);
	return t==valtInt || t==valtFloat;
}
inline bool val_is_object(value inVal)
{
	int t = val_type(inVal);
	return t==valtObject || t==valtEnum ||t==valtClass;
}

class AutoGCBlocking
{
public:
	inline AutoGCBlocking(bool inSoftUnlock=false) :
      mSoftUnlock(inSoftUnlock), mLocked( gc_try_blocking() ) {  }
	inline ~AutoGCBlocking() { Close(); }
	inline void Close()
   {
      if (mLocked)
      {
         if (mSoftUnlock)
            gc_try_unblocking();
         else
            gc_exit_blocking();
      }
      mLocked = false;
   }

	bool mLocked;
	bool mSoftUnlock;
};

class AutoGCUnblocking
{
public:
	AutoGCUnblocking() : mUnlocked( gc_try_unblocking() ) {  }
	~AutoGCUnblocking() { Close(); }
	void Close() { if (mUnlocked) gc_enter_blocking(); mUnlocked = false; }

	bool mUnlocked;
};


class AutoGCRoot
{
public:
   AutoGCRoot(value inValue)
   {
		mRoot = 0;
		mPtr = alloc_root();
		if (mPtr)
			*mPtr = inValue;
		else
			mRoot = create_root(inValue);
   }

  ~AutoGCRoot()
   {
		if (mPtr)
			free_root(mPtr);
		else if (mRoot)
         destroy_root(mRoot);
   }
   value get()const { return mPtr ? *mPtr : query_root(mRoot); }
   void set(value inValue)
	{ 
		if (mPtr)
			*mPtr = inValue;
		else
		{
			if (mRoot) destroy_root(mRoot);
			mRoot = create_root(inValue);
		}
	}
   
private:
   value *mPtr;
   gcroot mRoot;
   AutoGCRoot(const AutoGCRoot &);
   void operator=(const AutoGCRoot &);
};

struct CffiBytes
{
   CffiBytes( unsigned char *inData=0, int inLength=0) : data(inData), length(inLength) {}

   unsigned char *data;
   int length;
};

inline CffiBytes getByteData(value inValue)
{
   static field bField = 0;
   static field lengthField = 0;
   if (bField==0)
   {
      bField = val_id("b");
      lengthField = val_id("length");
   }

   if (val_is_object(inValue))
   {
      value b = val_field(inValue, bField);
      value len = val_field(inValue, lengthField);
      if (val_is_string(b) && val_is_int(len))
         return CffiBytes( (unsigned char *)val_string(b), val_int(len) );
      if (val_is_buffer(b) && val_is_int(len))
         return CffiBytes( (unsigned char *)buffer_data(val_to_buffer(b)), val_int(len) );
   }
   else if (val_is_buffer(inValue))
   {
      value len = val_field(inValue, lengthField);
      if (val_is_int(len))
      {
         buffer b = val_to_buffer(inValue);
         return CffiBytes( (unsigned char *)buffer_data(b), val_int(len) );
      }
   }
   return CffiBytes();
}

inline bool resizeByteData(value inValue, int inNewLen)
{
   if (!val_is_object(inValue))
      return false;

   static field bField = 0;
   static field lengthField = 0;
   if (bField==0)
   {
      bField = val_id("b");
      lengthField = val_id("length");
   }
   value len = val_field(inValue, lengthField);
   if (!val_is_int(len))
      return false;
   int oldLen = val_int(len);
   value b = val_field(inValue, bField);
   if (val_is_string(b))
   {
      if (inNewLen>oldLen)
      {
         value newString = alloc_raw_string(inNewLen);
         memcpy( (char *)val_string(newString), val_string(b), inNewLen);
         alloc_field(inValue, bField, newString );
      }
      alloc_field(inValue, lengthField, alloc_int(inNewLen) );
   }
   else if (val_is_buffer(b))
   {
      cffiByteBuffer buf = val_to_buffer(b);
      buffer_set_size(buf,inNewLen);
      alloc_field(inValue, lengthField, alloc_int(inNewLen) );
   }
   else
      return false;

   return true;
}


#define val_null alloc_null()

#define bfailure(x) val_throw(buffer_to_string(x))

#define copy_string(str,len) alloc_string_len(str,len)


// The "Check" macros throw an error if assumtion is false
#define val_check_kind(v,t)	if( !val_is_kind(v,t) ) hx_failure("invalid kind");
#define val_check_function(f,n) if( !val_is_function(f) || (val_fun_nargs(f) != (n) && val_fun_nargs(f) != faVarArgs) ) hx_failure("Bad function");
#define val_check(v,t)		if( !val_is_##t(v) ) hx_failure("type not " #t);

// The "Get" function will return or force an error
inline bool val_get_bool(value inVal) {  val_check(inVal,bool); return val_bool(inVal); }
inline int val_get_int(value inVal) {  val_check(inVal,int); return val_int(inVal); }
inline double val_get_double(value inVal) {  val_check(inVal,number); return val_number(inVal); }
inline const char *val_get_string(value inVal) {  val_check(inVal,string); return val_string(inVal); }
inline void *val_get_handle(value inVal,vkind inKind)
  {  val_check_kind(inVal,inKind); return val_to_kind(inVal,inKind); }


inline value alloc_string(const char *inStr)
{
   const char *end = inStr;
   while(*end) end++;
   return alloc_string_len(inStr,(int)(end-inStr));
}

inline value alloc_wstring(const wchar_t *inStr)
{
   const wchar_t *end = inStr;
   while(*end) end++;
   return alloc_wstring_len(inStr,(int)(end-inStr));
}

inline void hxcpp_unscramble(const unsigned char *bytes, int len, const char *key, unsigned char *dest)
{
   int keyLen = 0;
   while(key[keyLen])
      keyLen++;
   int state = 0;
   //state = ((state + key[i]) ^ ch) & 0xff);
   for(int i=0;i<len;i++)
   {
      dest[i] = ( (state + key[i%keyLen]) ^ bytes[i] ) & 0xff;
      state = bytes[i];
   }
}


//additional glue for easier neko modules compilation
#define val_true    alloc_bool(true)
#define val_false    alloc_bool(false)
inline void neko_error() { hx_error(); }


// Conservative marking within a buffer is not yet supported.
//inline void * alloc(int i) { return hx_alloc(i); }

// The bytes themselves will be GC'd, but not the pointers contained within.
inline void * alloc_private(int i) { return hx_alloc(i); }

// You should use alloc_buffer_len/buffer_data instead
//value alloc_empty_string(int len) { }


#endif


#endif