2017-12-21 04:03:34 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2017-12-21 04:03:34 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2019-08-04 17:18:57 +00:00
|
|
|
#include <math.h>
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "compile.h"
|
2018-03-31 20:40:36 +00:00
|
|
|
#include "state.h"
|
2018-08-20 00:21:27 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-12-06 19:30:11 +00:00
|
|
|
/* Generated bytes */
|
2019-03-22 22:34:50 +00:00
|
|
|
#ifndef JANET_BOOTSTRAP
|
2019-02-08 05:44:30 +00:00
|
|
|
extern const unsigned char *janet_core_image;
|
|
|
|
extern size_t janet_core_image_size;
|
|
|
|
#endif
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
|
|
|
|
* with native code. */
|
2018-10-17 03:08:26 +00:00
|
|
|
#if defined(JANET_NO_DYNAMIC_MODULES)
|
|
|
|
typedef int Clib;
|
|
|
|
#define load_clib(name) ((void) name, 0)
|
2019-03-22 18:31:20 +00:00
|
|
|
#define symbol_clib(lib, sym) ((void) lib, (void) sym, NULL)
|
2018-10-17 03:08:26 +00:00
|
|
|
#define error_clib() "dynamic libraries not supported"
|
|
|
|
#elif defined(JANET_WINDOWS)
|
2018-07-04 03:07:35 +00:00
|
|
|
#include <windows.h>
|
|
|
|
typedef HINSTANCE Clib;
|
|
|
|
#define load_clib(name) LoadLibrary((name))
|
|
|
|
#define symbol_clib(lib, sym) GetProcAddress((lib), (sym))
|
2019-07-28 01:44:44 +00:00
|
|
|
static char error_clib_buf[256];
|
|
|
|
static char *error_clib(void) {
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2019-07-28 18:25:14 +00:00
|
|
|
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
error_clib_buf, sizeof(error_clib_buf), NULL);
|
2019-07-28 01:44:44 +00:00
|
|
|
error_clib_buf[strlen(error_clib_buf) - 1] = '\0';
|
|
|
|
return error_clib_buf;
|
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
typedef void *Clib;
|
|
|
|
#define load_clib(name) dlopen((name), RTLD_NOW)
|
|
|
|
#define symbol_clib(lib, sym) dlsym((lib), (sym))
|
|
|
|
#define error_clib() dlerror()
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetModule janet_native(const char *name, const uint8_t **error) {
|
2018-07-04 03:07:35 +00:00
|
|
|
Clib lib = load_clib(name);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetModule init;
|
2019-05-30 02:11:56 +00:00
|
|
|
JanetModconf getter;
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!lib) {
|
2018-09-06 02:18:42 +00:00
|
|
|
*error = janet_cstring(error_clib());
|
2018-07-04 03:07:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
init = (JanetModule) symbol_clib(lib, "_janet_init");
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!init) {
|
2019-05-30 01:57:03 +00:00
|
|
|
*error = janet_cstring("could not find the _janet_init symbol");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-30 02:11:56 +00:00
|
|
|
getter = (JanetModconf) symbol_clib(lib, "_janet_mod_config");
|
|
|
|
if (!getter) {
|
2019-05-30 01:57:03 +00:00
|
|
|
*error = janet_cstring("could not find the _janet_mod_config symbol");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-30 02:11:56 +00:00
|
|
|
JanetBuildConfig modconf = getter();
|
2019-05-30 01:57:03 +00:00
|
|
|
JanetBuildConfig host = janet_config_current();
|
|
|
|
if (host.major != modconf.major ||
|
|
|
|
host.minor < modconf.minor ||
|
|
|
|
host.bits != modconf.bits) {
|
|
|
|
char errbuf[128];
|
|
|
|
sprintf(errbuf, "config mismatch - host %d.%.d.%d(%.4x) vs. module %d.%d.%d(%.4x)",
|
|
|
|
host.major,
|
|
|
|
host.minor,
|
|
|
|
host.patch,
|
|
|
|
host.bits,
|
|
|
|
modconf.major,
|
|
|
|
modconf.minor,
|
|
|
|
modconf.patch,
|
|
|
|
modconf.bits);
|
|
|
|
*error = janet_cstring(errbuf);
|
2018-07-04 03:07:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2019-06-19 02:01:14 +00:00
|
|
|
static const char *janet_dyncstring(const char *name, const char *dflt) {
|
|
|
|
Janet x = janet_dyn(name);
|
|
|
|
if (janet_checktype(x, JANET_NIL)) return dflt;
|
|
|
|
if (!janet_checktype(x, JANET_STRING)) {
|
|
|
|
janet_panicf("expected string, got %v", x);
|
|
|
|
}
|
|
|
|
const uint8_t *jstr = janet_unwrap_string(x);
|
|
|
|
const char *cstr = (const char *)jstr;
|
|
|
|
if (strlen(cstr) != (size_t) janet_string_length(jstr)) {
|
|
|
|
janet_panicf("string %v contains embedded 0s");
|
|
|
|
}
|
|
|
|
return cstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_path_sep(char c) {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
if (c == '\\') return 1;
|
|
|
|
#endif
|
|
|
|
return c == '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used for module system. */
|
|
|
|
static Janet janet_core_expand_path(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
const char *input = janet_getcstring(argv, 0);
|
|
|
|
const char *template = janet_getcstring(argv, 1);
|
2019-06-20 03:23:27 +00:00
|
|
|
const char *curfile = janet_dyncstring("current-file", "");
|
2019-06-19 02:01:14 +00:00
|
|
|
const char *syspath = janet_dyncstring("syspath", "");
|
|
|
|
JanetBuffer *out = janet_buffer(0);
|
|
|
|
size_t tlen = strlen(template);
|
|
|
|
|
|
|
|
/* Calculate name */
|
2019-06-20 03:23:27 +00:00
|
|
|
const char *name = input + strlen(input);
|
2019-06-19 02:01:14 +00:00
|
|
|
while (name > input) {
|
|
|
|
if (is_path_sep(*(name - 1))) break;
|
|
|
|
name--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate dirpath from current file */
|
2019-06-20 03:23:27 +00:00
|
|
|
const char *curname = curfile + strlen(curfile);
|
2019-06-19 02:01:14 +00:00
|
|
|
while (curname > curfile) {
|
|
|
|
if (is_path_sep(*curname)) break;
|
|
|
|
curname--;
|
|
|
|
}
|
2019-06-20 03:23:27 +00:00
|
|
|
const char *curdir;
|
|
|
|
int32_t curlen;
|
|
|
|
if (curname == curfile) {
|
|
|
|
/* Current file has one or zero path segments, so
|
|
|
|
* we are in the . directory. */
|
|
|
|
curdir = ".";
|
|
|
|
curlen = 1;
|
|
|
|
} else {
|
|
|
|
/* Current file has 2 or more segments, so we
|
|
|
|
* can cut off the last segment. */
|
|
|
|
curdir = curfile;
|
|
|
|
curlen = (int32_t)(curname - curfile);
|
|
|
|
}
|
2019-06-19 02:01:14 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < tlen; i++) {
|
|
|
|
if (template[i] == ':') {
|
|
|
|
if (strncmp(template + i, ":all:", 5) == 0) {
|
|
|
|
janet_buffer_push_cstring(out, input);
|
|
|
|
i += 4;
|
|
|
|
} else if (strncmp(template + i, ":cur:", 5) == 0) {
|
2019-06-20 03:23:27 +00:00
|
|
|
janet_buffer_push_bytes(out, (const uint8_t *)curdir, curlen);
|
2019-06-19 02:01:14 +00:00
|
|
|
i += 4;
|
|
|
|
} else if (strncmp(template + i, ":dir:", 5) == 0) {
|
2019-06-20 03:23:27 +00:00
|
|
|
janet_buffer_push_bytes(out, (const uint8_t *)input,
|
|
|
|
(int32_t)(name - input));
|
2019-06-19 02:01:14 +00:00
|
|
|
i += 4;
|
|
|
|
} else if (strncmp(template + i, ":sys:", 5) == 0) {
|
|
|
|
janet_buffer_push_cstring(out, syspath);
|
|
|
|
i += 4;
|
|
|
|
} else if (strncmp(template + i, ":name:", 6) == 0) {
|
|
|
|
janet_buffer_push_cstring(out, name);
|
|
|
|
i += 5;
|
|
|
|
} else {
|
|
|
|
janet_buffer_push_u8(out, (uint8_t) template[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
janet_buffer_push_u8(out, (uint8_t) template[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normalize */
|
|
|
|
uint8_t *scan = out->data;
|
|
|
|
uint8_t *print = scan;
|
|
|
|
uint8_t *scanend = scan + out->count;
|
|
|
|
int normal_section_count = 0;
|
|
|
|
int dot_count = 0;
|
|
|
|
while (scan < scanend) {
|
|
|
|
if (*scan == '.') {
|
|
|
|
if (dot_count >= 0) {
|
|
|
|
dot_count++;
|
|
|
|
} else {
|
|
|
|
*print++ = '.';
|
|
|
|
}
|
|
|
|
} else if (is_path_sep(*scan)) {
|
|
|
|
if (dot_count == 1) {
|
|
|
|
;
|
|
|
|
} else if (dot_count == 2) {
|
|
|
|
if (normal_section_count > 0) {
|
|
|
|
/* unprint last separator */
|
|
|
|
print--;
|
|
|
|
/* unprint last section */
|
|
|
|
while (print > out->data && !is_path_sep(*(print - 1)))
|
|
|
|
print--;
|
|
|
|
normal_section_count--;
|
|
|
|
} else {
|
|
|
|
*print++ = '.';
|
|
|
|
*print++ = '.';
|
|
|
|
*print++ = '/';
|
|
|
|
}
|
|
|
|
} else if (scan == out->data || dot_count != 0) {
|
|
|
|
while (dot_count > 0) {
|
|
|
|
--dot_count;
|
|
|
|
*print++ = '.';
|
|
|
|
}
|
|
|
|
if (scan > out->data) {
|
|
|
|
normal_section_count++;
|
|
|
|
}
|
|
|
|
*print++ = '/';
|
|
|
|
}
|
|
|
|
dot_count = 0;
|
|
|
|
} else {
|
2019-07-15 22:35:29 +00:00
|
|
|
while (dot_count > 0) {
|
|
|
|
--dot_count;
|
|
|
|
*print++ = '.';
|
|
|
|
}
|
2019-06-19 02:01:14 +00:00
|
|
|
dot_count = -1;
|
|
|
|
*print++ = *scan;
|
|
|
|
}
|
|
|
|
scan++;
|
|
|
|
}
|
2019-06-19 13:45:56 +00:00
|
|
|
out->count = (int32_t)(print - out->data);
|
2019-06-19 02:01:14 +00:00
|
|
|
return janet_wrap_buffer(out);
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:41:45 +00:00
|
|
|
static Janet janet_core_dyn(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
Janet value;
|
|
|
|
if (janet_vm_fiber->env) {
|
|
|
|
value = janet_table_get(janet_vm_fiber->env, argv[0]);
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
}
|
|
|
|
if (argc == 2 && janet_checktype(value, JANET_NIL)) {
|
|
|
|
return argv[1];
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet janet_core_setdyn(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
if (!janet_vm_fiber->env) {
|
|
|
|
janet_vm_fiber->env = janet_table(2);
|
|
|
|
}
|
|
|
|
janet_table_put(janet_vm_fiber->env, argv[0], argv[1]);
|
|
|
|
return argv[1];
|
|
|
|
}
|
|
|
|
|
2019-12-02 02:49:51 +00:00
|
|
|
// XXX inline asm function with a new op - OP_GET_PERMISSIVE?
|
|
|
|
// This would match up with OP_GET which is used for 'in'.
|
2019-11-08 23:35:27 +00:00
|
|
|
static Janet janet_core_get(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
2019-12-02 02:49:51 +00:00
|
|
|
Janet result = janet_get_permissive(argv[0], argv[1]);
|
|
|
|
if (argc == 3 && janet_checktype(result, JANET_NIL)) {
|
|
|
|
return argv[2];
|
2019-11-08 23:35:27 +00:00
|
|
|
}
|
2019-12-02 02:49:51 +00:00
|
|
|
return result;
|
2019-11-08 23:35:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_native(int32_t argc, Janet *argv) {
|
|
|
|
JanetModule init;
|
2019-01-06 04:37:10 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
2019-01-06 01:09:03 +00:00
|
|
|
const uint8_t *path = janet_getstring(argv, 0);
|
2018-07-04 03:07:35 +00:00
|
|
|
const uint8_t *error = NULL;
|
2019-01-06 04:37:10 +00:00
|
|
|
JanetTable *env;
|
|
|
|
if (argc == 2) {
|
|
|
|
env = janet_gettable(argv, 1);
|
|
|
|
} else {
|
|
|
|
env = janet_table(0);
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
init = janet_native((const char *)path, &error);
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!init) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panicf("could not load native %S: %S", path, error);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
init(env);
|
2019-07-27 03:43:54 +00:00
|
|
|
janet_table_put(env, janet_ckeywordv("native"), argv[0]);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_table(env);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_describe(int32_t argc, Janet *argv) {
|
2019-01-19 17:31:55 +00:00
|
|
|
JanetBuffer *b = janet_buffer(0);
|
2019-01-06 02:23:44 +00:00
|
|
|
for (int32_t i = 0; i < argc; ++i)
|
2019-01-19 17:31:55 +00:00
|
|
|
janet_description_b(b, argv[i]);
|
|
|
|
return janet_stringv(b->data, b->count);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_string(int32_t argc, Janet *argv) {
|
2019-01-19 17:31:55 +00:00
|
|
|
JanetBuffer *b = janet_buffer(0);
|
2019-01-06 02:23:44 +00:00
|
|
|
for (int32_t i = 0; i < argc; ++i)
|
2019-01-19 17:31:55 +00:00
|
|
|
janet_to_string_b(b, argv[i]);
|
|
|
|
return janet_stringv(b->data, b->count);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_symbol(int32_t argc, Janet *argv) {
|
2019-01-19 17:31:55 +00:00
|
|
|
JanetBuffer *b = janet_buffer(0);
|
2019-01-06 02:23:44 +00:00
|
|
|
for (int32_t i = 0; i < argc; ++i)
|
2019-01-19 17:31:55 +00:00
|
|
|
janet_to_string_b(b, argv[i]);
|
|
|
|
return janet_symbolv(b->data, b->count);
|
2018-01-21 22:08:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_keyword(int32_t argc, Janet *argv) {
|
2019-01-19 17:31:55 +00:00
|
|
|
JanetBuffer *b = janet_buffer(0);
|
2019-01-06 02:23:44 +00:00
|
|
|
for (int32_t i = 0; i < argc; ++i)
|
2019-01-19 17:31:55 +00:00
|
|
|
janet_to_string_b(b, argv[i]);
|
|
|
|
return janet_keywordv(b->data, b->count);
|
2019-01-03 00:41:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_buffer(int32_t argc, Janet *argv) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *b = janet_buffer(0);
|
2019-01-06 02:23:44 +00:00
|
|
|
for (int32_t i = 0; i < argc; ++i)
|
|
|
|
janet_to_string_b(b, argv[i]);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_buffer(b);
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_is_abstract(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_boolean(janet_checktype(argv[0], JANET_ABSTRACT));
|
2018-11-23 20:33:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_scannumber(int32_t argc, Janet *argv) {
|
|
|
|
double number;
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-12 22:31:15 +00:00
|
|
|
JanetByteView view = janet_getbytes(argv, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
if (janet_scan_number(view.bytes, view.len, &number))
|
|
|
|
return janet_wrap_nil();
|
|
|
|
return janet_wrap_number(number);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_tuple(int32_t argc, Janet *argv) {
|
|
|
|
return janet_wrap_tuple(janet_tuple_n(argv, argc));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_array(int32_t argc, Janet *argv) {
|
|
|
|
JanetArray *array = janet_array(argc);
|
|
|
|
array->count = argc;
|
|
|
|
memcpy(array->data, argv, argc * sizeof(Janet));
|
|
|
|
return janet_wrap_array(array);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:29:32 +00:00
|
|
|
static Janet janet_core_slice(int32_t argc, Janet *argv) {
|
2019-11-05 15:41:30 +00:00
|
|
|
JanetRange range;
|
2019-11-05 15:29:32 +00:00
|
|
|
JanetByteView bview;
|
|
|
|
JanetView iview;
|
|
|
|
if (janet_bytes_view(argv[0], &bview.bytes, &bview.len)) {
|
2019-11-05 15:41:30 +00:00
|
|
|
range = janet_getslice(argc, argv);
|
2019-11-05 15:29:32 +00:00
|
|
|
return janet_stringv(bview.bytes + range.start, range.end - range.start);
|
|
|
|
} else if (janet_indexed_view(argv[0], &iview.items, &iview.len)) {
|
2019-11-05 15:41:30 +00:00
|
|
|
range = janet_getslice(argc, argv);
|
2019-11-05 15:29:32 +00:00
|
|
|
return janet_wrap_tuple(janet_tuple_n(iview.items + range.start, range.end - range.start));
|
|
|
|
} else {
|
|
|
|
janet_panic_type(argv[0], 0, JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_table(int32_t argc, Janet *argv) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc & 1)
|
|
|
|
janet_panic("expected even number of arguments");
|
|
|
|
JanetTable *table = janet_table(argc >> 1);
|
|
|
|
for (i = 0; i < argc; i += 2) {
|
|
|
|
janet_table_put(table, argv[i], argv[i + 1]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_table(table);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_struct(int32_t argc, Janet *argv) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc & 1)
|
|
|
|
janet_panic("expected even number of arguments");
|
|
|
|
JanetKV *st = janet_struct_begin(argc >> 1);
|
|
|
|
for (i = 0; i < argc; i += 2) {
|
|
|
|
janet_struct_put(st, argv[i], argv[i + 1]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_struct(janet_struct_end(st));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_gensym(int32_t argc, Janet *argv) {
|
|
|
|
(void) argv;
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_symbol(janet_symbol_gen());
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_gccollect(int32_t argc, Janet *argv) {
|
|
|
|
(void) argv;
|
|
|
|
(void) argc;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_collect();
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_gcsetinterval(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t val = janet_getinteger(argv, 0);
|
2018-04-28 22:10:57 +00:00
|
|
|
if (val < 0)
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("expected non-negative integer");
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_vm_gc_interval = val;
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_gcinterval(int32_t argc, Janet *argv) {
|
|
|
|
(void) argv;
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_number(janet_vm_gc_interval);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_type(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetType t = janet_type(argv[0]);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (t == JANET_ABSTRACT) {
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv(janet_abstract_type(janet_unwrap_abstract(argv[0]))->name);
|
2018-01-16 01:14:21 +00:00
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv(janet_type_names[t]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_next(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetDictView view = janet_getdictionary(argv, 0);
|
|
|
|
const JanetKV *end = view.kvs + view.cap;
|
|
|
|
const JanetKV *kv = janet_checktype(argv[1], JANET_NIL)
|
2019-02-20 01:51:34 +00:00
|
|
|
? view.kvs
|
|
|
|
: janet_dict_find(view.kvs, view.cap, argv[1]) + 1;
|
2019-01-06 01:09:03 +00:00
|
|
|
while (kv < end) {
|
|
|
|
if (!janet_checktype(kv->key, JANET_NIL)) return kv->key;
|
|
|
|
kv++;
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_core_hash(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_number(janet_hash(argv[0]));
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2019-03-08 16:39:18 +00:00
|
|
|
static Janet janet_core_getline(int32_t argc, Janet *argv) {
|
2019-06-08 19:27:13 +00:00
|
|
|
FILE *in = janet_dynfile("in", stdin);
|
|
|
|
FILE *out = janet_dynfile("out", stdout);
|
2019-03-08 16:39:18 +00:00
|
|
|
janet_arity(argc, 0, 2);
|
|
|
|
JanetBuffer *buf = (argc >= 2) ? janet_getbuffer(argv, 1) : janet_buffer(10);
|
|
|
|
if (argc >= 1) {
|
|
|
|
const char *prompt = (const char *) janet_getstring(argv, 0);
|
2019-06-08 19:27:13 +00:00
|
|
|
fprintf(out, "%s", prompt);
|
|
|
|
fflush(out);
|
2019-03-08 16:39:18 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
buf->count = 0;
|
|
|
|
int c;
|
|
|
|
for (;;) {
|
2019-06-08 19:27:13 +00:00
|
|
|
c = fgetc(in);
|
|
|
|
if (feof(in) || c < 0) {
|
2019-03-08 16:39:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
janet_buffer_push_u8(buf, (uint8_t) c);
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return janet_wrap_buffer(buf);
|
|
|
|
}
|
|
|
|
|
2019-05-04 19:03:41 +00:00
|
|
|
static Janet janet_core_trace(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetFunction *func = janet_getfunction(argv, 0);
|
|
|
|
func->gc.flags |= JANET_FUNCFLAG_TRACE;
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet janet_core_untrace(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetFunction *func = janet_getfunction(argv, 0);
|
|
|
|
func->gc.flags &= ~JANET_FUNCFLAG_TRACE;
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-08-04 17:18:57 +00:00
|
|
|
static Janet janet_core_check_int(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
if (!janet_checktype(argv[0], JANET_NUMBER)) goto ret_false;
|
|
|
|
double num = janet_unwrap_number(argv[0]);
|
|
|
|
return janet_wrap_boolean(num == (double)((int32_t)num));
|
|
|
|
ret_false:
|
|
|
|
return janet_wrap_false();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet janet_core_check_nat(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
if (!janet_checktype(argv[0], JANET_NUMBER)) goto ret_false;
|
|
|
|
double num = janet_unwrap_number(argv[0]);
|
|
|
|
return janet_wrap_boolean(num >= 0 && (num == (double)((int32_t)num)));
|
|
|
|
ret_false:
|
|
|
|
return janet_wrap_false();
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg corelib_cfuns[] = {
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"native", janet_core_native,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(native path &opt env)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Load a native module from the given path. The path "
|
|
|
|
"must be an absolute or relative path on the file system, and is "
|
|
|
|
"usually a .so file on Unix systems, and a .dll file on Windows. "
|
|
|
|
"Returns an environment table that contains functions and other values "
|
|
|
|
"from the native module.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"describe", janet_core_describe,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(describe x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns a string that is a human readable description of a value x.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"string", janet_core_string,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(string & parts)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a string by concatenating values together. Values are "
|
|
|
|
"converted to bytes via describe if they are not byte sequences. "
|
|
|
|
"Returns the new string.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"symbol", janet_core_symbol,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(symbol & xs)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a symbol by concatenating values together. Values are "
|
|
|
|
"converted to bytes via describe if they are not byte sequences. Returns "
|
|
|
|
"the new symbol.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"keyword", janet_core_keyword,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(keyword & xs)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a keyword by concatenating values together. Values are "
|
|
|
|
"converted to bytes via describe if they are not byte sequences. Returns "
|
|
|
|
"the new keyword.")
|
2019-01-03 00:41:07 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"buffer", janet_core_buffer,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(buffer & xs)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a new buffer by concatenating values together. Values are "
|
|
|
|
"converted to bytes via describe if they are not byte sequences. Returns "
|
|
|
|
"the new buffer.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"abstract?", janet_core_is_abstract,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(abstract? x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Check if x is an abstract type.")
|
2018-11-23 20:33:49 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"table", janet_core_table,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(table & kvs)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a new table from a variadic number of keys and values. "
|
|
|
|
"kvs is a sequence k1, v1, k2, v2, k3, v3, ... If kvs has "
|
|
|
|
"an odd number of elements, an error will be thrown. Returns the "
|
|
|
|
"new table.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array", janet_core_array,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array & items)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Create a new array that contains items. Returns the new array.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"scan-number", janet_core_scannumber,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(scan-number str)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Parse a number from a byte sequence an return that number, either and integer "
|
|
|
|
"or a real. The number "
|
|
|
|
"must be in the same format as numbers in janet source code. Will return nil "
|
|
|
|
"on an invalid number.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"tuple", janet_core_tuple,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(tuple & items)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a new tuple that contains items. Returns the new tuple.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"struct", janet_core_struct,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(struct & kvs)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Create a new struct from a sequence of key value pairs. "
|
|
|
|
"kvs is a sequence k1, v1, k2, v2, k3, v3, ... If kvs has "
|
|
|
|
"an odd number of elements, an error will be thrown. Returns the "
|
|
|
|
"new struct.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"gensym", janet_core_gensym,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(gensym)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns a new symbol that is unique across the runtime. This means it "
|
|
|
|
"will not collide with any already created symbols during compilation, so "
|
|
|
|
"it can be used in macros to generate automatic bindings.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"gccollect", janet_core_gccollect,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(gccollect)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Run garbage collection. You should probably not call this manually.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"gcsetinterval", janet_core_gcsetinterval,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(gcsetinterval interval)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Set an integer number of bytes to allocate before running garbage collection. "
|
2019-12-01 02:27:36 +00:00
|
|
|
"Low values for interval will be slower but use less memory. "
|
2019-02-20 01:51:34 +00:00
|
|
|
"High values will be faster but use more memory.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"gcinterval", janet_core_gcinterval,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(gcinterval)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns the integer number of bytes to allocate before running an iteration "
|
|
|
|
"of garbage collection.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"type", janet_core_type,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(type x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns the type of x as a keyword symbol. x is one of\n"
|
|
|
|
"\t:nil\n"
|
|
|
|
"\t:boolean\n"
|
|
|
|
"\t:integer\n"
|
|
|
|
"\t:real\n"
|
|
|
|
"\t:array\n"
|
|
|
|
"\t:tuple\n"
|
|
|
|
"\t:table\n"
|
|
|
|
"\t:struct\n"
|
|
|
|
"\t:string\n"
|
|
|
|
"\t:buffer\n"
|
|
|
|
"\t:symbol\n"
|
|
|
|
"\t:keyword\n"
|
|
|
|
"\t:function\n"
|
|
|
|
"\t:cfunction\n\n"
|
|
|
|
"or another symbol for an abstract type.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"next", janet_core_next,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(next dict &opt key)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Gets the next key in a struct or table. Can be used to iterate through "
|
|
|
|
"the keys of a data structure in an unspecified order. Keys are guaranteed "
|
|
|
|
"to be seen only once per iteration if they data structure is not mutated "
|
|
|
|
"during iteration. If key is nil, next returns the first key. If next "
|
|
|
|
"returns nil, there are no more keys to iterate through. ")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"hash", janet_core_hash,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(hash value)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Gets a hash value for any janet value. The hash is an integer can be used "
|
|
|
|
"as a cheap hash function for all janet objects. If two values are strictly equal, "
|
|
|
|
"then they will have the same hash value.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-03-08 16:39:18 +00:00
|
|
|
{
|
|
|
|
"getline", janet_core_getline,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(getline &opt prompt buf)\n\n"
|
2019-03-13 00:56:16 +00:00
|
|
|
"Reads a line of input into a buffer, including the newline character, using a prompt. Returns the modified buffer. "
|
|
|
|
"Use this function to implement a simple interface for a terminal program.")
|
2019-03-08 16:39:18 +00:00
|
|
|
},
|
2019-04-16 19:41:45 +00:00
|
|
|
{
|
|
|
|
"dyn", janet_core_dyn,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(dyn key &opt default)\n\n"
|
|
|
|
"Get a dynamic binding. Returns the default value (or nil) if no binding found.")
|
2019-04-16 19:41:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"setdyn", janet_core_setdyn,
|
|
|
|
JDOC("(setdyn key value)\n\n"
|
|
|
|
"Set a dynamic binding. Returns value.")
|
|
|
|
},
|
2019-05-04 19:03:41 +00:00
|
|
|
{
|
|
|
|
"trace", janet_core_trace,
|
|
|
|
JDOC("(trace func)\n\n"
|
|
|
|
"Enable tracing on a function. Returns the function.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"untrace", janet_core_untrace,
|
|
|
|
JDOC("(untrace func)\n\n"
|
|
|
|
"Disables tracing on a function. Returns the function.")
|
|
|
|
},
|
2019-06-19 02:01:14 +00:00
|
|
|
{
|
|
|
|
"module/expand-path", janet_core_expand_path,
|
|
|
|
JDOC("(module/expand-path path template)\n\n"
|
|
|
|
"Expands a path template as found in module/paths for module/find. "
|
|
|
|
"This takes in a path (the argument to require) and a template string, template, "
|
|
|
|
"to expand the path to a path that can be "
|
|
|
|
"used for importing files.")
|
|
|
|
},
|
2019-08-04 17:18:57 +00:00
|
|
|
{
|
|
|
|
"int?", janet_core_check_int,
|
|
|
|
JDOC("(int? x)\n\n"
|
|
|
|
"Check if x can be exactly represented as a 32 bit signed two's complement integer.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nat?", janet_core_check_nat,
|
|
|
|
JDOC("(nat? x)\n\n"
|
|
|
|
"Check if x can be exactly represented as a non-negative 32 bit signed two's complement integer.")
|
|
|
|
},
|
2019-11-05 15:29:32 +00:00
|
|
|
{
|
|
|
|
"slice", janet_core_slice,
|
|
|
|
JDOC("(slice x &opt start end)\n\n"
|
|
|
|
"Extract a sub-range of an indexed data strutrue or byte sequence.")
|
|
|
|
},
|
2019-11-08 23:35:27 +00:00
|
|
|
{
|
|
|
|
"get", janet_core_get,
|
|
|
|
JDOC("(get ds key &opt dflt)\n\n"
|
|
|
|
"Get the value mapped to key in data structure ds, and return dflt or nil if not found. "
|
2019-12-01 23:35:54 +00:00
|
|
|
"Similar to in, but will not throw an error if the key is invalid for the data structure "
|
2019-11-08 23:35:27 +00:00
|
|
|
"unless the data structure is an abstract type. In that case, the abstract type getter may throw "
|
|
|
|
"an error.")
|
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-07-04 03:07:35 +00:00
|
|
|
};
|
|
|
|
|
2019-02-08 05:44:30 +00:00
|
|
|
#ifdef JANET_BOOTSTRAP
|
2019-01-06 08:23:03 +00:00
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Utility for inline assembly */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_quick_asm(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetTable *env,
|
|
|
|
int32_t flags,
|
|
|
|
const char *name,
|
|
|
|
int32_t arity,
|
2019-03-12 04:23:14 +00:00
|
|
|
int32_t min_arity,
|
|
|
|
int32_t max_arity,
|
2019-02-20 01:51:34 +00:00
|
|
|
int32_t slots,
|
|
|
|
const uint32_t *bytecode,
|
|
|
|
size_t bytecode_size,
|
|
|
|
const char *doc) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncDef *def = janet_funcdef_alloc();
|
2018-07-04 03:07:35 +00:00
|
|
|
def->arity = arity;
|
2019-03-12 04:23:14 +00:00
|
|
|
def->min_arity = min_arity;
|
|
|
|
def->max_arity = max_arity;
|
2018-07-04 03:07:35 +00:00
|
|
|
def->flags = flags;
|
|
|
|
def->slotcount = slots;
|
|
|
|
def->bytecode = malloc(bytecode_size);
|
2018-08-06 01:32:32 +00:00
|
|
|
def->bytecode_length = (int32_t)(bytecode_size / sizeof(uint32_t));
|
2018-09-06 02:18:42 +00:00
|
|
|
def->name = janet_cstring(name);
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!def->bytecode) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
memcpy(def->bytecode, bytecode, bytecode_size);
|
2018-11-16 21:24:10 +00:00
|
|
|
janet_def(env, name, janet_wrap_function(janet_thunk(def)), doc);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Macros for easier inline janet assembly */
|
2018-07-09 03:10:02 +00:00
|
|
|
#define SSS(op, a, b, c) ((op) | ((a) << 8) | ((b) << 16) | ((c) << 24))
|
|
|
|
#define SS(op, a, b) ((op) | ((a) << 8) | ((b) << 16))
|
|
|
|
#define SSI(op, a, b, I) ((op) | ((a) << 8) | ((b) << 16) | ((uint32_t)(I) << 24))
|
|
|
|
#define S(op, a) ((op) | ((a) << 8))
|
|
|
|
#define SI(op, a, I) ((op) | ((a) << 8) | ((uint32_t)(I) << 16))
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Templatize a varop */
|
|
|
|
static void templatize_varop(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetTable *env,
|
|
|
|
int32_t flags,
|
|
|
|
const char *name,
|
|
|
|
int32_t nullary,
|
|
|
|
int32_t unary,
|
|
|
|
uint32_t op,
|
|
|
|
const char *doc) {
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Variadic operator assembly. Must be templatized for each different opcode. */
|
|
|
|
/* Reg 0: Argument tuple (args) */
|
|
|
|
/* Reg 1: Argument count (argn) */
|
|
|
|
/* Reg 2: Jump flag (jump?) */
|
|
|
|
/* Reg 3: Accumulator (accum) */
|
|
|
|
/* Reg 4: Next operand (operand) */
|
|
|
|
/* Reg 5: Loop iterator (i) */
|
|
|
|
uint32_t varop_asm[] = {
|
2018-09-06 02:18:42 +00:00
|
|
|
SS(JOP_LENGTH, 1, 0), /* Put number of arguments in register 1 -> argn = count(args) */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Check nullary */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSS(JOP_EQUALS_IMMEDIATE, 2, 1, 0), /* Check if numargs equal to 0 */
|
|
|
|
SI(JOP_JUMP_IF_NOT, 2, 3), /* If not 0, jump to next check */
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Nullary */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_LOAD_INTEGER, 3, nullary), /* accum = nullary value */
|
|
|
|
S(JOP_RETURN, 3), /* return accum */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Check unary */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSI(JOP_EQUALS_IMMEDIATE, 2, 1, 1), /* Check if numargs equal to 1 */
|
|
|
|
SI(JOP_JUMP_IF_NOT, 2, 5), /* If not 1, jump to next check */
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Unary */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_LOAD_INTEGER, 3, unary), /* accum = unary value */
|
|
|
|
SSI(JOP_GET_INDEX, 4, 0, 0), /* operand = args[0] */
|
2018-07-09 03:10:02 +00:00
|
|
|
SSS(op, 3, 3, 4), /* accum = accum op operand */
|
2018-09-06 02:18:42 +00:00
|
|
|
S(JOP_RETURN, 3), /* return accum */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Mutli (2 or more) arity */
|
|
|
|
/* Prime loop */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSI(JOP_GET_INDEX, 3, 0, 0), /* accum = args[0] */
|
|
|
|
SI(JOP_LOAD_INTEGER, 5, 1), /* i = 1 */
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Main loop */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSS(JOP_GET, 4, 0, 5), /* operand = args[i] */
|
2018-07-09 03:10:02 +00:00
|
|
|
SSS(op, 3, 3, 4), /* accum = accum op operand */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSI(JOP_ADD_IMMEDIATE, 5, 5, 1), /* i++ */
|
2018-12-27 18:05:29 +00:00
|
|
|
SSI(JOP_EQUALS, 2, 5, 1), /* jump? = (i == argn) */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_JUMP_IF_NOT, 2, -4), /* if not jump? go back 4 */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Done, do last and return accumulator */
|
2018-09-06 02:18:42 +00:00
|
|
|
S(JOP_RETURN, 3) /* return accum */
|
2018-07-09 03:10:02 +00:00
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_quick_asm(
|
2019-02-20 01:51:34 +00:00
|
|
|
env,
|
|
|
|
flags | JANET_FUNCDEF_FLAG_VARARG,
|
|
|
|
name,
|
|
|
|
0,
|
2019-03-12 04:23:14 +00:00
|
|
|
0,
|
|
|
|
INT32_MAX,
|
2019-02-20 01:51:34 +00:00
|
|
|
6,
|
|
|
|
varop_asm,
|
|
|
|
sizeof(varop_asm),
|
|
|
|
doc);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Templatize variadic comparators */
|
|
|
|
static void templatize_comparator(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetTable *env,
|
|
|
|
int32_t flags,
|
|
|
|
const char *name,
|
|
|
|
int invert,
|
|
|
|
uint32_t op,
|
|
|
|
const char *doc) {
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Reg 0: Argument tuple (args) */
|
|
|
|
/* Reg 1: Argument count (argn) */
|
|
|
|
/* Reg 2: Jump flag (jump?) */
|
|
|
|
/* Reg 3: Last value (last) */
|
|
|
|
/* Reg 4: Next operand (next) */
|
|
|
|
/* Reg 5: Loop iterator (i) */
|
|
|
|
uint32_t comparator_asm[] = {
|
2018-09-06 02:18:42 +00:00
|
|
|
SS(JOP_LENGTH, 1, 0), /* Put number of arguments in register 1 -> argn = count(args) */
|
|
|
|
SSS(JOP_LESS_THAN_IMMEDIATE, 2, 1, 2), /* Check if numargs less than 2 */
|
|
|
|
SI(JOP_JUMP_IF, 2, 10), /* If numargs < 2, jump to done */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Prime loop */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSI(JOP_GET_INDEX, 3, 0, 0), /* last = args[0] */
|
|
|
|
SI(JOP_LOAD_INTEGER, 5, 1), /* i = 1 */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Main loop */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSS(JOP_GET, 4, 0, 5), /* next = args[i] */
|
2018-07-09 03:10:02 +00:00
|
|
|
SSS(op, 2, 3, 4), /* jump? = last compare next */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_JUMP_IF_NOT, 2, 7), /* if not jump? goto fail (return false) */
|
|
|
|
SSI(JOP_ADD_IMMEDIATE, 5, 5, 1), /* i++ */
|
|
|
|
SS(JOP_MOVE_NEAR, 3, 4), /* last = next */
|
2018-12-27 18:05:29 +00:00
|
|
|
SSI(JOP_EQUALS, 2, 5, 1), /* jump? = (i == argn) */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_JUMP_IF_NOT, 2, -6), /* if not jump? go back 6 */
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Done, return true */
|
2018-09-06 02:18:42 +00:00
|
|
|
S(invert ? JOP_LOAD_FALSE : JOP_LOAD_TRUE, 3),
|
|
|
|
S(JOP_RETURN, 3),
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Failed, return false */
|
2018-09-06 02:18:42 +00:00
|
|
|
S(invert ? JOP_LOAD_TRUE : JOP_LOAD_FALSE, 3),
|
|
|
|
S(JOP_RETURN, 3)
|
2018-07-09 03:10:02 +00:00
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_quick_asm(
|
2019-02-20 01:51:34 +00:00
|
|
|
env,
|
|
|
|
flags | JANET_FUNCDEF_FLAG_VARARG,
|
|
|
|
name,
|
|
|
|
0,
|
2019-03-12 04:23:14 +00:00
|
|
|
0,
|
|
|
|
INT32_MAX,
|
2019-02-20 01:51:34 +00:00
|
|
|
6,
|
|
|
|
comparator_asm,
|
|
|
|
sizeof(comparator_asm),
|
|
|
|
doc);
|
2018-07-09 03:10:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 16:53:39 +00:00
|
|
|
/* Make the apply function */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void make_apply(JanetTable *env) {
|
2018-08-26 16:53:39 +00:00
|
|
|
/* Reg 0: Function (fun) */
|
|
|
|
/* Reg 1: Argument tuple (args) */
|
|
|
|
/* Reg 2: Argument count (argn) */
|
|
|
|
/* Reg 3: Jump flag (jump?) */
|
|
|
|
/* Reg 4: Loop iterator (i) */
|
|
|
|
/* Reg 5: Loop values (x) */
|
|
|
|
uint32_t apply_asm[] = {
|
2018-09-06 02:18:42 +00:00
|
|
|
SS(JOP_LENGTH, 2, 1),
|
|
|
|
SSS(JOP_EQUALS_IMMEDIATE, 3, 2, 0), /* Immediate tail call if no args */
|
|
|
|
SI(JOP_JUMP_IF, 3, 9),
|
2018-08-26 16:53:39 +00:00
|
|
|
|
|
|
|
/* Prime loop */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_LOAD_INTEGER, 4, 0), /* i = 0 */
|
2018-08-26 16:53:39 +00:00
|
|
|
|
|
|
|
/* Main loop */
|
2018-09-06 02:18:42 +00:00
|
|
|
SSS(JOP_GET, 5, 1, 4), /* x = args[i] */
|
|
|
|
SSI(JOP_ADD_IMMEDIATE, 4, 4, 1), /* i++ */
|
2018-12-27 18:05:29 +00:00
|
|
|
SSI(JOP_EQUALS, 3, 4, 2), /* jump? = (i == argn) */
|
2018-09-06 02:18:42 +00:00
|
|
|
SI(JOP_JUMP_IF, 3, 3), /* if jump? go forward 3 */
|
|
|
|
S(JOP_PUSH, 5),
|
|
|
|
(JOP_JUMP | ((uint32_t)(-5) << 8)),
|
2018-08-26 16:53:39 +00:00
|
|
|
|
|
|
|
/* Push the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
S(JOP_PUSH_ARRAY, 5),
|
2018-08-26 16:53:39 +00:00
|
|
|
|
|
|
|
/* Call the funciton */
|
2018-09-06 02:18:42 +00:00
|
|
|
S(JOP_TAILCALL, 0)
|
2018-08-26 16:53:39 +00:00
|
|
|
};
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_APPLY | JANET_FUNCDEF_FLAG_VARARG,
|
2019-03-12 04:23:14 +00:00
|
|
|
"apply", 1, 1, INT32_MAX, 6, apply_asm, sizeof(apply_asm),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(apply f & args)\n\n"
|
|
|
|
"Applies a function to a variable number of arguments. Each element in args "
|
|
|
|
"is used as an argument to f, except the last element in args, which is expected to "
|
|
|
|
"be an array-like. Each element in this last argument is then also pushed as an argument to "
|
|
|
|
"f. For example:\n\n"
|
|
|
|
"\t(apply + 1000 (range 10))\n\n"
|
2019-05-16 16:12:55 +00:00
|
|
|
"sums the first 10 integers and 1000."));
|
2018-08-26 16:53:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 06:49:56 +00:00
|
|
|
static const uint32_t error_asm[] = {
|
|
|
|
JOP_ERROR
|
|
|
|
};
|
|
|
|
static const uint32_t debug_asm[] = {
|
2019-02-20 01:51:34 +00:00
|
|
|
JOP_SIGNAL | (2 << 24),
|
2019-11-24 23:45:53 +00:00
|
|
|
JOP_RETURN
|
2019-01-06 06:49:56 +00:00
|
|
|
};
|
|
|
|
static const uint32_t yield_asm[] = {
|
|
|
|
JOP_SIGNAL | (3 << 24),
|
|
|
|
JOP_RETURN
|
|
|
|
};
|
|
|
|
static const uint32_t resume_asm[] = {
|
|
|
|
JOP_RESUME | (1 << 24),
|
|
|
|
JOP_RETURN
|
|
|
|
};
|
2019-12-01 23:35:54 +00:00
|
|
|
static const uint32_t in_asm[] = {
|
2019-01-06 06:49:56 +00:00
|
|
|
JOP_GET | (1 << 24),
|
2019-08-06 23:10:48 +00:00
|
|
|
JOP_LOAD_NIL | (3 << 8),
|
|
|
|
JOP_EQUALS | (3 << 8) | (3 << 24),
|
|
|
|
JOP_JUMP_IF | (3 << 8) | (2 << 16),
|
|
|
|
JOP_RETURN,
|
|
|
|
JOP_RETURN | (2 << 8)
|
2019-01-06 06:49:56 +00:00
|
|
|
};
|
|
|
|
static const uint32_t put_asm[] = {
|
|
|
|
JOP_PUT | (1 << 16) | (2 << 24),
|
|
|
|
JOP_RETURN
|
|
|
|
};
|
|
|
|
static const uint32_t length_asm[] = {
|
|
|
|
JOP_LENGTH,
|
|
|
|
JOP_RETURN
|
|
|
|
};
|
|
|
|
static const uint32_t bnot_asm[] = {
|
|
|
|
JOP_BNOT,
|
|
|
|
JOP_RETURN
|
|
|
|
};
|
2019-06-24 16:44:13 +00:00
|
|
|
static const uint32_t propagate_asm[] = {
|
|
|
|
JOP_PROPAGATE | (1 << 24),
|
|
|
|
JOP_RETURN
|
|
|
|
};
|
2019-11-27 05:13:53 +00:00
|
|
|
#endif /* ifdef JANET_BOOTSTRAP */
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2019-11-27 05:13:53 +00:00
|
|
|
/*
|
|
|
|
* Setup Environment
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void janet_load_libs(JanetTable *env) {
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_core_cfuns(env, NULL, corelib_cfuns);
|
2019-11-27 05:13:53 +00:00
|
|
|
janet_lib_io(env);
|
|
|
|
janet_lib_math(env);
|
|
|
|
janet_lib_array(env);
|
|
|
|
janet_lib_tuple(env);
|
|
|
|
janet_lib_buffer(env);
|
|
|
|
janet_lib_table(env);
|
|
|
|
janet_lib_fiber(env);
|
|
|
|
janet_lib_os(env);
|
|
|
|
janet_lib_parse(env);
|
|
|
|
janet_lib_compile(env);
|
|
|
|
janet_lib_debug(env);
|
|
|
|
janet_lib_string(env);
|
|
|
|
janet_lib_marsh(env);
|
|
|
|
#ifdef JANET_PEG
|
|
|
|
janet_lib_peg(env);
|
|
|
|
#endif
|
|
|
|
#ifdef JANET_ASSEMBLER
|
|
|
|
janet_lib_asm(env);
|
|
|
|
#endif
|
|
|
|
#ifdef JANET_TYPED_ARRAY
|
|
|
|
janet_lib_typed_array(env);
|
|
|
|
#endif
|
|
|
|
#ifdef JANET_INT_TYPES
|
|
|
|
janet_lib_inttypes(env);
|
|
|
|
#endif
|
|
|
|
#ifdef JANET_THREADS
|
|
|
|
janet_lib_thread(env);
|
|
|
|
#endif
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-02-08 05:44:30 +00:00
|
|
|
#ifdef JANET_BOOTSTRAP
|
2019-11-27 05:13:53 +00:00
|
|
|
|
|
|
|
JanetTable *janet_core_dictionary(JanetTable *replacements) {
|
|
|
|
(void) replacements;
|
|
|
|
janet_panic("not defined in bootstrap");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JanetTable *janet_core_env(JanetTable *replacements) {
|
|
|
|
JanetTable *env = (NULL != replacements) ? replacements : janet_table(0);
|
2019-06-24 16:44:13 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_PROP,
|
|
|
|
"propagate", 2, 2, 2, 2, propagate_asm, sizeof(propagate_asm),
|
|
|
|
JDOC("(propagate x fiber)\n\n"
|
|
|
|
"Propagate a signal from a fiber to the current fiber. The resulting "
|
|
|
|
"stack trace from the current fiber will include frames from fiber. If "
|
|
|
|
"fiber is in a state that can be resumed, resuming the current fiber will "
|
|
|
|
"first resume fiber."));
|
2019-03-12 04:23:14 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_DEBUG,
|
2019-11-24 23:45:53 +00:00
|
|
|
"debug", 1, 0, 1, 1, debug_asm, sizeof(debug_asm),
|
|
|
|
JDOC("(debug &opt x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Throws a debug signal that can be caught by a parent fiber and used to inspect "
|
2019-11-24 23:45:53 +00:00
|
|
|
"the running state of the current fiber. Returns the value passed in by resume."));
|
2019-03-12 04:23:14 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_ERROR,
|
|
|
|
"error", 1, 1, 1, 1, error_asm, sizeof(error_asm),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(error e)\n\n"
|
|
|
|
"Throws an error e that can be caught and handled by a parent fiber."));
|
2019-02-20 04:41:16 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_YIELD,
|
2019-03-12 04:23:14 +00:00
|
|
|
"yield", 1, 0, 1, 2, yield_asm, sizeof(yield_asm),
|
2019-11-24 23:45:53 +00:00
|
|
|
JDOC("(yield &opt x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Yield a value to a parent fiber. When a fiber yields, its execution is paused until "
|
|
|
|
"another thread resumes it. The fiber will then resume, and the last yield call will "
|
|
|
|
"return the value that was passed to resume."));
|
2019-02-20 04:41:16 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_RESUME,
|
2019-03-12 04:23:14 +00:00
|
|
|
"resume", 2, 1, 2, 2, resume_asm, sizeof(resume_asm),
|
|
|
|
JDOC("(resume fiber &opt x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Resume a new or suspended fiber and optionally pass in a value to the fiber that "
|
|
|
|
"will be returned to the last yield in the case of a pending fiber, or the argument to "
|
|
|
|
"the dispatch function in the case of a new fiber. Returns either the return result of "
|
|
|
|
"the fiber's dispatch function, or the value from the next yield call in fiber."));
|
2019-11-08 23:35:27 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_IN,
|
2019-12-01 23:35:54 +00:00
|
|
|
"in", 3, 2, 3, 4, in_asm, sizeof(in_asm),
|
|
|
|
JDOC("(in ds key &opt dflt)\n\n"
|
|
|
|
"Get value in ds at key, works on associative data structures. Arrays, tuples, tables, structs, "
|
|
|
|
"strings, symbols, and buffers are all associative and can be used. Arrays, tuples, strings, buffers, "
|
|
|
|
"and symbols must use integer keys that are in bounds or an error is raised. Structs and tables can "
|
|
|
|
"take any value as a key except nil and will return nil or dflt if not found."));
|
2019-03-12 04:23:14 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_PUT,
|
|
|
|
"put", 3, 3, 3, 3, put_asm, sizeof(put_asm),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(put ds key value)\n\n"
|
|
|
|
"Associate a key with a value in any mutable associative data structure. Indexed data structures "
|
|
|
|
"(arrays and buffers) only accept non-negative integer keys, and will expand if an out of bounds "
|
|
|
|
"value is provided. In an array, extra space will be filled with nils, and in a buffer, extra "
|
|
|
|
"space will be filled with 0 bytes. In a table, putting a key that is contained in the table prototype "
|
|
|
|
"will hide the association defined by the prototype, but will not mutate the prototype table. Putting "
|
|
|
|
"a value nil into a table will remove the key from the table. Returns the data structure ds."));
|
2019-03-12 04:23:14 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_LENGTH,
|
|
|
|
"length", 1, 1, 1, 1, length_asm, sizeof(length_asm),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(length ds)\n\n"
|
|
|
|
"Returns the length or count of a data structure in constant time as an integer. For "
|
|
|
|
"structs and tables, returns the number of key-value pairs in the data structure."));
|
2019-03-12 04:23:14 +00:00
|
|
|
janet_quick_asm(env, JANET_FUN_BNOT,
|
|
|
|
"bnot", 1, 1, 1, 1, bnot_asm, sizeof(bnot_asm),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(bnot x)\n\nReturns the bit-wise inverse of integer x."));
|
2018-08-26 16:53:39 +00:00
|
|
|
make_apply(env);
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Variadic ops */
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_varop(env, JANET_FUN_ADD, "+", 0, 0, JOP_ADD,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(+ & xs)\n\n"
|
|
|
|
"Returns the sum of all xs. xs must be integers or real numbers only. If xs is empty, return 0."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_varop(env, JANET_FUN_SUBTRACT, "-", 0, 0, JOP_SUBTRACT,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(- & xs)\n\n"
|
|
|
|
"Returns the difference of xs. If xs is empty, returns 0. If xs has one element, returns the "
|
|
|
|
"negative value of that element. Otherwise, returns the first element in xs minus the sum of "
|
|
|
|
"the rest of the elements."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_varop(env, JANET_FUN_MULTIPLY, "*", 1, 1, JOP_MULTIPLY,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(* & xs)\n\n"
|
|
|
|
"Returns the product of all elements in xs. If xs is empty, returns 1."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_varop(env, JANET_FUN_DIVIDE, "/", 1, 1, JOP_DIVIDE,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(/ & xs)\n\n"
|
|
|
|
"Returns the quotient of xs. If xs is empty, returns 1. If xs has one value x, returns "
|
|
|
|
"the reciprocal of x. Otherwise return the first value of xs repeatedly divided by the remaining "
|
|
|
|
"values. Division by two integers uses truncating division."));
|
2018-12-17 03:13:48 +00:00
|
|
|
templatize_varop(env, JANET_FUN_BAND, "band", -1, -1, JOP_BAND,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(band & xs)\n\n"
|
|
|
|
"Returns the bit-wise and of all values in xs. Each x in xs must be an integer."));
|
2018-12-17 03:13:48 +00:00
|
|
|
templatize_varop(env, JANET_FUN_BOR, "bor", 0, 0, JOP_BOR,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(bor & xs)\n\n"
|
|
|
|
"Returns the bit-wise or of all values in xs. Each x in xs must be an integer."));
|
2018-12-17 03:13:48 +00:00
|
|
|
templatize_varop(env, JANET_FUN_BXOR, "bxor", 0, 0, JOP_BXOR,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(bxor & xs)\n\n"
|
|
|
|
"Returns the bit-wise xor of all values in xs. Each in xs must be an integer."));
|
2018-12-17 03:13:48 +00:00
|
|
|
templatize_varop(env, JANET_FUN_LSHIFT, "blshift", 1, 1, JOP_SHIFT_LEFT,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(blshift x & shifts)\n\n"
|
|
|
|
"Returns the value of x bit shifted left by the sum of all values in shifts. x "
|
|
|
|
"and each element in shift must be an integer."));
|
2018-12-17 03:13:48 +00:00
|
|
|
templatize_varop(env, JANET_FUN_RSHIFT, "brshift", 1, 1, JOP_SHIFT_RIGHT,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(brshift x & shifts)\n\n"
|
|
|
|
"Returns the value of x bit shifted right by the sum of all values in shifts. x "
|
|
|
|
"and each element in shift must be an integer."));
|
2018-12-17 03:13:48 +00:00
|
|
|
templatize_varop(env, JANET_FUN_RSHIFTU, "brushift", 1, 1, JOP_SHIFT_RIGHT_UNSIGNED,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(brushift x & shifts)\n\n"
|
|
|
|
"Returns the value of x bit shifted right by the sum of all values in shifts. x "
|
|
|
|
"and each element in shift must be an integer. The sign of x is not preserved, so "
|
|
|
|
"for positive shifts the return value will always be positive."));
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Variadic comparators */
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_ORDER_GT, "order>", 0, JOP_GREATER_THAN,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(order> & xs)\n\n"
|
|
|
|
"Check if xs is strictly descending according to a total order "
|
|
|
|
"over all values. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_ORDER_LT, "order<", 0, JOP_LESS_THAN,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(order< & xs)\n\n"
|
|
|
|
"Check if xs is strictly increasing according to a total order "
|
|
|
|
"over all values. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_ORDER_GTE, "order>=", 1, JOP_LESS_THAN,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(order>= & xs)\n\n"
|
|
|
|
"Check if xs is not increasing according to a total order "
|
|
|
|
"over all values. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_ORDER_LTE, "order<=", 1, JOP_GREATER_THAN,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(order<= & xs)\n\n"
|
|
|
|
"Check if xs is not decreasing according to a total order "
|
|
|
|
"over all values. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_ORDER_EQ, "=", 0, JOP_EQUALS,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(= & xs)\n\n"
|
|
|
|
"Returns true if all values in xs are the same, false otherwise."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_ORDER_NEQ, "not=", 1, JOP_EQUALS,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(not= & xs)\n\n"
|
|
|
|
"Return true if any values in xs are not equal, otherwise false."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_GT, ">", 0, JOP_NUMERIC_GREATER_THAN,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(> & xs)\n\n"
|
|
|
|
"Check if xs is in numerically descending order. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_LT, "<", 0, JOP_NUMERIC_LESS_THAN,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(< & xs)\n\n"
|
|
|
|
"Check if xs is in numerically ascending order. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_GTE, ">=", 0, JOP_NUMERIC_GREATER_THAN_EQUAL,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(>= & xs)\n\n"
|
|
|
|
"Check if xs is in numerically non-ascending order. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_LTE, "<=", 0, JOP_NUMERIC_LESS_THAN_EQUAL,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(<= & xs)\n\n"
|
|
|
|
"Check if xs is in numerically non-descending order. Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_EQ, "==", 0, JOP_NUMERIC_EQUAL,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(== & xs)\n\n"
|
|
|
|
"Check if all values in xs are numerically equal (4.0 == 4). Returns a boolean."));
|
2018-11-16 21:24:10 +00:00
|
|
|
templatize_comparator(env, JANET_FUN_NEQ, "not==", 1, JOP_NUMERIC_EQUAL,
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("(not== & xs)\n\n"
|
|
|
|
"Check if any values in xs are not numerically equal (3.0 not== 4). Returns a boolean."));
|
2018-07-09 03:10:02 +00:00
|
|
|
|
2018-08-07 04:54:47 +00:00
|
|
|
/* Platform detection */
|
2018-12-01 03:49:21 +00:00
|
|
|
janet_def(env, "janet/version", janet_cstringv(JANET_VERSION),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("The version number of the running janet program."));
|
2018-12-08 19:17:03 +00:00
|
|
|
janet_def(env, "janet/build", janet_cstringv(JANET_BUILD),
|
2019-02-20 01:51:34 +00:00
|
|
|
JDOC("The build identifier of the running janet program."));
|
2019-05-30 01:57:03 +00:00
|
|
|
janet_def(env, "janet/config-bits", janet_wrap_integer(JANET_CURRENT_CONFIG_BITS),
|
|
|
|
JDOC("The flag set of config options from janetconf.h which is used to check "
|
|
|
|
"if native modules are compatible with the host program."));
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2019-01-06 06:49:56 +00:00
|
|
|
/* Allow references to the environment */
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_def(env, "_env", janet_wrap_table(env), JDOC("The environment table for the current scope."));
|
2019-01-06 06:49:56 +00:00
|
|
|
|
2019-11-27 05:13:53 +00:00
|
|
|
janet_load_libs(env);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gcroot(janet_wrap_table(env));
|
2019-11-27 05:13:53 +00:00
|
|
|
return env;
|
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2019-11-27 05:13:53 +00:00
|
|
|
#else
|
2019-02-20 23:15:48 +00:00
|
|
|
|
2019-11-27 05:13:53 +00:00
|
|
|
JanetTable *janet_core_dictionary(JanetTable *replacements) {
|
2019-12-02 02:28:12 +00:00
|
|
|
JanetTable *dict;
|
|
|
|
if (NULL == janet_vm_core_dictionary) {
|
|
|
|
dict = janet_table(0);
|
|
|
|
janet_load_libs(dict);
|
|
|
|
janet_vm_core_dictionary = dict;
|
|
|
|
janet_gcroot(janet_wrap_table(dict));
|
|
|
|
/* do replacements */
|
|
|
|
if (NULL != replacements) {
|
|
|
|
for (int32_t i = 0; i < replacements->capacity; i++) {
|
|
|
|
if (!janet_checktype(replacements->data[i].key, JANET_NIL)) {
|
|
|
|
const JanetKV *kv = replacements->data + i;
|
|
|
|
janet_table_put(dict, kv->key, kv->value);
|
|
|
|
if (janet_checktype(kv->value, JANET_CFUNCTION)) {
|
|
|
|
janet_table_put(janet_vm_registry, kv->value, kv->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dict = janet_vm_core_dictionary;
|
|
|
|
}
|
2019-11-27 05:13:53 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
JanetTable *janet_core_env(JanetTable *replacements) {
|
|
|
|
JanetTable *dict = janet_core_dictionary(replacements);
|
2019-02-22 15:10:41 +00:00
|
|
|
Janet marsh_out = janet_unmarshal(
|
|
|
|
janet_core_image,
|
|
|
|
janet_core_image_size,
|
|
|
|
0,
|
2019-11-27 05:13:53 +00:00
|
|
|
dict,
|
2019-02-22 15:10:41 +00:00
|
|
|
NULL);
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_gcroot(marsh_out);
|
2019-11-27 05:13:53 +00:00
|
|
|
return janet_unwrap_table(marsh_out);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2019-11-27 05:13:53 +00:00
|
|
|
|
|
|
|
#endif
|