mirror of
https://github.com/janet-lang/janet
synced 2025-11-21 01:34:49 +00:00
Compare commits
131 Commits
v1.27.0
...
bytecode_o
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b099bd97f2 | ||
|
|
9c97d8f648 | ||
|
|
40080b23ae | ||
|
|
7acb5c63e0 | ||
|
|
fcca9bbab3 | ||
|
|
dbb2187425 | ||
|
|
82e51f9e81 | ||
|
|
4782a76bca | ||
|
|
d13788a4ed | ||
|
|
e64a0175b1 | ||
|
|
4aca94154f | ||
|
|
ac5f118dac | ||
|
|
a2812ec5eb | ||
|
|
70f13f1b62 | ||
|
|
77e62a25cb | ||
|
|
09345ec786 | ||
|
|
bad73baf98 | ||
|
|
3602f5aa5d | ||
|
|
672b705faf | ||
|
|
64e3cdeb2b | ||
|
|
909c906080 | ||
|
|
71bde11e95 | ||
|
|
fc20fbed92 | ||
|
|
e6b7c85c37 | ||
|
|
b3a92363f8 | ||
|
|
e9f2d1aca7 | ||
|
|
b4e3dbf331 | ||
|
|
c3620786cf | ||
|
|
41943746e4 | ||
|
|
176e816b8c | ||
|
|
50a19bd870 | ||
|
|
57b751b994 | ||
|
|
77732a8f44 | ||
|
|
c47c2e538d | ||
|
|
cc5545277d | ||
|
|
63353b98cd | ||
|
|
4dfc869b8a | ||
|
|
b4b1c7d80b | ||
|
|
e53c03028f | ||
|
|
8680aef42f | ||
|
|
c3fd71d643 | ||
|
|
30c47d685d | ||
|
|
80db682109 | ||
|
|
e8e5f66f4c | ||
|
|
aaf3d08bcd | ||
|
|
61132d6c40 | ||
|
|
9cc0645a1e | ||
|
|
fc8c6a429e | ||
|
|
2f966883d9 | ||
|
|
320ba80ca1 | ||
|
|
b621d4dd2e | ||
|
|
56d927c72d | ||
|
|
53afc2e50a | ||
|
|
89debac8f6 | ||
|
|
f2197fa2d8 | ||
|
|
a6a097c111 | ||
|
|
c3e28bc924 | ||
|
|
8d78fb1f6b | ||
|
|
148917d4ca | ||
|
|
d8cf9bf942 | ||
|
|
d6f5a060ed | ||
|
|
692b6ef8ac | ||
|
|
ac5f1fe1be | ||
|
|
0f35acade1 | ||
|
|
56d72ec4c5 | ||
|
|
71d51c160d | ||
|
|
0b58e505ee | ||
|
|
2a6c615bec | ||
|
|
ab8c5a0b5f | ||
|
|
68c35feaea | ||
|
|
88d0c2ca0f | ||
|
|
398833ebe3 | ||
|
|
358f5a03bf | ||
|
|
fba1fdabe4 | ||
|
|
d42afd21e5 | ||
|
|
20ada86761 | ||
|
|
3b353f1855 | ||
|
|
1467ab4f93 | ||
|
|
7e65c2bdad | ||
|
|
84a4e3e98a | ||
|
|
bcbeedb001 | ||
|
|
e04b103b5d | ||
|
|
ac75b94679 | ||
|
|
d3bb06cfd6 | ||
|
|
5cd729c4c1 | ||
|
|
c9fd2bdf39 | ||
|
|
e4be5992b3 | ||
|
|
2ac4988f1b | ||
|
|
19f14adb9e | ||
|
|
86de039492 | ||
|
|
2360164e4f | ||
|
|
c93ddceadb | ||
|
|
cd19dec44a | ||
|
|
53ba9c800a | ||
|
|
cabbaded68 | ||
|
|
9bb589f827 | ||
|
|
c3a06686c2 | ||
|
|
7d57f87007 | ||
|
|
4cc4a9d38b | ||
|
|
02c7cd0194 | ||
|
|
696efcb9e2 | ||
|
|
6e9cde8ac1 | ||
|
|
a9fae49671 | ||
|
|
440af9fd64 | ||
|
|
347721ae40 | ||
|
|
daea91044c | ||
|
|
4ed3f2c662 | ||
|
|
3641c8f60a | ||
|
|
e4b68cd940 | ||
|
|
b8c936e2fe | ||
|
|
83cd519702 | ||
|
|
54b54f85f3 | ||
|
|
ccd874fe4e | ||
|
|
9dc7e8ed3a | ||
|
|
485099fd6e | ||
|
|
d359c6b43e | ||
|
|
d9ed7a77f8 | ||
|
|
4238a4ca6a | ||
|
|
0902a5a981 | ||
|
|
f3192303ab | ||
|
|
bef5bd72c2 | ||
|
|
b6175e4296 | ||
|
|
3858b2e177 | ||
|
|
9a76e77981 | ||
|
|
8182d640cd | ||
|
|
1c6fda1a5c | ||
|
|
c51db1cf2f | ||
|
|
4e7930fc4c | ||
|
|
3563f8ccdb | ||
|
|
575af763f6 | ||
|
|
8b16b9b246 |
17
.github/workflows/test.yml
vendored
17
.github/workflows/test.yml
vendored
@@ -57,3 +57,20 @@ jobs:
|
||||
- name: Build the project
|
||||
shell: cmd
|
||||
run: make -j CC=gcc
|
||||
|
||||
test-mingw-linux:
|
||||
name: Build and test with Mingw on Linux + Wine
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
uses: actions/checkout@master
|
||||
- name: Setup Mingw and wine
|
||||
run: |
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt-get update
|
||||
sudo apt-get install libstdc++6:i386 libgcc-s1:i386
|
||||
sudo apt-get install gcc-mingw-w64-x86-64-win32 wine wine32 wine64
|
||||
- name: Compile the project
|
||||
run: make clean && make CC=x86_64-w64-mingw32-gcc LD=x86_64-w64-mingw32-gcc UNAME=MINGW RUN=wine
|
||||
- name: Test the project
|
||||
run: make test UNAME=MINGW RUN=wine
|
||||
|
||||
18
CHANGELOG.md
18
CHANGELOG.md
@@ -1,6 +1,24 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## ??? - Unreleased
|
||||
- Make imperative arithmetic macros variadic
|
||||
- `ev/connect` now yields to the event loop instead of blocking while waiting for an ACK.
|
||||
|
||||
## 1.28.0 - 2023-05-13
|
||||
- Various bug fixes
|
||||
- Make nested short-fn's behave a bit more predictably (it is still not recommended to nest short-fns).
|
||||
- Add `os/strftime` for date formatting.
|
||||
- Fix `ev/select` on threaded channels sometimes live-locking.
|
||||
- Support the `NO_COLOR` environment variable to turn off VT100 color codes in repl (and in scripts).
|
||||
See http://no-color.org/
|
||||
- Disallow using `(splice x)` in contexts where it doesn't make sense rather than silently coercing to `x`.
|
||||
Instead, raise a compiler error.
|
||||
- Change the names of `:user8` and `:user9` sigals to `:interrupt` and `:await`
|
||||
- Change the names of `:user8` and `:user9` fiber statuses to `:interrupted` and `:suspended`.
|
||||
- Add `ev/all-tasks` to see all currently suspended fibers.
|
||||
- Add `keep-syntax` and `keep-syntax!` functions to make writing macros easier.
|
||||
|
||||
## 1.27.0 - 2023-03-05
|
||||
- Change semantics around bracket tuples to no longer be equal to regular tuples.
|
||||
- Add `index` argument to `ffi/write` for symmetry with `ffi/read`.
|
||||
|
||||
41
Makefile
41
Makefile
@@ -31,6 +31,7 @@ LIBDIR?=$(PREFIX)/lib
|
||||
JANET_BUILD?="\"$(shell git log --pretty=format:'%h' -n 1 2> /dev/null || echo local)\""
|
||||
CLIBS=-lm -lpthread
|
||||
JANET_TARGET=build/janet
|
||||
JANET_BOOT=build/janet_boot
|
||||
JANET_IMPORT_LIB=build/janet.lib
|
||||
JANET_LIBRARY=build/libjanet.so
|
||||
JANET_STATIC_LIBRARY=build/libjanet.a
|
||||
@@ -45,18 +46,19 @@ SONAME_SETTER=-Wl,-soname,
|
||||
# For cross compilation
|
||||
HOSTCC?=$(CC)
|
||||
HOSTAR?=$(AR)
|
||||
CFLAGS?=-O2
|
||||
CFLAGS?=-O2 -g
|
||||
LDFLAGS?=-rdynamic
|
||||
RUN:=$(RUN)
|
||||
|
||||
COMMON_CFLAGS:=-std=c99 -Wall -Wextra -Isrc/include -Isrc/conf -fvisibility=hidden -fPIC
|
||||
BOOT_CFLAGS:=-DJANET_BOOTSTRAP -DJANET_BUILD=$(JANET_BUILD) -O0 -g $(COMMON_CFLAGS)
|
||||
BOOT_CFLAGS:=-DJANET_BOOTSTRAP -DJANET_BUILD=$(JANET_BUILD) -O0 $(COMMON_CFLAGS) -g
|
||||
BUILD_CFLAGS:=$(CFLAGS) $(COMMON_CFLAGS)
|
||||
|
||||
# For installation
|
||||
LDCONFIG:=ldconfig "$(LIBDIR)"
|
||||
|
||||
# Check OS
|
||||
UNAME:=$(shell uname -s)
|
||||
UNAME?=$(shell uname -s)
|
||||
ifeq ($(UNAME), Darwin)
|
||||
CLIBS:=$(CLIBS) -ldl
|
||||
SONAME_SETTER:=-Wl,-install_name,
|
||||
@@ -82,6 +84,8 @@ endif
|
||||
ifeq ($(findstring MINGW,$(UNAME)), MINGW)
|
||||
CLIBS:=-lws2_32 -lpsapi -lwsock32
|
||||
LDFLAGS:=-Wl,--out-implib,$(JANET_IMPORT_LIB)
|
||||
JANET_TARGET:=$(JANET_TARGET).exe
|
||||
JANET_BOOT:=$(JANET_BOOT).exe
|
||||
endif
|
||||
|
||||
$(shell mkdir -p build/core build/c build/boot)
|
||||
@@ -163,12 +167,12 @@ $(JANET_BOOT_OBJECTS): $(JANET_BOOT_HEADERS)
|
||||
build/%.boot.o: src/%.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS) Makefile
|
||||
$(CC) $(BOOT_CFLAGS) -o $@ -c $<
|
||||
|
||||
build/janet_boot: $(JANET_BOOT_OBJECTS)
|
||||
$(JANET_BOOT): $(JANET_BOOT_OBJECTS)
|
||||
$(CC) $(BOOT_CFLAGS) -o $@ $(JANET_BOOT_OBJECTS) $(CLIBS)
|
||||
|
||||
# Now the reason we bootstrap in the first place
|
||||
build/c/janet.c: build/janet_boot src/boot/boot.janet
|
||||
build/janet_boot . JANET_PATH '$(JANET_PATH)' > $@
|
||||
build/c/janet.c: $(JANET_BOOT) src/boot/boot.janet
|
||||
$(RUN) $(JANET_BOOT) . JANET_PATH '$(JANET_PATH)' > $@
|
||||
cksum $@
|
||||
|
||||
########################
|
||||
@@ -176,16 +180,16 @@ build/c/janet.c: build/janet_boot src/boot/boot.janet
|
||||
########################
|
||||
|
||||
ifeq ($(UNAME), Darwin)
|
||||
SONAME=libjanet.1.27.dylib
|
||||
SONAME=libjanet.1.28.dylib
|
||||
else
|
||||
SONAME=libjanet.so.1.27
|
||||
SONAME=libjanet.so.1.28
|
||||
endif
|
||||
|
||||
build/c/shell.c: src/mainclient/shell.c
|
||||
cp $< $@
|
||||
|
||||
build/janet.h: $(JANET_TARGET) src/include/janet.h $(JANETCONF_HEADER)
|
||||
./$(JANET_TARGET) tools/patch-header.janet src/include/janet.h $(JANETCONF_HEADER) $@
|
||||
$(RUN) ./$(JANET_TARGET) tools/patch-header.janet src/include/janet.h $(JANETCONF_HEADER) $@
|
||||
|
||||
build/janetconf.h: $(JANETCONF_HEADER)
|
||||
cp $< $@
|
||||
@@ -214,19 +218,19 @@ $(JANET_STATIC_LIBRARY): build/janet.o build/shell.o
|
||||
TEST_SCRIPTS=$(wildcard test/suite*.janet)
|
||||
|
||||
repl: $(JANET_TARGET)
|
||||
./$(JANET_TARGET)
|
||||
$(RUN) ./$(JANET_TARGET)
|
||||
|
||||
debug: $(JANET_TARGET)
|
||||
$(DEBUGGER) ./$(JANET_TARGET)
|
||||
|
||||
VALGRIND_COMMAND=valgrind --leak-check=full
|
||||
VALGRIND_COMMAND=valgrind --leak-check=full --quiet
|
||||
|
||||
valgrind: $(JANET_TARGET)
|
||||
$(VALGRIND_COMMAND) ./$(JANET_TARGET)
|
||||
|
||||
test: $(JANET_TARGET) $(TEST_PROGRAMS)
|
||||
for f in test/suite*.janet; do ./$(JANET_TARGET) "$$f" || exit; done
|
||||
for f in examples/*.janet; do ./$(JANET_TARGET) -k "$$f"; done
|
||||
for f in test/suite*.janet; do $(RUN) ./$(JANET_TARGET) "$$f" || exit; done
|
||||
for f in examples/*.janet; do $(RUN) ./$(JANET_TARGET) -k "$$f"; done
|
||||
|
||||
valtest: $(JANET_TARGET) $(TEST_PROGRAMS)
|
||||
for f in test/suite*.janet; do $(VALGRIND_COMMAND) ./$(JANET_TARGET) "$$f" || exit; done
|
||||
@@ -265,7 +269,7 @@ build/janet-%.tar.gz: $(JANET_TARGET) \
|
||||
docs: build/doc.html
|
||||
|
||||
build/doc.html: $(JANET_TARGET) tools/gendoc.janet
|
||||
$(JANET_TARGET) tools/gendoc.janet > build/doc.html
|
||||
$(RUN) $(JANET_TARGET) tools/gendoc.janet > build/doc.html
|
||||
|
||||
########################
|
||||
##### Installation #####
|
||||
@@ -281,7 +285,7 @@ build/janet.pc: $(JANET_TARGET)
|
||||
echo "Name: janet" >> $@
|
||||
echo "Url: https://janet-lang.org" >> $@
|
||||
echo "Description: Library for the Janet programming language." >> $@
|
||||
$(JANET_TARGET) -e '(print "Version: " janet/version)' >> $@
|
||||
$(RUN) $(JANET_TARGET) -e '(print "Version: " janet/version)' >> $@
|
||||
echo 'Cflags: -I$${includedir}' >> $@
|
||||
echo 'Libs: -L$${libdir} -ljanet' >> $@
|
||||
echo 'Libs.private: $(CLIBS)' >> $@
|
||||
@@ -289,9 +293,10 @@ build/janet.pc: $(JANET_TARGET)
|
||||
install: $(JANET_TARGET) $(JANET_LIBRARY) $(JANET_STATIC_LIBRARY) build/janet.pc build/janet.h
|
||||
mkdir -p '$(DESTDIR)$(BINDIR)'
|
||||
cp $(JANET_TARGET) '$(DESTDIR)$(BINDIR)/janet'
|
||||
strip '$(DESTDIR)$(BINDIR)/janet'
|
||||
mkdir -p '$(DESTDIR)$(INCLUDEDIR)/janet'
|
||||
cp -r build/janet.h '$(DESTDIR)$(INCLUDEDIR)/janet'
|
||||
ln -sf -T ./janet/janet.h '$(DESTDIR)$(INCLUDEDIR)/janet.h' || true #fixme bsd
|
||||
ln -sf ./janet/janet.h '$(DESTDIR)$(INCLUDEDIR)/janet.h'
|
||||
mkdir -p '$(DESTDIR)$(JANET_PATH)'
|
||||
mkdir -p '$(DESTDIR)$(LIBDIR)'
|
||||
if test $(UNAME) = Darwin ; then \
|
||||
@@ -321,7 +326,7 @@ install-jpm-git: $(JANET_TARGET)
|
||||
JANET_HEADERPATH='$(INCLUDEDIR)/janet' \
|
||||
JANET_BINPATH='$(BINDIR)' \
|
||||
JANET_LIBPATH='$(LIBDIR)' \
|
||||
../../$(JANET_TARGET) ./bootstrap.janet
|
||||
$(RUN) ../../$(JANET_TARGET) ./bootstrap.janet
|
||||
|
||||
uninstall:
|
||||
-rm '$(DESTDIR)$(BINDIR)/janet'
|
||||
@@ -341,7 +346,7 @@ format:
|
||||
|
||||
grammar: build/janet.tmLanguage
|
||||
build/janet.tmLanguage: tools/tm_lang_gen.janet $(JANET_TARGET)
|
||||
$(JANET_TARGET) $< > $@
|
||||
$(RUN) $(JANET_TARGET) $< > $@
|
||||
|
||||
compile-commands:
|
||||
# Requires pip install copmiledb
|
||||
|
||||
83
README.md
83
README.md
@@ -7,14 +7,14 @@
|
||||
<img src="https://raw.githubusercontent.com/janet-lang/janet/master/assets/janet-w200.png" alt="Janet logo" width=200 align="left">
|
||||
|
||||
**Janet** is a functional and imperative programming language and bytecode interpreter. It is a
|
||||
lisp-like language, but lists are replaced
|
||||
Lisp-like language, but lists are replaced
|
||||
by other data structures (arrays, tables (hash table), struct (immutable hash table), tuples).
|
||||
The language also supports bridging to native code written in C, meta-programming with macros, and bytecode assembly.
|
||||
|
||||
There is a REPL for trying out the language, as well as the ability
|
||||
to run script files. This client program is separate from the core runtime, so
|
||||
Janet can be embedded in other programs. Try Janet in your browser at
|
||||
[https://janet-lang.org](https://janet-lang.org).
|
||||
<https://janet-lang.org>.
|
||||
|
||||
If you'd like to financially support the ongoing development of Janet, consider
|
||||
[sponsoring its primary author](https://github.com/sponsors/bakpakin) through GitHub.
|
||||
@@ -41,8 +41,8 @@ Lua, but smaller than GNU Guile or Python.
|
||||
* Macros
|
||||
* Multithreading
|
||||
* Per-thread event loop for efficient evented IO
|
||||
* Byte code interpreter with an assembly interface, as well as bytecode verification
|
||||
* Tail call Optimization
|
||||
* Bytecode interpreter with an assembly interface, as well as bytecode verification
|
||||
* Tail-call optimization
|
||||
* Direct interop with C via abstract types and C functions
|
||||
* Dynamically load C libraries
|
||||
* Functional and imperative standard library
|
||||
@@ -57,7 +57,7 @@ Lua, but smaller than GNU Guile or Python.
|
||||
## Documentation
|
||||
|
||||
* For a quick tutorial, see [the introduction](https://janet-lang.org/docs/index.html) for more details.
|
||||
* For the full API for all functions in the core library, see [the core API doc](https://janet-lang.org/api/index.html)
|
||||
* For the full API for all functions in the core library, see [the core API doc](https://janet-lang.org/api/index.html).
|
||||
|
||||
Documentation is also available locally in the REPL.
|
||||
Use the `(doc symbol-name)` macro to get API
|
||||
@@ -65,7 +65,7 @@ documentation for symbols in the core library. For example,
|
||||
```
|
||||
(doc apply)
|
||||
```
|
||||
Shows documentation for the `apply` function.
|
||||
shows documentation for the `apply` function.
|
||||
|
||||
To get a list of all bindings in the default
|
||||
environment, use the `(all-bindings)` function. You
|
||||
@@ -84,7 +84,7 @@ the SourceHut mirror is actively maintained.
|
||||
|
||||
The Makefile is non-portable and requires GNU-flavored make.
|
||||
|
||||
```
|
||||
```sh
|
||||
cd somewhere/my/projects/janet
|
||||
make
|
||||
make test
|
||||
@@ -100,7 +100,7 @@ Find out more about the available make targets by running `make help`.
|
||||
32-bit Haiku build instructions are the same as the UNIX-like build instructions,
|
||||
but you need to specify an alternative compiler, such as `gcc-x86`.
|
||||
|
||||
```
|
||||
```sh
|
||||
cd somewhere/my/projects/janet
|
||||
make CC=gcc-x86
|
||||
make test
|
||||
@@ -112,10 +112,9 @@ make install-jpm-git
|
||||
### FreeBSD
|
||||
|
||||
FreeBSD build instructions are the same as the UNIX-like build instructions,
|
||||
but you need `gmake` to compile. Alternatively, install directly from
|
||||
packages, using `pkg install lang/janet`.
|
||||
but you need `gmake` to compile. Alternatively, install the package directly with `pkg install lang/janet`.
|
||||
|
||||
```
|
||||
```sh
|
||||
cd somewhere/my/projects/janet
|
||||
gmake
|
||||
gmake test
|
||||
@@ -127,19 +126,19 @@ gmake install-jpm-git
|
||||
### NetBSD
|
||||
|
||||
NetBSD build instructions are the same as the FreeBSD build instructions.
|
||||
Alternatively, install directly from packages, using `pkgin install janet`.
|
||||
Alternatively, install the package directly with `pkgin install janet`.
|
||||
|
||||
### Windows
|
||||
|
||||
1. Install [Visual Studio](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=15#) or [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15#)
|
||||
2. Run a Visual Studio Command Prompt (cl.exe and link.exe need to be on the PATH) and cd to the directory with janet.
|
||||
3. Run `build_win` to compile janet.
|
||||
1. Install [Visual Studio](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=15#) or [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15#).
|
||||
2. Run a Visual Studio Command Prompt (`cl.exe` and `link.exe` need to be on your PATH) and `cd` to the directory with Janet.
|
||||
3. Run `build_win` to compile Janet.
|
||||
4. Run `build_win test` to make sure everything is working.
|
||||
|
||||
To build an `.msi` installer executable, in addition to the above steps, you will have to:
|
||||
|
||||
5. Install, or otherwise add to your PATH the [WiX 3.11 Toolset](https://github.com/wixtoolset/wix3/releases)
|
||||
6. run `build_win dist`
|
||||
5. Install, or otherwise add to your PATH the [WiX 3.11 Toolset](https://github.com/wixtoolset/wix3/releases).
|
||||
6. Run `build_win dist`.
|
||||
|
||||
Now you should have an `.msi`. You can run `build_win install` to install the `.msi`, or execute the file itself.
|
||||
|
||||
@@ -175,9 +174,9 @@ ninja -C build install
|
||||
|
||||
Janet can be hacked on with pretty much any environment you like, but for IDE
|
||||
lovers, [Gnome Builder](https://wiki.gnome.org/Apps/Builder) is probably the
|
||||
best option, as it has excellent meson integration. It also offers code completion
|
||||
best option, as it has excellent Meson integration. It also offers code completion
|
||||
for Janet's C API right out of the box, which is very useful for exploring. VSCode, Vim,
|
||||
Emacs, and Atom will have syntax packages for the Janet language, though.
|
||||
Emacs, and Atom each have syntax packages for the Janet language, though.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -186,8 +185,8 @@ to try out the language, you don't need to install anything. You can also move t
|
||||
|
||||
## Usage
|
||||
|
||||
A REPL is launched when the binary is invoked with no arguments. Pass the -h flag
|
||||
to display the usage information. Individual scripts can be run with `./janet myscript.janet`
|
||||
A REPL is launched when the binary is invoked with no arguments. Pass the `-h` flag
|
||||
to display the usage information. Individual scripts can be run with `./janet myscript.janet`.
|
||||
|
||||
If you are looking to explore, you can print a list of all available macros, functions, and constants
|
||||
by entering the command `(all-bindings)` into the REPL.
|
||||
@@ -202,20 +201,26 @@ Hello, World!
|
||||
nil
|
||||
janet:3:> (os/exit)
|
||||
$ janet -h
|
||||
usage: build/janet [options] script args...
|
||||
usage: janet [options] script args...
|
||||
Options are:
|
||||
-h : Show this help
|
||||
-v : Print the version string
|
||||
-s : Use raw stdin instead of getline like functionality
|
||||
-e code : Execute a string of janet
|
||||
-E code arguments... : Evaluate an expression as a short-fn with arguments
|
||||
-d : Set the debug flag in the REPL
|
||||
-r : Enter the REPL after running all scripts
|
||||
-R : Disables loading profile.janet when JANET_PROFILE is present
|
||||
-p : Keep on executing if there is a top-level error (persistent)
|
||||
-q : Hide prompt, logo, and REPL output (quiet)
|
||||
-q : Hide logo (quiet)
|
||||
-k : Compile scripts but do not execute (flycheck)
|
||||
-m syspath : Set system path for loading global modules
|
||||
-c source output : Compile janet source code into an image
|
||||
-i : Load the script argument as an image file instead of source code
|
||||
-n : Disable ANSI color output in the REPL
|
||||
-l path : Execute code in a file before running the main script
|
||||
-l lib : Use a module before processing more arguments
|
||||
-w level : Set the lint warning level - default is "normal"
|
||||
-x level : Set the lint error level - default is "none"
|
||||
-- : Stop handling options
|
||||
```
|
||||
|
||||
@@ -226,8 +231,8 @@ If installed, you can also run `man janet` to get usage information.
|
||||
Janet can be embedded in a host program very easily. The normal build
|
||||
will create a file `build/janet.c`, which is a single C file
|
||||
that contains all the source to Janet. This file, along with
|
||||
`src/include/janet.h` and `src/conf/janetconf.h` can be dragged into any C
|
||||
project and compiled into the project. Janet should be compiled with `-std=c99`
|
||||
`src/include/janet.h` and `src/conf/janetconf.h`, can be dragged into any C
|
||||
project and compiled into it. Janet should be compiled with `-std=c99`
|
||||
on most compilers, and will need to be linked to the math library, `-lm`, and
|
||||
the dynamic linker, `-ldl`, if one wants to be able to load dynamic modules. If
|
||||
there is no need for dynamic modules, add the define
|
||||
@@ -237,24 +242,24 @@ See the [Embedding Section](https://janet-lang.org/capi/embedding.html) on the w
|
||||
|
||||
## Examples
|
||||
|
||||
See the examples directory for some example janet code.
|
||||
See the examples directory for some example Janet code.
|
||||
|
||||
## Discussion
|
||||
|
||||
Feel free to ask questions and join the discussion on the [Janet Gitter Channel](https://gitter.im/janet-language/community).
|
||||
Gitter provides Matrix and irc bridges as well.
|
||||
Feel free to ask questions and join the discussion on the [Janet Gitter channel](https://gitter.im/janet-language/community).
|
||||
Gitter provides Matrix and IRC bridges as well.
|
||||
|
||||
## FAQ
|
||||
|
||||
### Where is (favorite feature from other language)?
|
||||
|
||||
It may exist, it may not. If you want to propose major language features, go ahead and open an issue, but
|
||||
they will likely by closed as "will not implement". Often, such features make one usecase simpler at the expense
|
||||
It may exist, it may not. If you want to propose a major language feature, go ahead and open an issue, but
|
||||
it will likely be closed as "will not implement". Often, such features make one usecase simpler at the expense
|
||||
of 5 others by making the language more complicated.
|
||||
|
||||
### Is there a language spec?
|
||||
|
||||
There is not currently a spec besides the documentation at https://janet-lang.org.
|
||||
There is not currently a spec besides the documentation at <https://janet-lang.org>.
|
||||
|
||||
### Is this Scheme/Common Lisp? Where are the cons cells?
|
||||
|
||||
@@ -270,13 +275,13 @@ Internally, Janet is not at all like Clojure.
|
||||
No. They are immutable arrays and hash tables. Don't try and use them like Clojure's vectors
|
||||
and maps, instead they work well as table keys or other identifiers.
|
||||
|
||||
### Can I do Object Oriented programming with Janet?
|
||||
### Can I do object-oriented programming with Janet?
|
||||
|
||||
To some extent, yes. However, it is not the recommended method of abstraction, and performance may suffer.
|
||||
That said, tables can be used to make mutable objects with inheritance and polymorphism, where object
|
||||
methods are implemeted with keywords.
|
||||
methods are implemented with keywords.
|
||||
|
||||
```
|
||||
```clj
|
||||
(def Car @{:honk (fn [self msg] (print "car " self " goes " msg)) })
|
||||
(def my-car (table/setproto @{} Car))
|
||||
(:honk my-car "Beep!")
|
||||
@@ -287,9 +292,9 @@ methods are implemeted with keywords.
|
||||
Usually, one of a few reasons:
|
||||
- Often, it already exists in a different form and the Clojure port would be redundant.
|
||||
- Clojure programs often generate a lot of garbage and rely on the JVM to clean it up.
|
||||
Janet does not run on the JVM, and has a more primitive garbage collector.
|
||||
- We want to keep the Janet core small. With Lisps, usually a feature can be added as a library
|
||||
without feeling "bolted on", especially when compared to ALGOL like languages. Adding features
|
||||
Janet does not run on the JVM and has a more primitive garbage collector.
|
||||
- We want to keep the Janet core small. With Lisps, a feature can usually be added as a library
|
||||
without feeling "bolted on", especially when compared to ALGOL-like languages. Adding features
|
||||
to the core also makes it a bit more difficult to keep Janet maximally portable.
|
||||
|
||||
### Why is my terminal spitting out junk when I run the REPL?
|
||||
@@ -297,7 +302,7 @@ Usually, one of a few reasons:
|
||||
Make sure your terminal supports ANSI escape codes. Most modern terminals will
|
||||
support these, but some older terminals, Windows consoles, or embedded terminals
|
||||
will not. If your terminal does not support ANSI escape codes, run the REPL with
|
||||
the `-n` flag, which disables color output. You can also try the `-s` if further issues
|
||||
the `-n` flag, which disables color output. You can also try the `-s` flag if further issues
|
||||
ensue.
|
||||
|
||||
## Why is it called "Janet"?
|
||||
|
||||
10
janet.1
10
janet.1
@@ -183,6 +183,10 @@ default repl.
|
||||
.BR \-n
|
||||
Disable ANSI colors in the repl. Has no effect if no repl is run.
|
||||
|
||||
.TP
|
||||
.BR \-N
|
||||
Enable ANSI colors in the repl. Has no effect if no repl is run.
|
||||
|
||||
.TP
|
||||
.BR \-r
|
||||
Open a REPL (Read Eval Print Loop) after executing all sources. By default, if Janet is called with no
|
||||
@@ -268,5 +272,11 @@ This variable does nothing in the default configuration of Janet, as PRF is disa
|
||||
cannot be defined for this variable to have an effect.
|
||||
.RE
|
||||
|
||||
.B NO_COLOR
|
||||
.RS
|
||||
Turn off color by default in the repl and in the error handler of scripts. This can be changed at runtime
|
||||
via dynamic bindings *err-color* and *pretty-format*, or via the command line parameters -n and -N.
|
||||
.RE
|
||||
|
||||
.SH AUTHOR
|
||||
Written by Calvin Rose <calsrose@gmail.com>
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
project('janet', 'c',
|
||||
default_options : ['c_std=c99', 'build.c_std=c99', 'b_lundef=false', 'default_library=both'],
|
||||
version : '1.27.0')
|
||||
version : '1.28.0')
|
||||
|
||||
# Global settings
|
||||
janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet')
|
||||
|
||||
@@ -147,10 +147,10 @@
|
||||
(defn dec "Returns x - 1." [x] (- x 1))
|
||||
(defmacro ++ "Increments the var x by 1." [x] ~(set ,x (,+ ,x ,1)))
|
||||
(defmacro -- "Decrements the var x by 1." [x] ~(set ,x (,- ,x ,1)))
|
||||
(defmacro += "Increments the var x by n." [x n] ~(set ,x (,+ ,x ,n)))
|
||||
(defmacro -= "Decrements the var x by n." [x n] ~(set ,x (,- ,x ,n)))
|
||||
(defmacro *= "Shorthand for (set x (\\* x n))." [x n] ~(set ,x (,* ,x ,n)))
|
||||
(defmacro /= "Shorthand for (set x (/ x n))." [x n] ~(set ,x (,/ ,x ,n)))
|
||||
(defmacro += "Increments the var x by n." [x & ns] ~(set ,x (,+ ,x ,;ns)))
|
||||
(defmacro -= "Decrements the var x by n." [x & ns] ~(set ,x (,- ,x ,;ns)))
|
||||
(defmacro *= "Shorthand for (set x (\\* x n))." [x & ns] ~(set ,x (,* ,x ,;ns)))
|
||||
(defmacro /= "Shorthand for (set x (/ x n))." [x & ns] ~(set ,x (,/ ,x ,;ns)))
|
||||
(defmacro %= "Shorthand for (set x (% x n))." [x n] ~(set ,x (,% ,x ,n)))
|
||||
|
||||
(defmacro assert
|
||||
@@ -280,7 +280,7 @@
|
||||
(while (> i 0)
|
||||
(-- i)
|
||||
(def v (in forms i))
|
||||
(set ret (if (= ret true)
|
||||
(set ret (if (= i (- len 1))
|
||||
v
|
||||
(if (idempotent? v)
|
||||
['if v ret v]
|
||||
@@ -611,6 +611,13 @@
|
||||
(def $accum (gensym))
|
||||
~(do (def ,$accum @[]) (loop ,head (,array/push ,$accum (do ,;body))) ,$accum))
|
||||
|
||||
(defmacro catseq
|
||||
``Similar to `loop`, but concatenates each element from the loop body into an array and returns that.
|
||||
See `loop` for details.``
|
||||
[head & body]
|
||||
(def $accum (gensym))
|
||||
~(do (def ,$accum @[]) (loop ,head (,array/concat ,$accum (do ,;body))) ,$accum))
|
||||
|
||||
(defmacro tabseq
|
||||
``Similar to `loop`, but accumulates key value pairs into a table.
|
||||
See `loop` for details.``
|
||||
@@ -916,67 +923,68 @@
|
||||
(set k (next ind k)))
|
||||
ret)
|
||||
|
||||
(defmacro- map-aggregator
|
||||
`Aggregation logic for various map functions.`
|
||||
[maptype res val]
|
||||
(case maptype
|
||||
:map ~(array/push ,res ,val)
|
||||
:mapcat ~(array/concat ,res ,val)
|
||||
:keep ~(if (def y ,val) (array/push ,res y))
|
||||
:count ~(if ,val (++ ,res))
|
||||
:some ~(if (def y ,val) (do (set ,res y) (break)))
|
||||
:all ~(if (def y ,val) nil (do (set ,res y) (break)))))
|
||||
|
||||
(defmacro- map-n
|
||||
`Generates efficient map logic for a specific number of
|
||||
indexed beyond the first.`
|
||||
[n maptype res f ind inds]
|
||||
~(do
|
||||
(def ,(seq [k :range [0 n]] (symbol 'ind k)) ,inds)
|
||||
,;(seq [k :range [0 n]] ~(var ,(symbol 'key k) nil))
|
||||
(each x ,ind
|
||||
,;(seq [k :range [0 n]]
|
||||
~(if (= nil (set ,(symbol 'key k) (next ,(symbol 'ind k) ,(symbol 'key k)))) (break)))
|
||||
(map-aggregator ,maptype ,res (,f x ,;(seq [k :range [0 n]] ~(in ,(symbol 'ind k) ,(symbol 'key k))))))))
|
||||
|
||||
(defmacro- map-template
|
||||
[maptype res f ind inds]
|
||||
~(do
|
||||
(def ninds (length ,inds))
|
||||
(case ninds
|
||||
0 (each x ,ind (map-aggregator ,maptype ,res (,f x)))
|
||||
1 (map-n 1 ,maptype ,res ,f ,ind ,inds)
|
||||
2 (map-n 2 ,maptype ,res ,f ,ind ,inds)
|
||||
3 (map-n 3 ,maptype ,res ,f ,ind ,inds)
|
||||
4 (map-n 4 ,maptype ,res ,f ,ind ,inds)
|
||||
(do
|
||||
(def iter-keys (array/new-filled ninds))
|
||||
(def call-buffer (array/new-filled ninds))
|
||||
(var done false)
|
||||
(each x ,ind
|
||||
(forv i 0 ninds
|
||||
(let [old-key (in iter-keys i)
|
||||
ii (in ,inds i)
|
||||
new-key (next ii old-key)]
|
||||
(if (= nil new-key)
|
||||
(do (set done true) (break))
|
||||
(do (set (iter-keys i) new-key) (set (call-buffer i) (in ii new-key))))))
|
||||
(if done (break))
|
||||
(map-aggregator ,maptype ,res (,f x ;call-buffer)))))))
|
||||
|
||||
(defn map
|
||||
`Map a function over every value in a data structure and
|
||||
return an array of the results.`
|
||||
[f & inds]
|
||||
(def ninds (length inds))
|
||||
(if (= 0 ninds) (error "expected at least 1 indexed collection"))
|
||||
[f ind & inds]
|
||||
(def res @[])
|
||||
(def [i1 i2 i3 i4] inds)
|
||||
(case ninds
|
||||
1 (each x i1 (array/push res (f x)))
|
||||
2 (do
|
||||
(var k1 nil)
|
||||
(var k2 nil)
|
||||
(while true
|
||||
(if (= nil (set k1 (next i1 k1))) (break))
|
||||
(if (= nil (set k2 (next i2 k2))) (break))
|
||||
(array/push res (f (in i1 k1) (in i2 k2)))))
|
||||
3 (do
|
||||
(var k1 nil)
|
||||
(var k2 nil)
|
||||
(var k3 nil)
|
||||
(while true
|
||||
(if (= nil (set k1 (next i1 k1))) (break))
|
||||
(if (= nil (set k2 (next i2 k2))) (break))
|
||||
(if (= nil (set k3 (next i3 k3))) (break))
|
||||
(array/push res (f (in i1 k1) (in i2 k2) (in i3 k3)))))
|
||||
4 (do
|
||||
(var k1 nil)
|
||||
(var k2 nil)
|
||||
(var k3 nil)
|
||||
(var k4 nil)
|
||||
(while true
|
||||
(if (= nil (set k1 (next i1 k1))) (break))
|
||||
(if (= nil (set k2 (next i2 k2))) (break))
|
||||
(if (= nil (set k3 (next i3 k3))) (break))
|
||||
(if (= nil (set k4 (next i4 k4))) (break))
|
||||
(array/push res (f (in i1 k1) (in i2 k2) (in i3 k3) (in i4 k4)))))
|
||||
(do
|
||||
(def iterkeys (array/new-filled ninds))
|
||||
(var done false)
|
||||
(def call-buffer @[])
|
||||
(while true
|
||||
(forv i 0 ninds
|
||||
(let [old-key (in iterkeys i)
|
||||
ii (in inds i)
|
||||
new-key (next ii old-key)]
|
||||
(if (= nil new-key)
|
||||
(do (set done true) (break))
|
||||
(do (set (iterkeys i) new-key) (array/push call-buffer (in ii new-key))))))
|
||||
(if done (break))
|
||||
(array/push res (f ;call-buffer))
|
||||
(array/clear call-buffer))))
|
||||
(map-template :map res f ind inds)
|
||||
res)
|
||||
|
||||
(defn mapcat
|
||||
``Map a function over every element in an array or tuple and
|
||||
use `array/concat` to concatenate the results.``
|
||||
[f ind]
|
||||
[f ind & inds]
|
||||
(def res @[])
|
||||
(each x ind
|
||||
(array/concat res (f x)))
|
||||
(map-template :mapcat res f ind inds)
|
||||
res)
|
||||
|
||||
(defn filter
|
||||
@@ -992,23 +1000,19 @@
|
||||
(defn count
|
||||
``Count the number of items in `ind` for which `(pred item)`
|
||||
is true.``
|
||||
[pred ind]
|
||||
(var counter 0)
|
||||
(each item ind
|
||||
(if (pred item)
|
||||
(++ counter)))
|
||||
counter)
|
||||
[pred ind & inds]
|
||||
(var res 0)
|
||||
(map-template :count res pred ind inds)
|
||||
res)
|
||||
|
||||
(defn keep
|
||||
``Given a predicate `pred`, return a new array containing the truthy results
|
||||
of applying `pred` to each element in the indexed collection `ind`. This is
|
||||
different from `filter` which returns an array of the original elements where
|
||||
the predicate is truthy.``
|
||||
[pred ind]
|
||||
[pred ind & inds]
|
||||
(def res @[])
|
||||
(each item ind
|
||||
(if-let [y (pred item)]
|
||||
(array/push res y)))
|
||||
(map-template :keep res pred ind inds)
|
||||
res)
|
||||
|
||||
(defn range
|
||||
@@ -1133,16 +1137,16 @@
|
||||
(take-until (complement pred) ind))
|
||||
|
||||
(defn drop
|
||||
``Drop the first n elements in an indexed or bytes type. Returns a new tuple or string
|
||||
instance, respectively.``
|
||||
``Drop the first `n elements in an indexed or bytes type. Returns a new tuple or string
|
||||
instance, respectively. If `n` is negative, drops the last `n` elements instead.``
|
||||
[n ind]
|
||||
(def use-str (bytes? ind))
|
||||
(def f (if use-str string/slice tuple/slice))
|
||||
(def len (length ind))
|
||||
# make sure start is in [0, len]
|
||||
(def m (if (> n 0) n 0))
|
||||
(def start (if (> m len) len m))
|
||||
(f ind start -1))
|
||||
(def negn (>= n 0))
|
||||
(def start (if negn (min n len) 0))
|
||||
(def end (if negn len (max 0 (+ len n))))
|
||||
(f ind start end))
|
||||
|
||||
(defn drop-until
|
||||
"Same as `(drop-while (complement pred) ind)`."
|
||||
@@ -1232,6 +1236,29 @@
|
||||
(,eprintf (,dyn :pretty-format "%q") ,s)
|
||||
,s))
|
||||
|
||||
(defn keep-syntax
|
||||
``Creates a tuple with the tuple type and sourcemap of `before` but the
|
||||
elements of `after`. If either one of its argements is not a tuple, returns
|
||||
`after` unmodified. Useful to preserve syntactic information when transforming
|
||||
an ast in macros.``
|
||||
[before after]
|
||||
(if (and (= :tuple (type before))
|
||||
(= :tuple (type after)))
|
||||
(do
|
||||
(def res (if (= :parens (tuple/type before))
|
||||
(tuple/slice after)
|
||||
(tuple/brackets ;after)))
|
||||
(tuple/setmap res ;(tuple/sourcemap before)))
|
||||
after))
|
||||
|
||||
(defn keep-syntax!
|
||||
``Like `keep-syntax`, but if `after` is an array, it is coerced into a tuple.
|
||||
Useful to preserve syntactic information when transforming an ast in macros.``
|
||||
[before after]
|
||||
(keep-syntax before (if (= :array (type after))
|
||||
(tuple/slice after)
|
||||
after)))
|
||||
|
||||
(defmacro ->
|
||||
``Threading macro. Inserts x as the second value in the first form
|
||||
in `forms`, and inserts the modified first form into the second form
|
||||
@@ -1242,7 +1269,7 @@
|
||||
(tuple (in n 0) (array/slice n 1))
|
||||
(tuple n @[])))
|
||||
(def parts (array/concat @[h last] t))
|
||||
(tuple/slice parts 0))
|
||||
(keep-syntax! n parts))
|
||||
(reduce fop x forms))
|
||||
|
||||
(defmacro ->>
|
||||
@@ -1255,7 +1282,7 @@
|
||||
(tuple (in n 0) (array/slice n 1))
|
||||
(tuple n @[])))
|
||||
(def parts (array/concat @[h] t @[last]))
|
||||
(tuple/slice parts 0))
|
||||
(keep-syntax! n parts))
|
||||
(reduce fop x forms))
|
||||
|
||||
(defmacro -?>
|
||||
@@ -1271,7 +1298,7 @@
|
||||
(tuple n @[])))
|
||||
(def sym (gensym))
|
||||
(def parts (array/concat @[h sym] t))
|
||||
~(let [,sym ,last] (if ,sym ,(tuple/slice parts 0))))
|
||||
~(let [,sym ,last] (if ,sym ,(keep-syntax! n parts))))
|
||||
(reduce fop x forms))
|
||||
|
||||
(defmacro -?>>
|
||||
@@ -1287,7 +1314,7 @@
|
||||
(tuple n @[])))
|
||||
(def sym (gensym))
|
||||
(def parts (array/concat @[h] t @[sym]))
|
||||
~(let [,sym ,last] (if ,sym ,(tuple/slice parts 0))))
|
||||
~(let [,sym ,last] (if ,sym ,(keep-syntax! n parts))))
|
||||
(reduce fop x forms))
|
||||
|
||||
(defn- walk-ind [f form]
|
||||
@@ -1311,10 +1338,7 @@
|
||||
:table (walk-dict f form)
|
||||
:struct (table/to-struct (walk-dict f form))
|
||||
:array (walk-ind f form)
|
||||
:tuple (let [x (walk-ind f form)]
|
||||
(if (= :parens (tuple/type form))
|
||||
(tuple/slice x)
|
||||
(tuple/brackets ;x)))
|
||||
:tuple (keep-syntax! form (walk-ind f form))
|
||||
form))
|
||||
|
||||
(defn postwalk
|
||||
@@ -1722,6 +1746,14 @@
|
||||
(printf (dyn *pretty-format* "%q") x)
|
||||
(flush))
|
||||
|
||||
|
||||
(defn file/lines
|
||||
"Return an iterator over the lines of a file."
|
||||
[file]
|
||||
(coro
|
||||
(while (def line (file/read file :line))
|
||||
(yield line))))
|
||||
|
||||
###
|
||||
###
|
||||
### Pattern Matching
|
||||
@@ -2063,21 +2095,21 @@
|
||||
ret)
|
||||
|
||||
(defn all
|
||||
``Returns true if `(pred item)` returns a truthy value for every item in `xs`.
|
||||
Otherwise, returns the first falsey `(pred item)` result encountered.
|
||||
Returns true if `xs` is empty.``
|
||||
[pred xs]
|
||||
(var ret true)
|
||||
(loop [x :in xs :while ret] (set ret (pred x)))
|
||||
ret)
|
||||
``Returns true if `(pred item)` is truthy for every item in `ind`.
|
||||
Otherwise, returns the first falsey result encountered.
|
||||
Returns true if `ind` is empty.``
|
||||
[pred ind & inds]
|
||||
(var res true)
|
||||
(map-template :all res pred ind inds)
|
||||
res)
|
||||
|
||||
(defn some
|
||||
``Returns nil if all `xs` are false or nil, otherwise returns the result of the
|
||||
first truthy predicate, `(pred x)`.``
|
||||
[pred xs]
|
||||
(var ret nil)
|
||||
(loop [x :in xs :while (not ret)] (if-let [y (pred x)] (set ret y)))
|
||||
ret)
|
||||
``Returns nil if `(pred item)` is false or nil for every item in `ind`.
|
||||
Otherwise, returns the first truthy result encountered.``
|
||||
[pred ind & inds]
|
||||
(var res nil)
|
||||
(map-template :some res pred ind inds)
|
||||
res)
|
||||
|
||||
(defn deep-not=
|
||||
``Like `not=`, but mutable types (arrays, tables, buffers) are considered
|
||||
@@ -2087,8 +2119,24 @@
|
||||
(or
|
||||
(not= tx (type y))
|
||||
(case tx
|
||||
:tuple (or (not= (length x) (length y)) (some identity (map deep-not= x y)))
|
||||
:array (or (not= (length x) (length y)) (some identity (map deep-not= x y)))
|
||||
:tuple (or (not= (length x) (length y))
|
||||
(do
|
||||
(var ret false)
|
||||
(forv i 0 (length x)
|
||||
(def xx (in x i))
|
||||
(def yy (in y i))
|
||||
(if (deep-not= xx yy)
|
||||
(break (set ret true))))
|
||||
ret))
|
||||
:array (or (not= (length x) (length y))
|
||||
(do
|
||||
(var ret false)
|
||||
(forv i 0 (length x)
|
||||
(def xx (in x i))
|
||||
(def yy (in y i))
|
||||
(if (deep-not= xx yy)
|
||||
(break (set ret true))))
|
||||
ret))
|
||||
:struct (deep-not= (kvs x) (kvs y))
|
||||
:table (deep-not= (table/to-struct x) (table/to-struct y))
|
||||
:buffer (not= (string x) (string y))
|
||||
@@ -2115,6 +2163,19 @@
|
||||
:buffer (string x)
|
||||
x))
|
||||
|
||||
(defn thaw
|
||||
`Thaw an object (make it mutable) and do a deep copy, making
|
||||
child value also mutable. Closures, fibers, and abstract
|
||||
types will not be recursively thawed, but all other types will`
|
||||
[ds]
|
||||
(case (type ds)
|
||||
:array (walk-ind thaw ds)
|
||||
:tuple (walk-ind thaw ds)
|
||||
:table (walk-dict thaw (table/proto-flatten ds))
|
||||
:struct (walk-dict thaw (struct/proto-flatten ds))
|
||||
:string (buffer ds)
|
||||
ds))
|
||||
|
||||
(defn macex
|
||||
``Expand macros completely.
|
||||
`on-binding` is an optional callback for whenever a normal symbolic binding
|
||||
@@ -2182,6 +2243,7 @@
|
||||
(defn saw-special-arg
|
||||
[num]
|
||||
(set max-param-seen (max max-param-seen num)))
|
||||
(def prefix (gensym))
|
||||
(defn on-binding
|
||||
[x]
|
||||
(if (string/has-prefix? '$ x)
|
||||
@@ -2189,22 +2251,24 @@
|
||||
(= '$ x)
|
||||
(do
|
||||
(saw-special-arg 0)
|
||||
'$0)
|
||||
(symbol prefix '$0))
|
||||
(= '$& x)
|
||||
(do
|
||||
(set vararg true)
|
||||
x)
|
||||
(symbol prefix x))
|
||||
:else
|
||||
(do
|
||||
(def num (scan-number (string/slice x 1)))
|
||||
(if (nat? num)
|
||||
(saw-special-arg num))
|
||||
x))
|
||||
(do
|
||||
(saw-special-arg num)
|
||||
(symbol prefix x))
|
||||
x)))
|
||||
x))
|
||||
(def expanded (macex arg on-binding))
|
||||
(def name-splice (if name [name] []))
|
||||
(def fn-args (seq [i :range [0 (+ 1 max-param-seen)]] (symbol '$ i)))
|
||||
~(fn ,;name-splice [,;fn-args ,;(if vararg ['& '$&] [])] ,expanded))
|
||||
(def fn-args (seq [i :range [0 (+ 1 max-param-seen)]] (symbol prefix '$ i)))
|
||||
~(fn ,;name-splice [,;fn-args ,;(if vararg ['& (symbol prefix '$&)] [])] ,expanded))
|
||||
|
||||
###
|
||||
###
|
||||
@@ -3828,6 +3892,7 @@
|
||||
|
||||
(if-let [jp (getenv-alias "JANET_PATH")] (setdyn *syspath* jp))
|
||||
(if-let [jprofile (getenv-alias "JANET_PROFILE")] (setdyn *profilepath* jprofile))
|
||||
(set colorize (not (getenv-alias "NO_COLOR")))
|
||||
|
||||
(defn- get-lint-level
|
||||
[i]
|
||||
@@ -3845,7 +3910,7 @@
|
||||
-v : Print the version string
|
||||
-s : Use raw stdin instead of getline like functionality
|
||||
-e code : Execute a string of janet
|
||||
-E code arguments... : Evaluate an expression as a short-fn with arguments
|
||||
-E code arguments... : Evaluate an expression as a short-fn with arguments
|
||||
-d : Set the debug flag in the REPL
|
||||
-r : Enter the REPL after running all scripts
|
||||
-R : Disables loading profile.janet when JANET_PROFILE is present
|
||||
@@ -3856,6 +3921,7 @@
|
||||
-c source output : Compile janet source code into an image
|
||||
-i : Load the script argument as an image file instead of source code
|
||||
-n : Disable ANSI color output in the REPL
|
||||
-N : Enable ANSI color output in the REPL
|
||||
-l lib : Use a module before processing more arguments
|
||||
-w level : Set the lint warning level - default is "normal"
|
||||
-x level : Set the lint error level - default is "none"
|
||||
@@ -3871,6 +3937,7 @@
|
||||
"i" (fn [&] (set expect-image true) 1)
|
||||
"k" (fn [&] (set compile-only true) (set exit-on-error false) 1)
|
||||
"n" (fn [&] (set colorize false) 1)
|
||||
"N" (fn [&] (set colorize true) 1)
|
||||
"m" (fn [i &] (setdyn *syspath* (in args (+ i 1))) 2)
|
||||
"c" (fn c-switch [i &]
|
||||
(def path (in args (+ i 1)))
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
#define JANETCONF_H
|
||||
|
||||
#define JANET_VERSION_MAJOR 1
|
||||
#define JANET_VERSION_MINOR 27
|
||||
#define JANET_VERSION_MINOR 28
|
||||
#define JANET_VERSION_PATCH 0
|
||||
#define JANET_VERSION_EXTRA ""
|
||||
#define JANET_VERSION "1.27.0"
|
||||
#define JANET_VERSION_EXTRA "-dev"
|
||||
#define JANET_VERSION "1.28.0-dev"
|
||||
|
||||
/* #define JANET_BUILD "local" */
|
||||
|
||||
|
||||
@@ -324,7 +324,8 @@ static void buffer_push_impl(JanetBuffer *buffer, Janet *argv, int32_t argc_offs
|
||||
|
||||
JANET_CORE_FN(cfun_buffer_push_at,
|
||||
"(buffer/push-at buffer index & xs)",
|
||||
"Same as buffer/push, but inserts new data at index `index`.") {
|
||||
"Same as buffer/push, but copies the new data into the buffer "
|
||||
" at index `index`.") {
|
||||
janet_arity(argc, 2, -1);
|
||||
JanetBuffer *buffer = janet_getbuffer(argv, 0);
|
||||
int32_t index = janet_getinteger(argv, 1);
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <janet.h>
|
||||
#include "gc.h"
|
||||
#include "util.h"
|
||||
#include "regalloc.h"
|
||||
#endif
|
||||
|
||||
/* Look up table for instructions */
|
||||
@@ -106,6 +107,288 @@ enum JanetInstructionType janet_instructions[JOP_INSTRUCTION_COUNT] = {
|
||||
JINT_SSS /* JOP_CANCEL, */
|
||||
};
|
||||
|
||||
/* Remove all noops while preserving jumps and debugging information.
|
||||
* Useful as part of a filtering compiler pass. */
|
||||
void janet_bytecode_remove_noops(JanetFuncDef *def) {
|
||||
|
||||
/* Get an instruction rewrite map so we can rewrite jumps */
|
||||
uint32_t *pc_map = janet_smalloc(sizeof(uint32_t) * (1 + def->bytecode_length));
|
||||
uint32_t new_bytecode_length = 0;
|
||||
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
||||
uint32_t instr = def->bytecode[i];
|
||||
uint32_t opcode = instr & 0x7F;
|
||||
pc_map[i] = new_bytecode_length;
|
||||
if (opcode != JOP_NOOP) {
|
||||
new_bytecode_length++;
|
||||
}
|
||||
}
|
||||
pc_map[def->bytecode_length] = new_bytecode_length;
|
||||
|
||||
/* Linear scan rewrite bytecode and sourcemap. Also fix jumps. */
|
||||
int32_t j = 0;
|
||||
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
||||
uint32_t instr = def->bytecode[i];
|
||||
uint32_t opcode = instr & 0x7F;
|
||||
int32_t old_jump_target = 0;
|
||||
int32_t new_jump_target = 0;
|
||||
switch (opcode) {
|
||||
case JOP_NOOP:
|
||||
continue;
|
||||
case JOP_JUMP:
|
||||
/* relative pc is in DS field of instruction */
|
||||
old_jump_target = i + (((int32_t)instr) >> 8);
|
||||
new_jump_target = pc_map[old_jump_target];
|
||||
instr += (new_jump_target - old_jump_target + (i - j)) << 8;
|
||||
break;
|
||||
case JOP_JUMP_IF:
|
||||
case JOP_JUMP_IF_NIL:
|
||||
case JOP_JUMP_IF_NOT:
|
||||
case JOP_JUMP_IF_NOT_NIL:
|
||||
/* relative pc is in ES field of instruction */
|
||||
old_jump_target = i + (((int32_t)instr) >> 16);
|
||||
new_jump_target = pc_map[old_jump_target];
|
||||
instr += (new_jump_target - old_jump_target + (i - j)) << 16;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
def->bytecode[j] = instr;
|
||||
if (def->sourcemap != NULL) {
|
||||
def->sourcemap[j] = def->sourcemap[i];
|
||||
}
|
||||
j++;
|
||||
}
|
||||
|
||||
/* Rewrite symbolmap */
|
||||
for (int32_t i = 0; i < def->symbolmap_length; i++) {
|
||||
JanetSymbolMap *sm = def->symbolmap + i;
|
||||
/* Don't rewrite upvalue mappings */
|
||||
if (sm->birth_pc < UINT32_MAX) {
|
||||
sm->birth_pc = pc_map[sm->birth_pc];
|
||||
sm->death_pc = pc_map[sm->death_pc];
|
||||
}
|
||||
}
|
||||
|
||||
def->bytecode_length = new_bytecode_length;
|
||||
janet_sfree(pc_map);
|
||||
}
|
||||
|
||||
/* Remove redundant loads, moves and other instructions if possible and convert them to
|
||||
* noops. Input is assumed valid bytecode. */
|
||||
void janet_bytecode_movopt(JanetFuncDef *def) {
|
||||
JanetcRegisterAllocator ra;
|
||||
int recur = 1;
|
||||
|
||||
/* Iterate this until no more instructions can be removed. */
|
||||
while (recur) {
|
||||
janetc_regalloc_init(&ra);
|
||||
|
||||
/* Look for slots that have writes but no reads (and aren't in the closure bitset). */
|
||||
if (def->closure_bitset != NULL) {
|
||||
for (int32_t i = 0; i < def->slotcount; i++) {
|
||||
int32_t index = i >> 5;
|
||||
uint32_t mask = 1U << (((uint32_t) i) & 31);
|
||||
if (def->closure_bitset[index] & mask) {
|
||||
janetc_regalloc_touch(&ra, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define AA ((instr >> 8) & 0xFF)
|
||||
#define BB ((instr >> 16) & 0xFF)
|
||||
#define CC (instr >> 24)
|
||||
#define DD (instr >> 8)
|
||||
#define EE (instr >> 16)
|
||||
|
||||
/* Check reads and writes */
|
||||
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
||||
uint32_t instr = def->bytecode[i];
|
||||
switch (instr & 0x7F) {
|
||||
|
||||
/* Group instructions my how they read from slots */
|
||||
|
||||
/* No reads or writes */
|
||||
default:
|
||||
janet_assert(0, "unhandled instruction");
|
||||
case JOP_JUMP:
|
||||
case JOP_NOOP:
|
||||
case JOP_RETURN_NIL:
|
||||
/* Write A */
|
||||
case JOP_LOAD_INTEGER:
|
||||
case JOP_LOAD_CONSTANT:
|
||||
case JOP_LOAD_UPVALUE:
|
||||
case JOP_CLOSURE:
|
||||
/* Write D */
|
||||
case JOP_LOAD_NIL:
|
||||
case JOP_LOAD_TRUE:
|
||||
case JOP_LOAD_FALSE:
|
||||
case JOP_LOAD_SELF:
|
||||
case JOP_MAKE_ARRAY:
|
||||
case JOP_MAKE_BUFFER:
|
||||
case JOP_MAKE_STRING:
|
||||
case JOP_MAKE_STRUCT:
|
||||
case JOP_MAKE_TABLE:
|
||||
case JOP_MAKE_TUPLE:
|
||||
case JOP_MAKE_BRACKET_TUPLE:
|
||||
break;
|
||||
|
||||
/* Read A */
|
||||
case JOP_ERROR:
|
||||
case JOP_TYPECHECK:
|
||||
case JOP_JUMP_IF:
|
||||
case JOP_JUMP_IF_NOT:
|
||||
case JOP_JUMP_IF_NIL:
|
||||
case JOP_JUMP_IF_NOT_NIL:
|
||||
case JOP_SET_UPVALUE:
|
||||
/* Write E, Read A */
|
||||
case JOP_MOVE_FAR:
|
||||
janetc_regalloc_touch(&ra, AA);
|
||||
break;
|
||||
|
||||
/* Read B */
|
||||
case JOP_SIGNAL:
|
||||
/* Write A, Read B */
|
||||
case JOP_ADD_IMMEDIATE:
|
||||
case JOP_MULTIPLY_IMMEDIATE:
|
||||
case JOP_DIVIDE_IMMEDIATE:
|
||||
case JOP_SHIFT_LEFT_IMMEDIATE:
|
||||
case JOP_SHIFT_RIGHT_IMMEDIATE:
|
||||
case JOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE:
|
||||
case JOP_GREATER_THAN_IMMEDIATE:
|
||||
case JOP_LESS_THAN_IMMEDIATE:
|
||||
case JOP_EQUALS_IMMEDIATE:
|
||||
case JOP_NOT_EQUALS_IMMEDIATE:
|
||||
case JOP_GET_INDEX:
|
||||
janetc_regalloc_touch(&ra, BB);
|
||||
break;
|
||||
|
||||
/* Read D */
|
||||
case JOP_RETURN:
|
||||
case JOP_PUSH:
|
||||
case JOP_PUSH_ARRAY:
|
||||
case JOP_TAILCALL:
|
||||
janetc_regalloc_touch(&ra, DD);
|
||||
break;
|
||||
|
||||
/* Write A, Read E */
|
||||
case JOP_MOVE_NEAR:
|
||||
case JOP_LENGTH:
|
||||
case JOP_BNOT:
|
||||
case JOP_CALL:
|
||||
janetc_regalloc_touch(&ra, EE);
|
||||
break;
|
||||
|
||||
/* Read A, B */
|
||||
case JOP_PUT_INDEX:
|
||||
janetc_regalloc_touch(&ra, AA);
|
||||
janetc_regalloc_touch(&ra, BB);
|
||||
break;
|
||||
|
||||
/* Read A, E */
|
||||
case JOP_PUSH_2:
|
||||
janetc_regalloc_touch(&ra, AA);
|
||||
janetc_regalloc_touch(&ra, EE);
|
||||
break;
|
||||
|
||||
/* Read B, C */
|
||||
case JOP_PROPAGATE:
|
||||
/* Write A, Read B and C */
|
||||
case JOP_BAND:
|
||||
case JOP_BOR:
|
||||
case JOP_BXOR:
|
||||
case JOP_ADD:
|
||||
case JOP_SUBTRACT:
|
||||
case JOP_MULTIPLY:
|
||||
case JOP_DIVIDE:
|
||||
case JOP_MODULO:
|
||||
case JOP_REMAINDER:
|
||||
case JOP_SHIFT_LEFT:
|
||||
case JOP_SHIFT_RIGHT:
|
||||
case JOP_SHIFT_RIGHT_UNSIGNED:
|
||||
case JOP_GREATER_THAN:
|
||||
case JOP_LESS_THAN:
|
||||
case JOP_EQUALS:
|
||||
case JOP_COMPARE:
|
||||
case JOP_IN:
|
||||
case JOP_GET:
|
||||
case JOP_GREATER_THAN_EQUAL:
|
||||
case JOP_LESS_THAN_EQUAL:
|
||||
case JOP_NOT_EQUALS:
|
||||
case JOP_CANCEL:
|
||||
case JOP_RESUME:
|
||||
case JOP_NEXT:
|
||||
janetc_regalloc_touch(&ra, BB);
|
||||
janetc_regalloc_touch(&ra, CC);
|
||||
break;
|
||||
|
||||
/* Read A, B, C */
|
||||
case JOP_PUT:
|
||||
case JOP_PUSH_3:
|
||||
janetc_regalloc_touch(&ra, AA);
|
||||
janetc_regalloc_touch(&ra, BB);
|
||||
janetc_regalloc_touch(&ra, CC);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Iterate and set noops on instructions that make writes that no one ever reads.
|
||||
* Only set noops for instructions with no side effects - moves, loads, etc. that can't
|
||||
* raise errors (outside of systemic errors like oom or stack overflow). */
|
||||
recur = 0;
|
||||
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
||||
uint32_t instr = def->bytecode[i];
|
||||
switch (instr & 0x7F) {
|
||||
default:
|
||||
break;
|
||||
/* Write D */
|
||||
case JOP_LOAD_NIL:
|
||||
case JOP_LOAD_TRUE:
|
||||
case JOP_LOAD_FALSE:
|
||||
case JOP_LOAD_SELF:
|
||||
case JOP_MAKE_ARRAY:
|
||||
case JOP_MAKE_TUPLE:
|
||||
case JOP_MAKE_BRACKET_TUPLE: {
|
||||
if (!janetc_regalloc_check(&ra, DD)) {
|
||||
def->bytecode[i] = JOP_NOOP;
|
||||
recur = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
/* Write E, Read A */
|
||||
case JOP_MOVE_FAR: {
|
||||
if (!janetc_regalloc_check(&ra, EE)) {
|
||||
def->bytecode[i] = JOP_NOOP;
|
||||
recur = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
/* Write A, Read E */
|
||||
case JOP_MOVE_NEAR:
|
||||
/* Write A, Read B */
|
||||
case JOP_GET_INDEX:
|
||||
/* Write A */
|
||||
case JOP_LOAD_INTEGER:
|
||||
case JOP_LOAD_CONSTANT:
|
||||
case JOP_LOAD_UPVALUE:
|
||||
case JOP_CLOSURE: {
|
||||
if (!janetc_regalloc_check(&ra, AA)) {
|
||||
def->bytecode[i] = JOP_NOOP;
|
||||
recur = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
janetc_regalloc_deinit(&ra);
|
||||
#undef AA
|
||||
#undef BB
|
||||
#undef CC
|
||||
#undef DD
|
||||
#undef EE
|
||||
}
|
||||
}
|
||||
|
||||
/* Verify some bytecode */
|
||||
int janet_verify(JanetFuncDef *def) {
|
||||
int vargs = !!(def->flags & JANET_FUNCDEF_FLAG_VARARG);
|
||||
|
||||
@@ -209,14 +209,28 @@ const char *janet_optcstring(const Janet *argv, int32_t argc, int32_t n, const c
|
||||
#undef DEFINE_OPTLEN
|
||||
|
||||
const char *janet_getcstring(const Janet *argv, int32_t n) {
|
||||
const uint8_t *jstr = janet_getstring(argv, n);
|
||||
const char *cstr = (const char *)jstr;
|
||||
if (strlen(cstr) != (size_t) janet_string_length(jstr)) {
|
||||
janet_panic("string contains embedded 0s");
|
||||
if (!janet_checktype(argv[n], JANET_STRING)) {
|
||||
janet_panic_type(argv[n], n, JANET_TFLAG_STRING);
|
||||
}
|
||||
return janet_getcbytes(argv, n);
|
||||
}
|
||||
|
||||
const char *janet_getcbytes(const Janet *argv, int32_t n) {
|
||||
JanetByteView view = janet_getbytes(argv, n);
|
||||
const char *cstr = (const char *)view.bytes;
|
||||
if (strlen(cstr) != (size_t) view.len) {
|
||||
janet_panic("bytes contain embedded 0s");
|
||||
}
|
||||
return cstr;
|
||||
}
|
||||
|
||||
const char *janet_optcbytes(const Janet *argv, int32_t argc, int32_t n, const char *dflt) {
|
||||
if (n >= argc || janet_checktype(argv[n], JANET_NIL)) {
|
||||
return dflt;
|
||||
}
|
||||
return janet_getcbytes(argv, n);
|
||||
}
|
||||
|
||||
int32_t janet_getnat(const Janet *argv, int32_t n) {
|
||||
Janet x = argv[n];
|
||||
if (!janet_checkint(x)) goto bad;
|
||||
|
||||
@@ -422,6 +422,7 @@ JanetSlot *janetc_toslots(JanetCompiler *c, const Janet *vals, int32_t len) {
|
||||
int32_t i;
|
||||
JanetSlot *ret = NULL;
|
||||
JanetFopts subopts = janetc_fopts_default(c);
|
||||
subopts.flags |= JANET_FOPTS_ACCEPT_SPLICE;
|
||||
for (i = 0; i < len; i++) {
|
||||
janet_v_push(ret, janetc_value(subopts, vals[i]));
|
||||
}
|
||||
@@ -432,6 +433,7 @@ JanetSlot *janetc_toslots(JanetCompiler *c, const Janet *vals, int32_t len) {
|
||||
JanetSlot *janetc_toslotskv(JanetCompiler *c, Janet ds) {
|
||||
JanetSlot *ret = NULL;
|
||||
JanetFopts subopts = janetc_fopts_default(c);
|
||||
subopts.flags |= JANET_FOPTS_ACCEPT_SPLICE;
|
||||
const JanetKV *kvs = NULL;
|
||||
int32_t cap = 0, len = 0;
|
||||
janet_dictionary_view(ds, &kvs, &len, &cap);
|
||||
@@ -987,6 +989,10 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c) {
|
||||
/* Pop the scope */
|
||||
janetc_popscope(c);
|
||||
|
||||
/* Do basic optimization */
|
||||
janet_bytecode_movopt(def);
|
||||
janet_bytecode_remove_noops(def);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
|
||||
@@ -187,6 +187,7 @@ struct JanetCompiler {
|
||||
#define JANET_FOPTS_TAIL 0x10000
|
||||
#define JANET_FOPTS_HINT 0x20000
|
||||
#define JANET_FOPTS_DROP 0x40000
|
||||
#define JANET_FOPTS_ACCEPT_SPLICE 0x80000
|
||||
|
||||
/* Options for compiling a single form */
|
||||
struct JanetFopts {
|
||||
@@ -266,4 +267,8 @@ JanetSlot janetc_cslot(Janet x);
|
||||
/* Search for a symbol */
|
||||
JanetSlot janetc_resolve(JanetCompiler *c, const uint8_t *sym);
|
||||
|
||||
/* Bytecode optimization */
|
||||
void janet_bytecode_movopt(JanetFuncDef *def);
|
||||
void janet_bytecode_remove_noops(JanetFuncDef *def);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -458,7 +458,7 @@ JANET_CORE_FN(janet_core_getproto,
|
||||
? janet_wrap_struct(janet_struct_proto(st))
|
||||
: janet_wrap_nil();
|
||||
}
|
||||
janet_panicf("expected struct|table, got %v", argv[0]);
|
||||
janet_panicf("expected struct or table, got %v", argv[0]);
|
||||
}
|
||||
|
||||
JANET_CORE_FN(janet_core_struct,
|
||||
|
||||
212
src/core/ev.c
212
src/core/ev.c
@@ -472,8 +472,12 @@ const JanetAbstractType janet_stream_type = {
|
||||
/* Register a fiber to resume with value */
|
||||
void janet_schedule_signal(JanetFiber *fiber, Janet value, JanetSignal sig) {
|
||||
if (fiber->gc.flags & JANET_FIBER_EV_FLAG_CANCELED) return;
|
||||
fiber->gc.flags |= JANET_FIBER_FLAG_ROOT;
|
||||
if (!(fiber->gc.flags & JANET_FIBER_FLAG_ROOT)) {
|
||||
Janet task_element = janet_wrap_fiber(fiber);
|
||||
janet_table_put(&janet_vm.active_tasks, task_element, janet_wrap_true());
|
||||
}
|
||||
JanetTask t = { fiber, value, sig, ++fiber->sched_id };
|
||||
fiber->gc.flags |= JANET_FIBER_FLAG_ROOT;
|
||||
if (sig == JANET_SIGNAL_ERROR) fiber->gc.flags |= JANET_FIBER_EV_FLAG_CANCELED;
|
||||
janet_q_push(&janet_vm.spawn, &t, sizeof(t));
|
||||
}
|
||||
@@ -559,6 +563,7 @@ void janet_ev_init_common(void) {
|
||||
janet_vm.tq_count = 0;
|
||||
janet_vm.tq_capacity = 0;
|
||||
janet_table_init_raw(&janet_vm.threaded_abstracts, 0);
|
||||
janet_table_init_raw(&janet_vm.active_tasks, 0);
|
||||
janet_rng_seed(&janet_vm.ev_rng, 0);
|
||||
#ifndef JANET_WINDOWS
|
||||
pthread_attr_init(&janet_vm.new_thread_attr);
|
||||
@@ -573,13 +578,15 @@ void janet_ev_deinit_common(void) {
|
||||
janet_free(janet_vm.listeners);
|
||||
janet_vm.listeners = NULL;
|
||||
janet_table_deinit(&janet_vm.threaded_abstracts);
|
||||
janet_table_deinit(&janet_vm.active_tasks);
|
||||
#ifndef JANET_WINDOWS
|
||||
pthread_attr_destroy(&janet_vm.new_thread_attr);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Short hand to yield to event loop */
|
||||
/* Shorthand to yield to event loop */
|
||||
void janet_await(void) {
|
||||
/* Store the fiber in a gobal table */
|
||||
janet_signalv(JANET_SIGNAL_EVENT, janet_wrap_nil());
|
||||
}
|
||||
|
||||
@@ -665,19 +672,6 @@ static void janet_chan_init(JanetChannel *chan, int32_t limit, int threaded) {
|
||||
janet_os_mutex_init((JanetOSMutex *) &chan->lock);
|
||||
}
|
||||
|
||||
static void janet_chan_deinit(JanetChannel *chan) {
|
||||
janet_q_deinit(&chan->read_pending);
|
||||
janet_q_deinit(&chan->write_pending);
|
||||
if (janet_chan_is_threaded(chan)) {
|
||||
Janet item;
|
||||
while (!janet_q_pop(&chan->items, &item, sizeof(item))) {
|
||||
janet_chan_unpack(chan, &item, 1);
|
||||
}
|
||||
}
|
||||
janet_q_deinit(&chan->items);
|
||||
janet_os_mutex_deinit((JanetOSMutex *) &chan->lock);
|
||||
}
|
||||
|
||||
static void janet_chan_lock(JanetChannel *chan) {
|
||||
if (!janet_chan_is_threaded(chan)) return;
|
||||
janet_os_mutex_lock((JanetOSMutex *) &chan->lock);
|
||||
@@ -688,6 +682,25 @@ static void janet_chan_unlock(JanetChannel *chan) {
|
||||
janet_os_mutex_unlock((JanetOSMutex *) &chan->lock);
|
||||
}
|
||||
|
||||
static void janet_chan_deinit(JanetChannel *chan) {
|
||||
if (janet_chan_is_threaded(chan)) {
|
||||
Janet item;
|
||||
janet_chan_lock(chan);
|
||||
janet_q_deinit(&chan->read_pending);
|
||||
janet_q_deinit(&chan->write_pending);
|
||||
while (!janet_q_pop(&chan->items, &item, sizeof(item))) {
|
||||
janet_chan_unpack(chan, &item, 1);
|
||||
}
|
||||
janet_q_deinit(&chan->items);
|
||||
janet_chan_unlock(chan);
|
||||
} else {
|
||||
janet_q_deinit(&chan->read_pending);
|
||||
janet_q_deinit(&chan->write_pending);
|
||||
janet_q_deinit(&chan->items);
|
||||
}
|
||||
janet_os_mutex_deinit((JanetOSMutex *) &chan->lock);
|
||||
}
|
||||
|
||||
/*
|
||||
* Janet Channel abstract type
|
||||
*/
|
||||
@@ -764,6 +777,7 @@ static void janet_thread_chan_cb(JanetEVGenericMessage msg) {
|
||||
int mode = msg.tag;
|
||||
JanetChannel *channel = (JanetChannel *) msg.argp;
|
||||
Janet x = msg.argj;
|
||||
janet_chan_lock(channel);
|
||||
if (fiber->sched_id == sched_id) {
|
||||
if (mode == JANET_CP_MODE_CHOICE_READ) {
|
||||
janet_assert(!janet_chan_unpack(channel, &x, 0), "packing error");
|
||||
@@ -784,7 +798,6 @@ static void janet_thread_chan_cb(JanetEVGenericMessage msg) {
|
||||
int is_read = (mode == JANET_CP_MODE_CHOICE_READ) || (mode == JANET_CP_MODE_READ);
|
||||
if (is_read) {
|
||||
JanetChannelPending reader;
|
||||
janet_chan_lock(channel);
|
||||
if (!janet_q_pop(&channel->read_pending, &reader, sizeof(reader))) {
|
||||
JanetVM *vm = reader.thread;
|
||||
JanetEVGenericMessage msg;
|
||||
@@ -795,10 +808,8 @@ static void janet_thread_chan_cb(JanetEVGenericMessage msg) {
|
||||
msg.argj = x;
|
||||
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
||||
}
|
||||
janet_chan_unlock(channel);
|
||||
} else {
|
||||
JanetChannelPending writer;
|
||||
janet_chan_lock(channel);
|
||||
if (!janet_q_pop(&channel->write_pending, &writer, sizeof(writer))) {
|
||||
JanetVM *vm = writer.thread;
|
||||
JanetEVGenericMessage msg;
|
||||
@@ -809,21 +820,21 @@ static void janet_thread_chan_cb(JanetEVGenericMessage msg) {
|
||||
msg.argj = janet_wrap_nil();
|
||||
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
||||
}
|
||||
janet_chan_unlock(channel);
|
||||
}
|
||||
}
|
||||
janet_chan_unlock(channel);
|
||||
}
|
||||
|
||||
/* Push a value to a channel, and return 1 if channel should block, zero otherwise.
|
||||
* If the push would block, will add to the write_pending queue in the channel.
|
||||
* Handles both threaded and unthreaded channels. */
|
||||
static int janet_channel_push(JanetChannel *channel, Janet x, int mode) {
|
||||
static int janet_channel_push_with_lock(JanetChannel *channel, Janet x, int mode) {
|
||||
JanetChannelPending reader;
|
||||
int is_empty;
|
||||
if (janet_chan_pack(channel, &x)) {
|
||||
janet_chan_unlock(channel);
|
||||
janet_panicf("failed to pack value for channel: %v", x);
|
||||
}
|
||||
janet_chan_lock(channel);
|
||||
if (channel->closed) {
|
||||
janet_chan_unlock(channel);
|
||||
janet_panic("cannot write to closed channel");
|
||||
@@ -884,12 +895,16 @@ static int janet_channel_push(JanetChannel *channel, Janet x, int mode) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int janet_channel_push(JanetChannel *channel, Janet x, int mode) {
|
||||
janet_chan_lock(channel);
|
||||
return janet_channel_push_with_lock(channel, x, mode);
|
||||
}
|
||||
|
||||
/* Pop from a channel - returns 1 if item was obtained, 0 otherwise. The item
|
||||
* is returned by reference. If the pop would block, will add to the read_pending
|
||||
* queue in the channel. */
|
||||
static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice) {
|
||||
static int janet_channel_pop_with_lock(JanetChannel *channel, Janet *item, int is_choice) {
|
||||
JanetChannelPending writer;
|
||||
janet_chan_lock(channel);
|
||||
if (channel->closed) {
|
||||
janet_chan_unlock(channel);
|
||||
*item = janet_wrap_nil();
|
||||
@@ -934,6 +949,11 @@ static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice) {
|
||||
janet_chan_lock(channel);
|
||||
return janet_channel_pop_with_lock(channel, item, is_choice);
|
||||
}
|
||||
|
||||
JanetChannel *janet_channel_unwrap(void *abstract) {
|
||||
return abstract;
|
||||
}
|
||||
@@ -976,13 +996,32 @@ JANET_CORE_FN(cfun_channel_pop,
|
||||
janet_await();
|
||||
}
|
||||
|
||||
static void chan_unlock_args(const Janet *argv, int32_t n) {
|
||||
for (int32_t i = 0; i < n; i++) {
|
||||
int32_t len;
|
||||
const Janet *data;
|
||||
JanetChannel *chan;
|
||||
if (janet_indexed_view(argv[i], &data, &len) && len == 2) {
|
||||
chan = janet_getchannel(data, 0);
|
||||
} else {
|
||||
chan = janet_getchannel(argv, i);
|
||||
}
|
||||
janet_chan_unlock(chan);
|
||||
}
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_channel_choice,
|
||||
"(ev/select & clauses)",
|
||||
"Block until the first of several channel operations occur. Returns a tuple of the form [:give chan], [:take chan x], or [:close chan], where "
|
||||
"a :give tuple is the result of a write and :take tuple is the result of a read. Each clause must be either a channel (for "
|
||||
"a channel take operation) or a tuple [channel x] for a channel give operation. Operations are tried in order, such that the first "
|
||||
"clauses will take precedence over later clauses. Both and give and take operations can return a [:close chan] tuple, which indicates that "
|
||||
"the specified channel was closed while waiting, or that the channel was already closed.") {
|
||||
"Block until the first of several channel operations occur. Returns a "
|
||||
"tuple of the form [:give chan], [:take chan x], or [:close chan], "
|
||||
"where a :give tuple is the result of a write and a :take tuple is the "
|
||||
"result of a read. Each clause must be either a channel (for a channel "
|
||||
"take operation) or a tuple [channel x] (for a channel give operation). "
|
||||
"Operations are tried in order such that earlier clauses take "
|
||||
"precedence over later clauses. Both give and take operations can "
|
||||
"return a [:close chan] tuple, which indicates that the specified "
|
||||
"channel was closed while waiting, or that the channel was already "
|
||||
"closed.") {
|
||||
janet_arity(argc, 1, -1);
|
||||
int32_t len;
|
||||
const Janet *data;
|
||||
@@ -995,29 +1034,29 @@ JANET_CORE_FN(cfun_channel_choice,
|
||||
janet_chan_lock(chan);
|
||||
if (chan->closed) {
|
||||
janet_chan_unlock(chan);
|
||||
chan_unlock_args(argv, i);
|
||||
return make_close_result(chan);
|
||||
}
|
||||
if (janet_q_count(&chan->items) < chan->limit) {
|
||||
janet_chan_unlock(chan);
|
||||
janet_channel_push(chan, data[1], 1);
|
||||
janet_channel_push_with_lock(chan, data[1], 1);
|
||||
chan_unlock_args(argv, i);
|
||||
return make_write_result(chan);
|
||||
}
|
||||
janet_chan_unlock(chan);
|
||||
} else {
|
||||
/* Read */
|
||||
JanetChannel *chan = janet_getchannel(argv, i);
|
||||
janet_chan_lock(chan);
|
||||
if (chan->closed) {
|
||||
janet_chan_unlock(chan);
|
||||
chan_unlock_args(argv, i);
|
||||
return make_close_result(chan);
|
||||
}
|
||||
if (chan->items.head != chan->items.tail) {
|
||||
Janet item;
|
||||
janet_chan_unlock(chan);
|
||||
janet_channel_pop(chan, &item, 1);
|
||||
janet_channel_pop_with_lock(chan, &item, 1);
|
||||
chan_unlock_args(argv, i);
|
||||
return make_read_result(chan, item);
|
||||
}
|
||||
janet_chan_unlock(chan);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1026,12 +1065,12 @@ JANET_CORE_FN(cfun_channel_choice,
|
||||
if (janet_indexed_view(argv[i], &data, &len) && len == 2) {
|
||||
/* Write */
|
||||
JanetChannel *chan = janet_getchannel(data, 0);
|
||||
janet_channel_push(chan, data[1], 1);
|
||||
janet_channel_push_with_lock(chan, data[1], 1);
|
||||
} else {
|
||||
/* Read */
|
||||
Janet item;
|
||||
JanetChannel *chan = janet_getchannel(argv, i);
|
||||
janet_channel_pop(chan, &item, 1);
|
||||
janet_channel_pop_with_lock(chan, &item, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1252,16 +1291,7 @@ JanetFiber *janet_loop1(void) {
|
||||
while (peek_timeout(&to) && to.when <= now) {
|
||||
pop_timeout(0);
|
||||
if (to.curr_fiber != NULL) {
|
||||
/* This is a deadline (for a fiber, not a function call) */
|
||||
JanetFiberStatus s = janet_fiber_status(to.curr_fiber);
|
||||
int isFinished = (s == JANET_STATUS_DEAD ||
|
||||
s == JANET_STATUS_ERROR ||
|
||||
s == JANET_STATUS_USER0 ||
|
||||
s == JANET_STATUS_USER1 ||
|
||||
s == JANET_STATUS_USER2 ||
|
||||
s == JANET_STATUS_USER3 ||
|
||||
s == JANET_STATUS_USER4);
|
||||
if (!isFinished) {
|
||||
if (janet_fiber_can_resume(to.curr_fiber)) {
|
||||
janet_cancel(to.fiber, janet_cstringv("deadline expired"));
|
||||
}
|
||||
} else {
|
||||
@@ -1285,6 +1315,9 @@ JanetFiber *janet_loop1(void) {
|
||||
if (task.expected_sched_id != task.fiber->sched_id) continue;
|
||||
Janet res;
|
||||
JanetSignal sig = janet_continue_signal(task.fiber, task.value, &res, task.sig);
|
||||
if (!janet_fiber_can_resume(task.fiber)) {
|
||||
janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(task.fiber));
|
||||
}
|
||||
void *sv = task.fiber->supervisor_channel;
|
||||
int is_suspended = sig == JANET_SIGNAL_EVENT || sig == JANET_SIGNAL_YIELD || sig == JANET_SIGNAL_INTERRUPT;
|
||||
if (is_suspended) {
|
||||
@@ -1316,15 +1349,8 @@ JanetFiber *janet_loop1(void) {
|
||||
/* Drop timeouts that are no longer needed */
|
||||
while ((has_timeout = peek_timeout(&to))) {
|
||||
if (to.curr_fiber != NULL) {
|
||||
JanetFiberStatus s = janet_fiber_status(to.curr_fiber);
|
||||
int is_finished = (s == JANET_STATUS_DEAD ||
|
||||
s == JANET_STATUS_ERROR ||
|
||||
s == JANET_STATUS_USER0 ||
|
||||
s == JANET_STATUS_USER1 ||
|
||||
s == JANET_STATUS_USER2 ||
|
||||
s == JANET_STATUS_USER3 ||
|
||||
s == JANET_STATUS_USER4);
|
||||
if (is_finished) {
|
||||
if (!janet_fiber_can_resume(to.curr_fiber)) {
|
||||
janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(to.curr_fiber));
|
||||
pop_timeout(0);
|
||||
continue;
|
||||
}
|
||||
@@ -1476,6 +1502,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
|
||||
state = state->_next;
|
||||
}
|
||||
}
|
||||
/* Close the stream if requested and no more listeners are left */
|
||||
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
|
||||
janet_stream_close(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1630,6 +1660,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
||||
janet_unlisten(state, 0);
|
||||
state = next_state;
|
||||
}
|
||||
/* Close the stream if requested and no more listeners are left */
|
||||
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
|
||||
janet_stream_close(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1828,6 +1862,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
||||
|
||||
state = next_state;
|
||||
}
|
||||
/* Close the stream if requested and no more listeners are left */
|
||||
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
|
||||
janet_stream_close(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1931,6 +1969,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
||||
JanetAsyncStatus status3 = JANET_ASYNC_STATUS_NOT_DONE;
|
||||
JanetAsyncStatus status4 = JANET_ASYNC_STATUS_NOT_DONE;
|
||||
state->event = pfd;
|
||||
JanetStream *stream = state->stream;
|
||||
if (mask & POLLOUT)
|
||||
status1 = state->machine(state, JANET_ASYNC_EVENT_WRITE);
|
||||
if (mask & POLLIN)
|
||||
@@ -1944,6 +1983,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
||||
status3 == JANET_ASYNC_STATUS_DONE ||
|
||||
status4 == JANET_ASYNC_STATUS_DONE)
|
||||
janet_unlisten(state, 0);
|
||||
/* Close the stream if requested and no more listeners are left */
|
||||
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
|
||||
janet_stream_close(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2430,7 +2473,8 @@ void janet_ev_recvfrom(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, in
|
||||
typedef enum {
|
||||
JANET_ASYNC_WRITEMODE_WRITE,
|
||||
JANET_ASYNC_WRITEMODE_SEND,
|
||||
JANET_ASYNC_WRITEMODE_SENDTO
|
||||
JANET_ASYNC_WRITEMODE_SENDTO,
|
||||
JANET_ASYNC_WRITEMODE_CONNECT
|
||||
} JanetWriteMode;
|
||||
|
||||
typedef struct {
|
||||
@@ -2454,6 +2498,31 @@ typedef struct {
|
||||
#endif
|
||||
} StateWrite;
|
||||
|
||||
static JanetAsyncStatus handle_connect(JanetListenerState *s) {
|
||||
#ifdef JANET_WINDOWS
|
||||
int res = 0;
|
||||
int size = sizeof(res);
|
||||
int r = getsockopt((SOCKET)s->stream->handle, SOL_SOCKET, SO_ERROR, (char *)&res, &size);
|
||||
#else
|
||||
int res = 0;
|
||||
socklen_t size = sizeof res;
|
||||
int r = getsockopt(s->stream->handle, SOL_SOCKET, SO_ERROR, &res, &size);
|
||||
#endif
|
||||
if (r == 0) {
|
||||
if (res == 0) {
|
||||
janet_schedule(s->fiber, janet_wrap_abstract(s->stream));
|
||||
} else {
|
||||
s->stream->flags |= JANET_STREAM_TOCLOSE;
|
||||
janet_cancel(s->fiber, janet_cstringv(strerror(res)));
|
||||
}
|
||||
} else {
|
||||
s->stream->flags |= JANET_STREAM_TOCLOSE;
|
||||
janet_cancel(s->fiber, janet_ev_lasterr());
|
||||
}
|
||||
return JANET_ASYNC_STATUS_DONE;
|
||||
}
|
||||
|
||||
|
||||
JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event) {
|
||||
StateWrite *state = (StateWrite *) s;
|
||||
switch (event) {
|
||||
@@ -2483,6 +2552,11 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event)
|
||||
}
|
||||
break;
|
||||
case JANET_ASYNC_EVENT_USER: {
|
||||
#ifdef JANET_NET
|
||||
if (state->mode == JANET_ASYNC_WRITEMODE_CONNECT) {
|
||||
return handle_connect(s);
|
||||
}
|
||||
#endif
|
||||
/* Begin write */
|
||||
int32_t len;
|
||||
const uint8_t *bytes;
|
||||
@@ -2546,6 +2620,11 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event)
|
||||
janet_cancel(s->fiber, janet_cstringv("stream hup"));
|
||||
return JANET_ASYNC_STATUS_DONE;
|
||||
case JANET_ASYNC_EVENT_WRITE: {
|
||||
#ifdef JANET_NET
|
||||
if (state->mode == JANET_ASYNC_WRITEMODE_CONNECT) {
|
||||
return handle_connect(s);
|
||||
}
|
||||
#endif
|
||||
int32_t start, len;
|
||||
const uint8_t *bytes;
|
||||
start = state->start;
|
||||
@@ -2648,6 +2727,10 @@ void janet_ev_sendto_buffer(JanetStream *stream, JanetBuffer *buf, void *dest, i
|
||||
void janet_ev_sendto_string(JanetStream *stream, JanetString str, void *dest, int flags) {
|
||||
janet_ev_write_generic(stream, (void *) str, dest, JANET_ASYNC_WRITEMODE_SENDTO, 0, flags);
|
||||
}
|
||||
|
||||
void janet_ev_connect(JanetStream *stream, int flags) {
|
||||
janet_ev_write_generic(stream, NULL, NULL, JANET_ASYNC_WRITEMODE_CONNECT, 0, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* For a pipe ID */
|
||||
@@ -2828,7 +2911,7 @@ static JanetEVGenericMessage janet_go_thread_subr(JanetEVGenericMessage args) {
|
||||
JanetFiber *fiber;
|
||||
if (!janet_checktype(fiberv, JANET_FIBER)) {
|
||||
if (!janet_checktype(fiberv, JANET_FUNCTION)) {
|
||||
janet_panicf("expected function|fiber, got %v", fiberv);
|
||||
janet_panicf("expected function or fiber, got %v", fiberv);
|
||||
}
|
||||
JanetFunction *func = janet_unwrap_function(fiberv);
|
||||
if (func->def->min_arity > 1) {
|
||||
@@ -3174,6 +3257,20 @@ JANET_CORE_FN(janet_cfun_rwlock_write_release,
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
JANET_CORE_FN(janet_cfun_ev_all_tasks,
|
||||
"(ev/all-tasks)",
|
||||
"Get an array of all active fibers that are being used by the scheduler.") {
|
||||
janet_fixarity(argc, 0);
|
||||
(void) argv;
|
||||
JanetArray *array = janet_array(janet_vm.active_tasks.count);
|
||||
for (int32_t i = 0; i < janet_vm.active_tasks.capacity; i++) {
|
||||
if (!janet_checktype(janet_vm.active_tasks.data[i].key, JANET_NIL)) {
|
||||
janet_array_push(array, janet_vm.active_tasks.data[i].key);
|
||||
}
|
||||
}
|
||||
return janet_wrap_array(array);
|
||||
}
|
||||
|
||||
void janet_lib_ev(JanetTable *env) {
|
||||
JanetRegExt ev_cfuns_ext[] = {
|
||||
JANET_CORE_REG("ev/give", cfun_channel_push),
|
||||
@@ -3204,6 +3301,7 @@ void janet_lib_ev(JanetTable *env) {
|
||||
JANET_CORE_REG("ev/acquire-wlock", janet_cfun_rwlock_write_lock),
|
||||
JANET_CORE_REG("ev/release-rlock", janet_cfun_rwlock_read_release),
|
||||
JANET_CORE_REG("ev/release-wlock", janet_cfun_rwlock_write_release),
|
||||
JANET_CORE_REG("ev/all-tasks", janet_cfun_ev_all_tasks),
|
||||
JANET_REG_END
|
||||
};
|
||||
|
||||
|
||||
@@ -26,9 +26,10 @@
|
||||
#define JANET_FEATURES_H_defined
|
||||
|
||||
#if defined(__NetBSD__) || defined(__APPLE__) || defined(__OpenBSD__) \
|
||||
|| defined(__bsdi__) || defined(__DragonFly__)
|
||||
|| defined(__bsdi__) || defined(__DragonFly__) || defined(__FreeBSD__)
|
||||
/* Use BSD source on any BSD systems, include OSX */
|
||||
# define _BSD_SOURCE
|
||||
# define _POSIX_C_SOURCE 200809L
|
||||
#else
|
||||
/* Use POSIX feature flags */
|
||||
# ifndef _POSIX_C_SOURCE
|
||||
@@ -36,6 +37,10 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#define _DARWIN_C_SOURCE
|
||||
#endif
|
||||
|
||||
/* Needed for sched.h for cpu count */
|
||||
#ifdef __linux__
|
||||
#define _GNU_SOURCE
|
||||
@@ -45,6 +50,11 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
/* needed for inet_pton and InitializeSRWLock */
|
||||
#ifdef __MINGW32__
|
||||
#define _WIN32_WINNT _WIN32_WINNT_VISTA
|
||||
#endif
|
||||
|
||||
/* Needed for realpath on linux, as well as pthread rwlocks. */
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#define _XOPEN_SOURCE 600
|
||||
@@ -61,4 +71,9 @@
|
||||
#define _NETBSD_SOURCE
|
||||
#endif
|
||||
|
||||
/* Needed for several things when building with -std=c99. */
|
||||
#if !__BSD_VISIBLE && (defined(__DragonFly__) || defined(__FreeBSD__))
|
||||
#define __BSD_VISIBLE 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -495,6 +495,8 @@ JANET_CORE_FN(cfun_fiber_new,
|
||||
"* :t - block termination signals: error + user[0-4]\n"
|
||||
"* :u - block user signals\n"
|
||||
"* :y - block yield signals\n"
|
||||
"* :w - block await signals (user9)\n"
|
||||
"* :r - block interrupt signals (user8)\n"
|
||||
"* :0-9 - block a specific user signal\n\n"
|
||||
"The sigmask argument also can take environment flags. If any mutually "
|
||||
"exclusive flags are present, the last flag takes precedence.\n\n"
|
||||
@@ -518,7 +520,7 @@ JANET_CORE_FN(cfun_fiber_new,
|
||||
} else {
|
||||
switch (view.bytes[i]) {
|
||||
default:
|
||||
janet_panicf("invalid flag %c, expected a, t, d, e, u, y, i, or p", view.bytes[i]);
|
||||
janet_panicf("invalid flag %c, expected a, t, d, e, u, y, w, r, i, or p", view.bytes[i]);
|
||||
break;
|
||||
case 'a':
|
||||
fiber->flags |=
|
||||
@@ -548,6 +550,12 @@ JANET_CORE_FN(cfun_fiber_new,
|
||||
case 'y':
|
||||
fiber->flags |= JANET_FIBER_MASK_YIELD;
|
||||
break;
|
||||
case 'w':
|
||||
fiber->flags |= JANET_FIBER_MASK_USER9;
|
||||
break;
|
||||
case 'r':
|
||||
fiber->flags |= JANET_FIBER_MASK_USER8;
|
||||
break;
|
||||
case 'i':
|
||||
if (!janet_vm.fiber->env) {
|
||||
janet_vm.fiber->env = janet_table(0);
|
||||
@@ -575,7 +583,9 @@ JANET_CORE_FN(cfun_fiber_status,
|
||||
"* :error - the fiber has errored out\n"
|
||||
"* :debug - the fiber is suspended in debug mode\n"
|
||||
"* :pending - the fiber has been yielded\n"
|
||||
"* :user(0-9) - the fiber is suspended by a user signal\n"
|
||||
"* :user(0-7) - the fiber is suspended by a user signal\n"
|
||||
"* :interrupted - the fiber was interrupted\n"
|
||||
"* :suspended - the fiber is waiting to be resumed by the scheduler\n"
|
||||
"* :alive - the fiber is currently running and cannot be resumed\n"
|
||||
"* :new - the fiber has just been created and not yet run") {
|
||||
janet_fixarity(argc, 1);
|
||||
@@ -625,11 +635,7 @@ JANET_CORE_FN(cfun_fiber_setmaxstack,
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_fiber_can_resume,
|
||||
"(fiber/can-resume? fiber)",
|
||||
"Check if a fiber is finished and cannot be resumed.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetFiber *fiber = janet_getfiber(argv, 0);
|
||||
int janet_fiber_can_resume(JanetFiber *fiber) {
|
||||
JanetFiberStatus s = janet_fiber_status(fiber);
|
||||
int isFinished = s == JANET_STATUS_DEAD ||
|
||||
s == JANET_STATUS_ERROR ||
|
||||
@@ -638,7 +644,15 @@ JANET_CORE_FN(cfun_fiber_can_resume,
|
||||
s == JANET_STATUS_USER2 ||
|
||||
s == JANET_STATUS_USER3 ||
|
||||
s == JANET_STATUS_USER4;
|
||||
return janet_wrap_boolean(!isFinished);
|
||||
return !isFinished;
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_fiber_can_resume,
|
||||
"(fiber/can-resume? fiber)",
|
||||
"Check if a fiber is finished and cannot be resumed.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetFiber *fiber = janet_getfiber(argv, 0);
|
||||
return janet_wrap_boolean(janet_fiber_can_resume(fiber));
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_fiber_last_value,
|
||||
|
||||
@@ -138,7 +138,7 @@ int64_t janet_unwrap_s64(Janet x) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
janet_panicf("bad s64 initializer: %t", x);
|
||||
janet_panicf("can not convert %t %q to 64 bit signed integer", x, x);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ uint64_t janet_unwrap_u64(Janet x) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
janet_panicf("bad u64 initializer: %t", x);
|
||||
janet_panicf("can not convert %t %q to a 64 bit unsigned integer", x, x);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -502,6 +502,18 @@ static Janet cfun_it_s64_mod(int32_t argc, Janet *argv) {
|
||||
return janet_wrap_abstract(box);
|
||||
}
|
||||
|
||||
static Janet cfun_it_s64_modi(int32_t argc, Janet *argv) {
|
||||
janet_fixarity(argc, 2);
|
||||
int64_t *box = janet_abstract(&janet_s64_type, sizeof(int64_t));
|
||||
int64_t op2 = janet_unwrap_s64(argv[0]);
|
||||
int64_t op1 = janet_unwrap_s64(argv[1]);
|
||||
int64_t x = op1 % op2;
|
||||
*box = (op1 > 0)
|
||||
? ((op2 > 0) ? x : (0 == x ? x : x + op2))
|
||||
: ((op2 > 0) ? (0 == x ? x : x + op2) : x);
|
||||
return janet_wrap_abstract(box);
|
||||
}
|
||||
|
||||
OPMETHOD(int64_t, s64, add, +)
|
||||
OPMETHOD(int64_t, s64, sub, -)
|
||||
OPMETHODINVERT(int64_t, s64, subi, -)
|
||||
@@ -509,6 +521,7 @@ OPMETHOD(int64_t, s64, mul, *)
|
||||
DIVMETHOD_SIGNED(int64_t, s64, div, /)
|
||||
DIVMETHOD_SIGNED(int64_t, s64, rem, %)
|
||||
DIVMETHODINVERT_SIGNED(int64_t, s64, divi, /)
|
||||
DIVMETHODINVERT_SIGNED(int64_t, s64, remi, %)
|
||||
OPMETHOD(int64_t, s64, and, &)
|
||||
OPMETHOD(int64_t, s64, or, |)
|
||||
OPMETHOD(int64_t, s64, xor, ^)
|
||||
@@ -521,6 +534,7 @@ OPMETHOD(uint64_t, u64, mul, *)
|
||||
DIVMETHOD(uint64_t, u64, div, /)
|
||||
DIVMETHOD(uint64_t, u64, mod, %)
|
||||
DIVMETHODINVERT(uint64_t, u64, divi, /)
|
||||
DIVMETHODINVERT(uint64_t, u64, modi, %)
|
||||
OPMETHOD(uint64_t, u64, and, &)
|
||||
OPMETHOD(uint64_t, u64, or, |)
|
||||
OPMETHOD(uint64_t, u64, xor, ^)
|
||||
@@ -542,9 +556,9 @@ static JanetMethod it_s64_methods[] = {
|
||||
{"/", cfun_it_s64_div},
|
||||
{"r/", cfun_it_s64_divi},
|
||||
{"mod", cfun_it_s64_mod},
|
||||
{"rmod", cfun_it_s64_mod},
|
||||
{"rmod", cfun_it_s64_modi},
|
||||
{"%", cfun_it_s64_rem},
|
||||
{"r%", cfun_it_s64_rem},
|
||||
{"r%", cfun_it_s64_remi},
|
||||
{"&", cfun_it_s64_and},
|
||||
{"r&", cfun_it_s64_and},
|
||||
{"|", cfun_it_s64_or},
|
||||
@@ -567,9 +581,9 @@ static JanetMethod it_u64_methods[] = {
|
||||
{"/", cfun_it_u64_div},
|
||||
{"r/", cfun_it_u64_divi},
|
||||
{"mod", cfun_it_u64_mod},
|
||||
{"rmod", cfun_it_u64_mod},
|
||||
{"rmod", cfun_it_u64_modi},
|
||||
{"%", cfun_it_u64_mod},
|
||||
{"r%", cfun_it_u64_mod},
|
||||
{"r%", cfun_it_u64_modi},
|
||||
{"&", cfun_it_u64_and},
|
||||
{"r&", cfun_it_u64_and},
|
||||
{"|", cfun_it_u64_or},
|
||||
|
||||
@@ -254,45 +254,45 @@ JANET_CORE_FN(janet_srand,
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
|
||||
#define JANET_DEFINE_NAMED_MATHOP(c_name, janet_name, fop, doc)\
|
||||
JANET_CORE_FN(janet_##c_name, "(math/" #janet_name " x)", doc) {\
|
||||
#define JANET_DEFINE_NAMED_MATHOP(janet_name, fop, doc)\
|
||||
JANET_CORE_FN(janet_##fop, "(math/" janet_name " x)", doc) {\
|
||||
janet_fixarity(argc, 1); \
|
||||
double x = janet_getnumber(argv, 0); \
|
||||
return janet_wrap_number(fop(x)); \
|
||||
}
|
||||
|
||||
#define JANET_DEFINE_MATHOP(name, fop, doc) JANET_DEFINE_NAMED_MATHOP(name, name, fop, doc)
|
||||
#define JANET_DEFINE_MATHOP(fop, doc) JANET_DEFINE_NAMED_MATHOP(#fop, fop, doc)
|
||||
|
||||
JANET_DEFINE_MATHOP(acos, acos, "Returns the arccosine of x.")
|
||||
JANET_DEFINE_MATHOP(asin, asin, "Returns the arcsin of x.")
|
||||
JANET_DEFINE_MATHOP(atan, atan, "Returns the arctangent of x.")
|
||||
JANET_DEFINE_MATHOP(cos, cos, "Returns the cosine of x.")
|
||||
JANET_DEFINE_MATHOP(cosh, cosh, "Returns the hyperbolic cosine of x.")
|
||||
JANET_DEFINE_MATHOP(acosh, acosh, "Returns the hyperbolic arccosine of x.")
|
||||
JANET_DEFINE_MATHOP(sin, sin, "Returns the sine of x.")
|
||||
JANET_DEFINE_MATHOP(sinh, sinh, "Returns the hyperbolic sine of x.")
|
||||
JANET_DEFINE_MATHOP(asinh, asinh, "Returns the hyperbolic arcsine of x.")
|
||||
JANET_DEFINE_MATHOP(tan, tan, "Returns the tangent of x.")
|
||||
JANET_DEFINE_MATHOP(tanh, tanh, "Returns the hyperbolic tangent of x.")
|
||||
JANET_DEFINE_MATHOP(atanh, atanh, "Returns the hyperbolic arctangent of x.")
|
||||
JANET_DEFINE_MATHOP(exp, exp, "Returns e to the power of x.")
|
||||
JANET_DEFINE_MATHOP(exp2, exp2, "Returns 2 to the power of x.")
|
||||
JANET_DEFINE_MATHOP(expm1, expm1, "Returns e to the power of x minus 1.")
|
||||
JANET_DEFINE_MATHOP(log, log, "Returns the natural logarithm of x.")
|
||||
JANET_DEFINE_MATHOP(log10, log10, "Returns the log base 10 of x.")
|
||||
JANET_DEFINE_MATHOP(log2, log2, "Returns the log base 2 of x.")
|
||||
JANET_DEFINE_MATHOP(sqrt, sqrt, "Returns the square root of x.")
|
||||
JANET_DEFINE_MATHOP(cbrt, cbrt, "Returns the cube root of x.")
|
||||
JANET_DEFINE_MATHOP(ceil, ceil, "Returns the smallest integer value number that is not less than x.")
|
||||
JANET_DEFINE_MATHOP(abs, fabs, "Return the absolute value of x.")
|
||||
JANET_DEFINE_MATHOP(floor, floor, "Returns the largest integer value number that is not greater than x.")
|
||||
JANET_DEFINE_MATHOP(trunc, trunc, "Returns the integer between x and 0 nearest to x.")
|
||||
JANET_DEFINE_MATHOP(round, round, "Returns the integer nearest to x.")
|
||||
JANET_DEFINE_MATHOP(gamma, tgamma, "Returns gamma(x).")
|
||||
JANET_DEFINE_NAMED_MATHOP(lgamma, "log-gamma", lgamma, "Returns log-gamma(x).")
|
||||
JANET_DEFINE_MATHOP(log1p, log1p, "Returns (log base e of x) + 1 more accurately than (+ (math/log x) 1)")
|
||||
JANET_DEFINE_MATHOP(erf, erf, "Returns the error function of x.")
|
||||
JANET_DEFINE_MATHOP(erfc, erfc, "Returns the complementary error function of x.")
|
||||
JANET_DEFINE_MATHOP(acos, "Returns the arccosine of x.")
|
||||
JANET_DEFINE_MATHOP(asin, "Returns the arcsin of x.")
|
||||
JANET_DEFINE_MATHOP(atan, "Returns the arctangent of x.")
|
||||
JANET_DEFINE_MATHOP(cos, "Returns the cosine of x.")
|
||||
JANET_DEFINE_MATHOP(cosh, "Returns the hyperbolic cosine of x.")
|
||||
JANET_DEFINE_MATHOP(acosh, "Returns the hyperbolic arccosine of x.")
|
||||
JANET_DEFINE_MATHOP(sin, "Returns the sine of x.")
|
||||
JANET_DEFINE_MATHOP(sinh, "Returns the hyperbolic sine of x.")
|
||||
JANET_DEFINE_MATHOP(asinh, "Returns the hyperbolic arcsine of x.")
|
||||
JANET_DEFINE_MATHOP(tan, "Returns the tangent of x.")
|
||||
JANET_DEFINE_MATHOP(tanh, "Returns the hyperbolic tangent of x.")
|
||||
JANET_DEFINE_MATHOP(atanh, "Returns the hyperbolic arctangent of x.")
|
||||
JANET_DEFINE_MATHOP(exp, "Returns e to the power of x.")
|
||||
JANET_DEFINE_MATHOP(exp2, "Returns 2 to the power of x.")
|
||||
JANET_DEFINE_MATHOP(expm1, "Returns e to the power of x minus 1.")
|
||||
JANET_DEFINE_MATHOP(log, "Returns the natural logarithm of x.")
|
||||
JANET_DEFINE_MATHOP(log10, "Returns the log base 10 of x.")
|
||||
JANET_DEFINE_MATHOP(log2, "Returns the log base 2 of x.")
|
||||
JANET_DEFINE_MATHOP(sqrt, "Returns the square root of x.")
|
||||
JANET_DEFINE_MATHOP(cbrt, "Returns the cube root of x.")
|
||||
JANET_DEFINE_MATHOP(ceil, "Returns the smallest integer value number that is not less than x.")
|
||||
JANET_DEFINE_MATHOP(floor, "Returns the largest integer value number that is not greater than x.")
|
||||
JANET_DEFINE_MATHOP(trunc, "Returns the integer between x and 0 nearest to x.")
|
||||
JANET_DEFINE_MATHOP(round, "Returns the integer nearest to x.")
|
||||
JANET_DEFINE_MATHOP(log1p, "Returns (log base e of x) + 1 more accurately than (+ (math/log x) 1)")
|
||||
JANET_DEFINE_MATHOP(erf, "Returns the error function of x.")
|
||||
JANET_DEFINE_MATHOP(erfc, "Returns the complementary error function of x.")
|
||||
JANET_DEFINE_NAMED_MATHOP("log-gamma", lgamma, "Returns log-gamma(x).")
|
||||
JANET_DEFINE_NAMED_MATHOP("abs", fabs, "Return the absolute value of x.")
|
||||
JANET_DEFINE_NAMED_MATHOP("gamma", tgamma, "Returns gamma(x).")
|
||||
|
||||
#define JANET_DEFINE_MATH2OP(name, fop, signature, doc)\
|
||||
JANET_CORE_FN(janet_##name, signature, doc) {\
|
||||
@@ -317,7 +317,7 @@ static double janet_gcd(double x, double y) {
|
||||
#ifdef NAN
|
||||
return NAN;
|
||||
#else
|
||||
return 0.0 \ 0.0;
|
||||
return 0.0 / 0.0;
|
||||
#endif
|
||||
}
|
||||
if (isinf(x) || isinf(y)) return INFINITY;
|
||||
@@ -370,7 +370,7 @@ void janet_lib_math(JanetTable *env) {
|
||||
JANET_CORE_REG("math/floor", janet_floor),
|
||||
JANET_CORE_REG("math/ceil", janet_ceil),
|
||||
JANET_CORE_REG("math/pow", janet_pow),
|
||||
JANET_CORE_REG("math/abs", janet_abs),
|
||||
JANET_CORE_REG("math/abs", janet_fabs),
|
||||
JANET_CORE_REG("math/sinh", janet_sinh),
|
||||
JANET_CORE_REG("math/cosh", janet_cosh),
|
||||
JANET_CORE_REG("math/tanh", janet_tanh),
|
||||
@@ -385,7 +385,7 @@ void janet_lib_math(JanetTable *env) {
|
||||
JANET_CORE_REG("math/hypot", janet_hypot),
|
||||
JANET_CORE_REG("math/exp2", janet_exp2),
|
||||
JANET_CORE_REG("math/log1p", janet_log1p),
|
||||
JANET_CORE_REG("math/gamma", janet_gamma),
|
||||
JANET_CORE_REG("math/gamma", janet_tgamma),
|
||||
JANET_CORE_REG("math/log-gamma", janet_lgamma),
|
||||
JANET_CORE_REG("math/erfc", janet_erfc),
|
||||
JANET_CORE_REG("math/erf", janet_erf),
|
||||
|
||||
127
src/core/net.c
127
src/core/net.c
@@ -460,7 +460,7 @@ JANET_CORE_FN(cfun_net_connect,
|
||||
if (binding) {
|
||||
struct addrinfo *rp = NULL;
|
||||
int did_bind = 0;
|
||||
for (rp = ai; rp != NULL; rp = rp->ai_next) {
|
||||
for (rp = binding; rp != NULL; rp = rp->ai_next) {
|
||||
if (bind(sock, rp->ai_addr, (int) rp->ai_addrlen) == 0) {
|
||||
did_bind = 1;
|
||||
break;
|
||||
@@ -477,14 +477,20 @@ JANET_CORE_FN(cfun_net_connect,
|
||||
}
|
||||
}
|
||||
|
||||
/* Wrap socket in abstract type JanetStream */
|
||||
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
||||
|
||||
/* Set up the socket for non-blocking IO before connecting */
|
||||
janet_net_socknoblock(sock);
|
||||
|
||||
/* Connect to socket */
|
||||
#ifdef JANET_WINDOWS
|
||||
int status = WSAConnect(sock, addr, addrlen, NULL, NULL, NULL, NULL);
|
||||
Janet lasterr = janet_ev_lasterr();
|
||||
int err = WSAGetLastError();
|
||||
freeaddrinfo(ai);
|
||||
#else
|
||||
int status = connect(sock, addr, addrlen);
|
||||
Janet lasterr = janet_ev_lasterr();
|
||||
int err = errno;
|
||||
if (is_unix) {
|
||||
janet_free(ai);
|
||||
} else {
|
||||
@@ -492,17 +498,22 @@ JANET_CORE_FN(cfun_net_connect,
|
||||
}
|
||||
#endif
|
||||
|
||||
if (status == -1) {
|
||||
JSOCKCLOSE(sock);
|
||||
janet_panicf("could not connect socket: %V", lasterr);
|
||||
if (status != 0) {
|
||||
#ifdef JANET_WINDOWS
|
||||
if (err != WSAEWOULDBLOCK) {
|
||||
#else
|
||||
if (err != EINPROGRESS) {
|
||||
#endif
|
||||
JSOCKCLOSE(sock);
|
||||
Janet lasterr = janet_ev_lasterr();
|
||||
janet_panicf("could not connect socket: %V", lasterr);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set up the socket for non-blocking IO after connect - TODO - non-blocking connect? */
|
||||
janet_net_socknoblock(sock);
|
||||
/* Handle the connect() result in the event loop*/
|
||||
janet_ev_connect(stream, MSG_NOSIGNAL);
|
||||
|
||||
/* Wrap socket in abstract type JanetStream */
|
||||
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
||||
return janet_wrap_abstract(stream);
|
||||
janet_await();
|
||||
}
|
||||
|
||||
static const char *serverify_socket(JSock sfd) {
|
||||
@@ -872,6 +883,98 @@ JANET_CORE_FN(cfun_stream_flush,
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
struct sockopt_type {
|
||||
const char *name;
|
||||
int level;
|
||||
int optname;
|
||||
enum JanetType type;
|
||||
};
|
||||
|
||||
/* List of supported socket options; The type JANET_POINTER is used
|
||||
* for options that require special handling depending on the type. */
|
||||
static const struct sockopt_type sockopt_type_list[] = {
|
||||
{ "so-broadcast", SOL_SOCKET, SO_BROADCAST, JANET_BOOLEAN },
|
||||
{ "so-reuseaddr", SOL_SOCKET, SO_REUSEADDR, JANET_BOOLEAN },
|
||||
{ "so-keepalive", SOL_SOCKET, SO_KEEPALIVE, JANET_BOOLEAN },
|
||||
{ "ip-multicast-ttl", IPPROTO_IP, IP_MULTICAST_TTL, JANET_NUMBER },
|
||||
{ "ip-add-membership", IPPROTO_IP, IP_ADD_MEMBERSHIP, JANET_POINTER },
|
||||
{ "ip-drop-membership", IPPROTO_IP, IP_DROP_MEMBERSHIP, JANET_POINTER },
|
||||
{ "ipv6-join-group", IPPROTO_IPV6, IPV6_JOIN_GROUP, JANET_POINTER },
|
||||
{ "ipv6-leave-group", IPPROTO_IPV6, IPV6_LEAVE_GROUP, JANET_POINTER },
|
||||
{ NULL, 0, 0, JANET_POINTER }
|
||||
};
|
||||
|
||||
JANET_CORE_FN(cfun_net_setsockopt,
|
||||
"(net/setsockopt stream option value)",
|
||||
"set socket options.\n"
|
||||
"\n"
|
||||
"supported options and associated value types:\n"
|
||||
"- :so-broadcast boolean\n"
|
||||
"- :so-reuseaddr boolean\n"
|
||||
"- :so-keepalive boolean\n"
|
||||
"- :ip-multicast-ttl number\n"
|
||||
"- :ip-add-membership string\n"
|
||||
"- :ip-drop-membership string\n"
|
||||
"- :ipv6-join-group string\n"
|
||||
"- :ipv6-leave-group string\n") {
|
||||
janet_arity(argc, 3, 3);
|
||||
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
||||
janet_stream_flags(stream, JANET_STREAM_SOCKET);
|
||||
JanetKeyword optstr = janet_getkeyword(argv, 1);
|
||||
|
||||
const struct sockopt_type *st = sockopt_type_list;
|
||||
while (st->name) {
|
||||
if (janet_cstrcmp(optstr, st->name) == 0) {
|
||||
break;
|
||||
}
|
||||
st++;
|
||||
}
|
||||
|
||||
if (st->name == NULL) {
|
||||
janet_panicf("unknown socket option %q", argv[1]);
|
||||
}
|
||||
|
||||
union {
|
||||
int v_int;
|
||||
struct ip_mreq v_mreq;
|
||||
struct ipv6_mreq v_mreq6;
|
||||
} val;
|
||||
|
||||
void *optval = (void *)&val;
|
||||
socklen_t optlen = 0;
|
||||
|
||||
if (st->type == JANET_BOOLEAN) {
|
||||
val.v_int = janet_getboolean(argv, 2);
|
||||
optlen = sizeof(val.v_int);
|
||||
} else if (st->type == JANET_NUMBER) {
|
||||
val.v_int = janet_getinteger(argv, 2);
|
||||
optlen = sizeof(val.v_int);
|
||||
} else if (st->optname == IP_ADD_MEMBERSHIP || st->optname == IP_DROP_MEMBERSHIP) {
|
||||
const char *addr = janet_getcstring(argv, 2);
|
||||
memset(&val.v_mreq, 0, sizeof val.v_mreq);
|
||||
val.v_mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
inet_pton(AF_INET, addr, &val.v_mreq.imr_multiaddr.s_addr);
|
||||
optlen = sizeof(val.v_mreq);
|
||||
} else if (st->optname == IPV6_JOIN_GROUP || st->optname == IPV6_LEAVE_GROUP) {
|
||||
const char *addr = janet_getcstring(argv, 2);
|
||||
memset(&val.v_mreq6, 0, sizeof val.v_mreq6);
|
||||
val.v_mreq6.ipv6mr_interface = 0;
|
||||
inet_pton(AF_INET6, addr, &val.v_mreq6.ipv6mr_multiaddr);
|
||||
optlen = sizeof(val.v_mreq6);
|
||||
} else {
|
||||
janet_panicf("invalid socket option type");
|
||||
}
|
||||
|
||||
janet_assert(optlen != 0, "invalid socket option value");
|
||||
|
||||
int r = setsockopt((JSock) stream->handle, st->level, st->optname, optval, optlen);
|
||||
if (r == -1) {
|
||||
janet_panicf("setsockopt(%q): %s", argv[1], strerror(errno));
|
||||
}
|
||||
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
|
||||
static const JanetMethod net_stream_methods[] = {
|
||||
{"chunk", cfun_stream_chunk},
|
||||
{"close", janet_cfun_stream_close},
|
||||
@@ -886,6 +989,7 @@ static const JanetMethod net_stream_methods[] = {
|
||||
{"evchunk", janet_cfun_stream_chunk},
|
||||
{"evwrite", janet_cfun_stream_write},
|
||||
{"shutdown", cfun_net_shutdown},
|
||||
{"setsockopt", cfun_net_setsockopt},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -910,6 +1014,7 @@ void janet_lib_net(JanetTable *env) {
|
||||
JANET_CORE_REG("net/peername", cfun_net_getpeername),
|
||||
JANET_CORE_REG("net/localname", cfun_net_getsockname),
|
||||
JANET_CORE_REG("net/address-unpack", cfun_net_address_unpack),
|
||||
JANET_CORE_REG("net/setsockopt", cfun_net_setsockopt),
|
||||
JANET_REG_END
|
||||
};
|
||||
janet_core_cfuns_ext(env, NULL, net_cfuns);
|
||||
|
||||
244
src/core/os.c
244
src/core/os.c
@@ -126,6 +126,8 @@ JANET_CORE_FN(os_which,
|
||||
"* :freebsd\n\n"
|
||||
"* :openbsd\n\n"
|
||||
"* :netbsd\n\n"
|
||||
"* :dragonfly\n\n"
|
||||
"* :bsd\n\n"
|
||||
"* :posix - A POSIX compatible system (default)\n\n"
|
||||
"May also return a custom keyword specified at build time.") {
|
||||
janet_fixarity(argc, 0);
|
||||
@@ -150,6 +152,8 @@ JANET_CORE_FN(os_which,
|
||||
return janet_ckeywordv("netbsd");
|
||||
#elif defined(__OpenBSD__)
|
||||
return janet_ckeywordv("openbsd");
|
||||
#elif defined(__DragonFly__)
|
||||
return janet_ckeywordv("dragonfly");
|
||||
#elif defined(JANET_BSD)
|
||||
return janet_ckeywordv("bsd");
|
||||
#else
|
||||
@@ -620,12 +624,99 @@ JANET_CORE_FN(os_proc_wait,
|
||||
#endif
|
||||
}
|
||||
|
||||
struct keyword_signal {
|
||||
const char *keyword;
|
||||
int signal;
|
||||
};
|
||||
|
||||
#ifndef JANET_WINDOWS
|
||||
static const struct keyword_signal signal_keywords[] = {
|
||||
#ifdef SIGKILL
|
||||
{"kill", SIGKILL},
|
||||
#endif
|
||||
{"int", SIGINT},
|
||||
{"abrt", SIGABRT},
|
||||
{"fpe", SIGFPE},
|
||||
{"ill", SIGILL},
|
||||
{"segv", SIGSEGV},
|
||||
#ifdef SIGTERM
|
||||
{"term", SIGTERM},
|
||||
#endif
|
||||
#ifdef SIGARLM
|
||||
{"alrm", SIGALRM},
|
||||
#endif
|
||||
#ifdef SIGHUP
|
||||
{"hup", SIGHUP},
|
||||
#endif
|
||||
#ifdef SIGPIPE
|
||||
{"pipe", SIGPIPE},
|
||||
#endif
|
||||
#ifdef SIGQUIT
|
||||
{"quit", SIGQUIT},
|
||||
#endif
|
||||
#ifdef SIGUSR1
|
||||
{"usr1", SIGUSR1},
|
||||
#endif
|
||||
#ifdef SIGUSR2
|
||||
{"usr2", SIGUSR2},
|
||||
#endif
|
||||
#ifdef SIGCHLD
|
||||
{"chld", SIGCHLD},
|
||||
#endif
|
||||
#ifdef SIGCONT
|
||||
{"cont", SIGCONT},
|
||||
#endif
|
||||
#ifdef SIGSTOP
|
||||
{"stop", SIGSTOP},
|
||||
#endif
|
||||
#ifdef SIGTSTP
|
||||
{"tstp", SIGTSTP},
|
||||
#endif
|
||||
#ifdef SIGTTIN
|
||||
{"ttin", SIGTTIN},
|
||||
#endif
|
||||
#ifdef SIGTTOU
|
||||
{"ttou", SIGTTOU},
|
||||
#endif
|
||||
#ifdef SIGBUS
|
||||
{"bus", SIGBUS},
|
||||
#endif
|
||||
#ifdef SIGPOLL
|
||||
{"poll", SIGPOLL},
|
||||
#endif
|
||||
#ifdef SIGPROF
|
||||
{"prof", SIGPROF},
|
||||
#endif
|
||||
#ifdef SIGSYS
|
||||
{"sys", SIGSYS},
|
||||
#endif
|
||||
#ifdef SIGTRAP
|
||||
{"trap", SIGTRAP},
|
||||
#endif
|
||||
#ifdef SIGURG
|
||||
{"urg", SIGURG},
|
||||
#endif
|
||||
#ifdef SIGVTALRM
|
||||
{"vtlarm", SIGVTALRM},
|
||||
#endif
|
||||
#ifdef SIGXCPU
|
||||
{"xcpu", SIGXCPU},
|
||||
#endif
|
||||
#ifdef SIGXFSZ
|
||||
{"xfsz", SIGXFSZ},
|
||||
#endif
|
||||
{NULL, 0},
|
||||
};
|
||||
#endif
|
||||
|
||||
JANET_CORE_FN(os_proc_kill,
|
||||
"(os/proc-kill proc &opt wait)",
|
||||
"(os/proc-kill proc &opt wait signal)",
|
||||
"Kill a subprocess by sending SIGKILL to it on posix systems, or by closing the process "
|
||||
"handle on windows. If `wait` is truthy, will wait for the process to finish and "
|
||||
"returns the exit code. Otherwise, returns `proc`.") {
|
||||
janet_arity(argc, 1, 2);
|
||||
"returns the exit code. Otherwise, returns `proc`. If signal is specified send it instead."
|
||||
"Signal keywords are named after their C counterparts but in lowercase with the leading "
|
||||
"`SIG` stripped. Signals are ignored on windows.") {
|
||||
janet_arity(argc, 1, 3);
|
||||
JanetProc *proc = janet_getabstract(argv, 0, &ProcAT);
|
||||
if (proc->flags & JANET_PROC_WAITED) {
|
||||
janet_panicf("cannot kill process that has already finished");
|
||||
@@ -639,7 +730,22 @@ JANET_CORE_FN(os_proc_kill,
|
||||
CloseHandle(proc->pHandle);
|
||||
CloseHandle(proc->tHandle);
|
||||
#else
|
||||
int status = kill(proc->pid, SIGKILL);
|
||||
int signal = -1;
|
||||
if (argc == 3) {
|
||||
JanetKeyword signal_kw = janet_getkeyword(argv, 2);
|
||||
const struct keyword_signal *ptr = signal_keywords;
|
||||
while (ptr->keyword) {
|
||||
if (!janet_cstrcmp(signal_kw, ptr->keyword)) {
|
||||
signal = ptr->signal;
|
||||
break;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
if (signal == -1) {
|
||||
janet_panic("undefined signal");
|
||||
}
|
||||
}
|
||||
int status = kill(proc->pid, signal == -1 ? SIGKILL : signal);
|
||||
if (status) {
|
||||
janet_panic(strerror(errno));
|
||||
}
|
||||
@@ -1039,21 +1145,21 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, int is_spawn) {
|
||||
if (pipe_in != JANET_HANDLE_NONE) {
|
||||
posix_spawn_file_actions_adddup2(&actions, pipe_in, 0);
|
||||
posix_spawn_file_actions_addclose(&actions, pipe_in);
|
||||
} else if (new_in != JANET_HANDLE_NONE) {
|
||||
} else if (new_in != JANET_HANDLE_NONE && new_in != 0) {
|
||||
posix_spawn_file_actions_adddup2(&actions, new_in, 0);
|
||||
posix_spawn_file_actions_addclose(&actions, new_in);
|
||||
}
|
||||
if (pipe_out != JANET_HANDLE_NONE) {
|
||||
posix_spawn_file_actions_adddup2(&actions, pipe_out, 1);
|
||||
posix_spawn_file_actions_addclose(&actions, pipe_out);
|
||||
} else if (new_out != JANET_HANDLE_NONE) {
|
||||
} else if (new_out != JANET_HANDLE_NONE && new_out != 1) {
|
||||
posix_spawn_file_actions_adddup2(&actions, new_out, 1);
|
||||
posix_spawn_file_actions_addclose(&actions, new_out);
|
||||
}
|
||||
if (pipe_err != JANET_HANDLE_NONE) {
|
||||
posix_spawn_file_actions_adddup2(&actions, pipe_err, 2);
|
||||
posix_spawn_file_actions_addclose(&actions, pipe_err);
|
||||
} else if (new_err != JANET_HANDLE_NONE) {
|
||||
} else if (new_err != JANET_HANDLE_NONE && new_err != 2) {
|
||||
posix_spawn_file_actions_adddup2(&actions, new_err, 2);
|
||||
posix_spawn_file_actions_addclose(&actions, new_err);
|
||||
}
|
||||
@@ -1081,7 +1187,8 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, int is_spawn) {
|
||||
|
||||
os_execute_cleanup(envp, child_argv);
|
||||
if (status) {
|
||||
janet_panicf("%p: %s", argv[0], strerror(errno));
|
||||
/* correct for macos bug where errno is not set */
|
||||
janet_panicf("%p: %s", argv[0], strerror(errno ? errno : ENOENT));
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1273,14 +1380,32 @@ JANET_CORE_FN(os_time,
|
||||
}
|
||||
|
||||
JANET_CORE_FN(os_clock,
|
||||
"(os/clock)",
|
||||
"Return the number of whole + fractional seconds since some fixed point in time. The clock "
|
||||
"is guaranteed to be non-decreasing in real time.") {
|
||||
"(os/clock &opt source)",
|
||||
"Return the number of whole + fractional seconds of the requested clock source.\n\n"
|
||||
"The `source` argument selects the clock source to use, when not specified the default "
|
||||
"is `:realtime`:\n"
|
||||
"- :realtime: Return the real (i.e., wall-clock) time. This clock is affected by discontinuous "
|
||||
" jumps in the system time\n"
|
||||
"- :monotonic: Return the number of whole + fractional seconds since some fixed point in "
|
||||
" time. The clock is guaranteed to be non-decreasing in real time.\n"
|
||||
"- :cputime: Return the CPU time consumed by this process (i.e. all threads in the process)\n") {
|
||||
janet_sandbox_assert(JANET_SANDBOX_HRTIME);
|
||||
janet_fixarity(argc, 0);
|
||||
(void) argv;
|
||||
janet_arity(argc, 0, 1);
|
||||
enum JanetTimeSource source = JANET_TIME_REALTIME;
|
||||
if (argc == 1) {
|
||||
JanetKeyword sourcestr = janet_getkeyword(argv, 0);
|
||||
if (janet_cstrcmp(sourcestr, "realtime") == 0) {
|
||||
source = JANET_TIME_REALTIME;
|
||||
} else if (janet_cstrcmp(sourcestr, "monotonic") == 0) {
|
||||
source = JANET_TIME_MONOTONIC;
|
||||
} else if (janet_cstrcmp(sourcestr, "cputime") == 0) {
|
||||
source = JANET_TIME_CPUTIME;
|
||||
} else {
|
||||
janet_panicf("expected :realtime, :monotonic, or :cputime, got %v", argv[0]);
|
||||
}
|
||||
}
|
||||
struct timespec tv;
|
||||
if (janet_gettime(&tv)) janet_panic("could not get time");
|
||||
if (janet_gettime(&tv, source)) janet_panic("could not get time");
|
||||
double dtime = tv.tv_sec + (tv.tv_nsec / 1E9);
|
||||
return janet_wrap_number(dtime);
|
||||
}
|
||||
@@ -1346,6 +1471,40 @@ JANET_CORE_FN(os_cryptorand,
|
||||
return janet_wrap_buffer(buffer);
|
||||
}
|
||||
|
||||
/* Helper function to get given or current time as local or UTC struct tm.
|
||||
* - arg n+0: optional time_t to be converted, uses current time if not given
|
||||
* - arg n+1: optional truthy to indicate the convnersion uses local time */
|
||||
static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct tm *t_infos) {
|
||||
time_t t;
|
||||
if (argc > n && !janet_checktype(argv[n], JANET_NIL)) {
|
||||
int64_t integer = janet_getinteger64(argv, n);
|
||||
t = (time_t) integer;
|
||||
} else {
|
||||
time(&t);
|
||||
}
|
||||
struct tm *t_info = NULL;
|
||||
if (argc > n + 1 && janet_truthy(argv[n + 1])) {
|
||||
/* local time */
|
||||
#ifdef JANET_WINDOWS
|
||||
_tzset();
|
||||
localtime_s(t_infos, &t);
|
||||
t_info = t_infos;
|
||||
#else
|
||||
tzset();
|
||||
t_info = localtime_r(&t, t_infos);
|
||||
#endif
|
||||
} else {
|
||||
/* utc time */
|
||||
#ifdef JANET_WINDOWS
|
||||
gmtime_s(t_infos, &t);
|
||||
t_info = t_infos;
|
||||
#else
|
||||
t_info = gmtime_r(&t, t_infos);
|
||||
#endif
|
||||
}
|
||||
return t_info;
|
||||
}
|
||||
|
||||
JANET_CORE_FN(os_date,
|
||||
"(os/date &opt time local)",
|
||||
"Returns the given time as a date struct, or the current time if `time` is not given. "
|
||||
@@ -1363,34 +1522,8 @@ JANET_CORE_FN(os_date,
|
||||
"* :dst - if Day Light Savings is in effect") {
|
||||
janet_arity(argc, 0, 2);
|
||||
(void) argv;
|
||||
time_t t;
|
||||
struct tm t_infos;
|
||||
struct tm *t_info = NULL;
|
||||
if (argc && !janet_checktype(argv[0], JANET_NIL)) {
|
||||
int64_t integer = janet_getinteger64(argv, 0);
|
||||
t = (time_t) integer;
|
||||
} else {
|
||||
time(&t);
|
||||
}
|
||||
if (argc >= 2 && janet_truthy(argv[1])) {
|
||||
/* local time */
|
||||
#ifdef JANET_WINDOWS
|
||||
_tzset();
|
||||
localtime_s(&t_infos, &t);
|
||||
t_info = &t_infos;
|
||||
#else
|
||||
tzset();
|
||||
t_info = localtime_r(&t, &t_infos);
|
||||
#endif
|
||||
} else {
|
||||
/* utc time */
|
||||
#ifdef JANET_WINDOWS
|
||||
gmtime_s(&t_infos, &t);
|
||||
t_info = &t_infos;
|
||||
#else
|
||||
t_info = gmtime_r(&t, &t_infos);
|
||||
#endif
|
||||
}
|
||||
struct tm *t_info = time_to_tm(argv, argc, 0, &t_infos);
|
||||
JanetKV *st = janet_struct_begin(9);
|
||||
janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t_info->tm_sec));
|
||||
janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t_info->tm_min));
|
||||
@@ -1404,6 +1537,34 @@ JANET_CORE_FN(os_date,
|
||||
return janet_wrap_struct(janet_struct_end(st));
|
||||
}
|
||||
|
||||
#define SIZETIMEFMT 250
|
||||
|
||||
JANET_CORE_FN(os_strftime,
|
||||
"(os/strftime fmt &opt time local)",
|
||||
"Format the given time as a string, or the current time if `time` is not given. "
|
||||
"The time is formatted according to the same rules as the ISO C89 function strftime(). "
|
||||
"The time is formatted in UTC unless `local` is truthy, in which case the date is formatted for "
|
||||
"the local timezone.") {
|
||||
janet_arity(argc, 1, 3);
|
||||
const char *fmt = janet_getcstring(argv, 0);
|
||||
/* ANSI X3.159-1989, section 4.12.3.5 "The strftime function" */
|
||||
static const char *valid = "aAbBcdHIjmMpSUwWxXyYZ%";
|
||||
const char *p = fmt;
|
||||
while (*p) {
|
||||
if (*p++ == '%') {
|
||||
if (!strchr(valid, *p)) {
|
||||
janet_panicf("invalid conversion specifier '%%%c'", *p);
|
||||
}
|
||||
p++;
|
||||
}
|
||||
}
|
||||
struct tm t_infos;
|
||||
struct tm *t_info = time_to_tm(argv, argc, 1, &t_infos);
|
||||
char buf[SIZETIMEFMT];
|
||||
(void)strftime(buf, SIZETIMEFMT, fmt, t_info);
|
||||
return janet_cstringv(buf);
|
||||
}
|
||||
|
||||
static int entry_getdst(Janet env_entry) {
|
||||
Janet v;
|
||||
if (janet_checktype(env_entry, JANET_TABLE)) {
|
||||
@@ -2306,6 +2467,7 @@ void janet_lib_os(JanetTable *env) {
|
||||
JANET_CORE_REG("os/mktime", os_mktime),
|
||||
JANET_CORE_REG("os/time", os_time), /* not high resolution */
|
||||
JANET_CORE_REG("os/date", os_date), /* not high resolution */
|
||||
JANET_CORE_REG("os/strftime", os_strftime),
|
||||
JANET_CORE_REG("os/sleep", os_sleep),
|
||||
|
||||
/* env functions */
|
||||
|
||||
@@ -1194,7 +1194,8 @@ static Janet parser_state_delimiters(const JanetParser *_p) {
|
||||
}
|
||||
}
|
||||
}
|
||||
str = janet_string(p->buf + oldcount, (int32_t)(p->bufcount - oldcount));
|
||||
/* avoid ptr arithmetic on NULL */
|
||||
str = janet_string(oldcount ? p->buf + oldcount : p->buf, (int32_t)(p->bufcount - oldcount));
|
||||
p->bufcount = oldcount;
|
||||
return janet_wrap_string(str);
|
||||
}
|
||||
@@ -1205,10 +1206,11 @@ static Janet parser_state_frames(const JanetParser *p) {
|
||||
states->count = count;
|
||||
uint8_t *buf = p->buf;
|
||||
/* Iterate arg stack backwards */
|
||||
Janet *args = p->args + p->argcount;
|
||||
Janet *args = p->argcount ? p->args + p->argcount : p->args; /* avoid ptr arithmetic on NULL */
|
||||
for (int32_t i = count - 1; i >= 0; --i) {
|
||||
JanetParseState *s = p->states + i;
|
||||
if (s->flags & PFLAG_CONTAINER) {
|
||||
/* avoid ptr arithmetic on args if NULL */
|
||||
if ((s->flags & PFLAG_CONTAINER) && s->argn) {
|
||||
args -= s->argn;
|
||||
}
|
||||
states->data[i] = janet_wrap_parse_state(s, args, buf, (uint32_t) p->bufcount);
|
||||
|
||||
@@ -1100,7 +1100,7 @@ static void spec_matchtime(Builder *b, int32_t argc, const Janet *argv) {
|
||||
Janet fun = argv[1];
|
||||
if (!janet_checktype(fun, JANET_FUNCTION) &&
|
||||
!janet_checktype(fun, JANET_CFUNCTION)) {
|
||||
peg_panicf(b, "expected function|cfunction, got %v", fun);
|
||||
peg_panicf(b, "expected function or cfunction, got %v", fun);
|
||||
}
|
||||
uint32_t tag = (argc == 3) ? emit_tag(b, argv[2]) : 0;
|
||||
uint32_t cindex = emit_constant(b, fun);
|
||||
@@ -1637,7 +1637,7 @@ typedef struct {
|
||||
JanetPeg *peg;
|
||||
PegState s;
|
||||
JanetByteView bytes;
|
||||
JanetByteView repl;
|
||||
Janet subst;
|
||||
int32_t start;
|
||||
} PegCall;
|
||||
|
||||
@@ -1653,7 +1653,7 @@ static PegCall peg_cfun_init(int32_t argc, Janet *argv, int get_replace) {
|
||||
ret.peg = compile_peg(argv[0]);
|
||||
}
|
||||
if (get_replace) {
|
||||
ret.repl = janet_getbytes(argv, 1);
|
||||
ret.subst = argv[1];
|
||||
ret.bytes = janet_getbytes(argv, 2);
|
||||
} else {
|
||||
ret.bytes = janet_getbytes(argv, 1);
|
||||
@@ -1738,7 +1738,8 @@ static Janet cfun_peg_replace_generic(int32_t argc, Janet *argv, int only_one) {
|
||||
trail = i;
|
||||
}
|
||||
int32_t nexti = (int32_t)(result - c.bytes.bytes);
|
||||
janet_buffer_push_bytes(ret, c.repl.bytes, c.repl.len);
|
||||
JanetByteView subst = janet_text_substitution(&c.subst, c.bytes.bytes + i, nexti - i, c.s.captures);
|
||||
janet_buffer_push_bytes(ret, subst.bytes, subst.len);
|
||||
trail = nexti;
|
||||
if (nexti == i) nexti++;
|
||||
i = nexti;
|
||||
@@ -1754,14 +1755,20 @@ static Janet cfun_peg_replace_generic(int32_t argc, Janet *argv, int only_one) {
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_peg_replace_all,
|
||||
"(peg/replace-all peg repl text &opt start & args)",
|
||||
"Replace all matches of peg in text with repl, returning a new buffer. The peg does not need to make captures to do replacement.") {
|
||||
"(peg/replace-all peg subst text &opt start & args)",
|
||||
"Replace all matches of `peg` in `text` with `subst`, returning a new buffer. "
|
||||
"The peg does not need to make captures to do replacement. "
|
||||
"If `subst` is a function, it will be called with the "
|
||||
"matching text followed by any captures.") {
|
||||
return cfun_peg_replace_generic(argc, argv, 0);
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_peg_replace,
|
||||
"(peg/replace peg repl text &opt start & args)",
|
||||
"Replace first match of peg in text with repl, returning a new buffer. The peg does not need to make captures to do replacement. "
|
||||
"Replace first match of `peg` in `text` with `subst`, returning a new buffer. "
|
||||
"The peg does not need to make captures to do replacement. "
|
||||
"If `subst` is a function, it will be called with the "
|
||||
"matching text followed by any captures. "
|
||||
"If no matches are found, returns the input string in a new buffer.") {
|
||||
return cfun_peg_replace_generic(argc, argv, 1);
|
||||
}
|
||||
|
||||
@@ -637,7 +637,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
|
||||
}
|
||||
}
|
||||
|
||||
janet_sorted_keys(kvs, cap, S->keysort_buffer + ks_start);
|
||||
janet_sorted_keys(kvs, cap, S->keysort_buffer == NULL ? NULL : S->keysort_buffer + ks_start);
|
||||
S->keysort_start += len;
|
||||
if (!(S->flags & JANET_PRETTY_NOTRUNC) && (len > JANET_PRETTY_DICT_LIMIT)) {
|
||||
len = JANET_PRETTY_DICT_LIMIT;
|
||||
@@ -736,7 +736,7 @@ static void pushtypes(JanetBuffer *buffer, int types) {
|
||||
if (first) {
|
||||
first = 0;
|
||||
} else {
|
||||
janet_buffer_push_u8(buffer, '|');
|
||||
janet_buffer_push_cstring(buffer, (types == 1) ? " or " : ", ");
|
||||
}
|
||||
janet_buffer_push_cstring(buffer, janet_type_names[i]);
|
||||
}
|
||||
@@ -809,7 +809,8 @@ static const char *scanformat(
|
||||
*(form++) = '%';
|
||||
const char *p2 = strfrmt;
|
||||
while (p2 <= p) {
|
||||
if (strchr(FMT_REPLACE_INTTYPES, *p2) != NULL) {
|
||||
char *loc = strchr(FMT_REPLACE_INTTYPES, *p2);
|
||||
if (loc != NULL && *loc != '\0') {
|
||||
const char *mapping = get_fmt_mapping(*p2++);
|
||||
size_t len = strlen(mapping);
|
||||
strcpy(form, mapping);
|
||||
@@ -845,7 +846,7 @@ void janet_formatbv(JanetBuffer *b, const char *format, va_list args) {
|
||||
}
|
||||
case 'd':
|
||||
case 'i': {
|
||||
int64_t n = va_arg(args, long);
|
||||
int64_t n = va_arg(args, int);
|
||||
nb = snprintf(item, MAX_ITEM, form, n);
|
||||
break;
|
||||
}
|
||||
@@ -853,7 +854,7 @@ void janet_formatbv(JanetBuffer *b, const char *format, va_list args) {
|
||||
case 'X':
|
||||
case 'o':
|
||||
case 'u': {
|
||||
uint64_t n = va_arg(args, unsigned long);
|
||||
uint64_t n = va_arg(args, unsigned int);
|
||||
nb = snprintf(item, MAX_ITEM, form, n);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#include "util.h"
|
||||
#endif
|
||||
|
||||
/* The JanetRegisterAllocator is really just a bitset. */
|
||||
|
||||
void janetc_regalloc_init(JanetcRegisterAllocator *ra) {
|
||||
ra->chunks = NULL;
|
||||
ra->count = 0;
|
||||
@@ -139,6 +141,14 @@ void janetc_regalloc_free(JanetcRegisterAllocator *ra, int32_t reg) {
|
||||
ra->chunks[chunk] &= ~ithbit(bit);
|
||||
}
|
||||
|
||||
/* Check if a register is set. */
|
||||
int janetc_regalloc_check(JanetcRegisterAllocator *ra, int32_t reg) {
|
||||
int32_t chunk = reg >> 5;
|
||||
int32_t bit = reg & 0x1F;
|
||||
while (chunk >= ra->count) pushchunk(ra);
|
||||
return !!(ra->chunks[chunk] & ithbit(bit));
|
||||
}
|
||||
|
||||
/* Get a register that will fit in 8 bits (< 256). Do not call this
|
||||
* twice with the same value of nth without calling janetc_regalloc_free
|
||||
* on the returned register before. */
|
||||
|
||||
@@ -56,5 +56,6 @@ int32_t janetc_regalloc_temp(JanetcRegisterAllocator *ra, JanetcRegisterTemp nth
|
||||
void janetc_regalloc_freetemp(JanetcRegisterAllocator *ra, int32_t reg, JanetcRegisterTemp nth);
|
||||
void janetc_regalloc_clone(JanetcRegisterAllocator *dest, JanetcRegisterAllocator *src);
|
||||
void janetc_regalloc_touch(JanetcRegisterAllocator *ra, int32_t reg);
|
||||
int janetc_regalloc_check(JanetcRegisterAllocator *ra, int32_t reg);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -39,6 +39,10 @@ static JanetSlot janetc_quote(JanetFopts opts, int32_t argn, const Janet *argv)
|
||||
|
||||
static JanetSlot janetc_splice(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
JanetSlot ret;
|
||||
if (!(opts.flags & JANET_FOPTS_ACCEPT_SPLICE)) {
|
||||
janetc_cerror(opts.compiler, "splice can only be used in function parameters and data constructors, it has no effect here");
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
}
|
||||
if (argn != 1) {
|
||||
janetc_cerror(opts.compiler, "expected 1 argument to splice");
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
@@ -75,7 +79,9 @@ static JanetSlot quasiquote(JanetFopts opts, Janet x, int depth, int level) {
|
||||
const uint8_t *head = janet_unwrap_symbol(tup[0]);
|
||||
if (!janet_cstrcmp(head, "unquote")) {
|
||||
if (level == 0) {
|
||||
return janetc_value(janetc_fopts_default(opts.compiler), tup[1]);
|
||||
JanetFopts subopts = janetc_fopts_default(opts.compiler);
|
||||
subopts.flags |= JANET_FOPTS_ACCEPT_SPLICE;
|
||||
return janetc_value(subopts, tup[1]);
|
||||
} else {
|
||||
level--;
|
||||
}
|
||||
@@ -203,8 +209,9 @@ static int destructure(JanetCompiler *c,
|
||||
janetc_emit(c, JOP_JUMP);
|
||||
int32_t label_loop_exit = janet_v_count(c->buffer);
|
||||
|
||||
c->buffer[label_loop_cond_jump] |= (label_loop_exit - label_loop_cond_jump) << 16;
|
||||
c->buffer[label_loop_loop] |= (label_loop_start - label_loop_loop) << 8;
|
||||
/* avoid shifting negative numbers */
|
||||
c->buffer[label_loop_cond_jump] |= (uint32_t)(label_loop_exit - label_loop_cond_jump) << 16;
|
||||
c->buffer[label_loop_loop] |= (uint32_t)(label_loop_start - label_loop_loop) << 8;
|
||||
|
||||
janetc_freeslot(c, argi);
|
||||
janetc_freeslot(c, arg);
|
||||
@@ -257,7 +264,7 @@ static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
|
||||
|
||||
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
if (argn != 2) {
|
||||
janetc_cerror(opts.compiler, "expected 2 arguments");
|
||||
janetc_cerror(opts.compiler, "expected 2 arguments to set");
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
}
|
||||
JanetFopts subopts = janetc_fopts_default(opts.compiler);
|
||||
@@ -328,11 +335,11 @@ static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv)
|
||||
return tab;
|
||||
}
|
||||
|
||||
static JanetSlot dohead(JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) {
|
||||
static JanetSlot dohead(const char *kind, JanetCompiler *c, JanetFopts opts, Janet *head, int32_t argn, const Janet *argv) {
|
||||
JanetFopts subopts = janetc_fopts_default(c);
|
||||
JanetSlot ret;
|
||||
if (argn < 2) {
|
||||
janetc_cerror(c, "expected at least 2 arguments");
|
||||
janetc_error(c, janet_formatc("expected at least 2 arguments to %s", kind));
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
}
|
||||
*head = argv[0];
|
||||
@@ -347,7 +354,17 @@ static int namelocal(JanetCompiler *c, const uint8_t *head, int32_t flags, Janet
|
||||
int isUnnamedRegister = !(ret.flags & JANET_SLOT_NAMED) &&
|
||||
ret.index > 0 &&
|
||||
ret.envindex >= 0;
|
||||
if (!isUnnamedRegister) {
|
||||
/* optimization for `(def x my-def)` - don't emit a movn/movf instruction, we can just alias my-def */
|
||||
/* TODO - implement optimization for `(def x my-var)` correctly as well w/ de-aliasing */
|
||||
int canAlias = !(flags & JANET_SLOT_MUTABLE) &&
|
||||
!(ret.flags & JANET_SLOT_MUTABLE) &&
|
||||
(ret.flags & JANET_SLOT_NAMED) &&
|
||||
(ret.index >= 0) &&
|
||||
(ret.envindex == -1);
|
||||
if (canAlias) {
|
||||
ret.flags &= ~JANET_SLOT_MUTABLE;
|
||||
isUnnamedRegister = 1; /* don't free slot after use - is an alias for another slot */
|
||||
} else if (!isUnnamedRegister) {
|
||||
/* Slot is not able to be named */
|
||||
JanetSlot localslot = janetc_farslot(c);
|
||||
janetc_copy(c, localslot, ret);
|
||||
@@ -397,7 +414,7 @@ static JanetSlot janetc_var(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
JanetCompiler *c = opts.compiler;
|
||||
Janet head;
|
||||
JanetTable *attr_table = handleattr(c, argn, argv);
|
||||
JanetSlot ret = dohead(c, opts, &head, argn, argv);
|
||||
JanetSlot ret = dohead("var", c, opts, &head, argn, argv);
|
||||
if (c->result.status == JANET_COMPILE_ERROR)
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
destructure(c, argv[0], ret, varleaf, attr_table);
|
||||
@@ -447,7 +464,7 @@ static JanetSlot janetc_def(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
Janet head;
|
||||
opts.flags &= ~JANET_FOPTS_HINT;
|
||||
JanetTable *attr_table = handleattr(c, argn, argv);
|
||||
JanetSlot ret = dohead(c, opts, &head, argn, argv);
|
||||
JanetSlot ret = dohead("def", c, opts, &head, argn, argv);
|
||||
if (c->result.status == JANET_COMPILE_ERROR)
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
destructure(c, argv[0], ret, defleaf, attr_table);
|
||||
@@ -487,6 +504,7 @@ static JanetSlot janetc_if(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
/* Get options */
|
||||
condopts = janetc_fopts_default(c);
|
||||
bodyopts = opts;
|
||||
bodyopts.flags &= ~JANET_FOPTS_ACCEPT_SPLICE;
|
||||
|
||||
/* Set target for compilation */
|
||||
target = (drop || tail)
|
||||
@@ -563,6 +581,7 @@ static JanetSlot janetc_do(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
subopts.flags = JANET_FOPTS_DROP;
|
||||
} else {
|
||||
subopts = opts;
|
||||
subopts.flags &= ~JANET_FOPTS_ACCEPT_SPLICE;
|
||||
}
|
||||
ret = janetc_value(subopts, argv[i]);
|
||||
if (i != argn - 1) {
|
||||
@@ -586,6 +605,7 @@ static JanetSlot janetc_upscope(JanetFopts opts, int32_t argn, const Janet *argv
|
||||
subopts.flags = JANET_FOPTS_DROP;
|
||||
} else {
|
||||
subopts = opts;
|
||||
subopts.flags &= ~JANET_FOPTS_ACCEPT_SPLICE;
|
||||
}
|
||||
ret = janetc_value(subopts, argv[i]);
|
||||
if (i != argn - 1) {
|
||||
@@ -697,8 +717,8 @@ static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv)
|
||||
uint8_t ifjmp = JOP_JUMP_IF;
|
||||
uint8_t ifnjmp = JOP_JUMP_IF_NOT;
|
||||
|
||||
if (argn < 2) {
|
||||
janetc_cerror(c, "expected at least 2 arguments");
|
||||
if (argn < 1) {
|
||||
janetc_cerror(c, "expected at least 1 argument to while");
|
||||
return janetc_cslot(janet_wrap_nil());
|
||||
}
|
||||
|
||||
@@ -958,12 +978,26 @@ static JanetSlot janetc_fn(JanetFopts opts, int32_t argn, const Janet *argv) {
|
||||
max_arity = (vararg || allow_extra) ? INT32_MAX : arity;
|
||||
if (!seenopt) min_arity = arity;
|
||||
|
||||
/* Check for self ref */
|
||||
/* Check for self ref (also avoid if arguments shadow own name) */
|
||||
if (selfref) {
|
||||
JanetSlot slot = janetc_farslot(c);
|
||||
slot.flags = JANET_SLOT_NAMED | JANET_FUNCTION;
|
||||
janetc_emit_s(c, JOP_LOAD_SELF, slot, 1);
|
||||
janetc_nameslot(c, janet_unwrap_symbol(head), slot);
|
||||
/* Check if the parameters shadow the function name. If so, don't
|
||||
* emit JOP_LOAD_SELF and add a binding since that most users
|
||||
* seem to expect that function parameters take precedence over the
|
||||
* function name */
|
||||
const uint8_t *sym = janet_unwrap_symbol(head);
|
||||
int32_t len = janet_v_count(c->scope->syms);
|
||||
int found = 0;
|
||||
for (int32_t i = 0; i < len; i++) {
|
||||
if (c->scope->syms[i].sym == sym) {
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
JanetSlot slot = janetc_farslot(c);
|
||||
slot.flags = JANET_SLOT_NAMED | JANET_FUNCTION;
|
||||
janetc_emit_s(c, JOP_LOAD_SELF, slot, 1);
|
||||
janetc_nameslot(c, sym, slot);
|
||||
}
|
||||
}
|
||||
|
||||
/* Compile function body */
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#ifndef JANET_STATE_H_defined
|
||||
#define JANET_STATE_H_defined
|
||||
|
||||
#include <janet.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef JANET_EV
|
||||
@@ -158,6 +159,7 @@ struct JanetVM {
|
||||
size_t listener_cap;
|
||||
size_t extra_listeners;
|
||||
JanetTable threaded_abstracts; /* All abstract types that can be shared between threads (used in this thread) */
|
||||
JanetTable active_tasks; /* All possibly live task fibers - used just for tracking */
|
||||
#ifdef JANET_WINDOWS
|
||||
void **iocp;
|
||||
#elif defined(JANET_EV_EPOLL)
|
||||
|
||||
@@ -364,14 +364,13 @@ JANET_CORE_FN(cfun_string_findall,
|
||||
|
||||
struct replace_state {
|
||||
struct kmp_state kmp;
|
||||
const uint8_t *subst;
|
||||
int32_t substlen;
|
||||
Janet subst;
|
||||
};
|
||||
|
||||
static void replacesetup(int32_t argc, Janet *argv, struct replace_state *s) {
|
||||
janet_arity(argc, 3, 4);
|
||||
JanetByteView pat = janet_getbytes(argv, 0);
|
||||
JanetByteView subst = janet_getbytes(argv, 1);
|
||||
Janet subst = argv[1];
|
||||
JanetByteView text = janet_getbytes(argv, 2);
|
||||
int32_t start = 0;
|
||||
if (argc == 4) {
|
||||
@@ -380,13 +379,14 @@ static void replacesetup(int32_t argc, Janet *argv, struct replace_state *s) {
|
||||
}
|
||||
kmp_init(&s->kmp, text.bytes, text.len, pat.bytes, pat.len);
|
||||
s->kmp.i = start;
|
||||
s->subst = subst.bytes;
|
||||
s->substlen = subst.len;
|
||||
s->subst = subst;
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_string_replace,
|
||||
"(string/replace patt subst str)",
|
||||
"Replace the first occurrence of `patt` with `subst` in the string `str`. "
|
||||
"If `subst` is a function, it will be called with `patt` only if a match is found, "
|
||||
"and should return the actual replacement text to use. "
|
||||
"Will return the new string if `patt` is found, otherwise returns `str`.") {
|
||||
int32_t result;
|
||||
struct replace_state s;
|
||||
@@ -397,10 +397,11 @@ JANET_CORE_FN(cfun_string_replace,
|
||||
kmp_deinit(&s.kmp);
|
||||
return janet_stringv(s.kmp.text, s.kmp.textlen);
|
||||
}
|
||||
buf = janet_string_begin(s.kmp.textlen - s.kmp.patlen + s.substlen);
|
||||
JanetByteView subst = janet_text_substitution(&s.subst, s.kmp.text + result, s.kmp.patlen, NULL);
|
||||
buf = janet_string_begin(s.kmp.textlen - s.kmp.patlen + subst.len);
|
||||
safe_memcpy(buf, s.kmp.text, result);
|
||||
safe_memcpy(buf + result, s.subst, s.substlen);
|
||||
safe_memcpy(buf + result + s.substlen,
|
||||
safe_memcpy(buf + result, subst.bytes, subst.len);
|
||||
safe_memcpy(buf + result + subst.len,
|
||||
s.kmp.text + result + s.kmp.patlen,
|
||||
s.kmp.textlen - result - s.kmp.patlen);
|
||||
kmp_deinit(&s.kmp);
|
||||
@@ -411,6 +412,8 @@ JANET_CORE_FN(cfun_string_replaceall,
|
||||
"(string/replace-all patt subst str)",
|
||||
"Replace all instances of `patt` with `subst` in the string `str`. Overlapping "
|
||||
"matches will not be counted, only the first match in such a span will be replaced. "
|
||||
"If `subst` is a function, it will be called with `patt` once for each match, "
|
||||
"and should return the actual replacement text to use. "
|
||||
"Will return the new string if `patt` is found, otherwise returns `str`.") {
|
||||
int32_t result;
|
||||
struct replace_state s;
|
||||
@@ -419,8 +422,9 @@ JANET_CORE_FN(cfun_string_replaceall,
|
||||
replacesetup(argc, argv, &s);
|
||||
janet_buffer_init(&b, s.kmp.textlen);
|
||||
while ((result = kmp_next(&s.kmp)) >= 0) {
|
||||
JanetByteView subst = janet_text_substitution(&s.subst, s.kmp.text + result, s.kmp.patlen, NULL);
|
||||
janet_buffer_push_bytes(&b, s.kmp.text + lastindex, result - lastindex);
|
||||
janet_buffer_push_bytes(&b, s.subst, s.substlen);
|
||||
janet_buffer_push_bytes(&b, subst.bytes, subst.len);
|
||||
lastindex = result + s.kmp.patlen;
|
||||
kmp_seti(&s.kmp, lastindex);
|
||||
}
|
||||
@@ -531,7 +535,30 @@ JANET_CORE_FN(cfun_string_join,
|
||||
JANET_CORE_FN(cfun_string_format,
|
||||
"(string/format format & values)",
|
||||
"Similar to C's `snprintf`, but specialized for operating with Janet values. Returns "
|
||||
"a new string.") {
|
||||
"a new string.\n\n"
|
||||
"The following conversion specifiers are supported, where the upper case specifiers generate "
|
||||
"upper case output:\n"
|
||||
"- `c`: ASCII character.\n"
|
||||
"- `d`, `i`: integer, formatted as a decimal number.\n"
|
||||
"- `x`, `X`: integer, formatted as a hexadecimal number.\n"
|
||||
"- `o`: integer, formatted as an octal number.\n"
|
||||
"- `f`, `F`: floating point number, formatted as a decimal number.\n"
|
||||
"- `e`, `E`: floating point number, formatted in scientific notation.\n"
|
||||
"- `g`, `G`: floating point number, formatted in its shortest form.\n"
|
||||
"- `a`, `A`: floating point number, formatted as a hexadecimal number.\n"
|
||||
"- `s`: formatted as a string, precision indicates padding and maximum length.\n"
|
||||
"- `t`: emit the type of the given value.\n"
|
||||
"- `v`: format with (describe x)"
|
||||
"- `V`: format with (string x)"
|
||||
"- `j`: format to jdn (Janet data notation).\n"
|
||||
"\n"
|
||||
"The following conversion specifiers are used for \"pretty-printing\", where the upper-case "
|
||||
"variants generate colored output. These speficiers can take a precision "
|
||||
"argument to specify the maximum nesting depth to print.\n"
|
||||
"- `p`, `P`: pretty format, truncating if necessary\n"
|
||||
"- `m`, `M`: pretty format without truncating.\n"
|
||||
"- `q`, `Q`: pretty format on one line, truncating if necessary.\n"
|
||||
"- `n`, `N`: pretty format on one line without truncation.\n") {
|
||||
janet_arity(argc, 1, -1);
|
||||
JanetBuffer *buffer = janet_buffer(0);
|
||||
const char *strfrmt = (const char *) janet_getstring(argv, 0);
|
||||
|
||||
139
src/core/util.c
139
src/core/util.c
@@ -92,8 +92,8 @@ const char *const janet_signal_names[14] = {
|
||||
"user5",
|
||||
"user6",
|
||||
"user7",
|
||||
"user8",
|
||||
"user9"
|
||||
"interrupt",
|
||||
"await"
|
||||
};
|
||||
|
||||
const char *const janet_status_names[16] = {
|
||||
@@ -109,8 +109,8 @@ const char *const janet_status_names[16] = {
|
||||
"user5",
|
||||
"user6",
|
||||
"user7",
|
||||
"user8",
|
||||
"user9",
|
||||
"interrupted",
|
||||
"suspended",
|
||||
"new",
|
||||
"alive"
|
||||
};
|
||||
@@ -118,6 +118,7 @@ const char *const janet_status_names[16] = {
|
||||
#ifndef JANET_PRF
|
||||
|
||||
int32_t janet_string_calchash(const uint8_t *str, int32_t len) {
|
||||
if (NULL == str) return 5381;
|
||||
const uint8_t *end = str + len;
|
||||
uint32_t hash = 5381;
|
||||
while (str < end)
|
||||
@@ -662,6 +663,59 @@ JanetBinding janet_binding_from_entry(Janet entry) {
|
||||
return binding;
|
||||
}
|
||||
|
||||
/* If the value at the given address can be coerced to a byte view,
|
||||
return that byte view. If it can't, replace the value at the address
|
||||
with the result of janet_to_string, and return a byte view over that
|
||||
string. */
|
||||
static JanetByteView memoize_byte_view(Janet *value) {
|
||||
JanetByteView result;
|
||||
if (!janet_bytes_view(*value, &result.bytes, &result.len)) {
|
||||
JanetString str = janet_to_string(*value);
|
||||
*value = janet_wrap_string(str);
|
||||
result.bytes = str;
|
||||
result.len = janet_string_length(str);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static JanetByteView to_byte_view(Janet value) {
|
||||
JanetByteView result;
|
||||
if (!janet_bytes_view(value, &result.bytes, &result.len)) {
|
||||
JanetString str = janet_to_string(value);
|
||||
result.bytes = str;
|
||||
result.len = janet_string_length(str);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
JanetByteView janet_text_substitution(
|
||||
Janet *subst,
|
||||
const uint8_t *bytes,
|
||||
uint32_t len,
|
||||
JanetArray *extra_argv) {
|
||||
int32_t extra_argc = extra_argv == NULL ? 0 : extra_argv->count;
|
||||
JanetType type = janet_type(*subst);
|
||||
switch (type) {
|
||||
case JANET_FUNCTION:
|
||||
case JANET_CFUNCTION: {
|
||||
int32_t argc = 1 + extra_argc;
|
||||
Janet *argv = janet_tuple_begin(argc);
|
||||
argv[0] = janet_stringv(bytes, len);
|
||||
for (int32_t i = 0; i < extra_argc; i++) {
|
||||
argv[i + 1] = extra_argv->data[i];
|
||||
}
|
||||
janet_tuple_end(argv);
|
||||
if (type == JANET_FUNCTION) {
|
||||
return to_byte_view(janet_call(janet_unwrap_function(*subst), argc, argv));
|
||||
} else {
|
||||
return to_byte_view(janet_unwrap_cfunction(*subst)(argc, argv));
|
||||
}
|
||||
}
|
||||
default:
|
||||
return memoize_byte_view(subst);
|
||||
}
|
||||
}
|
||||
|
||||
JanetBinding janet_resolve_ext(JanetTable *env, const uint8_t *sym) {
|
||||
Janet entry = janet_table_get(env, janet_wrap_symbol(sym));
|
||||
return janet_binding_from_entry(entry);
|
||||
@@ -821,34 +875,73 @@ int32_t janet_sorted_keys(const JanetKV *dict, int32_t cap, int32_t *index_buffe
|
||||
/* Clock shims for various platforms */
|
||||
#ifdef JANET_GETTIME
|
||||
#ifdef JANET_WINDOWS
|
||||
int janet_gettime(struct timespec *spec) {
|
||||
FILETIME ftime;
|
||||
GetSystemTimeAsFileTime(&ftime);
|
||||
int64_t wintime = (int64_t)(ftime.dwLowDateTime) | ((int64_t)(ftime.dwHighDateTime) << 32);
|
||||
/* Windows epoch is January 1, 1601 apparently */
|
||||
wintime -= 116444736000000000LL;
|
||||
spec->tv_sec = wintime / 10000000LL;
|
||||
/* Resolution is 100 nanoseconds. */
|
||||
spec->tv_nsec = wintime % 10000000LL * 100;
|
||||
#include <profileapi.h>
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
||||
if (source == JANET_TIME_REALTIME) {
|
||||
FILETIME ftime;
|
||||
GetSystemTimeAsFileTime(&ftime);
|
||||
int64_t wintime = (int64_t)(ftime.dwLowDateTime) | ((int64_t)(ftime.dwHighDateTime) << 32);
|
||||
/* Windows epoch is January 1, 1601 apparently */
|
||||
wintime -= 116444736000000000LL;
|
||||
spec->tv_sec = wintime / 10000000LL;
|
||||
/* Resolution is 100 nanoseconds. */
|
||||
spec->tv_nsec = wintime % 10000000LL * 100;
|
||||
} else if (source == JANET_TIME_MONOTONIC) {
|
||||
LARGE_INTEGER count;
|
||||
LARGE_INTEGER perf_freq;
|
||||
QueryPerformanceCounter(&count);
|
||||
QueryPerformanceFrequency(&perf_freq);
|
||||
spec->tv_sec = count.QuadPart / perf_freq.QuadPart;
|
||||
spec->tv_nsec = (long)((count.QuadPart % perf_freq.QuadPart) * 1000000000 / perf_freq.QuadPart);
|
||||
} else if (source == JANET_TIME_CPUTIME) {
|
||||
FILETIME creationTime, exitTime, kernelTime, userTime;
|
||||
GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime);
|
||||
int64_t tmp = ((int64_t)userTime.dwHighDateTime << 32) + userTime.dwLowDateTime;
|
||||
spec->tv_sec = tmp / 10000000LL;
|
||||
spec->tv_nsec = tmp % 10000000LL * 100;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* clock_gettime() wasn't available on Mac until 10.12. */
|
||||
#elif defined(JANET_APPLE) && !defined(MAC_OS_X_VERSION_10_12)
|
||||
#include <mach/clock.h>
|
||||
#include <mach/mach.h>
|
||||
int janet_gettime(struct timespec *spec) {
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
clock_get_time(cclock, &mts);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
spec->tv_sec = mts.tv_sec;
|
||||
spec->tv_nsec = mts.tv_nsec;
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
||||
if (source == JANET_TIME_REALTIME) {
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
clock_get_time(cclock, &mts);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
spec->tv_sec = mts.tv_sec;
|
||||
spec->tv_nsec = mts.tv_nsec;
|
||||
} else if (source == JANET_TIME_MONOTONIC) {
|
||||
clock_serv_t cclock;
|
||||
int nsecs;
|
||||
mach_msg_type_number_t count;
|
||||
host_get_clock_service(mach_host_self(), clock, &cclock);
|
||||
clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
clock_getres(CLOCK_MONOTONIC, spec);
|
||||
}
|
||||
if (source == JANET_TIME_CPUTIME) {
|
||||
clock_t tmp = clock();
|
||||
spec->tv_sec = tmp;
|
||||
spec->tv_nsec = (tmp - spec->tv_sec) * 1.0e9;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int janet_gettime(struct timespec *spec) {
|
||||
return clock_gettime(CLOCK_REALTIME, spec);
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
||||
clockid_t cid = CLOCK_REALTIME;
|
||||
if (source == JANET_TIME_REALTIME) {
|
||||
cid = CLOCK_REALTIME;
|
||||
} else if (source == JANET_TIME_MONOTONIC) {
|
||||
cid = CLOCK_MONOTONIC;
|
||||
} else if (source == JANET_TIME_CPUTIME) {
|
||||
cid = CLOCK_PROCESS_CPUTIME_ID;
|
||||
}
|
||||
return clock_gettime(cid, spec);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -93,6 +93,11 @@ void janet_buffer_format(
|
||||
Janet *argv);
|
||||
Janet janet_next_impl(Janet ds, Janet key, int is_interpreter);
|
||||
JanetBinding janet_binding_from_entry(Janet entry);
|
||||
JanetByteView janet_text_substitution(
|
||||
Janet *subst,
|
||||
const uint8_t *bytes,
|
||||
uint32_t len,
|
||||
JanetArray *extra_args);
|
||||
|
||||
/* Registry functions */
|
||||
void janet_registry_put(
|
||||
@@ -121,7 +126,12 @@ void janet_core_cfuns_ext(JanetTable *env, const char *regprefix, const JanetReg
|
||||
|
||||
/* Clock gettime */
|
||||
#ifdef JANET_GETTIME
|
||||
int janet_gettime(struct timespec *spec);
|
||||
enum JanetTimeSource {
|
||||
JANET_TIME_REALTIME,
|
||||
JANET_TIME_MONOTONIC,
|
||||
JANET_TIME_CPUTIME
|
||||
};
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source);
|
||||
#endif
|
||||
|
||||
/* strdup */
|
||||
|
||||
@@ -439,20 +439,21 @@ int janet_compare(Janet x, Janet y) {
|
||||
return status - 2;
|
||||
}
|
||||
|
||||
static int32_t getter_checkint(Janet key, int32_t max) {
|
||||
static int32_t getter_checkint(JanetType type, Janet key, int32_t max) {
|
||||
if (!janet_checkint(key)) goto bad;
|
||||
int32_t ret = janet_unwrap_integer(key);
|
||||
if (ret < 0) goto bad;
|
||||
if (ret >= max) goto bad;
|
||||
return ret;
|
||||
bad:
|
||||
janet_panicf("expected integer key in range [0, %d), got %v", max, key);
|
||||
janet_panicf("expected integer key for %s in range [0, %d), got %v", janet_type_names[type], max, key);
|
||||
}
|
||||
|
||||
/* Gets a value and returns. Can panic. */
|
||||
Janet janet_in(Janet ds, Janet key) {
|
||||
Janet value;
|
||||
switch (janet_type(ds)) {
|
||||
JanetType type = janet_type(ds);
|
||||
switch (type) {
|
||||
default:
|
||||
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
||||
break;
|
||||
@@ -464,19 +465,19 @@ Janet janet_in(Janet ds, Janet key) {
|
||||
break;
|
||||
case JANET_ARRAY: {
|
||||
JanetArray *array = janet_unwrap_array(ds);
|
||||
int32_t index = getter_checkint(key, array->count);
|
||||
int32_t index = getter_checkint(type, key, array->count);
|
||||
value = array->data[index];
|
||||
break;
|
||||
}
|
||||
case JANET_TUPLE: {
|
||||
const Janet *tuple = janet_unwrap_tuple(ds);
|
||||
int32_t len = janet_tuple_length(tuple);
|
||||
value = tuple[getter_checkint(key, len)];
|
||||
value = tuple[getter_checkint(type, key, len)];
|
||||
break;
|
||||
}
|
||||
case JANET_BUFFER: {
|
||||
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
||||
int32_t index = getter_checkint(key, buffer->count);
|
||||
int32_t index = getter_checkint(type, key, buffer->count);
|
||||
value = janet_wrap_integer(buffer->data[index]);
|
||||
break;
|
||||
}
|
||||
@@ -484,7 +485,7 @@ Janet janet_in(Janet ds, Janet key) {
|
||||
case JANET_SYMBOL:
|
||||
case JANET_KEYWORD: {
|
||||
const uint8_t *str = janet_unwrap_string(ds);
|
||||
int32_t index = getter_checkint(key, janet_string_length(str));
|
||||
int32_t index = getter_checkint(type, key, janet_string_length(str));
|
||||
value = janet_wrap_integer(str[index]);
|
||||
break;
|
||||
}
|
||||
@@ -752,13 +753,14 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
|
||||
}
|
||||
|
||||
void janet_put(Janet ds, Janet key, Janet value) {
|
||||
switch (janet_type(ds)) {
|
||||
JanetType type = janet_type(ds);
|
||||
switch (type) {
|
||||
default:
|
||||
janet_panicf("expected %T, got %v",
|
||||
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
||||
case JANET_ARRAY: {
|
||||
JanetArray *array = janet_unwrap_array(ds);
|
||||
int32_t index = getter_checkint(key, INT32_MAX - 1);
|
||||
int32_t index = getter_checkint(type, key, INT32_MAX - 1);
|
||||
if (index >= array->count) {
|
||||
janet_array_setcount(array, index + 1);
|
||||
}
|
||||
@@ -767,7 +769,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
|
||||
}
|
||||
case JANET_BUFFER: {
|
||||
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
||||
int32_t index = getter_checkint(key, INT32_MAX - 1);
|
||||
int32_t index = getter_checkint(type, key, INT32_MAX - 1);
|
||||
if (!janet_checkint(value))
|
||||
janet_panicf("can only put integers in buffers, got %v", value);
|
||||
if (index >= buffer->count) {
|
||||
|
||||
@@ -1423,6 +1423,7 @@ static JanetSignal janet_continue_no_check(JanetFiber *fiber, Janet in, Janet *o
|
||||
if (sig != JANET_SIGNAL_OK && !(child->flags & (1 << sig))) {
|
||||
*out = in;
|
||||
janet_fiber_set_status(fiber, sig);
|
||||
fiber->last_value = child->last_value;
|
||||
return sig;
|
||||
}
|
||||
/* Check if we need any special handling for certain opcodes */
|
||||
|
||||
@@ -182,7 +182,7 @@ extern "C" {
|
||||
/* Enable or disable the FFI library. Currently, FFI only enabled on
|
||||
* x86-64 operating systems. */
|
||||
#ifndef JANET_NO_FFI
|
||||
#if !defined(__EMSCRIPTEN__) && (defined(__x86_64__) || defined(_M_X64))
|
||||
#if !defined(__EMSCRIPTEN__)
|
||||
#define JANET_FFI
|
||||
#endif
|
||||
#endif
|
||||
@@ -568,6 +568,7 @@ typedef void *JanetAbstract;
|
||||
#define JANET_STREAM_WRITABLE 0x400
|
||||
#define JANET_STREAM_ACCEPTABLE 0x800
|
||||
#define JANET_STREAM_UDPSERVER 0x1000
|
||||
#define JANET_STREAM_TOCLOSE 0x10000
|
||||
|
||||
typedef enum {
|
||||
JANET_ASYNC_EVENT_INIT,
|
||||
@@ -1479,6 +1480,7 @@ JANET_API void janet_ev_readchunk(JanetStream *stream, JanetBuffer *buf, int32_t
|
||||
JANET_API void janet_ev_recv(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags);
|
||||
JANET_API void janet_ev_recvchunk(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags);
|
||||
JANET_API void janet_ev_recvfrom(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags);
|
||||
JANET_API void janet_ev_connect(JanetStream *stream, int flags);
|
||||
#endif
|
||||
|
||||
/* Write async to a stream */
|
||||
@@ -1687,6 +1689,7 @@ JANET_API void janet_table_clear(JanetTable *table);
|
||||
JANET_API JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv);
|
||||
JANET_API JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv);
|
||||
JANET_API JanetFiberStatus janet_fiber_status(JanetFiber *fiber);
|
||||
JANET_API int janet_fiber_can_resume(JanetFiber *fiber);
|
||||
JANET_API JanetFiber *janet_current_fiber(void);
|
||||
JANET_API JanetFiber *janet_root_fiber(void);
|
||||
|
||||
@@ -1877,7 +1880,7 @@ JANET_API Janet janet_resolve_core(const char *name);
|
||||
/* sourcemaps only */
|
||||
#define JANET_REG_S(JNAME, CNAME) {JNAME, CNAME, NULL, __FILE__, CNAME##_sourceline_}
|
||||
#define JANET_FN_S(CNAME, USAGE, DOCSTRING) \
|
||||
static int32_t CNAME##_sourceline_ = __LINE__; \
|
||||
static const int32_t CNAME##_sourceline_ = __LINE__; \
|
||||
Janet CNAME (int32_t argc, Janet *argv)
|
||||
#define JANET_DEF_S(ENV, JNAME, VAL, DOC) \
|
||||
janet_def_sm(ENV, JNAME, VAL, NULL, __FILE__, __LINE__)
|
||||
@@ -1893,7 +1896,7 @@ JANET_API Janet janet_resolve_core(const char *name);
|
||||
/* sourcemaps and docstrings */
|
||||
#define JANET_REG_SD(JNAME, CNAME) {JNAME, CNAME, CNAME##_docstring_, __FILE__, CNAME##_sourceline_}
|
||||
#define JANET_FN_SD(CNAME, USAGE, DOCSTRING) \
|
||||
static int32_t CNAME##_sourceline_ = __LINE__; \
|
||||
static const int32_t CNAME##_sourceline_ = __LINE__; \
|
||||
static const char CNAME##_docstring_[] = USAGE "\n\n" DOCSTRING; \
|
||||
Janet CNAME (int32_t argc, Janet *argv)
|
||||
#define JANET_DEF_SD(ENV, JNAME, VAL, DOC) \
|
||||
@@ -1967,6 +1970,7 @@ JANET_API JanetTable *janet_gettable(const Janet *argv, int32_t n);
|
||||
JANET_API JanetStruct janet_getstruct(const Janet *argv, int32_t n);
|
||||
JANET_API JanetString janet_getstring(const Janet *argv, int32_t n);
|
||||
JANET_API const char *janet_getcstring(const Janet *argv, int32_t n);
|
||||
JANET_API const char *janet_getcbytes(const Janet *argv, int32_t n);
|
||||
JANET_API JanetSymbol janet_getsymbol(const Janet *argv, int32_t n);
|
||||
JANET_API JanetKeyword janet_getkeyword(const Janet *argv, int32_t n);
|
||||
JANET_API JanetBuffer *janet_getbuffer(const Janet *argv, int32_t n);
|
||||
@@ -1996,6 +2000,7 @@ JANET_API JanetTuple janet_opttuple(const Janet *argv, int32_t argc, int32_t n,
|
||||
JANET_API JanetStruct janet_optstruct(const Janet *argv, int32_t argc, int32_t n, JanetStruct dflt);
|
||||
JANET_API JanetString janet_optstring(const Janet *argv, int32_t argc, int32_t n, JanetString dflt);
|
||||
JANET_API const char *janet_optcstring(const Janet *argv, int32_t argc, int32_t n, const char *dflt);
|
||||
JANET_API const char *janet_optcbytes(const Janet *argv, int32_t argc, int32_t n, const char *dflt);
|
||||
JANET_API JanetSymbol janet_optsymbol(const Janet *argv, int32_t argc, int32_t n, JanetString dflt);
|
||||
JANET_API JanetKeyword janet_optkeyword(const Janet *argv, int32_t argc, int32_t n, JanetString dflt);
|
||||
JANET_API JanetFiber *janet_optfiber(const Janet *argv, int32_t argc, int32_t n, JanetFiber *dflt);
|
||||
|
||||
@@ -147,8 +147,11 @@ static void setup_console_output(void) {
|
||||
DWORD dwMode = 0;
|
||||
GetConsoleMode(hOut, &dwMode);
|
||||
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||
dwMode |= ENABLE_PROCESSED_OUTPUT;
|
||||
SetConsoleMode(hOut, dwMode);
|
||||
SetConsoleOutputCP(65001);
|
||||
if (IsValidCodePage(65001)) {
|
||||
SetConsoleOutputCP(65001);
|
||||
}
|
||||
}
|
||||
|
||||
/* Ansi terminal raw mode */
|
||||
|
||||
@@ -24,6 +24,11 @@
|
||||
(def errsym (keyword (gensym)))
|
||||
~(assert (= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg))
|
||||
|
||||
(defn check-compile-error
|
||||
[form]
|
||||
(def result (compile form))
|
||||
(assert (table? result) (string/format "expected compilation error for %j, but compiled without error" form)))
|
||||
|
||||
(defmacro assert-no-error
|
||||
[msg & forms]
|
||||
(def errsym (keyword (gensym)))
|
||||
|
||||
@@ -252,6 +252,9 @@
|
||||
(def xs (apply tuple (seq [x :down [8 -2] :when (even? x)] (tuple (/ x 2) x))))
|
||||
(assert (= xs '((4 8) (3 6) (2 4) (1 2) (0 0))) "seq macro 2")
|
||||
|
||||
(def xs (catseq [x :range [0 3]] [x x]))
|
||||
(assert (deep= xs @[0 0 1 1 2 2]) "catseq")
|
||||
|
||||
# :range-to and :down-to
|
||||
(assert (deep= (seq [x :range-to [0 10]] x) (seq [x :range [0 11]] x)) "loop :range-to")
|
||||
(assert (deep= (seq [x :down-to [10 0]] x) (seq [x :down [10 -1]] x)) "loop :down-to")
|
||||
@@ -320,11 +323,27 @@
|
||||
(assert (deep= (map + [1 2 3] [10 20 30] [100 200 300]) @[111 222 333]))
|
||||
(assert (deep= (map + [1 2 3] [10 20 30] [100 200 300] [1000 2000 3000]) @[1111 2222 3333]))
|
||||
(assert (deep= (map + [1 2 3] [10 20 30] [100 200 300] [1000 2000 3000] [10000 20000 30000]) @[11111 22222 33333]))
|
||||
(assert (deep= (map + [1 2 3] [10 20 30] [100 200 300] [1000 2000 3000] [10000 20000 30000] [100000 200000 300000]) @[111111 222222 333333]))
|
||||
|
||||
# Mapping uses the shortest sequence
|
||||
(assert (deep= (map + [1 2 3 4] [10 20 30]) @[11 22 33]))
|
||||
(assert (deep= (map + [1 2 3 4] [10 20 30] [100 200]) @[111 222]))
|
||||
(assert (deep= (map + [1 2 3 4] [10 20 30] [100 200] [1000]) @[1111]))
|
||||
(assert (deep= (map + [1 2 3 4] [10 20 30] [100 200] [1000] []) @[]))
|
||||
|
||||
# Variadic arguments to map-like functions
|
||||
(assert (deep= (mapcat tuple [1 2 3 4] [5 6 7 8]) @[1 5 2 6 3 7 4 8]))
|
||||
(assert (deep= (keep |(if (> $1 0) (/ $0 $1)) [1 2 3 4 5] [1 2 1 0 1]) @[1 1 3 5]))
|
||||
|
||||
(assert (= (count = [1 3 2 4 3 5 4 2 1] [1 2 3 4 5 4 3 2 1]) 4))
|
||||
|
||||
(assert (= (some not= (range 5) (range 5)) nil))
|
||||
(assert (= (some = [1 2 3 4 5] [5 4 3 2 1]) true))
|
||||
|
||||
(assert (= (all = (range 5) (range 5)) true))
|
||||
(assert (= (all not= [1 2 3 4 5] [5 4 3 2 1]) false))
|
||||
|
||||
(assert (= false (deep-not= [1] [1])) "issue #1149")
|
||||
|
||||
# Sort function
|
||||
(assert (deep=
|
||||
@@ -342,6 +361,8 @@
|
||||
(assert (= (and 0 1 nil) nil) "and 0 1 nil")
|
||||
(assert (= (and 1) 1) "and 1")
|
||||
(assert (= (and) true) "and with no arguments")
|
||||
(assert (= (and 1 true) true) "and with trailing true")
|
||||
(assert (= (and 1 true 2) 2) "and with internal true")
|
||||
|
||||
(assert (= (or true true) true) "or true true")
|
||||
(assert (= (or true false) true) "or true false")
|
||||
@@ -353,4 +374,11 @@
|
||||
(assert (= (or 1) 1) "or 1")
|
||||
(assert (= (or) nil) "or with no arguments")
|
||||
|
||||
(def yielder
|
||||
(coro
|
||||
(defer (yield :end)
|
||||
(repeat 5 (yield :item)))))
|
||||
(def items (seq [x :in yielder] x))
|
||||
(assert (deep= @[:item :item :item :item :item :end] items) "yield within nested fibers")
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -72,6 +72,10 @@
|
||||
(assert (= (string/replace "X" "." "XXX...XXX...XXX") ".XX...XXX...XXX") "string/replace 1")
|
||||
(assert (= (string/replace-all "X" "." "XXX...XXX...XXX") "...............") "string/replace-all 1")
|
||||
(assert (= (string/replace-all "XX" "." "XXX...XXX...XXX") ".X....X....X") "string/replace-all 2")
|
||||
(assert (= (string/replace "xx" string/ascii-upper "xxyxyxyxxxy") "XXyxyxyxxxy") "string/replace function")
|
||||
(assert (= (string/replace-all "xx" string/ascii-upper "xxyxyxyxxxy") "XXyxyxyXXxy") "string/replace-all function")
|
||||
(assert (= (string/replace "x" 12 "xyx") "12yx") "string/replace stringable")
|
||||
(assert (= (string/replace-all "x" 12 "xyx") "12y12") "string/replace-all stringable")
|
||||
(assert (= (string/ascii-lower "ABCabc&^%!@:;.") "abcabc&^%!@:;.") "string/ascii-lower")
|
||||
(assert (= (string/ascii-upper "ABCabc&^%!@:;.") "ABCABC&^%!@:;.") "string/ascii-lower")
|
||||
(assert (= (string/reverse "") "") "string/reverse 1")
|
||||
|
||||
@@ -349,7 +349,7 @@
|
||||
(def janet-longstring
|
||||
~{:delim (some "`")
|
||||
:open (capture :delim :n)
|
||||
:close (cmt (* (not (> -1 "`")) (-> :n) (<- :delim)) ,=)
|
||||
:close (cmt (* (not (> -1 "`")) (-> :n) (<- (backmatch :n))) ,=)
|
||||
:main (* :open (any (if-not :close 1)) :close -1)})
|
||||
|
||||
(check-match janet-longstring "`john" false)
|
||||
@@ -359,6 +359,7 @@
|
||||
(check-match janet-longstring "`` ``" true)
|
||||
(check-match janet-longstring "``` `` ```" true)
|
||||
(check-match janet-longstring "`` ```" false)
|
||||
(check-match janet-longstring "`a``b`" false)
|
||||
|
||||
# Line and column capture
|
||||
|
||||
|
||||
@@ -83,8 +83,13 @@
|
||||
(assert (deep= (drop 10 []) []) "drop 2")
|
||||
(assert (deep= (drop 0 [1 2 3 4 5]) [1 2 3 4 5]) "drop 3")
|
||||
(assert (deep= (drop 10 [1 2 3]) []) "drop 4")
|
||||
(assert (deep= (drop -2 [:a :b :c]) [:a :b :c]) "drop 5")
|
||||
(assert-error :invalid-type (drop 3 {}) "drop 6")
|
||||
(assert (deep= (drop -1 [1 2 3]) [1 2]) "drop 5")
|
||||
(assert (deep= (drop -10 [1 2 3]) []) "drop 6")
|
||||
(assert (deep= (drop 1 "abc") "bc") "drop 7")
|
||||
(assert (deep= (drop 10 "abc") "") "drop 8")
|
||||
(assert (deep= (drop -1 "abc") "ab") "drop 9")
|
||||
(assert (deep= (drop -10 "abc") "") "drop 10")
|
||||
(assert-error :invalid-type (drop 3 {}) "drop 11")
|
||||
|
||||
# drop-until
|
||||
|
||||
@@ -98,4 +103,18 @@
|
||||
# Quasiquote bracketed tuples
|
||||
(assert (= (tuple/type ~[1 2 3]) (tuple/type '[1 2 3])) "quasiquote bracket tuples")
|
||||
|
||||
# No useless splices
|
||||
(check-compile-error '((splice [1 2 3]) 0))
|
||||
(check-compile-error '(if ;[1 2] 5))
|
||||
(check-compile-error '(while ;[1 2 3] (print :hi)))
|
||||
(check-compile-error '(def x ;[1 2 3]))
|
||||
(check-compile-error '(fn [x] ;[x 1 2 3]))
|
||||
|
||||
# No splice propagation
|
||||
(check-compile-error '(+ 1 (do ;[2 3 4]) 5))
|
||||
(check-compile-error '(+ 1 (upscope ;[2 3 4]) 5))
|
||||
# compiler inlines when condition is constant, ensure that optimization doesn't break
|
||||
(check-compile-error '(+ 1 (if true ;[3 4])))
|
||||
(check-compile-error '(+ 1 (if false nil ;[3 4])))
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -261,4 +261,12 @@
|
||||
(modcheck -10 3)
|
||||
(modcheck -10 -3)
|
||||
|
||||
# Check for issue #1130
|
||||
(var d (int/s64 7))
|
||||
(mod 0 d)
|
||||
|
||||
(var d (int/s64 7))
|
||||
(def result (seq [n :in (range -21 0)] (mod n d)))
|
||||
(assert (deep= result (map int/s64 @[0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6])) "issue #1130")
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -60,7 +60,7 @@
|
||||
:buffer (/ '(* "@" :bytes) (constant :string))
|
||||
:long-bytes {:delim (some "`")
|
||||
:open (capture :delim :n)
|
||||
:close (cmt (* (not (> -1 "`")) (-> :n) ':delim) ,=)
|
||||
:close (cmt (* (not (> -1 "`")) (-> :n) '(backmatch :n)) ,=)
|
||||
:main (drop (* :open (any (if-not :close 1)) :close))}
|
||||
:long-string (/ ':long-bytes (constant :string))
|
||||
:long-buffer (/ '(* "@" :long-bytes) (constant :string))
|
||||
@@ -239,6 +239,12 @@
|
||||
(assert (= (os/mktime (os/date now true) true) now) "local os/mktime")
|
||||
(assert (= (os/mktime {:year 1970}) 0) "os/mktime default values")
|
||||
|
||||
# OS strftime test
|
||||
|
||||
(assert (= (os/strftime "%Y-%m-%d %H:%M:%S" 0) "1970-01-01 00:00:00") "strftime UTC epoch")
|
||||
(assert (= (os/strftime "%Y-%m-%d %H:%M:%S" 1388608200) "2014-01-01 20:30:00") "strftime january 2014")
|
||||
(assert (= (try (os/strftime "%%%d%t") ([err] err)) "invalid conversion specifier '%t'") "invalid conversion specifier")
|
||||
|
||||
# Appending buffer to self
|
||||
|
||||
(with-dyns [:out @""]
|
||||
@@ -327,4 +333,29 @@
|
||||
|
||||
(assert (pos? (length (gensym))) "gensym not empty, regression #753")
|
||||
|
||||
|
||||
# os/clock. These tests might prove fragile under CI because they
|
||||
# rely on measured time. We'll see.
|
||||
|
||||
(defmacro measure-time [clocks & body]
|
||||
(def [t1 t2] [(gensym) (gensym)])
|
||||
~(do
|
||||
(def ,t1 (map |(os/clock $) ,clocks))
|
||||
,;body
|
||||
(def ,t2 (map |(os/clock $) ,clocks))
|
||||
(zipcoll ,clocks (map |(- ;$) (map tuple ,t2 ,t1))))
|
||||
)
|
||||
|
||||
# Spin for 0.1 seconds
|
||||
(def dt (measure-time [:realtime :monotonic :cputime]
|
||||
(def t1 (os/clock :monotonic))
|
||||
(while (< (- (os/clock :monotonic) t1) 0.1) true)))
|
||||
(assert (> (dt :monotonic) 0.10))
|
||||
(assert (> (dt :cputime) 0.05))
|
||||
|
||||
# Sleep for 0.1 seconds
|
||||
(def dt (measure-time [:realtime :monotonic :cputime] (os/sleep 0.1)))
|
||||
(assert (> (dt :monotonic) 0.10))
|
||||
(assert (< (dt :cputime) 0.05))
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -330,7 +330,6 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
|
||||
(assert (deep= (peg/find-all '"/" p) @[0 4 10 14]) "peg find-all")
|
||||
|
||||
# Peg replace and replace-all
|
||||
(var ti 0)
|
||||
(defn check-replacer
|
||||
[x y z]
|
||||
(assert (= (string/replace x y z) (string (peg/replace x y z))) "replacer test replace")
|
||||
@@ -339,6 +338,32 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
|
||||
(check-replacer "abc" "Z" "")
|
||||
(check-replacer "aba" "ZZZZZZ" "ababababababa")
|
||||
(check-replacer "aba" "" "ababababababa")
|
||||
(check-replacer "aba" string/ascii-upper "ababababababa")
|
||||
(check-replacer "aba" 123 "ababababababa")
|
||||
|
||||
(assert (= (string (peg/replace-all ~(set "ab") string/ascii-upper "abcaa"))
|
||||
"ABcAA")
|
||||
"peg/replace-all cfunction")
|
||||
(assert (= (string (peg/replace-all ~(set "ab") |$ "abcaa"))
|
||||
"abcaa")
|
||||
"peg/replace-all function")
|
||||
|
||||
(defn peg-test [name f peg subst text expected]
|
||||
(assert (= (string (f peg subst text)) expected) name))
|
||||
|
||||
(peg-test "peg/replace has access to captures"
|
||||
peg/replace
|
||||
~(sequence "." (capture (set "ab")))
|
||||
(fn [str char] (string/format "%s -> %s, " str (string/ascii-upper char)))
|
||||
".a.b.c"
|
||||
".a -> A, .b.c")
|
||||
|
||||
(peg-test "peg/replace-all has access to captures"
|
||||
peg/replace-all
|
||||
~(sequence "." (capture (set "ab")))
|
||||
(fn [str char] (string/format "%s -> %s, " str (string/ascii-upper char)))
|
||||
".a.b.c"
|
||||
".a -> A, .b -> B, .c")
|
||||
|
||||
# Peg bug
|
||||
(assert (deep= @[] (peg/match '(any 1) @"")) "peg empty pattern 1")
|
||||
|
||||
@@ -52,6 +52,11 @@
|
||||
(def retval (os/proc-wait p))
|
||||
(assert (not= retval 24) "Process was *not* terminated by parent"))
|
||||
|
||||
(let [p (os/spawn [janet "-e" `(do (ev/sleep 30) (os/exit 24)`] :p)]
|
||||
(os/proc-kill p false :term)
|
||||
(def retval (os/proc-wait p))
|
||||
(assert (not= retval 24) "Process was *not* terminated by parent"))
|
||||
|
||||
# Parallel subprocesses
|
||||
|
||||
(defn calc-1
|
||||
@@ -97,6 +102,20 @@
|
||||
(os/execute [janet "-e" `(repeat 20 (print :hello))`] :p {:out f})
|
||||
(file/flush f)))
|
||||
|
||||
# each-line iterator
|
||||
|
||||
(assert-no-error "file/lines iterator"
|
||||
(def outstream (os/open "unique.txt" :wct))
|
||||
(def buf1 "123\n456\n")
|
||||
(defer (:close outstream)
|
||||
(:write outstream buf1))
|
||||
(var buf2 "")
|
||||
(with [f (file/open "unique.txt" :r)]
|
||||
(each line (file/lines f)
|
||||
(set buf2 (string buf2 line))))
|
||||
(assert (= buf1 buf2) "file/lines iterator")
|
||||
(os/rm "unique.txt"))
|
||||
|
||||
# Issue #593
|
||||
(assert-no-error "file writing 3"
|
||||
(def outfile (file/open "unique.txt" :w))
|
||||
|
||||
@@ -44,6 +44,43 @@
|
||||
(assert (= :brackets (tuple/type (1 (macex1 '~[1 2 3 4])))) "macex1 qq bracket tuple")
|
||||
(assert (deep= (macex1 '~@[1 2 3 4 ,blah]) '~@[1 2 3 4 ,blah]) "macex1 qq array")
|
||||
|
||||
# Sourcemaps in threading macros
|
||||
(defn check-threading [macro expansion]
|
||||
(def expanded (macex1 (tuple macro 0 '(x) '(y))))
|
||||
(assert (= expanded expansion) (string macro " expansion value"))
|
||||
(def smap-x (tuple/sourcemap (get expanded 1)))
|
||||
(def smap-y (tuple/sourcemap expanded))
|
||||
(def line first)
|
||||
(defn column [t] (t 1))
|
||||
(assert (not= smap-x [-1 -1]) (string macro " x sourcemap existence"))
|
||||
(assert (not= smap-y [-1 -1]) (string macro " y sourcemap existence"))
|
||||
(assert (or (< (line smap-x) (line smap-y))
|
||||
(and (= (line smap-x) (line smap-y))
|
||||
(< (column smap-x) (column smap-y))))
|
||||
(string macro " relation between x and y sourcemap")))
|
||||
|
||||
(check-threading '-> '(y (x 0)))
|
||||
(check-threading '->> '(y (x 0)))
|
||||
|
||||
# keep-syntax
|
||||
(let [brak '[1 2 3]
|
||||
par '(1 2 3)]
|
||||
|
||||
(tuple/setmap brak 2 1)
|
||||
|
||||
(assert (deep= (keep-syntax brak @[1 2 3]) @[1 2 3]) "keep-syntax brackets ignore array")
|
||||
(assert (= (keep-syntax! brak @[1 2 3]) '[1 2 3]) "keep-syntax! brackets replace array")
|
||||
|
||||
(assert (= (keep-syntax! par (map inc @[1 2 3])) '(2 3 4)) "keep-syntax! parens coerce array")
|
||||
(assert (not= (keep-syntax! brak @[1 2 3]) '(1 2 3)) "keep-syntax! brackets not parens")
|
||||
(assert (not= (keep-syntax! par @[1 2 3]) '[1 2 3]) "keep-syntax! parens not brackets")
|
||||
(assert (= (tuple/sourcemap brak)
|
||||
(tuple/sourcemap (keep-syntax! brak @[1 2 3]))) "keep-syntax! brackets source map")
|
||||
|
||||
(keep-syntax par brak)
|
||||
(assert (not= (tuple/sourcemap brak) (tuple/sourcemap par)) "keep-syntax no mutate")
|
||||
(assert (= (keep-syntax 1 brak) brak) "keep-syntax brackets ignore type"))
|
||||
|
||||
# Cancel test
|
||||
(def f (fiber/new (fn [&] (yield 1) (yield 2) (yield 3) 4) :yti))
|
||||
(assert (= 1 (resume f)) "cancel resume 1")
|
||||
@@ -216,4 +253,13 @@
|
||||
# Check missing struct proto bug.
|
||||
(assert (struct/getproto (struct/with-proto {:a 1} :b 2 :c nil)) "missing struct proto")
|
||||
|
||||
# Test thaw and freeze
|
||||
(def table-to-freeze @{:c 22 :b [1 2 3 4] :d @"test" :e "test2"})
|
||||
(def table-to-freeze-with-inline-proto @{:a @[1 2 3] :b @[1 2 3 4] :c 22 :d @"test" :e @"test2"})
|
||||
(def struct-to-thaw (struct/with-proto {:a [1 2 3]} :c 22 :b [1 2 3 4] :d "test" :e "test2"))
|
||||
(table/setproto table-to-freeze @{:a @[1 2 3]})
|
||||
(assert (deep= {:a [1 2 3] :b [1 2 3 4] :c 22 :d "test" :e "test2"} (freeze table-to-freeze)))
|
||||
(assert (deep= table-to-freeze-with-inline-proto (thaw table-to-freeze)))
|
||||
(assert (deep= table-to-freeze-with-inline-proto (thaw struct-to-thaw)))
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -28,7 +28,8 @@
|
||||
(assert (= (thunk) 1) "delay 3")
|
||||
(assert (= counter 1) "delay 4")
|
||||
|
||||
(def has-ffi (dyn 'ffi/native))
|
||||
# We should get ARM support...
|
||||
(def has-ffi (and (dyn 'ffi/native) (= (os/arch) :x64)))
|
||||
|
||||
# FFI check
|
||||
(compwhen has-ffi
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
(start-suite 15)
|
||||
|
||||
(assert (deep= (in (disasm (defn a [] (def x 10) x)) :symbolmap)
|
||||
@[[0 3 0 'a] [1 3 1 'x]])
|
||||
@[[0 2 0 'a] [0 2 1 'x]])
|
||||
"symbolslots when *debug* is true")
|
||||
|
||||
(defn a [arg]
|
||||
@@ -33,11 +33,11 @@
|
||||
(def y 20)
|
||||
(def z 30)
|
||||
(+ x y z)))) :symbolmap)
|
||||
@[[0 7 0 'arg]
|
||||
[0 7 1 'a]
|
||||
[1 7 2 'x]
|
||||
[2 7 3 'y]
|
||||
[3 7 4 'z]])
|
||||
@[[0 6 0 'arg]
|
||||
[0 6 1 'a]
|
||||
[0 6 2 'x]
|
||||
[1 6 3 'y]
|
||||
[2 6 4 'z]])
|
||||
"arg & inner symbolslots")
|
||||
|
||||
# buffer/push-at
|
||||
@@ -45,4 +45,6 @@
|
||||
(assert (deep= @"abc456789" (buffer/push-at @"abc123" 3 "456789")) "buffer/push-at 2")
|
||||
(assert (deep= @"abc423" (buffer/push-at @"abc123" 3 "4")) "buffer/push-at 3")
|
||||
|
||||
(assert (= 10 (do (var x 10) (def y x) (++ x) y)) "no invalid aliasing")
|
||||
|
||||
(end-suite)
|
||||
|
||||
Reference in New Issue
Block a user