scheme48.h.in 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: Richard Kelsey, Jonathan Rees, Marcus Crestani, Mike
  5. * Sperber, Michael Zabka, Harald Glab-Phlak
  6. */
  7. #if defined HAVE_STDINT_H
  8. #include <stdint.h> /* uintXX_t, C99 */
  9. #endif
  10. #if defined HAVE_SYS_TYPES_H
  11. #include <sys/types.h> /* size_t */
  12. #endif
  13. typedef long s48_value;
  14. #define NO_ERRORS 0 /* errno value */
  15. #if SIZEOF_VOID_P == 4
  16. #define S48_MAX_FIXNUM_VALUE ((1 << 29) - 1)
  17. #define S48_MIN_FIXNUM_VALUE (-1 << 29)
  18. #define S48_LOG_BYTES_PER_CELL 2
  19. #elif SIZEOF_VOID_P == 8
  20. #define S48_MAX_FIXNUM_VALUE ((1L << 61) - 1)
  21. #define S48_MIN_FIXNUM_VALUE (-1L << 61)
  22. #define S48_LOG_BYTES_PER_CELL 3
  23. #else
  24. #error "What size are your pointers, really?"
  25. #endif
  26. /* New FFI */
  27. typedef struct s48_ref_s *s48_ref_t;
  28. typedef struct s48_call_s *s48_call_t;
  29. /* local refs */
  30. S48_EXTERN s48_ref_t s48_make_local_ref(s48_call_t call, s48_value obj);
  31. S48_EXTERN s48_ref_t s48_copy_local_ref(s48_call_t call, s48_ref_t ref);
  32. S48_EXTERN void s48_free_local_ref(s48_call_t call, s48_ref_t ref);
  33. S48_EXTERN void s48_free_local_ref_array(s48_call_t call, s48_ref_t *refs, size_t len);
  34. /* global refs */
  35. S48_EXTERN s48_ref_t s48_make_global_ref(s48_value obj);
  36. S48_EXTERN void s48_free_global_ref(s48_ref_t ref);
  37. S48_EXTERN s48_ref_t s48_local_to_global_ref(s48_ref_t ref);
  38. /* local bufs */
  39. S48_EXTERN void *s48_make_local_buf (s48_call_t call, size_t s);
  40. S48_EXTERN void s48_free_local_buf (s48_call_t call, void *buffer);
  41. /* subcalls */
  42. S48_EXTERN s48_call_t s48_make_subcall(s48_call_t call);
  43. S48_EXTERN void s48_free_subcall(s48_call_t subcall);
  44. S48_EXTERN s48_ref_t s48_finish_subcall(s48_call_t call, s48_call_t subcall, s48_ref_t ref);
  45. /* immediate refs */
  46. S48_EXTERN s48_ref_t s48_get_immediate_ref(long immediate_index);
  47. /* external code should not use this, but might need to... */
  48. S48_EXTERN void s48_setref(s48_ref_t ref, s48_value obj);
  49. S48_EXTERN s48_value s48_deref(s48_ref_t ref);
  50. /* Misc stuff */
  51. #define s48_eq_p_2(c, r1, r2) (s48_deref(r1) == s48_deref(r2))
  52. /* Superceded name for the above definition, retained for compatibility. */
  53. #define s48_eq_2(c, r1, r2) (c, s48_deref(r1) == s48_deref(r2))
  54. S48_EXTERN int s48_stob_has_type_2(s48_call_t, s48_ref_t, int);
  55. S48_EXTERN long s48_stob_length_2(s48_call_t, s48_ref_t, int);
  56. S48_EXTERN long s48_stob_byte_length_2(s48_call_t, s48_ref_t, int);
  57. S48_EXTERN s48_ref_t s48_stob_ref_2(s48_call_t, s48_ref_t, int, long);
  58. S48_EXTERN void s48_stob_set_2(s48_call_t, s48_ref_t, int, long, s48_ref_t);
  59. S48_EXTERN char s48_stob_byte_ref_2(s48_call_t, s48_ref_t, int, long);
  60. S48_EXTERN void s48_stob_byte_set_2(s48_call_t, s48_ref_t, int, long, char);
  61. S48_EXTERN s48_ref_t s48_make_string_2(s48_call_t, int, long);
  62. S48_EXTERN void s48_string_set_2(s48_call_t, s48_ref_t s, long i, long c);
  63. S48_EXTERN long s48_string_ref_2(s48_call_t, s48_ref_t s, long i);
  64. S48_EXTERN long s48_string_length_2(s48_call_t, s48_ref_t s);
  65. S48_EXTERN s48_ref_t s48_enter_string_latin_1_2(s48_call_t, const char* s);
  66. S48_EXTERN s48_ref_t s48_enter_string_latin_1_n_2(s48_call_t, const char* s, long count);
  67. S48_EXTERN long s48_string_latin_1_length_2(s48_call_t, s48_ref_t s);
  68. S48_EXTERN long s48_string_latin_1_length_n_2(s48_call_t, s48_ref_t s, long start, long count);
  69. S48_EXTERN void s48_copy_latin_1_to_string_2(s48_call_t, const char* s, s48_ref_t sch_s);
  70. S48_EXTERN void s48_copy_latin_1_to_string_n_2(s48_call_t, const char* s, long len, s48_ref_t sch_s);
  71. S48_EXTERN void s48_copy_string_to_latin_1_2(s48_call_t, s48_ref_t sch_s, char* s);
  72. S48_EXTERN void s48_copy_string_to_latin_1_n_2(s48_call_t, s48_ref_t sch_s, long start, long count, char* s);
  73. S48_EXTERN char * s48_extract_latin_1_from_string_2(s48_call_t, s48_ref_t);
  74. S48_EXTERN s48_ref_t s48_enter_string_utf_8_2(s48_call_t, const char* s);
  75. S48_EXTERN s48_ref_t s48_enter_string_utf_8_n_2(s48_call_t, const char* s, long count);
  76. S48_EXTERN long s48_string_utf_8_length_2(s48_call_t, s48_ref_t s);
  77. S48_EXTERN long s48_string_utf_8_length_n_2(s48_call_t, s48_ref_t s, long start, long count);
  78. S48_EXTERN long s48_copy_string_to_utf_8_2(s48_call_t, s48_ref_t sch_s, char* s);
  79. S48_EXTERN long s48_copy_string_to_utf_8_n_2(s48_call_t, s48_ref_t sch_s, long start, long count, char* s);
  80. S48_EXTERN char * s48_extract_utf_8_from_string_2(s48_call_t, s48_ref_t);
  81. S48_EXTERN s48_ref_t s48_enter_string_utf_16be_2(s48_call_t, const uint16_t *);
  82. S48_EXTERN s48_ref_t s48_enter_string_utf_16be_n_2(s48_call_t, const uint16_t *, long);
  83. S48_EXTERN long s48_string_utf_16be_length_2(s48_call_t, s48_ref_t);
  84. S48_EXTERN long s48_string_utf_16be_length_n_2(s48_call_t, s48_ref_t, long, long);
  85. S48_EXTERN uint16_t * s48_extract_utf_16be_from_string_2(s48_call_t, s48_ref_t);
  86. S48_EXTERN s48_ref_t s48_enter_string_utf_16le_2(s48_call_t, const uint16_t *);
  87. S48_EXTERN long s48_copy_string_to_utf_16be_2(s48_call_t, s48_ref_t, uint16_t *);
  88. S48_EXTERN long s48_copy_string_to_utf_16be_n_2(s48_call_t, s48_ref_t, long, long, uint16_t *);
  89. S48_EXTERN s48_ref_t s48_enter_string_utf_16le_n_2(s48_call_t, const uint16_t *, long);
  90. S48_EXTERN long s48_string_utf_16le_length_2(s48_call_t, s48_ref_t);
  91. S48_EXTERN long s48_string_utf_16le_length_n_2(s48_call_t, s48_ref_t, long, long);
  92. S48_EXTERN long s48_copy_string_to_utf_16le_2(s48_call_t, s48_ref_t, uint16_t *);
  93. S48_EXTERN long s48_copy_string_to_utf_16le_n_2(s48_call_t, s48_ref_t, long, long, uint16_t *);
  94. S48_EXTERN uint16_t * s48_extract_utf_16le_from_string_2(s48_call_t, s48_ref_t);
  95. S48_EXTERN s48_ref_t s48_enter_char_2(s48_call_t, long);
  96. S48_EXTERN long s48_extract_char_2(s48_call_t, s48_ref_t);
  97. S48_EXTERN s48_ref_t s48_enter_long_as_fixnum_2(s48_call_t, long);
  98. S48_EXTERN s48_ref_t s48_enter_long_2(s48_call_t, long);
  99. S48_EXTERN long s48_extract_long_2(s48_call_t, s48_ref_t);
  100. S48_EXTERN s48_ref_t s48_enter_unsigned_long_2(s48_call_t, unsigned long);
  101. S48_EXTERN unsigned long s48_extract_unsigned_long_2(s48_call_t, s48_ref_t);
  102. S48_EXTERN s48_ref_t s48_enter_double_2(s48_call_t, double);
  103. S48_EXTERN double s48_extract_double_2(s48_call_t, s48_ref_t);
  104. S48_EXTERN s48_ref_t s48_cons_2(s48_call_t, s48_ref_t, s48_ref_t);
  105. S48_EXTERN s48_ref_t s48_enter_byte_vector_2(s48_call_t, const char *, long);
  106. S48_EXTERN s48_ref_t s48_enter_unmovable_byte_vector_2(s48_call_t, const char *, long);
  107. S48_EXTERN char * s48_extract_byte_vector_2(s48_call_t, s48_ref_t);
  108. S48_EXTERN char * s48_extract_byte_vector_readonly_2(s48_call_t, s48_ref_t);
  109. S48_EXTERN char * s48_extract_unmovable_byte_vector_2(s48_call_t, s48_ref_t);
  110. S48_EXTERN void s48_extract_byte_vector_region_2(s48_call_t, s48_ref_t, long, long, char*);
  111. S48_EXTERN void s48_enter_byte_vector_region_2(s48_call_t, s48_ref_t, long, long, char*);
  112. S48_EXTERN char * s48_extract_byte_vector_unmanaged_2(s48_call_t, s48_ref_t);
  113. S48_EXTERN void s48_release_byte_vector_2(s48_call_t, s48_ref_t, char*);
  114. S48_EXTERN void s48_copy_from_byte_vector_2(s48_call_t, s48_ref_t, char *);
  115. S48_EXTERN void s48_copy_to_byte_vector_2(s48_call_t, s48_ref_t, char *);
  116. S48_EXTERN s48_ref_t s48_make_vector_2(s48_call_t, long, s48_ref_t);
  117. S48_EXTERN s48_ref_t s48_make_byte_vector_2(s48_call_t, long);
  118. S48_EXTERN s48_ref_t s48_make_unmovable_byte_vector_2(s48_call_t, long);
  119. S48_EXTERN s48_ref_t s48_enter_byte_string_2(s48_call_t, const char *);
  120. S48_EXTERN s48_ref_t s48_enter_byte_substring_2(s48_call_t, const char *, long);
  121. S48_EXTERN s48_ref_t s48_make_record_2(s48_call_t, s48_ref_t);
  122. S48_EXTERN s48_ref_t s48_make_weak_pointer_2(s48_call_t, s48_ref_t);
  123. S48_EXTERN void s48_check_record_type_2(s48_call_t, s48_ref_t, s48_ref_t);
  124. S48_EXTERN s48_ref_t s48_length_2(s48_call_t, s48_ref_t);
  125. S48_EXTERN s48_ref_t s48_enter_pointer_2(s48_call_t, void *);
  126. S48_EXTERN void* s48_extract_pointer_2(s48_call_t, s48_ref_t);
  127. S48_EXTERN s48_ref_t s48_get_imported_binding_2(char *);
  128. S48_EXTERN s48_ref_t s48_get_imported_binding_local_2(s48_call_t, char *);
  129. S48_EXTERN s48_ref_t s48_define_exported_binding_2(s48_call_t, char *, s48_ref_t);
  130. S48_EXTERN s48_ref_t s48_set_channel_os_index_2(s48_call_t, s48_ref_t, long);
  131. S48_EXTERN s48_ref_t s48_add_channel_2(s48_call_t, s48_ref_t, s48_ref_t, long);
  132. S48_EXTERN void s48_close_channel_2(s48_call_t, long);
  133. S48_EXTERN void s48_check_enum_set_type_2(s48_call_t, s48_ref_t, s48_ref_t);
  134. S48_EXTERN long s48_enum_set2integer_2(s48_call_t, s48_ref_t);
  135. S48_EXTERN s48_ref_t s48_integer2enum_set_2(s48_call_t, s48_ref_t, long);
  136. S48_EXTERN s48_ref_t s48_call_scheme_2(s48_call_t call, s48_ref_t proc, long nargs, ...);
  137. S48_EXTERN s48_ref_t s48_get_current_time(s48_call_t call);
  138. S48_EXTERN s48_ref_t s48_get_timezone(s48_call_t call);
  139. #define s48_make_value_2(c, type) (s48_make_byte_vector_2(c, sizeof(type)))
  140. #define s48_make_sized_value_2(c, size) (s48_make_byte_vector_2(c, size))
  141. S48_EXTERN void * s48_value_pointer_2(s48_call_t, s48_ref_t);
  142. #define s48_extract_value_pointer_2(c, x, type) ((type *) s48_value_pointer_2(c, x))
  143. #define s48_extract_value_2(c, x, type) (*(s48_extract_value_pointer_2(c, (x), type)))
  144. #define s48_value_size_2(c, x) (s48_byte_vector_length_2(c, x))
  145. #define s48_set_value_2(c, x, type, v) (s48_extract_value_2(c, (x), type) = (v))
  146. #define s48_unsafe_extract_value_pointer_2(c, x, type) \
  147. (s48_address_after_header_2(c, (x), type))
  148. #define s48_unsafe_extract_value_2(c, x, type) \
  149. (*(s48_unsafe_extract_value_pointer_2(c, (x), type)))
  150. #define s48_unsafe_set_value_2(c, x, type, v) \
  151. (s48_unsafe_extract_value_2(c, (x), type) = (v))
  152. #define s48_unsafe_extract_double_2(c, x) \
  153. (*(s48_address_after_header_2(c, (x), double)))
  154. #define s48_arg_ref_2(c, argv, index, argc) ((argv)[(argc)-(index)-1])
  155. /* Exceptions */
  156. S48_EXTERN void s48_error_2(s48_call_t call, const char* who, const char* message,
  157. long irritant_count, ...);
  158. S48_EXTERN void s48_assertion_violation_2(s48_call_t call, const char* who, const char* message,
  159. long irritant_count, ...);
  160. S48_EXTERN void s48_os_error_2(s48_call_t call, const char* who, int the_errno,
  161. long irritant_count, ...);
  162. S48_EXTERN void s48_out_of_memory_error_2(s48_call_t call);
  163. /* Internal use */
  164. S48_EXTERN void s48_raise_scheme_exception_2(s48_call_t call, long type, long nargs, ...);
  165. /* Type checking */
  166. #define s48_check_pair_2(c, v) do { if (!s48_pair_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a pair", 1, v); } while (0)
  167. #define s48_check_fixnum_2(c, v) do { if (!s48_fixnum_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a fixnum", 1, v); } while (0)
  168. #define s48_check_string_2(c, v) do { if (!s48_string_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a string", 1, v); } while (0)
  169. #define s48_check_byte_vector_2(c, v) do { if (!s48_byte_vector_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a bytevector", 1, v); } while (0)
  170. #define s48_check_channel_2(c, v) do { if (!s48_channel_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a channel", 1, v); } while (0)
  171. #define s48_check_record_2(c, v) do { if (!s48_record_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a record", 1, v); } while (0)
  172. #define s48_check_value_2(c, v) do { if (!s48_byte_vector_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be an external value", 1, v); } while (0)
  173. #define s48_check_export_binding_2(c, v) do { if (!s48_export_binding_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be an exported value", 1, v ); } while (0)
  174. #define s48_check_boolean_2(c, v) \
  175. do { if (!s48_false_p_2(c, v) && !s48_true_p_2(c, v)) \
  176. s48_assertion_violation_2(c, NULL, "must be a boolean", 1, v); } while (0)
  177. #define s48_value_p_2(c, v) (s48_byte_vector_p_2(c, v))
  178. #define s48_true_p_2(c, r) (s48_deref(r) == _s48_value_true)
  179. #define s48_false_p_2(c, r) (s48_deref(r) == _s48_value_false)
  180. #define s48_null_p_2(c, r) (s48_deref(r) == _s48_value_null)
  181. #define s48_extract_boolean_2(c, r) (!(s48_deref(r) == _s48_value_false))
  182. #define s48_enter_boolean_2(c, v) ((v) ? s48_true_2(c) : s48_false_2(c))
  183. #define s48_shared_binding_check_2(c, binding) \
  184. do { if (s48_deref(s48_shared_binding_ref_2(c, binding)) == _s48_value_unspecific) \
  185. s48_raise_scheme_exception_2(c, s48_exception_unbound_external_name, 1, \
  186. s48_shared_binding_name_2(c, binding)); \
  187. } while(0)
  188. #ifndef NO_OLD_FFI
  189. /* Misc stuff */
  190. #define S48_EQ_P(v1, v2) ((v1) == (v2))
  191. /* Superceded name for the above definition, retained for compatibility. */
  192. #define S48_EQ(v1, v2) ((v1) == (v2))
  193. S48_EXTERN int s48_stob_has_type(s48_value, int);
  194. S48_EXTERN long s48_stob_length(s48_value, int);
  195. S48_EXTERN long s48_stob_byte_length(s48_value, int);
  196. S48_EXTERN s48_value s48_stob_ref(s48_value, int, long);
  197. S48_EXTERN void s48_stob_set(s48_value, int, long, s48_value);
  198. S48_EXTERN char s48_stob_byte_ref(s48_value, int, long);
  199. S48_EXTERN void s48_stob_byte_set(s48_value, int, long, char);
  200. S48_EXTERN char * s48_register_gc_rootB(char *);
  201. S48_EXTERN void s48_unregister_gc_rootB(char *);
  202. S48_EXTERN void s48_push_gc_rootsB(char *, long);
  203. S48_EXTERN char s48_pop_gc_rootsB(void);
  204. S48_EXTERN s48_value s48_make_string(int, long);
  205. S48_EXTERN void s48_string_set(s48_value s, long i, long c);
  206. S48_EXTERN long s48_string_ref(s48_value s, long i);
  207. S48_EXTERN long s48_string_length(s48_value s);
  208. S48_EXTERN s48_value s48_enter_string_latin_1(char* s);
  209. S48_EXTERN s48_value s48_enter_string_latin_1_n(char* s, long count);
  210. S48_EXTERN void s48_copy_latin_1_to_string(char* s, s48_value sch_s);
  211. S48_EXTERN void s48_copy_latin_1_to_string_n(char* s, long len, s48_value sch_s);
  212. S48_EXTERN void s48_copy_string_to_latin_1(s48_value sch_s, char* s);
  213. S48_EXTERN void s48_copy_string_to_latin_1_n(s48_value sch_s, long start, long count, char* s);
  214. S48_EXTERN s48_value s48_enter_string_utf_8(char* s);
  215. S48_EXTERN s48_value s48_enter_string_utf_8_n(char* s, long count);
  216. S48_EXTERN long s48_string_utf_8_length(s48_value s);
  217. S48_EXTERN long s48_string_utf_8_length_n(s48_value s, long start, long count);
  218. S48_EXTERN long s48_copy_string_to_utf_8(s48_value sch_s, char* s);
  219. S48_EXTERN long s48_copy_string_to_utf_8_n(s48_value sch_s, long start, long count, char* s);
  220. S48_EXTERN s48_value s48_enter_string_utf_16be(const uint16_t *);
  221. S48_EXTERN s48_value s48_enter_string_utf_16be_n(const uint16_t *, long);
  222. S48_EXTERN long s48_string_utf_16be_length(s48_value);
  223. S48_EXTERN long s48_string_utf_16be_length_n(s48_value, long, long);
  224. S48_EXTERN long s48_copy_string_to_utf_16be(s48_value, uint16_t *);
  225. S48_EXTERN long s48_copy_string_to_utf_16be_n(s48_value, long, long, uint16_t *);
  226. S48_EXTERN s48_value s48_enter_string_utf_16le(const uint16_t *);
  227. S48_EXTERN s48_value s48_enter_string_utf_16le_n(const uint16_t *, long);
  228. S48_EXTERN long s48_string_utf_16le_length(s48_value);
  229. S48_EXTERN long s48_string_utf_16le_length_n(s48_value, long, long);
  230. S48_EXTERN long s48_copy_string_to_utf_16le(s48_value, uint16_t *);
  231. S48_EXTERN long s48_copy_string_to_utf_16le_n(s48_value, long, long, uint16_t *);
  232. S48_EXTERN s48_value s48_enter_char(long);
  233. S48_EXTERN long s48_extract_char(s48_value);
  234. S48_EXTERN s48_value s48_enter_fixnum(long);
  235. S48_EXTERN long s48_extract_fixnum(s48_value);
  236. S48_EXTERN s48_value s48_enter_integer(long);
  237. S48_EXTERN long s48_extract_integer(s48_value);
  238. S48_EXTERN s48_value s48_enter_unsigned_integer(unsigned long);
  239. S48_EXTERN unsigned long s48_extract_unsigned_integer(s48_value);
  240. S48_EXTERN s48_value s48_enter_double(double);
  241. S48_EXTERN double s48_extract_double(s48_value);
  242. S48_EXTERN s48_value s48_cons(s48_value, s48_value);
  243. S48_EXTERN s48_value s48_enter_byte_vector(char *, long);
  244. S48_EXTERN s48_value s48_enter_unmovable_byte_vector(char *, long);
  245. S48_EXTERN char * s48_extract_byte_vector(s48_value);
  246. S48_EXTERN s48_value s48_make_vector(long, s48_value);
  247. S48_EXTERN s48_value s48_make_byte_vector(long);
  248. S48_EXTERN s48_value s48_make_unmovable_byte_vector(long);
  249. S48_EXTERN s48_value s48_enter_byte_string(char *);
  250. S48_EXTERN s48_value s48_enter_byte_substring(char *, long);
  251. S48_EXTERN s48_value s48_make_record(s48_value);
  252. S48_EXTERN s48_value s48_make_weak_pointer(s48_value);
  253. S48_EXTERN void s48_check_record_type(s48_value, s48_value);
  254. S48_EXTERN s48_value s48_length(s48_value);
  255. S48_EXTERN void* s48_extract_pointer(s48_value);
  256. S48_EXTERN s48_value s48_get_imported_binding(char *);
  257. S48_EXTERN s48_value s48_set_channel_os_index(s48_value, long);
  258. S48_EXTERN s48_value s48_add_channel(s48_value, s48_value, long);
  259. S48_EXTERN void s48_close_channel(long);
  260. S48_EXTERN void s48_check_enum_set_type(s48_value, s48_value);
  261. S48_EXTERN long s48_enum_set2integer(s48_value);
  262. S48_EXTERN s48_value s48_integer2enum_set(s48_value, long);
  263. S48_EXTERN s48_value s48_call_scheme(s48_value proc, long nargs, ...);
  264. #define S48_MAKE_VALUE(type) (s48_make_byte_vector(sizeof(type)))
  265. #define S48_MAKE_SIZED_VALUE(size) (s48_make_byte_vector(size))
  266. S48_EXTERN void * s48_value_pointer(s48_value);
  267. #define S48_EXTRACT_VALUE_POINTER(x, type) ((type *) s48_value_pointer(x))
  268. #define S48_EXTRACT_VALUE(x, type) (*(S48_EXTRACT_VALUE_POINTER((x), type)))
  269. #define S48_VALUE_SIZE(x) (S48_BYTE_VECTOR_LENGTH(x))
  270. #define S48_SET_VALUE(x, type, v) (S48_EXTRACT_VALUE((x), type) = (v))
  271. #define S48_UNSAFE_EXTRACT_VALUE_POINTER(x, type) \
  272. (S48_ADDRESS_AFTER_HEADER((x), type))
  273. #define S48_UNSAFE_EXTRACT_VALUE(x, type) \
  274. (*(S48_UNSAFE_EXTRACT_VALUE_POINTER((x), type)))
  275. #define S48_UNSAFE_SET_VALUE(x, type, v) \
  276. (S48_UNSAFE_EXTRACT_VALUE((x), type) = (v))
  277. #define S48_UNSAFE_EXTRACT_DOUBLE(x) \
  278. (*(S48_ADDRESS_AFTER_HEADER((x), double)))
  279. #define S48_ARG_REF(argv, index, argc) ((argv)[(argc)-(index)-1])
  280. #define S48_DECLARE_GC_PROTECT(n) long ___gc_buffer[(n)+2]
  281. #define S48_GC_PROTECT_1(v) \
  282. (___gc_buffer[2]=(long)&(v), \
  283. s48_push_gc_rootsB((char *) ___gc_buffer, 1))
  284. #define S48_GC_PROTECT_2(v1, v2) \
  285. (___gc_buffer[2]=(long)&(v1), ___gc_buffer[3]=(long)&(v2), \
  286. s48_push_gc_rootsB((char *) ___gc_buffer, 2))
  287. #define S48_GC_PROTECT_3(v1, v2, v3) \
  288. (___gc_buffer[2]=(long)&(v1), \
  289. ___gc_buffer[3]=(long)&(v2), \
  290. ___gc_buffer[4]=(long)&(v3), \
  291. s48_push_gc_rootsB((char *) ___gc_buffer, 3))
  292. #define S48_GC_PROTECT_4(v1, v2, v3, v4) \
  293. (___gc_buffer[2]=(long)&(v1), \
  294. ___gc_buffer[3]=(long)&(v2), \
  295. ___gc_buffer[4]=(long)&(v3), \
  296. ___gc_buffer[5]=(long)&(v4), \
  297. s48_push_gc_rootsB((char *) ___gc_buffer, 4))
  298. #define S48_GC_PROTECT_5(v1, v2, v3, v4, v5) \
  299. (___gc_buffer[2]=(long)&(v1), \
  300. ___gc_buffer[3]=(long)&(v2), \
  301. ___gc_buffer[4]=(long)&(v3), \
  302. ___gc_buffer[5]=(long)&(v4), \
  303. ___gc_buffer[6]=(long)&(v5), \
  304. s48_push_gc_rootsB((char *) ___gc_buffer, 5))
  305. #define S48_GC_PROTECT_6(v1, v2, v3, v4, v5, v6) \
  306. (___gc_buffer[2]=(long)&(v1), \
  307. ___gc_buffer[3]=(long)&(v2), \
  308. ___gc_buffer[4]=(long)&(v3), \
  309. ___gc_buffer[5]=(long)&(v4), \
  310. ___gc_buffer[6]=(long)&(v5), \
  311. ___gc_buffer[7]=(long)&(v6), \
  312. s48_push_gc_rootsB((char *) ___gc_buffer, 6))
  313. #define S48_GC_PROTECT_7(v1, v2, v3, v4, v5, v6, v7) \
  314. (___gc_buffer[2]=(long)&(v1), \
  315. ___gc_buffer[3]=(long)&(v2), \
  316. ___gc_buffer[4]=(long)&(v3), \
  317. ___gc_buffer[5]=(long)&(v4), \
  318. ___gc_buffer[6]=(long)&(v5), \
  319. ___gc_buffer[7]=(long)&(v6), \
  320. ___gc_buffer[8]=(long)&(v7), \
  321. s48_push_gc_rootsB((char *) ___gc_buffer, 7))
  322. #define S48_GC_PROTECT_8(v1, v2, v3, v4, v5, v6, v7, v8) \
  323. (___gc_buffer[2]=(long)&(v1), \
  324. ___gc_buffer[3]=(long)&(v2), \
  325. ___gc_buffer[4]=(long)&(v3), \
  326. ___gc_buffer[5]=(long)&(v4), \
  327. ___gc_buffer[6]=(long)&(v5), \
  328. ___gc_buffer[7]=(long)&(v6), \
  329. ___gc_buffer[8]=(long)&(v7), \
  330. ___gc_buffer[9]=(long)&(v8), \
  331. s48_push_gc_rootsB((char *) ___gc_buffer, 8))
  332. #define S48_GC_PROTECT_9(v1, v2, v3, v4, v5, v6, v7, v8, v9) \
  333. (___gc_buffer[2]=(long)&(v1), \
  334. ___gc_buffer[3]=(long)&(v2), \
  335. ___gc_buffer[4]=(long)&(v3), \
  336. ___gc_buffer[5]=(long)&(v4), \
  337. ___gc_buffer[6]=(long)&(v5), \
  338. ___gc_buffer[7]=(long)&(v6), \
  339. ___gc_buffer[8]=(long)&(v7), \
  340. ___gc_buffer[9]=(long)&(v8), \
  341. ___gc_buffer[10]=(long)&(v9), \
  342. s48_push_gc_rootsB((char *) ___gc_buffer, 9))
  343. #define S48_GC_PROTECT_10(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) \
  344. (___gc_buffer[2]=(long)&(v1), \
  345. ___gc_buffer[3]=(long)&(v2), \
  346. ___gc_buffer[4]=(long)&(v3), \
  347. ___gc_buffer[5]=(long)&(v4), \
  348. ___gc_buffer[6]=(long)&(v5), \
  349. ___gc_buffer[7]=(long)&(v6), \
  350. ___gc_buffer[8]=(long)&(v7), \
  351. ___gc_buffer[9]=(long)&(v8), \
  352. ___gc_buffer[10]=(long)&(v9), \
  353. ___gc_buffer[11]=(long)&(v10), \
  354. s48_push_gc_rootsB((char *) ___gc_buffer, 10))
  355. #define S48_GC_UNPROTECT() \
  356. do { if (! s48_pop_gc_rootsB()) \
  357. s48_raise_scheme_exception( S48_EXCEPTION_GC_PROTECTION_MISMATCH, 0); \
  358. } while(0)
  359. #define S48_GC_PROTECT_GLOBAL(v) ((void*)(s48_register_gc_rootB((char *)&(v))))
  360. #define S48_GC_UNPROTECT_GLOBAL(f) (s48_unregister_gc_rootB((char *)(f)))
  361. /* Exceptions */
  362. S48_EXTERN void s48_error(const char* who, const char* message,
  363. long irritant_count, ...);
  364. S48_EXTERN void s48_assertion_violation(const char* who, const char* message,
  365. long irritant_count, ...);
  366. S48_EXTERN void s48_os_error(const char* who, int the_errno,
  367. long irritant_count, ...);
  368. S48_EXTERN void s48_out_of_memory_error();
  369. /* The following are deprecated */
  370. S48_EXTERN void s48_raise_argument_type_error(s48_value value);
  371. S48_EXTERN void s48_raise_argument_number_error(s48_value value,
  372. s48_value min,
  373. s48_value max);
  374. S48_EXTERN void s48_raise_range_error(s48_value value,
  375. s48_value min,
  376. s48_value max);
  377. S48_EXTERN void s48_raise_closed_channel_error();
  378. S48_EXTERN void s48_raise_os_error(int the_errno);
  379. S48_EXTERN void s48_raise_string_os_error(char *reason);
  380. S48_EXTERN void s48_raise_out_of_memory_error();
  381. /* Internal use */
  382. S48_EXTERN void s48_raise_scheme_exception(long type, long nargs, ...);
  383. /* Type checking */
  384. #define S48_CHECK_PAIR(v) do { if (!S48_PAIR_P(v)) s48_assertion_violation(NULL, "must be a pair", 1, v); } while (0)
  385. #define S48_CHECK_FIXNUM(v) do { if (!S48_FIXNUM_P(v)) s48_assertion_violation(NULL, "must be a fixnum", 1, v); } while (0)
  386. #define S48_CHECK_STRING(v) do { if (!S48_STRING_P(v)) s48_assertion_violation(NULL, "must be a string", 1, v); } while (0)
  387. #define S48_CHECK_BYTE_VECTOR(v) do { if (!S48_BYTE_VECTOR_P(v)) s48_assertion_violation(NULL, "must be a bytevector", 1, v); } while (0)
  388. #define S48_CHECK_CHANNEL(v) do { if (!S48_CHANNEL_P(v)) s48_assertion_violation(NULL, "must be a channel", 1, v); } while (0)
  389. #define S48_CHECK_RECORD(v) do { if (!S48_RECORD_P(v)) s48_assertion_violation(NULL, "must be a record", 1, v); } while (0)
  390. #define S48_CHECK_VALUE(v) do { if (!S48_BYTE_VECTOR_P(v)) s48_assertion_violation(NULL, "must be an external value", 1, v); } while (0)
  391. #define S48_CHECK_EXPORT_BINDING(v) do { if (!S48_EXPORT_BINDING_P(v)) s48_assertion_violation(NULL, "must be an exported value", 1, v ); } while (0)
  392. #define S48_CHECK_BOOLEAN(v) \
  393. do { s48_value s48_temp = (v); \
  394. if (s48_temp != S48_TRUE && s48_temp != S48_FALSE) \
  395. s48_assertion_violation(NULL, "must be a boolean", 1, v); } while (0)
  396. #define S48_VALUE_P(v) (S48_BYTE_VECTOR_P(v))
  397. #define S48_TRUE_P(v) ((v) == S48_TRUE)
  398. #define S48_FALSE_P(v) ((v) == S48_FALSE)
  399. #define S48_NULL_P(v) ((v) == S48_NULL)
  400. #define S48_EXTRACT_BOOLEAN(v) ((v) != S48_FALSE)
  401. #define S48_ENTER_BOOLEAN(v) ((v) ? S48_TRUE : S48_FALSE)
  402. #define S48_SHARED_BINDING_CHECK(binding) \
  403. do { if (S48_UNSPECIFIC == S48_SHARED_BINDING_REF(binding)) \
  404. s48_raise_scheme_exception(S48_EXCEPTION_UNBOUND_EXTERNAL_NAME, 1, \
  405. S48_SHARED_BINDING_NAME(binding)); \
  406. } while(0)
  407. #endif /* !NO_OLD_FFI */
  408. /* both */
  409. S48_EXTERN s48_value s48_define_exported_binding(char *, s48_value);
  410. S48_EXTERN s48_value s48_enter_pointer(void *);
  411. #define S48_EXPORT_FUNCTION(p) (s48_define_exported_binding(#p, s48_enter_pointer((void*) p)))
  412. #define s48_export_function(p) S48_EXPORT_FUNCTION(p)
  413. S48_EXTERN void s48_note_external_event(long);