mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 01:37:19 +00:00
Merge remote-tracking branch 'upstream/master' into crazy-brackets
This commit is contained in:
commit
86ba69c16b
@ -2,6 +2,8 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
## 0.4.0 - ??
|
## 0.4.0 - ??
|
||||||
|
- Add partition function to core library.
|
||||||
|
- Pre-compile core library into an image for faster startup.
|
||||||
- Add methods to parser values that mirror the api.
|
- Add methods to parser values that mirror the api.
|
||||||
- Add janet\_getmethod to CAPI for easier use of method like syntax.
|
- Add janet\_getmethod to CAPI for easier use of method like syntax.
|
||||||
- Add get/set to abstract types to allow them to behave more
|
- Add get/set to abstract types to allow them to behave more
|
||||||
|
37
Makefile
37
Makefile
@ -47,7 +47,7 @@ else
|
|||||||
CLIBS:=$(CLIBS) -lrt
|
CLIBS:=$(CLIBS) -lrt
|
||||||
endif
|
endif
|
||||||
|
|
||||||
$(shell mkdir -p build/core build/mainclient build/webclient)
|
$(shell mkdir -p build/core build/mainclient build/webclient build/boot)
|
||||||
|
|
||||||
# Source headers
|
# Source headers
|
||||||
JANET_HEADERS=$(sort $(wildcard src/include/janet/*.h))
|
JANET_HEADERS=$(sort $(wildcard src/include/janet/*.h))
|
||||||
@ -60,14 +60,33 @@ JANET_WEBCLIENT_SOURCES=$(sort $(wildcard src/webclient/*.c))
|
|||||||
|
|
||||||
all: $(JANET_TARGET) $(JANET_LIBRARY)
|
all: $(JANET_TARGET) $(JANET_LIBRARY)
|
||||||
|
|
||||||
|
##################################################################
|
||||||
|
##### The bootstrap interpreter that compiles the core image #####
|
||||||
|
##################################################################
|
||||||
|
|
||||||
|
JANET_BOOT_OBJECTS=$(patsubst src/%.c,build/%.boot.o,$(JANET_CORE_SOURCES) src/boot/boot.c) \
|
||||||
|
build/core.gen.o \
|
||||||
|
build/boot.gen.o
|
||||||
|
|
||||||
|
build/%.boot.o: src/%.c
|
||||||
|
$(CC) $(CFLAGS) -DJANET_BOOTSTRAP -o $@ -c $<
|
||||||
|
|
||||||
|
build/janet_boot: $(JANET_BOOT_OBJECTS)
|
||||||
|
$(CC) $(CFLAGS) -DJANET_BOOTSTRAP -o $@ $^ $(CLIBS)
|
||||||
|
|
||||||
|
# Now the reason we bootstrap in the first place
|
||||||
|
build/core_image.c: build/janet_boot
|
||||||
|
build/janet_boot
|
||||||
|
|
||||||
##########################################################
|
##########################################################
|
||||||
##### The main interpreter program and shared object #####
|
##### The main interpreter program and shared object #####
|
||||||
##########################################################
|
##########################################################
|
||||||
|
|
||||||
JANET_CORE_OBJECTS=$(patsubst src/%.c,build/%.o,$(JANET_CORE_SOURCES)) build/core.gen.o
|
JANET_CORE_OBJECTS=$(patsubst src/%.c,build/%.o,$(JANET_CORE_SOURCES)) build/core_image.o
|
||||||
JANET_MAINCLIENT_OBJECTS=$(patsubst src/%.c,build/%.o,$(JANET_MAINCLIENT_SOURCES)) build/init.gen.o
|
JANET_MAINCLIENT_OBJECTS=$(patsubst src/%.c,build/%.o,$(JANET_MAINCLIENT_SOURCES)) build/init.gen.o
|
||||||
|
|
||||||
%.gen.o: %.gen.c
|
# Compile the core image generated by the bootstrap build
|
||||||
|
build/core_image.o: build/core_image.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS)
|
||||||
$(CC) $(CFLAGS) -o $@ -c $<
|
$(CC) $(CFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
build/%.o: src/%.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS)
|
build/%.o: src/%.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS)
|
||||||
@ -92,11 +111,14 @@ EMCFLAGS=-std=c99 -Wall -Wextra -Isrc/include -O2 \
|
|||||||
JANET_EMTARGET=build/janet.js
|
JANET_EMTARGET=build/janet.js
|
||||||
JANET_WEB_SOURCES=$(JANET_CORE_SOURCES) $(JANET_WEBCLIENT_SOURCES)
|
JANET_WEB_SOURCES=$(JANET_CORE_SOURCES) $(JANET_WEBCLIENT_SOURCES)
|
||||||
JANET_EMOBJECTS=$(patsubst src/%.c,build/%.bc,$(JANET_WEB_SOURCES)) \
|
JANET_EMOBJECTS=$(patsubst src/%.c,build/%.bc,$(JANET_WEB_SOURCES)) \
|
||||||
build/webinit.gen.bc build/core.gen.bc
|
build/webinit.gen.bc build/core_image.bc
|
||||||
|
|
||||||
%.gen.bc: %.gen.c
|
%.gen.bc: %.gen.c
|
||||||
$(EMCC) $(EMCFLAGS) -o $@ -c $<
|
$(EMCC) $(EMCFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
|
build/core_image.bc: build/core_image.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS)
|
||||||
|
$(EMCC) $(EMCFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
build/%.bc: src/%.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS)
|
build/%.bc: src/%.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS)
|
||||||
$(EMCC) $(EMCFLAGS) -o $@ -c $<
|
$(EMCC) $(EMCFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
@ -109,6 +131,9 @@ emscripten: $(JANET_EMTARGET)
|
|||||||
##### Generated C files #####
|
##### Generated C files #####
|
||||||
#############################
|
#############################
|
||||||
|
|
||||||
|
%.gen.o: %.gen.c
|
||||||
|
$(CC) $(CFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
build/xxd: tools/xxd.c
|
build/xxd: tools/xxd.c
|
||||||
$(CC) $< -o $@
|
$(CC) $< -o $@
|
||||||
|
|
||||||
@ -118,12 +143,14 @@ build/init.gen.c: src/mainclient/init.janet build/xxd
|
|||||||
build/xxd $< $@ janet_gen_init
|
build/xxd $< $@ janet_gen_init
|
||||||
build/webinit.gen.c: src/webclient/webinit.janet build/xxd
|
build/webinit.gen.c: src/webclient/webinit.janet build/xxd
|
||||||
build/xxd $< $@ janet_gen_webinit
|
build/xxd $< $@ janet_gen_webinit
|
||||||
|
build/boot.gen.c: src/boot/boot.janet build/xxd
|
||||||
|
build/xxd $< $@ janet_gen_boot
|
||||||
|
|
||||||
########################
|
########################
|
||||||
##### Amalgamation #####
|
##### Amalgamation #####
|
||||||
########################
|
########################
|
||||||
|
|
||||||
amalg: build/janet.c build/janet.h
|
amalg: build/janet.c build/janet.h build/core_image.c
|
||||||
|
|
||||||
build/janet.c: $(JANET_LOCAL_HEADERS) $(JANET_CORE_SOURCES) tools/amalg.janet $(JANET_TARGET)
|
build/janet.c: $(JANET_LOCAL_HEADERS) $(JANET_CORE_SOURCES) tools/amalg.janet $(JANET_TARGET)
|
||||||
$(JANET_TARGET) tools/amalg.janet > $@
|
$(JANET_TARGET) tools/amalg.janet > $@
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
mkdir build
|
mkdir build
|
||||||
mkdir build\core
|
mkdir build\core
|
||||||
mkdir build\mainclient
|
mkdir build\mainclient
|
||||||
|
mkdir build\boot
|
||||||
|
|
||||||
@rem Build the xxd tool for generating sources
|
@rem Build the xxd tool for generating sources
|
||||||
@cl /nologo /c tools/xxd.c /Fobuild\xxd.obj
|
@cl /nologo /c tools/xxd.c /Fobuild\xxd.obj
|
||||||
@ -34,12 +35,33 @@ mkdir build\mainclient
|
|||||||
@if errorlevel 1 goto :BUILDFAIL
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
@build\xxd.exe src\mainclient\init.janet build\init.gen.c janet_gen_init
|
@build\xxd.exe src\mainclient\init.janet build\init.gen.c janet_gen_init
|
||||||
@if errorlevel 1 goto :BUILDFAIL
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
@build\xxd.exe src\boot\boot.janet build\boot.gen.c janet_gen_boot
|
||||||
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
|
||||||
@rem Build the generated sources
|
@rem Build the generated sources
|
||||||
@%JANET_COMPILE% /Fobuild\core\core.gen.obj build\core.gen.c
|
@%JANET_COMPILE% /Fobuild\boot\core.gen.obj build\core.gen.c
|
||||||
@if errorlevel 1 goto :BUILDFAIL
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
@%JANET_COMPILE% /Fobuild\mainclient\init.gen.obj build\init.gen.c
|
@%JANET_COMPILE% /Fobuild\mainclient\init.gen.obj build\init.gen.c
|
||||||
@if errorlevel 1 goto :BUILDFAIL
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
@%JANET_COMPILE% /Fobuild\boot\boot.gen.obj build\boot.gen.c
|
||||||
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
|
||||||
|
@rem Build the bootstrap interpretter
|
||||||
|
for %%f in (src\core\*.c) do (
|
||||||
|
@%JANET_COMPILE% /DJANET_BOOTSTRAP /Fobuild\boot\%%~nf.obj %%f
|
||||||
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
)
|
||||||
|
for %%f in (src\boot\*.c) do (
|
||||||
|
@%JANET_COMPILE% /DJANET_BOOTSTRAP /Fobuild\boot\%%~nf.obj %%f
|
||||||
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
)
|
||||||
|
%JANET_LINK% /out:build\janet_boot.exe build\boot\*.obj
|
||||||
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
build\janet_boot
|
||||||
|
|
||||||
|
@rem Build the core image
|
||||||
|
@%JANET_COMPILE% /Fobuild\core_image.obj build\core_image.c
|
||||||
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
|
||||||
@rem Build the sources
|
@rem Build the sources
|
||||||
for %%f in (src\core\*.c) do (
|
for %%f in (src\core\*.c) do (
|
||||||
@ -54,7 +76,7 @@ for %%f in (src\mainclient\*.c) do (
|
|||||||
)
|
)
|
||||||
|
|
||||||
@rem Link everything to main client
|
@rem Link everything to main client
|
||||||
%JANET_LINK% /out:janet.exe build\core\*.obj build\mainclient\*.obj
|
%JANET_LINK% /out:janet.exe build\core\*.obj build\mainclient\*.obj build\core_image.obj
|
||||||
@if errorlevel 1 goto :BUILDFAIL
|
@if errorlevel 1 goto :BUILDFAIL
|
||||||
|
|
||||||
echo === Successfully built janet.exe for Windows ===
|
echo === Successfully built janet.exe for Windows ===
|
||||||
|
43
src/boot/boot.c
Normal file
43
src/boot/boot.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019 Calvin Rose
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <janet/janet.h>
|
||||||
|
|
||||||
|
extern const unsigned char *janet_gen_boot;
|
||||||
|
extern int32_t janet_gen_boot_size;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int status;
|
||||||
|
JanetTable *env;
|
||||||
|
|
||||||
|
/* Set up VM */
|
||||||
|
janet_init();
|
||||||
|
env = janet_core_env();
|
||||||
|
|
||||||
|
/* Run bootstrap script to generate core image */
|
||||||
|
status = janet_dobytes(env, janet_gen_boot, janet_gen_boot_size, "boot.janet", NULL);
|
||||||
|
|
||||||
|
/* Deinitialize vm */
|
||||||
|
janet_deinit();
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
40
src/boot/boot.janet
Normal file
40
src/boot/boot.janet
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
# Copyright (C) Calvin Rose 2019
|
||||||
|
|
||||||
|
# The bootstrap script is used to produce the source file for
|
||||||
|
# embedding the core image.
|
||||||
|
|
||||||
|
# Tool to dump a marshalled version of the janet core to stdout. The
|
||||||
|
# image should eventually allow janet to be started from a pre-compiled
|
||||||
|
# image rather than recompiled every time from the embedded source. More
|
||||||
|
# work will go into shrinking the image (it isn't currently that large but
|
||||||
|
# could be smaller), creating the mechanism to load the image, and modifying
|
||||||
|
# the build process to compile janet with a built image rather than
|
||||||
|
# embedded source.
|
||||||
|
|
||||||
|
# Get image. This image contains as much of the core library and documentation that
|
||||||
|
# can be written to an image (no cfunctions, no abstracts (stdout, stdin, stderr)),
|
||||||
|
# everything else goes. Cfunctions and abstracts will be referenced from a registry
|
||||||
|
# table which will be generated on janet startup.
|
||||||
|
(do
|
||||||
|
(def image (let [env-pairs (pairs (env-lookup *env*))
|
||||||
|
essential-pairs (filter (fn [[k v]] (or (cfunction? v) (abstract? v))) env-pairs)
|
||||||
|
lookup (table ;(mapcat identity essential-pairs))
|
||||||
|
reverse-lookup (invert lookup)]
|
||||||
|
(marshal *env* reverse-lookup)))
|
||||||
|
|
||||||
|
# Create C source file that contains images a uint8_t buffer. This
|
||||||
|
# can be compiled and linked statically into the main janet library
|
||||||
|
# and example client.
|
||||||
|
(def chunks (seq [b :in image] (string b)))
|
||||||
|
(def image-file (file/open "build/core_image.c" :w))
|
||||||
|
(file/write image-file
|
||||||
|
"#include <janet/janet.h>\n"
|
||||||
|
"static const unsigned char janet_core_image_bytes[] = {")
|
||||||
|
(loop [line :in (partition 16 chunks)]
|
||||||
|
(def str (string ;(interpose ", " line)))
|
||||||
|
(file/write image-file str ",\n"))
|
||||||
|
(file/write image-file
|
||||||
|
"0};\n\n"
|
||||||
|
"const unsigned char *janet_core_image = janet_core_image_bytes;\n"
|
||||||
|
"size_t janet_core_image_size = sizeof(janet_core_image_bytes);\n")
|
||||||
|
(file/close image-file))
|
@ -267,5 +267,5 @@ static const JanetReg array_cfuns[] = {
|
|||||||
|
|
||||||
/* Load the array module */
|
/* Load the array module */
|
||||||
void janet_lib_array(JanetTable *env) {
|
void janet_lib_array(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, array_cfuns);
|
janet_core_cfuns(env, NULL, array_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -951,7 +951,7 @@ static const JanetReg asm_cfuns[] = {
|
|||||||
|
|
||||||
/* Load the library */
|
/* Load the library */
|
||||||
void janet_lib_asm(JanetTable *env) {
|
void janet_lib_asm(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, asm_cfuns);
|
janet_core_cfuns(env, NULL, asm_cfuns);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -387,5 +387,5 @@ static const JanetReg buffer_cfuns[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void janet_lib_buffer(JanetTable *env) {
|
void janet_lib_buffer(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, buffer_cfuns);
|
janet_core_cfuns(env, NULL, buffer_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -749,5 +749,5 @@ static const JanetReg compile_cfuns[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void janet_lib_compile(JanetTable *env) {
|
void janet_lib_compile(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, compile_cfuns);
|
janet_core_cfuns(env, NULL, compile_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -1067,6 +1067,20 @@ value, one key will be ignored."
|
|||||||
(++ i))
|
(++ i))
|
||||||
ret)
|
ret)
|
||||||
|
|
||||||
|
(defn partition
|
||||||
|
"Partition an indexed data structure into tuples
|
||||||
|
of size n. Returns a new array."
|
||||||
|
[n ind]
|
||||||
|
(var i 0) (var nextn n)
|
||||||
|
(def len (length ind))
|
||||||
|
(def ret (array/new (math/ceil (/ len n))))
|
||||||
|
(while (<= nextn len)
|
||||||
|
(array/push ret (tuple/slice ind i nextn))
|
||||||
|
(set i nextn)
|
||||||
|
(+= nextn n))
|
||||||
|
(if (not= i len) (array/push ret (tuple/slice ind i)))
|
||||||
|
ret)
|
||||||
|
|
||||||
###
|
###
|
||||||
###
|
###
|
||||||
### Pattern Matching
|
### Pattern Matching
|
||||||
|
@ -28,8 +28,13 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Generated bytes */
|
/* Generated bytes */
|
||||||
|
#ifdef JANET_BOOTSTRAP
|
||||||
extern const unsigned char *janet_gen_core;
|
extern const unsigned char *janet_gen_core;
|
||||||
extern int32_t janet_gen_core_size;
|
extern int32_t janet_gen_core_size;
|
||||||
|
#else
|
||||||
|
extern const unsigned char *janet_core_image;
|
||||||
|
extern size_t janet_core_image_size;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
|
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
|
||||||
* with native code. */
|
* with native code. */
|
||||||
@ -371,7 +376,7 @@ static const JanetReg corelib_cfuns[] = {
|
|||||||
{NULL, NULL, NULL}
|
{NULL, NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef JANET_NO_BOOTSTRAP
|
#ifdef JANET_BOOTSTRAP
|
||||||
|
|
||||||
/* Utility for inline assembly */
|
/* Utility for inline assembly */
|
||||||
static void janet_quick_asm(
|
static void janet_quick_asm(
|
||||||
@ -595,12 +600,9 @@ static const uint32_t bnot_asm[] = {
|
|||||||
|
|
||||||
JanetTable *janet_core_env(void) {
|
JanetTable *janet_core_env(void) {
|
||||||
JanetTable *env = janet_table(0);
|
JanetTable *env = janet_table(0);
|
||||||
Janet ret = janet_wrap_table(env);
|
janet_core_cfuns(env, NULL, corelib_cfuns);
|
||||||
|
|
||||||
/* Load main functions */
|
#ifdef JANET_BOOTSTRAP
|
||||||
janet_cfuns(env, NULL, corelib_cfuns);
|
|
||||||
|
|
||||||
#ifndef JANET_NO_BOOTSTRAP
|
|
||||||
janet_quick_asm(env, JANET_FUN_YIELD, "debug", 0, 1, debug_asm, sizeof(debug_asm),
|
janet_quick_asm(env, JANET_FUN_YIELD, "debug", 0, 1, debug_asm, sizeof(debug_asm),
|
||||||
JDOC("(debug)\n\n"
|
JDOC("(debug)\n\n"
|
||||||
"Throws a debug signal that can be caught by a parent fiber and used to inspect "
|
"Throws a debug signal that can be caught by a parent fiber and used to inspect "
|
||||||
@ -731,11 +733,11 @@ JanetTable *janet_core_env(void) {
|
|||||||
JDOC("The build identifier of the running janet program."));
|
JDOC("The build identifier of the running janet program."));
|
||||||
|
|
||||||
/* Allow references to the environment */
|
/* Allow references to the environment */
|
||||||
janet_def(env, "_env", ret, JDOC("The environment table for the current scope."));
|
janet_def(env, "_env", janet_wrap_table(env), JDOC("The environment table for the current scope."));
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Set as gc root */
|
/* Set as gc root */
|
||||||
janet_gcroot(janet_wrap_table(env));
|
janet_gcroot(janet_wrap_table(env));
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Load auxiliary envs */
|
/* Load auxiliary envs */
|
||||||
janet_lib_io(env);
|
janet_lib_io(env);
|
||||||
@ -756,9 +758,26 @@ JanetTable *janet_core_env(void) {
|
|||||||
janet_lib_asm(env);
|
janet_lib_asm(env);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef JANET_NO_BOOTSTRAP
|
#ifdef JANET_BOOTSTRAP
|
||||||
/* Run bootstrap source */
|
/* Run bootstrap source */
|
||||||
janet_dobytes(env, janet_gen_core, janet_gen_core_size, "core.janet", NULL);
|
janet_dobytes(env, janet_gen_core, janet_gen_core_size, "core.janet", NULL);
|
||||||
|
#else
|
||||||
|
|
||||||
|
/* Unmarshal from core image */
|
||||||
|
Janet marsh_out;
|
||||||
|
int status = janet_unmarshal(
|
||||||
|
janet_core_image,
|
||||||
|
janet_core_image_size,
|
||||||
|
0,
|
||||||
|
&marsh_out,
|
||||||
|
env,
|
||||||
|
NULL);
|
||||||
|
if (status) {
|
||||||
|
printf("error unmarshaling core image\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
janet_gcroot(marsh_out);
|
||||||
|
env = janet_unwrap_table(marsh_out);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return env;
|
return env;
|
||||||
|
@ -376,5 +376,5 @@ static const JanetReg debug_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_debug(JanetTable *env) {
|
void janet_lib_debug(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, debug_cfuns);
|
janet_core_cfuns(env, NULL, debug_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -433,5 +433,5 @@ static const JanetReg fiber_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_fiber(JanetTable *env) {
|
void janet_lib_fiber(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, fiber_cfuns);
|
janet_core_cfuns(env, NULL, fiber_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -394,18 +394,18 @@ static const JanetReg io_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_io(JanetTable *env) {
|
void janet_lib_io(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, io_cfuns);
|
janet_core_cfuns(env, NULL, io_cfuns);
|
||||||
|
|
||||||
/* stdout */
|
/* stdout */
|
||||||
janet_def(env, "stdout",
|
janet_core_def(env, "stdout",
|
||||||
makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
||||||
JDOC("The standard output file."));
|
JDOC("The standard output file."));
|
||||||
/* stderr */
|
/* stderr */
|
||||||
janet_def(env, "stderr",
|
janet_core_def(env, "stderr",
|
||||||
makef(stderr, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
makef(stderr, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
||||||
JDOC("The standard error file."));
|
JDOC("The standard error file."));
|
||||||
/* stdin */
|
/* stdin */
|
||||||
janet_def(env, "stdin",
|
janet_core_def(env, "stdin",
|
||||||
makef(stdin, IO_READ | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
makef(stdin, IO_READ | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
||||||
JDOC("The standard input file."));
|
JDOC("The standard input file."));
|
||||||
}
|
}
|
||||||
|
@ -124,15 +124,20 @@ JanetTable *janet_env_lookup(JanetTable *env) {
|
|||||||
|
|
||||||
/* Marshal an integer onto the buffer */
|
/* Marshal an integer onto the buffer */
|
||||||
static void pushint(MarshalState *st, int32_t x) {
|
static void pushint(MarshalState *st, int32_t x) {
|
||||||
if (x >= 0 && x < 200) {
|
if (x >= 0 && x < 128) {
|
||||||
janet_buffer_push_u8(st->buf, x);
|
janet_buffer_push_u8(st->buf, x);
|
||||||
|
} else if (x <= 8191 && x >= -8192) {
|
||||||
|
uint8_t intbuf[2];
|
||||||
|
intbuf[0] = ((x >> 8) & 0x3F) | 0x80;
|
||||||
|
intbuf[1] = x & 0xFF;
|
||||||
|
janet_buffer_push_bytes(st->buf, intbuf, 2);
|
||||||
} else {
|
} else {
|
||||||
uint8_t intbuf[5];
|
uint8_t intbuf[5];
|
||||||
intbuf[0] = LB_INTEGER;
|
intbuf[0] = LB_INTEGER;
|
||||||
intbuf[1] = x & 0xFF;
|
intbuf[1] = (x >> 24) & 0xFF;
|
||||||
intbuf[2] = (x >> 8) & 0xFF;
|
intbuf[2] = (x >> 16) & 0xFF;
|
||||||
intbuf[3] = (x >> 16) & 0xFF;
|
intbuf[3] = (x >> 8) & 0xFF;
|
||||||
intbuf[4] = (x >> 24) & 0xFF;
|
intbuf[4] = x & 0xFF;
|
||||||
janet_buffer_push_bytes(st->buf, intbuf, 5);
|
janet_buffer_push_bytes(st->buf, intbuf, 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -234,10 +239,12 @@ static void marshal_one_def(MarshalState *st, JanetFuncDef *def, int flags) {
|
|||||||
|
|
||||||
/* marshal source maps if needed */
|
/* marshal source maps if needed */
|
||||||
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCEMAP) {
|
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCEMAP) {
|
||||||
|
int32_t current = 0;
|
||||||
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
||||||
JanetSourceMapping map = def->sourcemap[i];
|
JanetSourceMapping map = def->sourcemap[i];
|
||||||
pushint(st, map.start);
|
pushint(st, map.start - current);
|
||||||
pushint(st, map.end);
|
pushint(st, map.end - map.start);
|
||||||
|
current = map.end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -546,14 +553,19 @@ static int32_t readint(UnmarshalState *st, const uint8_t **atdata) {
|
|||||||
const uint8_t *data = *atdata;
|
const uint8_t *data = *atdata;
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
if (data >= st->end) longjmp(st->err, UMR_EOS);
|
if (data >= st->end) longjmp(st->err, UMR_EOS);
|
||||||
if (*data < 200) {
|
if (*data < 128) {
|
||||||
ret = *data++;
|
ret = *data++;
|
||||||
|
} else if (*data < 192) {
|
||||||
|
if (data + 2 > st->end) longjmp(st->err, UMR_EOS);
|
||||||
|
ret = ((data[0] & 0x3F) << 8) + data[1];
|
||||||
|
ret = ((ret << 18) >> 18);
|
||||||
|
data += 2;
|
||||||
} else if (*data == LB_INTEGER) {
|
} else if (*data == LB_INTEGER) {
|
||||||
if (data + 5 > st->end) longjmp(st->err, UMR_EOS);
|
if (data + 5 > st->end) longjmp(st->err, UMR_EOS);
|
||||||
ret = (data[1]) |
|
ret = (data[1] << 24) |
|
||||||
(data[2] << 8) |
|
(data[2] << 16) |
|
||||||
(data[3] << 16) |
|
(data[3] << 8) |
|
||||||
(data[4] << 24);
|
data[4];
|
||||||
data += 5;
|
data += 5;
|
||||||
} else {
|
} else {
|
||||||
longjmp(st->err, UMR_EXPECTED_INTEGER);
|
longjmp(st->err, UMR_EXPECTED_INTEGER);
|
||||||
@ -748,13 +760,16 @@ static const uint8_t *unmarshal_one_def(
|
|||||||
|
|
||||||
/* Unmarshal source maps if needed */
|
/* Unmarshal source maps if needed */
|
||||||
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCEMAP) {
|
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCEMAP) {
|
||||||
|
int32_t current = 0;
|
||||||
def->sourcemap = malloc(sizeof(JanetSourceMapping) * bytecode_length);
|
def->sourcemap = malloc(sizeof(JanetSourceMapping) * bytecode_length);
|
||||||
if (!def->sourcemap) {
|
if (!def->sourcemap) {
|
||||||
JANET_OUT_OF_MEMORY;
|
JANET_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
for (int32_t i = 0; i < bytecode_length; i++) {
|
for (int32_t i = 0; i < bytecode_length; i++) {
|
||||||
def->sourcemap[i].start = readint(st, &data);
|
current += readint(st, &data);
|
||||||
def->sourcemap[i].end = readint(st, &data);
|
def->sourcemap[i].start = current;
|
||||||
|
current += readint(st, &data);
|
||||||
|
def->sourcemap[i].end = current;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
def->sourcemap = NULL;
|
def->sourcemap = NULL;
|
||||||
@ -905,8 +920,8 @@ static const uint8_t *unmarshal_one(
|
|||||||
EXTRA(1);
|
EXTRA(1);
|
||||||
lead = data[0];
|
lead = data[0];
|
||||||
if (lead < 200) {
|
if (lead < 200) {
|
||||||
*out = janet_wrap_integer(lead);
|
*out = janet_wrap_integer(readint(st, &data));
|
||||||
return data + 1;
|
return data;
|
||||||
}
|
}
|
||||||
switch (lead) {
|
switch (lead) {
|
||||||
case LB_NIL:
|
case LB_NIL:
|
||||||
@ -1175,5 +1190,5 @@ static const JanetReg marsh_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_marsh(JanetTable *env) {
|
void janet_lib_marsh(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, marsh_cfuns);
|
janet_core_cfuns(env, NULL, marsh_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -181,8 +181,8 @@ static const JanetReg math_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_math(JanetTable *env) {
|
void janet_lib_math(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, math_cfuns);
|
janet_core_cfuns(env, NULL, math_cfuns);
|
||||||
#ifndef JANET_NO_BOOTSTRAP
|
#ifdef JANET_BOOTSTRAP
|
||||||
janet_def(env, "math/pi", janet_wrap_number(3.1415926535897931),
|
janet_def(env, "math/pi", janet_wrap_number(3.1415926535897931),
|
||||||
JDOC("The value pi."));
|
JDOC("The value pi."));
|
||||||
janet_def(env, "math/e", janet_wrap_number(2.7182818284590451),
|
janet_def(env, "math/e", janet_wrap_number(2.7182818284590451),
|
||||||
|
@ -378,5 +378,5 @@ static const JanetReg os_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_os(JanetTable *env) {
|
void janet_lib_os(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, os_cfuns);
|
janet_core_cfuns(env, NULL, os_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -889,5 +889,5 @@ static const JanetReg parse_cfuns[] = {
|
|||||||
|
|
||||||
/* Load the library */
|
/* Load the library */
|
||||||
void janet_lib_parse(JanetTable *env) {
|
void janet_lib_parse(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, parse_cfuns);
|
janet_core_cfuns(env, NULL, parse_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -1103,5 +1103,5 @@ static const JanetReg peg_cfuns[] = {
|
|||||||
|
|
||||||
/* Load the peg module */
|
/* Load the peg module */
|
||||||
void janet_lib_peg(JanetTable *env) {
|
void janet_lib_peg(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, peg_cfuns);
|
janet_core_cfuns(env, NULL, peg_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -221,7 +221,6 @@ static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv)
|
|||||||
return rvalue;
|
return rvalue;
|
||||||
} else {
|
} else {
|
||||||
/* Error */
|
/* Error */
|
||||||
janet_inspect(argv[0]);
|
|
||||||
janetc_cerror(opts.compiler, "expected symbol or tuple for l-value to set");
|
janetc_cerror(opts.compiler, "expected symbol or tuple for l-value to set");
|
||||||
return janetc_cslot(janet_wrap_nil());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
}
|
}
|
||||||
|
@ -619,5 +619,5 @@ static const JanetReg string_cfuns[] = {
|
|||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_string(JanetTable *env) {
|
void janet_lib_string(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, string_cfuns);
|
janet_core_cfuns(env, NULL, string_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -273,5 +273,5 @@ static const JanetReg table_cfuns[] = {
|
|||||||
|
|
||||||
/* Load the table module */
|
/* Load the table module */
|
||||||
void janet_lib_table(JanetTable *env) {
|
void janet_lib_table(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, table_cfuns);
|
janet_core_cfuns(env, NULL, table_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -147,5 +147,5 @@ static const JanetReg tuple_cfuns[] = {
|
|||||||
|
|
||||||
/* Load the tuple module */
|
/* Load the tuple module */
|
||||||
void janet_lib_tuple(JanetTable *env) {
|
void janet_lib_tuple(JanetTable *env) {
|
||||||
janet_cfuns(env, NULL, tuple_cfuns);
|
janet_core_cfuns(env, NULL, tuple_cfuns);
|
||||||
}
|
}
|
||||||
|
@ -284,6 +284,24 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef JANET_BOOTSTRAP
|
||||||
|
void janet_core_def(JanetTable *env, const char *name, Janet x, const void *p) {
|
||||||
|
(void) p;
|
||||||
|
janet_table_put(env, janet_csymbolv(name), x);
|
||||||
|
}
|
||||||
|
|
||||||
|
void janet_core_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns) {
|
||||||
|
(void) regprefix;
|
||||||
|
while (cfuns->name) {
|
||||||
|
Janet name = janet_csymbolv(cfuns->name);
|
||||||
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
||||||
|
janet_core_def(env, cfuns->name, fun, cfuns->documentation);
|
||||||
|
janet_table_put(janet_vm_registry, fun, name);
|
||||||
|
cfuns++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Resolve a symbol in the environment */
|
/* Resolve a symbol in the environment */
|
||||||
JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out) {
|
JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out) {
|
||||||
Janet ref;
|
Janet ref;
|
||||||
|
@ -28,8 +28,9 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Omit docstrings in some builds */
|
/* Omit docstrings in some builds */
|
||||||
#ifdef JANET_NO_BOOTSTRAP
|
#ifndef JANET_BOOTSTRAP
|
||||||
#define JDOC(x) NULL
|
#define JDOC(x) NULL
|
||||||
|
#define JANET_NO_BOOTSTRAP
|
||||||
#else
|
#else
|
||||||
#define JDOC(x) x
|
#define JDOC(x) x
|
||||||
#endif
|
#endif
|
||||||
@ -52,6 +53,16 @@ const void *janet_strbinsearch(
|
|||||||
size_t itemsize,
|
size_t itemsize,
|
||||||
const uint8_t *key);
|
const uint8_t *key);
|
||||||
|
|
||||||
|
/* Inside the janet core, defining globals is different
|
||||||
|
* at bootstrap time and normal runtime */
|
||||||
|
#ifdef JANET_BOOTSTRAP
|
||||||
|
#define janet_core_def janet_def
|
||||||
|
#define janet_core_cfuns janet_cfuns
|
||||||
|
#else
|
||||||
|
void janet_core_def(JanetTable *env, const char *name, Janet x, const void *p);
|
||||||
|
void janet_core_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Initialize builtin libraries */
|
/* Initialize builtin libraries */
|
||||||
void janet_lib_io(JanetTable *env);
|
void janet_lib_io(JanetTable *env);
|
||||||
void janet_lib_math(JanetTable *env);
|
void janet_lib_math(JanetTable *env);
|
||||||
|
@ -1117,7 +1117,6 @@ JANET_API Janet janet_getindex(Janet ds, int32_t index);
|
|||||||
JANET_API int32_t janet_length(Janet x);
|
JANET_API int32_t janet_length(Janet x);
|
||||||
JANET_API void janet_put(Janet ds, Janet key, Janet value);
|
JANET_API void janet_put(Janet ds, Janet key, Janet value);
|
||||||
JANET_API void janet_putindex(Janet ds, int32_t index, Janet value);
|
JANET_API void janet_putindex(Janet ds, int32_t index, Janet value);
|
||||||
JANET_API void janet_inspect(Janet x);
|
|
||||||
|
|
||||||
/* VM functions */
|
/* VM functions */
|
||||||
JANET_API int janet_init(void);
|
JANET_API int janet_init(void);
|
||||||
|
@ -71,5 +71,6 @@
|
|||||||
(each h headers (dofile h))
|
(each h headers (dofile h))
|
||||||
(each s sources (dofile s))
|
(each s sources (dofile s))
|
||||||
|
|
||||||
# Relies on this file being built
|
# Relies on these files being built
|
||||||
(dofile "build/core.gen.c")
|
(dofile "build/core.gen.c")
|
||||||
|
(dofile "build/core_image.c")
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
# Tool to dump a marshalled version of the janet core to stdout. The
|
|
||||||
# image should eventually allow janet to be started from a pre-compiled
|
|
||||||
# image rather than recompiled every time from the embedded source. More
|
|
||||||
# work will go into shrinking the image (it isn't currently that large but
|
|
||||||
# could be smaller), creating the mechanism to load the image, and modifying
|
|
||||||
# the build process to compile janet with a built image rather than
|
|
||||||
# embedded source.
|
|
||||||
|
|
||||||
# Get image. This image contains as much of the core library and documentation that
|
|
||||||
# can be written to an image (no cfunctions, no abstracts (stdout, stdin, stderr)),
|
|
||||||
# everything else goes. Cfunctions and abstracts will be referenced from a register
|
|
||||||
# table which will be generated on janet startup.
|
|
||||||
(def image (let [env-pairs (pairs (env-lookup *env*))
|
|
||||||
essential-pairs (filter (fn [[k v]] (or (cfunction? v) (abstract? v))) env-pairs)
|
|
||||||
lookup (table ;(mapcat identity essential-pairs))
|
|
||||||
reverse-lookup (invert lookup)]
|
|
||||||
(marshal (table/getproto *env*) reverse-lookup)))
|
|
||||||
|
|
||||||
# Write image
|
|
||||||
(file/write stdout image)
|
|
||||||
(file/flush stdout)
|
|
Loading…
Reference in New Issue
Block a user