nany
nany.h
1 /*
2 ** Nany - https://nany.io
3 ** This Source Code Form is subject to the terms of the Mozilla Public
4 ** License, v. 2.0. If a copy of the MPL was not distributed with this
5 ** file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 */
7 #ifndef __LIBNANYC_NANY_C_H__
8 #define __LIBNANYC_NANY_C_H__
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdint.h>
12 
13 #if defined(_WIN32) || defined(__CYGWIN__)
14 # ifdef __GNUC__
15 # define LIBNANYC_VISIBILITY_EXPORT __attribute__ ((dllexport))
16 # define LIBNANYC_VISIBILITY_IMPORT __attribute__ ((dllimport))
17 # else
18 # define LIBNANYC_VISIBILITY_EXPORT __declspec(dllexport) /* note: actually gcc seems to also supports this syntax */
19 # define LIBNANYC_VISIBILITY_IMPORT __declspec(dllimport) /* note: actually gcc seems to also supports this syntax */
20 # endif
21 #else
22 # define LIBNANYC_VISIBILITY_EXPORT __attribute__((visibility("default")))
23 # define LIBNANYC_VISIBILITY_IMPORT __attribute__((visibility("default")))
24 #endif
25 
26 #if defined(_DLL) && !defined(LIBNANYC_DLL_EXPORT)
27 # define LIBNANYC_DLL_EXPORT
28 #endif
29 
34 #if defined(LIBNANYC_DLL_EXPORT)
35 # define NY_EXPORT LIBNANYC_VISIBILITY_EXPORT
36 #else
37 # define NY_EXPORT LIBNANYC_VISIBILITY_IMPORT
38 #endif
39 
40 
41 
42 
43 
44 
45 
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 
57 NY_EXPORT void nylib_print_info_for_bugreport();
58 
65 NY_EXPORT char* nylib_get_info_for_bugreport(uint32_t* length);
66 
68 NY_EXPORT const char* nylib_website_url();
69 
78 NY_EXPORT uint32_t nylib_get_version(uint32_t* major, uint32_t* minor, uint32_t* patch);
79 
81 NY_EXPORT const char* nylib_version();
83 NY_EXPORT const char* nylib_version_metadata();
85 NY_EXPORT const char* nylib_version_prerelease();
86 
98 NY_EXPORT int nylib_check_compatible_version(uint32_t major, uint32_t minor);
108 #ifndef LIBNANYC_NYBOOL_T
109 #define LIBNANYC_NYBOOL_T
110 
111 typedef enum nybool_t {nyfalse = 0, nytrue} nybool_t;
112 #endif
113 
114 
115 #ifndef LIBNANYC_NYANYSTR_T
116 #define LIBNANYC_NYANYSTR_T
117 typedef struct {
118  uint32_t size;
119  const char* c_str;
120 }
121 nyanystr_t;
122 
124 static inline nyanystr_t nyanystr(const char* const text) {
125  nyanystr_t s = {((text) ? (uint32_t) strlen(text) : 0), text};
126  return s;
127 }
128 
130 static inline nyanystr_t nyanystr_ex(const char* const text, uint32_t len) {
131  nyanystr_t s = {len, text};
132  return s;
133 }
134 
136 static inline void nyanystr_duplicate(nyanystr_t* const out, const nyanystr_t* const src) {
137  uint32_t len = src->size;
138  const char* srcstr = src->c_str;
139  char* str = (char*) malloc(sizeof(char) * (len + 1));
140  memcpy(str, srcstr, len);
141  str[len] = '\0';
142  out->c_str = str;
143  out->size = len;
144 }
145 #endif
146 
147 
149 typedef enum { /* nytype_t */
151  nyt_void = 0,
153  nyt_any,
155  nyt_ptr,
157  nyt_bool,
159  nyt_u8,
161  nyt_u16,
163  nyt_u32,
165  nyt_u64,
167  nyt_i8,
169  nyt_i16,
171  nyt_i32,
173  nyt_i64,
175  nyt_f32,
177  nyt_f64
178 }
179 nytype_t;
180 
181 enum {
183  nyt_count = nyt_f64 + 1,
184 };
185 
186 
192 typedef enum { /* nyvisibility_t */
194  nyv_undefined,
196  nyv_default,
198  nyv_private,
200  nyv_protected,
202  nyv_internal,
204  nyv_public,
206  nyv_published
207 }
208 nyvisibility_t;
209 
210 enum {
212  nyv_count = nyv_published + 1,
213 };
214 
215 
217 typedef struct nythread_t nythread_t;
219 typedef struct nyproject_t nyproject_t;
221 typedef struct nytarget_t nytarget_t;
223 typedef struct nysource_t nysource_t;
225 typedef struct nybuild_t nybuild_t;
227 typedef struct nyprogram_t nyprogram_t;
229 typedef struct nythread_t nytctx_t;
235 struct nyallocator_t;
236 
237 typedef struct nyallocator_cf_t {
238  void* userdata;
240  nybool_t (*create_mt)(struct nyallocator_t*, const struct nyallocator_cf_t*);
242  nybool_t (*create_st)(struct nyallocator_t*, const struct nyallocator_cf_t*);
244  void (*on_not_enough_memory)(struct nyallocator_t*, nybool_t limit_reached);
245 }
247 
248 typedef struct nyallocator_t {
249  void* userdata;
251  void* (*allocate)(struct nyallocator_t*, size_t);
253  void* (*reallocate)(struct nyallocator_t*, void* ptr, size_t oldsize, size_t newsize);
255  void (*deallocate)(struct nyallocator_t*, void* ptr, size_t);
257  volatile size_t reserved_mem0;
261  void (*on_not_enough_memory)(struct nyallocator_t*, nybool_t limit_reached);
265  void (*release)(struct nyallocator_t*);
266 }
268 
269 
270 NY_EXPORT void nyallocator_cf_init(nyallocator_cf_t*);
271 
273 NY_EXPORT void nany_memalloc_set_default(nyallocator_t*);
275 NY_EXPORT void nany_memalloc_set_with_limit(nyallocator_t*, size_t limit);
277 void nany_memalloc_copy(nyallocator_t* out, const nyallocator_t* const src);
285 typedef enum nyconsole_output_t {
287  nycout = 1,
289  nycerr = 2,
290 }
291 nyconsole_output_t;
292 
293 
295 typedef enum nycolor_t {
297  nyc_none = 0,
299  nyc_black,
301  nyc_red,
303  nyc_green,
305  nyc_yellow,
307  nyc_blue,
309  nyc_purple,
311  nyc_gray,
313  nyc_white,
315  nyc_lightblue
316 }
317 nycolor_t;
318 
319 enum {
321  nyc_count = nyc_lightblue + 1
322 };
323 
324 
325 typedef struct nyconsole_t {
327  void (*write_stdout)(void*, const char* text, size_t length);
329  void (*write_stderr)(void*, const char* text, size_t length);
331  void (*flush)(void*, nyconsole_output_t);
333  void (*set_color)(void*, nyconsole_output_t, nycolor_t);
335  nybool_t (*has_color)(void*, nyconsole_output_t);
336 
338  void* internal;
340  void (*release)(const struct nyconsole_t*);
341 }
343 
345 NY_EXPORT void nyconsole_cf_set_stdcout(nyconsole_t*);
346 
347 void nyconsole_cf_copy(nyconsole_t* out, const nyconsole_t* const src);
358 typedef struct nyproject_cf_t {
362  void (*on_target_added)(nyproject_t*, nytarget_t*, const char* name, uint32_t len);
364  void (*on_target_removed)(nyproject_t*, nytarget_t*, const char* name, uint32_t len);
367 }
369 
370 
378 NY_EXPORT nyproject_t* nyproject_create(const nyproject_cf_t* cf);
379 
384 NY_EXPORT void nyproject_ref(nyproject_t* project);
385 
390 NY_EXPORT void nyproject_unref(nyproject_t* project);
391 
393 NY_EXPORT void nyproject_cf_init(nyproject_cf_t*);
394 
395 
397 NY_EXPORT nybool_t nyproject_add_source_from_file(nyproject_t*, const char* filename);
399 NY_EXPORT nybool_t nyproject_add_source_from_file_n(nyproject_t*, const char* filename, size_t);
400 
402 NY_EXPORT nybool_t nyproject_add_source(nyproject_t*, const char* text);
404 NY_EXPORT nybool_t nyproject_add_source_n(nyproject_t*, const char* text, size_t);
405 
406 
408 NY_EXPORT void nyproject_lock(const nyproject_t*);
410 NY_EXPORT void nyproject_unlock(const nyproject_t*);
412 NY_EXPORT nybool_t nyproject_trylock(const nyproject_t*);
423 typedef struct nybuild_cf_t {
428 
434  /* (can be used to only retrieve the list of atoms or unittests) */
435  nybool_t ignore_atoms;
436 
438  void* userdata;
439 
441  void (*on_unittest)(void* userdata, const char* mod, uint32_t mlen, const char* name, uint32_t nlen);
442 
444  nybool_t (*on_query)(const nyproject_t*);
446  void (*on_begin)(const nyproject_t*, nybuild_t*);
447 
449  nybool_t (*on_progress)(const nyproject_t*, nybuild_t*, const char* id, const char* element,
450  uint32_t percent);
452  nybool_t (*on_binding_discovery)(nybuild_t*, const char* name, uint32_t size);
453 
455  void (*on_end)(const nyproject_t*, nybuild_t*, nybool_t success);
456  void (*on_error_file_eacces)(const nyproject_t*, nybuild_t*, const char* filename, uint32_t length);
457 }
459 
460 
461 
465 NY_EXPORT nybuild_t* nybuild_prepare(nyproject_t*, const nybuild_cf_t*);
466 
470 NY_EXPORT nybool_t nybuild(nybuild_t*);
471 
478 NY_EXPORT void nybuild_print_report_to_console(nybuild_t* build, nybool_t print_header);
479 
484 NY_EXPORT void nybuild_ref(nybuild_t* build);
489 NY_EXPORT void nybuild_unref(nybuild_t* build);
490 
492 NY_EXPORT void nybuild_cf_init(nybuild_cf_t* cf, const nyproject_t* project);
501 typedef struct nyfile_t nyfile_t;
502 
503 
504 typedef enum nyio_type_t {
505  nyiot_failed = 0,
506  nyiot_file,
507  nyiot_folder,
508 }
509 nyio_type_t;
510 
511 
512 typedef enum nyio_err_t {
514  nyioe_ok = 0,
516  nyioe_failed,
518  nyioe_unsupported,
520  nyioe_memory,
522  nyioe_access,
524  nyioe_exists,
525 }
526 nyio_err_t;
527 
528 
529 typedef enum nyio_automout_flag_t {
531  nyioaf_all = (uint32_t) - 1,
533  nyioaf_none = 0,
535  nyioaf_root = (1 << 0),
537  nyioaf_tmp = (1 << 1),
539  nyioaf_home = (1 << 2),
541  nyioaf_users = (1 << 3),
542 }
543 nyio_automout_flag_t;
544 
545 
546 
547 
549 typedef nybool_t (*nyio_opened_files_it_t)(const char* vpath, uint32_t len, const char* localpath,
550  uint32_t lplen);
551 
552 
554 typedef struct nyio_adapter_t nyio_adapter_t;
555 
556 typedef struct nyio_iterator_t nyio_iterator_t;
557 
558 
566  void* internal;
568  void* invalid_fd;
569 
571  nyio_type_t (*stat)(nyio_adapter_t*, const char* path, uint32_t len);
573  nyio_type_t (*statex)(nyio_adapter_t*, const char* path, uint32_t len, uint64_t* size, int64_t* modified);
574 
575 
577  uint64_t (*file_read)(void*, void* buffer, uint64_t bufsize);
579  uint64_t (*file_write)(void*, const void* buffer, uint64_t bufsize);
581  void* (*file_open)(nyio_adapter_t*, const char* path, uint32_t len,
582  nybool_t readm, nybool_t writem, nybool_t appendm, nybool_t truncm);
584  void (*file_close)(void*);
586  nybool_t (*file_eof)(void*);
587 
589  nyio_err_t (*file_seek)(void*, uint64_t offset);
591  nyio_err_t (*file_seek_from_end)(void*, int64_t offset);
593  nyio_err_t (*file_seek_cur)(void*, int64_t offset);
595  uint64_t (*file_tell)(void*);
596 
598  void (*file_flush)(void*);
599 
601  uint64_t (*file_size)(nyio_adapter_t*, const char* path, uint32_t len);
603  nyio_err_t (*file_erase)(nyio_adapter_t*, const char* path, uint32_t len);
605  nyio_err_t (*file_exists)(nyio_adapter_t*, const char* path, uint32_t len);
607  nyio_err_t (*file_resize)(nyio_adapter_t*, const char* path, uint32_t len, uint64_t newsize);
608 
610  nyio_err_t (*file_get_contents)(nyio_adapter_t*, char** content, uint64_t* size, uint64_t* capacity,
611  const char* path, uint32_t len);
613  nyio_err_t (*file_set_contents)(nyio_adapter_t*, const char* path, uint32_t len, const char* content,
614  uint32_t ctlen);
616  nyio_err_t (*file_append_contents)(nyio_adapter_t*, const char* path, uint32_t len, const char* content,
617  uint32_t ctlen);
618 
619 
621  nyio_err_t (*folder_create)(nyio_adapter_t*, const char* path, uint32_t len);
623  nyio_err_t (*folder_erase)(nyio_adapter_t*, const char* path, uint32_t len);
625  nyio_err_t (*folder_clear)(nyio_adapter_t*, const char* path, uint32_t len);
627  uint64_t (*folder_size)(nyio_adapter_t*, const char* path, uint32_t len);
629  nyio_err_t (*folder_exists)(nyio_adapter_t*, const char* path, uint32_t len);
630 
632  nyio_iterator_t* (*folder_iterate)(nyio_adapter_t*, const char* path, uint32_t len,
633  nybool_t recursive, nybool_t files, nybool_t folders);
635  nyio_iterator_t* (*folder_next)(nyio_iterator_t*);
637  const char* (*folder_iterator_fullpath)(nyio_adapter_t*, nyio_iterator_t*);
639  const char* (*folder_iterator_name)(nyio_iterator_t*);
641  uint64_t (*folder_iterator_size)(nyio_iterator_t*);
643  nyio_type_t (*folder_iterator_type)(nyio_iterator_t*);
645  void (*folder_iterator_close)(nyio_iterator_t*);
646 
648  void (*clone)(nyio_adapter_t* parent, nyio_adapter_t* dst);
650  void (*release)(nyio_adapter_t*);
651 };
652 
653 
654 typedef struct nyio_cf_t {
656  nyio_err_t (*on_mount_query)(nyprogram_t*, nytctx_t*, const char* url, const char* path, uint32_t len);
658  nyio_err_t (*on_adapter_create)(nyprogram_t*, nytctx_t*, nyio_adapter_t**, const char* url,
659  nyio_adapter_t* parent);
660 
663  uint32_t automount_flags;
664 }
665 nyio_cf_t;
666 
670 NY_EXPORT void nyio_adapter_create_from_local_folder(nyio_adapter_t*, nyallocator_t*,
671  const char* localfolder, size_t len);
680 typedef struct nybacktrace_entry_t {
682  const char* atom;
684  const char* filename;
686  uint32_t atom_size;
688  uint32_t filename_size;
690  uint32_t line;
692  uint32_t column;
693 }
695 
696 
698 typedef struct nyprogram_cf_t {
703 
708  nybool_t (*on_execute)(nyprogram_t*);
709 
714  nybool_t (*on_thread_create)(nyprogram_t*, nytctx_t*, nythread_t* parent, const char* name, uint32_t size);
719  void (*on_thread_destroy)(nyprogram_t*, nythread_t*);
720 
722  /* wip - void (*on_error)(const nyprogram_t*, const nybacktrace_entry_t** backtrace, uint32_t bt_len); */
727  void (*on_terminate)(const nyprogram_t*, nybool_t error, int exitcode);
728 
731 
734 }
736 
737 
739 typedef struct nyvm_t {
743  nyprogram_t* program;
745  nytctx_t* tctx;
748 }
749 nyvm_t;
750 
751 
757 NY_EXPORT nyprogram_t* nyprogram_prepare(nybuild_t* build, const nyprogram_cf_t* cf);
758 
768 NY_EXPORT int nyprogram_main(nyprogram_t* program, uint32_t argc, const char** argv);
769 
770 
775 NY_EXPORT void nyprogram_ref(nyprogram_t* program);
780 NY_EXPORT void nyprogram_unref(nyprogram_t* program);
781 
783 NY_EXPORT void nyprogram_cf_init(nyprogram_cf_t* cf, const nybuild_cf_t*);
798 nybool_t nyprint_ast_from_file(const char* filename, int fd, nybool_t unixcolors);
799 
805 NY_EXPORT nybool_t nyprint_ast_from_file_n(const char* filename, size_t length, int fd, nybool_t unixcolors);
806 
813 NY_EXPORT nybool_t nyprint_ast_from_memory(const char* content, int fd, nybool_t unixcolors);
820 NY_EXPORT nybool_t nyprint_ast_from_memory_n(const char* content, size_t length, int fd,
821  nybool_t unixcolors);
822 
829 NY_EXPORT nybool_t nytry_parse_file(const char* const filename);
837 NY_EXPORT nybool_t nytry_parse_file_n(const char* filename, size_t length);
838 
839 
840 
841 
849 NY_EXPORT nyvisibility_t nycstring_to_visibility(const char* const text);
855 NY_EXPORT nyvisibility_t nycstring_to_visibility_n(const char* const text, size_t length);
856 
858 NY_EXPORT const char* nyvisibility_to_cstring(nyvisibility_t);
859 
860 
867 NY_EXPORT nytype_t nycstring_to_type(const char* const text);
868 
875 NY_EXPORT nytype_t nycstring_to_type_n(const char* const text, size_t length);
876 
878 NY_EXPORT const char* nytype_to_cstring(nytype_t);
879 
881 NY_EXPORT uint32_t nytype_sizeof(nytype_t);
889 typedef struct nyrun_cf_t {
901  nybool_t verbose;
902 }
903 nyrun_cf_t;
904 
906 NY_EXPORT void nyrun_cf_init(nyrun_cf_t*);
907 
909 NY_EXPORT void nyrun_cf_release(nyrun_cf_t*);
919 NY_EXPORT int nyrun(const nyrun_cf_t* cf, const char* source, uint32_t argc, const char** argv);
920 
931 NY_EXPORT int nyrun_n(const nyrun_cf_t* cf, const char* source, size_t length, uint32_t argc,
932  const char** argv);
933 
934 
944 NY_EXPORT int nyrun_file(const nyrun_cf_t* cf, const char* file, uint32_t argc, const char** argv);
945 
956 NY_EXPORT int nyrun_file_n(const nyrun_cf_t* cf, const char* file, size_t length, uint32_t argc,
957  const char** argv);
958 
959 NY_EXPORT int nyrun_filelist(const nyrun_cf_t* cf, const char** files, uint32_t file_count, uint32_t argc,
960  const char** argv);
964 #ifdef __cplusplus
965 }
966 #endif
967 
968 #endif /* __LIBNANYC_NANY_C_H__ */
Definition: nany.h:654
nybool_t(* create_st)(struct nyallocator_t *, const struct nyallocator_cf_t *)
Definition: nany.h:242
void(* on_internal_abort)(struct nyallocator_t *)
Definition: nany.h:263
Definition: nany.h:325
Definition: nany.h:358
nybool_t warnings_into_errors
Definition: nany.h:430
Definition: nany.h:680
const char * filename
Definition: nany.h:684
nyallocator_t allocator
Definition: nany.h:360
void * userdata
Definition: nany.h:438
void(* on_not_enough_memory)(struct nyallocator_t *, nybool_t limit_reached)
Definition: nany.h:244
const char * atom
Definition: nany.h:682
Definition: nany.h:739
Definition: nany.h:423
Adapter for a filesystem.
Definition: nany.h:564
nyallocator_t allocator
Definition: nany.h:700
uint32_t atom_size
Definition: nany.h:686
nyanystr_t entrypoint
Definition: nany.h:432
nybool_t ignore_atoms
Definition: nany.h:435
nyprogram_cf_t program
Definition: nany.h:899
nyio_cf_t io
Definition: nany.h:730
nyallocator_t allocator
Definition: nany.h:891
nyprogram_t * program
Definition: nany.h:743
size_t limit_mem_size
Definition: nany.h:259
nyconsole_t console
Definition: nany.h:893
void(* release)(struct nyallocator_t *)
Definition: nany.h:265
Definition: nany.h:248
nybool_t verbose
Definition: nany.h:901
nybool_t with_nsl_unittests
Definition: nany.h:366
nyconsole_t * console
Definition: nany.h:747
Definition: nany.h:117
Definition: nany.h:698
Definition: std.io.cpp:13
void * invalid_fd
Definition: nany.h:568
nyconsole_t console
Definition: nany.h:427
Definition: nany.h:237
nytctx_t * tctx
Definition: nany.h:745
nybool_t(* create_mt)(struct nyallocator_t *, const struct nyallocator_cf_t *)
Definition: nany.h:240
nyallocator_t * allocator
Definition: nany.h:741
volatile size_t reserved_mem0
Definition: nany.h:257
nybuild_cf_t build
Definition: nany.h:897
uint32_t automount_flags
Definition: nany.h:663
Definition: nany.h:889
nyproject_cf_t project
Definition: nany.h:895
nyconsole_t console
Definition: nany.h:702
uint32_t filename_size
Definition: nany.h:688
nyallocator_t allocator
Definition: nany.h:425
uint32_t line
Definition: nany.h:690
uint32_t column
Definition: nany.h:692
void(* deallocate)(struct nyallocator_t *, void *ptr, size_t)
Definition: nany.h:255
nyanystr_t entrypoint
Definition: nany.h:733