1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Add function versions of macro API bindings.

This should help address #81. Also hide janet_exit
and janet_assert, as they are really meant for internal usage.
I have not verified that this yet actually works with Rust's
bindgen.
This commit is contained in:
Calvin Rose 2019-04-27 15:45:28 -04:00
parent 73b81e0253
commit f10028d41a
12 changed files with 216 additions and 64 deletions

View File

@ -23,6 +23,7 @@
#ifndef JANET_AMALG
#include <janet.h>
#include "gc.h"
#include "util.h"
#endif
/* Look up table for instructions */

View File

@ -36,6 +36,34 @@ void janet_panicv(Janet message) {
}
}
void janet_panicf(const char *format, ...) {
va_list args;
const uint8_t *ret;
JanetBuffer buffer;
int32_t len = 0;
while (format[len]) len++;
janet_buffer_init(&buffer, len);
va_start(args, format);
janet_formatb(&buffer, format, args);
va_end(args);
ret = janet_string(buffer.data, buffer.count);
janet_buffer_deinit(&buffer);
janet_panics(ret);
}
void janet_printf(const char *format, ...) {
va_list args;
JanetBuffer buffer;
int32_t len = 0;
while (format[len]) len++;
janet_buffer_init(&buffer, len);
va_start(args, format);
janet_formatb(&buffer, format, args);
va_end(args);
fwrite(buffer.data, buffer.count, 1, stdout);
janet_buffer_deinit(&buffer);
}
void janet_panic(const char *message) {
janet_panicv(janet_cstringv(message));
}
@ -221,3 +249,21 @@ void janet_setdyn(const char *name, Janet value) {
}
janet_table_put(janet_vm_fiber->env, janet_ckeywordv(name), value);
}
/* Some definitions for function-like macros */
JANET_API JanetStructHead *(janet_struct_head)(const JanetKV *st) {
return janet_struct_head(st);
}
JANET_API JanetAbstractHead *(janet_abstract_head)(const void *abstract) {
return janet_abstract_head(abstract);
}
JANET_API JanetStringHead *(janet_string_head)(const uint8_t *s) {
return janet_string_head(s);
}
JANET_API JanetTupleHead *(janet_tuple_head)(const Janet *tuple) {
return janet_tuple_head(tuple);
}

View File

@ -292,6 +292,10 @@ void janet_fiber_popframe(JanetFiber *fiber) {
fiber->frame = frame->prevframe;
}
JanetFiberStatus janet_fiber_status(JanetFiber *f) {
return ((f)->flags & JANET_FIBER_STATUS_MASK) >> JANET_FIBER_STATUS_OFFSET;
}
/* CFuns */
static Janet cfun_fiber_getenv(int32_t argc, Janet *argv) {
@ -369,8 +373,7 @@ static Janet cfun_fiber_new(int32_t argc, Janet *argv) {
static Janet cfun_fiber_status(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JanetFiber *fiber = janet_getfiber(argv, 0);
uint32_t s = (fiber->flags & JANET_FIBER_STATUS_MASK) >>
JANET_FIBER_STATUS_OFFSET;
uint32_t s = janet_fiber_status(fiber);
return janet_ckeywordv(janet_status_names[s]);
}

View File

@ -25,6 +25,7 @@
#include "state.h"
#include "symcache.h"
#include "gc.h"
#include "util.h"
#endif
/* GC State */
@ -107,11 +108,11 @@ static void janet_mark_buffer(JanetBuffer *buffer) {
}
static void janet_mark_abstract(void *adata) {
if (janet_gc_reachable(janet_abstract_header(adata)))
if (janet_gc_reachable(janet_abstract_head(adata)))
return;
janet_gc_mark(janet_abstract_header(adata));
if (janet_abstract_header(adata)->type->gcmark) {
janet_abstract_header(adata)->type->gcmark(adata, janet_abstract_size(adata));
janet_gc_mark(janet_abstract_head(adata));
if (janet_abstract_head(adata)->type->gcmark) {
janet_abstract_head(adata)->type->gcmark(adata, janet_abstract_size(adata));
}
}

View File

@ -291,20 +291,24 @@ static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags) {
marshal_one(st, janet_wrap_fiber(fiber->child), flags + 1);
}
void janet_marshal_size(JanetMarshalContext *ctx, size_t value) {
janet_marshal_int64(ctx, (int64_t) value);
}
void janet_marshal_int64(JanetMarshalContext *ctx, int64_t value) {
MarshalState *st = (MarshalState *)(ctx->m_state);
push64(st, (uint64_t) value);
};
}
void janet_marshal_int(JanetMarshalContext *ctx, int32_t value) {
MarshalState *st = (MarshalState *)(ctx->m_state);
pushint(st, value);
};
}
void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value) {
MarshalState *st = (MarshalState *)(ctx->m_state);
pushbyte(st, value);
};
}
void janet_marshal_bytes(JanetMarshalContext *ctx, const uint8_t *bytes, size_t len) {
MarshalState *st = (MarshalState *)(ctx->m_state);
@ -967,18 +971,22 @@ static const uint8_t *unmarshal_one_fiber(
int32_t janet_unmarshal_int(JanetMarshalContext *ctx) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
return readint(st, &(ctx->data));
};
}
size_t janet_unmarshal_size(JanetMarshalContext *ctx) {
return (size_t) janet_unmarshal_int64(ctx);
}
int64_t janet_unmarshal_int64(JanetMarshalContext *ctx) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
return read64(st, &(ctx->data));
};
}
uint8_t janet_unmarshal_byte(JanetMarshalContext *ctx) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
MARSH_EOS(st, ctx->data);
return *(ctx->data++);
};
}
void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, size_t len) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);

View File

@ -634,6 +634,10 @@ void janet_parser_deinit(JanetParser *parser) {
free(parser->states);
}
int janet_parser_has_more(JanetParser *parser) {
return parser->pending;
}
/* C functions */
static int parsermark(void *p, size_t size) {

View File

@ -23,6 +23,7 @@
#ifndef JANET_AMALG
#include <janet.h>
#include "regalloc.h"
#include "util.h"
#endif
void janetc_regalloc_init(JanetcRegisterAllocator *ra) {

View File

@ -45,6 +45,7 @@
#ifndef JANET_AMALG
#include <janet.h>
#include "util.h"
#endif
/* Lookup table for getting values of characters when parsing numbers. Handles

View File

@ -27,6 +27,28 @@
#include <janet.h>
#endif
/* Handle runtime errors */
#ifndef janet_exit
#include <stdio.h>
#define janet_exit(m) do { \
printf("C runtime error at line %d in file %s: %s\n",\
__LINE__,\
__FILE__,\
(m));\
exit(1);\
} while (0)
#endif
#define janet_assert(c, m) do { \
if (!(c)) janet_exit((m)); \
} while (0)
/* What to do when out of memory */
#ifndef JANET_OUT_OF_MEMORY
#include <stdio.h>
#define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0)
#endif
/* Omit docstrings in some builds */
#ifndef JANET_BOOTSTRAP
#define JDOC(x) NULL

View File

@ -22,6 +22,7 @@
#ifndef JANET_AMALG
#include "vector.h"
#include "util.h"
#endif
/* Grow the buffer dynamically. Used for push operations. */

View File

@ -22,8 +22,57 @@
#ifndef JANET_AMALG
#include <janet.h>
#include "util.h"
#endif
/* Macro fills */
JanetType (janet_type)(Janet x) { return janet_type(x); }
int (janet_checktype)(Janet x, JanetType type) { return janet_checktype(x, type); }
int (janet_checktypes)(Janet x, int typeflags) { return janet_checktypes(x, typeflags); }
int (janet_truthy)(Janet x) { return janet_truthy(x); }
const JanetKV *(janet_unwrap_struct)(Janet x) { return janet_unwrap_struct(x); }
const Janet *(janet_unwrap_tuple)(Janet x) { return janet_unwrap_tuple(x); }
JanetFiber *(janet_unwrap_fiber)(Janet x) { return janet_unwrap_fiber(x); }
JanetArray *(janet_unwrap_array)(Janet x) { return janet_unwrap_array(x); }
JanetTable *(janet_unwrap_table)(Janet x) { return janet_unwrap_table(x); }
JanetBuffer *(janet_unwrap_buffer)(Janet x) { return janet_unwrap_buffer(x); }
const uint8_t *(janet_unwrap_string)(Janet x) { return janet_unwrap_string(x); }
const uint8_t *(janet_unwrap_symbol)(Janet x) { return janet_unwrap_symbol(x); }
const uint8_t *(janet_unwrap_keyword)(Janet x) { return janet_unwrap_keyword(x); }
void *(janet_unwrap_abstract)(Janet x) { return janet_unwrap_abstract(x); }
void *(janet_unwrap_pointer)(Janet x) { return janet_unwrap_pointer(x); }
JanetFunction *(janet_unwrap_function)(Janet x) { return janet_unwrap_function(x); }
JanetCFunction (janet_unwrap_cfunction)(Janet x) { return janet_unwrap_cfunction(x); }
int (janet_unwrap_boolean)(Janet x) { return janet_unwrap_boolean(x); }
double (janet_unwrap_number)(Janet x) { return janet_unwrap_number(x); }
int32_t (janet_unwrap_integer)(Janet x) { return janet_unwrap_integer(x); }
#ifdef JANET_NANBOX
Janet (janet_wrap_nil)(void) { return janet_wrap_nil(); }
Janet (janet_wrap_number)(double x) { return janet_wrap_number(); }
Janet (janet_wrap_true)(void) { return janet_wrap_true(); }
Janet (janet_wrap_false)(void) { return janet_wrap_false(); }
Janet (janet_wrap_boolean)(int x) { return janet_wrap_boolean(); }
Janet (janet_wrap_string)(const uint8_t *x) { return janet_wrap_string(); }
Janet (janet_wrap_symbol)(const uint8_t *x) { return janet_wrap_symbol(); }
Janet (janet_wrap_keyword)(const uint8_t *x) { return janet_wrap_keyword(); }
Janet (janet_wrap_array)(JanetArray *x) { return janet_wrap_array(); }
Janet (janet_wrap_tuple)(const Janet *x) { return janet_wrap_tuple(); }
Janet (janet_wrap_struct)(const JanetKV *x) { return janet_wrap_struct(); }
Janet (janet_wrap_fiber)(JanetFiber *x) { return janet_wrap_fiber(); }
Janet (janet_wrap_buffer)(JanetBuffer *x) { return janet_wrap_buffer(); }
Janet (janet_wrap_function)(JanetFunction *x) { return janet_wrap_function(); }
Janet (janet_wrap_cfunction)(JanetCFunction x) { return janet_wrap_cfunction(); }
Janet (janet_wrap_table)(JanetTable *x) { return janet_wrap_table(); }
Janet (janet_wrap_abstract)(void *x) { return janet_wrap_abstract(); }
Janet (janet_wrap_pointer)(void *x) { return janet_wrap_pointer(); }
Janet (janet_wrap_integer)(int32_t x) { return janet_wrap_integer(); }
#endif
/*****/
void *janet_memalloc_empty(int32_t count) {
int32_t i;
void *mem = malloc(count * sizeof(JanetKV));

View File

@ -151,28 +151,6 @@ extern "C" {
#endif
#endif
/* Handle runtime errors */
#ifndef janet_exit
#include <stdio.h>
#define janet_exit(m) do { \
printf("C runtime error at line %d in file %s: %s\n",\
__LINE__,\
__FILE__,\
(m));\
exit(1);\
} while (0)
#endif
#define janet_assert(c, m) do { \
if (!(c)) janet_exit((m)); \
} while (0)
/* What to do when out of memory */
#ifndef JANET_OUT_OF_MEMORY
#include <stdio.h>
#define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0)
#endif
/* Prevent some recursive functions from recursing too deeply
* ands crashing (the parser). Instead, error out. */
#define JANET_RECURSION_GUARD 1024
@ -224,6 +202,7 @@ extern "C" {
#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdio.h>
/* Names of all of the types */
extern const char *const janet_type_names[16];
@ -374,6 +353,63 @@ typedef enum JanetType {
* janet_u64(x) - get 64 bits of payload for hashing
*/
/***** START SECTION NON-C API *****/
/* Some janet types use offset tricks to make operations easier in C. For
* external bindings, we should prefer using the Head structs directly, and
* use the host language to add sugar around the manipulation of the Janet types. */
JANET_API JanetStructHead *janet_struct_head(const JanetKV *st);
JANET_API JanetAbstractHead *janet_abstract_head(const void *abstract);
JANET_API JanetStringHead *janet_string_head(const uint8_t *s);
JANET_API JanetTupleHead *janet_tuple_head(const Janet *tuple);
/* Some language bindings won't have access to the macro versions. */
JANET_API JanetType janet_type(Janet x);
JANET_API int janet_checktype(Janet x, JanetType type);
JANET_API int janet_checktypes(Janet x, int typeflags);
JANET_API int janet_truthy(Janet x);
JANET_API const JanetKV *janet_unwrap_struct(Janet x);
JANET_API const Janet *janet_unwrap_tuple(Janet x);
JANET_API JanetFiber *janet_unwrap_fiber(Janet x);
JANET_API JanetArray *janet_unwrap_array(Janet x);
JANET_API JanetTable *janet_unwrap_table(Janet x);
JANET_API JanetBuffer *janet_unwrap_buffer(Janet x);
JANET_API const uint8_t *janet_unwrap_string(Janet x);
JANET_API const uint8_t *janet_unwrap_symbol(Janet x);
JANET_API const uint8_t *janet_unwrap_keyword(Janet x);
JANET_API void *janet_unwrap_abstract(Janet x);
JANET_API void *janet_unwrap_pointer(Janet x);
JANET_API JanetFunction *janet_unwrap_function(Janet x);
JANET_API JanetCFunction janet_unwrap_cfunction(Janet x);
JANET_API int janet_unwrap_boolean(Janet x);
JANET_API double janet_unwrap_number(Janet x);
JANET_API int32_t janet_unwrap_integer(Janet x);
JANET_API Janet janet_wrap_nil(void);
JANET_API Janet janet_wrap_number(double x);
JANET_API Janet janet_wrap_true(void);
JANET_API Janet janet_wrap_false(void);
JANET_API Janet janet_wrap_boolean(int x);
JANET_API Janet janet_wrap_string(const uint8_t *x);
JANET_API Janet janet_wrap_symbol(const uint8_t *x);
JANET_API Janet janet_wrap_keyword(const uint8_t *x);
JANET_API Janet janet_wrap_array(JanetArray *x);
JANET_API Janet janet_wrap_tuple(const Janet *x);
JANET_API Janet janet_wrap_struct(const JanetKV *x);
JANET_API Janet janet_wrap_fiber(JanetFiber *x);
JANET_API Janet janet_wrap_buffer(JanetBuffer *x);
JANET_API Janet janet_wrap_function(JanetFunction *x);
JANET_API Janet janet_wrap_cfunction(JanetCFunction x);
JANET_API Janet janet_wrap_table(JanetTable *x);
JANET_API Janet janet_wrap_abstract(void *x);
JANET_API Janet janet_wrap_pointer(void *x);
JANET_API Janet janet_wrap_integer(int32_t x);
/***** END SECTION NON-C API *****/
#ifdef JANET_NANBOX_64
#include <math.h>
@ -500,7 +536,6 @@ union Janet {
#define janet_truthy(x) \
((x).tagged.type != JANET_NIL && ((x).tagged.type != JANET_BOOLEAN || ((x).tagged.payload.integer & 0x1)))
JANET_API Janet janet_wrap_number(double x);
JANET_API Janet janet_nanbox32_from_tagi(uint32_t tag, int32_t integer);
JANET_API Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer);
@ -538,7 +573,6 @@ JANET_API Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer);
#define janet_unwrap_function(x) ((JanetFunction *)(x).tagged.payload.pointer)
#define janet_unwrap_cfunction(x) ((JanetCFunction)(x).tagged.payload.pointer)
#define janet_unwrap_boolean(x) ((x).tagged.payload.integer)
JANET_API double janet_unwrap_number(Janet x);
#else
@ -576,25 +610,6 @@ struct Janet {
#define janet_unwrap_boolean(x) ((x).as.u64 & 0x1)
#define janet_unwrap_number(x) ((x).as.number)
JANET_API Janet janet_wrap_nil(void);
JANET_API Janet janet_wrap_number(double x);
JANET_API Janet janet_wrap_true(void);
JANET_API Janet janet_wrap_false(void);
JANET_API Janet janet_wrap_boolean(int x);
JANET_API Janet janet_wrap_string(const uint8_t *x);
JANET_API Janet janet_wrap_symbol(const uint8_t *x);
JANET_API Janet janet_wrap_keyword(const uint8_t *x);
JANET_API Janet janet_wrap_array(JanetArray *x);
JANET_API Janet janet_wrap_tuple(const Janet *x);
JANET_API Janet janet_wrap_struct(const JanetKV *x);
JANET_API Janet janet_wrap_fiber(JanetFiber *x);
JANET_API Janet janet_wrap_buffer(JanetBuffer *x);
JANET_API Janet janet_wrap_function(JanetFunction *x);
JANET_API Janet janet_wrap_cfunction(JanetCFunction x);
JANET_API Janet janet_wrap_table(JanetTable *x);
JANET_API Janet janet_wrap_abstract(void *x);
JANET_API Janet janet_wrap_pointer(void *x);
/* End of tagged union implementation */
#endif
@ -999,7 +1014,7 @@ JANET_API Janet janet_parser_produce(JanetParser *parser);
JANET_API const char *janet_parser_error(JanetParser *parser);
JANET_API void janet_parser_flush(JanetParser *parser);
JANET_API void janet_parser_eof(JanetParser *parser);
#define janet_parser_has_more(P) ((P)->pending)
JANET_API int janet_parser_has_more(JanetParser *parser);
/* Assembly */
#ifdef JANET_ASSEMBLER
@ -1156,7 +1171,7 @@ JANET_API JanetKV *janet_table_find(JanetTable *t, Janet key);
/* Fiber */
JANET_API JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv);
JANET_API JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv);
#define janet_fiber_status(f) (((f)->flags & JANET_FIBER_STATUS_MASK) >> JANET_FIBER_STATUS_OFFSET)
JANET_API JanetFiberStatus janet_fiber_status(JanetFiber *fiber);
/* Treat similar types through uniform interfaces for iteration */
JANET_API int janet_indexed_view(Janet seq, const Janet **data, int32_t *len);
@ -1166,9 +1181,9 @@ JANET_API Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key
JANET_API const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv);
/* Abstract */
#define janet_abstract_header(u) ((JanetAbstractHead *)((char *)u - offsetof(JanetAbstractHead, data)))
#define janet_abstract_type(u) (janet_abstract_header(u)->type)
#define janet_abstract_size(u) (janet_abstract_header(u)->size)
#define janet_abstract_head(u) ((JanetAbstractHead *)((char *)u - offsetof(JanetAbstractHead, data)))
#define janet_abstract_type(u) (janet_abstract_head(u)->type)
#define janet_abstract_size(u) (janet_abstract_head(u)->size)
JANET_API void *janet_abstract(const JanetAbstractType *type, size_t size);
/* Native */
@ -1247,8 +1262,8 @@ JANET_API void janet_register(const char *name, JanetCFunction cfun);
JANET_API void janet_panicv(Janet message);
JANET_API void janet_panic(const char *message);
JANET_API void janet_panics(const uint8_t *message);
#define janet_panicf(...) janet_panics(janet_formatc(__VA_ARGS__))
#define janet_printf(...) fputs((const char *)janet_formatc(__VA_ARGS__), stdout)
JANET_API void janet_panicf(const char *format, ...);
JANET_API void janet_printf(const char *format, ...);
JANET_API void janet_panic_type(Janet x, int32_t n, int expected);
JANET_API void janet_panic_abstract(Janet x, int32_t n, const JanetAbstractType *at);
JANET_API void janet_arity(int32_t arity, int32_t min, int32_t max);
@ -1289,14 +1304,14 @@ JANET_API FILE *janet_getfile(const Janet *argv, int32_t n, int *flags);
JANET_API FILE *janet_dynfile(const char *name, FILE *def);
/* Marshal API */
#define janet_marshal_size(ctx, x) janet_marshal_int64((ctx), (int64_t) (x))
JANET_API void janet_marshal_size(JanetMarshalContext *ctx, size_t value);
JANET_API void janet_marshal_int(JanetMarshalContext *ctx, int32_t value);
JANET_API void janet_marshal_int64(JanetMarshalContext *ctx, int64_t value);
JANET_API void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value);
JANET_API void janet_marshal_bytes(JanetMarshalContext *ctx, const uint8_t *bytes, size_t len);
JANET_API void janet_marshal_janet(JanetMarshalContext *ctx, Janet x);
#define janet_unmarshal_size(ctx) ((size_t) janet_unmarshal_int64((ctx)))
JANET_API size_t janet_unmarshal_size(JanetMarshalContext *ctx);
JANET_API int32_t janet_unmarshal_int(JanetMarshalContext *ctx);
JANET_API int64_t janet_unmarshal_int64(JanetMarshalContext *ctx);
JANET_API uint8_t janet_unmarshal_byte(JanetMarshalContext *ctx);