scheme48.h.in 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees.
  2. See file COPYING. */
  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif
  7. /*
  8. * For building and linking DLLs on Windows, we need to mark functions
  9. * the DLL calls in Scheme 48 and vice versa explicitly---and differently
  10. * depending on whether we're compiling the DLL or Scheme 48 itself.
  11. *
  12. * Therefore, on Windows, we assume that __COMPILING_SCHEME48_ITSELF__
  13. * is defined when Scheme 48 itself is being compiled, and not when
  14. * we're compiling something external against it.
  15. */
  16. #ifdef _WIN32
  17. # ifdef __COMPILING_SCHEME48_ITSELF__
  18. # define S48_EXTERN_ADD_ONS __declspec(dllexport)
  19. # else
  20. # define S48_EXTERN_ADD_ONS __declspec(dllimport)
  21. # endif
  22. #endif
  23. #ifndef S48_EXTERN_ADD_ONS
  24. # define S48_EXTERN_ADD_ONS
  25. #endif
  26. #define S48_EXTERN extern S48_EXTERN_ADD_ONS
  27. #include "scheme48write-barrier.h"
  28. typedef long s48_value;
  29. #define NO_ERRORS 0 /* errno value */
  30. /* Misc stuff */
  31. #define S48_EQ_P(v1, v2) ((v1) == (v2))
  32. /* Superceded name for the above definition, retained for compatibility. */
  33. #define S48_EQ(v1, v2) ((v1) == (v2))
  34. #define S48_MAX_FIXNUM_VALUE ((1 << 29) - 1)
  35. #define S48_MIN_FIXNUM_VALUE (-1 << 29)
  36. S48_EXTERN int s48_stob_has_type(s48_value, int);
  37. S48_EXTERN long s48_stob_length(s48_value, int);
  38. S48_EXTERN long s48_stob_byte_length(s48_value, int);
  39. S48_EXTERN s48_value s48_stob_ref(s48_value, int, long);
  40. S48_EXTERN void s48_stob_set(s48_value, int, long, s48_value);
  41. S48_EXTERN char s48_stob_byte_ref(s48_value, int, long);
  42. S48_EXTERN void s48_stob_byte_set(s48_value, int, long, char);
  43. S48_EXTERN char * s48_register_gc_rootB(char *);
  44. S48_EXTERN void s48_unregister_gc_rootB(char *);
  45. S48_EXTERN void s48_push_gc_rootsB(char *, long);
  46. S48_EXTERN char s48_pop_gc_rootsB(void);
  47. S48_EXTERN char s48_pop_gc_roots_up_to_markerB(char *);
  48. S48_EXTERN s48_value s48_make_string(int, long);
  49. S48_EXTERN void s48_string_set(s48_value s, long i, long c);
  50. S48_EXTERN long s48_string_ref(s48_value s, long i);
  51. S48_EXTERN long s48_string_length(s48_value s);
  52. S48_EXTERN s48_value s48_enter_string_latin_1(char* s);
  53. S48_EXTERN s48_value s48_enter_string_latin_1_n(char* s, long count);
  54. S48_EXTERN void s48_copy_latin_1_to_string(char* s, s48_value sch_s);
  55. S48_EXTERN void s48_copy_latin_1_to_string_n(char* s, long len, s48_value sch_s);
  56. S48_EXTERN void s48_copy_string_to_latin_1(s48_value sch_s, char* s);
  57. S48_EXTERN void s48_copy_string_to_latin_1_n(s48_value sch_s, long start, long count, char* s);
  58. S48_EXTERN s48_value s48_enter_string_utf_8(char* s);
  59. S48_EXTERN s48_value s48_enter_string_utf_8_n(char* s, long count);
  60. S48_EXTERN long s48_string_utf_8_length(s48_value s);
  61. S48_EXTERN long s48_string_utf_8_length_n(s48_value s, long start, long count);
  62. S48_EXTERN void s48_copy_string_to_utf_8(s48_value sch_s, char* s);
  63. S48_EXTERN void s48_copy_string_to_utf_8_n(s48_value sch_s, long start, long count, char* s);
  64. S48_EXTERN s48_value s48_enter_char(long);
  65. S48_EXTERN long s48_extract_char(s48_value);
  66. S48_EXTERN s48_value s48_enter_fixnum(long);
  67. S48_EXTERN long s48_extract_fixnum(s48_value);
  68. S48_EXTERN s48_value s48_enter_integer(long);
  69. S48_EXTERN long s48_extract_integer(s48_value);
  70. S48_EXTERN s48_value s48_enter_double(double);
  71. S48_EXTERN double s48_extract_double(s48_value);
  72. S48_EXTERN s48_value s48_cons(s48_value, s48_value);
  73. S48_EXTERN s48_value s48_enter_byte_vector(char *, long);
  74. S48_EXTERN char * s48_extract_byte_vector(s48_value);
  75. S48_EXTERN s48_value s48_make_vector(int, s48_value);
  76. S48_EXTERN s48_value s48_make_byte_vector(int);
  77. S48_EXTERN s48_value s48_enter_byte_string(char *);
  78. S48_EXTERN s48_value s48_enter_byte_substring(char *, long);
  79. S48_EXTERN s48_value s48_make_record(s48_value);
  80. S48_EXTERN s48_value s48_make_weak_pointer(s48_value);
  81. S48_EXTERN void s48_check_record_type(s48_value, s48_value);
  82. S48_EXTERN long s48_length(s48_value);
  83. S48_EXTERN s48_value s48_enter_pointer(void *);
  84. S48_EXTERN s48_value s48_get_imported_binding(char *);
  85. S48_EXTERN void s48_define_exported_binding(char *, s48_value);
  86. S48_EXTERN s48_value s48_set_channel_os_index(s48_value, long);
  87. S48_EXTERN s48_value s48_add_channel(s48_value, s48_value, long);
  88. S48_EXTERN void s48_close_channel(long);
  89. S48_EXTERN void s48_check_enum_set_type(s48_value, s48_value);
  90. S48_EXTERN long s48_enum_set2integer(s48_value);
  91. S48_EXTERN s48_value s48_integer2enum_set(s48_value, long);
  92. S48_EXTERN s48_value s48_call_scheme(s48_value proc, long nargs, ...);
  93. #define S48_EXPORT_FUNCTION(p) (s48_define_exported_binding(#p, s48_enter_pointer((void*) p)))
  94. #define S48_MAKE_VALUE(type) (s48_make_byte_vector(sizeof(type)))
  95. S48_EXTERN void * s48_value_pointer(s48_value);
  96. #define S48_EXTRACT_VALUE_POINTER(x, type) ((type *) s48_value_pointer(x))
  97. #define S48_EXTRACT_VALUE(x, type) (*(S48_EXTRACT_VALUE_POINTER((x), type)))
  98. #define S48_SET_VALUE(x, type, v) (S48_EXTRACT_VALUE((x), type) = (v))
  99. #define S48_UNSAFE_EXTRACT_VALUE_POINTER(x, type) \
  100. (S48_ADDRESS_AFTER_HEADER((x), type))
  101. #define S48_UNSAFE_EXTRACT_VALUE(x, type) \
  102. (*(S48_UNSAFE_EXTRACT_VALUE_POINTER((x), type)))
  103. #define S48_UNSAFE_SET_VALUE(x, type, v) \
  104. (S48_UNSAFE_EXTRACT_VALUE((x), type) = (v))
  105. #define S48_UNSAFE_EXTRACT_DOUBLE(x) \
  106. (*(S48_ADDRESS_AFTER_HEADER((x), double)))
  107. #define S48_DECLARE_GC_PROTECT(n) long ___gc_buffer[(n)+2]
  108. #define S48_GC_PROTECT_1(v) \
  109. (___gc_buffer[2]=(long)&(v), \
  110. s48_push_gc_rootsB((char *) ___gc_buffer, 1))
  111. #define S48_GC_PROTECT_2(v1, v2) \
  112. (___gc_buffer[2]=(long)&(v1), ___gc_buffer[3]=(long)&(v2), \
  113. s48_push_gc_rootsB((char *) ___gc_buffer, 2))
  114. #define S48_GC_PROTECT_3(v1, v2, v3) \
  115. (___gc_buffer[2]=(long)&(v1), \
  116. ___gc_buffer[3]=(long)&(v2), \
  117. ___gc_buffer[4]=(long)&(v3), \
  118. s48_push_gc_rootsB((char *) ___gc_buffer, 3))
  119. #define S48_GC_PROTECT_4(v1, v2, v3, v4) \
  120. (___gc_buffer[2]=(long)&(v1), \
  121. ___gc_buffer[3]=(long)&(v2), \
  122. ___gc_buffer[4]=(long)&(v3), \
  123. ___gc_buffer[5]=(long)&(v4), \
  124. s48_push_gc_rootsB((char *) ___gc_buffer, 4))
  125. #define S48_GC_PROTECT_5(v1, v2, v3, v4, v5) \
  126. (___gc_buffer[2]=(long)&(v1), \
  127. ___gc_buffer[3]=(long)&(v2), \
  128. ___gc_buffer[4]=(long)&(v3), \
  129. ___gc_buffer[5]=(long)&(v4), \
  130. ___gc_buffer[6]=(long)&(v5), \
  131. s48_push_gc_rootsB((char *) ___gc_buffer, 5))
  132. #define S48_GC_PROTECT_6(v1, v2, v3, v4, v5, v6) \
  133. (___gc_buffer[2]=(long)&(v1), \
  134. ___gc_buffer[3]=(long)&(v2), \
  135. ___gc_buffer[4]=(long)&(v3), \
  136. ___gc_buffer[5]=(long)&(v4), \
  137. ___gc_buffer[6]=(long)&(v5), \
  138. ___gc_buffer[7]=(long)&(v6), \
  139. s48_push_gc_rootsB((char *) ___gc_buffer, 6))
  140. #define S48_GC_PROTECT_7(v1, v2, v3, v4, v5, v6, v7) \
  141. (___gc_buffer[2]=(long)&(v1), \
  142. ___gc_buffer[3]=(long)&(v2), \
  143. ___gc_buffer[4]=(long)&(v3), \
  144. ___gc_buffer[5]=(long)&(v4), \
  145. ___gc_buffer[6]=(long)&(v5), \
  146. ___gc_buffer[7]=(long)&(v6), \
  147. ___gc_buffer[8]=(long)&(v7), \
  148. s48_push_gc_rootsB((char *) ___gc_buffer, 7))
  149. #define S48_GC_PROTECT_8(v1, v2, v3, v4, v5, v6, v7, v8) \
  150. (___gc_buffer[2]=(long)&(v1), \
  151. ___gc_buffer[3]=(long)&(v2), \
  152. ___gc_buffer[4]=(long)&(v3), \
  153. ___gc_buffer[5]=(long)&(v4), \
  154. ___gc_buffer[6]=(long)&(v5), \
  155. ___gc_buffer[7]=(long)&(v6), \
  156. ___gc_buffer[8]=(long)&(v7), \
  157. ___gc_buffer[9]=(long)&(v8), \
  158. s48_push_gc_rootsB((char *) ___gc_buffer, 8))
  159. #define S48_GC_PROTECT_9(v1, v2, v3, v4, v5, v6, v7, v8, v9) \
  160. (___gc_buffer[2]=(long)&(v1), \
  161. ___gc_buffer[3]=(long)&(v2), \
  162. ___gc_buffer[4]=(long)&(v3), \
  163. ___gc_buffer[5]=(long)&(v4), \
  164. ___gc_buffer[6]=(long)&(v5), \
  165. ___gc_buffer[7]=(long)&(v6), \
  166. ___gc_buffer[8]=(long)&(v7), \
  167. ___gc_buffer[9]=(long)&(v8), \
  168. ___gc_buffer[10]=(long)&(v9), \
  169. s48_push_gc_rootsB((char *) ___gc_buffer, 9))
  170. #define S48_GC_PROTECT_10(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) \
  171. (___gc_buffer[2]=(long)&(v1), \
  172. ___gc_buffer[3]=(long)&(v2), \
  173. ___gc_buffer[4]=(long)&(v3), \
  174. ___gc_buffer[5]=(long)&(v4), \
  175. ___gc_buffer[6]=(long)&(v5), \
  176. ___gc_buffer[7]=(long)&(v6), \
  177. ___gc_buffer[8]=(long)&(v7), \
  178. ___gc_buffer[9]=(long)&(v8), \
  179. ___gc_buffer[10]=(long)&(v9), \
  180. ___gc_buffer[11]=(long)&(v10), \
  181. s48_push_gc_rootsB((char *) ___gc_buffer, 10))
  182. #define S48_GC_UNPROTECT() \
  183. do { if (! s48_pop_gc_rootsB()) \
  184. s48_raise_scheme_exception( S48_EXCEPTION_GC_PROTECTION_MISMATCH, 0); \
  185. } while(0)
  186. #define S48_GC_PROTECT_GLOBAL(v) ((void*)(s48_register_gc_rootB((char *)&(v))))
  187. #define S48_GC_UNPROTECT_GLOBAL(f) (s48_unregister_gc_rootB((char *)(f)))
  188. /* Exceptions */
  189. S48_EXTERN void s48_raise_scheme_exception(long type, long nargs, ...);
  190. S48_EXTERN void s48_raise_argument_type_error(s48_value value);
  191. S48_EXTERN void s48_raise_argument_number_error(s48_value value,
  192. s48_value min,
  193. s48_value max);
  194. S48_EXTERN void s48_raise_range_error(s48_value value,
  195. s48_value min,
  196. s48_value max);
  197. S48_EXTERN void s48_raise_closed_channel_error();
  198. S48_EXTERN void s48_raise_os_error(int the_errno);
  199. S48_EXTERN void s48_raise_string_os_error(char *reason);
  200. S48_EXTERN void s48_raise_out_of_memory_error();
  201. /* Old names retained for compatibility; use the versions with 'ument_'
  202. after the '_arg'. */
  203. S48_EXTERN void s48_raise_argument_type_error(s48_value value);
  204. S48_EXTERN void s48_raise_argument_number_error(s48_value value,
  205. s48_value min,
  206. s48_value max);
  207. /* Type checking */
  208. #define S48_CHECK_PAIR(v) do { if (!S48_PAIR_P(v)) s48_raise_argument_type_error(v); } while (0)
  209. #define S48_CHECK_FIXNUM(v) do { if (!S48_FIXNUM_P(v)) s48_raise_argument_type_error(v); } while (0)
  210. #define S48_CHECK_STRING(v) do { if (!S48_STRING_P(v)) s48_raise_argument_type_error(v); } while (0)
  211. #define S48_CHECK_BYTE_VECTOR(v) do { if (!S48_BYTE_VECTOR_P(v)) s48_raise_argument_type_error(v); } while (0)
  212. #define S48_CHECK_CHANNEL(v) do { if (!S48_CHANNEL_P(v)) s48_raise_argument_type_error(v); } while (0)
  213. #define S48_CHECK_RECORD(v) do { if (!S48_RECORD_P(v)) s48_raise_argument_type_error(v); } while (0)
  214. #define S48_CHECK_VALUE(v) do { if (!S48_BYTE_VECTOR_P(v)) s48_raise_argument_type_error(v); } while (0)
  215. #define S48_CHECK_EXPORT_BINDING(v) do { if (!S48_EXPORT_BINDING_P(v)) s48_raise_argument_type_error(v); } while (0)
  216. #define S48_CHECK_BOOLEAN(v) \
  217. do { s48_value s48_temp = (v); \
  218. if (s48_temp != S48_TRUE && s48_temp != S48_FALSE) \
  219. s48_raise_argument_type_error(v); } while (0)
  220. #define S48_VALUE_P(v) (S48_BYTE_VECTOR_P(v))
  221. #define S48_TRUE_P(v) ((v) == S48_TRUE)
  222. #define S48_FALSE_P(v) ((v) == S48_FALSE)
  223. #define S48_EXTRACT_BOOLEAN(v) ((v) != S48_FALSE)
  224. #define S48_ENTER_BOOLEAN(v) ((v) ? S48_TRUE : S48_FALSE)
  225. S48_EXTERN void s48_check_record_type(s48_value record, s48_value type_binding);
  226. #define S48_SHARED_BINDING_CHECK(binding) \
  227. do { if (S48_UNSPECIFIC == S48_SHARED_BINDING_REF(binding)) \
  228. s48_raise_scheme_exception(S48_EXCEPTION_UNBOUND_EXTERNAL_NAME, 1, \
  229. S48_SHARED_BINDING_NAME(binding)); \
  230. } while(0)
  231. #ifdef __cplusplus
  232. /* closing brace for extern "C" */
  233. }
  234. #endif