2019-02-08 00:44:30 -05:00
|
|
|
/*
|
2021-05-31 13:46:02 -05:00
|
|
|
* Copyright (c) 2021 Calvin Rose
|
2019-02-08 00:44:30 -05: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-02-18 20:13:35 -05:00
|
|
|
#include <janet.h>
|
2019-02-21 11:22:29 -05:00
|
|
|
#include "tests.h"
|
2019-02-08 00:44:30 -05:00
|
|
|
|
2020-01-28 23:38:52 -06:00
|
|
|
#ifdef JANET_WINDOWS
|
2020-01-28 23:46:14 -06:00
|
|
|
#include <direct.h>
|
2020-01-28 23:38:52 -06:00
|
|
|
#define chdir(x) _chdir(x)
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2019-02-08 00:44:30 -05:00
|
|
|
extern const unsigned char *janet_gen_boot;
|
|
|
|
extern int32_t janet_gen_boot_size;
|
|
|
|
|
2019-03-22 18:07:10 -04:00
|
|
|
int main(int argc, const char **argv) {
|
2019-02-08 00:44:30 -05:00
|
|
|
|
2019-02-21 11:22:29 -05:00
|
|
|
/* Init janet */
|
2019-02-08 00:44:30 -05:00
|
|
|
janet_init();
|
2019-02-21 11:22:29 -05:00
|
|
|
|
|
|
|
/* Run tests */
|
|
|
|
array_test();
|
|
|
|
buffer_test();
|
|
|
|
number_test();
|
|
|
|
system_test();
|
|
|
|
table_test();
|
|
|
|
|
|
|
|
/* C tests passed */
|
|
|
|
|
|
|
|
/* Set up VM */
|
|
|
|
int status;
|
|
|
|
JanetTable *env;
|
2019-03-08 11:39:18 -05:00
|
|
|
|
|
|
|
env = janet_core_env(NULL);
|
2019-02-08 00:44:30 -05:00
|
|
|
|
2019-03-22 18:07:10 -04:00
|
|
|
/* Create args tuple */
|
|
|
|
JanetArray *args = janet_array(argc);
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
janet_array_push(args, janet_cstringv(argv[i]));
|
2019-07-20 16:57:07 -05:00
|
|
|
janet_def(env, "boot/args", janet_wrap_array(args), "Command line arguments.");
|
2019-03-22 18:07:10 -04:00
|
|
|
|
2019-06-19 19:43:38 -04:00
|
|
|
/* Add in options from janetconf.h so boot.janet can configure the image as needed. */
|
|
|
|
JanetTable *opts = janet_table(0);
|
|
|
|
#ifdef JANET_NO_DOCSTRINGS
|
|
|
|
janet_table_put(opts, janet_ckeywordv("no-docstrings"), janet_wrap_true());
|
|
|
|
#endif
|
|
|
|
#ifdef JANET_NO_SOURCEMAPS
|
|
|
|
janet_table_put(opts, janet_ckeywordv("no-sourcemaps"), janet_wrap_true());
|
|
|
|
#endif
|
2019-07-20 16:57:07 -05:00
|
|
|
janet_def(env, "boot/config", janet_wrap_table(opts), "Boot options");
|
2019-06-19 19:43:38 -04:00
|
|
|
|
2019-02-08 00:44:30 -05:00
|
|
|
/* Run bootstrap script to generate core image */
|
2020-01-28 23:38:52 -06:00
|
|
|
const char *boot_filename;
|
2019-06-20 11:52:43 -04:00
|
|
|
#ifdef JANET_NO_SOURCEMAPS
|
2020-01-28 23:38:52 -06:00
|
|
|
boot_filename = NULL;
|
2019-06-20 11:52:43 -04:00
|
|
|
#else
|
2020-01-28 23:38:52 -06:00
|
|
|
boot_filename = "boot.janet";
|
2019-06-20 11:52:43 -04:00
|
|
|
#endif
|
2020-01-28 23:38:52 -06:00
|
|
|
|
2020-01-28 23:46:14 -06:00
|
|
|
int chdir_status = chdir(argv[1]);
|
|
|
|
if (chdir_status) {
|
|
|
|
fprintf(stderr, "Could not change to directory %s\n", argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-01-28 23:38:52 -06:00
|
|
|
|
|
|
|
FILE *boot_file = fopen("src/boot/boot.janet", "rb");
|
|
|
|
if (NULL == boot_file) {
|
|
|
|
fprintf(stderr, "Could not open src/boot/boot.janet\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Slurp file into buffer */
|
|
|
|
fseek(boot_file, 0, SEEK_END);
|
|
|
|
size_t boot_size = ftell(boot_file);
|
|
|
|
fseek(boot_file, 0, SEEK_SET);
|
2021-03-23 23:00:48 +13:00
|
|
|
unsigned char *boot_buffer = janet_malloc(boot_size);
|
2020-01-28 23:38:52 -06:00
|
|
|
if (NULL == boot_buffer) {
|
|
|
|
fprintf(stderr, "Failed to allocate boot buffer\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!fread(boot_buffer, 1, boot_size, boot_file)) {
|
|
|
|
fprintf(stderr, "Failed to read into boot buffer\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fclose(boot_file);
|
|
|
|
|
2020-02-23 15:27:57 -06:00
|
|
|
status = janet_dobytes(env, boot_buffer, (int32_t) boot_size, boot_filename, NULL);
|
2021-03-23 23:00:48 +13:00
|
|
|
janet_free(boot_buffer);
|
2019-02-08 00:44:30 -05:00
|
|
|
|
|
|
|
/* Deinitialize vm */
|
|
|
|
janet_deinit();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|