mirror of
https://github.com/janet-lang/janet
synced 2025-10-27 21:57:41 +00:00
Compare commits
79 Commits
v1.18.1
...
1.19.1-rc1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a0af8caad | ||
|
|
8ff2fecb26 | ||
|
|
1855c6aed5 | ||
|
|
d4c6643311 | ||
|
|
e8c738002b | ||
|
|
309c3aaeb8 | ||
|
|
1f8bcadb3b | ||
|
|
6f4af5fef8 | ||
|
|
868cdb9f8b | ||
|
|
2f76a429ef | ||
|
|
a69799aa42 | ||
|
|
139bef2142 | ||
|
|
8ba142bcf4 | ||
|
|
c49e4966f6 | ||
|
|
516fa4e49d | ||
|
|
6bf9f89429 | ||
|
|
a0ddfcb109 | ||
|
|
3df7921fdc | ||
|
|
6172a9ca2d | ||
|
|
4a40e57cf0 | ||
|
|
cdedda4ca1 | ||
|
|
e6babd84f7 | ||
|
|
868ec1a7e3 | ||
|
|
e08394c870 | ||
|
|
a99500aebf | ||
|
|
aa5095c23b | ||
|
|
9e0f36e5a7 | ||
|
|
d481d079ba | ||
|
|
bc9ec7ac4a | ||
|
|
6f7e81067c | ||
|
|
af946f398e | ||
|
|
c7ca26e9c7 | ||
|
|
ef7129f45d | ||
|
|
a20bdd334a | ||
|
|
2ef49a92cc | ||
|
|
75f56b68c6 | ||
|
|
d34d319d89 | ||
|
|
6660c1da38 | ||
|
|
4e263b8c39 | ||
|
|
3cb604df02 | ||
|
|
af9dc7a69e | ||
|
|
1247e69c78 | ||
|
|
aab0e4315d | ||
|
|
14f6517733 | ||
|
|
5d75effb37 | ||
|
|
ab4f18954b | ||
|
|
e1460c65e8 | ||
|
|
425a0fcf07 | ||
|
|
7205ee5e0a | ||
|
|
72c5db8910 | ||
|
|
3067f4be3a | ||
|
|
2aa1ccdd76 | ||
|
|
0284df503f | ||
|
|
2833a983d8 | ||
|
|
39c6be7cb7 | ||
|
|
fdc94c1353 | ||
|
|
9cc4e48124 | ||
|
|
34c7f15d6d | ||
|
|
899a9b025e | ||
|
|
deb4315383 | ||
|
|
9a06660fdb | ||
|
|
5c35d24e13 | ||
|
|
03f99752a7 | ||
|
|
fd37567c18 | ||
|
|
6e38bf1578 | ||
|
|
8b2d278840 | ||
|
|
06aa0a124d | ||
|
|
eb4595158d | ||
|
|
32103441f1 | ||
|
|
7ed0aa6630 | ||
|
|
f690229f31 | ||
|
|
f3bab72a86 | ||
|
|
2bd63c2d27 | ||
|
|
545d9e85e9 | ||
|
|
21a4ab4ec7 | ||
|
|
66fbbeb5ec | ||
|
|
55879c7b6d | ||
|
|
fab65d6c40 | ||
|
|
4d983e54b5 |
19
.github/workflows/release.yml
vendored
19
.github/workflows/release.yml
vendored
@@ -33,3 +33,22 @@ jobs:
|
||||
build/janet.h
|
||||
build/c/janet.c
|
||||
build/c/shell.c
|
||||
|
||||
release-windows:
|
||||
name: Build release binaries for windows
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
uses: actions/checkout@master
|
||||
- name: Setup MSVC
|
||||
uses: ilammy/msvc-dev-cmd@v1
|
||||
- name: Build the project
|
||||
shell: cmd
|
||||
run: build_win all
|
||||
- name: Draft the release
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
draft: true
|
||||
files: |
|
||||
./*.zip
|
||||
./*.msi
|
||||
|
||||
20
CHANGELOG.md
20
CHANGELOG.md
@@ -1,6 +1,26 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## Unreleased - ???
|
||||
- Add an optional `prefix` paramater to `debug/stacktrace` to allow printing prettier error messages.
|
||||
- Remove appveyor for CI pipeline
|
||||
- Fixed a bug that prevented sending threaded abstracts over threaded channels.
|
||||
- Fix bug in the `map` function with arity at least 3.
|
||||
|
||||
## 1.19.0 - 2021-11-27
|
||||
- Add `math/log-gamma` to replace `math/gamma`, and change `math/gamma` to be the expected gamma function.
|
||||
- Fix leaking file-descriptors in os/spawn and os/execute.
|
||||
- Ctrl-C will now raise SIGINT.
|
||||
- Allow quoted literals in the `match` macro to behave as expected in patterns.
|
||||
- Fix windows net related bug for TCP servers.
|
||||
- Allow evaluating ev streams with dofile.
|
||||
- Fix `ev` related bug with operations on already closed file descriptors.
|
||||
- Add struct and table agnostic `getproto` function.
|
||||
- Add a number of functions related to structs.
|
||||
- Add prototypes to structs. Structs can now inherit from other structs, just like tables.
|
||||
- Create a struct with a prototype with `struct/with-proto`.
|
||||
- Deadlocked channels will no longer exit early - instead they will hang, which is more intuitive.
|
||||
|
||||
## 1.18.1 - 2021-10-16
|
||||
- Fix some documentation typos
|
||||
- Fix - Set pipes passed to subprocess to blocking mode.
|
||||
|
||||
5
Makefile
5
Makefile
@@ -62,15 +62,18 @@ ifeq ($(UNAME), Darwin)
|
||||
else ifeq ($(UNAME), Linux)
|
||||
CLIBS:=$(CLIBS) -lrt -ldl
|
||||
endif
|
||||
|
||||
# For other unix likes, add flags here!
|
||||
ifeq ($(UNAME), Haiku)
|
||||
LDCONFIG:=true
|
||||
LDFLAGS=-Wl,--export-dynamic
|
||||
endif
|
||||
# For Android (termux)
|
||||
ifeq ($(UNAME), Linux) # uname on Darwin doesn't recognise -o
|
||||
ifeq ($(shell uname -o), Android)
|
||||
CLIBS:=$(CLIBS) -landroid-spawn
|
||||
endif
|
||||
endif
|
||||
|
||||
$(shell mkdir -p build/core build/c build/boot)
|
||||
all: $(JANET_TARGET) $(JANET_LIBRARY) $(JANET_STATIC_LIBRARY) build/janet.h
|
||||
@@ -162,7 +165,7 @@ build/c/janet.c: build/janet_boot src/boot/boot.janet
|
||||
##### Amalgamation #####
|
||||
########################
|
||||
|
||||
SONAME=libjanet.so.1.18
|
||||
SONAME=libjanet.so.1.19
|
||||
|
||||
build/c/shell.c: src/mainclient/shell.c
|
||||
cp $< $@
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
[](https://gitter.im/janet-language/community)
|
||||
|
||||
[](https://ci.appveyor.com/project/bakpakin/janet/branch/master)
|
||||
[](https://builds.sr.ht/~bakpakin/janet/commits/freebsd.yml?)
|
||||
[](https://builds.sr.ht/~bakpakin/janet/commits/openbsd.yml?)
|
||||
[](https://github.com/janet-lang/janet/actions/workflows/test.yml)
|
||||
@@ -257,7 +256,7 @@ Nope. There are no cons cells here.
|
||||
|
||||
### Is this a Clojure port?
|
||||
|
||||
No. It's similar to Clojure superficially because I like Lisps and I like the asthetics.
|
||||
No. It's similar to Clojure superficially because I like Lisps and I like the aesthetics.
|
||||
Internally, Janet is not at all like Clojure.
|
||||
|
||||
### Are the immutable data structures (tuples and structs) implemented as hash tries?
|
||||
|
||||
51
appveyor.yml
51
appveyor.yml
@@ -1,51 +0,0 @@
|
||||
version: build-{build}
|
||||
clone_folder: c:\projects\janet
|
||||
image:
|
||||
- Visual Studio 2019
|
||||
configuration:
|
||||
- Release
|
||||
platform:
|
||||
- x64
|
||||
- x86
|
||||
environment:
|
||||
matrix:
|
||||
- arch: Win64
|
||||
matrix:
|
||||
fast_finish: true
|
||||
|
||||
# skip unsupported combinations
|
||||
init:
|
||||
- call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" %platform%
|
||||
|
||||
install:
|
||||
- set JANET_BUILD=%appveyor_repo_commit:~0,7%
|
||||
- build_win all
|
||||
- set janet_outname=%appveyor_repo_tag_name%
|
||||
- if "%janet_outname%"=="" set /P janet_outname=<build\version.txt
|
||||
build: off
|
||||
|
||||
artifacts:
|
||||
- name: janet.c
|
||||
path: dist\janet.c
|
||||
type: File
|
||||
- name: janet.h
|
||||
path: dist\janet.h
|
||||
type: File
|
||||
- name: shell.c
|
||||
path: dist\shell.c
|
||||
type: File
|
||||
- name: "janet-$(janet_outname)-windows-%platform%"
|
||||
path: dist
|
||||
type: Zip
|
||||
- path: "janet-$(janet_outname)-windows-%platform%-installer.msi"
|
||||
type: File
|
||||
|
||||
deploy:
|
||||
description: 'The Janet Programming Language.'
|
||||
provider: GitHub
|
||||
auth_token:
|
||||
secure: lwEXy09qhj2jSH9s1C/KvCkAUqJSma8phFR+0kbsfUc3rVxpNK5uD3z9Md0SjYRx
|
||||
artifact: /(janet|shell).*/
|
||||
draft: true
|
||||
on:
|
||||
APPVEYOR_REPO_TAG: true
|
||||
@@ -1,10 +1,10 @@
|
||||
# An example of using Janet's extensible module system
|
||||
# to import files from URL. To try this, run `janet -l examples/urlloader.janet`
|
||||
# from the repl, and then:
|
||||
# An example of using Janet's extensible module system to import files from
|
||||
# URL. To try this, run `janet -l ./examples/urlloader.janet` from the command
|
||||
# line, and then at the REPL type:
|
||||
#
|
||||
# (import https://raw.githubusercontent.com/janet-lang/janet/master/examples/colors.janet :as c)
|
||||
#
|
||||
# This will import a file using curl. You can then try
|
||||
# This will import a file using curl. You can then try:
|
||||
#
|
||||
# (print (c/color :green "Hello!"))
|
||||
#
|
||||
@@ -13,9 +13,9 @@
|
||||
|
||||
(defn- load-url
|
||||
[url args]
|
||||
(def f (file/popen (string "curl " url)))
|
||||
(def res (dofile f :source url ;args))
|
||||
(try (file/close f) ([err] nil))
|
||||
(def p (os/spawn ["curl" url "-s"] :p {:out :pipe}))
|
||||
(def res (dofile (p :out) :source url ;args))
|
||||
(:wait p)
|
||||
res)
|
||||
|
||||
(defn- check-http-url
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
project('janet', 'c',
|
||||
default_options : ['c_std=c99', 'build.c_std=c99', 'b_lundef=false', 'default_library=both'],
|
||||
version : '1.18.1')
|
||||
version : '1.19.1')
|
||||
|
||||
# Global settings
|
||||
janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet')
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
``Use a function or macro literal `f` as a macro. This lets
|
||||
any function be used as a macro. Inside a quasiquote, the
|
||||
idiom `(as-macro ,my-custom-macro arg1 arg2...)` can be used
|
||||
to avoid unwanted variable capture.``
|
||||
to avoid unwanted variable capture of `my-custom-macro`.``
|
||||
[f & args]
|
||||
(f ;args))
|
||||
|
||||
@@ -698,6 +698,14 @@
|
||||
"Returns the numeric minimum of the arguments."
|
||||
[& args] (extreme < args))
|
||||
|
||||
(defn max-of
|
||||
"Returns the numeric maximum of the argument sequence."
|
||||
[args] (extreme > args))
|
||||
|
||||
(defn min-of
|
||||
"Returns the numeric minimum of the argument sequence."
|
||||
[args] (extreme < args))
|
||||
|
||||
(defn first
|
||||
"Get the first element from an indexed data structure."
|
||||
[xs]
|
||||
@@ -900,7 +908,7 @@
|
||||
(while true
|
||||
(if (= nil (set k1 (next i1 k1))) (break))
|
||||
(if (= nil (set k2 (next i2 k2))) (break))
|
||||
(if (= nil (set k3 (next i2 k3))) (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)
|
||||
@@ -910,8 +918,8 @@
|
||||
(while true
|
||||
(if (= nil (set k1 (next i1 k1))) (break))
|
||||
(if (= nil (set k2 (next i2 k2))) (break))
|
||||
(if (= nil (set k3 (next i2 k3))) (break))
|
||||
(if (= nil (set k4 (next i2 k4))) (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))
|
||||
@@ -1677,6 +1685,8 @@
|
||||
already bound to `<sym>`, rather than matching and rebinding it.
|
||||
|
||||
Any other value pattern will only match if it is equal to `x`.
|
||||
Quoting a pattern with `'` will also treat the value as a literal value to match against.
|
||||
|
||||
```
|
||||
[x & cases]
|
||||
|
||||
@@ -1728,6 +1738,10 @@
|
||||
(array/push x s)
|
||||
(put b2g pattern @[s]))
|
||||
|
||||
# match quoted literal
|
||||
(and (= t :tuple) (= 2 (length pattern)) (= 'quote (pattern 0)))
|
||||
(break)
|
||||
|
||||
# match data structure template
|
||||
(or isarr (= t :struct) (= t :table))
|
||||
(do
|
||||
@@ -1765,6 +1779,10 @@
|
||||
# match local binding
|
||||
(= t :symbol) (break)
|
||||
|
||||
# match quoted literal
|
||||
(and (= t :tuple) (= 2 (length pattern)) (= 'quote (pattern 0)))
|
||||
(array/push anda ['= s pattern])
|
||||
|
||||
# match global unification
|
||||
(and (= t :tuple) (= 2 (length pattern)) (= '@ (pattern 0)))
|
||||
(if-let [x (in gun (pattern 1))]
|
||||
@@ -2047,7 +2065,7 @@
|
||||
```
|
||||
Shorthand for fn. Arguments are given as $n, where n is the 0-indexed
|
||||
argument of the function. $ is also an alias for the first (index 0) argument.
|
||||
The $& symbol will make the anonymous function variadic if it apears in the
|
||||
The $& symbol will make the anonymous function variadic if it appears in the
|
||||
body of the function - it can be combined with positional arguments.
|
||||
|
||||
Example usage:
|
||||
@@ -2205,7 +2223,7 @@
|
||||
col
|
||||
": compile error: ")
|
||||
(if macrof
|
||||
(debug/stacktrace macrof msg)
|
||||
(debug/stacktrace macrof msg "")
|
||||
(eprint msg))
|
||||
(when ec
|
||||
(print-line-col where line col)
|
||||
@@ -2269,11 +2287,14 @@
|
||||
(default on-compile-warning warn-compile)
|
||||
(default on-parse-error bad-parse)
|
||||
(default evaluator (fn evaluate [x &] (x)))
|
||||
(default default-where "<anonymous>")
|
||||
(default guard :ydt)
|
||||
|
||||
(var where default-where)
|
||||
|
||||
(if where
|
||||
(put env :current-file where)
|
||||
(set where "<anonymous>"))
|
||||
|
||||
# Evaluate 1 source form in a protected manner
|
||||
(def lints @[])
|
||||
(defn eval1 [source &opt l c]
|
||||
@@ -2352,8 +2373,12 @@
|
||||
|
||||
[:source new-where]
|
||||
(if (string? new-where)
|
||||
(set where new-where)
|
||||
(set where default-where))
|
||||
(do
|
||||
(set where new-where)
|
||||
(put env :current-file new-where))
|
||||
(do
|
||||
(set where default-where)
|
||||
(put env :current-file nil)))
|
||||
|
||||
(do
|
||||
(var pindex 0)
|
||||
@@ -2582,7 +2607,7 @@
|
||||
@{})
|
||||
|
||||
(defn dofile
|
||||
`Evaluate a file and return the resulting environment. :env, :expander,
|
||||
`Evaluate a file, file path, or stream and return the resulting environment. :env, :expander,
|
||||
:evaluator, :read, and :parser are passed through to the underlying
|
||||
run-context call. If exit is true, any top level errors will trigger a
|
||||
call to (os/exit 1) after printing the error.`
|
||||
@@ -2594,8 +2619,9 @@
|
||||
:evaluator evaluator
|
||||
:read read
|
||||
:parser parser}]
|
||||
(def f (if (= (type path) :core/file)
|
||||
path
|
||||
(def f (case (type path)
|
||||
:core/file path
|
||||
:core/stream path
|
||||
(file/open path :rb)))
|
||||
(def path-is-file (= f path))
|
||||
(default env (make-env))
|
||||
@@ -2604,7 +2630,7 @@
|
||||
(put env :source (or src (if-not path-is-file spath path)))
|
||||
(var exit-error nil)
|
||||
(var exit-fiber nil)
|
||||
(defn chunks [buf _] (file/read f 4096 buf))
|
||||
(defn chunks [buf _] (:read f 4096 buf))
|
||||
(defn bp [&opt x y]
|
||||
(when exit
|
||||
(bad-parse x y)
|
||||
@@ -2634,8 +2660,7 @@
|
||||
:on-status (fn [f x]
|
||||
(when (not= (fiber/status f) :dead)
|
||||
(when exit
|
||||
(eprint x)
|
||||
(debug/stacktrace f)
|
||||
(debug/stacktrace f x "")
|
||||
(eflush)
|
||||
(os/exit 1))
|
||||
(put env :exit true)
|
||||
@@ -2646,7 +2671,7 @@
|
||||
:read read
|
||||
:parser parser
|
||||
:source (or src (if path-is-file "<anonymous>" spath))}))
|
||||
(if-not path-is-file (file/close f))
|
||||
(if-not path-is-file (:close f))
|
||||
(when exit-error
|
||||
(if exit-fiber
|
||||
(propagate exit-error exit-fiber)
|
||||
@@ -3142,7 +3167,7 @@
|
||||
"Print the current fiber stack"
|
||||
[]
|
||||
(print)
|
||||
(with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal)))
|
||||
(with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal) ""))
|
||||
(print))
|
||||
|
||||
(defn .frame
|
||||
@@ -3339,9 +3364,7 @@
|
||||
(printf (get e :pretty-format "%q") x)
|
||||
(flush))
|
||||
(do
|
||||
(def ec (dyn :err-color))
|
||||
(eprint (if ec "\e[31m" "") fs ": " x)
|
||||
(debug/stacktrace f)
|
||||
(debug/stacktrace f x "")
|
||||
(eflush)
|
||||
(if (e :debug) (enter-debugger f x))))))
|
||||
|
||||
@@ -3495,8 +3518,7 @@
|
||||
(try
|
||||
(dofile path :evaluator flycheck-evaluator ;(kvs kwargs))
|
||||
([e f]
|
||||
(eprint e)
|
||||
(debug/stacktrace f)))
|
||||
(debug/stacktrace f e "")))
|
||||
nil)
|
||||
|
||||
###
|
||||
@@ -3681,15 +3703,6 @@
|
||||
|
||||
(do
|
||||
|
||||
(defn proto-flatten
|
||||
"Flatten a table and its prototypes into a single table."
|
||||
[into x]
|
||||
(when x
|
||||
(proto-flatten into (table/getproto x))
|
||||
(loop [k :keys x]
|
||||
(put into k (x k))))
|
||||
into)
|
||||
|
||||
# Deprecate file/popen
|
||||
(when-let [v (get root-env 'file/popen)]
|
||||
(put v :deprecated true))
|
||||
@@ -3698,7 +3711,7 @@
|
||||
# flatten nested tables.
|
||||
(loop [[k v] :in (pairs root-env)
|
||||
:when (symbol? k)]
|
||||
(def flat (proto-flatten @{} v))
|
||||
(def flat (table/proto-flatten v))
|
||||
(when (boot/config :no-docstrings)
|
||||
(put flat :doc nil))
|
||||
(when (boot/config :no-sourcemaps)
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
#define JANETCONF_H
|
||||
|
||||
#define JANET_VERSION_MAJOR 1
|
||||
#define JANET_VERSION_MINOR 18
|
||||
#define JANET_VERSION_MINOR 19
|
||||
#define JANET_VERSION_PATCH 1
|
||||
#define JANET_VERSION_EXTRA ""
|
||||
#define JANET_VERSION "1.18.1"
|
||||
#define JANET_VERSION_EXTRA "-dev"
|
||||
#define JANET_VERSION "1.19.1-dev"
|
||||
|
||||
/* #define JANET_BUILD "local" */
|
||||
|
||||
|
||||
@@ -63,8 +63,8 @@ void *janet_abstract_begin_threaded(const JanetAbstractType *atype, size_t size)
|
||||
}
|
||||
janet_vm.next_collection += size + sizeof(JanetAbstractHead);
|
||||
header->gc.flags = JANET_MEMORY_THREADED_ABSTRACT;
|
||||
header->gc.next = NULL; /* Clear memory for address sanitizers */
|
||||
header->gc.refcount = 1;
|
||||
header->gc.data.next = NULL; /* Clear memory for address sanitizers */
|
||||
header->gc.data.refcount = 1;
|
||||
header->size = size;
|
||||
header->type = atype;
|
||||
void *abstract = (void *) & (header->data);
|
||||
@@ -86,11 +86,11 @@ void *janet_abstract_threaded(const JanetAbstractType *atype, size_t size) {
|
||||
#ifdef JANET_WINDOWS
|
||||
|
||||
static int32_t janet_incref(JanetAbstractHead *ab) {
|
||||
return InterlockedIncrement(&ab->gc.refcount);
|
||||
return InterlockedIncrement(&ab->gc.data.refcount);
|
||||
}
|
||||
|
||||
static int32_t janet_decref(JanetAbstractHead *ab) {
|
||||
return InterlockedDecrement(&ab->gc.refcount);
|
||||
return InterlockedDecrement(&ab->gc.data.refcount);
|
||||
}
|
||||
|
||||
void janet_os_mutex_init(JanetOSMutex *mutex) {
|
||||
@@ -112,11 +112,11 @@ void janet_os_mutex_unlock(JanetOSMutex *mutex) {
|
||||
#else
|
||||
|
||||
static int32_t janet_incref(JanetAbstractHead *ab) {
|
||||
return __atomic_add_fetch(&ab->gc.refcount, 1, __ATOMIC_RELAXED);
|
||||
return __atomic_add_fetch(&ab->gc.data.refcount, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static int32_t janet_decref(JanetAbstractHead *ab) {
|
||||
return __atomic_add_fetch(&ab->gc.refcount, -1, __ATOMIC_RELAXED);
|
||||
return __atomic_add_fetch(&ab->gc.data.refcount, -1, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
void janet_os_mutex_init(JanetOSMutex *mutex) {
|
||||
|
||||
@@ -465,6 +465,25 @@ JANET_CORE_FN(janet_core_table,
|
||||
return janet_wrap_table(table);
|
||||
}
|
||||
|
||||
JANET_CORE_FN(janet_core_getproto,
|
||||
"(getproto x)",
|
||||
"Get the prototype of a table or struct. Will return nil if `x` has no prototype.") {
|
||||
janet_fixarity(argc, 1);
|
||||
if (janet_checktype(argv[0], JANET_TABLE)) {
|
||||
JanetTable *t = janet_unwrap_table(argv[0]);
|
||||
return t->proto
|
||||
? janet_wrap_table(t->proto)
|
||||
: janet_wrap_nil();
|
||||
}
|
||||
if (janet_checktype(argv[0], JANET_STRUCT)) {
|
||||
JanetStruct st = janet_unwrap_struct(argv[0]);
|
||||
return janet_struct_proto(st)
|
||||
? janet_wrap_struct(janet_struct_proto(st))
|
||||
: janet_wrap_nil();
|
||||
}
|
||||
janet_panicf("expected struct|table, got %v", argv[0]);
|
||||
}
|
||||
|
||||
JANET_CORE_FN(janet_core_struct,
|
||||
"(struct & kvs)",
|
||||
"Create a new struct from a sequence of key value pairs. "
|
||||
@@ -472,8 +491,9 @@ JANET_CORE_FN(janet_core_struct,
|
||||
"an odd number of elements, an error will be thrown. Returns the "
|
||||
"new struct.") {
|
||||
int32_t i;
|
||||
if (argc & 1)
|
||||
if (argc & 1) {
|
||||
janet_panic("expected even number of arguments");
|
||||
}
|
||||
JanetKV *st = janet_struct_begin(argc >> 1);
|
||||
for (i = 0; i < argc; i += 2) {
|
||||
janet_struct_put(st, argv[i], argv[i + 1]);
|
||||
@@ -960,6 +980,7 @@ static void janet_load_libs(JanetTable *env) {
|
||||
JANET_CORE_REG("nat?", janet_core_check_nat),
|
||||
JANET_CORE_REG("slice", janet_core_slice),
|
||||
JANET_CORE_REG("signal", janet_core_signal),
|
||||
JANET_CORE_REG("getproto", janet_core_getproto),
|
||||
JANET_REG_END
|
||||
};
|
||||
janet_core_cfuns_ext(env, NULL, corelib_cfuns);
|
||||
@@ -969,6 +990,7 @@ static void janet_load_libs(JanetTable *env) {
|
||||
janet_lib_tuple(env);
|
||||
janet_lib_buffer(env);
|
||||
janet_lib_table(env);
|
||||
janet_lib_struct(env);
|
||||
janet_lib_fiber(env);
|
||||
janet_lib_os(env);
|
||||
janet_lib_parse(env);
|
||||
|
||||
@@ -86,7 +86,7 @@ void janet_debug_find(
|
||||
}
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
current = current->data.next;
|
||||
}
|
||||
if (best_def) {
|
||||
*def_out = best_def;
|
||||
@@ -96,15 +96,18 @@ void janet_debug_find(
|
||||
}
|
||||
}
|
||||
|
||||
void janet_stacktrace(JanetFiber *fiber, Janet err) {
|
||||
janet_stacktrace_ext(fiber, err, NULL);
|
||||
}
|
||||
|
||||
/* Error reporting. This can be emulated from within Janet, but for
|
||||
* consitency with the top level code it is defined once. */
|
||||
void janet_stacktrace(JanetFiber *fiber, Janet err) {
|
||||
void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix) {
|
||||
|
||||
int32_t fi;
|
||||
const char *errstr = (const char *)janet_to_string(err);
|
||||
JanetFiber **fibers = NULL;
|
||||
|
||||
/* Don't print error line if it is nil. */
|
||||
int wrote_error = janet_checktype(err, JANET_NIL);
|
||||
int wrote_error = !prefix;
|
||||
|
||||
int print_color = janet_truthy(janet_dyn("err-color"));
|
||||
if (print_color) janet_eprintf("\x1b[31m");
|
||||
@@ -126,9 +129,9 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
|
||||
/* Print prelude to stack frame */
|
||||
if (!wrote_error) {
|
||||
JanetFiberStatus status = janet_fiber_status(fiber);
|
||||
const char *prefix = status == JANET_STATUS_ERROR ? "" : "status ";
|
||||
const char *override_prefix = prefix ? prefix : "";
|
||||
janet_eprintf("%s%s: %s\n",
|
||||
prefix,
|
||||
override_prefix,
|
||||
janet_status_names[status],
|
||||
errstr);
|
||||
wrote_error = 1;
|
||||
@@ -361,14 +364,15 @@ JANET_CORE_FN(cfun_debug_stack,
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_debug_stacktrace,
|
||||
"(debug/stacktrace fiber &opt err)",
|
||||
"(debug/stacktrace fiber &opt err prefix)",
|
||||
"Prints a nice looking stacktrace for a fiber. Can optionally provide "
|
||||
"an error value to print the stack trace with. If `err` is nil or not "
|
||||
"provided, will skip the error line. Returns the fiber.") {
|
||||
janet_arity(argc, 1, 2);
|
||||
"provided, and no prefix is given, will skip the error line. Returns the fiber.") {
|
||||
janet_arity(argc, 1, 3);
|
||||
JanetFiber *fiber = janet_getfiber(argv, 0);
|
||||
Janet x = argc == 1 ? janet_wrap_nil() : argv[1];
|
||||
janet_stacktrace(fiber, x);
|
||||
const char *prefix = janet_optcstring(argv, argc, 2, NULL);
|
||||
janet_stacktrace_ext(fiber, x, prefix);
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
/* The above copyright notice and this permission notice shall be included in
|
||||
/*
|
||||
* Copyright (c) 2021 Calvin Rose
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
@@ -10,6 +20,7 @@
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef JANET_AMALG
|
||||
#include "features.h"
|
||||
#include <janet.h>
|
||||
@@ -223,6 +234,9 @@ static void add_timeout(JanetTimeout to) {
|
||||
|
||||
/* Create a new event listener */
|
||||
static JanetListenerState *janet_listen_impl(JanetStream *stream, JanetListener behavior, int mask, size_t size, void *user) {
|
||||
if (stream->flags & JANET_STREAM_CLOSED) {
|
||||
janet_panic("cannot listen on closed stream");
|
||||
}
|
||||
if (stream->_mask & mask) {
|
||||
janet_panic("cannot listen for duplicate event on stream");
|
||||
}
|
||||
@@ -333,8 +347,10 @@ static void janet_stream_close_impl(JanetStream *stream, int is_gc) {
|
||||
{
|
||||
CloseHandle(stream->handle);
|
||||
}
|
||||
stream->handle = INVALID_HANDLE_VALUE;
|
||||
#else
|
||||
close(stream->handle);
|
||||
stream->handle = -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -448,7 +464,9 @@ const JanetAbstractType janet_stream_type = {
|
||||
|
||||
/* Register a fiber to resume with value */
|
||||
void janet_schedule_signal(JanetFiber *fiber, Janet value, JanetSignal sig) {
|
||||
if (fiber->flags & JANET_FIBER_FLAG_CANCELED) return;
|
||||
JanetTask t = { fiber, value, sig, ++fiber->sched_id };
|
||||
if (sig == JANET_SIGNAL_ERROR) fiber->flags |= JANET_FIBER_FLAG_CANCELED;
|
||||
janet_q_push(&janet_vm.spawn, &t, sizeof(t));
|
||||
}
|
||||
|
||||
@@ -602,7 +620,7 @@ static int janet_chan_unpack(JanetChannel *chan, Janet *x, int is_cleanup) {
|
||||
return 1;
|
||||
case JANET_BUFFER: {
|
||||
JanetBuffer *buf = janet_unwrap_buffer(*x);
|
||||
int flags = is_cleanup ? JANET_MARSHAL_UNSAFE : (JANET_MARSHAL_UNSAFE | JANET_MARSHAL_DECREF);
|
||||
int flags = is_cleanup ? (JANET_MARSHAL_UNSAFE | JANET_MARSHAL_DECREF) : JANET_MARSHAL_UNSAFE;
|
||||
*x = janet_unmarshal(buf->data, buf->count, flags, NULL, NULL);
|
||||
janet_buffer_deinit(buf);
|
||||
janet_free(buf);
|
||||
@@ -819,8 +837,8 @@ static int janet_channel_push(JanetChannel *channel, Janet x, int mode) {
|
||||
pending.mode = mode ? JANET_CP_MODE_CHOICE_WRITE : JANET_CP_MODE_WRITE;
|
||||
janet_q_push(&channel->write_pending, &pending, sizeof(pending));
|
||||
janet_chan_unlock(channel);
|
||||
janet_ev_inc_refcount();
|
||||
if (is_threaded) {
|
||||
janet_ev_inc_refcount();
|
||||
janet_gcroot(janet_wrap_fiber(pending.fiber));
|
||||
}
|
||||
return 1;
|
||||
@@ -837,6 +855,7 @@ static int janet_channel_push(JanetChannel *channel, Janet x, int mode) {
|
||||
msg.argj = x;
|
||||
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
||||
} else {
|
||||
janet_ev_dec_refcount();
|
||||
if (reader.mode == JANET_CP_MODE_CHOICE_READ) {
|
||||
janet_schedule(reader.fiber, make_read_result(channel, x));
|
||||
} else {
|
||||
@@ -869,8 +888,8 @@ static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice)
|
||||
pending.mode = is_choice ? JANET_CP_MODE_CHOICE_READ : JANET_CP_MODE_READ;
|
||||
janet_q_push(&channel->read_pending, &pending, sizeof(pending));
|
||||
janet_chan_unlock(channel);
|
||||
janet_ev_inc_refcount();
|
||||
if (is_threaded) {
|
||||
janet_ev_inc_refcount();
|
||||
janet_gcroot(janet_wrap_fiber(pending.fiber));
|
||||
}
|
||||
return 0;
|
||||
@@ -888,6 +907,7 @@ static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice)
|
||||
msg.argj = janet_wrap_nil();
|
||||
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
||||
} else {
|
||||
janet_ev_dec_refcount();
|
||||
if (writer.mode == JANET_CP_MODE_CHOICE_WRITE) {
|
||||
janet_schedule(writer.fiber, make_write_result(channel));
|
||||
} else {
|
||||
@@ -1091,6 +1111,7 @@ JANET_CORE_FN(cfun_channel_close,
|
||||
msg.argj = janet_wrap_nil();
|
||||
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
||||
} else {
|
||||
janet_ev_dec_refcount();
|
||||
if (writer.mode == JANET_CP_MODE_CHOICE_WRITE) {
|
||||
janet_schedule(writer.fiber, janet_wrap_nil());
|
||||
} else {
|
||||
@@ -1110,6 +1131,7 @@ JANET_CORE_FN(cfun_channel_close,
|
||||
msg.argj = janet_wrap_nil();
|
||||
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
||||
} else {
|
||||
janet_ev_dec_refcount();
|
||||
if (reader.mode == JANET_CP_MODE_CHOICE_READ) {
|
||||
janet_schedule(reader.fiber, janet_wrap_nil());
|
||||
} else {
|
||||
@@ -1180,13 +1202,13 @@ JanetFiber *janet_loop1(void) {
|
||||
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);
|
||||
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) {
|
||||
janet_cancel(to.fiber, janet_cstringv("deadline expired"));
|
||||
}
|
||||
@@ -1206,6 +1228,7 @@ JanetFiber *janet_loop1(void) {
|
||||
while (janet_vm.spawn.head != janet_vm.spawn.tail) {
|
||||
JanetTask task = {NULL, janet_wrap_nil(), JANET_SIGNAL_OK, 0};
|
||||
janet_q_pop(&janet_vm.spawn, &task, sizeof(task));
|
||||
task.fiber->flags &= ~JANET_FIBER_FLAG_CANCELED;
|
||||
if (task.expected_sched_id != task.fiber->sched_id) continue;
|
||||
Janet res;
|
||||
JanetSignal sig = janet_continue_signal(task.fiber, task.value, &res, task.sig);
|
||||
@@ -2618,6 +2641,8 @@ int janet_make_pipe(JanetHandle handles[2], int mode) {
|
||||
return 0;
|
||||
#else
|
||||
if (pipe(handles)) return -1;
|
||||
if (mode != 2 && fcntl(handles[0], F_SETFD, FD_CLOEXEC)) goto error;
|
||||
if (mode != 1 && fcntl(handles[1], F_SETFD, FD_CLOEXEC)) goto error;
|
||||
if (mode != 2 && fcntl(handles[0], F_SETFL, O_NONBLOCK)) goto error;
|
||||
if (mode != 1 && fcntl(handles[1], F_SETFL, O_NONBLOCK)) goto error;
|
||||
return 0;
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
|
||||
#define JANET_FIBER_STATUS_MASK 0x3F0000
|
||||
#define JANET_FIBER_RESUME_SIGNAL 0x400000
|
||||
#define JANET_FIBER_FLAG_CANCELED 0x400000
|
||||
#define JANET_FIBER_STATUS_OFFSET 16
|
||||
|
||||
#define JANET_FIBER_BREAKPOINT 0x1000000
|
||||
|
||||
@@ -162,10 +162,13 @@ recur: /* Manual tail recursion */
|
||||
}
|
||||
|
||||
static void janet_mark_struct(const JanetKV *st) {
|
||||
recur:
|
||||
if (janet_gc_reachable(janet_struct_head(st)))
|
||||
return;
|
||||
janet_gc_mark(janet_struct_head(st));
|
||||
janet_mark_kvs(st, janet_struct_capacity(st));
|
||||
st = janet_struct_proto(st);
|
||||
if (st) goto recur;
|
||||
}
|
||||
|
||||
static void janet_mark_tuple(const Janet *tuple) {
|
||||
@@ -323,7 +326,7 @@ void janet_sweep() {
|
||||
JanetGCObject *current = janet_vm.blocks;
|
||||
JanetGCObject *next;
|
||||
while (NULL != current) {
|
||||
next = current->next;
|
||||
next = current->data.next;
|
||||
if (current->flags & (JANET_MEM_REACHABLE | JANET_MEM_DISABLED)) {
|
||||
previous = current;
|
||||
current->flags &= ~JANET_MEM_REACHABLE;
|
||||
@@ -331,7 +334,7 @@ void janet_sweep() {
|
||||
janet_vm.block_count--;
|
||||
janet_deinit_block(current);
|
||||
if (NULL != previous) {
|
||||
previous->next = next;
|
||||
previous->data.next = next;
|
||||
} else {
|
||||
janet_vm.blocks = next;
|
||||
}
|
||||
@@ -395,7 +398,7 @@ void *janet_gcalloc(enum JanetMemoryType type, size_t size) {
|
||||
|
||||
/* Prepend block to heap list */
|
||||
janet_vm.next_collection += size;
|
||||
mem->next = janet_vm.blocks;
|
||||
mem->data.next = janet_vm.blocks;
|
||||
janet_vm.blocks = mem;
|
||||
janet_vm.block_count++;
|
||||
|
||||
@@ -532,7 +535,7 @@ void janet_clear_memory(void) {
|
||||
JanetGCObject *current = janet_vm.blocks;
|
||||
while (NULL != current) {
|
||||
janet_deinit_block(current);
|
||||
JanetGCObject *next = current->next;
|
||||
JanetGCObject *next = current->data.next;
|
||||
janet_free(current);
|
||||
current = next;
|
||||
}
|
||||
|
||||
@@ -483,6 +483,19 @@ static Janet cfun_io_print_impl_x(int32_t argc, Janet *argv, int newline,
|
||||
janet_buffer_push_u8(buf, '\n');
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
case JANET_FUNCTION: {
|
||||
/* Special case function */
|
||||
JanetFunction *fun = janet_unwrap_function(x);
|
||||
JanetBuffer *buf = janet_buffer(0);
|
||||
for (int32_t i = offset; i < argc; ++i) {
|
||||
janet_to_string_b(buf, argv[i]);
|
||||
}
|
||||
if (newline)
|
||||
janet_buffer_push_u8(buf, '\n');
|
||||
Janet args[1] = { janet_wrap_buffer(buf) };
|
||||
janet_call(fun, 1, args);
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
case JANET_NIL:
|
||||
f = dflt_file;
|
||||
if (f == NULL) janet_panic("cannot print to nil");
|
||||
|
||||
@@ -64,8 +64,9 @@ enum {
|
||||
LB_FUNCDEF_REF, /* 220 */
|
||||
LB_UNSAFE_CFUNCTION, /* 221 */
|
||||
LB_UNSAFE_POINTER, /* 222 */
|
||||
LB_STRUCT_PROTO, /* 223 */
|
||||
#ifdef JANET_EV
|
||||
LB_THREADED_ABSTRACT/* 223 */
|
||||
LB_THREADED_ABSTRACT/* 224 */
|
||||
#endif
|
||||
} LeadBytes;
|
||||
|
||||
@@ -542,8 +543,10 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
|
||||
int32_t count;
|
||||
const JanetKV *struct_ = janet_unwrap_struct(x);
|
||||
count = janet_struct_length(struct_);
|
||||
pushbyte(st, LB_STRUCT);
|
||||
pushbyte(st, janet_struct_proto(struct_) ? LB_STRUCT_PROTO : LB_STRUCT);
|
||||
pushint(st, count);
|
||||
if (janet_struct_proto(struct_))
|
||||
marshal_one(st, janet_wrap_struct(janet_struct_proto(struct_)), flags + 1);
|
||||
for (int32_t i = 0; i < janet_struct_capacity(struct_); i++) {
|
||||
if (janet_checktype(struct_[i].key, JANET_NIL))
|
||||
continue;
|
||||
@@ -1281,6 +1284,7 @@ static const uint8_t *unmarshal_one(
|
||||
case LB_ARRAY:
|
||||
case LB_TUPLE:
|
||||
case LB_STRUCT:
|
||||
case LB_STRUCT_PROTO:
|
||||
case LB_TABLE:
|
||||
case LB_TABLE_PROTO:
|
||||
/* Things that open with integers */
|
||||
@@ -1310,9 +1314,15 @@ static const uint8_t *unmarshal_one(
|
||||
}
|
||||
*out = janet_wrap_tuple(janet_tuple_end(tup));
|
||||
janet_v_push(st->lookup, *out);
|
||||
} else if (lead == LB_STRUCT) {
|
||||
} else if (lead == LB_STRUCT || lead == LB_STRUCT_PROTO) {
|
||||
/* Struct */
|
||||
JanetKV *struct_ = janet_struct_begin(len);
|
||||
if (lead == LB_STRUCT_PROTO) {
|
||||
Janet proto;
|
||||
data = unmarshal_one(st, data, &proto, flags + 1);
|
||||
janet_asserttype(proto, JANET_STRUCT);
|
||||
janet_struct_proto(struct_) = janet_unwrap_struct(proto);
|
||||
}
|
||||
for (int32_t i = 0; i < len; i++) {
|
||||
Janet key, value;
|
||||
data = unmarshal_one(st, data, &key, flags + 1);
|
||||
|
||||
@@ -286,7 +286,8 @@ JANET_DEFINE_MATHOP(fabs, 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, lgamma, "Returns gamma(x).")
|
||||
JANET_DEFINE_MATHOP(gamma, tgamma, "Returns gamma(x).")
|
||||
JANET_DEFINE_MATHOP(lgamma, 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.")
|
||||
@@ -309,6 +310,42 @@ JANET_CORE_FN(janet_not, "(not x)", "Returns the boolean inverse of x.") {
|
||||
return janet_wrap_boolean(!janet_truthy(argv[0]));
|
||||
}
|
||||
|
||||
static double janet_gcd(double x, double y) {
|
||||
if (isnan(x) || isnan(y)) {
|
||||
#ifdef NAN
|
||||
return NAN;
|
||||
#else
|
||||
return 0.0 \ 0.0;
|
||||
#endif
|
||||
}
|
||||
if (isinf(x) || isinf(y)) return INFINITY;
|
||||
while (y != 0) {
|
||||
double temp = y;
|
||||
y = fmod(x, y);
|
||||
x = temp;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
static double janet_lcm(double x, double y) {
|
||||
return (x / janet_gcd(x, y)) * y;
|
||||
}
|
||||
|
||||
JANET_CORE_FN(janet_cfun_gcd, "(math/gcd x y)",
|
||||
"Returns the greatest common divisor between x and y.") {
|
||||
janet_fixarity(argc, 2);
|
||||
double x = janet_getnumber(argv, 0);
|
||||
double y = janet_getnumber(argv, 1);
|
||||
return janet_wrap_number(janet_gcd(x, y));
|
||||
}
|
||||
|
||||
JANET_CORE_FN(janet_cfun_lcm, "(math/lcm x y)",
|
||||
"Returns the least common multiple of x and y.") {
|
||||
janet_fixarity(argc, 2);
|
||||
double x = janet_getnumber(argv, 0);
|
||||
double y = janet_getnumber(argv, 1);
|
||||
return janet_wrap_number(janet_lcm(x, y));
|
||||
}
|
||||
|
||||
/* Module entry point */
|
||||
void janet_lib_math(JanetTable *env) {
|
||||
@@ -347,12 +384,15 @@ void janet_lib_math(JanetTable *env) {
|
||||
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/log-gamma", janet_lgamma),
|
||||
JANET_CORE_REG("math/erfc", janet_erfc),
|
||||
JANET_CORE_REG("math/erf", janet_erf),
|
||||
JANET_CORE_REG("math/expm1", janet_expm1),
|
||||
JANET_CORE_REG("math/trunc", janet_trunc),
|
||||
JANET_CORE_REG("math/round", janet_round),
|
||||
JANET_CORE_REG("math/next", janet_nextafter),
|
||||
JANET_CORE_REG("math/gcd", janet_cfun_gcd),
|
||||
JANET_CORE_REG("math/lcm", janet_cfun_lcm),
|
||||
JANET_REG_END
|
||||
};
|
||||
janet_core_cfuns_ext(env, NULL, math_cfuns);
|
||||
|
||||
@@ -132,22 +132,20 @@ JanetAsyncStatus net_machine_accept(JanetListenerState *s, JanetAsyncEvent event
|
||||
case JANET_ASYNC_EVENT_MARK: {
|
||||
if (state->lstream) janet_mark(janet_wrap_abstract(state->lstream));
|
||||
if (state->astream) janet_mark(janet_wrap_abstract(state->astream));
|
||||
if (state->function) janet_mark(janet_wrap_abstract(state->function));
|
||||
if (state->function) janet_mark(janet_wrap_function(state->function));
|
||||
break;
|
||||
}
|
||||
case JANET_ASYNC_EVENT_CLOSE:
|
||||
janet_schedule(s->fiber, janet_wrap_nil());
|
||||
return JANET_ASYNC_STATUS_DONE;
|
||||
case JANET_ASYNC_EVENT_COMPLETE: {
|
||||
int seconds;
|
||||
int bytes = sizeof(seconds);
|
||||
if (NO_ERROR != getsockopt((SOCKET) state->astream->handle, SOL_SOCKET, SO_CONNECT_TIME,
|
||||
(char *)&seconds, &bytes)) {
|
||||
if (state->astream->flags & JANET_STREAM_CLOSED) {
|
||||
janet_cancel(s->fiber, janet_cstringv("failed to accept connection"));
|
||||
return JANET_ASYNC_STATUS_DONE;
|
||||
}
|
||||
SOCKET lsock = (SOCKET) state->lstream->handle;
|
||||
if (NO_ERROR != setsockopt((SOCKET) state->astream->handle, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
|
||||
(char *) & (state->lstream->handle), sizeof(SOCKET))) {
|
||||
(char *) &lsock, sizeof(lsock))) {
|
||||
janet_cancel(s->fiber, janet_cstringv("failed to accept connection"));
|
||||
return JANET_ASYNC_STATUS_DONE;
|
||||
}
|
||||
@@ -382,7 +380,7 @@ JANET_CORE_FN(cfun_net_connect,
|
||||
int is_unix = 0;
|
||||
char *bindhost = (char *) janet_optcstring(argv, argc, 3, NULL);
|
||||
char *bindport = NULL;
|
||||
if (janet_checkint(argv[4])) {
|
||||
if (argc >= 5 && janet_checkint(argv[4])) {
|
||||
bindport = (char *)janet_to_string(argv[4]);
|
||||
} else {
|
||||
bindport = (char *)janet_optcstring(argv, argc, 4, NULL);
|
||||
@@ -411,6 +409,7 @@ JANET_CORE_FN(cfun_net_connect,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Create socket */
|
||||
JSock sock = JSOCKDEFAULT;
|
||||
void *addr = NULL;
|
||||
@@ -431,7 +430,7 @@ JANET_CORE_FN(cfun_net_connect,
|
||||
struct addrinfo *rp = NULL;
|
||||
for (rp = ai; rp != NULL; rp = rp->ai_next) {
|
||||
#ifdef JANET_WINDOWS
|
||||
sock = WSASocketW(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
|
||||
sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
|
||||
#else
|
||||
sock = socket(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol);
|
||||
#endif
|
||||
@@ -649,7 +648,7 @@ struct sockaddr_in6 *sin6; // IPv6 address + port
|
||||
struct sockaddr_un *sun; // Unix Domain Socket Address
|
||||
*/
|
||||
|
||||
/* Turn a socket address into a host, port pair (port is optional).
|
||||
/* Turn a socket address into a host, port pair.
|
||||
* For unix domain sockets, returned tuple will have only a single element, the path string. */
|
||||
static Janet janet_so_getname(const void *sa_any) {
|
||||
const struct sockaddr *sa = sa_any;
|
||||
@@ -663,7 +662,7 @@ static Janet janet_so_getname(const void *sa_any) {
|
||||
janet_panic("unable to decode ipv4 host address");
|
||||
}
|
||||
Janet pair[2] = {janet_cstringv(buffer), janet_wrap_integer(ntohs(sai->sin_port))};
|
||||
return janet_wrap_tuple(janet_tuple_n(pair, sai->sin_port ? 2 : 1));
|
||||
return janet_wrap_tuple(janet_tuple_n(pair, 2));
|
||||
}
|
||||
case AF_INET6: {
|
||||
const struct sockaddr_in6 *sai6 = sa_any;
|
||||
@@ -671,7 +670,7 @@ static Janet janet_so_getname(const void *sa_any) {
|
||||
janet_panic("unable to decode ipv4 host address");
|
||||
}
|
||||
Janet pair[2] = {janet_cstringv(buffer), janet_wrap_integer(ntohs(sai6->sin6_port))};
|
||||
return janet_wrap_tuple(janet_tuple_n(pair, sai6->sin6_port ? 2 : 1));
|
||||
return janet_wrap_tuple(janet_tuple_n(pair, 2));
|
||||
}
|
||||
#ifndef JANET_WINDOWS
|
||||
case AF_UNIX: {
|
||||
@@ -695,6 +694,7 @@ JANET_CORE_FN(cfun_net_getsockname,
|
||||
"Gets the local address and port in a tuple in that order.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type);
|
||||
if (js->flags & JANET_STREAM_CLOSED) janet_panic("stream closed");
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t slen = sizeof(ss);
|
||||
memset(&ss, 0, slen);
|
||||
@@ -710,6 +710,7 @@ JANET_CORE_FN(cfun_net_getpeername,
|
||||
"Gets the remote peer's address and port in a tuple in that order.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type);
|
||||
if (js->flags & JANET_STREAM_CLOSED) janet_panic("stream closed");
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t slen = sizeof(ss);
|
||||
memset(&ss, 0, slen);
|
||||
|
||||
@@ -409,15 +409,13 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
|
||||
|
||||
#else /* windows check */
|
||||
|
||||
/* Function that is called in separate thread to wait on a pid */
|
||||
static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
|
||||
JanetProc *proc = (JanetProc *) args.argp;
|
||||
pid_t result;
|
||||
static int proc_get_status(JanetProc *proc) {
|
||||
/* Use POSIX shell semantics for interpreting signals */
|
||||
int status = 0;
|
||||
pid_t result;
|
||||
do {
|
||||
result = waitpid(proc->pid, &status, 0);
|
||||
} while (result == -1 && errno == EINTR);
|
||||
/* Use POSIX shell semantics for interpreting signals */
|
||||
if (WIFEXITED(status)) {
|
||||
status = WEXITSTATUS(status);
|
||||
} else if (WIFSTOPPED(status)) {
|
||||
@@ -425,7 +423,21 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
|
||||
} else {
|
||||
status = WTERMSIG(status) + 128;
|
||||
}
|
||||
args.argi = status;
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Function that is called in separate thread to wait on a pid */
|
||||
static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
|
||||
JanetProc *proc = (JanetProc *) args.argp;
|
||||
#ifdef WNOWAIT
|
||||
pid_t result;
|
||||
int status = 0;
|
||||
do {
|
||||
result = waitpid(proc->pid, &status, WNOWAIT);
|
||||
} while (result == -1 && errno == EINTR);
|
||||
#else
|
||||
args.tag = proc_get_status(proc);
|
||||
#endif
|
||||
return args;
|
||||
}
|
||||
|
||||
@@ -434,9 +446,13 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
|
||||
/* Callback that is called in main thread when subroutine completes. */
|
||||
static void janet_proc_wait_cb(JanetEVGenericMessage args) {
|
||||
janet_ev_dec_refcount();
|
||||
int status = args.argi;
|
||||
JanetProc *proc = (JanetProc *) args.argp;
|
||||
if (NULL != proc) {
|
||||
#ifdef WNOWAIT
|
||||
int status = proc_get_status(proc);
|
||||
#else
|
||||
int status = args.tag;
|
||||
#endif
|
||||
proc->return_code = (int32_t) status;
|
||||
proc->flags |= JANET_PROC_WAITED;
|
||||
proc->flags &= ~JANET_PROC_WAITING;
|
||||
@@ -469,7 +485,9 @@ static int janet_proc_gc(void *p, size_t s) {
|
||||
/* Kill and wait to prevent zombies */
|
||||
kill(proc->pid, SIGKILL);
|
||||
int status;
|
||||
waitpid(proc->pid, &status, 0);
|
||||
if (!(proc->flags & JANET_PROC_WAITING)) {
|
||||
waitpid(proc->pid, &status, 0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
@@ -540,7 +558,7 @@ JANET_CORE_FN(os_proc_wait,
|
||||
JANET_CORE_FN(os_proc_kill,
|
||||
"(os/proc-kill proc &opt wait)",
|
||||
"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 finsih and "
|
||||
"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);
|
||||
JanetProc *proc = janet_getabstract(argv, 0, &ProcAT);
|
||||
@@ -685,6 +703,12 @@ static int janet_proc_get(void *p, Janet key, Janet *out) {
|
||||
*out = (NULL == proc->err) ? janet_wrap_nil() : janet_wrap_abstract(proc->err);
|
||||
return 1;
|
||||
}
|
||||
#ifndef JANET_WINDOWS
|
||||
if (janet_keyeq(key, "pid")) {
|
||||
*out = janet_wrap_number(proc->pid);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
if ((-1 != proc->return_code) && janet_keyeq(key, "return-code")) {
|
||||
*out = janet_wrap_integer(proc->return_code);
|
||||
return 1;
|
||||
@@ -948,18 +972,24 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, int is_spawn) {
|
||||
posix_spawn_file_actions_init(&actions);
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
posix_spawn_file_actions_adddup2(&actions, new_err, 2);
|
||||
posix_spawn_file_actions_addclose(&actions, new_err);
|
||||
}
|
||||
|
||||
pid_t pid;
|
||||
@@ -1053,7 +1083,9 @@ JANET_CORE_FN(os_execute,
|
||||
JANET_CORE_FN(os_spawn,
|
||||
"(os/spawn args &opt flags env)",
|
||||
"Execute a program on the system and return a handle to the process. Otherwise, the "
|
||||
"same arguments as os/execute. Does not wait for the process.") {
|
||||
"same arguments as os/execute. Does not wait for the process. "
|
||||
"The returned value has the fields :in, :out, :err, :return-code and "
|
||||
"the additional field :pid on unix like platforms.") {
|
||||
return os_execute_impl(argc, argv, 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -51,15 +51,15 @@ static const uint32_t symchars[8] = {
|
||||
};
|
||||
|
||||
/* Check if a character is a valid symbol character
|
||||
* symbol chars are A-Z, a-z, 0-9, or one of !$&*+-./:<=>@\^_~| */
|
||||
static int is_symbol_char(uint8_t c) {
|
||||
* symbol chars are A-Z, a-z, 0-9, or one of !$&*+-./:<=>@\^_| */
|
||||
int janet_is_symbol_char(uint8_t c) {
|
||||
return symchars[c >> 5] & ((uint32_t)1 << (c & 0x1F));
|
||||
}
|
||||
|
||||
/* Validate some utf8. Useful for identifiers. Only validates
|
||||
* the encoding, does not check for valid code points (they
|
||||
* are less well defined than the encoding). */
|
||||
static int valid_utf8(const uint8_t *str, int32_t len) {
|
||||
int janet_valid_utf8(const uint8_t *str, int32_t len) {
|
||||
int32_t i = 0;
|
||||
int32_t j;
|
||||
while (i < len) {
|
||||
@@ -411,7 +411,7 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
|
||||
Janet ret;
|
||||
double numval;
|
||||
int32_t blen;
|
||||
if (is_symbol_char(c)) {
|
||||
if (janet_is_symbol_char(c)) {
|
||||
push_buf(p, (uint8_t) c);
|
||||
if (c > 127) state->argn = 1; /* Use to indicate non ascii */
|
||||
return 1;
|
||||
@@ -422,7 +422,7 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
|
||||
int start_num = start_dig || p->buf[0] == '-' || p->buf[0] == '+' || p->buf[0] == '.';
|
||||
if (p->buf[0] == ':') {
|
||||
/* Don't do full utf-8 check unless we have seen non ascii characters. */
|
||||
int valid = (!state->argn) || valid_utf8(p->buf + 1, blen - 1);
|
||||
int valid = (!state->argn) || janet_valid_utf8(p->buf + 1, blen - 1);
|
||||
if (!valid) {
|
||||
p->error = "invalid utf-8 in keyword";
|
||||
return 0;
|
||||
@@ -442,7 +442,7 @@ static int tokenchar(JanetParser *p, JanetParseState *state, uint8_t c) {
|
||||
return 0;
|
||||
} else {
|
||||
/* Don't do full utf-8 check unless we have seen non ascii characters. */
|
||||
int valid = (!state->argn) || valid_utf8(p->buf, blen);
|
||||
int valid = (!state->argn) || janet_valid_utf8(p->buf, blen);
|
||||
if (!valid) {
|
||||
p->error = "invalid utf-8 in symbol";
|
||||
return 0;
|
||||
@@ -582,7 +582,7 @@ static int root(JanetParser *p, JanetParseState *state, uint8_t c) {
|
||||
switch (c) {
|
||||
default:
|
||||
if (is_whitespace(c)) return 1;
|
||||
if (!is_symbol_char(c)) {
|
||||
if (!janet_is_symbol_char(c)) {
|
||||
p->error = "unexpected character";
|
||||
return 1;
|
||||
}
|
||||
@@ -746,6 +746,7 @@ Janet janet_parser_produce(JanetParser *parser) {
|
||||
}
|
||||
parser->pending--;
|
||||
parser->argcount--;
|
||||
parser->states[0].argn--;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -759,6 +760,7 @@ Janet janet_parser_produce_wrapped(JanetParser *parser) {
|
||||
}
|
||||
parser->pending--;
|
||||
parser->argcount--;
|
||||
parser->states[0].argn--;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1093,8 +1095,9 @@ static Janet janet_wrap_parse_state(JanetParseState *s, Janet *args,
|
||||
|
||||
if (s->flags & PFLAG_CONTAINER) {
|
||||
JanetArray *container_args = janet_array(s->argn);
|
||||
container_args->count = s->argn;
|
||||
safe_memcpy(container_args->data, args, sizeof(args[0])*s->argn);
|
||||
for (int32_t i = 0; i < s->argn; i++) {
|
||||
janet_array_push(container_args, args[i]);
|
||||
}
|
||||
janet_table_put(state, janet_ckeywordv("args"),
|
||||
janet_wrap_array(container_args));
|
||||
}
|
||||
@@ -1189,11 +1192,14 @@ static Janet parser_state_frames(const JanetParser *p) {
|
||||
JanetArray *states = janet_array(count);
|
||||
states->count = count;
|
||||
uint8_t *buf = p->buf;
|
||||
Janet *args = p->args;
|
||||
/* Iterate arg stack backwards */
|
||||
Janet *args = p->args + p->argcount;
|
||||
for (int32_t i = count - 1; i >= 0; --i) {
|
||||
JanetParseState *s = p->states + i;
|
||||
if (s->flags & PFLAG_CONTAINER) {
|
||||
args -= s->argn;
|
||||
}
|
||||
states->data[i] = janet_wrap_parse_state(s, args, buf, (uint32_t) p->bufcount);
|
||||
args -= s->argn;
|
||||
}
|
||||
return janet_wrap_array(states);
|
||||
}
|
||||
|
||||
@@ -1253,6 +1253,18 @@ static uint32_t peg_compile1(Builder *b, Janet peg) {
|
||||
emit_bytes(b, RULE_LITERAL, len, str);
|
||||
break;
|
||||
}
|
||||
case JANET_TABLE: {
|
||||
/* Build grammar table */
|
||||
JanetTable *new_grammar = janet_table_clone(janet_unwrap_table(peg));
|
||||
new_grammar->proto = grammar;
|
||||
b->grammar = grammar = new_grammar;
|
||||
/* Run the main rule */
|
||||
Janet main_rule = janet_table_rawget(grammar, janet_ckeywordv("main"));
|
||||
if (janet_checktype(main_rule, JANET_NIL))
|
||||
peg_panic(b, "grammar requires :main rule");
|
||||
rule = peg_compile1(b, main_rule);
|
||||
break;
|
||||
}
|
||||
case JANET_STRUCT: {
|
||||
/* Build grammar table */
|
||||
const JanetKV *st = janet_unwrap_struct(peg);
|
||||
|
||||
@@ -261,21 +261,13 @@ void janet_to_string_b(JanetBuffer *buffer, Janet x) {
|
||||
|
||||
/* See parse.c for full table */
|
||||
|
||||
static const uint32_t pp_symchars[8] = {
|
||||
0x00000000, 0xf7ffec72, 0xc7ffffff, 0x07fffffe,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000
|
||||
};
|
||||
|
||||
static int pp_is_symbol_char(uint8_t c) {
|
||||
return pp_symchars[c >> 5] & ((uint32_t)1 << (c & 0x1F));
|
||||
}
|
||||
|
||||
/* Check if a symbol or keyword contains no symbol characters */
|
||||
static int contains_bad_chars(const uint8_t *sym, int issym) {
|
||||
int32_t len = janet_string_length(sym);
|
||||
if (len && issym && sym[0] >= '0' && sym[0] <= '9') return 1;
|
||||
if (!janet_valid_utf8(sym, len)) return 1;
|
||||
for (int32_t i = 0; i < len; i++) {
|
||||
if (!pp_is_symbol_char(sym[i])) return 1;
|
||||
if (!janet_is_symbol_char(sym[i])) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -570,12 +562,12 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
|
||||
case JANET_STRUCT:
|
||||
case JANET_TABLE: {
|
||||
int istable = janet_checktype(x, JANET_TABLE);
|
||||
janet_buffer_push_cstring(S->buffer, istable ? "@" : "{");
|
||||
|
||||
/* For object-like tables, print class name */
|
||||
if (istable) {
|
||||
JanetTable *t = janet_unwrap_table(x);
|
||||
JanetTable *proto = t->proto;
|
||||
janet_buffer_push_cstring(S->buffer, "@");
|
||||
if (NULL != proto) {
|
||||
Janet name = janet_table_get(proto, janet_ckeywordv("_name"));
|
||||
const uint8_t *n;
|
||||
@@ -590,8 +582,25 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
|
||||
}
|
||||
}
|
||||
}
|
||||
janet_buffer_push_cstring(S->buffer, "{");
|
||||
} else {
|
||||
JanetStruct st = janet_unwrap_struct(x);
|
||||
JanetStruct proto = janet_struct_proto(st);
|
||||
if (NULL != proto) {
|
||||
Janet name = janet_struct_get(proto, janet_ckeywordv("_name"));
|
||||
const uint8_t *n;
|
||||
int32_t len;
|
||||
if (janet_bytes_view(name, &n, &len)) {
|
||||
if (S->flags & JANET_PRETTY_COLOR) {
|
||||
janet_buffer_push_cstring(S->buffer, janet_class_color);
|
||||
}
|
||||
janet_buffer_push_bytes(S->buffer, n, len);
|
||||
if (S->flags & JANET_PRETTY_COLOR) {
|
||||
janet_buffer_push_cstring(S->buffer, "\x1B[0m");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
janet_buffer_push_cstring(S->buffer, "{");
|
||||
|
||||
S->depth--;
|
||||
S->indent += 2;
|
||||
|
||||
@@ -79,9 +79,9 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char
|
||||
const char *e = janet_parser_error(&parser);
|
||||
errflags |= 0x04;
|
||||
ret = janet_cstringv(e);
|
||||
int32_t line = parser.line;
|
||||
int32_t col = parser.column;
|
||||
janet_eprintf("%s:%d:%d: parse error: %s\n", sourcePath, line, col, e);
|
||||
size_t line = parser.line;
|
||||
size_t col = parser.column;
|
||||
janet_eprintf("%s:%lu:%lu: parse error: %s\n", sourcePath, line, col, e);
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -39,13 +39,14 @@ JanetKV *janet_struct_begin(int32_t count) {
|
||||
head->length = count;
|
||||
head->capacity = capacity;
|
||||
head->hash = 0;
|
||||
head->proto = NULL;
|
||||
|
||||
JanetKV *st = (JanetKV *)(head->data);
|
||||
janet_memempty(st, capacity);
|
||||
return st;
|
||||
}
|
||||
|
||||
/* Find an item in a struct. Should be similar to janet_dict_find, but
|
||||
/* Find an item in a struct without looking for prototypes. Should be similar to janet_dict_find, but
|
||||
* specialized to structs (slightly more compact). */
|
||||
const JanetKV *janet_struct_find(const JanetKV *st, Janet key) {
|
||||
int32_t cap = janet_struct_capacity(st);
|
||||
@@ -68,7 +69,7 @@ const JanetKV *janet_struct_find(const JanetKV *st, Janet key) {
|
||||
* preforms an in-place insertion sort. This ensures the internal structure of the
|
||||
* hash map is independent of insertion order.
|
||||
*/
|
||||
void janet_struct_put(JanetKV *st, Janet key, Janet value) {
|
||||
void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) {
|
||||
int32_t cap = janet_struct_capacity(st);
|
||||
int32_t hash = janet_hash(key);
|
||||
int32_t index = janet_maphash(cap, hash);
|
||||
@@ -123,13 +124,19 @@ void janet_struct_put(JanetKV *st, Janet key, Janet value) {
|
||||
dist = otherdist;
|
||||
hash = otherhash;
|
||||
} else if (status == 0) {
|
||||
/* A key was added to the struct more than once - replace old value */
|
||||
kv->value = value;
|
||||
if (replace) {
|
||||
/* A key was added to the struct more than once - replace old value */
|
||||
kv->value = value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void janet_struct_put(JanetKV *st, Janet key, Janet value) {
|
||||
janet_struct_put_ext(st, key, value, 1);
|
||||
}
|
||||
|
||||
/* Finish building a struct */
|
||||
const JanetKV *janet_struct_end(JanetKV *st) {
|
||||
if (janet_struct_hash(st) != janet_struct_length(st)) {
|
||||
@@ -143,16 +150,43 @@ const JanetKV *janet_struct_end(JanetKV *st) {
|
||||
janet_struct_put(newst, kv->key, kv->value);
|
||||
}
|
||||
}
|
||||
janet_struct_proto(newst) = janet_struct_proto(st);
|
||||
st = newst;
|
||||
}
|
||||
janet_struct_hash(st) = janet_kv_calchash(st, janet_struct_capacity(st));
|
||||
if (janet_struct_proto(st)) {
|
||||
janet_struct_hash(st) += 2654435761u * janet_struct_hash(janet_struct_proto(st));
|
||||
}
|
||||
return (const JanetKV *)st;
|
||||
}
|
||||
|
||||
/* Get an item from a struct without looking into prototypes. */
|
||||
Janet janet_struct_rawget(const JanetKV *st, Janet key) {
|
||||
const JanetKV *kv = janet_struct_find(st, key);
|
||||
return kv ? kv->value : janet_wrap_nil();
|
||||
}
|
||||
|
||||
/* Get an item from a struct */
|
||||
Janet janet_struct_get(const JanetKV *st, Janet key) {
|
||||
const JanetKV *kv = janet_struct_find(st, key);
|
||||
return kv ? kv->value : janet_wrap_nil();
|
||||
for (int i = JANET_MAX_PROTO_DEPTH; st && i; --i, st = janet_struct_proto(st)) {
|
||||
const JanetKV *kv = janet_struct_find(st, key);
|
||||
if (NULL != kv && !janet_checktype(kv->key, JANET_NIL)) {
|
||||
return kv->value;
|
||||
}
|
||||
}
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
|
||||
/* Get an item from a struct, and record which prototype the item came from. */
|
||||
Janet janet_struct_get_ex(const JanetKV *st, Janet key, JanetStruct *which) {
|
||||
for (int i = JANET_MAX_PROTO_DEPTH; st && i; --i, st = janet_struct_proto(st)) {
|
||||
const JanetKV *kv = janet_struct_find(st, key);
|
||||
if (NULL != kv && !janet_checktype(kv->key, JANET_NIL)) {
|
||||
*which = st;
|
||||
return kv->value;
|
||||
}
|
||||
}
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
|
||||
/* Convert struct to table */
|
||||
@@ -167,3 +201,107 @@ JanetTable *janet_struct_to_table(const JanetKV *st) {
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
/* C Functions */
|
||||
|
||||
JANET_CORE_FN(cfun_struct_with_proto,
|
||||
"(struct/with-proto proto & kvs)",
|
||||
"Create a structure, as with the usual struct constructor but set the "
|
||||
"struct prototype as well.") {
|
||||
janet_arity(argc, 1, -1);
|
||||
JanetStruct proto = janet_optstruct(argv, argc, 0, NULL);
|
||||
if (!(argc & 1))
|
||||
janet_panic("expected odd number of arguments");
|
||||
JanetKV *st = janet_struct_begin(argc / 2);
|
||||
for (int32_t i = 1; i < argc; i += 2) {
|
||||
janet_struct_put(st, argv[i], argv[i + 1]);
|
||||
}
|
||||
janet_struct_proto(st) = proto;
|
||||
return janet_wrap_struct(janet_struct_end(st));
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_struct_getproto,
|
||||
"(struct/getproto st)",
|
||||
"Return the prototype of a struct, or nil if it doesn't have one.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetStruct st = janet_getstruct(argv, 0);
|
||||
return janet_struct_proto(st)
|
||||
? janet_wrap_struct(janet_struct_proto(st))
|
||||
: janet_wrap_nil();
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_struct_flatten,
|
||||
"(struct/proto-flatten st)",
|
||||
"Convert a struct with prototypes to a struct with no prototypes by merging "
|
||||
"all key value pairs from recursive prototypes into one new struct.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetStruct st = janet_getstruct(argv, 0);
|
||||
|
||||
/* get an upper bounds on the number of items in the final struct */
|
||||
int64_t pair_count = 0;
|
||||
JanetStruct cursor = st;
|
||||
while (cursor) {
|
||||
pair_count += janet_struct_length(cursor);
|
||||
cursor = janet_struct_proto(cursor);
|
||||
}
|
||||
|
||||
if (pair_count > INT32_MAX) {
|
||||
janet_panic("struct too large");
|
||||
}
|
||||
|
||||
JanetKV *accum = janet_struct_begin((int32_t) pair_count);
|
||||
cursor = st;
|
||||
while (cursor) {
|
||||
for (int32_t i = 0; i < janet_struct_capacity(cursor); i++) {
|
||||
const JanetKV *kv = cursor + i;
|
||||
if (!janet_checktype(kv->key, JANET_NIL)) {
|
||||
janet_struct_put_ext(accum, kv->key, kv->value, 0);
|
||||
}
|
||||
}
|
||||
cursor = janet_struct_proto(cursor);
|
||||
}
|
||||
return janet_wrap_struct(janet_struct_end(accum));
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_struct_to_table,
|
||||
"(struct/to-table st &opt recursive)",
|
||||
"Convert a struct to a table. If recursive is true, also convert the "
|
||||
"table's prototypes into the new struct's prototypes as well.") {
|
||||
janet_arity(argc, 1, 2);
|
||||
JanetStruct st = janet_getstruct(argv, 0);
|
||||
int recursive = argc > 1 && janet_truthy(argv[1]);
|
||||
JanetTable *tab = NULL;
|
||||
JanetStruct cursor = st;
|
||||
JanetTable *tab_cursor = tab;
|
||||
do {
|
||||
if (tab) {
|
||||
tab_cursor->proto = janet_table(janet_struct_length(cursor));
|
||||
tab_cursor = tab_cursor->proto;
|
||||
} else {
|
||||
tab = janet_table(janet_struct_length(cursor));
|
||||
tab_cursor = tab;
|
||||
}
|
||||
/* TODO - implement as memcpy since struct memory should be compatible
|
||||
* with table memory */
|
||||
for (int32_t i = 0; i < janet_struct_capacity(cursor); i++) {
|
||||
const JanetKV *kv = cursor + i;
|
||||
if (!janet_checktype(kv->key, JANET_NIL)) {
|
||||
janet_table_put(tab_cursor, kv->key, kv->value);
|
||||
}
|
||||
}
|
||||
cursor = janet_struct_proto(cursor);
|
||||
} while (recursive && cursor);
|
||||
return janet_wrap_table(tab);
|
||||
}
|
||||
|
||||
/* Load the struct module */
|
||||
void janet_lib_struct(JanetTable *env) {
|
||||
JanetRegExt struct_cfuns[] = {
|
||||
JANET_CORE_REG("struct/with-proto", cfun_struct_with_proto),
|
||||
JANET_CORE_REG("struct/getproto", cfun_struct_getproto),
|
||||
JANET_CORE_REG("struct/proto-flatten", cfun_struct_flatten),
|
||||
JANET_CORE_REG("struct/to-table", cfun_struct_to_table),
|
||||
JANET_REG_END
|
||||
};
|
||||
janet_core_cfuns_ext(env, NULL, struct_cfuns);
|
||||
}
|
||||
|
||||
101
src/core/table.c
101
src/core/table.c
@@ -132,37 +132,21 @@ static void janet_table_rehash(JanetTable *t, int32_t size) {
|
||||
|
||||
/* Get a value out of the table */
|
||||
Janet janet_table_get(JanetTable *t, Janet key) {
|
||||
JanetKV *bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
||||
return bucket->value;
|
||||
/* Check prototypes */
|
||||
{
|
||||
int i;
|
||||
for (i = JANET_MAX_PROTO_DEPTH, t = t->proto; t && i; t = t->proto, --i) {
|
||||
bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
||||
return bucket->value;
|
||||
}
|
||||
for (int i = JANET_MAX_PROTO_DEPTH; t && i; t = t->proto, --i) {
|
||||
JanetKV *bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
||||
return bucket->value;
|
||||
}
|
||||
return janet_wrap_nil();
|
||||
}
|
||||
|
||||
/* Get a value out of the table, and record which prototype it was from. */
|
||||
Janet janet_table_get_ex(JanetTable *t, Janet key, JanetTable **which) {
|
||||
JanetKV *bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) {
|
||||
*which = t;
|
||||
return bucket->value;
|
||||
}
|
||||
/* Check prototypes */
|
||||
{
|
||||
int i;
|
||||
for (i = JANET_MAX_PROTO_DEPTH, t = t->proto; t && i; t = t->proto, --i) {
|
||||
bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) {
|
||||
*which = t;
|
||||
return bucket->value;
|
||||
}
|
||||
for (int i = JANET_MAX_PROTO_DEPTH; t && i; t = t->proto, --i) {
|
||||
JanetKV *bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) {
|
||||
*which = t;
|
||||
return bucket->value;
|
||||
}
|
||||
}
|
||||
return janet_wrap_nil();
|
||||
@@ -217,6 +201,23 @@ void janet_table_put(JanetTable *t, Janet key, Janet value) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Used internally so don't check arguments
|
||||
* Put into a table, but if the key already exists do nothing. */
|
||||
static void janet_table_put_no_overwrite(JanetTable *t, Janet key, Janet value) {
|
||||
JanetKV *bucket = janet_table_find(t, key);
|
||||
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
||||
return;
|
||||
if (NULL == bucket || 2 * (t->count + t->deleted + 1) > t->capacity) {
|
||||
janet_table_rehash(t, janet_tablen(2 * t->count + 2));
|
||||
}
|
||||
bucket = janet_table_find(t, key);
|
||||
if (janet_checktype(bucket->value, JANET_BOOLEAN))
|
||||
--t->deleted;
|
||||
bucket->key = key;
|
||||
bucket->value = value;
|
||||
++t->count;
|
||||
}
|
||||
|
||||
/* Clear a table */
|
||||
void janet_table_clear(JanetTable *t) {
|
||||
int32_t capacity = t->capacity;
|
||||
@@ -226,19 +227,6 @@ void janet_table_clear(JanetTable *t) {
|
||||
t->deleted = 0;
|
||||
}
|
||||
|
||||
/* Convert table to struct */
|
||||
const JanetKV *janet_table_to_struct(JanetTable *t) {
|
||||
JanetKV *st = janet_struct_begin(t->count);
|
||||
JanetKV *kv = t->data;
|
||||
JanetKV *end = t->data + t->capacity;
|
||||
while (kv < end) {
|
||||
if (!janet_checktype(kv->key, JANET_NIL))
|
||||
janet_struct_put(st, kv->key, kv->value);
|
||||
kv++;
|
||||
}
|
||||
return janet_struct_end(st);
|
||||
}
|
||||
|
||||
/* Clone a table. */
|
||||
JanetTable *janet_table_clone(JanetTable *table) {
|
||||
JanetTable *newTable = janet_gcalloc(JANET_MEMORY_TABLE, sizeof(JanetTable));
|
||||
@@ -275,6 +263,34 @@ void janet_table_merge_struct(JanetTable *table, const JanetKV *other) {
|
||||
janet_table_mergekv(table, other, janet_struct_capacity(other));
|
||||
}
|
||||
|
||||
/* Convert table to struct */
|
||||
const JanetKV *janet_table_to_struct(JanetTable *t) {
|
||||
JanetKV *st = janet_struct_begin(t->count);
|
||||
JanetKV *kv = t->data;
|
||||
JanetKV *end = t->data + t->capacity;
|
||||
while (kv < end) {
|
||||
if (!janet_checktype(kv->key, JANET_NIL))
|
||||
janet_struct_put(st, kv->key, kv->value);
|
||||
kv++;
|
||||
}
|
||||
return janet_struct_end(st);
|
||||
}
|
||||
|
||||
JanetTable *janet_table_proto_flatten(JanetTable *t) {
|
||||
JanetTable *newTable = janet_table(0);
|
||||
while (t) {
|
||||
JanetKV *kv = t->data;
|
||||
JanetKV *end = t->data + t->capacity;
|
||||
while (kv < end) {
|
||||
if (!janet_checktype(kv->key, JANET_NIL))
|
||||
janet_table_put_no_overwrite(newTable, kv->key, kv->value);
|
||||
kv++;
|
||||
}
|
||||
t = t->proto;
|
||||
}
|
||||
return newTable;
|
||||
}
|
||||
|
||||
/* C Functions */
|
||||
|
||||
JANET_CORE_FN(cfun_table_new,
|
||||
@@ -349,6 +365,14 @@ JANET_CORE_FN(cfun_table_clear,
|
||||
return janet_wrap_table(table);
|
||||
}
|
||||
|
||||
JANET_CORE_FN(cfun_table_proto_flatten,
|
||||
"(table/proto-flatten tab)",
|
||||
"Create a new table that is the result of merging all prototypes into a new table.") {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetTable *table = janet_gettable(argv, 0);
|
||||
return janet_wrap_table(janet_table_proto_flatten(table));
|
||||
}
|
||||
|
||||
/* Load the table module */
|
||||
void janet_lib_table(JanetTable *env) {
|
||||
JanetRegExt table_cfuns[] = {
|
||||
@@ -359,6 +383,7 @@ void janet_lib_table(JanetTable *env) {
|
||||
JANET_CORE_REG("table/rawget", cfun_table_rawget),
|
||||
JANET_CORE_REG("table/clone", cfun_table_clone),
|
||||
JANET_CORE_REG("table/clear", cfun_table_clear),
|
||||
JANET_CORE_REG("table/proto-flatten", cfun_table_proto_flatten),
|
||||
JANET_REG_END
|
||||
};
|
||||
janet_core_cfuns_ext(env, NULL, table_cfuns);
|
||||
|
||||
@@ -57,6 +57,8 @@
|
||||
|
||||
/* Utils */
|
||||
#define janet_maphash(cap, hash) ((uint32_t)(hash) & (cap - 1))
|
||||
int janet_valid_utf8(const uint8_t *str, int32_t len);
|
||||
int janet_is_symbol_char(uint8_t c);
|
||||
extern const char janet_base64[65];
|
||||
int32_t janet_array_calchash(const Janet *array, int32_t len);
|
||||
int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len);
|
||||
@@ -126,6 +128,7 @@ void janet_lib_array(JanetTable *env);
|
||||
void janet_lib_tuple(JanetTable *env);
|
||||
void janet_lib_buffer(JanetTable *env);
|
||||
void janet_lib_table(JanetTable *env);
|
||||
void janet_lib_struct(JanetTable *env);
|
||||
void janet_lib_fiber(JanetTable *env);
|
||||
void janet_lib_os(JanetTable *env);
|
||||
void janet_lib_string(JanetTable *env);
|
||||
|
||||
@@ -101,6 +101,17 @@ static int traversal_next(Janet *x, Janet *y) {
|
||||
janet_vm.traversal = t;
|
||||
return 0;
|
||||
}
|
||||
/* Traverse prototype */
|
||||
JanetStruct sproto = sself->proto;
|
||||
JanetStruct oproto = sother->proto;
|
||||
if (sproto && !oproto) return 3;
|
||||
if (!sproto && oproto) return 1;
|
||||
if (oproto && sproto) {
|
||||
*x = janet_wrap_struct(sproto);
|
||||
*y = janet_wrap_struct(oproto);
|
||||
janet_vm.traversal = t - 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
t--;
|
||||
}
|
||||
@@ -273,6 +284,8 @@ int janet_equals(Janet x, Janet y) {
|
||||
if (s1 == s2) break;
|
||||
if (janet_struct_hash(s1) != janet_struct_hash(s2)) return 0;
|
||||
if (janet_struct_length(s1) != janet_struct_length(s2)) return 0;
|
||||
if (janet_struct_proto(s1) && !janet_struct_proto(s2)) return 0;
|
||||
if (!janet_struct_proto(s1) && janet_struct_proto(s2)) return 0;
|
||||
push_traversal_node(janet_struct_head(s1), janet_struct_head(s2), 0);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -866,7 +866,7 @@ struct JanetGCObject {
|
||||
union {
|
||||
JanetGCObject *next;
|
||||
int32_t refcount; /* For threaded abstract types */
|
||||
};
|
||||
} data;
|
||||
};
|
||||
|
||||
/* A lightweight green thread in janet. Does not correspond to
|
||||
@@ -961,6 +961,7 @@ struct JanetStructHead {
|
||||
int32_t length;
|
||||
int32_t hash;
|
||||
int32_t capacity;
|
||||
const JanetKV *proto;
|
||||
const JanetKV data[];
|
||||
};
|
||||
|
||||
@@ -1620,10 +1621,13 @@ JANET_API JanetSymbol janet_symbol_gen(void);
|
||||
#define janet_struct_length(t) (janet_struct_head(t)->length)
|
||||
#define janet_struct_capacity(t) (janet_struct_head(t)->capacity)
|
||||
#define janet_struct_hash(t) (janet_struct_head(t)->hash)
|
||||
#define janet_struct_proto(t) (janet_struct_head(t)->proto)
|
||||
JANET_API JanetKV *janet_struct_begin(int32_t count);
|
||||
JANET_API void janet_struct_put(JanetKV *st, Janet key, Janet value);
|
||||
JANET_API JanetStruct janet_struct_end(JanetKV *st);
|
||||
JANET_API Janet janet_struct_get(JanetStruct st, Janet key);
|
||||
JANET_API Janet janet_struct_rawget(JanetStruct st, Janet key);
|
||||
JANET_API Janet janet_struct_get_ex(JanetStruct st, Janet key, JanetStruct *which);
|
||||
JANET_API JanetTable *janet_struct_to_table(JanetStruct st);
|
||||
JANET_API const JanetKV *janet_struct_find(JanetStruct st, Janet key);
|
||||
|
||||
@@ -1759,6 +1763,7 @@ JANET_API JanetSignal janet_step(JanetFiber *fiber, Janet in, Janet *out);
|
||||
JANET_API Janet janet_call(JanetFunction *fun, int32_t argc, const Janet *argv);
|
||||
JANET_API Janet janet_mcall(const char *name, int32_t argc, Janet *argv);
|
||||
JANET_API void janet_stacktrace(JanetFiber *fiber, Janet err);
|
||||
JANET_API void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix);
|
||||
|
||||
/* Scratch Memory API */
|
||||
typedef void (*JanetScratchFinalizer)(void *);
|
||||
|
||||
@@ -136,7 +136,6 @@ static JANET_THREAD_LOCAL int gbl_cols = 80;
|
||||
static JANET_THREAD_LOCAL char *gbl_history[JANET_HISTORY_MAX];
|
||||
static JANET_THREAD_LOCAL int gbl_history_count = 0;
|
||||
static JANET_THREAD_LOCAL int gbl_historyi = 0;
|
||||
static JANET_THREAD_LOCAL int gbl_sigint_flag = 0;
|
||||
static JANET_THREAD_LOCAL struct termios gbl_termios_start;
|
||||
static JANET_THREAD_LOCAL JanetByteView gbl_matches[JANET_MATCH_MAX];
|
||||
static JANET_THREAD_LOCAL int gbl_match_count = 0;
|
||||
@@ -758,9 +757,9 @@ static int line() {
|
||||
kleft();
|
||||
break;
|
||||
case 3: /* ctrl-c */
|
||||
clearlines();
|
||||
gbl_sigint_flag = 1;
|
||||
return -1;
|
||||
norawmode();
|
||||
kill(getpid(), SIGINT);
|
||||
/* fallthrough */
|
||||
case 17: /* ctrl-q */
|
||||
gbl_cancel_current_repl_form = 1;
|
||||
clearlines();
|
||||
@@ -962,11 +961,7 @@ void janet_line_get(const char *p, JanetBuffer *buffer) {
|
||||
}
|
||||
if (line()) {
|
||||
norawmode();
|
||||
if (gbl_sigint_flag) {
|
||||
raise(SIGINT);
|
||||
} else {
|
||||
fputc('\n', out);
|
||||
}
|
||||
fputc('\n', out);
|
||||
return;
|
||||
}
|
||||
fflush(stdin);
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
(var suite-num 0)
|
||||
(var start-time 0)
|
||||
|
||||
(def is-verbose (os/getenv "VERBOSE"))
|
||||
|
||||
(defn assert
|
||||
"Override's the default assert with some nice error handling."
|
||||
[x &opt e]
|
||||
@@ -12,11 +14,9 @@
|
||||
(++ num-tests-run)
|
||||
(when x (++ num-tests-passed))
|
||||
(def str (string e))
|
||||
(def truncated
|
||||
(if (> (length e) 40) (string (string/slice e 0 35) "...") (describe e)))
|
||||
(if x
|
||||
(eprintf "\e[32m✔\e[0m %s: %v" truncated x)
|
||||
(eprintf "\n\e[31m✘\e[0m %s: %v" truncated x))
|
||||
(when is-verbose (eprintf "\e[32m✔\e[0m %s: %v" (describe e) x))
|
||||
(eprintf "\n\e[31m✘\e[0m %s: %v" (describe e) x))
|
||||
x)
|
||||
|
||||
(defmacro assert-error
|
||||
@@ -32,10 +32,10 @@
|
||||
(defn start-suite [x]
|
||||
(set suite-num x)
|
||||
(set start-time (os/clock))
|
||||
(eprint "\nRunning test suite " x " tests...\n "))
|
||||
(eprint "Starting suite " x "..."))
|
||||
|
||||
(defn end-suite []
|
||||
(def delta (- (os/clock) start-time))
|
||||
(eprintf "\n\nTest suite %d finished in %.3f seconds" suite-num delta)
|
||||
(eprint num-tests-passed " of " num-tests-run " tests passed.\n")
|
||||
(eprinf "Finished suite %d in %.3f seconds - " suite-num delta)
|
||||
(eprint num-tests-passed " of " num-tests-run " tests passed.")
|
||||
(if (not= num-tests-passed num-tests-run) (os/exit 1)))
|
||||
|
||||
@@ -202,6 +202,7 @@
|
||||
|
||||
#🐙🐙🐙🐙
|
||||
|
||||
(defn foo [Θa Θb Θc] 0)
|
||||
(def 🦊 :fox)
|
||||
(def 🐮 :cow)
|
||||
(assert (= (string "🐼" 🦊 🐮) "🐼foxcow") "emojis 🙉 :)")
|
||||
|
||||
@@ -288,6 +288,11 @@
|
||||
(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]))
|
||||
|
||||
# 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]))
|
||||
|
||||
# Sort function
|
||||
(assert (deep=
|
||||
(range 99)
|
||||
|
||||
@@ -168,6 +168,16 @@
|
||||
(assert (= (string out-buf) "Hello\nhi") "print and prin to buffer 1")
|
||||
(assert (= (string err-buf) "Sup\nnot much.") "eprint and eprin to buffer 1")
|
||||
|
||||
# Printing to functions
|
||||
(def out-buf @"")
|
||||
(defn prepend [x]
|
||||
(with-dyns [:out out-buf]
|
||||
(prin "> " x)))
|
||||
(with-dyns [:out prepend]
|
||||
(print "Hello world"))
|
||||
|
||||
(assert (= (string out-buf) "> Hello world\n") "print to buffer via function")
|
||||
|
||||
(assert (= (string '()) (string [])) "empty bracket tuple literal")
|
||||
|
||||
# with-vars
|
||||
@@ -308,8 +318,9 @@
|
||||
(assert (deep= (range 4) a) "eachk 1")
|
||||
|
||||
|
||||
(tracev (def my-unique-var-name true))
|
||||
(assert my-unique-var-name "tracev upscopes")
|
||||
(with-dyns [:err @""]
|
||||
(tracev (def my-unique-var-name true))
|
||||
(assert my-unique-var-name "tracev upscopes"))
|
||||
|
||||
(assert (pos? (length (gensym))) "gensym not empty, regression #753")
|
||||
|
||||
|
||||
@@ -348,4 +348,8 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
|
||||
(assert (deep= @[111] (peg/match '(number :d+) "111")) "simple number capture 1")
|
||||
(assert (deep= @[255] (peg/match '(number :w+) "0xff")) "simple number capture 2")
|
||||
|
||||
# quoted match test
|
||||
(assert (= :yes (match 'john 'john :yes _ :nope)) "quoted literal match 1")
|
||||
(assert (= :nope (match 'john ''john :yes _ :nope)) "quoted literal match 2")
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -151,36 +151,36 @@
|
||||
|
||||
(:close s))
|
||||
|
||||
(defn check-matching-names [stream]
|
||||
(def ln (net/localname stream))
|
||||
(def pn (net/peername stream))
|
||||
(def [my-ip my-port] ln)
|
||||
(def [remote-ip remote-port] pn)
|
||||
(def msg (string my-ip " " my-port " " remote-ip " " remote-port))
|
||||
(def buf @"")
|
||||
(ev/gather
|
||||
(net/write stream msg)
|
||||
(net/read stream 1024 buf))
|
||||
(def comparison (string/split " " buf))
|
||||
(assert (and (= my-ip (get comparison 2))
|
||||
(= (string my-port) (get comparison 3))
|
||||
(= remote-ip (get comparison 0))
|
||||
(= (string remote-port) (get comparison 1)))
|
||||
(string/format "localname should match peername: msg=%j, buf=%j" msg buf)))
|
||||
|
||||
# Test on both server and client
|
||||
(defn names-handler
|
||||
[stream]
|
||||
(defer (:close stream)
|
||||
(check-matching-names stream)))
|
||||
|
||||
# Test localname and peername
|
||||
(repeat 20
|
||||
|
||||
(defn check-matching-names [stream]
|
||||
(def [my-ip my-port] (net/localname stream))
|
||||
(def [remote-ip remote-port] (net/peername stream))
|
||||
(def msg (string my-ip " " my-port " " remote-ip " " remote-port))
|
||||
(def buf @"")
|
||||
(ev/gather
|
||||
(net/write stream msg)
|
||||
(net/read stream 1024 buf))
|
||||
(def comparison (string/split " " buf))
|
||||
(assert (and (= my-ip (get comparison 2))
|
||||
(= (string my-port) (get comparison 3))
|
||||
(= remote-ip (get comparison 0))
|
||||
(= (string remote-port) (get comparison 1)))
|
||||
"localname should match peername"))
|
||||
|
||||
# Test on both server and client
|
||||
(defn names-handler
|
||||
[stream]
|
||||
(defer (:close stream)
|
||||
(check-matching-names stream)))
|
||||
(with [s (net/server "127.0.0.1" "8000" names-handler)]
|
||||
(defn test-names []
|
||||
(with [conn (net/connect "127.0.0.1" "8000")]
|
||||
(check-matching-names conn)))
|
||||
(test-names)
|
||||
(test-names))
|
||||
|
||||
(repeat 20 (test-names)))
|
||||
(gccollect))
|
||||
|
||||
# Create pipe
|
||||
|
||||
@@ -161,10 +161,59 @@
|
||||
([err] :caught))))
|
||||
"regression #638"))
|
||||
|
||||
|
||||
# Struct prototypes
|
||||
(def x (struct/with-proto {1 2 3 4} 5 6))
|
||||
(def y (-> x marshal unmarshal))
|
||||
(def z {1 2 3 4})
|
||||
(assert (= 2 (get x 1)) "struct get proto value 1")
|
||||
(assert (= 4 (get x 3)) "struct get proto value 2")
|
||||
(assert (= 6 (get x 5)) "struct get proto value 3")
|
||||
(assert (= x y) "struct proto marshal equality 1")
|
||||
(assert (= (getproto x) (getproto y)) "struct proto marshal equality 2")
|
||||
(assert (= 0 (cmp x y)) "struct proto comparison 1")
|
||||
(assert (= 0 (cmp (getproto x) (getproto y))) "struct proto comparison 2")
|
||||
(assert (not= (cmp x z) 0) "struct proto comparison 3")
|
||||
(assert (not= (cmp y z) 0) "struct proto comparison 4")
|
||||
(assert (not= x z) "struct proto comparison 5")
|
||||
(assert (not= y z) "struct proto comparison 6")
|
||||
(assert (= (x 5) 6) "struct proto get 1")
|
||||
(assert (= (y 5) 6) "struct proto get 1")
|
||||
(assert (deep= x y) "struct proto deep= 1")
|
||||
(assert (deep-not= x z) "struct proto deep= 2")
|
||||
(assert (deep-not= y z) "struct proto deep= 3")
|
||||
|
||||
# Issue #751
|
||||
(def t {:side false})
|
||||
(assert (nil? (get-in t [:side :note])) "get-in with false value")
|
||||
(assert (= (get-in t [:side :note] "dflt") "dflt")
|
||||
"get-in with false value and default")
|
||||
|
||||
(assert (= (math/gcd 462 1071) 21) "math/gcd 1")
|
||||
(assert (= (math/lcm 462 1071) 23562) "math/lcm 1")
|
||||
|
||||
# Evaluate stream with `dofile`
|
||||
(def [r w] (os/pipe))
|
||||
(:write w "(setdyn :x 10)")
|
||||
(:close w)
|
||||
(def stream-env (dofile r))
|
||||
(assert (= (stream-env :x) 10) "dofile stream 1")
|
||||
|
||||
# Issue #861 - should be valgrind clean
|
||||
(def step1 "(a b c d)\n")
|
||||
(def step2 "(a b)\n")
|
||||
(def p1 (parser/new))
|
||||
(parser/state p1)
|
||||
(parser/consume p1 step1)
|
||||
(loop [v :iterate (parser/produce p1)])
|
||||
(parser/state p1)
|
||||
(def p2 (parser/clone p1))
|
||||
(parser/state p2)
|
||||
(parser/consume p2 step2)
|
||||
(loop [v :iterate (parser/produce p2)])
|
||||
(parser/state p2)
|
||||
|
||||
# Check missing struct proto bug.
|
||||
(assert (struct/getproto (struct/with-proto {:a 1} :b 2 :c nil)) "missing struct proto")
|
||||
|
||||
(end-suite)
|
||||
|
||||
38
test/suite0011.janet
Normal file
38
test/suite0011.janet
Normal file
@@ -0,0 +1,38 @@
|
||||
# Copyright (c) 2021 Calvin Rose & contributors
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
|
||||
(import ./helper :prefix "" :exit true)
|
||||
(start-suite 11)
|
||||
|
||||
(assert (< 11899423.08 (math/gamma 11.5) 11899423.085)
|
||||
"math/gamma")
|
||||
|
||||
(assert (< 2605.1158 (math/log-gamma 500) 2605.1159)
|
||||
"math/log-gamma")
|
||||
|
||||
(def ch (ev/thread-chan 2))
|
||||
(def att (ev/thread-chan 109))
|
||||
(assert att "`att` was nil after creation")
|
||||
(ev/give ch att)
|
||||
(ev/do-thread
|
||||
(assert (ev/take ch) "channel packing bug for threaded abstracts on threaded channels."))
|
||||
|
||||
(end-suite)
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
# Format all code with astyle
|
||||
|
||||
STYLEOPTS="--style=attach --indent-switches --convert-tabs \
|
||||
--align-pointer=name --pad-header --pad-oper --unpad-paren --indent-labels"
|
||||
--align-pointer=name --pad-header --pad-oper --unpad-paren --indent-labels --formatted"
|
||||
|
||||
astyle $STYLEOPTS */*.c
|
||||
astyle $STYLEOPTS */*/*.c
|
||||
astyle $STYLEOPTS */*/*.h
|
||||
rm -f */*.c.orig
|
||||
rm -f */*/*.c.orig
|
||||
rm -f */*/*.h.orig
|
||||
|
||||
Reference in New Issue
Block a user