1 /*
2 * Copyright (c) 2019 Calvin Rose
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 
23 module janet.c;
24 
25 import core.stdc.stdarg;
26 import core.stdc.stddef;
27 import core.stdc.stdio;
28 
29 extern (C):
30 
31 /***** START SECTION CONFIG *****/
32 
33 enum JANET_BUILD = "local";
34 
35 /*
36  * Detect OS and endianess.
37  * From webkit source. There is likely some extreneous
38  * detection for unsupported platforms
39  */
40 
41 /* Check Unix */
42 
43 /* Darwin */
44 
45 /* GNU/Hurd */
46 
47 /* Solaris */
48 
49 /* Enable certain posix features */
50 
51 enum JANET_WINDOWS = 1;
52 
53 /* Check 64-bit vs 32-bit */ /* Windows 64 bit */
54 /* Itanium in LP64 mode */
55 /* DEC Alpha */
56 /* BE */
57 /* S390 64-bit (BE) */
58 
59 /* ARM 64-bit */
60 enum JANET_64 = 1;
61 
62 /* Check big endian */
63 /* MIPS 32-bit */
64 /* CPU(PPC) - PowerPC 32-bit */
65 
66 /* PowerPC 64-bit */
67 /* Sparc 32bit */
68 /* Sparc 64-bit */
69 /* S390 64-bit */
70 /* S390 32-bit */
71 /* ARM big endian */
72 /* ARM RealView compiler */
73 
74 enum JANET_LITTLE_ENDIAN = 1;
75 
76 /* Check emscripten */
77 
78 /* Define how global janet state is declared */
79 
80 /* Enable or disable dynamic module loading. Enabled by default. */
81 
82 /* Enable or disable the assembler. Enabled by default. */
83 
84 /* Enable or disable the peg module */
85 
86 /* Enable or disable the typedarray module */
87 
88 /* Enable or disable large int types (for now 64 bit, maybe 128 / 256 bit integer types) */
89 
90 /* How to export symbols */
91 
92 /* Tell complier some functions don't return */
93 
94 /* Prevent some recursive functions from recursing too deeply
95  * ands crashing (the parser). Instead, error out. */
96 enum JANET_RECURSION_GUARD = 1024;
97 
98 /* Maximum depth to follow table prototypes before giving up and returning nil. */
99 enum JANET_MAX_PROTO_DEPTH = 200;
100 
101 /* Maximum depth to follow table prototypes before giving up and returning nil. */
102 enum JANET_MAX_MACRO_EXPAND = 200;
103 
104 /* Define max stack size for stacks before raising a stack overflow error.
105  * If this is not defined, fiber stacks can grow without limit (until memory
106  * runs out) */
107 
108 enum JANET_STACK_MAX = 16384;
109 
110 /* Use nanboxed values - uses 8 bytes per value instead of 12 or 16.
111  * To turn of nanboxing, for debugging purposes or for certain
112  * architectures (Nanboxing only tested on x86 and x64), comment out
113  * the JANET_NANBOX define.*/
114 
115 /* We will only enable nanboxing by default on 64 bit systems
116  * on x86. This is mainly because the approach is tied to the
117  * implicit 47 bit address space. */
118 
119 /* Runtime config constants */
120 enum JANET_NANBOX_BIT = 0;
121 
122 enum JANET_SINGLE_THREADED_BIT = 0;
123 
124 enum JANET_CURRENT_CONFIG_BITS = JANET_SINGLE_THREADED_BIT | JANET_NANBOX_BIT;
125 
126 /* Represents the settings used to compile Janet, as well as the version */
127 struct JanetBuildConfig
128 {
129     uint major;
130     uint minor;
131     uint patch;
132     uint bits;
133 }
134 
135 /* Get config of current compilation unit. */
136 
137 /***** END SECTION CONFIG *****/
138 
139 /***** START SECTION TYPES *****/
140 
141 /* Names of all of the types */
142 extern __gshared const(char*)[16] janet_type_names;
143 extern __gshared const(char*)[14] janet_signal_names;
144 extern __gshared const(char*)[16] janet_status_names;
145 
146 /* Fiber signals */
147 enum JanetSignal
148 {
149     JANET_SIGNAL_OK = 0,
150     JANET_SIGNAL_ERROR = 1,
151     JANET_SIGNAL_DEBUG = 2,
152     JANET_SIGNAL_YIELD = 3,
153     JANET_SIGNAL_USER0 = 4,
154     JANET_SIGNAL_USER1 = 5,
155     JANET_SIGNAL_USER2 = 6,
156     JANET_SIGNAL_USER3 = 7,
157     JANET_SIGNAL_USER4 = 8,
158     JANET_SIGNAL_USER5 = 9,
159     JANET_SIGNAL_USER6 = 10,
160     JANET_SIGNAL_USER7 = 11,
161     JANET_SIGNAL_USER8 = 12,
162     JANET_SIGNAL_USER9 = 13
163 }
164 
165 /* Fiber statuses - mostly corresponds to signals. */
166 enum JanetFiberStatus
167 {
168     JANET_STATUS_DEAD = 0,
169     JANET_STATUS_ERROR = 1,
170     JANET_STATUS_DEBUG = 2,
171     JANET_STATUS_PENDING = 3,
172     JANET_STATUS_USER0 = 4,
173     JANET_STATUS_USER1 = 5,
174     JANET_STATUS_USER2 = 6,
175     JANET_STATUS_USER3 = 7,
176     JANET_STATUS_USER4 = 8,
177     JANET_STATUS_USER5 = 9,
178     JANET_STATUS_USER6 = 10,
179     JANET_STATUS_USER7 = 11,
180     JANET_STATUS_USER8 = 12,
181     JANET_STATUS_USER9 = 13,
182     JANET_STATUS_NEW = 14,
183     JANET_STATUS_ALIVE = 15
184 }
185 
186 /* Use type punning for GC objects */
187 
188 /* All of the primary Janet GCed types */
189 
190 /* Prefixed Janet types */
191 
192 /* Other structs */
193 alias JanetCFunction = extern(C) Janet function (int, Janet*);
194 
195 /* Basic types for all Janet Values */
196 enum JanetType
197 {
198     JANET_NUMBER = 0,
199     JANET_NIL = 1,
200     JANET_BOOLEAN = 2,
201     JANET_FIBER = 3,
202     JANET_STRING = 4,
203     JANET_SYMBOL = 5,
204     JANET_KEYWORD = 6,
205     JANET_ARRAY = 7,
206     JANET_TUPLE = 8,
207     JANET_TABLE = 9,
208     JANET_STRUCT = 10,
209     JANET_BUFFER = 11,
210     JANET_FUNCTION = 12,
211     JANET_CFUNCTION = 13,
212     JANET_ABSTRACT = 14,
213     JANET_POINTER = 15
214 }
215 
216 enum JANET_COUNT_TYPES = JanetType.JANET_POINTER + 1;
217 
218 /* Type flags */
219 enum JANET_TFLAG_NIL = 1 << JanetType.JANET_NIL;
220 enum JANET_TFLAG_BOOLEAN = 1 << JanetType.JANET_BOOLEAN;
221 enum JANET_TFLAG_FIBER = 1 << JanetType.JANET_FIBER;
222 enum JANET_TFLAG_NUMBER = 1 << JanetType.JANET_NUMBER;
223 enum JANET_TFLAG_STRING = 1 << JanetType.JANET_STRING;
224 enum JANET_TFLAG_SYMBOL = 1 << JanetType.JANET_SYMBOL;
225 enum JANET_TFLAG_KEYWORD = 1 << JanetType.JANET_KEYWORD;
226 enum JANET_TFLAG_ARRAY = 1 << JanetType.JANET_ARRAY;
227 enum JANET_TFLAG_TUPLE = 1 << JanetType.JANET_TUPLE;
228 enum JANET_TFLAG_TABLE = 1 << JanetType.JANET_TABLE;
229 enum JANET_TFLAG_STRUCT = 1 << JanetType.JANET_STRUCT;
230 enum JANET_TFLAG_BUFFER = 1 << JanetType.JANET_BUFFER;
231 enum JANET_TFLAG_FUNCTION = 1 << JanetType.JANET_FUNCTION;
232 enum JANET_TFLAG_CFUNCTION = 1 << JanetType.JANET_CFUNCTION;
233 enum JANET_TFLAG_ABSTRACT = 1 << JanetType.JANET_ABSTRACT;
234 enum JANET_TFLAG_POINTER = 1 << JanetType.JANET_POINTER;
235 
236 enum JANET_TFLAG_BYTES = JANET_TFLAG_STRING | JANET_TFLAG_SYMBOL | JANET_TFLAG_BUFFER | JANET_TFLAG_KEYWORD;
237 enum JANET_TFLAG_INDEXED = JANET_TFLAG_ARRAY | JANET_TFLAG_TUPLE;
238 enum JANET_TFLAG_DICTIONARY = JANET_TFLAG_TABLE | JANET_TFLAG_STRUCT;
239 enum JANET_TFLAG_LENGTHABLE = JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY;
240 enum JANET_TFLAG_CALLABLE = JANET_TFLAG_FUNCTION | JANET_TFLAG_CFUNCTION | JANET_TFLAG_LENGTHABLE | JANET_TFLAG_ABSTRACT;
241 
242 /* We provide three possible implementations of Janets. The preferred
243  * nanboxing approach, for 32 or 64 bits, and the standard C version. Code in the rest of the
244  * application must interact through exposed interface. */
245 
246 /* Required interface for Janet */
247 /* wrap and unwrap for all types */
248 /* Get type quickly */
249 /* Check against type quickly */
250 /* Small footprint */
251 /* 32 bit integer support */
252 
253 /* janet_type(x)
254  * janet_checktype(x, t)
255  * janet_wrap_##TYPE(x)
256  * janet_unwrap_##TYPE(x)
257  * janet_truthy(x)
258  * janet_memclear(p, n) - clear memory for hash tables to nils
259  * janet_u64(x) - get 64 bits of payload for hashing
260  */
261 
262 /***** START SECTION NON-C API *****/
263 
264 /* Some janet types use offset tricks to make operations easier in C. For
265  * external bindings, we should prefer using the Head structs directly, and
266  * use the host language to add sugar around the manipulation of the Janet types. */
267 
268 JanetStructHead* janet_struct_head (const(JanetKV)* st);
269 JanetAbstractHead* janet_abstract_head (const(void)* abstract_);
270 JanetStringHead* janet_string_head (const(ubyte)* s);
271 JanetTupleHead* janet_tuple_head (const(Janet)* tuple);
272 
273 /* Some language bindings won't have access to the macro versions. */
274 
275 JanetType janet_type (Janet x);
276 int janet_checktype (Janet x, JanetType type);
277 int janet_checktypes (Janet x, int typeflags);
278 int janet_truthy (Janet x);
279 
280 const(JanetKV)* janet_unwrap_struct (Janet x);
281 const(Janet)* janet_unwrap_tuple (Janet x);
282 JanetFiber* janet_unwrap_fiber (Janet x);
283 JanetArray* janet_unwrap_array (Janet x);
284 JanetTable* janet_unwrap_table (Janet x);
285 JanetBuffer* janet_unwrap_buffer (Janet x);
286 const(ubyte)* janet_unwrap_string (Janet x);
287 const(ubyte)* janet_unwrap_symbol (Janet x);
288 const(ubyte)* janet_unwrap_keyword (Janet x);
289 void* janet_unwrap_abstract (Janet x);
290 void* janet_unwrap_pointer (Janet x);
291 JanetFunction* janet_unwrap_function (Janet x);
292 JanetCFunction janet_unwrap_cfunction (Janet x);
293 int janet_unwrap_boolean (Janet x);
294 double janet_unwrap_number (Janet x);
295 int janet_unwrap_integer (Janet x);
296 
297 Janet janet_wrap_nil ();
298 Janet janet_wrap_number (double x);
299 Janet janet_wrap_true ();
300 Janet janet_wrap_false ();
301 Janet janet_wrap_boolean (int x);
302 Janet janet_wrap_string (const(ubyte)* x);
303 Janet janet_wrap_symbol (const(ubyte)* x);
304 Janet janet_wrap_keyword (const(ubyte)* x);
305 Janet janet_wrap_array (JanetArray* x);
306 Janet janet_wrap_tuple (const(Janet)* x);
307 Janet janet_wrap_struct (const(JanetKV)* x);
308 Janet janet_wrap_fiber (JanetFiber* x);
309 Janet janet_wrap_buffer (JanetBuffer* x);
310 Janet janet_wrap_function (JanetFunction* x);
311 Janet janet_wrap_cfunction (JanetCFunction x);
312 Janet janet_wrap_table (JanetTable* x);
313 Janet janet_wrap_abstract (void* x);
314 Janet janet_wrap_pointer (void* x);
315 Janet janet_wrap_integer (int x);
316 
317 /***** END SECTION NON-C API *****/
318 
319 /* 64 Nanboxed Janet value */
320 
321 /* Wrap the simple types */
322 
323 /* Unwrap the simple types */
324 
325 /* Wrap the pointer types */
326 
327 /* Unwrap the pointer types */
328 
329 /* 32 bit nanboxed janet */
330 
331 /* Wrap the pointer types */
332 
333 /* A general janet value type for more standard C */
334 struct Janet
335 {
336     union _Anonymous_0
337     {
338         ulong u64;
339         double number;
340         int integer;
341         void* pointer;
342         const(void)* cpointer;
343     }
344 
345     _Anonymous_0 as;
346     JanetType type;
347 }
348 
349 extern (D) auto janet_u64(T)(auto ref T x)
350 {
351     return x.as.u64;
352 }
353 
354 extern (D) auto janet_type(T)(auto ref T x)
355 {
356     return x.type;
357 }
358 
359 extern (D) auto janet_checktype(T0, T1)(auto ref T0 x, auto ref T1 t)
360 {
361     return x.type == t;
362 }
363 
364 extern (D) auto janet_truthy(T)(auto ref T x)
365 {
366     return x.type != JanetType.JANET_NIL && (x.type != JanetType.JANET_BOOLEAN || (x.as.integer & 0x1));
367 }
368 
369 extern (D) auto janet_unwrap_struct(T)(auto ref T x)
370 {
371     return cast(const(JanetKV)*) x.as.pointer;
372 }
373 
374 extern (D) auto janet_unwrap_tuple(T)(auto ref T x)
375 {
376     return cast(const(Janet)*) x.as.pointer;
377 }
378 
379 extern (D) auto janet_unwrap_fiber(T)(auto ref T x)
380 {
381     return cast(JanetFiber*) x.as.pointer;
382 }
383 
384 extern (D) auto janet_unwrap_array(T)(auto ref T x)
385 {
386     return cast(JanetArray*) x.as.pointer;
387 }
388 
389 extern (D) auto janet_unwrap_table(T)(auto ref T x)
390 {
391     return cast(JanetTable*) x.as.pointer;
392 }
393 
394 extern (D) auto janet_unwrap_buffer(T)(auto ref T x)
395 {
396     return cast(JanetBuffer*) x.as.pointer;
397 }
398 
399 extern (D) auto janet_unwrap_string(T)(auto ref T x)
400 {
401     return cast(const(ubyte)*) x.as.pointer;
402 }
403 
404 extern (D) auto janet_unwrap_symbol(T)(auto ref T x)
405 {
406     return cast(const(ubyte)*) x.as.pointer;
407 }
408 
409 extern (D) auto janet_unwrap_keyword(T)(auto ref T x)
410 {
411     return cast(const(ubyte)*) x.as.pointer;
412 }
413 
414 extern (D) auto janet_unwrap_abstract(T)(auto ref T x)
415 {
416     return x.as.pointer;
417 }
418 
419 extern (D) auto janet_unwrap_pointer(T)(auto ref T x)
420 {
421     return x.as.pointer;
422 }
423 
424 extern (D) auto janet_unwrap_function(T)(auto ref T x)
425 {
426     return cast(JanetFunction*) x.as.pointer;
427 }
428 
429 extern (D) auto janet_unwrap_cfunction(T)(auto ref T x)
430 {
431     return cast(JanetCFunction) x.as.pointer;
432 }
433 
434 extern (D) auto janet_unwrap_boolean(T)(auto ref T x)
435 {
436     return x.as.u64 & 0x1;
437 }
438 
439 extern (D) auto janet_unwrap_number(T)(auto ref T x)
440 {
441     return x.as.number;
442 }
443 
444 /* End of tagged union implementation */
445 
446 int janet_checkint (Janet x);
447 int janet_checkint64 (Janet x);
448 int janet_checksize (Janet x);
449 
450 extern (D) auto janet_checkintrange(T)(auto ref T x)
451 {
452     return x == cast(int) x;
453 }
454 
455 extern (D) auto janet_checkint64range(T)(auto ref T x)
456 {
457     return x == cast(long) x;
458 }
459 
460 extern (D) auto janet_unwrap_integer(T)(auto ref T x)
461 {
462     return cast(int) janet_unwrap_number(x);
463 }
464 
465 extern (D) auto janet_wrap_integer(T)(auto ref T x)
466 {
467     return janet_wrap_number(cast(int) x);
468 }
469 
470 extern (D) auto janet_checktypes(T0, T1)(auto ref T0 x, auto ref T1 tps)
471 {
472     return (1 << janet_type(x)) & tps;
473 }
474 
475 /* GC Object type pun. The lower 16 bits of flags are reserved for the garbage collector,
476  * but the upper 16 can be used per type for custom flags. The current collector is a linked
477  * list of blocks, which is naive but works. */
478 struct JanetGCObject
479 {
480     int flags;
481     JanetGCObject* next;
482 }
483 
484 /* Fiber signal masks. */
485 enum JANET_FIBER_MASK_ERROR = 2;
486 enum JANET_FIBER_MASK_DEBUG = 4;
487 enum JANET_FIBER_MASK_YIELD = 8;
488 
489 enum JANET_FIBER_MASK_USER0 = 16 << 0;
490 enum JANET_FIBER_MASK_USER1 = 16 << 1;
491 enum JANET_FIBER_MASK_USER2 = 16 << 2;
492 enum JANET_FIBER_MASK_USER3 = 16 << 3;
493 enum JANET_FIBER_MASK_USER4 = 16 << 4;
494 enum JANET_FIBER_MASK_USER5 = 16 << 5;
495 enum JANET_FIBER_MASK_USER6 = 16 << 6;
496 enum JANET_FIBER_MASK_USER7 = 16 << 7;
497 enum JANET_FIBER_MASK_USER8 = 16 << 8;
498 enum JANET_FIBER_MASK_USER9 = 16 << 9;
499 
500 extern (D) auto JANET_FIBER_MASK_USERN(T)(auto ref T N)
501 {
502     return 16 << N;
503 }
504 
505 enum JANET_FIBER_MASK_USER = 0x3FF0;
506 
507 enum JANET_FIBER_STATUS_MASK = 0xFF0000;
508 enum JANET_FIBER_STATUS_OFFSET = 16;
509 
510 /* A lightweight green thread in janet. Does not correspond to
511  * operating system threads. */
512 struct JanetFiber
513 {
514     JanetGCObject gc; /* GC Object stuff */
515     int flags; /* More flags */
516     int frame; /* Index of the stack frame */
517     int stackstart; /* Beginning of next args */
518     int stacktop; /* Top of stack. Where values are pushed and popped from. */
519     int capacity;
520     int maxstack; /* Arbitrary defined limit for stack overflow */
521     JanetTable* env; /* Dynamic bindings table (usually current environment). */
522     Janet* data;
523     JanetFiber* child; /* Keep linked list of fibers for restarting pending fibers */
524 }
525 
526 /* Mark if a stack frame is a tail call for debugging */
527 enum JANET_STACKFRAME_TAILCALL = 1;
528 
529 /* Mark if a stack frame is an entrance frame */
530 enum JANET_STACKFRAME_ENTRANCE = 2;
531 
532 /* A stack frame on the fiber. Is stored along with the stack values. */
533 struct JanetStackFrame
534 {
535     JanetFunction* func;
536     uint* pc;
537     JanetFuncEnv* env;
538     int prevframe;
539     int flags;
540 }
541 
542 /* Number of Janets a frame takes up in the stack */
543 enum JANET_FRAME_SIZE = (JanetStackFrame.sizeof + Janet.sizeof - 1) / Janet.sizeof;
544 
545 /* A dynamic array type. */
546 struct JanetArray
547 {
548     JanetGCObject gc;
549     int count;
550     int capacity;
551     Janet* data;
552 }
553 
554 /* A byte buffer type. Used as a mutable string or string builder. */
555 struct JanetBuffer
556 {
557     JanetGCObject gc;
558     int count;
559     int capacity;
560     ubyte* data;
561 }
562 
563 /* A mutable associative data type. Backed by a hashtable. */
564 struct JanetTable
565 {
566     JanetGCObject gc;
567     int count;
568     int capacity;
569     int deleted;
570     JanetKV* data;
571     JanetTable* proto;
572 }
573 
574 /* A key value pair in a struct or table */
575 struct JanetKV
576 {
577     Janet key;
578     Janet value;
579 }
580 
581 /* Prefix for a tuple */
582 struct JanetTupleHead
583 {
584     JanetGCObject gc;
585     int length;
586     int hash;
587     int sm_start;
588     int sm_end;
589     const(Janet)[] data;
590 }
591 
592 /* Prefix for a struct */
593 struct JanetStructHead
594 {
595     JanetGCObject gc;
596     int length;
597     int hash;
598     int capacity;
599     const(JanetKV)[] data;
600 }
601 
602 /* Prefix for a string */
603 struct JanetStringHead
604 {
605     JanetGCObject gc;
606     int length;
607     int hash;
608     const(ubyte)[] data;
609 }
610 
611 /* Prefix for an abstract value */
612 struct JanetAbstractHead
613 {
614     JanetGCObject gc;
615     const(JanetAbstractType)* type;
616     size_t size;
617     long[] data; /* Use long long to ensure most general alignment */
618 }
619 
620 /* Some function definition flags */
621 enum JANET_FUNCDEF_FLAG_VARARG = 0x10000;
622 enum JANET_FUNCDEF_FLAG_NEEDSENV = 0x20000;
623 enum JANET_FUNCDEF_FLAG_HASNAME = 0x80000;
624 enum JANET_FUNCDEF_FLAG_HASSOURCE = 0x100000;
625 enum JANET_FUNCDEF_FLAG_HASDEFS = 0x200000;
626 enum JANET_FUNCDEF_FLAG_HASENVS = 0x400000;
627 enum JANET_FUNCDEF_FLAG_HASSOURCEMAP = 0x800000;
628 enum JANET_FUNCDEF_FLAG_STRUCTARG = 0x1000000;
629 enum JANET_FUNCDEF_FLAG_TAG = 0xFFFF;
630 
631 /* Source mapping structure for a bytecode instruction */
632 struct JanetSourceMapping
633 {
634     int start;
635     int end;
636 }
637 
638 /* A function definition. Contains information needed to instantiate closures. */
639 struct JanetFuncDef
640 {
641     JanetGCObject gc;
642     int* environments; /* Which environments to capture from parent. */
643     Janet* constants;
644     JanetFuncDef** defs;
645     uint* bytecode;
646 
647     /* Various debug information */
648     JanetSourceMapping* sourcemap;
649     const(ubyte)* source;
650     const(ubyte)* name;
651 
652     int flags;
653     int slotcount; /* The amount of stack space required for the function */
654     int arity; /* Not including varargs */
655     int min_arity; /* Including varargs */
656     int max_arity; /* Including varargs */
657     int constants_length;
658     int bytecode_length;
659     int environments_length;
660     int defs_length;
661 }
662 
663 /* A function environment */
664 struct JanetFuncEnv
665 {
666     JanetGCObject gc;
667 
668     union _Anonymous_1
669     {
670         JanetFiber* fiber;
671         Janet* values;
672     }
673 
674     _Anonymous_1 as;
675     int length; /* Size of environment */
676     int offset; /* Stack offset when values still on stack. If offset is <= 0, then
677     environment is no longer on the stack. */
678 }
679 
680 enum JANET_FUNCFLAG_TRACE = 1 << 16;
681 
682 /* A function */
683 struct JanetFunction
684 {
685     JanetGCObject gc;
686     JanetFuncDef* def;
687     JanetFuncEnv*[] envs;
688 }
689 
690 struct JanetParseState;
691 
692 enum JanetParserStatus
693 {
694     JANET_PARSE_ROOT = 0,
695     JANET_PARSE_ERROR = 1,
696     JANET_PARSE_PENDING = 2,
697     JANET_PARSE_DEAD = 3
698 }
699 
700 /* A janet parser */
701 struct JanetParser
702 {
703     Janet* args;
704     const(char)* error;
705     JanetParseState* states;
706     ubyte* buf;
707     size_t argcount;
708     size_t argcap;
709     size_t statecount;
710     size_t statecap;
711     size_t bufcount;
712     size_t bufcap;
713     size_t offset;
714     size_t pending;
715     int lookback;
716     int flag;
717 }
718 
719 struct JanetMarshalContext
720 {
721     void* m_state; /* void* to not expose MarshalState ?*/
722     void* u_state;
723     int flags;
724     const(ubyte)* data;
725 }
726 
727 /* Defines an abstract type */
728 struct JanetAbstractType
729 {
730     const(char)* name;
731     int function (void* data, size_t len) gc;
732     int function (void* data, size_t len) gcmark;
733     Janet function (void* data, Janet key) get;
734     void function (void* data, Janet key, Janet value) put;
735     void function (void* p, JanetMarshalContext* ctx) marshal;
736     void function (void* p, JanetMarshalContext* ctx) unmarshal;
737     void function (void* p, JanetBuffer* buffer) tostring;
738 }
739 
740 struct JanetReg
741 {
742     const(char)* name;
743     JanetCFunction cfun;
744     const(char)* documentation;
745 }
746 
747 struct JanetMethod
748 {
749     const(char)* name;
750     JanetCFunction cfun;
751 }
752 
753 struct JanetView
754 {
755     const(Janet)* items;
756     int len;
757 }
758 
759 struct JanetByteView
760 {
761     const(ubyte)* bytes;
762     int len;
763 }
764 
765 struct JanetDictView
766 {
767     const(JanetKV)* kvs;
768     int len;
769     int cap;
770 }
771 
772 struct JanetRange
773 {
774     int start;
775     int end;
776 }
777 
778 /***** END SECTION TYPES *****/
779 
780 /***** START SECTION OPCODES *****/
781 
782 /* Bytecode op argument types */
783 enum JanetOpArgType
784 {
785     JANET_OAT_SLOT = 0,
786     JANET_OAT_ENVIRONMENT = 1,
787     JANET_OAT_CONSTANT = 2,
788     JANET_OAT_INTEGER = 3,
789     JANET_OAT_TYPE = 4,
790     JANET_OAT_SIMPLETYPE = 5,
791     JANET_OAT_LABEL = 6,
792     JANET_OAT_FUNCDEF = 7
793 }
794 
795 /* Various types of instructions */
796 enum JanetInstructionType
797 {
798     JINT_0 = 0, /* No args */
799     JINT_S = 1, /* Slot(3) */
800     JINT_L = 2, /* Label(3) */
801     JINT_SS = 3, /* Slot(1), Slot(2) */
802     JINT_SL = 4, /* Slot(1), Label(2) */
803     JINT_ST = 5, /* Slot(1), Slot(2) */
804     JINT_SI = 6, /* Slot(1), Immediate(2) */
805     JINT_SD = 7, /* Slot(1), Closure(2) */
806     JINT_SU = 8, /* Slot(1), Unsigned Immediate(2) */
807     JINT_SSS = 9, /* Slot(1), Slot(1), Slot(1) */
808     JINT_SSI = 10, /* Slot(1), Slot(1), Immediate(1) */
809     JINT_SSU = 11, /* Slot(1), Slot(1), Unsigned Immediate(1) */
810     JINT_SES = 12, /* Slot(1), Environment(1), Far Slot(1) */
811     JINT_SC = 13 /* Slot(1), Constant(2) */
812 }
813 
814 /* All opcodes for the bytecode interpreter. */
815 enum JanetOpCode
816 {
817     JOP_NOOP = 0,
818     JOP_ERROR = 1,
819     JOP_TYPECHECK = 2,
820     JOP_RETURN = 3,
821     JOP_RETURN_NIL = 4,
822     JOP_ADD_IMMEDIATE = 5,
823     JOP_ADD = 6,
824     JOP_SUBTRACT = 7,
825     JOP_MULTIPLY_IMMEDIATE = 8,
826     JOP_MULTIPLY = 9,
827     JOP_DIVIDE_IMMEDIATE = 10,
828     JOP_DIVIDE = 11,
829     JOP_BAND = 12,
830     JOP_BOR = 13,
831     JOP_BXOR = 14,
832     JOP_BNOT = 15,
833     JOP_SHIFT_LEFT = 16,
834     JOP_SHIFT_LEFT_IMMEDIATE = 17,
835     JOP_SHIFT_RIGHT = 18,
836     JOP_SHIFT_RIGHT_IMMEDIATE = 19,
837     JOP_SHIFT_RIGHT_UNSIGNED = 20,
838     JOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE = 21,
839     JOP_MOVE_FAR = 22,
840     JOP_MOVE_NEAR = 23,
841     JOP_JUMP = 24,
842     JOP_JUMP_IF = 25,
843     JOP_JUMP_IF_NOT = 26,
844     JOP_GREATER_THAN = 27,
845     JOP_GREATER_THAN_IMMEDIATE = 28,
846     JOP_LESS_THAN = 29,
847     JOP_LESS_THAN_IMMEDIATE = 30,
848     JOP_EQUALS = 31,
849     JOP_EQUALS_IMMEDIATE = 32,
850     JOP_COMPARE = 33,
851     JOP_LOAD_NIL = 34,
852     JOP_LOAD_TRUE = 35,
853     JOP_LOAD_FALSE = 36,
854     JOP_LOAD_INTEGER = 37,
855     JOP_LOAD_CONSTANT = 38,
856     JOP_LOAD_UPVALUE = 39,
857     JOP_LOAD_SELF = 40,
858     JOP_SET_UPVALUE = 41,
859     JOP_CLOSURE = 42,
860     JOP_PUSH = 43,
861     JOP_PUSH_2 = 44,
862     JOP_PUSH_3 = 45,
863     JOP_PUSH_ARRAY = 46,
864     JOP_CALL = 47,
865     JOP_TAILCALL = 48,
866     JOP_RESUME = 49,
867     JOP_SIGNAL = 50,
868     JOP_PROPAGATE = 51,
869     JOP_GET = 52,
870     JOP_PUT = 53,
871     JOP_GET_INDEX = 54,
872     JOP_PUT_INDEX = 55,
873     JOP_LENGTH = 56,
874     JOP_MAKE_ARRAY = 57,
875     JOP_MAKE_BUFFER = 58,
876     JOP_MAKE_STRING = 59,
877     JOP_MAKE_STRUCT = 60,
878     JOP_MAKE_TABLE = 61,
879     JOP_MAKE_TUPLE = 62,
880     JOP_MAKE_BRACKET_TUPLE = 63,
881     JOP_NUMERIC_LESS_THAN = 64,
882     JOP_NUMERIC_LESS_THAN_EQUAL = 65,
883     JOP_NUMERIC_GREATER_THAN = 66,
884     JOP_NUMERIC_GREATER_THAN_EQUAL = 67,
885     JOP_NUMERIC_EQUAL = 68,
886     JOP_INSTRUCTION_COUNT = 69
887 }
888 
889 /* Info about all instructions */
890 extern __gshared JanetInstructionType[JanetOpCode.JOP_INSTRUCTION_COUNT] janet_instructions;
891 
892 /***** END SECTION OPCODES *****/
893 
894 /***** START SECTION MAIN *****/
895 
896 /* Parsing */
897 void janet_parser_init (JanetParser* parser);
898 void janet_parser_deinit (JanetParser* parser);
899 void janet_parser_consume (JanetParser* parser, ubyte c);
900 JanetParserStatus janet_parser_status (JanetParser* parser);
901 Janet janet_parser_produce (JanetParser* parser);
902 const(char)* janet_parser_error (JanetParser* parser);
903 void janet_parser_flush (JanetParser* parser);
904 void janet_parser_eof (JanetParser* parser);
905 int janet_parser_has_more (JanetParser* parser);
906 
907 /* Assembly */
908 enum JanetAssembleStatus
909 {
910     JANET_ASSEMBLE_OK = 0,
911     JANET_ASSEMBLE_ERROR = 1
912 }
913 
914 struct JanetAssembleResult
915 {
916     JanetFuncDef* funcdef;
917     const(ubyte)* error;
918     JanetAssembleStatus status;
919 }
920 
921 JanetAssembleResult janet_asm (Janet source, int flags);
922 Janet janet_disasm (JanetFuncDef* def);
923 Janet janet_asm_decode_instruction (uint instr);
924 
925 /* Compilation */
926 enum JanetCompileStatus
927 {
928     JANET_COMPILE_OK = 0,
929     JANET_COMPILE_ERROR = 1
930 }
931 
932 struct JanetCompileResult
933 {
934     JanetFuncDef* funcdef;
935     const(ubyte)* error;
936     JanetFiber* macrofiber;
937     JanetSourceMapping error_mapping;
938     JanetCompileStatus status;
939 }
940 
941 JanetCompileResult janet_compile (Janet source, JanetTable* env, const(ubyte)* where);
942 
943 /* Get the default environment for janet */
944 JanetTable* janet_core_env (JanetTable* replacements);
945 
946 int janet_dobytes (JanetTable* env, const(ubyte)* bytes, int len, const(char)* sourcePath, Janet* out_);
947 int janet_dostring (JanetTable* env, const(char)* str, const(char)* sourcePath, Janet* out_);
948 
949 /* Number scanning */
950 int janet_scan_number (const(ubyte)* str, int len, double* out_);
951 int janet_scan_int64 (const(ubyte)* str, int len, long* out_);
952 int janet_scan_uint64 (const(ubyte)* str, int len, ulong* out_);
953 
954 /* Debugging */
955 void janet_debug_break (JanetFuncDef* def, int pc);
956 void janet_debug_unbreak (JanetFuncDef* def, int pc);
957 void janet_debug_find (
958     JanetFuncDef** def_out,
959     int* pc_out,
960     const(ubyte)* source,
961     int offset);
962 
963 /* Array functions */
964 JanetArray* janet_array (int capacity);
965 JanetArray* janet_array_n (const(Janet)* elements, int n);
966 void janet_array_ensure (JanetArray* array, int capacity, int growth);
967 void janet_array_setcount (JanetArray* array, int count);
968 void janet_array_push (JanetArray* array, Janet x);
969 Janet janet_array_pop (JanetArray* array);
970 Janet janet_array_peek (JanetArray* array);
971 
972 /* Buffer functions */
973 JanetBuffer* janet_buffer (int capacity);
974 JanetBuffer* janet_buffer_init (JanetBuffer* buffer, int capacity);
975 void janet_buffer_deinit (JanetBuffer* buffer);
976 void janet_buffer_ensure (JanetBuffer* buffer, int capacity, int growth);
977 void janet_buffer_setcount (JanetBuffer* buffer, int count);
978 void janet_buffer_extra (JanetBuffer* buffer, int n);
979 void janet_buffer_push_bytes (JanetBuffer* buffer, const(ubyte)* string, int len);
980 void janet_buffer_push_string (JanetBuffer* buffer, const(ubyte)* string);
981 void janet_buffer_push_cstring (JanetBuffer* buffer, const(char)* cstring);
982 void janet_buffer_push_u8 (JanetBuffer* buffer, ubyte x);
983 void janet_buffer_push_u16 (JanetBuffer* buffer, ushort x);
984 void janet_buffer_push_u32 (JanetBuffer* buffer, uint x);
985 void janet_buffer_push_u64 (JanetBuffer* buffer, ulong x);
986 
987 /* Tuple */
988 
989 enum JANET_TUPLE_FLAG_BRACKETCTOR = 0x10000;
990 
991 extern (D) auto janet_tuple_head(T)(auto ref T t)
992 {
993     return cast(JanetTupleHead*) cast(char*) t - offsetof(JanetTupleHead, data);
994 }
995 
996 extern (D) auto janet_tuple_length(T)(auto ref T t)
997 {
998     return janet_tuple_head(t).length;
999 }
1000 
1001 extern (D) auto janet_tuple_hash(T)(auto ref T t)
1002 {
1003     return janet_tuple_head(t).hash;
1004 }
1005 
1006 extern (D) auto janet_tuple_sm_start(T)(auto ref T t)
1007 {
1008     return janet_tuple_head(t).sm_start;
1009 }
1010 
1011 extern (D) auto janet_tuple_sm_end(T)(auto ref T t)
1012 {
1013     return janet_tuple_head(t).sm_end;
1014 }
1015 
1016 extern (D) auto janet_tuple_flag(T)(auto ref T t)
1017 {
1018     return janet_tuple_head(t).gc.flags;
1019 }
1020 
1021 Janet* janet_tuple_begin (int length);
1022 const(Janet)* janet_tuple_end (Janet* tuple);
1023 const(Janet)* janet_tuple_n (const(Janet)* values, int n);
1024 int janet_tuple_equal (const(Janet)* lhs, const(Janet)* rhs);
1025 int janet_tuple_compare (const(Janet)* lhs, const(Janet)* rhs);
1026 
1027 /* String/Symbol functions */
1028 extern (D) auto janet_string_head(T)(auto ref T s)
1029 {
1030     return cast(JanetStringHead*) cast(char*) s - offsetof(JanetStringHead, data);
1031 }
1032 
1033 extern (D) auto janet_string_length(T)(auto ref T s)
1034 {
1035     return janet_string_head(s).length;
1036 }
1037 
1038 extern (D) auto janet_string_hash(T)(auto ref T s)
1039 {
1040     return janet_string_head(s).hash;
1041 }
1042 
1043 ubyte* janet_string_begin (int length);
1044 const(ubyte)* janet_string_end (ubyte* str);
1045 const(ubyte)* janet_string (const(ubyte)* buf, int len);
1046 const(ubyte)* janet_cstring (const(char)* cstring);
1047 int janet_string_compare (const(ubyte)* lhs, const(ubyte)* rhs);
1048 int janet_string_equal (const(ubyte)* lhs, const(ubyte)* rhs);
1049 int janet_string_equalconst (const(ubyte)* lhs, const(ubyte)* rhs, int rlen, int rhash);
1050 const(ubyte)* janet_description (Janet x);
1051 const(ubyte)* janet_to_string (Janet x);
1052 void janet_to_string_b (JanetBuffer* buffer, Janet x);
1053 void janet_description_b (JanetBuffer* buffer, Janet x);
1054 
1055 extern (D) auto janet_cstringv(T)(auto ref T cstr)
1056 {
1057     return janet_wrap_string(janet_cstring(cstr));
1058 }
1059 
1060 extern (D) auto janet_stringv(T0, T1)(auto ref T0 str, auto ref T1 len)
1061 {
1062     return janet_wrap_string(janet_string(str, len));
1063 }
1064 
1065 const(ubyte)* janet_formatc (const(char)* format, ...);
1066 void janet_formatb (JanetBuffer* bufp, const(char)* format, va_list args);
1067 
1068 /* Symbol functions */
1069 const(ubyte)* janet_symbol (const(ubyte)* str, int len);
1070 const(ubyte)* janet_csymbol (const(char)* str);
1071 const(ubyte)* janet_symbol_gen ();
1072 
1073 extern (D) auto janet_symbolv(T0, T1)(auto ref T0 str, auto ref T1 len)
1074 {
1075     return janet_wrap_symbol(janet_symbol(str, len));
1076 }
1077 
1078 extern (D) auto janet_csymbolv(T)(auto ref T cstr)
1079 {
1080     return janet_wrap_symbol(janet_csymbol(cstr));
1081 }
1082 
1083 /* Keyword functions */
1084 alias janet_keyword = janet_symbol;
1085 alias janet_ckeyword = janet_csymbol;
1086 
1087 extern (D) auto janet_keywordv(T0, T1)(auto ref T0 str, auto ref T1 len)
1088 {
1089     return janet_wrap_keyword(janet_keyword());
1090 }
1091 
1092 extern (D) auto janet_ckeywordv(T)(auto ref T cstr)
1093 {
1094     return janet_wrap_keyword(janet_ckeyword());
1095 }
1096 
1097 /* Structs */
1098 extern (D) auto janet_struct_head(T)(auto ref T t)
1099 {
1100     return cast(JanetStructHead*) cast(char*) t - offsetof(JanetStructHead, data);
1101 }
1102 
1103 extern (D) auto janet_struct_length(T)(auto ref T t)
1104 {
1105     return janet_struct_head(t).length;
1106 }
1107 
1108 extern (D) auto janet_struct_capacity(T)(auto ref T t)
1109 {
1110     return janet_struct_head(t).capacity;
1111 }
1112 
1113 extern (D) auto janet_struct_hash(T)(auto ref T t)
1114 {
1115     return janet_struct_head(t).hash;
1116 }
1117 
1118 JanetKV* janet_struct_begin (int count);
1119 void janet_struct_put (JanetKV* st, Janet key, Janet value);
1120 const(JanetKV)* janet_struct_end (JanetKV* st);
1121 Janet janet_struct_get (const(JanetKV)* st, Janet key);
1122 JanetTable* janet_struct_to_table (const(JanetKV)* st);
1123 int janet_struct_equal (const(JanetKV)* lhs, const(JanetKV)* rhs);
1124 int janet_struct_compare (const(JanetKV)* lhs, const(JanetKV)* rhs);
1125 const(JanetKV)* janet_struct_find (const(JanetKV)* st, Janet key);
1126 
1127 /* Table functions */
1128 JanetTable* janet_table (int capacity);
1129 JanetTable* janet_table_init (JanetTable* table, int capacity);
1130 void janet_table_deinit (JanetTable* table);
1131 Janet janet_table_get (JanetTable* t, Janet key);
1132 Janet janet_table_rawget (JanetTable* t, Janet key);
1133 Janet janet_table_remove (JanetTable* t, Janet key);
1134 void janet_table_put (JanetTable* t, Janet key, Janet value);
1135 const(JanetKV)* janet_table_to_struct (JanetTable* t);
1136 void janet_table_merge_table (JanetTable* table, JanetTable* other);
1137 void janet_table_merge_struct (JanetTable* table, const(JanetKV)* other);
1138 JanetKV* janet_table_find (JanetTable* t, Janet key);
1139 JanetTable* janet_table_clone (JanetTable* table);
1140 
1141 /* Fiber */
1142 JanetFiber* janet_fiber (JanetFunction* callee, int capacity, int argc, const(Janet)* argv);
1143 JanetFiber* janet_fiber_reset (JanetFiber* fiber, JanetFunction* callee, int argc, const(Janet)* argv);
1144 JanetFiberStatus janet_fiber_status (JanetFiber* fiber);
1145 JanetFiber* janet_current_fiber ();
1146 
1147 /* Treat similar types through uniform interfaces for iteration */
1148 int janet_indexed_view (Janet seq, const(Janet*)* data, int* len);
1149 int janet_bytes_view (Janet str, const(ubyte*)* data, int* len);
1150 int janet_dictionary_view (Janet tab, const(JanetKV*)* data, int* len, int* cap);
1151 Janet janet_dictionary_get (const(JanetKV)* data, int cap, Janet key);
1152 const(JanetKV)* janet_dictionary_next (const(JanetKV)* kvs, int cap, const(JanetKV)* kv);
1153 
1154 /* Abstract */
1155 extern (D) auto janet_abstract_head(T)(auto ref T u)
1156 {
1157     return cast(JanetAbstractHead*) cast(char*) u - offsetof(JanetAbstractHead, data);
1158 }
1159 
1160 extern (D) auto janet_abstract_type(T)(auto ref T u)
1161 {
1162     return janet_abstract_head(u).type;
1163 }
1164 
1165 extern (D) auto janet_abstract_size(T)(auto ref T u)
1166 {
1167     return janet_abstract_head(u).size;
1168 }
1169 
1170 void* janet_abstract_begin (const(JanetAbstractType)* type, size_t size);
1171 void* janet_abstract_end (void*);
1172 void* janet_abstract (const(JanetAbstractType)* type, size_t size); /* begin and end in one call */
1173 
1174 /* Native */
1175 alias JanetModule = void function (JanetTable*);
1176 alias JanetModconf = JanetBuildConfig function ();
1177 JanetModule janet_native (const(char)* name, const(ubyte*)* error);
1178 
1179 /* Marshaling */
1180 void janet_marshal (JanetBuffer* buf, Janet x, JanetTable* rreg, int flags);
1181 Janet janet_unmarshal (
1182     const(ubyte)* bytes,
1183     size_t len,
1184     int flags,
1185     JanetTable* reg,
1186     const(ubyte*)* next);
1187 JanetTable* janet_env_lookup (JanetTable* env);
1188 void janet_env_lookup_into (JanetTable* renv, JanetTable* env, const(char)* prefix, int recurse);
1189 
1190 /* GC */
1191 void janet_mark (Janet x);
1192 void janet_sweep ();
1193 void janet_collect ();
1194 void janet_clear_memory ();
1195 void janet_gcroot (Janet root);
1196 int janet_gcunroot (Janet root);
1197 int janet_gcunrootall (Janet root);
1198 int janet_gclock ();
1199 void janet_gcunlock (int handle);
1200 
1201 /* Functions */
1202 JanetFuncDef* janet_funcdef_alloc ();
1203 JanetFunction* janet_thunk (JanetFuncDef* def);
1204 int janet_verify (JanetFuncDef* def);
1205 
1206 /* Pretty printing */
1207 enum JANET_PRETTY_COLOR = 1;
1208 JanetBuffer* janet_pretty (JanetBuffer* buffer, int depth, int flags, Janet x);
1209 
1210 /* Misc */
1211 int janet_equals (Janet x, Janet y);
1212 int janet_hash (Janet x);
1213 int janet_compare (Janet x, Janet y);
1214 int janet_cstrcmp (const(ubyte)* str, const(char)* other);
1215 Janet janet_get (Janet ds, Janet key);
1216 Janet janet_getindex (Janet ds, int index);
1217 int janet_length (Janet x);
1218 void janet_put (Janet ds, Janet key, Janet value);
1219 void janet_putindex (Janet ds, int index, Janet value);
1220 ulong janet_getflags (const(Janet)* argv, int n, const(char)* flags);
1221 
1222 extern (D) auto janet_flag_at(T0, T1)(auto ref T0 F, auto ref T1 I)
1223 {
1224     return F & ((1) << I);
1225 }
1226 
1227 Janet janet_wrap_number_safe (double x);
1228 
1229 /* VM functions */
1230 int janet_init ();
1231 void janet_deinit ();
1232 JanetSignal janet_continue (JanetFiber* fiber, Janet in_, Janet* out_);
1233 JanetSignal janet_pcall (JanetFunction* fun, int argn, const(Janet)* argv, Janet* out_, JanetFiber** f);
1234 Janet janet_call (JanetFunction* fun, int argc, const(Janet)* argv);
1235 void janet_stacktrace (JanetFiber* fiber, Janet err);
1236 
1237 /* Scratch Memory API */
1238 void* janet_smalloc (size_t size);
1239 void* janet_srealloc (void* mem, size_t size);
1240 void janet_sfree (void* mem);
1241 
1242 /* C Library helpers */
1243 enum JanetBindingType
1244 {
1245     JANET_BINDING_NONE = 0,
1246     JANET_BINDING_DEF = 1,
1247     JANET_BINDING_VAR = 2,
1248     JANET_BINDING_MACRO = 3
1249 }
1250 
1251 void janet_def (JanetTable* env, const(char)* name, Janet val, const(char)* documentation);
1252 void janet_var (JanetTable* env, const(char)* name, Janet val, const(char)* documentation);
1253 void janet_cfuns (JanetTable* env, const(char)* regprefix, const(JanetReg)* cfuns);
1254 JanetBindingType janet_resolve (JanetTable* env, const(ubyte)* sym, Janet* out_);
1255 void janet_register (const(char)* name, JanetCFunction cfun);
1256 
1257 /* New C API */
1258 
1259 /* Allow setting entry name for static libraries */
1260 
1261 alias JANET_ENTRY_NAME = janet_init;
1262 
1263 void janet_panicv (Janet message);
1264 void janet_panic (const(char)* message);
1265 void janet_panics (const(ubyte)* message);
1266 void janet_panicf (const(char)* format, ...);
1267 void janet_printf (const(char)* format, ...);
1268 void janet_panic_type (Janet x, int n, int expected);
1269 void janet_panic_abstract (Janet x, int n, const(JanetAbstractType)* at);
1270 void janet_arity (int arity, int min, int max);
1271 void janet_fixarity (int arity, int fix);
1272 
1273 Janet janet_getmethod (const(ubyte)* method, const(JanetMethod)* methods);
1274 double janet_getnumber (const(Janet)* argv, int n);
1275 JanetArray* janet_getarray (const(Janet)* argv, int n);
1276 const(Janet)* janet_gettuple (const(Janet)* argv, int n);
1277 JanetTable* janet_gettable (const(Janet)* argv, int n);
1278 const(JanetKV)* janet_getstruct (const(Janet)* argv, int n);
1279 const(ubyte)* janet_getstring (const(Janet)* argv, int n);
1280 const(char)* janet_getcstring (const(Janet)* argv, int n);
1281 const(ubyte)* janet_getsymbol (const(Janet)* argv, int n);
1282 const(ubyte)* janet_getkeyword (const(Janet)* argv, int n);
1283 JanetBuffer* janet_getbuffer (const(Janet)* argv, int n);
1284 JanetFiber* janet_getfiber (const(Janet)* argv, int n);
1285 JanetFunction* janet_getfunction (const(Janet)* argv, int n);
1286 JanetCFunction janet_getcfunction (const(Janet)* argv, int n);
1287 int janet_getboolean (const(Janet)* argv, int n);
1288 void* janet_getpointer (const(Janet)* argv, int n);
1289 
1290 int janet_getinteger (const(Janet)* argv, int n);
1291 long janet_getinteger64 (const(Janet)* argv, int n);
1292 size_t janet_getsize (const(Janet)* argv, int n);
1293 JanetView janet_getindexed (const(Janet)* argv, int n);
1294 JanetByteView janet_getbytes (const(Janet)* argv, int n);
1295 JanetDictView janet_getdictionary (const(Janet)* argv, int n);
1296 void* janet_getabstract (const(Janet)* argv, int n, const(JanetAbstractType)* at);
1297 JanetRange janet_getslice (int argc, const(Janet)* argv);
1298 int janet_gethalfrange (const(Janet)* argv, int n, int length, const(char)* which);
1299 int janet_getargindex (const(Janet)* argv, int n, int length, const(char)* which);
1300 
1301 Janet janet_dyn (const(char)* name);
1302 void janet_setdyn (const(char)* name, Janet value);
1303 
1304 FILE* janet_getfile (const(Janet)* argv, int n, int* flags);
1305 FILE* janet_dynfile (const(char)* name, FILE* def);
1306 
1307 /* Marshal API */
1308 void janet_marshal_size (JanetMarshalContext* ctx, size_t value);
1309 void janet_marshal_int (JanetMarshalContext* ctx, int value);
1310 void janet_marshal_int64 (JanetMarshalContext* ctx, long value);
1311 void janet_marshal_byte (JanetMarshalContext* ctx, ubyte value);
1312 void janet_marshal_bytes (JanetMarshalContext* ctx, const(ubyte)* bytes, size_t len);
1313 void janet_marshal_janet (JanetMarshalContext* ctx, Janet x);
1314 
1315 size_t janet_unmarshal_size (JanetMarshalContext* ctx);
1316 int janet_unmarshal_int (JanetMarshalContext* ctx);
1317 long janet_unmarshal_int64 (JanetMarshalContext* ctx);
1318 ubyte janet_unmarshal_byte (JanetMarshalContext* ctx);
1319 void janet_unmarshal_bytes (JanetMarshalContext* ctx, ubyte* dest, size_t len);
1320 Janet janet_unmarshal_janet (JanetMarshalContext* ctx);
1321 
1322 void janet_register_abstract_type (const(JanetAbstractType)* at);
1323 const(JanetAbstractType)* janet_get_abstract_type (Janet key);
1324 
1325 enum JanetTArrayType
1326 {
1327     JANET_TARRAY_TYPE_U8 = 0,
1328     JANET_TARRAY_TYPE_S8 = 1,
1329     JANET_TARRAY_TYPE_U16 = 2,
1330     JANET_TARRAY_TYPE_S16 = 3,
1331     JANET_TARRAY_TYPE_U32 = 4,
1332     JANET_TARRAY_TYPE_S32 = 5,
1333     JANET_TARRAY_TYPE_U64 = 6,
1334     JANET_TARRAY_TYPE_S64 = 7,
1335     JANET_TARRAY_TYPE_F32 = 8,
1336     JANET_TARRAY_TYPE_F64 = 9
1337 }
1338 
1339 struct JanetTArrayBuffer
1340 {
1341     ubyte* data;
1342     size_t size;
1343     int flags;
1344 }
1345 
1346 struct JanetTArrayView
1347 {
1348     union _Anonymous_2
1349     {
1350         void* pointer;
1351         ubyte* u8;
1352         byte* s8;
1353         ushort* u16;
1354         short* s16;
1355         uint* u32;
1356         int* s32;
1357         ulong* u64;
1358         long* s64;
1359         float* f32;
1360         double* f64;
1361     }
1362 
1363     _Anonymous_2 as;
1364     JanetTArrayBuffer* buffer;
1365     size_t size;
1366     size_t stride;
1367     JanetTArrayType type;
1368 }
1369 
1370 JanetTArrayBuffer* janet_tarray_buffer (size_t size);
1371 JanetTArrayView* janet_tarray_view (JanetTArrayType type, size_t size, size_t stride, size_t offset, JanetTArrayBuffer* buffer);
1372 int janet_is_tarray_view (Janet x, JanetTArrayType type);
1373 JanetTArrayBuffer* janet_gettarray_buffer (const(Janet)* argv, int n);
1374 JanetTArrayView* janet_gettarray_view (const(Janet)* argv, int n, JanetTArrayType type);
1375 JanetTArrayView* janet_gettarray_any (const(Janet)* argv, int n);
1376 
1377 enum JanetIntType
1378 {
1379     JANET_INT_NONE = 0,
1380     JANET_INT_S64 = 1,
1381     JANET_INT_U64 = 2
1382 }
1383 
1384 JanetIntType janet_is_int (Janet x);
1385 Janet janet_wrap_s64 (long x);
1386 Janet janet_wrap_u64 (ulong x);
1387 long janet_unwrap_s64 (Janet x);
1388 ulong janet_unwrap_u64 (Janet x);
1389 int janet_scan_int64 (const(ubyte)* str, int len, long* out_);
1390 int janet_scan_uint64 (const(ubyte)* str, int len, ulong* out_);
1391 
1392 /***** END SECTION MAIN *****/
1393 
1394 /* JANET_H_defined */