The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "sol.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <dlfcn.h>
  7. #include <stdarg.h>
  8. sol_object_t *sol_cast_int(sol_state_t *state, sol_object_t *obj) {
  9. sol_object_t *res, *ls;
  10. if(sol_is_int(obj)) return sol_incref(obj);
  11. ls = sol_new_list(state);
  12. sol_list_insert(state, ls, 0, obj);
  13. res = obj->ops->toint(state, ls);
  14. sol_obj_free(ls);
  15. return res;
  16. }
  17. sol_object_t *sol_cast_float(sol_state_t *state, sol_object_t *obj) {
  18. sol_object_t *res, *ls;
  19. if(sol_is_float(obj)) return sol_incref(obj);
  20. ls = sol_new_list(state);
  21. sol_list_insert(state, ls, 0, obj);
  22. res = obj->ops->tofloat(state, ls);
  23. sol_obj_free(ls);
  24. return res;
  25. }
  26. sol_object_t *sol_cast_string(sol_state_t *state, sol_object_t *obj) {
  27. sol_object_t *res, *ls;
  28. if(sol_is_string(obj)) return sol_incref(obj);
  29. ls = sol_new_list(state);
  30. sol_list_insert(state, ls, 0, obj);
  31. res = obj->ops->tostring(state, ls);
  32. sol_obj_free(ls);
  33. return res;
  34. }
  35. sol_object_t *sol_cast_repr(sol_state_t *state, sol_object_t *obj) {
  36. sol_object_t *res, *ls = sol_new_list(state);
  37. sol_list_insert(state, ls, 0, obj);
  38. res = obj->ops->repr(state, ls);
  39. sol_obj_free(ls);
  40. return res;
  41. }
  42. // This will not fail here; error checking is done in sol_state_init().
  43. sol_object_t *sol_new_singlet(sol_state_t *state, const char *name) {
  44. sol_object_t *res = malloc(sizeof(sol_object_t));
  45. if(res) {
  46. res->type = SOL_SINGLET;
  47. res->refcnt = 0;
  48. res->ops = &(state->SingletOps);
  49. res->str = strdup(name);
  50. }
  51. return sol_incref(res); // XXX Segfault
  52. }
  53. // And, now, for the rest of the checked stuff...
  54. void sol_init_object(sol_state_t *state, sol_object_t *obj) {
  55. if(obj->ops->init) obj->ops->init(state, obj);
  56. }
  57. sol_object_t *sol_new_int(sol_state_t *state, long i) {
  58. sol_object_t *res = sol_alloc_object(state);
  59. res->type = SOL_INTEGER;
  60. res->ival = i;
  61. res->ops = &(state->IntOps);
  62. sol_init_object(state, res);
  63. return res;
  64. }
  65. sol_object_t *sol_new_float(sol_state_t *state, double f) {
  66. sol_object_t *res = sol_alloc_object(state);
  67. res->type = SOL_FLOAT;
  68. res->fval = f;
  69. res->ops = &(state->FloatOps);
  70. sol_init_object(state, res);
  71. return res;
  72. }
  73. sol_object_t *sol_new_string(sol_state_t *state, const char *s) {
  74. sol_object_t *res = sol_alloc_object(state);
  75. res->type = SOL_STRING;
  76. res->str = strdup(s);
  77. if(!res->str) {
  78. sol_obj_free(res);
  79. sol_set_error(state, state->OutOfMemory);
  80. return sol_incref(state->None);
  81. }
  82. res->ops = &(state->StringOps);
  83. sol_init_object(state, res);
  84. return res;
  85. }
  86. int sol_string_cmp(sol_state_t *state, sol_object_t *str, const char *s) {
  87. return strcmp(str->str, s);
  88. }
  89. sol_object_t *sol_string_concat(sol_state_t *state, sol_object_t *a, sol_object_t *b) {
  90. sol_object_t *res, *sa = sol_cast_string(state, a), *sb = sol_cast_string(state, b);
  91. int n = strlen(sa->str) + strlen(sb->str) + 1;
  92. char *s = malloc(n);
  93. res = sol_new_string(state, strncat(strncpy(s, a->str, n), b->str, n));
  94. sol_obj_free(sa);
  95. sol_obj_free(sb);
  96. free(s);
  97. return res;
  98. }
  99. sol_object_t *sol_string_concat_cstr(sol_state_t *state, sol_object_t *a, char *s) {
  100. sol_object_t *b = sol_new_string(state, s);
  101. sol_object_t *res = sol_string_concat(state, a, b);
  102. sol_obj_free(b);
  103. return res;
  104. }
  105. sol_object_t *sol_f_str_free(sol_state_t *state, sol_object_t *obj) {
  106. free(obj->str);
  107. return obj;
  108. }
  109. sol_object_t *sol_new_list(sol_state_t *state) {
  110. sol_object_t *res = sol_alloc_object(state);
  111. res->type = SOL_LIST;
  112. res->seq = dsl_seq_new_array(NULL, &(state->obfuncs));
  113. res->ops = &(state->ListOps);
  114. sol_init_object(state, res);
  115. return res;
  116. }
  117. sol_object_t *sol_list_from_seq(sol_state_t *state, dsl_seq *seq) {
  118. sol_object_t *res = sol_alloc_object(state);
  119. res->type = SOL_LIST;
  120. res->seq = seq;
  121. res->ops = &(state->ListOps);
  122. sol_init_object(state, res);
  123. return res;
  124. }
  125. int sol_list_len(sol_state_t *state, sol_object_t *list) {
  126. return dsl_seq_len(list->seq);
  127. }
  128. sol_object_t *sol_list_sublist(sol_state_t *state, sol_object_t *list, int idx) {
  129. int i = 0;
  130. dsl_seq *subl;
  131. if(idx < 0) {
  132. return sol_set_error_string(state, "Create sublist at negative index");
  133. }
  134. subl = dsl_seq_copy(list->seq);
  135. for(i=0; i<idx; i++) {
  136. dsl_seq_delete(subl, 0);
  137. }
  138. return sol_list_from_seq(state, subl);
  139. }
  140. sol_object_t *sol_list_get_index(sol_state_t *state, sol_object_t *list, int idx) {
  141. if(idx<0 || idx>=dsl_seq_len(list->seq)) return sol_incref(state->None);
  142. return sol_incref(AS_OBJ(dsl_seq_get(list->seq, idx)));
  143. }
  144. void sol_list_set_index(sol_state_t *state, sol_object_t *list, int idx, sol_object_t *obj) {
  145. if(idx<0 || idx>=dsl_seq_len(list->seq)) return;
  146. dsl_seq_set(list->seq, idx, obj);
  147. }
  148. void sol_list_insert(sol_state_t *state, sol_object_t *list, int idx, sol_object_t *obj) {
  149. if(idx<0 || idx>dsl_seq_len(list->seq)) return;
  150. dsl_seq_insert(list->seq, idx, obj);
  151. }
  152. sol_object_t *sol_list_remove(sol_state_t *state, sol_object_t *list, int idx) {
  153. if(idx<0 || idx>=dsl_seq_len(list->seq)) return sol_incref(state->None);
  154. return dsl_seq_remove(list->seq, idx);
  155. }
  156. sol_object_t *sol_list_copy(sol_state_t *state, sol_object_t *list) {
  157. return sol_list_from_seq(state, dsl_seq_copy(list->seq));
  158. }
  159. sol_object_t *sol_list_truncate(sol_state_t *state, sol_object_t *list, int len) {
  160. dsl_seq *newseq = dsl_seq_copy(list->seq);
  161. dsl_seq_iter *iter = dsl_new_seq_iter(newseq);
  162. int pos = dsl_seq_iter_seek(iter, len);
  163. int sz = dsl_seq_len(newseq);
  164. int i;
  165. if(pos>=len) {
  166. for(i=0; i<sz-pos; i++) {
  167. dsl_seq_iter_delete_at(iter);
  168. }
  169. }
  170. dsl_free_seq_iter(iter);
  171. return sol_list_from_seq(state, newseq);
  172. }
  173. void sol_list_append(sol_state_t *state, sol_object_t *dest, sol_object_t *src) {
  174. dsl_seq *oldseq = dest->seq;
  175. dest->seq = dsl_seq_append(dest->seq, src->seq);
  176. dsl_free_seq(oldseq);
  177. }
  178. sol_object_t *sol_f_list_free(sol_state_t *state, sol_object_t *list) {
  179. dsl_free_seq(list->seq);
  180. return list;
  181. }
  182. /*int sol_test_cycle(sol_state_t *state, sol_object_t *seq) {
  183. sol_object_t *seen[1024]={};
  184. sol_object_t *cur = seq, **item;
  185. while(cur) {
  186. item = seen;
  187. while(*item) {
  188. if(*item == cur) return 1;
  189. item++;
  190. }
  191. *item = cur;
  192. if(sol_is_list(seq)) {
  193. cur = cur->lnext;
  194. } else {
  195. cur = cur->mnext;
  196. }
  197. }
  198. return 0;
  199. }
  200. int sol_validate_list(sol_state_t *state, sol_object_t *list) {
  201. sol_object_t *cur = list;
  202. int i = 0;
  203. char msg[128];
  204. while(cur) {
  205. if(!sol_is_list(cur)) {
  206. snprintf(msg, 128, "Node at index %d not a list node", i);
  207. sol_obj_free(sol_set_error_string(state, msg));
  208. return 1;
  209. }
  210. /*if(cur->lnext && !cur->lvalue) {
  211. snprintf(msg, 128, "Node at index %d has a next node but NULL value", i);
  212. sol_obj_free(sol_set_error_string(state, msg));
  213. return 1;
  214. }*//*
  215. cur = cur->lnext;
  216. i++;
  217. }
  218. if(sol_test_cycle(state, list)) {
  219. snprintf(msg, 128, "Cycle detected");
  220. sol_obj_free(sol_set_error_string(state, msg));
  221. return 1;
  222. }
  223. return 0;
  224. }*/
  225. sol_object_t *sol_new_map(sol_state_t *state) {
  226. sol_object_t *map = sol_alloc_object(state);
  227. map->type = SOL_MAP;
  228. map->ops = &(state->MapOps);
  229. map->seq = dsl_seq_new_array(NULL, &(state->obfuncs));
  230. sol_init_object(state, map);
  231. return map;
  232. }
  233. sol_object_t *sol_map_from_seq(sol_state_t *state, dsl_seq *seq) {
  234. sol_object_t *map = sol_alloc_object(state);
  235. if(sol_has_error(state)) return sol_incref(state->None);
  236. map->type = SOL_MAP;
  237. map->ops = &(state->MapOps);
  238. map->seq = seq;
  239. return map;
  240. }
  241. int sol_map_len(sol_state_t *state, sol_object_t *map) {
  242. return dsl_seq_len(map->seq);
  243. }
  244. sol_object_t *sol_map_mcell(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
  245. sol_object_t *list, *cmp, *icmp, *res = NULL;
  246. dsl_seq_iter *iter;
  247. if(!sol_is_map(map)) {
  248. printf("WARNING: Attempt to index non-map as map\n");
  249. return sol_incref(state->None);
  250. }
  251. list = sol_new_list(state);
  252. iter = dsl_new_seq_iter(map->seq);
  253. if(sol_has_error(state)) {
  254. dsl_free_seq_iter(iter);
  255. sol_obj_free(list);
  256. return sol_incref(state->None);
  257. }
  258. sol_list_insert(state, list, 0, key);
  259. sol_list_insert(state, list, 1, state->None);
  260. if(!dsl_seq_iter_is_invalid(iter)) do {
  261. sol_list_set_index(state, list, 1, AS_OBJ(dsl_seq_iter_at(iter))->key);
  262. cmp = key->ops->cmp(state, list);
  263. icmp = sol_cast_int(state, cmp);
  264. sol_obj_free(cmp);
  265. if(icmp->ival == 0) {
  266. res = AS_OBJ(dsl_seq_iter_at(iter));
  267. }
  268. sol_obj_free(icmp);
  269. } while(dsl_seq_iter_next(iter));
  270. dsl_free_seq_iter(iter);
  271. sol_obj_free(list);
  272. if(res) {
  273. return res;
  274. }
  275. return sol_incref(state->None);
  276. }
  277. int sol_map_has(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
  278. sol_object_t *mcell = sol_map_mcell(state, map, key);
  279. int res = sol_is_none(state, mcell);
  280. sol_obj_free(mcell);
  281. return res;
  282. }
  283. sol_object_t *sol_map_get(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
  284. sol_object_t *mcell = sol_map_mcell(state, map, key);
  285. if(sol_is_none(state, mcell)) {
  286. return mcell;
  287. }
  288. return sol_incref(mcell->val);
  289. }
  290. sol_object_t *sol_map_get_name(sol_state_t *state, sol_object_t *map, char *name) {
  291. sol_object_t *key = sol_new_string(state, name);
  292. sol_object_t *res = sol_map_get(state, map, key);
  293. sol_obj_free(key);
  294. return res;
  295. }
  296. void sol_map_set(sol_state_t *state, sol_object_t *map, sol_object_t *key, sol_object_t *val) {
  297. sol_object_t *mcell =sol_map_mcell(state, map, key), *newcell, *temp;
  298. if(sol_is_none(state, mcell)) {
  299. newcell = sol_alloc_object(state);
  300. newcell->type = SOL_MCELL;
  301. newcell->ops = &(state->MCellOps);
  302. newcell->key = sol_incref(key);
  303. newcell->val = sol_incref(val);
  304. dsl_seq_insert(map->seq, 0, newcell);
  305. sol_obj_free(newcell);
  306. } else {
  307. temp = mcell->val;
  308. mcell->val = sol_incref(val);
  309. sol_obj_free(temp);
  310. }
  311. }
  312. void sol_map_set_name(sol_state_t *state, sol_object_t *map, char *name, sol_object_t *val) {
  313. sol_object_t *key = sol_new_string(state, name);
  314. sol_map_set(state, map, key, val);
  315. sol_obj_free(key);
  316. }
  317. void sol_map_set_existing(sol_state_t *state, sol_object_t *map, sol_object_t *key, sol_object_t *val) {
  318. sol_object_t *mcell =sol_map_mcell(state, map, key), *temp;
  319. if(!sol_is_none(state, mcell)) {
  320. temp = mcell->val;
  321. mcell->val = sol_incref(val);
  322. sol_obj_free(temp);
  323. }
  324. }
  325. sol_object_t *sol_map_copy(sol_state_t *state, sol_object_t *map) {
  326. return sol_map_from_seq(state, dsl_seq_copy(map->seq));
  327. }
  328. void sol_map_merge(sol_state_t *state, sol_object_t *dest, sol_object_t *src) {
  329. dsl_seq_iter *iter = dsl_new_seq_iter(src->seq);
  330. if(!dsl_seq_iter_is_invalid(iter)) do {
  331. sol_map_set(state, dest, AS_OBJ(dsl_seq_iter_at(iter))->key, AS_OBJ(dsl_seq_iter_at(iter))->val);
  332. } while(dsl_seq_iter_next(iter));
  333. dsl_free_seq_iter(iter);
  334. }
  335. void sol_map_merge_existing(sol_state_t *state, sol_object_t *dest, sol_object_t *src) {
  336. dsl_seq_iter *iter = dsl_new_seq_iter(src->seq);
  337. if(!dsl_seq_iter_is_invalid(iter)) do {
  338. sol_map_set_existing(state, dest, AS_OBJ(dsl_seq_iter_at(iter))->key, AS_OBJ(dsl_seq_iter_at(iter))->val);
  339. } while(dsl_seq_iter_next(iter));
  340. dsl_free_seq_iter(iter);
  341. }
  342. void sol_map_invert(sol_state_t *state, sol_object_t *map) {
  343. dsl_seq *pairs = dsl_seq_copy(map->seq);
  344. dsl_seq_iter *iter = dsl_new_seq_iter(pairs);
  345. sol_object_t *mcell;
  346. while(!dsl_seq_iter_is_invalid(iter)) {
  347. mcell = dsl_seq_iter_at(iter);
  348. sol_map_set(state, map, mcell->val, mcell->key);
  349. dsl_seq_iter_next(iter);
  350. }
  351. dsl_free_seq_iter(iter);
  352. dsl_free_seq(pairs);
  353. }
  354. sol_object_t *sol_f_map_free(sol_state_t *state, sol_object_t *map) {
  355. dsl_free_seq(map->seq);
  356. return map;
  357. }
  358. sol_object_t *sol_f_mcell_free(sol_state_t *state, sol_object_t *mcell) {
  359. if(mcell->key) sol_obj_free(mcell->key);
  360. if(mcell->val) sol_obj_free(mcell->val);
  361. return mcell;
  362. }
  363. /*int sol_validate_map(sol_state_t *state, sol_object_t *map) {
  364. sol_object_t *cur = map;
  365. int i = 0;
  366. char msg[128];
  367. while(cur) {
  368. if(!sol_is_map(cur)) {
  369. snprintf(msg, 128, "Node at index %d not a map node", i);
  370. sol_obj_free(sol_set_error_string(state, msg));
  371. return 1;
  372. }
  373. if(cur->mnext && (!cur->mkey || !cur->mval)) {
  374. snprintf(msg, 128, "Node at index %d has a next node but NULL key or value", i);
  375. sol_obj_free(sol_set_error_string(state, msg));
  376. return 1;
  377. }
  378. cur = cur->mnext;
  379. i++;
  380. }
  381. return 0;
  382. }*/
  383. sol_object_t *sol_new_cfunc(sol_state_t *state, sol_cfunc_t cfunc) {
  384. sol_object_t *res = sol_alloc_object(state);
  385. res->type = SOL_CFUNCTION;
  386. res->ops = &(state->CFuncOps);
  387. res->cfunc = cfunc;
  388. sol_init_object(state, res);
  389. return res;
  390. }
  391. sol_object_t *sol_new_cdata(sol_state_t *state, void *cdata, sol_ops_t *ops) {
  392. sol_object_t *res = sol_alloc_object(state);
  393. res->type = SOL_CDATA;
  394. res->ops = ops;
  395. res->cdata = cdata;
  396. sol_init_object(state, res);
  397. return res;
  398. }
  399. sol_object_t *sol_new_buffer(sol_state_t *state, void *buffer, ssize_t sz, sol_owntype_t own, sol_freefunc_t freef, sol_movefunc_t movef) {
  400. sol_object_t *res = sol_alloc_object(state);
  401. res->type = SOL_BUFFER;
  402. res->ops = &(state->BufferOps);
  403. res->buffer = buffer;
  404. res->sz = sz;
  405. res->own = own;
  406. res->freef = freef;
  407. res->movef = movef;
  408. sol_init_object(state, res);
  409. return res;
  410. }
  411. sol_object_t *sol_f_buffer_free(sol_state_t *state, sol_object_t *buf) {
  412. switch(buf->own) {
  413. case OWN_FREE:
  414. free(buf->buffer);
  415. break;
  416. case OWN_CALLF:
  417. buf->freef(buf->buffer, buf->sz);
  418. break;
  419. }
  420. return buf;
  421. }
  422. sol_object_t *sol_new_dylib(sol_state_t *state, void *handle) {
  423. sol_object_t *res = sol_alloc_object(state);
  424. res->type = SOL_DYLIB;
  425. res->ops = &(state->DyLibOps);
  426. res->dlhandle = handle;
  427. sol_init_object(state, res);
  428. return res;
  429. }
  430. sol_object_t *sol_f_dylib_free(sol_state_t *state, sol_object_t *dylib) {
  431. dlclose(dylib->dlhandle);
  432. return dylib;
  433. }
  434. sol_object_t *sol_new_dysym(sol_state_t *state, void *sym, dsl_seq *argtp, sol_buftype_t rettp) {
  435. sol_object_t *res = sol_alloc_object(state);
  436. res->type = SOL_DYSYM;
  437. res->ops = &(state->DySymOps);
  438. res->dlsym = sym;
  439. if(argtp) {
  440. res->argtp = dsl_seq_copy(argtp);
  441. } else {
  442. res->argtp = dsl_seq_new_array(NULL, &(state->obfuncs));
  443. }
  444. res->rettp = rettp;
  445. sol_init_object(state, res);
  446. return res;
  447. }
  448. sol_object_t *sol_new_stream(sol_state_t *state, FILE *stream, sol_modes_t modes) {
  449. sol_object_t *res = sol_alloc_object(state);
  450. res->type = SOL_STREAM;
  451. res->ops = &(state->StreamOps);
  452. res->stream = stream;
  453. res->modes = modes;
  454. sol_init_object(state, res);
  455. return res;
  456. }
  457. size_t sol_stream_printf(sol_state_t *state, sol_object_t *stream, const char *fmt, ...) {
  458. va_list va;
  459. size_t res;
  460. if(!(stream->modes & MODE_WRITE)) {
  461. if(state) sol_obj_free(sol_set_error_string(state, "Write to non-writable stream"));
  462. return 0;
  463. }
  464. va_start(va, fmt);
  465. res = vfprintf(stream->stream, fmt, va);
  466. va_end(va);
  467. return res;
  468. }
  469. size_t sol_stream_vprintf(sol_state_t *state, sol_object_t *stream, const char *fmt, va_list va) {
  470. if(!(stream->modes & MODE_WRITE)) {
  471. if(state) sol_obj_free(sol_set_error_string(state, "Write to non-writable stream"));
  472. return 0;
  473. }
  474. return vfprintf(stream->stream, fmt, va);
  475. }
  476. size_t sol_stream_scanf(sol_state_t *state, sol_object_t *stream, const char *fmt, ...) {
  477. va_list va;
  478. size_t res;
  479. if(!(stream->modes & MODE_READ)) {
  480. if(state) sol_obj_free(sol_set_error_string(state, "Read from non-readable stream"));
  481. return 0;
  482. }
  483. va_start(va, fmt);
  484. res = vfscanf(stream->stream, fmt, va);
  485. va_end(va);
  486. return res;
  487. }
  488. size_t sol_stream_fread(sol_state_t *state, sol_object_t *stream, char *buffer, size_t sz, size_t memb) {
  489. if(!(stream->modes & MODE_READ)) {
  490. if(state) sol_obj_free(sol_set_error_string(state, "Read from non-readable stream"));
  491. return 0;
  492. }
  493. return fread(buffer, sz, memb, stream->stream);
  494. }
  495. size_t sol_stream_fwrite(sol_state_t *state, sol_object_t *stream, char *buffer, size_t sz, size_t memb) {
  496. if(!(stream->modes & MODE_WRITE)) {
  497. if(state) sol_obj_free(sol_set_error_string(state, "Write to non-writable stream"));
  498. return 0;
  499. }
  500. return fwrite(buffer, sz, memb, stream->stream);
  501. }
  502. char *sol_stream_fgets(sol_state_t *state, sol_object_t *stream, char *buffer, size_t sz) {
  503. if(!(stream->modes & MODE_READ)) {
  504. if(state) sol_obj_free(sol_set_error_string(state, "Read from non-readable stream"));
  505. return NULL;
  506. }
  507. return fgets(buffer, sz, stream->stream);
  508. }
  509. int sol_stream_fputc(sol_state_t *state, sol_object_t *stream, int ch) {
  510. if(!(stream->modes & MODE_WRITE)) {
  511. if(state) sol_obj_free(sol_set_error_string(state, "Write to non-writable stream"));
  512. return 0;
  513. }
  514. return fputc(ch, stream->stream);
  515. }
  516. int sol_stream_feof(sol_state_t *state, sol_object_t *stream) {
  517. return feof(stream->stream);
  518. }
  519. int sol_stream_ferror(sol_state_t *state, sol_object_t *stream) {
  520. return ferror(stream->stream);
  521. }
  522. int sol_stream_fseek(sol_state_t *state, sol_object_t *stream, long offset, int whence) {
  523. return fseek(stream->stream, offset, whence);
  524. }
  525. long sol_stream_ftell(sol_state_t *state, sol_object_t *stream) {
  526. return ftell(stream->stream);
  527. }
  528. int sol_stream_fflush(sol_state_t *state, sol_object_t *stream) {
  529. return fflush(stream->stream);
  530. }
  531. sol_object_t *sol_f_stream_free(sol_state_t *state, sol_object_t *stream) {
  532. //printf("IO: Closing open file\n");
  533. fclose(stream->stream);
  534. return stream;
  535. }