Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9033b217d9 | ||
|
|
1d9548fd28 | ||
|
|
9a6c195880 | ||
|
|
3eb938a0ba | ||
|
|
66f1e74e0c | ||
|
|
7c382b94af | ||
|
|
9297a9167e | ||
|
|
0410a1cec9 | ||
|
|
b545a1e491 | ||
|
|
c08b5b11c6 | ||
|
|
8939b39fc2 | ||
|
|
e12a0c561e | ||
|
|
61c3e82123 | ||
|
|
d43b41695d | ||
|
|
fdca8b2657 | ||
|
|
dec06d8c82 | ||
|
|
11c84cbd2e | ||
|
|
67501bcddc | ||
|
|
1f05e7ce73 | ||
|
|
02c279af12 | ||
|
|
2c2fb820ab | ||
|
|
1ad1d6d6b0 | ||
|
|
83d8161959 | ||
|
|
b87ea61713 | ||
|
|
c355ab6482 | ||
|
|
f0ed2d48ce | ||
|
|
d23a46fa9f | ||
|
|
6c0cbbd73e | ||
|
|
d58ef57e13 | ||
|
|
1a9493534e | ||
|
|
45fe1cb084 | ||
|
|
3f0e7c5613 | ||
|
|
49e76833d6 | ||
|
|
7963e44a5e | ||
|
|
3ba5e44dae | ||
|
|
1d74ddac78 | ||
|
|
c1e3d4f976 | ||
|
|
b4fc2b35fc | ||
|
|
18a5d8affb | ||
|
|
cd80994a70 | ||
|
|
e02d7bf913 | ||
|
|
fe5ff41514 | ||
|
|
e9887a760e | ||
|
|
0559fe5927 | ||
|
|
1a05bff45d | ||
|
|
4d6670342b | ||
|
|
193e85882e | ||
|
|
a58c007e0c | ||
|
|
ce6f4e6d15 | ||
|
|
321096665a | ||
|
|
b93aae9f86 | ||
|
|
a503069479 | ||
|
|
120ea0fc8a | ||
|
|
d725c66d93 | ||
|
|
fab917dba3 | ||
|
|
23fb92f444 | ||
|
|
db61109495 | ||
|
|
c4e8ad0d65 | ||
|
|
ce77fa54b9 | ||
|
|
eee921bf7b | ||
|
|
73c6a0be8b | ||
|
|
7644bd87a5 | ||
|
|
9e2c5a29a4 | ||
|
|
09498422d0 | ||
|
|
a697bab7f2 | ||
|
|
36e81d587e | ||
|
|
17a0abd57a | ||
|
|
e6cfc20556 | ||
|
|
906f03102b | ||
|
|
6a594e2bba | ||
|
|
10dd8f843b | ||
|
|
53ecea8b03 | ||
|
|
efba8d8bfa | ||
|
|
5a201065dc | ||
|
|
307675284c | ||
|
|
38a9513111 | ||
|
|
578f798cc1 | ||
|
|
efbdf4b9b0 | ||
|
|
9679bed4d7 | ||
|
|
b6c0045fc2 | ||
|
|
10612ddf22 | ||
|
|
cbcbbcf876 | ||
|
|
95b81c2a13 | ||
|
|
a525f71e13 | ||
|
|
750d2a3f7a | ||
|
|
61e9fafb73 | ||
|
|
aa48f6a881 | ||
|
|
b5e2d45b7d | ||
|
|
29f2eb7dab | ||
|
|
a3754e3841 | ||
|
|
337c040da8 | ||
|
|
b4d465e2ed | ||
|
|
0bb3d1c6bd | ||
|
|
86476615ef | ||
|
|
860b3cc5a7 | ||
|
|
bb5e6f65f5 | ||
|
|
95127516f7 | ||
|
|
ef0c495f16 | ||
|
|
2472bb79ab | ||
|
|
04b9174d75 | ||
|
|
d31a58f0d9 | ||
|
|
f97ab2649c | ||
|
|
d969d42784 | ||
|
|
17c4c4d993 | ||
|
|
0f7a968389 | ||
|
|
272ec8ca47 | ||
|
|
a0e8e1c0fb | ||
|
|
b477dba34f | ||
|
|
8d524ef971 | ||
|
|
83cb5152fd | ||
|
|
3c100575a3 | ||
|
|
d1b6161f2e | ||
|
|
785781d089 | ||
|
|
b2ac4c9394 | ||
|
|
50ac792747 | ||
|
|
7d433e80e5 | ||
|
|
f9cb812bc1 | ||
|
|
f8711f2e36 | ||
|
|
0251f564d9 | ||
|
|
e4acc08cf5 | ||
|
|
2ab5d25787 | ||
|
|
acde8078f8 | ||
|
|
487ac9c1c4 | ||
|
|
f0f65f8bec | ||
|
|
23943b6180 | ||
|
|
d9125b0a81 | ||
|
|
d1d46db907 | ||
|
|
02056d1030 | ||
|
|
14a3686a3f | ||
|
|
c04b182c6c | ||
|
|
f5462d98e0 | ||
|
|
75d0c07b46 | ||
|
|
1de8dcb695 | ||
|
|
0e167bef27 | ||
|
|
34df817638 | ||
|
|
311a5790d6 | ||
|
|
b4c75f97b4 | ||
|
|
70692e6618 | ||
|
|
529d584262 | ||
|
|
c0fb275d5b | ||
|
|
24f46be144 | ||
|
|
0fe3da9dea | ||
|
|
03d0ad3f03 | ||
|
|
a87f43701f | ||
|
|
dce3a3a2a5 | ||
|
|
7b20084b28 | ||
|
|
542808102f | ||
|
|
fd18b6a41f | ||
|
|
4c9c95f80f | ||
|
|
89460154bb | ||
|
|
b026e8877a | ||
|
|
597381b2e0 | ||
|
|
df70729b14 | ||
|
|
61b116c1a5 | ||
|
|
d37a10bbd0 | ||
|
|
5d75c5c861 | ||
|
|
db4089cc71 | ||
|
|
f9f4f14edf | ||
|
|
58946ce879 | ||
|
|
de1d5ff793 | ||
|
|
a4be8fb260 | ||
|
|
031a27f5fa | ||
|
|
f3c1931cac | ||
|
|
2e48d81c20 | ||
|
|
e7ef3fc95d | ||
|
|
2d01aa1c6a | ||
|
|
ae07124550 | ||
|
|
1f744f4dac | ||
|
|
ad88ac642f | ||
|
|
f7956e9d80 | ||
|
|
da6892aa96 | ||
|
|
7a3bd20804 | ||
|
|
f7015166dc | ||
|
|
c6f346a355 | ||
|
|
911a3ba97f | ||
|
|
a1668a6b28 | ||
|
|
a4cb74a83f | ||
|
|
bbf9552186 | ||
|
|
e621b5828e | ||
|
|
68e282c50a | ||
|
|
403a50fbcd | ||
|
|
8e98cd4f41 | ||
|
|
499c385000 | ||
|
|
aecafc099d | ||
|
|
785c7d700a | ||
|
|
cecc8a0f95 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
/.eunit
|
/.eunit
|
||||||
/.release
|
/.release
|
||||||
/docs
|
/doc
|
||||||
/ebin
|
/ebin
|
||||||
/lib/*/ebin/*
|
/lib/*/ebin/*
|
||||||
/lib/*/tmp
|
/lib/*/tmp
|
||||||
|
|||||||
@@ -8,3 +8,4 @@ notifications:
|
|||||||
otp_release:
|
otp_release:
|
||||||
- 17.0
|
- 17.0
|
||||||
- 17.1
|
- 17.1
|
||||||
|
- 17.3
|
||||||
|
|||||||
+101
@@ -1,5 +1,106 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v1.0.5 (2015-06-29)
|
||||||
|
|
||||||
|
* Enhancements
|
||||||
|
* [Elixir] Support 18.0
|
||||||
|
* [IEx] Rely only on loaded applications for autocompletion on IEx
|
||||||
|
* [Record] Expand attributes and macros in record extractor
|
||||||
|
* [String] `Optimize String.rstrip/1`
|
||||||
|
* [String] `Optimize String.downcase/1`
|
||||||
|
* [String] `Optimize String.upcase/1`
|
||||||
|
|
||||||
|
* Bug fixes
|
||||||
|
* [EEx] Ensure blocks do not clobber EEx buffers
|
||||||
|
* [Enum] Ensure `Enum.take/2` does not consume one extra item when halting on the last emittable item
|
||||||
|
* [ExUnit] Fix `StringIO` processes leakage in the `ExUnit.CaptureIO` when there are errors inside the `capture_io` block
|
||||||
|
* [Float] Avoid rounding errors on `Float.parse/1`
|
||||||
|
* [GenEvent] Fix `GenEvent` detection of modules that aren't loaded
|
||||||
|
* [IO] Read 4K blocks instead of lines in `IO.binread/2`. This fixes a bug where CRLF were being ignored and is also going to improve performance
|
||||||
|
* [Logger] Handle `:undefined` arity in `Logger.Translator` (we get :undefined when a temporary worker of a `simple_one_for_one` supervisor crashes)
|
||||||
|
* [Mix] Ensure config is escaped before being injected into escripts. This fixes a bug where escripts failed to build when containing values like maps in config files
|
||||||
|
* [Mix] Ensure we properly underscore acronyms followed by paths, for example, `HTTP.Foo`
|
||||||
|
* [Stream] Ensure `Stream.flat_map/2` does not consume more items than necessary when piped to another `Stream.flat_map/2` that halts in the inner stream
|
||||||
|
* [Version] Fix `to_string` for versions with numeric pre releases
|
||||||
|
|
||||||
|
## v1.0.4 (2015-04-07)
|
||||||
|
|
||||||
|
* Enhancements
|
||||||
|
* [Elixir] Support Erlang 17.5 and 18.0-rc
|
||||||
|
* [Mix] Support "--search PATTERN" in "mix help"
|
||||||
|
* [Mix] Support `:start_permanent` that starts the application as permanent
|
||||||
|
* [Mix] Support `:build_embedded` that compile protocols, avoid symlinks and ensure protocols are loaded on boot
|
||||||
|
* [Mix] Make "mix app.start" public and support "--permanent" and "--temporary" options
|
||||||
|
* [URI] Speed decoding operations
|
||||||
|
|
||||||
|
* Bug fixes
|
||||||
|
* [Elixir] Produce clearer error messages for syntax errors before a sigil
|
||||||
|
* [Elixir] Return value when matching on `_` in the last line of a block
|
||||||
|
* [Elixir] Ensure changes in child deps forces the parent to recompile
|
||||||
|
* [Kernel] Fix a bug where `=~/2` returned false when matching on an empty string. The current behaviour was changed to match the behaviour of `String.contains?/2`
|
||||||
|
* [Mix] Store relative paths in erlang manifests
|
||||||
|
* [Mix] Fix httpc proxy option profile for downloading hex and other safe packges
|
||||||
|
* [Mix] Fix missing behaviour warning when compiling Erlang modules
|
||||||
|
|
||||||
|
## v1.0.3 (2015-02-12)
|
||||||
|
|
||||||
|
* Enhancements
|
||||||
|
* [Elixir] Do not show exported vars warning
|
||||||
|
* [Elixir] Improve error messages when using invalid expressions inside maps
|
||||||
|
* [Elixir] Skip aliases and import warnings when code cannot compile
|
||||||
|
* [IEx] Improve autocompletion for Erlang/Elixir based on loaded applications
|
||||||
|
* [IO] Make `IO.ANSI.Docs` readable on white background
|
||||||
|
* [Mix] Provide clearer error message when updating archives on Windows
|
||||||
|
* [Mix] Don't go through hex.pm API server to install Hex
|
||||||
|
|
||||||
|
* Bug fixes
|
||||||
|
* [Elixir] Ensure all Elixir applications work with code upgrades
|
||||||
|
* [Elixir] Don't leave spurious processes and stale data when the parallel compiler fails
|
||||||
|
* [Inspect] Keep original stacktrace on Inspect errors
|
||||||
|
* [IO] Fix `IO.ANSI.Docs` handling of `*`, `**`, `_` where they would be activated in the middle of words
|
||||||
|
* [Logger] Fix logging with `metadata[:function]`
|
||||||
|
* [Mix] Don't show hex update message on mix `local.hex`
|
||||||
|
* [Mix] Ensure tasks are properly underscored and camelized
|
||||||
|
* [Mix] Ensure Mix does not use ANSI escapes on Windows unless ANSI is enabled
|
||||||
|
* [Mix] Only shutdown Logger if the Mix application is being actually started
|
||||||
|
* [Mix] Ensure priv/include are copied on Windows even if source did not change
|
||||||
|
* [Mix] Ensure rebar dependencies work when user path has a space on Windows
|
||||||
|
* [Regex] Fix `Regex.replace/4` failures when the number of substitutions in pattern is more than the number of matches and support `\g{...}` patterns
|
||||||
|
* [String] Fix LFCR being treated as a grapheme instead of CRLF
|
||||||
|
* [URI] Do more strict matching on URI schema
|
||||||
|
|
||||||
|
## v1.0.2 (2014-10-21)
|
||||||
|
|
||||||
|
* Bug fixes
|
||||||
|
* [EEx] Remove invalid warning when attempting to inline code in EEx
|
||||||
|
* [EEx] Unify syntax errors to always include `file:line:`
|
||||||
|
* [IEx] Set null prompt function in dumb term mode
|
||||||
|
* [Kernel] Do not define an ets table with the same name as the module as it clashes with user code
|
||||||
|
* [Kernel] Fail compiling try expressions if they lack one of catch/after/rescue/else clauses
|
||||||
|
* [Kernel] Improve error messages when parsing fails on aliases
|
||||||
|
* [Kernel] Improve warnings coming from compile-time evaluation of arguments
|
||||||
|
* [String] Fix `String.replace/4` typespec
|
||||||
|
|
||||||
|
## v1.0.1 (2014-10-07)
|
||||||
|
|
||||||
|
* Enhancements
|
||||||
|
* [Mix] Add MIX_ENV output to archive.build (as on escript.build)
|
||||||
|
|
||||||
|
* Bug fixes
|
||||||
|
* [CLI] Fix Elixir CLI when running on Cygwin
|
||||||
|
* [ExUnit] Include stacktraces in ExUnit timeouts
|
||||||
|
* [Mix] Load dependencies before `deps.check` compiles dependencies
|
||||||
|
* [Mix] Ensure `Mix.Config` is deep merged
|
||||||
|
* [Mix] Change version requirement in new apps to `~> MAJOR.MINOR`
|
||||||
|
* [Mix] Report correct location if `local.hex` fails and give higher priority to powershell install on Windows
|
||||||
|
* [Mix] Ensure `--elixirc-paths` option for `compile.elixir` is a subset of the project `:elixirc_paths` configuration and it does not remove skipped entries from the manifest
|
||||||
|
* [Mix] Ensure `compile.elixir` writes to manifest when files are removed
|
||||||
|
* [Mix] Ensure `compile.elixir` purges and deletes modules before compiling
|
||||||
|
* [Mix] Do not crash on malformed proxy env var
|
||||||
|
* [Stream] Ensure `chunk/4` works correctly when halted
|
||||||
|
* [System] Do not allow relative paths in `System.cmd/3` as documented
|
||||||
|
* [System] Fix `:stderr_to_stdout` option in `System.cmd/3`
|
||||||
|
|
||||||
## v1.0.0 (2014-09-10)
|
## v1.0.0 (2014-09-10)
|
||||||
|
|
||||||
* Enhancements
|
* Enhancements
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Copyright 2012-2013 Plataformatec.
|
Copyright 2012 Plataformatec
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
REBAR := rebar
|
REBAR := rebar
|
||||||
|
DOCS := v1.0
|
||||||
|
CANONICAL := stable
|
||||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
||||||
ERLC := erlc -I lib/elixir/include
|
ERLC := erlc -I lib/elixir/include
|
||||||
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
||||||
@@ -124,17 +126,18 @@ clean_exbeam:
|
|||||||
|
|
||||||
#==> Release tasks
|
#==> Release tasks
|
||||||
|
|
||||||
SOURCE_REF = $(shell head="$$(git rev-parse HEAD)" tag="$$(git tag --points-at $$head | tail -1)" ; echo "$${tag:-$$head}\c")
|
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
|
||||||
DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" -o docs/$(2) -p http://elixir-lang.org/docs.html
|
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}\c")
|
||||||
|
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "v$(VERSION)" $(call LOGO_PATH) -o doc/$(2) -a http://elixir-lang.org/docs/$(CANONICAL)/$(2)/ -p http://elixir-lang.org/docs.html
|
||||||
|
|
||||||
docs: compile ../ex_doc/bin/ex_doc
|
docs: compile ../ex_doc/bin/ex_doc
|
||||||
$(Q) rm -rf docs
|
$(Q) rm -rf doc
|
||||||
$(call DOCS,Elixir,elixir,Kernel)
|
$(call COMPILE_DOCS,Elixir,elixir,Kernel)
|
||||||
$(call DOCS,EEx,eex,EEx)
|
$(call COMPILE_DOCS,EEx,eex,EEx)
|
||||||
$(call DOCS,Mix,mix,Mix)
|
$(call COMPILE_DOCS,Mix,mix,Mix)
|
||||||
$(call DOCS,IEx,iex,IEx)
|
$(call COMPILE_DOCS,IEx,iex,IEx)
|
||||||
$(call DOCS,ExUnit,ex_unit,ExUnit)
|
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit)
|
||||||
$(call DOCS,Logger,logger,Logger)
|
$(call COMPILE_DOCS,Logger,logger,Logger)
|
||||||
|
|
||||||
../ex_doc/bin/ex_doc:
|
../ex_doc/bin/ex_doc:
|
||||||
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
|
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
|
||||||
@@ -145,9 +148,18 @@ release_zip: compile
|
|||||||
zip -9 -r v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE Makefile README.md VERSION
|
zip -9 -r v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE Makefile README.md VERSION
|
||||||
|
|
||||||
release_docs: docs
|
release_docs: docs
|
||||||
cd ../docs
|
rm -rf ../docs/$(DOCS)/*/
|
||||||
rm -rf ../docs/master
|
mv doc/* ../docs/$(DOCS)
|
||||||
mv docs ../docs/master
|
|
||||||
|
# This task requires aws-cli to be installed and set up for access to s3.hex.pm
|
||||||
|
# See: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html
|
||||||
|
|
||||||
|
publish_mix: compile
|
||||||
|
cd lib/mix && MIX_ENV=prod mix escript.build
|
||||||
|
aws s3 cp lib/mix/mix s3://s3.hex.pm/builds/mix/v$(VERSION)/mix --acl public-read
|
||||||
|
aws s3 cp lib/mix/mix s3://s3.hex.pm/builds/mix/mix --acl public-read
|
||||||
|
rm lib/mix/mix
|
||||||
|
rm -rf lib/mix/_build
|
||||||
|
|
||||||
#==> Tests tasks
|
#==> Tests tasks
|
||||||
|
|
||||||
|
|||||||
+23
-11
@@ -1,28 +1,40 @@
|
|||||||
# Release process
|
# Release process
|
||||||
|
|
||||||
|
## All releases
|
||||||
|
|
||||||
This document simply outlines the release process:
|
This document simply outlines the release process:
|
||||||
|
|
||||||
1. Remove all `-dev` extension from versions (see below for all files)
|
1. Ensure you are running on the oldest supported Erlang version
|
||||||
|
|
||||||
2. Ensure CHANGELOG is updated and add current date
|
2. Remove all `-dev` extension from versions (see below for all files)
|
||||||
|
|
||||||
3. Commit changes above with title "Release vVERSION" and generate new tag
|
3. Ensure CHANGELOG is updated and add current date
|
||||||
|
|
||||||
4. Run `make clean test` to ensure all tests pass from scratch and the CI is green
|
4. Commit changes above with title "Release vVERSION" and generate new tag
|
||||||
|
|
||||||
5. Push master and the new tag
|
5. Run `make clean test` to ensure all tests pass from scratch and the CI is green
|
||||||
|
|
||||||
6. Release new docs with `make release_docs`, move docs to `docs/stable`
|
6. Ensure minimum supported Hex works with new release (instructions upcoming)
|
||||||
|
|
||||||
7. Release new zip with `make release_zip`, push new zip to GitHub Releases, name it `Precompiled.zip`
|
7. Push branch and the new tag
|
||||||
|
|
||||||
8. Fast-forward merge master into stable branch with `git merge master --ff` and push it
|
8. Release new docs with `make release_docs`, copy docs to `docs/stable` if appropriate, and push
|
||||||
|
|
||||||
9. After release, bump versions, add `-dev` back and commit
|
9. Release new zip with `make release_zip`, push `Precompiled.zip` to GitHub Releases
|
||||||
|
|
||||||
10. `make release_docs` once again and push it to `elixir-lang/docs`
|
10. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
||||||
|
|
||||||
11. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
11. Build and push standalone Mix with `make publish_mix` (requires AWS credentials)
|
||||||
|
|
||||||
|
## New vMAJOR.MINOR releases
|
||||||
|
|
||||||
|
12. Create a new branch "vMAJOR.MINOR"
|
||||||
|
|
||||||
|
13. Move docs generation to `docs/vMAJOR.MINOR` and copy them from `docs/stable`
|
||||||
|
|
||||||
|
14. In master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||||
|
|
||||||
|
15. `make release_docs` and push it to `elixir-lang/docs`
|
||||||
|
|
||||||
## Places where version is mentioned
|
## Places where version is mentioned
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -81,7 +81,7 @@ done
|
|||||||
SELF=$(readlink_f "$0")
|
SELF=$(readlink_f "$0")
|
||||||
SCRIPT_PATH=$(dirname "$SELF")
|
SCRIPT_PATH=$(dirname "$SELF")
|
||||||
|
|
||||||
if [ "$OSTYPE" = "cygwin" ]; then SCRIPT_PATH=$(cygpath -m "SCRIPT_PATH"); fi
|
if [ "$OSTYPE" = "cygwin" ]; then SCRIPT_PATH=$(cygpath -m "$SCRIPT_PATH"); fi
|
||||||
if [ "$MODE" != "iex" ]; then ERL="-noshell -s elixir start_cli $ERL"; fi
|
if [ "$MODE" != "iex" ]; then ERL="-noshell -s elixir start_cli $ERL"; fi
|
||||||
|
|
||||||
# Check for terminal support
|
# Check for terminal support
|
||||||
|
|||||||
+5
-1
@@ -1,5 +1,9 @@
|
|||||||
defmodule EEx.SyntaxError do
|
defmodule EEx.SyntaxError do
|
||||||
defexception [:message]
|
defexception [:message, :file, :line]
|
||||||
|
|
||||||
|
def message(exception) do
|
||||||
|
"#{exception.file}:#{exception.line}: #{exception.message}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule EEx do
|
defmodule EEx do
|
||||||
|
|||||||
@@ -12,10 +12,14 @@ defmodule EEx.Compiler do
|
|||||||
def compile(source, opts) do
|
def compile(source, opts) do
|
||||||
file = opts[:file] || "nofile"
|
file = opts[:file] || "nofile"
|
||||||
line = opts[:line] || 1
|
line = opts[:line] || 1
|
||||||
tokens = EEx.Tokenizer.tokenize(source, line)
|
case EEx.Tokenizer.tokenize(source, line) do
|
||||||
state = %{engine: opts[:engine] || @default_engine,
|
{:ok, tokens} ->
|
||||||
file: file, line: line, quoted: [], start_line: nil}
|
state = %{engine: opts[:engine] || @default_engine,
|
||||||
generate_buffer(tokens, "", [], state)
|
file: file, line: line, quoted: [], start_line: nil}
|
||||||
|
generate_buffer(tokens, "", [], state)
|
||||||
|
{:error, line, message} ->
|
||||||
|
raise EEx.SyntaxError, line: line, file: file, message: message
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generates the buffers by handling each expression from the tokenizer
|
# Generates the buffers by handling each expression from the tokenizer
|
||||||
@@ -51,16 +55,17 @@ defmodule EEx.Compiler do
|
|||||||
{buffer, t}
|
{buffer, t}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp generate_buffer([{:end_expr, line, _, chars}|_], _buffer, [], _state) do
|
defp generate_buffer([{:end_expr, line, _, chars}|_], _buffer, [], state) do
|
||||||
raise EEx.SyntaxError, message: "unexpected token: #{inspect chars} at line #{inspect line}"
|
raise EEx.SyntaxError, message: "unexpected token #{inspect chars}", file: state.file, line: line
|
||||||
end
|
end
|
||||||
|
|
||||||
defp generate_buffer([], buffer, [], state) do
|
defp generate_buffer([], buffer, [], state) do
|
||||||
state.engine.handle_body(buffer)
|
state.engine.handle_body(buffer)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp generate_buffer([], _buffer, _scope, _state) do
|
defp generate_buffer([], _buffer, _scope, state) do
|
||||||
raise EEx.SyntaxError, message: "unexpected end of string. expecting a closing <% end %>."
|
raise EEx.SyntaxError, message: "unexpected end of string, expected a closing '<% end %>'",
|
||||||
|
file: state.file, line: state.line
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a placeholder and wrap it inside the expression block
|
# Creates a placeholder and wrap it inside the expression block
|
||||||
|
|||||||
@@ -98,19 +98,16 @@ defmodule EEx.Engine do
|
|||||||
"""
|
"""
|
||||||
def handle_expr(buffer, "=", expr) do
|
def handle_expr(buffer, "=", expr) do
|
||||||
quote do
|
quote do
|
||||||
tmp = unquote(buffer)
|
tmp1 = unquote(buffer)
|
||||||
tmp <> (case unquote(expr) do
|
tmp1 <> String.Chars.to_string(unquote(expr))
|
||||||
bin when is_binary(bin) -> bin
|
|
||||||
oth -> String.Chars.to_string(oth)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_expr(buffer, "", expr) do
|
def handle_expr(buffer, "", expr) do
|
||||||
quote do
|
quote do
|
||||||
tmp = unquote(buffer)
|
tmp2 = unquote(buffer)
|
||||||
unquote(expr)
|
unquote(expr)
|
||||||
tmp
|
tmp2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ defmodule EEx.Tokenizer do
|
|||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Tokenizes the given char list or binary.
|
Tokenizes the given char list or binary.
|
||||||
It returns 4 different types of tokens as result:
|
|
||||||
|
It returns {:ok, list} with the following tokens:
|
||||||
|
|
||||||
* `{:text, contents}`
|
* `{:text, contents}`
|
||||||
* `{:expr, line, marker, contents}`
|
* `{:expr, line, marker, contents}`
|
||||||
@@ -11,33 +12,43 @@ defmodule EEx.Tokenizer do
|
|||||||
* `{:middle_expr, line, marker, contents}`
|
* `{:middle_expr, line, marker, contents}`
|
||||||
* `{:end_expr, line, marker, contents}`
|
* `{:end_expr, line, marker, contents}`
|
||||||
|
|
||||||
|
Or `{:error, line, error}` in case of errors.
|
||||||
"""
|
"""
|
||||||
def tokenize(bin, line) when is_binary(bin) do
|
def tokenize(bin, line) when is_binary(bin) do
|
||||||
tokenize(String.to_char_list(bin), line)
|
tokenize(String.to_char_list(bin), line)
|
||||||
end
|
end
|
||||||
|
|
||||||
def tokenize(list, line) do
|
def tokenize(list, line) do
|
||||||
Enum.reverse(tokenize(list, line, [], []))
|
tokenize(list, line, [], [])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize('<%%' ++ t, line, buffer, acc) do
|
defp tokenize('<%%' ++ t, line, buffer, acc) do
|
||||||
{buffer, new_line, rest} = tokenize_expr t, line, [?%, ?<|buffer]
|
case expr(t, line, [?%, ?<|buffer]) do
|
||||||
tokenize rest, new_line, [?>, ?%|buffer], acc
|
{:error, _, _} = error -> error
|
||||||
|
{:ok, buffer, new_line, rest} ->
|
||||||
|
tokenize rest, new_line, [?>, ?%|buffer], acc
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize('<%#' ++ t, line, buffer, acc) do
|
defp tokenize('<%#' ++ t, line, buffer, acc) do
|
||||||
{_, new_line, rest} = tokenize_expr t, line, []
|
case expr(t, line, []) do
|
||||||
tokenize rest, new_line, buffer, acc
|
{:error, _, _} = error -> error
|
||||||
|
{:ok, _, new_line, rest} ->
|
||||||
|
tokenize rest, new_line, buffer, acc
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize('<%' ++ t, line, buffer, acc) do
|
defp tokenize('<%' ++ t, line, buffer, acc) do
|
||||||
{marker, t} = retrieve_marker(t)
|
{marker, t} = retrieve_marker(t)
|
||||||
{expr, new_line, rest} = tokenize_expr t, line, []
|
|
||||||
|
|
||||||
token = token_name(expr)
|
case expr(t, line, []) do
|
||||||
acc = tokenize_text(buffer, acc)
|
{:error, _, _} = error -> error
|
||||||
final = {token, line, marker, Enum.reverse(expr)}
|
{:ok, expr, new_line, rest} ->
|
||||||
tokenize rest, new_line, [], [final | acc]
|
token = token_name(expr)
|
||||||
|
acc = tokenize_text(buffer, acc)
|
||||||
|
final = {token, line, marker, Enum.reverse(expr)}
|
||||||
|
tokenize rest, new_line, [], [final | acc]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize('\n' ++ t, line, buffer, acc) do
|
defp tokenize('\n' ++ t, line, buffer, acc) do
|
||||||
@@ -49,7 +60,7 @@ defmodule EEx.Tokenizer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize([], _line, buffer, acc) do
|
defp tokenize([], _line, buffer, acc) do
|
||||||
tokenize_text(buffer, acc)
|
{:ok, Enum.reverse(tokenize_text(buffer, acc))}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Retrieve marker for <%
|
# Retrieve marker for <%
|
||||||
@@ -64,20 +75,20 @@ defmodule EEx.Tokenizer do
|
|||||||
|
|
||||||
# Tokenize an expression until we find %>
|
# Tokenize an expression until we find %>
|
||||||
|
|
||||||
defp tokenize_expr([?%, ?>|t], line, buffer) do
|
defp expr([?%, ?>|t], line, buffer) do
|
||||||
{buffer, line, t}
|
{:ok, buffer, line, t}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize_expr('\n' ++ t, line, buffer) do
|
defp expr('\n' ++ t, line, buffer) do
|
||||||
tokenize_expr t, line + 1, [?\n|buffer]
|
expr t, line + 1, [?\n|buffer]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize_expr([h|t], line, buffer) do
|
defp expr([h|t], line, buffer) do
|
||||||
tokenize_expr t, line, [h|buffer]
|
expr t, line, [h|buffer]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tokenize_expr([], _line, _buffer) do
|
defp expr([], line, _buffer) do
|
||||||
raise EEx.SyntaxError, message: "missing token: %>"
|
{:error, line, "missing token '%>'"}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Receive an expression content and check
|
# Receive an expression content and check
|
||||||
|
|||||||
@@ -5,23 +5,26 @@ defmodule EEx.TokenizerTest do
|
|||||||
require EEx.Tokenizer, as: T
|
require EEx.Tokenizer, as: T
|
||||||
|
|
||||||
test "simple chars lists" do
|
test "simple chars lists" do
|
||||||
assert T.tokenize('foo', 1) == [ {:text, 'foo'} ]
|
assert T.tokenize('foo', 1) == {:ok, [{:text, 'foo'}]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "simple strings" do
|
test "simple strings" do
|
||||||
assert T.tokenize("foo", 1) == [ {:text, 'foo'} ]
|
assert T.tokenize("foo", 1) == {:ok, [{:text, 'foo'}]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with embedded code" do
|
test "strings with embedded code" do
|
||||||
assert T.tokenize('foo <% bar %>', 1) == [ {:text, 'foo '}, {:expr, 1, "", ' bar '} ]
|
assert T.tokenize('foo <% bar %>', 1) ==
|
||||||
|
{:ok, [{:text, 'foo '}, {:expr, 1, "", ' bar '}]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with embedded equals code" do
|
test "strings with embedded equals code" do
|
||||||
assert T.tokenize('foo <%= bar %>', 1) == [ {:text, 'foo '}, {:expr, 1, "=", ' bar '} ]
|
assert T.tokenize('foo <%= bar %>', 1) ==
|
||||||
|
{:ok, [{:text, 'foo '}, {:expr, 1, "=", ' bar '}]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with more than one line" do
|
test "strings with more than one line" do
|
||||||
assert T.tokenize('foo\n<%= bar %>', 1) == [ {:text, 'foo\n'}, {:expr, 2, "=", ' bar '} ]
|
assert T.tokenize('foo\n<%= bar %>', 1) ==
|
||||||
|
{:ok, [{:text, 'foo\n'}, {:expr, 2, "=", ' bar '}]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with more than one line and expression with more than one line" do
|
test "strings with more than one line and expression with more than one line" do
|
||||||
@@ -32,50 +35,50 @@ baz %>
|
|||||||
<% foo %>
|
<% foo %>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
assert T.tokenize(string, 1) == [
|
assert T.tokenize(string, 1) == {:ok, [
|
||||||
{:text, 'foo '},
|
{:text, 'foo '},
|
||||||
{:expr, 1, "=", ' bar\n\nbaz '},
|
{:expr, 1, "=", ' bar\n\nbaz '},
|
||||||
{:text, '\n'},
|
{:text, '\n'},
|
||||||
{:expr, 4, "", ' foo '},
|
{:expr, 4, "", ' foo '},
|
||||||
{:text, '\n'}
|
{:text, '\n'}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "quotation" do
|
test "quotation" do
|
||||||
assert T.tokenize('foo <%% true %>', 1) == [
|
assert T.tokenize('foo <%% true %>', 1) == {:ok, [
|
||||||
{:text, 'foo <% true %>'}
|
{:text, 'foo <% true %>'}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "quotation with do/end" do
|
test "quotation with do/end" do
|
||||||
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1) == [
|
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1) == {:ok, [
|
||||||
{:text, 'foo <% true do %>bar<% end %>'}
|
{:text, 'foo <% true do %>bar<% end %>'}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "comments" do
|
test "comments" do
|
||||||
assert T.tokenize('foo <%# true %>', 1) == [
|
assert T.tokenize('foo <%# true %>', 1) == {:ok, [
|
||||||
{:text, 'foo '}
|
{:text, 'foo '}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "comments with do/end" do
|
test "comments with do/end" do
|
||||||
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1) == [
|
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1) == {:ok, [
|
||||||
{:text, 'foo bar'}
|
{:text, 'foo bar'}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with embedded do end" do
|
test "strings with embedded do end" do
|
||||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == [
|
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, [
|
||||||
{:text, 'foo '},
|
{:text, 'foo '},
|
||||||
{:start_expr, 1, "", ' if true do '},
|
{:start_expr, 1, "", ' if true do '},
|
||||||
{:text, 'bar'},
|
{:text, 'bar'},
|
||||||
{:end_expr, 1, "", ' end '}
|
{:end_expr, 1, "", ' end '}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with embedded -> end" do
|
test "strings with embedded -> end" do
|
||||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) == [
|
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) == {:ok, [
|
||||||
{:text, 'foo '},
|
{:text, 'foo '},
|
||||||
{:start_expr, 1, "", ' cond do '},
|
{:start_expr, 1, "", ' cond do '},
|
||||||
{:middle_expr, 1, "", ' false -> '},
|
{:middle_expr, 1, "", ' false -> '},
|
||||||
@@ -83,23 +86,21 @@ baz %>
|
|||||||
{:middle_expr, 1, "", ' true -> '},
|
{:middle_expr, 1, "", ' true -> '},
|
||||||
{:text, 'baz'},
|
{:text, 'baz'},
|
||||||
{:end_expr, 1, "", ' end '}
|
{:end_expr, 1, "", ' end '}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strings with embedded keywords blocks" do
|
test "strings with embedded keywords blocks" do
|
||||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == [
|
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, [
|
||||||
{:text, 'foo '},
|
{:text, 'foo '},
|
||||||
{:start_expr, 1, "", ' if true do '},
|
{:start_expr, 1, "", ' if true do '},
|
||||||
{:text, 'bar'},
|
{:text, 'bar'},
|
||||||
{:middle_expr, 1, "", ' else '},
|
{:middle_expr, 1, "", ' else '},
|
||||||
{:text, 'baz'},
|
{:text, 'baz'},
|
||||||
{:end_expr, 1, "", ' end '}
|
{:end_expr, 1, "", ' end '}
|
||||||
]
|
]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raise syntax error when there is start mark and no end mark" do
|
test "raise syntax error when there is start mark and no end mark" do
|
||||||
assert_raise EEx.SyntaxError, "missing token: %>", fn ->
|
assert T.tokenize('foo <% :bar', 1) == {:error, 1, "missing token '%>'"}
|
||||||
T.tokenize('foo <% :bar', 1)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -111,25 +111,25 @@ defmodule EExTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "raises a syntax error when the token is invalid" do
|
test "raises a syntax error when the token is invalid" do
|
||||||
assert_raise EEx.SyntaxError, "missing token: %>", fn ->
|
assert_raise EEx.SyntaxError, "nofile:1: missing token '%>'", fn ->
|
||||||
EEx.compile_string "foo <%= bar"
|
EEx.compile_string "foo <%= bar"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raises a syntax error when end expression is found without a start expression" do
|
test "raises a syntax error when end expression is found without a start expression" do
|
||||||
assert_raise EEx.SyntaxError, "unexpected token: ' end ' at line 1", fn ->
|
assert_raise EEx.SyntaxError, "nofile:1: unexpected token ' end '", fn ->
|
||||||
EEx.compile_string "foo <% end %>"
|
EEx.compile_string "foo <% end %>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raises a syntax error when start expression is found without an end expression" do
|
test "raises a syntax error when start expression is found without an end expression" do
|
||||||
assert_raise EEx.SyntaxError, "unexpected end of string. expecting a closing <% end %>.", fn ->
|
assert_raise EEx.SyntaxError, "nofile:2: unexpected end of string, expected a closing '<% end %>'", fn ->
|
||||||
EEx.compile_string "foo <% if true do %>"
|
EEx.compile_string "foo\n<% if true do %>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raises a syntax error when nested end expression is found without an start expression" do
|
test "raises a syntax error when nested end expression is found without a start expression" do
|
||||||
assert_raise EEx.SyntaxError, "unexpected token: ' end ' at line 1", fn ->
|
assert_raise EEx.SyntaxError, "nofile:1: unexpected token ' end '", fn ->
|
||||||
EEx.compile_string "foo <% if true do %><% end %><% end %>"
|
EEx.compile_string "foo <% if true do %><% end %><% end %>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -252,7 +252,7 @@ Number <%= x %>
|
|||||||
assert_eval expected, string
|
assert_eval expected, string
|
||||||
end
|
end
|
||||||
|
|
||||||
test "do not consider already finished functions" do
|
test "properly handle functions on the left side of clauses" do
|
||||||
expected = """
|
expected = """
|
||||||
foo
|
foo
|
||||||
|
|
||||||
@@ -287,6 +287,18 @@ foo
|
|||||||
assert_eval "\n\n Good\n \n", string
|
assert_eval "\n\n Good\n \n", string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "evaluates expressions with buffers" do
|
||||||
|
string = """
|
||||||
|
<%= 123 %>
|
||||||
|
<% if true do %>
|
||||||
|
<%= 456 %>
|
||||||
|
<% end %>
|
||||||
|
<%= 789 %>
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert_eval "123\n\n789\n", string
|
||||||
|
end
|
||||||
|
|
||||||
test "for comprehensions" do
|
test "for comprehensions" do
|
||||||
string = """
|
string = """
|
||||||
<%= for _name <- packages || [] do %>
|
<%= for _name <- packages || [] do %>
|
||||||
|
|||||||
+5
-13
@@ -51,16 +51,7 @@ defmodule Code do
|
|||||||
:code.del_path(to_char_list(Path.expand path))
|
:code.del_path(to_char_list(Path.expand path))
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc false
|
||||||
Re-add paths given to the command line to keep their position
|
|
||||||
on the overall code path.
|
|
||||||
|
|
||||||
Some tools may change the code path by prepending new items but
|
|
||||||
still want the paths given by the user to have higher priority.
|
|
||||||
Calling this function guarantees the paths are re-added on
|
|
||||||
top of the user given ones.
|
|
||||||
"""
|
|
||||||
@spec readd_paths() :: :ok
|
|
||||||
def readd_paths() do
|
def readd_paths() do
|
||||||
{pa, pz} = :elixir_code_server.call(:paths)
|
{pa, pz} = :elixir_code_server.call(:paths)
|
||||||
:code.add_pathsa(pa)
|
:code.add_pathsa(pa)
|
||||||
@@ -260,7 +251,7 @@ defmodule Code do
|
|||||||
Accepts `relative_to` as an argument to tell where the file is located.
|
Accepts `relative_to` as an argument to tell where the file is located.
|
||||||
|
|
||||||
While `load_file` loads a file and returns the loaded modules and their
|
While `load_file` loads a file and returns the loaded modules and their
|
||||||
byte code, `eval_file` simply evalutes the file contents and returns the
|
byte code, `eval_file` simply evaluates the file contents and returns the
|
||||||
evaluation result and its bindings.
|
evaluation result and its bindings.
|
||||||
"""
|
"""
|
||||||
def eval_file(file, relative_to \\ nil) do
|
def eval_file(file, relative_to \\ nil) do
|
||||||
@@ -325,7 +316,7 @@ defmodule Code do
|
|||||||
Check `compiler_options/1` for more information.
|
Check `compiler_options/1` for more information.
|
||||||
"""
|
"""
|
||||||
def compiler_options do
|
def compiler_options do
|
||||||
:elixir_code_server.call :compiler_options
|
:elixir_config.get :compiler_options
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -364,7 +355,8 @@ defmodule Code do
|
|||||||
bad = bad |> Keyword.keys |> Enum.join(", ")
|
bad = bad |> Keyword.keys |> Enum.join(", ")
|
||||||
raise ArgumentError, message: "unknown compiler options: #{bad}"
|
raise ArgumentError, message: "unknown compiler options: #{bad}"
|
||||||
end
|
end
|
||||||
:elixir_code_server.cast {:compiler_options, opts}
|
update = &:orddict.merge(fn(_, _, value) -> value end, &1, opts)
|
||||||
|
:elixir_config.update :compiler_options, update
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|||||||
+16
-7
@@ -1261,6 +1261,15 @@ defmodule Enum do
|
|||||||
:lists.foldl(fun, acc, collection)
|
:lists.foldl(fun, acc, collection)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reduce(%{__struct__: _} = collection, acc, fun) do
|
||||||
|
Enumerable.reduce(collection, {:cont, acc},
|
||||||
|
fn x, acc -> {:cont, fun.(x, acc)} end) |> elem(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reduce(%{} = collection, acc, fun) do
|
||||||
|
:maps.fold(fn k, v, acc -> fun.({k, v}, acc) end, acc, collection)
|
||||||
|
end
|
||||||
|
|
||||||
def reduce(collection, acc, fun) do
|
def reduce(collection, acc, fun) do
|
||||||
Enumerable.reduce(collection, {:cont, acc},
|
Enumerable.reduce(collection, {:cont, acc},
|
||||||
fn x, acc -> {:cont, fun.(x, acc)} end) |> elem(1)
|
fn x, acc -> {:cont, fun.(x, acc)} end) |> elem(1)
|
||||||
@@ -1722,23 +1731,23 @@ defmodule Enum do
|
|||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
def take(collection, count) when is_list(collection) and count > 0 do
|
def take(collection, count) when is_list(collection) and is_integer(count) and count > 0 do
|
||||||
do_take(collection, count)
|
do_take(collection, count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def take(collection, count) when count > 0 do
|
def take(collection, count) when count > 0 and is_integer(count) do
|
||||||
{_, {res, _}} =
|
{_, {res, _}} =
|
||||||
Enumerable.reduce(collection, {:cont, {[], count}}, fn(entry, {list, count}) ->
|
Enumerable.reduce(collection, {:cont, {[], count}}, fn(entry, {list, count}) ->
|
||||||
if count > 1 do
|
case count do
|
||||||
{:cont, {[entry|list], count - 1}}
|
0 -> {:halt, {list, count}}
|
||||||
else
|
1 -> {:halt, {[entry|list], count - 1}}
|
||||||
{:halt, {[entry|list], count}}
|
_ -> {:cont, {[entry|list], count - 1}}
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
:lists.reverse(res)
|
:lists.reverse(res)
|
||||||
end
|
end
|
||||||
|
|
||||||
def take(collection, count) when count < 0 do
|
def take(collection, count) when count < 0 and is_integer(count) do
|
||||||
Stream.take(collection, count).({:cont, []}, &{:cont, [&1|&2]})
|
Stream.take(collection, count).({:cont, []}, &{:cont, [&1|&2]})
|
||||||
|> elem(1) |> :lists.reverse
|
|> elem(1) |> :lists.reverse
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -564,6 +564,14 @@ defmodule BadStructError do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defmodule BadMapError do
|
||||||
|
defexception [term: nil]
|
||||||
|
|
||||||
|
def message(exception) do
|
||||||
|
"expected a map, got: #{inspect(exception.term)}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defmodule MatchError do
|
defmodule MatchError do
|
||||||
defexception [term: nil]
|
defexception [term: nil]
|
||||||
|
|
||||||
@@ -669,7 +677,12 @@ defmodule KeyError do
|
|||||||
defexception key: nil, term: nil
|
defexception key: nil, term: nil
|
||||||
|
|
||||||
def message(exception) do
|
def message(exception) do
|
||||||
"key #{inspect exception.key} not found in: #{inspect exception.term}"
|
msg = "key #{inspect exception.key} not found"
|
||||||
|
if exception.term != nil do
|
||||||
|
msg <> " in: #{inspect exception.term}"
|
||||||
|
else
|
||||||
|
msg
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -768,6 +781,20 @@ defmodule ErlangError do
|
|||||||
%MatchError{term: term}
|
%MatchError{term: term}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalize({:badmap, term}, _stacktrace) do
|
||||||
|
%BadMapError{term: term}
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize({:badkey, key}, stacktrace) do
|
||||||
|
term =
|
||||||
|
case stacktrace || :erlang.get_stacktrace do
|
||||||
|
[{:maps, :update, [_, _, map], _}|_] -> map
|
||||||
|
[{:maps, :get, [_, map], _}|_] -> map
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
%KeyError{key: key, term: term}
|
||||||
|
end
|
||||||
|
|
||||||
def normalize({:case_clause, term}, _stacktrace) do
|
def normalize({:case_clause, term}, _stacktrace) do
|
||||||
%CaseClauseError{term: term}
|
%CaseClauseError{term: term}
|
||||||
end
|
end
|
||||||
|
|||||||
+18
-54
@@ -28,75 +28,39 @@ defmodule Float do
|
|||||||
"""
|
"""
|
||||||
@spec parse(binary) :: {float, binary} | :error
|
@spec parse(binary) :: {float, binary} | :error
|
||||||
def parse("-" <> binary) do
|
def parse("-" <> binary) do
|
||||||
case parse_unsign(binary) do
|
case parse_unsigned(binary) do
|
||||||
:error -> :error
|
:error -> :error
|
||||||
{number, remainder} -> {-number, remainder}
|
{number, remainder} -> {-number, remainder}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(binary) do
|
def parse(binary) do
|
||||||
parse_unsign(binary)
|
parse_unsigned(binary)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp parse_unsign("-" <> _), do: :error
|
defp parse_unsigned(<<char, rest::binary>>) when char in ?0..?9, do:
|
||||||
defp parse_unsign(binary) when is_binary(binary) do
|
parse_unsigned(rest, false, false, <<char>>)
|
||||||
case Integer.parse binary do
|
|
||||||
:error -> :error
|
|
||||||
{integer_part, after_integer} -> parse_unsign after_integer, integer_part
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Dot followed by digit is required afterwards or we are done
|
defp parse_unsigned(binary) when is_binary(binary), do:
|
||||||
defp parse_unsign(<< ?., char, rest :: binary >>, int) when char in ?0..?9 do
|
:error
|
||||||
parse_unsign(rest, char - ?0, 1, int)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp parse_unsign(rest, int) do
|
defp parse_unsigned(<<char, rest :: binary>>, dot?, e?, acc) when char in ?0..?9, do:
|
||||||
{:erlang.float(int), rest}
|
parse_unsigned(rest, dot?, e?, <<acc::binary, char>>)
|
||||||
end
|
|
||||||
|
|
||||||
# Handle decimal points
|
defp parse_unsigned(<<?., char, rest :: binary>>, false, false, acc) when char in ?0..?9, do:
|
||||||
defp parse_unsign(<< char, rest :: binary >>, float, decimal, int) when char in ?0..?9 do
|
parse_unsigned(rest, true, false, <<acc::binary, ?., char>>)
|
||||||
parse_unsign rest, 10 * float + (char - ?0), decimal + 1, int
|
|
||||||
end
|
|
||||||
|
|
||||||
defp parse_unsign(<< ?e, after_e :: binary >>, float, decimal, int) do
|
defp parse_unsigned(<<?e, char, rest :: binary>>, dot?, false, acc) when char in ?0..?9, do:
|
||||||
case Integer.parse after_e do
|
parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, char>>)
|
||||||
:error ->
|
|
||||||
# Note we rebuild the binary here instead of breaking it apart at
|
|
||||||
# the function clause because the current approach copies a binary
|
|
||||||
# just on this branch. If we broke it apart in the function clause,
|
|
||||||
# the copy would happen when calling Integer.parse/1.
|
|
||||||
{floatify(int, float, decimal), << ?e, after_e :: binary >>}
|
|
||||||
{exponential, after_exponential} ->
|
|
||||||
{floatify(int, float, decimal, exponential), after_exponential}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp parse_unsign(bitstring, float, decimal, int) do
|
defp parse_unsigned(<<?e, ?-, char, rest :: binary>>, dot?, false, acc) when char in ?0..?9, do:
|
||||||
{floatify(int, float, decimal), bitstring}
|
parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, ?-, char>>)
|
||||||
end
|
|
||||||
|
|
||||||
defp floatify(int, float, decimal, exponential \\ 0) do
|
defp parse_unsigned(rest, dot?, _e?, acc), do:
|
||||||
multiplier = if int < 0, do: -1.0, else: 1.0
|
{:erlang.binary_to_float(add_dot(acc, dot?)), rest}
|
||||||
|
|
||||||
# Try to ensure the minimum amount of rounding errors
|
defp add_dot(acc, true), do: acc
|
||||||
result = multiplier * (abs(int) * :math.pow(10, decimal) + float) * :math.pow(10, exponential - decimal)
|
defp add_dot(acc, false), do: acc <> ".0"
|
||||||
|
|
||||||
# Try avoiding stuff like this:
|
|
||||||
# iex(1)> 0.0001 * 75
|
|
||||||
# 0.007500000000000001
|
|
||||||
# Due to IEEE 754 floating point standard
|
|
||||||
# http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
|
|
||||||
|
|
||||||
final_decimal_places = decimal - exponential
|
|
||||||
if final_decimal_places > 0 do
|
|
||||||
decimal_power_round = :math.pow(10, final_decimal_places)
|
|
||||||
trunc(result * decimal_power_round) / decimal_power_round
|
|
||||||
else
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Rounds a float to the largest integer less than or equal to `num`.
|
Rounds a float to the largest integer less than or equal to `num`.
|
||||||
|
|||||||
+16
-19
@@ -188,7 +188,7 @@ defmodule GenEvent do
|
|||||||
@type manager :: pid | name | {atom, node}
|
@type manager :: pid | name | {atom, node}
|
||||||
|
|
||||||
@typedoc "Supported values for new handlers"
|
@typedoc "Supported values for new handlers"
|
||||||
@type handler :: atom | {atom, term} | pid
|
@type handler :: atom | {atom, term} | {pid, reference}
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
defmacro __using__(_) do
|
defmacro __using__(_) do
|
||||||
@@ -601,11 +601,7 @@ defmodule GenEvent do
|
|||||||
reply(tag, reply)
|
reply(tag, reply)
|
||||||
loop(parent, name, handlers, debug, hib)
|
loop(parent, name, handlers, debug, hib)
|
||||||
{_from, tag, {:add_handler, handler, args}} ->
|
{_from, tag, {:add_handler, handler, args}} ->
|
||||||
{hib, reply, handlers} = server_add_handler(handler, args, handlers, nil)
|
{hib, reply, handlers} = server_add_handler(handler, args, handlers)
|
||||||
reply(tag, reply)
|
|
||||||
loop(parent, name, handlers, debug, hib)
|
|
||||||
{_from, tag, {:add_handler, handler, args, notify}} ->
|
|
||||||
{hib, reply, handlers} = server_add_handler(handler, args, handlers, notify)
|
|
||||||
reply(tag, reply)
|
reply(tag, reply)
|
||||||
loop(parent, name, handlers, debug, hib)
|
loop(parent, name, handlers, debug, hib)
|
||||||
{_from, tag, {:add_mon_handler, handler, args, notify}} ->
|
{_from, tag, {:add_mon_handler, handler, args, notify}} ->
|
||||||
@@ -739,13 +735,13 @@ defmodule GenEvent do
|
|||||||
IO.puts dev, "*DBG* #{inspect name}: #{inspect dbg}"
|
IO.puts dev, "*DBG* #{inspect name}: #{inspect dbg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp server_add_handler({module, id}, args, handlers, notify) do
|
defp server_add_handler({module, id}, args, handlers) do
|
||||||
handler = handler(module: module, id: {module, id}, pid: notify)
|
handler = handler(module: module, id: {module, id})
|
||||||
do_add_handler(module, handler, args, handlers, :ok)
|
do_add_handler(module, handler, args, handlers, :ok)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp server_add_handler(module, args, handlers, notify) do
|
defp server_add_handler(module, args, handlers) do
|
||||||
handler = handler(module: module, id: module, pid: notify)
|
handler = handler(module: module, id: module)
|
||||||
do_add_handler(module, handler, args, handlers, :ok)
|
do_add_handler(module, handler, args, handlers, :ok)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -763,7 +759,7 @@ defmodule GenEvent do
|
|||||||
|
|
||||||
defp server_add_process_handler(pid, handlers, notify) do
|
defp server_add_process_handler(pid, handlers, notify) do
|
||||||
ref = Process.monitor(pid)
|
ref = Process.monitor(pid)
|
||||||
handler = handler(module: GenEvent.Stream, id: pid,
|
handler = handler(module: GenEvent.Stream, id: {self(), ref},
|
||||||
pid: notify, ref: ref)
|
pid: notify, ref: ref)
|
||||||
do_add_handler(GenEvent.Stream, handler, {pid, ref}, handlers, {self(), ref})
|
do_add_handler(GenEvent.Stream, handler, {pid, ref}, handlers, {self(), ref})
|
||||||
end
|
end
|
||||||
@@ -779,7 +775,7 @@ defmodule GenEvent do
|
|||||||
if sup do
|
if sup do
|
||||||
server_add_mon_handler(module2, {args2, state}, handlers, sup)
|
server_add_mon_handler(module2, {args2, state}, handlers, sup)
|
||||||
else
|
else
|
||||||
server_add_handler(module2, {args2, state}, handlers, nil)
|
server_add_handler(module2, {args2, state}, handlers)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -796,7 +792,7 @@ defmodule GenEvent do
|
|||||||
|
|
||||||
defp server_split_process_handlers(mode, event, [handler|t], handlers, streams) do
|
defp server_split_process_handlers(mode, event, [handler|t], handlers, streams) do
|
||||||
case handler(handler, :id) do
|
case handler(handler, :id) do
|
||||||
pid when is_pid(pid) ->
|
{pid, _ref} when is_pid(pid) ->
|
||||||
server_process_notify(mode, event, handler)
|
server_process_notify(mode, event, handler)
|
||||||
server_split_process_handlers(mode, event, t, handlers, [handler|streams])
|
server_split_process_handlers(mode, event, t, handlers, [handler|streams])
|
||||||
_ ->
|
_ ->
|
||||||
@@ -858,16 +854,17 @@ defmodule GenEvent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp server_collect_process_handlers(mode, event, [handler|t], handlers, name) when mode in [:sync, :ack] do
|
defp server_collect_process_handlers(mode, event, [handler|t], handlers, name) when mode in [:sync, :ack] do
|
||||||
handler(ref: ref) = handler
|
handler(ref: ref, id: id) = handler
|
||||||
|
|
||||||
receive do
|
receive do
|
||||||
{^ref, :ok} ->
|
{^ref, :ok} ->
|
||||||
server_collect_process_handlers(mode, event, t, [handler|handlers], name)
|
server_collect_process_handlers(mode, event, t, [handler|handlers], name)
|
||||||
{^ref, :done} ->
|
{_from, tag, {:delete_handler, ^id, args}} ->
|
||||||
do_terminate(handler, :remove_handler, event, name, :normal)
|
do_terminate(handler, args, :remove, name, :normal)
|
||||||
|
reply(tag, :ok)
|
||||||
server_collect_process_handlers(mode, event, t, handlers, name)
|
server_collect_process_handlers(mode, event, t, handlers, name)
|
||||||
{:DOWN, ^ref, _, _, reason} ->
|
{:DOWN, ^ref, _, _, reason} ->
|
||||||
do_terminate(handler, {:stop, reason}, :remove, name, :shutdown)
|
do_terminate(handler, {:stop, reason}, :DOWN, name, :shutdown)
|
||||||
server_collect_process_handlers(mode, event, t, handlers, name)
|
server_collect_process_handlers(mode, event, t, handlers, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -939,7 +936,7 @@ defmodule GenEvent do
|
|||||||
case :lists.keyfind(ref, handler(:ref) + 1, handlers) do
|
case :lists.keyfind(ref, handler(:ref) + 1, handlers) do
|
||||||
false -> :error
|
false -> :error
|
||||||
handler ->
|
handler ->
|
||||||
do_terminate(handler, {:stop, reason}, :remove, name, :shutdown)
|
do_terminate(handler, {:stop, reason}, :DOWN, name, :shutdown)
|
||||||
{:ok, :lists.keydelete(ref, handler(:ref) + 1, handlers)}
|
{:ok, :lists.keydelete(ref, handler(:ref) + 1, handlers)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1018,7 +1015,7 @@ defmodule GenEvent do
|
|||||||
case reason do
|
case reason do
|
||||||
{:undef, [{m,f,a,_}|_]=mfas} ->
|
{:undef, [{m,f,a,_}|_]=mfas} ->
|
||||||
cond do
|
cond do
|
||||||
:code.is_loaded(m) ->
|
:code.is_loaded(m) === false ->
|
||||||
{:"module could not be loaded", mfas}
|
{:"module could not be loaded", mfas}
|
||||||
function_exported?(m, f, length(a)) ->
|
function_exported?(m, f, length(a)) ->
|
||||||
reason
|
reason
|
||||||
|
|||||||
@@ -80,7 +80,8 @@ defimpl Enumerable, for: GenEvent.Stream do
|
|||||||
|
|
||||||
defp start(%{manager: manager} = stream) do
|
defp start(%{manager: manager} = stream) do
|
||||||
try do
|
try do
|
||||||
{:ok, {pid, ref}} = :gen.call(manager, self(), {:add_process_handler, self(), self()}, :infinity)
|
{:ok, {pid, ref}} = :gen.call(manager, self(),
|
||||||
|
{:add_process_handler, self(), self()}, :infinity)
|
||||||
mon_ref = Process.monitor(pid)
|
mon_ref = Process.monitor(pid)
|
||||||
{pid, ref, mon_ref}
|
{pid, ref, mon_ref}
|
||||||
catch
|
catch
|
||||||
@@ -92,19 +93,6 @@ defimpl Enumerable, for: GenEvent.Stream do
|
|||||||
self = self()
|
self = self()
|
||||||
|
|
||||||
receive do
|
receive do
|
||||||
# The handler was removed. Stop iteration, resolve the
|
|
||||||
# event later. We need to demonitor now, otherwise DOWN
|
|
||||||
# appears with higher priority in the shutdown process.
|
|
||||||
{:gen_event_EXIT, ^self, _reason} = event ->
|
|
||||||
Process.demonitor(mon_ref, [:flush])
|
|
||||||
send(self, event)
|
|
||||||
{:halt, {:removed, acc}}
|
|
||||||
|
|
||||||
# The manager died. Stop iteration, resolve the event later.
|
|
||||||
{:DOWN, ^mon_ref, _, _, _} = event ->
|
|
||||||
send(self, event)
|
|
||||||
{:halt, {:removed, acc}}
|
|
||||||
|
|
||||||
# Got an async event.
|
# Got an async event.
|
||||||
{_from, {^pid, ^ref}, {:notify, event}} ->
|
{_from, {^pid, ^ref}, {:notify, event}} ->
|
||||||
{[{:async, pid, ref, event}], acc}
|
{[{:async, pid, ref, event}], acc}
|
||||||
@@ -116,6 +104,19 @@ defimpl Enumerable, for: GenEvent.Stream do
|
|||||||
# Got an ack event.
|
# Got an ack event.
|
||||||
{_from, {^pid, ^ref}, {:ack_notify, event}} ->
|
{_from, {^pid, ^ref}, {:ack_notify, event}} ->
|
||||||
{[{:ack, pid, ref, event}], acc}
|
{[{:ack, pid, ref, event}], acc}
|
||||||
|
|
||||||
|
# The handler was removed. Stop iteration, resolve the
|
||||||
|
# event later. We need to demonitor now, otherwise DOWN
|
||||||
|
# appears with higher priority in the shutdown process.
|
||||||
|
{:gen_event_EXIT, {^pid, ^ref}, _reason} = event ->
|
||||||
|
Process.demonitor(mon_ref, [:flush])
|
||||||
|
send(self, event)
|
||||||
|
{:halt, {:removed, acc}}
|
||||||
|
|
||||||
|
# The manager died. Stop iteration, resolve the event later.
|
||||||
|
{:DOWN, ^mon_ref, _, _, _} = event ->
|
||||||
|
send(self, event)
|
||||||
|
{:halt, {:removed, acc}}
|
||||||
after
|
after
|
||||||
timeout ->
|
timeout ->
|
||||||
exit({:timeout, {__MODULE__, :next, [stream, acc]}})
|
exit({:timeout, {__MODULE__, :next, [stream, acc]}})
|
||||||
@@ -135,26 +136,20 @@ defimpl Enumerable, for: GenEvent.Stream do
|
|||||||
|
|
||||||
# If we reach this branch, the handler was not removed yet,
|
# If we reach this branch, the handler was not removed yet,
|
||||||
# so we trigger a request for doing so.
|
# so we trigger a request for doing so.
|
||||||
defp stop(stream, {pid, _, _} = acc) do
|
defp stop(stream, {pid, ref, _} = acc) do
|
||||||
parent = self()
|
_ = GenEvent.remove_handler(pid, {pid, ref}, :shutdown)
|
||||||
_ = Task.start(fn -> GenEvent.remove_handler(pid, parent, :shutdown) end)
|
|
||||||
stop(stream, {:removed, acc})
|
stop(stream, {:removed, acc})
|
||||||
end
|
end
|
||||||
|
|
||||||
defp wait_for_handler_removal(pid, ref, mon_ref) do
|
defp wait_for_handler_removal(pid, ref, mon_ref) do
|
||||||
self = self()
|
|
||||||
|
|
||||||
receive do
|
receive do
|
||||||
{_from, {^pid, ^ref}, {notify, _event}} when notify in [:ack_notify, :sync_notify] ->
|
{:gen_event_EXIT, {^pid, ^ref}, reason}
|
||||||
send pid, {ref, :done}
|
|
||||||
wait_for_handler_removal(pid, ref, mon_ref)
|
|
||||||
{:gen_event_EXIT, ^self, reason}
|
|
||||||
when reason == :normal
|
when reason == :normal
|
||||||
when reason == :shutdown
|
when reason == :shutdown
|
||||||
when tuple_size(reason) == 3 and elem(reason, 0) == :swapped ->
|
when tuple_size(reason) == 3 and elem(reason, 0) == :swapped ->
|
||||||
Process.demonitor(mon_ref, [:flush])
|
Process.demonitor(mon_ref, [:flush])
|
||||||
:ok
|
:ok
|
||||||
{:gen_event_EXIT, ^self, reason} ->
|
{:gen_event_EXIT, {^pid, ^ref}, reason} ->
|
||||||
Process.demonitor(mon_ref, [:flush])
|
Process.demonitor(mon_ref, [:flush])
|
||||||
{:error, reason}
|
{:error, reason}
|
||||||
{:DOWN, ^mon_ref, _, _, reason} ->
|
{:DOWN, ^mon_ref, _, _, reason} ->
|
||||||
@@ -164,7 +159,7 @@ defimpl Enumerable, for: GenEvent.Stream do
|
|||||||
|
|
||||||
defp flush_events(ref) do
|
defp flush_events(ref) do
|
||||||
receive do
|
receive do
|
||||||
{_from, {_pid, ^ref}, {:notify, _event}} ->
|
{_from, {_pid, ^ref}, {notify, _event}} when notify in [:notify, :ack_notify, :sync_notify] ->
|
||||||
flush_events(ref)
|
flush_events(ref)
|
||||||
after
|
after
|
||||||
0 -> :ok
|
0 -> :ok
|
||||||
|
|||||||
@@ -185,6 +185,8 @@ defmodule Inspect.Algebra do
|
|||||||
Inspect.inspect(map, opts)
|
Inspect.inspect(map, opts)
|
||||||
rescue
|
rescue
|
||||||
e ->
|
e ->
|
||||||
|
stacktrace = System.stacktrace
|
||||||
|
|
||||||
# Because we try to raise a nice error message in case
|
# Because we try to raise a nice error message in case
|
||||||
# we can't inspect a struct, there is a chance the error
|
# we can't inspect a struct, there is a chance the error
|
||||||
# message itself relies on the struct being printed, so
|
# message itself relies on the struct being printed, so
|
||||||
@@ -198,9 +200,10 @@ defmodule Inspect.Algebra do
|
|||||||
Process.put(:inspect_trap, true)
|
Process.put(:inspect_trap, true)
|
||||||
res = Inspect.Map.inspect(map, opts)
|
res = Inspect.Map.inspect(map, opts)
|
||||||
formatted = IO.iodata_to_binary(format(res, :infinity))
|
formatted = IO.iodata_to_binary(format(res, :infinity))
|
||||||
raise ArgumentError,
|
reraise ArgumentError,
|
||||||
"Got #{inspect e.__struct__} with message " <>
|
"Got #{inspect e.__struct__} with message " <>
|
||||||
"\"#{Exception.message(e)}\" while inspecting #{formatted}"
|
"\"#{Exception.message(e)}\" while inspecting #{formatted}",
|
||||||
|
stacktrace
|
||||||
after
|
after
|
||||||
Process.delete(:inspect_trap)
|
Process.delete(:inspect_trap)
|
||||||
end
|
end
|
||||||
@@ -435,7 +438,7 @@ defmodule Inspect.Algebra do
|
|||||||
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
|
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
|
||||||
"[1! 2! 3! ...]"
|
"[1! 2! 3! ...]"
|
||||||
"""
|
"""
|
||||||
@spec surround_many(binary, [any], binary, integer | :infinity, (term -> t), binary) :: t
|
@spec surround_many(binary, [any], binary, Inspect.Opts.t, (term, Inspect.Opts.t -> t), binary) :: t
|
||||||
def surround_many(left, docs, right, opts, fun, separator \\ @surround_separator) do
|
def surround_many(left, docs, right, opts, fun, separator \\ @surround_separator) do
|
||||||
do_surround_many(left, docs, right, opts.limit, opts, fun, separator)
|
do_surround_many(left, docs, right, opts.limit, opts, fun, separator)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -123,8 +123,9 @@ defmodule IO do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@read_all_size 4096
|
||||||
defp do_binread_all(mapped_dev, acc) do
|
defp do_binread_all(mapped_dev, acc) do
|
||||||
case :file.read_line(mapped_dev) do
|
case :file.read(mapped_dev, @read_all_size) do
|
||||||
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
|
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
|
||||||
:eof -> acc
|
:eof -> acc
|
||||||
other -> other
|
other -> other
|
||||||
@@ -180,7 +181,7 @@ defmodule IO do
|
|||||||
Inspects and writes the given argument to the device.
|
Inspects and writes the given argument to the device.
|
||||||
|
|
||||||
It enables pretty printing by default with width of
|
It enables pretty printing by default with width of
|
||||||
80 characters. Th width can be changed by explicitly
|
80 characters. The width can be changed by explicitly
|
||||||
passing the `:width` option.
|
passing the `:width` option.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
@@ -25,10 +25,10 @@ defmodule IO.ANSI.Docs do
|
|||||||
[enabled: true,
|
[enabled: true,
|
||||||
doc_bold: [:bright],
|
doc_bold: [:bright],
|
||||||
doc_code: [:cyan, :bright],
|
doc_code: [:cyan, :bright],
|
||||||
doc_headings: [:yellow, :bright],
|
doc_headings: [:yellow],
|
||||||
doc_inline_code: [:cyan],
|
doc_inline_code: [:cyan],
|
||||||
doc_table_heading: [:reverse],
|
doc_table_heading: [:reverse],
|
||||||
doc_title: [:reverse, :yellow, :bright],
|
doc_title: [:reverse, :yellow],
|
||||||
doc_underline: [:underline],
|
doc_underline: [:underline],
|
||||||
width: 80]
|
width: 80]
|
||||||
end
|
end
|
||||||
@@ -194,7 +194,7 @@ defmodule IO.ANSI.Docs do
|
|||||||
lines
|
lines
|
||||||
|> Enum.join(" ")
|
|> Enum.join(" ")
|
||||||
|> handle_links
|
|> handle_links
|
||||||
|> handle_inline(nil, [], [], options)
|
|> handle_inline(options)
|
||||||
|> String.split(~r{\s})
|
|> String.split(~r{\s})
|
||||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
||||||
|
|
||||||
@@ -262,7 +262,7 @@ defmodule IO.ANSI.Docs do
|
|||||||
col = col
|
col = col
|
||||||
|> String.replace(~r/\\ \|/x, "|")
|
|> String.replace(~r/\\ \|/x, "|")
|
||||||
|> handle_links
|
|> handle_links
|
||||||
|> handle_inline(nil, [], [], options)
|
|> handle_inline(options)
|
||||||
{col, length_without_escape(col, 0)}
|
{col, length_without_escape(col, 0)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -392,61 +392,104 @@ defmodule IO.ANSI.Docs do
|
|||||||
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Single inline quotes.
|
|
||||||
@single [?`, ?_, ?*]
|
|
||||||
|
|
||||||
# ` does not require space in between
|
# We have four entries: **, *, _ and `.
|
||||||
@spaced [?_, ?*]
|
#
|
||||||
|
# The first three behave the same while the last one is simpler
|
||||||
|
# when it comes to delimiters. But, since the first has two
|
||||||
|
# characters, we need to handle 3 cases:
|
||||||
|
#
|
||||||
|
# 1. **
|
||||||
|
# 2. _ and *
|
||||||
|
# 3. `
|
||||||
|
#
|
||||||
|
# Where the first two should have the same code but match differently.
|
||||||
|
@single [?_, ?*]
|
||||||
|
|
||||||
# Clauses for handling spaces
|
# Characters that can mark the beginning or the end of a word.
|
||||||
defp handle_inline(<<?*, ?*, ?\s, rest :: binary>>, nil, buffer, acc, options) do
|
# Only support the most common ones at this moment.
|
||||||
handle_inline(rest, nil, [?\s, ?*, ?*|buffer], acc, options)
|
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&, ?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>, ?.]
|
||||||
|
|
||||||
|
# Inline start
|
||||||
|
|
||||||
|
defp handle_inline(<<?*, ?*, rest :: binary>>, options) do
|
||||||
|
handle_inline(rest, ?d, ["**"], [], options)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_inline(<<mark, ?\s, rest :: binary>>, nil, buffer, acc, options) when mark in @spaced do
|
defp handle_inline(<<mark, rest :: binary>>, options) when mark in @single do
|
||||||
handle_inline(rest, nil, [?\s, mark|buffer], acc, options)
|
handle_inline(rest, mark, [<<mark>>], [], options)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_inline(<<?\s, ?*, ?*, rest :: binary>>, limit, buffer, acc, options) do
|
defp handle_inline(rest, options) do
|
||||||
handle_inline(rest, limit, [?*, ?*, ?\s|buffer], acc, options)
|
handle_inline(rest, nil, [], [], options)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_inline(<<?\s, mark, rest :: binary>>, limit, buffer, acc, options) when mark in @spaced do
|
# Inline delimiters
|
||||||
handle_inline(rest, limit, [mark, ?\s|buffer], acc, options)
|
|
||||||
|
defp handle_inline(<<delimiter, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
|
||||||
|
when rest != "" and delimiter in @delimiters do
|
||||||
|
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<delimiter, mark, rest :: binary>>, nil, buffer, acc, options)
|
||||||
|
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||||
|
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<?`, rest :: binary>>, nil, buffer, acc, options)
|
||||||
|
when rest != "" do
|
||||||
|
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Clauses for handling escape
|
# Clauses for handling escape
|
||||||
|
|
||||||
|
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
|
||||||
|
when rest != "" do
|
||||||
|
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<?\\, ?\\, mark, rest :: binary>>, nil, buffer, acc, options)
|
||||||
|
when rest != "" and mark in @single do
|
||||||
|
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
defp handle_inline(<<?\\, ?\\, rest :: binary>>, limit, buffer, acc, options) do
|
defp handle_inline(<<?\\, ?\\, rest :: binary>>, limit, buffer, acc, options) do
|
||||||
handle_inline(rest, limit, [?\\|buffer], acc, options)
|
handle_inline(rest, limit, [?\\|buffer], acc, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_inline(<<?\\, ?*, ?*, rest :: binary>>, limit, buffer, acc, options) do
|
# An escape is not valid inside `
|
||||||
handle_inline(rest, limit, [?*, ?*|buffer], acc, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# A escape is not valid inside `
|
|
||||||
defp handle_inline(<<?\\, mark, rest :: binary>>, limit, buffer, acc, options)
|
defp handle_inline(<<?\\, mark, rest :: binary>>, limit, buffer, acc, options)
|
||||||
when mark in [?_, ?*, ?`] and not(mark == limit and mark == ?`) do
|
when not(mark == limit and mark == ?`) do
|
||||||
handle_inline(rest, limit, [mark|buffer], acc, options)
|
handle_inline(rest, limit, [mark|buffer], acc, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Inline start
|
|
||||||
defp handle_inline(<<?*, ?*, rest :: binary>>, nil, buffer, acc, options) when rest != "" do
|
|
||||||
handle_inline(rest, ?d, ["**"], [Enum.reverse(buffer)|acc], options)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp handle_inline(<<mark, rest :: binary>>, nil, buffer, acc, options) when rest != "" and mark in @single do
|
|
||||||
handle_inline(rest, mark, [<<mark>>], [Enum.reverse(buffer)|acc], options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Inline end
|
# Inline end
|
||||||
defp handle_inline(<<?*, ?*, rest :: binary>>, ?d, buffer, acc, options) do
|
|
||||||
|
defp handle_inline(<<?*, ?*, delimiter, rest :: binary>>, ?d, buffer, acc, options)
|
||||||
|
when delimiter in @delimiters do
|
||||||
|
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<mark, delimiter, rest :: binary>>, mark, buffer, acc, options)
|
||||||
|
when delimiter in @delimiters and mark in @single do
|
||||||
|
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<?*, ?*, rest:: binary>>, ?d, buffer, acc, options)
|
||||||
|
when rest == "" do
|
||||||
|
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<mark, rest :: binary>>, mark, buffer, acc, options)
|
||||||
|
when rest == "" and mark in @single do
|
||||||
|
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp handle_inline(<<?`, rest :: binary>>, ?`, buffer, acc, options) do
|
||||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_inline(<<mark, rest :: binary>>, mark, buffer, acc, options) when mark in @single do
|
# Catch all
|
||||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp handle_inline(<<char, rest :: binary>>, mark, buffer, acc, options) do
|
defp handle_inline(<<char, rest :: binary>>, mark, buffer, acc, options) do
|
||||||
handle_inline(rest, mark, [char|buffer], acc, options)
|
handle_inline(rest, mark, [char|buffer], acc, options)
|
||||||
|
|||||||
@@ -1386,7 +1386,14 @@ defmodule Kernel do
|
|||||||
iex> "abcd" =~ "ad"
|
iex> "abcd" =~ "ad"
|
||||||
false
|
false
|
||||||
|
|
||||||
|
iex> "abcd" =~ ""
|
||||||
|
true
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@spec (String.t =~ (String.t | Regex.t)) :: boolean
|
||||||
|
def left =~ "" when is_binary(left), do: true
|
||||||
|
|
||||||
def left =~ right when is_binary(left) and is_binary(right) do
|
def left =~ right when is_binary(left) and is_binary(right) do
|
||||||
:binary.match(left, right) != :nomatch
|
:binary.match(left, right) != :nomatch
|
||||||
end
|
end
|
||||||
@@ -1813,7 +1820,9 @@ defmodule Kernel do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp unnest({{:., _, [expr, key]}, _, []}, acc, kind)
|
defp unnest({{:., _, [expr, key]}, _, []}, acc, kind)
|
||||||
when is_tuple(expr) and elem(expr, 0) != :__aliases__ and elem(expr, 0) != :__MODULE__ do
|
when is_tuple(expr) and
|
||||||
|
:erlang.element(1, expr) != :__aliases__ and
|
||||||
|
:erlang.element(1, expr) != :__MODULE__ do
|
||||||
unnest(expr, [{:map, key}|acc], kind)
|
unnest(expr, [{:map, key}|acc], kind)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1834,8 +1843,8 @@ defmodule Kernel do
|
|||||||
|
|
||||||
defp proper_start?({{:., _, [expr, _]}, _, _args})
|
defp proper_start?({{:., _, [expr, _]}, _, _args})
|
||||||
when is_atom(expr)
|
when is_atom(expr)
|
||||||
when elem(expr, 0) == :__aliases__
|
when :erlang.element(1, expr) == :__aliases__
|
||||||
when elem(expr, 0) == :__MODULE__, do: true
|
when :erlang.element(1, expr) == :__MODULE__, do: true
|
||||||
|
|
||||||
defp proper_start?({atom, _, _args})
|
defp proper_start?({atom, _, _args})
|
||||||
when is_atom(atom), do: true
|
when is_atom(atom), do: true
|
||||||
@@ -2057,7 +2066,7 @@ defmodule Kernel do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp warn_info([entry|_]) do
|
defp warn_info([entry|_]) do
|
||||||
opts = elem(entry, tuple_size(entry) - 1)
|
opts = :erlang.element(tuple_size(entry), entry)
|
||||||
Exception.format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line)) <> " "
|
Exception.format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line)) <> " "
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ defmodule Kernel.CLI do
|
|||||||
## Helpers
|
## Helpers
|
||||||
|
|
||||||
defp at_exit(res) do
|
defp at_exit(res) do
|
||||||
hooks = :elixir_code_server.call(:flush_at_exit)
|
hooks = :elixir_config.get_and_put(:at_exit, [])
|
||||||
res = Enum.reduce(hooks, res, &exec_fun/2)
|
res = Enum.reduce(hooks, res, &exec_fun/2)
|
||||||
if hooks == [], do: res, else: at_exit(res)
|
if hooks == [], do: res, else: at_exit(res)
|
||||||
end
|
end
|
||||||
@@ -113,7 +113,8 @@ defmodule Kernel.CLI do
|
|||||||
IO.puts :stderr, Exception.format(kind, reason, prune_stacktrace(trace))
|
IO.puts :stderr, Exception.format(kind, reason, prune_stacktrace(trace))
|
||||||
end
|
end
|
||||||
|
|
||||||
@elixir_internals [:elixir_compiler, :elixir_module, :elixir_translator, :elixir_expand]
|
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_translator,
|
||||||
|
:elixir_expand, :elixir_lexical]
|
||||||
|
|
||||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||||
prune_stacktrace(t)
|
prune_stacktrace(t)
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ defmodule Kernel.LexicalTracker do
|
|||||||
defp to_pid(pid) when is_pid(pid), do: pid
|
defp to_pid(pid) when is_pid(pid), do: pid
|
||||||
defp to_pid(mod) when is_atom(mod) do
|
defp to_pid(mod) when is_atom(mod) do
|
||||||
table = :elixir_module.data_table(mod)
|
table = :elixir_module.data_table(mod)
|
||||||
[{_, val}] = :ets.lookup(table, :__lexical_tracker)
|
[{_, val}] = :ets.lookup(table, {:elixir, :lexical_tracker})
|
||||||
val
|
val
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ defmodule Kernel.ParallelCompiler do
|
|||||||
else
|
else
|
||||||
:elixir_compiler.file(h, Keyword.get(options, :dest))
|
:elixir_compiler.file(h, Keyword.get(options, :dest))
|
||||||
end
|
end
|
||||||
{:compiled, h}
|
{:shutdown, h}
|
||||||
catch
|
catch
|
||||||
kind, reason ->
|
kind, reason ->
|
||||||
{:failure, kind, reason, System.stacktrace}
|
{:failure, kind, reason, System.stacktrace}
|
||||||
@@ -166,7 +166,7 @@ defmodule Kernel.ParallelCompiler do
|
|||||||
|
|
||||||
spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result)
|
spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result)
|
||||||
|
|
||||||
{:DOWN, _down_ref, :process, down_pid, {:compiled, file}} ->
|
{:DOWN, _down_ref, :process, down_pid, {:shutdown, file}} ->
|
||||||
if callback = Keyword.get(options, :each_file) do
|
if callback = Keyword.get(options, :each_file) do
|
||||||
callback.(file)
|
callback.(file)
|
||||||
end
|
end
|
||||||
@@ -187,9 +187,15 @@ defmodule Kernel.ParallelCompiler do
|
|||||||
defp handle_failure(ref, reason, entries, waiting, queued) do
|
defp handle_failure(ref, reason, entries, waiting, queued) do
|
||||||
if file = find_failure(ref, queued) do
|
if file = find_failure(ref, queued) do
|
||||||
print_failure(file, reason)
|
print_failure(file, reason)
|
||||||
|
|
||||||
if all_missing?(entries, waiting, queued) do
|
if all_missing?(entries, waiting, queued) do
|
||||||
collect_failures(queued, length(queued) - 1)
|
collect_failures(queued, length(queued) - 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Enum.each queued, fn {child, _, _} ->
|
||||||
|
Process.exit(child, :kill)
|
||||||
|
end
|
||||||
|
|
||||||
exit({:shutdown, 1})
|
exit({:shutdown, 1})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -201,7 +207,7 @@ defmodule Kernel.ParallelCompiler do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp print_failure(_file, {:compiled, _}) do
|
defp print_failure(_file, {:shutdown, _}) do
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -215,7 +221,8 @@ defmodule Kernel.ParallelCompiler do
|
|||||||
IO.puts Exception.format(:exit, reason, [])
|
IO.puts Exception.format(:exit, reason, [])
|
||||||
end
|
end
|
||||||
|
|
||||||
@elixir_internals [:elixir_compiler, :elixir_module, :elixir_translator, :elixir_expand]
|
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_translator,
|
||||||
|
:elixir_expand, :elixir_lexical]
|
||||||
|
|
||||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||||
prune_stacktrace(t)
|
prune_stacktrace(t)
|
||||||
|
|||||||
@@ -172,10 +172,22 @@ defmodule Kernel.SpecialForms do
|
|||||||
iex> << 1, 2, 3 >>
|
iex> << 1, 2, 3 >>
|
||||||
<< 1, 2, 3 >>
|
<< 1, 2, 3 >>
|
||||||
|
|
||||||
## Bitstring types
|
## Types
|
||||||
|
|
||||||
A bitstring is made of many segments. Each segment has a
|
A bitstring is made of many segments and each segment has a
|
||||||
type, which defaults to integer:
|
type. There are 9 types used in bitstrings:
|
||||||
|
|
||||||
|
- `integer`
|
||||||
|
- `float`
|
||||||
|
- `bits` (alias for bitstring)
|
||||||
|
- `bitstring`
|
||||||
|
- `binary`
|
||||||
|
- `bytes` (alias for binary)
|
||||||
|
- `utf8`
|
||||||
|
- `utf16`
|
||||||
|
- `utf32`
|
||||||
|
|
||||||
|
When no type is specified, the default is `integer`:
|
||||||
|
|
||||||
iex> <<1, 2, 3>>
|
iex> <<1, 2, 3>>
|
||||||
<<1, 2, 3>>
|
<<1, 2, 3>>
|
||||||
@@ -186,14 +198,7 @@ defmodule Kernel.SpecialForms do
|
|||||||
iex> <<0, "foo">>
|
iex> <<0, "foo">>
|
||||||
<<0, 102, 111, 111>>
|
<<0, 102, 111, 111>>
|
||||||
|
|
||||||
Any other type needs to be explicitly tagged. For example,
|
Variables or any other type need to be explicitly tagged:
|
||||||
in order to store a float type in the binary, one has to do:
|
|
||||||
|
|
||||||
iex> <<3.14 :: float>>
|
|
||||||
<<64, 9, 30, 184, 81, 235, 133, 31>>
|
|
||||||
|
|
||||||
This also means that variables need to be explicitly tagged,
|
|
||||||
otherwise Elixir defaults to integer:
|
|
||||||
|
|
||||||
iex> rest = "oo"
|
iex> rest = "oo"
|
||||||
iex> <<102, rest>>
|
iex> <<102, rest>>
|
||||||
@@ -201,103 +206,161 @@ defmodule Kernel.SpecialForms do
|
|||||||
|
|
||||||
We can solve this by explicitly tagging it as a binary:
|
We can solve this by explicitly tagging it as a binary:
|
||||||
|
|
||||||
<<102, rest :: binary>>
|
iex> rest = "oo"
|
||||||
|
iex> <<102, rest :: binary>>
|
||||||
The type can be integer, float, bitstring/bits, binary/bytes,
|
"foo"
|
||||||
utf8, utf16 or utf32, e.g.:
|
|
||||||
|
|
||||||
<<102 :: float, rest :: binary>>
|
|
||||||
|
|
||||||
An integer can be any arbitrary precision integer. A float is an
|
|
||||||
IEEE 754 binary32 or binary64 floating point number. A bitstring
|
|
||||||
is an arbitrary series of bits. A binary is a special case of
|
|
||||||
bitstring that has a total size divisible by 8.
|
|
||||||
|
|
||||||
The utf8, utf16, and utf32 types are for unicode codepoints. They
|
The utf8, utf16, and utf32 types are for unicode codepoints. They
|
||||||
can also be applied to literal strings and char lists:
|
can also be applied to literal strings and char lists:
|
||||||
|
|
||||||
iex> <<"foo" :: utf16>>
|
iex> <<"foo" :: utf16>>
|
||||||
<<0,102,0,111,0,111>>
|
<<0, 102, 0, 111, 0, 111>>
|
||||||
|
iex> <<"foo" :: utf32>>
|
||||||
|
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
|
||||||
|
|
||||||
The bits type is an alias for bitstring. The bytes type is an
|
## Options
|
||||||
alias for binary.
|
|
||||||
|
|
||||||
The signedness can also be given as signed or unsigned. The
|
Many options can be given by using `-` as separator. Order is
|
||||||
signedness only matters for matching and relevant only for
|
arbitrary, so the following are all equivalent:
|
||||||
integers. If unspecified, it defaults to unsigned. Example:
|
|
||||||
|
|
||||||
iex> <<-100 :: signed, _rest :: binary>> = <<-100, "foo">>
|
|
||||||
<<156,102,111,111>>
|
|
||||||
|
|
||||||
This match would have failed if we did not specify that the
|
|
||||||
value -100 is signed. If we're matching into a variable instead
|
|
||||||
of a value, the signedness won't be checked; rather, the number
|
|
||||||
will simply be interpreted as having the given (or implied)
|
|
||||||
signedness, e.g.:
|
|
||||||
|
|
||||||
iex> <<val, _rest :: binary>> = <<-100, "foo">>
|
|
||||||
iex> val
|
|
||||||
156
|
|
||||||
|
|
||||||
Here, `val` is interpreted as unsigned.
|
|
||||||
|
|
||||||
The endianness of a segment can be big, little or native (the
|
|
||||||
latter meaning it will be resolved at VM load time). Many options
|
|
||||||
can be given by using `-` as separator:
|
|
||||||
|
|
||||||
<<102 :: integer-native, rest :: binary>>
|
<<102 :: integer-native, rest :: binary>>
|
||||||
|
<<102 :: native-integer, rest :: binary>>
|
||||||
Or:
|
|
||||||
|
|
||||||
<<102 :: unsigned-big-integer, rest :: binary>>
|
<<102 :: unsigned-big-integer, rest :: binary>>
|
||||||
|
<<102 :: unsigned-big-integer-size(8), rest :: binary>>
|
||||||
|
<<102 :: unsigned-big-integer-8, rest :: binary>>
|
||||||
|
<<102 :: 8-integer-big-unsigned, rest :: binary>>
|
||||||
|
<<102, rest :: binary>>
|
||||||
|
|
||||||
And so on.
|
### Unit and Size
|
||||||
|
|
||||||
Endianness only makes sense for integers and some UTF code
|
The length of the match is equal to the `unit` (a number of bits) times the
|
||||||
point types (utf16 and utf32).
|
`size` (the number of repeated segnments of length `unit`).
|
||||||
|
|
||||||
Finally, we can also specify size and unit for each segment. The
|
Type | Default Unit
|
||||||
unit is multiplied by the size to give the effective size of
|
--------- | ------------
|
||||||
the segment in bits. The default unit for integers, floats,
|
`integer` | 1 bit
|
||||||
and bitstrings is 1. For binaries, it is 8.
|
`float` | 1 bit
|
||||||
|
`binary` | 8 bits
|
||||||
|
|
||||||
Since integers are default, the default unit is 1. The example below
|
Sizes for types are a bit more nuanced. The default size for integers is 8.
|
||||||
matches because the string "foo" takes 24 bits and we match it
|
|
||||||
against a segment of 24 bits, 8 of which are taken by the integer
|
|
||||||
102 and the remaining 16 bits are specified on the rest.
|
|
||||||
|
|
||||||
iex> <<102, _rest :: size(16)>> = "foo"
|
For floats, it is 64. For floats, `size * unit` must result in 32 or 64,
|
||||||
"foo"
|
corresponding to [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point)
|
||||||
|
binary32 and binary64, respectively.
|
||||||
|
|
||||||
We can also match by specifying size and unit explicitly:
|
For binaries, the default is the size of the binary. Only the last binary in a
|
||||||
|
match can use the default size. All others must have their size specified
|
||||||
|
explicitly, even if the match is unambiguous. For example:
|
||||||
|
|
||||||
iex> <<102, _rest :: size(2)-unit(8)>> = "foo"
|
iex> <<name::binary-size(5), " the ", species::binary>> = <<"Frank the Walrus">>
|
||||||
"foo"
|
"Frank the Walrus"
|
||||||
|
iex> {name, species}
|
||||||
|
{"Frank", "Walrus"}
|
||||||
|
|
||||||
However, if we expect a size of 32, it won't match:
|
Failing to specify the size for the non-last causes compilation to fail:
|
||||||
|
|
||||||
iex> <<102, _rest :: size(32)>> = "foo"
|
<<name::binary, " the ", species::binary>> = <<"Frank the Walrus">>
|
||||||
** (MatchError) no match of right hand side value: "foo"
|
** (CompileError): a binary field without size is only allowed at the end of a binary pattern
|
||||||
|
|
||||||
Size and unit are not applicable to utf8, utf16, and utf32.
|
#### Shortcut Syntax
|
||||||
|
|
||||||
The default size for integers is 8. For floats, it is 64. For
|
|
||||||
binaries, it is the size of the binary. Only the last binary
|
|
||||||
in a binary match can use the default size (all others must
|
|
||||||
have their size specified explicitly).
|
|
||||||
|
|
||||||
Size and unit can also be specified using a syntax shortcut
|
Size and unit can also be specified using a syntax shortcut
|
||||||
when passing integer values:
|
when passing integer values:
|
||||||
|
|
||||||
<< x :: 8 >> == << x :: size(8) >>
|
iex> x = 1
|
||||||
<< x :: 8 * 4 >> == << x :: size(8)-unit(4) >>
|
iex> << x :: 8 >> == << x :: size(8) >>
|
||||||
<< x :: _ * 4 >> == << x :: unit(4) >>
|
true
|
||||||
|
iex> << x :: 8 * 4 >> == << x :: size(8)-unit(4) >>
|
||||||
|
true
|
||||||
|
|
||||||
This syntax reflects the fact the effective size is given by
|
This syntax reflects the fact the effective size is given by
|
||||||
multiplying the size by the unit.
|
multiplying the size by the unit.
|
||||||
|
|
||||||
For floats, `size * unit` must result in 32 or 64, corresponding
|
### Modifiers
|
||||||
to binary32 and binary64, respectively.
|
|
||||||
|
Some types have associated modifiers to clear up ambiguity in byte
|
||||||
|
representation.
|
||||||
|
|
||||||
|
Modifier | Relevant Type(s)
|
||||||
|
-------------------- | ----------------
|
||||||
|
`signed` | `integer`
|
||||||
|
`unsigned` (default) | `integer`
|
||||||
|
`little` | `integer`, `utf16`, `utf32`
|
||||||
|
`big` (default) | `integer`, `utf16`, `utf32`
|
||||||
|
`native` | `integer`, `utf16`, `utf32`
|
||||||
|
|
||||||
|
### Sign
|
||||||
|
|
||||||
|
Integers can be `signed` or `unsigned`, defaulting to `unsigned`.
|
||||||
|
|
||||||
|
iex> <<int::integer>> = <<-100>>
|
||||||
|
<<156>>
|
||||||
|
iex> int
|
||||||
|
156
|
||||||
|
iex> <<int::integer-signed>> = <<-100>>
|
||||||
|
<<156>>
|
||||||
|
iex> int
|
||||||
|
-100
|
||||||
|
|
||||||
|
`signed` and `unsigned` are only used for matching binaries (see below) and
|
||||||
|
are only used for integers.
|
||||||
|
|
||||||
|
iex> <<-100 :: signed, _rest :: binary>> = <<-100, "foo">>
|
||||||
|
<<156, 102, 111, 111>>
|
||||||
|
|
||||||
|
### Endianness
|
||||||
|
|
||||||
|
Elixir has three options for endianness: `big`, `little`, and `native`.
|
||||||
|
The default is `big`. `native` is determined by the VM at startup.
|
||||||
|
|
||||||
|
iex> <<number::little-integer-size(16)>> = <<0, 1>>
|
||||||
|
<<0, 1>>
|
||||||
|
iex> number
|
||||||
|
256
|
||||||
|
iex> <<number::big-integer-size(16)>> = <<0, 1>>
|
||||||
|
<<0, 1>>
|
||||||
|
iex> number
|
||||||
|
1
|
||||||
|
iex> <<number::native-integer-size(16)>> = <<0, 1>>
|
||||||
|
<<0, 1>>
|
||||||
|
iex> number
|
||||||
|
256
|
||||||
|
|
||||||
|
## Binary/Bitstring Matching
|
||||||
|
|
||||||
|
Binary matching is a powerful feature in Elixir that is useful for extracting
|
||||||
|
information from binaries as well as pattern matching.
|
||||||
|
|
||||||
|
Binary matching can be used by itself to extract information from binaries:
|
||||||
|
|
||||||
|
iex> <<"Hello, ", place::binary>> = "Hello, World"
|
||||||
|
"Hello, World"
|
||||||
|
iex> place
|
||||||
|
"World"
|
||||||
|
|
||||||
|
Or as a part of function definitions to pattern match:
|
||||||
|
|
||||||
|
defmodule ImageTyper
|
||||||
|
@png_signature <<137::size(8), 80::size(8), 78::size(8), 71::size(8),
|
||||||
|
13::size(8), 10::size(8), 26::size(8), 10::size(8)>>
|
||||||
|
@jpg_signature <<255::size(8), 216::size(8)>>
|
||||||
|
|
||||||
|
def type(<<@png_signature, rest::binary>>), do: :png
|
||||||
|
def type(<<@jpg_signature, rest::binary>>), do: :jpg
|
||||||
|
def type(_), do :unknown
|
||||||
|
end
|
||||||
|
|
||||||
|
### Performance & Optimizations
|
||||||
|
|
||||||
|
The Erlang compiler can provide a number of optimizations on binary creation
|
||||||
|
and matching. To see optimization output, set the `bin_opt_info` compiler
|
||||||
|
option:
|
||||||
|
|
||||||
|
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
|
||||||
|
|
||||||
|
To learn more about specific optimizations and performance considerations,
|
||||||
|
check out
|
||||||
|
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
|
||||||
"""
|
"""
|
||||||
defmacro unquote(:<<>>)(args)
|
defmacro unquote(:<<>>)(args)
|
||||||
|
|
||||||
|
|||||||
@@ -644,6 +644,10 @@ defmodule Kernel.Typespec do
|
|||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp typespec_to_ast({:user_type, line, name, args}) do
|
||||||
|
typespec_to_ast({:type, line, name, args})
|
||||||
|
end
|
||||||
|
|
||||||
defp typespec_to_ast({:type, line, :tuple, :any}) do
|
defp typespec_to_ast({:type, line, :tuple, :any}) do
|
||||||
{:tuple, [line: line], []}
|
{:tuple, [line: line], []}
|
||||||
end
|
end
|
||||||
@@ -677,8 +681,13 @@ defmodule Kernel.Typespec do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp typespec_to_ast({:type, line, :map, fields}) do
|
defp typespec_to_ast({:type, line, :map, fields}) do
|
||||||
fields = Enum.map fields, fn {:type, _, :map_field_assoc, k, v} ->
|
fields = Enum.map fields, fn
|
||||||
{typespec_to_ast(k), typespec_to_ast(v)}
|
# OTP 18
|
||||||
|
{:type, _, :map_field_assoc, [k, v]} ->
|
||||||
|
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||||
|
# OTP 17
|
||||||
|
{:type, _, :map_field_assoc, k, v} ->
|
||||||
|
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||||
end
|
end
|
||||||
|
|
||||||
{struct, fields} = Keyword.pop(fields, :__struct__)
|
{struct, fields} = Keyword.pop(fields, :__struct__)
|
||||||
@@ -821,9 +830,17 @@ defmodule Kernel.Typespec do
|
|||||||
|
|
||||||
## Handle maps and structs
|
## Handle maps and structs
|
||||||
defp typespec({:%{}, meta, fields}, vars, caller) do
|
defp typespec({:%{}, meta, fields}, vars, caller) do
|
||||||
fields = :lists.map(fn {k, v} ->
|
fields =
|
||||||
{:type, line(meta), :map_field_assoc, typespec(k, vars, caller), typespec(v, vars, caller)}
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
end, fields)
|
:lists.map(fn {k, v} ->
|
||||||
|
{:type, line(meta), :map_field_assoc, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||||
|
end, fields)
|
||||||
|
else
|
||||||
|
:lists.map(fn {k, v} ->
|
||||||
|
{:type, line(meta), :map_field_assoc, typespec(k, vars, caller), typespec(v, vars, caller)}
|
||||||
|
end, fields)
|
||||||
|
end
|
||||||
|
|
||||||
{:type, line(meta), :map, fields}
|
{:type, line(meta), :map, fields}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -884,7 +901,8 @@ defmodule Kernel.Typespec do
|
|||||||
|
|
||||||
# Handle ranges
|
# Handle ranges
|
||||||
defp typespec({:.., meta, args}, vars, caller) do
|
defp typespec({:.., meta, args}, vars, caller) do
|
||||||
typespec({:range, meta, args}, vars, caller)
|
args = for arg <- args, do: typespec(arg, vars, caller)
|
||||||
|
{:type, line(meta), :range, args}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Handle special forms
|
# Handle special forms
|
||||||
@@ -968,9 +986,21 @@ defmodule Kernel.Typespec do
|
|||||||
typespec((quote do: :elixir.as_boolean(unquote(arg))), vars, caller)
|
typespec((quote do: :elixir.as_boolean(unquote(arg))), vars, caller)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp typespec({:fun, meta, args}, vars, caller) do
|
||||||
|
args = for arg <- args, do: typespec(arg, vars, caller)
|
||||||
|
{:type, line(meta), :fun, args}
|
||||||
|
end
|
||||||
|
|
||||||
defp typespec({name, meta, arguments}, vars, caller) do
|
defp typespec({name, meta, arguments}, vars, caller) do
|
||||||
arguments = for arg <- arguments, do: typespec(arg, vars, caller)
|
arguments = for arg <- arguments, do: typespec(arg, vars, caller)
|
||||||
{:type, line(meta), name, arguments}
|
|
||||||
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
|
arity = length(arguments)
|
||||||
|
type = if :erl_internal.is_type(name, arity), do: :type, else: :user_type
|
||||||
|
{type, line(meta), name, arguments}
|
||||||
|
else
|
||||||
|
{:type, line(meta), name, arguments}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Handle literals
|
# Handle literals
|
||||||
|
|||||||
@@ -833,7 +833,7 @@ defmodule Macro do
|
|||||||
:elixir_lexical.record_remote(receiver, env.lexical_tracker)
|
:elixir_lexical.record_remote(receiver, env.lexical_tracker)
|
||||||
{receiver, true}
|
{receiver, true}
|
||||||
aliases ->
|
aliases ->
|
||||||
aliases = for alias <- aliases, do: elem(do_expand_once(alias, env), 0)
|
aliases = :lists.map(&elem(do_expand_once(&1, env), 0), aliases)
|
||||||
|
|
||||||
case :lists.all(&is_atom/1, aliases) do
|
case :lists.all(&is_atom/1, aliases) do
|
||||||
true ->
|
true ->
|
||||||
|
|||||||
+28
-35
@@ -504,19 +504,19 @@ defmodule Module do
|
|||||||
def add_doc(module, line, kind, tuple, signature, doc) when
|
def add_doc(module, line, kind, tuple, signature, doc) when
|
||||||
kind in [:def, :defmacro, :type, :opaque] and (is_binary(doc) or is_boolean(doc) or doc == nil) do
|
kind in [:def, :defmacro, :type, :opaque] and (is_binary(doc) or is_boolean(doc) or doc == nil) do
|
||||||
assert_not_compiled!(:add_doc, module)
|
assert_not_compiled!(:add_doc, module)
|
||||||
table = docs_table_for(module)
|
table = data_table_for(module)
|
||||||
|
|
||||||
{signature, _} = :lists.mapfoldl fn(x, acc) ->
|
{signature, _} = :lists.mapfoldl fn(x, acc) ->
|
||||||
{simplify_signature(x, acc), acc + 1}
|
{simplify_signature(x, acc), acc + 1}
|
||||||
end, 1, signature
|
end, 1, signature
|
||||||
|
|
||||||
case :ets.lookup(table, tuple) do
|
case :ets.lookup(table, {:doc, tuple}) do
|
||||||
[] ->
|
[] ->
|
||||||
:ets.insert(table, {tuple, line, kind, signature, doc})
|
:ets.insert(table, {{:doc, tuple}, line, kind, signature, doc})
|
||||||
:ok
|
:ok
|
||||||
[{tuple, line, _old_kind, old_sign, old_doc}] ->
|
[{doc_tuple, line, _old_kind, old_sign, old_doc}] ->
|
||||||
:ets.insert(table, {
|
:ets.insert(table, {
|
||||||
tuple,
|
doc_tuple,
|
||||||
line,
|
line,
|
||||||
kind,
|
kind,
|
||||||
merge_signatures(old_sign, signature, 1),
|
merge_signatures(old_sign, signature, 1),
|
||||||
@@ -612,7 +612,7 @@ defmodule Module do
|
|||||||
"""
|
"""
|
||||||
def defines?(module, tuple) when is_tuple(tuple) do
|
def defines?(module, tuple) when is_tuple(tuple) do
|
||||||
assert_not_compiled!(:defines?, module)
|
assert_not_compiled!(:defines?, module)
|
||||||
table = function_table_for(module)
|
table = defs_table_for(module)
|
||||||
:ets.lookup(table, tuple) != []
|
:ets.lookup(table, tuple) != []
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -632,7 +632,7 @@ defmodule Module do
|
|||||||
"""
|
"""
|
||||||
def defines?(module, tuple, kind) do
|
def defines?(module, tuple, kind) do
|
||||||
assert_not_compiled!(:defines?, module)
|
assert_not_compiled!(:defines?, module)
|
||||||
table = function_table_for(module)
|
table = defs_table_for(module)
|
||||||
case :ets.lookup(table, tuple) do
|
case :ets.lookup(table, tuple) do
|
||||||
[{_, ^kind, _, _, _, _, _}] -> true
|
[{_, ^kind, _, _, _, _, _}] -> true
|
||||||
_ -> false
|
_ -> false
|
||||||
@@ -652,8 +652,8 @@ defmodule Module do
|
|||||||
"""
|
"""
|
||||||
def definitions_in(module) do
|
def definitions_in(module) do
|
||||||
assert_not_compiled!(:definitions_in, module)
|
assert_not_compiled!(:definitions_in, module)
|
||||||
table = function_table_for(module)
|
table = defs_table_for(module)
|
||||||
for {tuple, _, _, _, _, _, _} <- :ets.tab2list(table), do: tuple
|
:lists.concat :ets.match(table, {:'$1', :_, :_, :_, :_, :_, :_})
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -671,8 +671,8 @@ defmodule Module do
|
|||||||
"""
|
"""
|
||||||
def definitions_in(module, kind) do
|
def definitions_in(module, kind) do
|
||||||
assert_not_compiled!(:definitions_in, module)
|
assert_not_compiled!(:definitions_in, module)
|
||||||
table = function_table_for(module)
|
table = defs_table_for(module)
|
||||||
for {tuple, stored_kind, _, _, _, _, _} <- :ets.tab2list(table), stored_kind == kind, do: tuple
|
:lists.concat :ets.match(table, {:'$1', kind, :_, :_, :_, :_, :_})
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -684,7 +684,7 @@ defmodule Module do
|
|||||||
def make_overridable(module, tuples) do
|
def make_overridable(module, tuples) do
|
||||||
assert_not_compiled!(:make_overridable, module)
|
assert_not_compiled!(:make_overridable, module)
|
||||||
|
|
||||||
for tuple <- tuples do
|
:lists.foreach(fn tuple ->
|
||||||
case :elixir_def.lookup_definition(module, tuple) do
|
case :elixir_def.lookup_definition(module, tuple) do
|
||||||
false ->
|
false ->
|
||||||
{name, arity} = tuple
|
{name, arity} = tuple
|
||||||
@@ -692,27 +692,26 @@ defmodule Module do
|
|||||||
clause ->
|
clause ->
|
||||||
:elixir_def.delete_definition(module, tuple)
|
:elixir_def.delete_definition(module, tuple)
|
||||||
|
|
||||||
neighbours = if loaded?(Module.LocalsTracker) do
|
neighbours = if :elixir_compiler.get_opt(:internal) do
|
||||||
Module.LocalsTracker.yank(module, tuple)
|
|
||||||
else
|
|
||||||
[]
|
[]
|
||||||
|
else
|
||||||
|
Module.LocalsTracker.yank(module, tuple)
|
||||||
end
|
end
|
||||||
|
|
||||||
old = get_attribute(module, :__overridable)
|
old = :elixir_def_overridable.overridable(module)
|
||||||
merged = :orddict.update(tuple, fn({count, _, _, _}) ->
|
merged = :orddict.update(tuple, fn({count, _, _, _}) ->
|
||||||
{count + 1, clause, neighbours, false}
|
{count + 1, clause, neighbours, false}
|
||||||
end, {1, clause, neighbours, false}, old)
|
end, {1, clause, neighbours, false}, old)
|
||||||
|
:elixir_def_overridable.overridable(module, merged)
|
||||||
put_attribute(module, :__overridable, merged)
|
|
||||||
end
|
end
|
||||||
end
|
end, tuples)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns `true` if `tuple` in `module` is marked as overridable.
|
Returns `true` if `tuple` in `module` is marked as overridable.
|
||||||
"""
|
"""
|
||||||
def overridable?(module, tuple) do
|
def overridable?(module, tuple) do
|
||||||
!!List.keyfind(get_attribute(module, :__overridable), tuple, 0)
|
!!List.keyfind(:elixir_def_overridable.overridable(module), tuple, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -731,7 +730,7 @@ defmodule Module do
|
|||||||
assert_not_compiled!(:put_attribute, module)
|
assert_not_compiled!(:put_attribute, module)
|
||||||
table = data_table_for(module)
|
table = data_table_for(module)
|
||||||
value = normalize_attribute(key, value)
|
value = normalize_attribute(key, value)
|
||||||
acc = :ets.lookup_element(table, :__acc_attributes, 2)
|
acc = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||||
|
|
||||||
new =
|
new =
|
||||||
if :lists.member(key, acc) do
|
if :lists.member(key, acc) do
|
||||||
@@ -832,13 +831,13 @@ defmodule Module do
|
|||||||
table = data_table_for(module)
|
table = data_table_for(module)
|
||||||
|
|
||||||
if Keyword.get(opts, :persist) do
|
if Keyword.get(opts, :persist) do
|
||||||
old = :ets.lookup_element(table, :__persisted_attributes, 2)
|
old = :ets.lookup_element(table, {:elixir, :persisted_attributes}, 2)
|
||||||
:ets.insert(table, {:__persisted_attributes, [new|old]})
|
:ets.insert(table, {{:elixir, :persisted_attributes}, [new|old]})
|
||||||
end
|
end
|
||||||
|
|
||||||
if Keyword.get(opts, :accumulate) do
|
if Keyword.get(opts, :accumulate) do
|
||||||
old = :ets.lookup_element(table, :__acc_attributes, 2)
|
old = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||||
:ets.insert(table, {:__acc_attributes, [new|old]})
|
:ets.insert(table, {{:elixir, :acc_attributes}, [new|old]})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -901,7 +900,7 @@ defmodule Module do
|
|||||||
case :ets.lookup(table, key) do
|
case :ets.lookup(table, key) do
|
||||||
[{^key, val}] -> val
|
[{^key, val}] -> val
|
||||||
[] ->
|
[] ->
|
||||||
acc = :ets.lookup_element(table, :__acc_attributes, 2)
|
acc = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
:lists.member(key, acc) ->
|
:lists.member(key, acc) ->
|
||||||
@@ -956,15 +955,11 @@ defmodule Module do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp data_table_for(module) do
|
defp data_table_for(module) do
|
||||||
module
|
:elixir_module.data_table(module)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp function_table_for(module) do
|
defp defs_table_for(module) do
|
||||||
:elixir_def.table(module)
|
:elixir_module.defs_table(module)
|
||||||
end
|
|
||||||
|
|
||||||
defp docs_table_for(module) do
|
|
||||||
:elixir_module.docs_table(module)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp assert_not_compiled!(fun, module) do
|
defp assert_not_compiled!(fun, module) do
|
||||||
@@ -972,6 +967,4 @@ defmodule Module do
|
|||||||
raise ArgumentError,
|
raise ArgumentError,
|
||||||
"could not call #{fun} on module #{inspect module} because it was already compiled"
|
"could not call #{fun} on module #{inspect module} because it was already compiled"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp loaded?(module), do: is_tuple :code.is_loaded(module)
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ defmodule Module.LocalsTracker do
|
|||||||
defp to_pid(pid) when is_pid(pid), do: pid
|
defp to_pid(pid) when is_pid(pid), do: pid
|
||||||
defp to_pid(mod) when is_atom(mod) do
|
defp to_pid(mod) when is_atom(mod) do
|
||||||
table = :elixir_module.data_table(mod)
|
table = :elixir_module.data_table(mod)
|
||||||
[{_, val}] = :ets.lookup(table, :__locals_tracker)
|
[{_, val}] = :ets.lookup(table, {:elixir, :locals_tracker})
|
||||||
val
|
val
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+14
-12
@@ -19,11 +19,12 @@ defmodule Protocol do
|
|||||||
defmacro def({name, _, args}) when is_atom(name) and is_list(args) do
|
defmacro def({name, _, args}) when is_atom(name) and is_list(args) do
|
||||||
arity = length(args)
|
arity = length(args)
|
||||||
|
|
||||||
type_args = for _ <- :lists.seq(2, arity), do: quote(do: term)
|
type_args = :lists.map(fn _ -> quote(do: term) end,
|
||||||
|
:lists.seq(2, arity))
|
||||||
type_args = [quote(do: t) | type_args]
|
type_args = [quote(do: t) | type_args]
|
||||||
|
|
||||||
call_args = for i <- :lists.seq(2, arity),
|
call_args = :lists.map(fn i -> {String.to_atom(<<?x, i + 64>>), [], __MODULE__} end,
|
||||||
do: {String.to_atom(<<?x, i + 64>>), [], __MODULE__}
|
:lists.seq(2, arity))
|
||||||
call_args = [quote(do: t) | call_args]
|
call_args = [quote(do: t) | call_args]
|
||||||
|
|
||||||
quote do
|
quote do
|
||||||
@@ -440,7 +441,7 @@ defmodule Protocol do
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Define the implementation for builtins.
|
# Define the implementation for builtins.
|
||||||
for {guard, mod} <- builtin do
|
:lists.foreach(fn {guard, mod} ->
|
||||||
target = Module.concat(__MODULE__, mod)
|
target = Module.concat(__MODULE__, mod)
|
||||||
|
|
||||||
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
||||||
@@ -449,7 +450,7 @@ defmodule Protocol do
|
|||||||
false -> any_impl_for
|
false -> any_impl_for
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end, builtin)
|
||||||
|
|
||||||
@spec impl_for!(term) :: atom() | no_return()
|
@spec impl_for!(term) :: atom() | no_return()
|
||||||
Kernel.def impl_for!(data) do
|
Kernel.def impl_for!(data) do
|
||||||
@@ -609,16 +610,17 @@ defmodule Protocol do
|
|||||||
@doc false
|
@doc false
|
||||||
def __spec__?(module, name, arity) do
|
def __spec__?(module, name, arity) do
|
||||||
signature = {name, arity}
|
signature = {name, arity}
|
||||||
specs = Module.get_attribute(module, :spec)
|
|
||||||
|
|
||||||
|
specs = Module.get_attribute(module, :spec)
|
||||||
found =
|
found =
|
||||||
for {:spec, expr, caller} <- specs,
|
:lists.map(fn {:spec, expr, caller} ->
|
||||||
Kernel.Typespec.spec_to_signature(expr) == signature do
|
if Kernel.Typespec.spec_to_signature(expr) == signature do
|
||||||
Kernel.Typespec.define_spec(:callback, expr, caller)
|
Kernel.Typespec.define_spec(:callback, expr, caller)
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
end, specs)
|
||||||
|
|
||||||
found != []
|
:lists.any(& &1 == true, found)
|
||||||
end
|
end
|
||||||
|
|
||||||
## Helpers
|
## Helpers
|
||||||
|
|||||||
@@ -45,10 +45,11 @@ defmodule Record.Extractor do
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Read a file and return its abstract syntax form that also
|
# Read a file and return its abstract syntax form that also
|
||||||
# includes record and other preprocessor modules. This is done
|
# includes record but with macros and other attributes expanded,
|
||||||
# by using Erlang's epp_dodger.
|
# such as `-include(...)` and `-include_lib(...)`. This is done
|
||||||
|
# by using Erlang's epp.
|
||||||
defp read_file(file) do
|
defp read_file(file) do
|
||||||
case :epp_dodger.quick_parse_file(file) do
|
case :epp.parse_file(file, []) do
|
||||||
{:ok, form} ->
|
{:ok, form} ->
|
||||||
form
|
form
|
||||||
other ->
|
other ->
|
||||||
@@ -80,11 +81,11 @@ defmodule Record.Extractor do
|
|||||||
|
|
||||||
defp eval_record(cons, form) do
|
defp eval_record(cons, form) do
|
||||||
form = form ++
|
form = form ++
|
||||||
[ {:function, 0, :hello, 0, [
|
[{:function, 0, :hello, 0, [
|
||||||
{:clause, 0, [], [], [ cons ]} ]} ]
|
{:clause, 0, [], [], [cons]}]}]
|
||||||
|
|
||||||
{:function, 0, :hello, 0, [
|
{:function, 0, :hello, 0, [
|
||||||
{:clause, 0, [], [], [ record_ast ]} ]} = :erl_expand_records.module(form, []) |> List.last
|
{:clause, 0, [], [], [record_ast]}]} = :erl_expand_records.module(form, []) |> List.last
|
||||||
|
|
||||||
{:value, record, _} = :erl_eval.expr(record_ast, [])
|
{:value, record, _} = :erl_eval.expr(record_ast, [])
|
||||||
record
|
record
|
||||||
|
|||||||
@@ -419,8 +419,8 @@ defmodule Regex do
|
|||||||
|
|
||||||
The replacement can be either a string or a function. The string
|
The replacement can be either a string or a function. The string
|
||||||
is used as a replacement for every match and it allows specific
|
is used as a replacement for every match and it allows specific
|
||||||
captures to be accessed via `\N`, where `N` is the capture. In
|
captures to be accessed via `\N` or `\g{N}`, where `N` is the
|
||||||
case `\0` is used, the whole match is inserted.
|
capture. In case `\0` is used, the whole match is inserted.
|
||||||
|
|
||||||
When the replacement is a function, the function may have arity
|
When the replacement is a function, the function may have arity
|
||||||
N where each argument maps to a capture, with the first argument
|
N where each argument maps to a capture, with the first argument
|
||||||
@@ -479,6 +479,11 @@ defmodule Regex do
|
|||||||
defp precompile_replacement(""),
|
defp precompile_replacement(""),
|
||||||
do: []
|
do: []
|
||||||
|
|
||||||
|
defp precompile_replacement(<<?\\, ?g, ?{, rest :: binary>>) when byte_size(rest) > 0 do
|
||||||
|
{ns, <<?}, rest :: binary>>} = pick_int(rest)
|
||||||
|
[List.to_integer(ns) | precompile_replacement(rest)]
|
||||||
|
end
|
||||||
|
|
||||||
defp precompile_replacement(<<?\\, x, rest :: binary>>) when x < ?0 or x > ?9 do
|
defp precompile_replacement(<<?\\, x, rest :: binary>>) when x < ?0 or x > ?9 do
|
||||||
case precompile_replacement(rest) do
|
case precompile_replacement(rest) do
|
||||||
[head | t] when is_binary(head) ->
|
[head | t] when is_binary(head) ->
|
||||||
@@ -550,7 +555,7 @@ defmodule Regex do
|
|||||||
cond do
|
cond do
|
||||||
is_binary(part) ->
|
is_binary(part) ->
|
||||||
part
|
part
|
||||||
part > tuple_size(indexes) ->
|
part >= tuple_size(indexes) ->
|
||||||
""
|
""
|
||||||
true ->
|
true ->
|
||||||
get_index(string, elem(indexes, part))
|
get_index(string, elem(indexes, part))
|
||||||
|
|||||||
+37
-30
@@ -156,18 +156,22 @@ defmodule Stream do
|
|||||||
@spec chunk(Enumerable.t, non_neg_integer, non_neg_integer, Enumerable.t | nil) :: Enumerable.t
|
@spec chunk(Enumerable.t, non_neg_integer, non_neg_integer, Enumerable.t | nil) :: Enumerable.t
|
||||||
def chunk(enum, n, step, pad \\ nil) when n > 0 and step > 0 do
|
def chunk(enum, n, step, pad \\ nil) when n > 0 and step > 0 do
|
||||||
limit = :erlang.max(n, step)
|
limit = :erlang.max(n, step)
|
||||||
lazy enum, {[], 0},
|
if is_nil(pad) do
|
||||||
fn(f1) -> R.chunk(n, step, limit, f1) end,
|
lazy enum, {[], 0}, fn(f1) -> R.chunk(n, step, limit, f1) end
|
||||||
fn(f1) -> &do_chunk(&1, n, pad, f1) end
|
else
|
||||||
|
lazy enum, {[], 0},
|
||||||
|
fn(f1) -> R.chunk(n, step, limit, f1) end,
|
||||||
|
&do_chunk(&1, n, pad, &2)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_chunk(acc(h, {buffer, count} = old, t) = acc, n, pad, f1) do
|
defp do_chunk(acc(_, {_, 0}, _) = acc, _, _, _) do
|
||||||
if is_nil(pad) || count == 0 do
|
{:cont, acc}
|
||||||
{:cont, acc}
|
end
|
||||||
else
|
|
||||||
buffer = :lists.reverse(buffer) ++ Enum.take(pad, n - count)
|
defp do_chunk(acc(h, {buffer, count} = old, t), n, pad, f1) do
|
||||||
cont_with_acc(f1, buffer, h, old, t)
|
buffer = :lists.reverse(buffer) ++ Enum.take(pad, n - count)
|
||||||
end
|
cont_with_acc(f1, buffer, h, old, t)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -186,7 +190,7 @@ defmodule Stream do
|
|||||||
def chunk_by(enum, fun) do
|
def chunk_by(enum, fun) do
|
||||||
lazy enum, nil,
|
lazy enum, nil,
|
||||||
fn(f1) -> R.chunk_by(fun, f1) end,
|
fn(f1) -> R.chunk_by(fun, f1) end,
|
||||||
fn(f1) -> &do_chunk_by(&1, f1) end
|
&do_chunk_by(&1, &2)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_chunk_by(acc(_, nil, _) = acc, _f1) do
|
defp do_chunk_by(acc(_, nil, _) = acc, _f1) do
|
||||||
@@ -514,11 +518,11 @@ defmodule Stream do
|
|||||||
@spec take(Enumerable.t, non_neg_integer) :: Enumerable.t
|
@spec take(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||||
def take(_enum, 0), do: %Stream{enum: []}
|
def take(_enum, 0), do: %Stream{enum: []}
|
||||||
|
|
||||||
def take(enum, n) when n > 0 do
|
def take(enum, n) when n > 0 and is_integer(n) do
|
||||||
lazy enum, n, fn(f1) -> R.take(f1) end
|
lazy enum, n, fn(f1) -> R.take(f1) end
|
||||||
end
|
end
|
||||||
|
|
||||||
def take(enum, n) when n < 0 do
|
def take(enum, n) when n < 0 and is_integer(n) do
|
||||||
&do_take(enum, abs(n), &1, &2)
|
&do_take(enum, abs(n), &1, &2)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -695,9 +699,11 @@ defmodule Stream do
|
|||||||
next.({:halt, next_acc})
|
next.({:halt, next_acc})
|
||||||
:erlang.raise(kind, reason, stacktrace)
|
:erlang.raise(kind, reason, stacktrace)
|
||||||
else
|
else
|
||||||
{:halted, [:outer|acc]} ->
|
# Only take into account outer halts when the op is not halt itself.
|
||||||
|
# Otherwise, we were the ones wishing to halt, so we should just stop.
|
||||||
|
{:halted, [:outer|acc]} when op != :halt ->
|
||||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner)
|
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner)
|
||||||
{:halted, [:inner|acc]} ->
|
{:halted, [_|acc]} ->
|
||||||
next.({:halt, next_acc})
|
next.({:halt, next_acc})
|
||||||
{:halted, acc}
|
{:halted, acc}
|
||||||
{:done, [_|acc]} ->
|
{:done, [_|acc]} ->
|
||||||
@@ -1127,12 +1133,12 @@ defmodule Stream do
|
|||||||
|
|
||||||
@compile {:inline, lazy: 2, lazy: 3, lazy: 4}
|
@compile {:inline, lazy: 2, lazy: 3, lazy: 4}
|
||||||
|
|
||||||
defp lazy(%Stream{funs: funs} = lazy, fun),
|
defp lazy(%Stream{done: nil, funs: funs} = lazy, fun),
|
||||||
do: %{lazy | funs: [fun|funs] }
|
do: %{lazy | funs: [fun|funs] }
|
||||||
defp lazy(enum, fun),
|
defp lazy(enum, fun),
|
||||||
do: %Stream{enum: enum, funs: [fun]}
|
do: %Stream{enum: enum, funs: [fun]}
|
||||||
|
|
||||||
defp lazy(%Stream{funs: funs, accs: accs} = lazy, acc, fun),
|
defp lazy(%Stream{done: nil, funs: funs, accs: accs} = lazy, acc, fun),
|
||||||
do: %{lazy | funs: [fun|funs], accs: [acc|accs] }
|
do: %{lazy | funs: [fun|funs], accs: [acc|accs] }
|
||||||
defp lazy(enum, acc, fun),
|
defp lazy(enum, acc, fun),
|
||||||
do: %Stream{enum: enum, funs: [fun], accs: [acc]}
|
do: %Stream{enum: enum, funs: [fun], accs: [acc]}
|
||||||
@@ -1171,19 +1177,20 @@ defimpl Enumerable, for: Stream do
|
|||||||
case reduce.({command, [acc|accs]}) do
|
case reduce.({command, [acc|accs]}) do
|
||||||
{:suspended, [acc|accs], continuation} ->
|
{:suspended, [acc|accs], continuation} ->
|
||||||
{:suspended, acc, &do_each(continuation, done, accs, &1)}
|
{:suspended, acc, &do_each(continuation, done, accs, &1)}
|
||||||
{:halted, [acc|_]} ->
|
{:halted, accs} ->
|
||||||
{:halted, acc}
|
do_done {:halted, accs}, done
|
||||||
{:done, [acc|_] = accs} ->
|
{:done, accs} ->
|
||||||
case done do
|
do_done {:done, accs}, done
|
||||||
nil ->
|
end
|
||||||
{:done, acc}
|
end
|
||||||
{done, fun} ->
|
|
||||||
case done.(fun).(accs) do
|
defp do_done({reason, [acc|_]}, nil), do: {reason, acc}
|
||||||
{:cont, [acc|_]} -> {:done, acc}
|
defp do_done({reason, [acc|t]}, {done, fun}) do
|
||||||
{:halt, [acc|_]} -> {:halted, acc}
|
[h|_] = Enum.reverse(t)
|
||||||
{:suspend, [acc|_]} -> {:suspended, acc, &({:done, elem(&1, 1)})}
|
case done.([acc, h], fun) do
|
||||||
end
|
{:cont, [acc|_]} -> {reason, acc}
|
||||||
end
|
{:halt, [acc|_]} -> {:halted, acc}
|
||||||
|
{:suspend, [acc|_]} -> {:suspended, acc, &({:done, elem(&1, 1)})}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -132,10 +132,16 @@ defmodule Stream.Reducers do
|
|||||||
defmacro take(f \\ nil) do
|
defmacro take(f \\ nil) do
|
||||||
quote do
|
quote do
|
||||||
fn(entry, acc(h, n, t) = orig) ->
|
fn(entry, acc(h, n, t) = orig) ->
|
||||||
if n >= 1 do
|
case n do
|
||||||
cont_with_acc(unquote(f), entry, h, n-1, t)
|
0 ->
|
||||||
else
|
{:halt, orig}
|
||||||
{:halt, orig}
|
1 ->
|
||||||
|
case cont_with_acc(unquote(f), entry, h, n-1, t) do
|
||||||
|
{:cont, acc} -> {:halt, acc}
|
||||||
|
reason -> reason
|
||||||
|
end
|
||||||
|
_ ->
|
||||||
|
cont_with_acc(unquote(f), entry, h, n-1, t)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+12
-11
@@ -74,21 +74,21 @@ defmodule String do
|
|||||||
be avoided in favor of binary functions or pattern matching.
|
be avoided in favor of binary functions or pattern matching.
|
||||||
For example, imagine you have a string `prefix` and you want to
|
For example, imagine you have a string `prefix` and you want to
|
||||||
remove this prefix from another string named `full`.
|
remove this prefix from another string named `full`.
|
||||||
|
|
||||||
One may be tempted to write:
|
One may be tempted to write:
|
||||||
|
|
||||||
iex> take_prefix = fn full, prefix ->
|
iex> take_prefix = fn full, prefix ->
|
||||||
...> base = String.length(prefix)
|
...> base = String.length(prefix)
|
||||||
...> String.slice(full, base, String.length(full) - base)
|
...> String.slice(full, base, String.length(full) - base)
|
||||||
...> end
|
...> end
|
||||||
...> take_prefix.("Mr. John", "Mr. ")
|
...> take_prefix.("Mr. John", "Mr. ")
|
||||||
"John"
|
"John"
|
||||||
|
|
||||||
Although the function above works, it performs poorly. To
|
Although the function above works, it performs poorly. To
|
||||||
calculate the length of the string, we need to traverse it
|
calculate the length of the string, we need to traverse it
|
||||||
fully, so we traverse both `prefix` and `full` strings, then
|
fully, so we traverse both `prefix` and `full` strings, then
|
||||||
slice the `full` one, traversing it again.
|
slice the `full` one, traversing it again.
|
||||||
|
|
||||||
A first attempting at improving it could be with ranges:
|
A first attempting at improving it could be with ranges:
|
||||||
|
|
||||||
iex> take_prefix = fn full, prefix ->
|
iex> take_prefix = fn full, prefix ->
|
||||||
@@ -103,7 +103,7 @@ defmodule String do
|
|||||||
extract a substring from a string, we can use `byte_size/1`
|
extract a substring from a string, we can use `byte_size/1`
|
||||||
and `binary_part/3` as there is no chance we will slice in
|
and `binary_part/3` as there is no chance we will slice in
|
||||||
the middle of a codepoint made of more than one byte:
|
the middle of a codepoint made of more than one byte:
|
||||||
|
|
||||||
iex> take_prefix = fn full, prefix ->
|
iex> take_prefix = fn full, prefix ->
|
||||||
...> base = byte_size(prefix)
|
...> base = byte_size(prefix)
|
||||||
...> binary_part(full, base, byte_size(full) - base)
|
...> binary_part(full, base, byte_size(full) - base)
|
||||||
@@ -112,7 +112,7 @@ defmodule String do
|
|||||||
"John"
|
"John"
|
||||||
|
|
||||||
Or simply used pattern matching:
|
Or simply used pattern matching:
|
||||||
|
|
||||||
iex> take_prefix = fn full, prefix ->
|
iex> take_prefix = fn full, prefix ->
|
||||||
...> base = byte_size(prefix)
|
...> base = byte_size(prefix)
|
||||||
...> <<_ :: binary-size(base), rest :: binary>> = full
|
...> <<_ :: binary-size(base), rest :: binary>> = full
|
||||||
@@ -650,13 +650,14 @@ defmodule String do
|
|||||||
iex> String.replace("a,b,c", ",", "-", global: false)
|
iex> String.replace("a,b,c", ",", "-", global: false)
|
||||||
"a-b,c"
|
"a-b,c"
|
||||||
|
|
||||||
The pattern can also be a regex. In those cases, one can give `\N`
|
The pattern can also be a regex. In those cases, one can give `\N` or
|
||||||
in the `replacement` string to access a specific capture in the regex:
|
`\g{N}` in the `replacement` string to access a specific capture in the
|
||||||
|
regex:
|
||||||
|
|
||||||
iex> String.replace("a,b,c", ~r/,(.)/, ",\\1\\1")
|
iex> String.replace("a,b,c", ~r/,(.)/, ",\\1\\1")
|
||||||
"a,bb,cc"
|
"a,bb,cc"
|
||||||
|
|
||||||
Notice we had to escape the escape character `\`. By giving `&`,
|
Notice we had to escape the escape character `\`. By giving `\0`,
|
||||||
one can inject the whole matched pattern in the replacement string.
|
one can inject the whole matched pattern in the replacement string.
|
||||||
|
|
||||||
When strings are used as a pattern, a developer can also use the
|
When strings are used as a pattern, a developer can also use the
|
||||||
@@ -672,8 +673,8 @@ defmodule String do
|
|||||||
"a[,,]b[,,]c"
|
"a[,,]b[,,]c"
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec replace(t, t, t) :: t
|
@spec replace(t, t | Regex.t, t) :: t
|
||||||
@spec replace(t, t, t, Keyword.t) :: t
|
@spec replace(t, t | Regex.t, t, Keyword.t) :: t
|
||||||
|
|
||||||
def replace(subject, pattern, replacement, options \\ []) when is_binary(replacement) do
|
def replace(subject, pattern, replacement, options \\ []) when is_binary(replacement) do
|
||||||
if Regex.regex?(pattern) do
|
if Regex.regex?(pattern) do
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ defmodule Supervisor do
|
|||||||
defmodule Stack do
|
defmodule Stack do
|
||||||
use GenServer
|
use GenServer
|
||||||
|
|
||||||
def start_link(state) do
|
def start_link(state, opts) do
|
||||||
GenServer.start_link(__MODULE__, state, [name: :sup_stack])
|
GenServer.start_link(__MODULE__, state, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_call(:pop, _from, [h|t]) do
|
def handle_call(:pop, _from, [h|t]) do
|
||||||
@@ -38,16 +38,17 @@ defmodule Supervisor do
|
|||||||
# Import helpers for defining supervisors
|
# Import helpers for defining supervisors
|
||||||
import Supervisor.Spec
|
import Supervisor.Spec
|
||||||
|
|
||||||
# We are going to supervise the Stack server which will
|
# We are going to supervise the Stack server which
|
||||||
# be started with a single argument [:hello]
|
# will be started with a single argument [:hello]
|
||||||
|
# and the default name of :sup_stack.
|
||||||
children = [
|
children = [
|
||||||
worker(Stack, [[:hello]])
|
worker(Stack, [[:hello], [name: :sup_stack]])
|
||||||
]
|
]
|
||||||
|
|
||||||
# Start the supervisor with our one child
|
# Start the supervisor with our one child
|
||||||
{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
|
{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
|
||||||
|
|
||||||
Notice that when starting the GenServer, we have registered it
|
Notice that when starting the GenServer, we are registering it
|
||||||
with name `:sup_stack`, which allows us to call it directly and
|
with name `:sup_stack`, which allows us to call it directly and
|
||||||
get what is on the stack:
|
get what is on the stack:
|
||||||
|
|
||||||
@@ -65,7 +66,7 @@ defmodule Supervisor do
|
|||||||
Let's try it:
|
Let's try it:
|
||||||
|
|
||||||
GenServer.call(:sup_stack, :pop)
|
GenServer.call(:sup_stack, :pop)
|
||||||
=ERROR REPORT====
|
** (exit) exited in: GenServer.call(:sup_stack, :pop, 5000)
|
||||||
|
|
||||||
Luckily, since the server is being supervised by a supervisor, the
|
Luckily, since the server is being supervised by a supervisor, the
|
||||||
supervisor will automatically start a new one, with the default stack
|
supervisor will automatically start a new one, with the default stack
|
||||||
@@ -136,6 +137,70 @@ defmodule Supervisor do
|
|||||||
in this module behave slightly differently when this strategy is
|
in this module behave slightly differently when this strategy is
|
||||||
used.
|
used.
|
||||||
|
|
||||||
|
## Simple one for one
|
||||||
|
|
||||||
|
The simple one for one supervisor is useful when you want to dynamically
|
||||||
|
start and stop supervisor children. For example, imagine you want to
|
||||||
|
dynamically create multiple stacks. We can do so by defining a simple one
|
||||||
|
for one supervisor:
|
||||||
|
|
||||||
|
# Import helpers for defining supervisors
|
||||||
|
import Supervisor.Spec
|
||||||
|
|
||||||
|
# This time, we don't pass any argument because
|
||||||
|
# the argument will be given when we start the child
|
||||||
|
children = [
|
||||||
|
worker(Stack, [], restart: :transient)
|
||||||
|
]
|
||||||
|
|
||||||
|
# Start the supervisor with our one child
|
||||||
|
{:ok, sup_pid} = Supervisor.start_link(children, strategy: :simple_one_for_one)
|
||||||
|
|
||||||
|
There are a couple differences here:
|
||||||
|
|
||||||
|
* The simple one for one specification can define only one child which
|
||||||
|
works as a template for when we call `start_child/2`
|
||||||
|
|
||||||
|
* We have define the child to have restart strategy of transient. This
|
||||||
|
means that, if the child process exits due to a `:normal`, `:shutdown`
|
||||||
|
or `{:shutdown, term}` reason, it won't be restarted. This is useful
|
||||||
|
as it allows our workers to politely shutdown and be removed from the
|
||||||
|
simple one for one supervisor, without being restarted. You can find
|
||||||
|
more information about restart strategies on `Supervisor.Spec`
|
||||||
|
|
||||||
|
With the supervisor defined, let's dynamically start stacks:
|
||||||
|
|
||||||
|
{:ok, pid} = Supervisor.start_child(sup_pid, [[:hello, :world], []])
|
||||||
|
GenServer.call(pid, :pop) #=> :hello
|
||||||
|
GenServer.call(pid, :pop) #=> :world
|
||||||
|
|
||||||
|
{:ok, pid} = Supervisor.start_child(sup_pid, [[:something, :else], []])
|
||||||
|
GenServer.call(pid, :pop) #=> :something
|
||||||
|
GenServer.call(pid, :pop) #=> :else
|
||||||
|
|
||||||
|
Supervisor.count_children(sup_pid)
|
||||||
|
#=> %{active: 2, specs: 1, supervisors: 0, workers: 2}
|
||||||
|
|
||||||
|
## Exit reasons
|
||||||
|
|
||||||
|
From the example above, you may have noticed that the transient restart
|
||||||
|
strategy for the worker does not restart the child in case it crashes with
|
||||||
|
reason `:normal`, `:shutdown` or `{:shutdown, term}`.
|
||||||
|
|
||||||
|
So one may ask: which exit reason should I choose when existing my worker?
|
||||||
|
There are three options:
|
||||||
|
|
||||||
|
* `:normal` - on such cases, the exit won't be logged, there is no restart
|
||||||
|
on transient mode and linked processes do not exit
|
||||||
|
|
||||||
|
* `:shutdown` or `{:shutdown, term}` - on such cases, the exit won't be
|
||||||
|
logged, there is no restart on transient mode and linked processes exit
|
||||||
|
with the same reason unless trapping exits
|
||||||
|
|
||||||
|
* any other term - on such cases, the exit will be logged, there are
|
||||||
|
restarts on transient mode and linked processes exit with the same reason
|
||||||
|
unless trapping exits
|
||||||
|
|
||||||
## Name Registration
|
## Name Registration
|
||||||
|
|
||||||
A supervisor is bound to the same name registration rules as a `GenServer`.
|
A supervisor is bound to the same name registration rules as a `GenServer`.
|
||||||
|
|||||||
+13
-10
@@ -72,7 +72,7 @@ defmodule System do
|
|||||||
"""
|
"""
|
||||||
@spec argv() :: [String.t]
|
@spec argv() :: [String.t]
|
||||||
def argv do
|
def argv do
|
||||||
:elixir_code_server.call :argv
|
:elixir_config.get(:argv)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -83,7 +83,7 @@ defmodule System do
|
|||||||
"""
|
"""
|
||||||
@spec argv([String.t]) :: :ok
|
@spec argv([String.t]) :: :ok
|
||||||
def argv(args) do
|
def argv(args) do
|
||||||
:elixir_code_server.cast({:argv, args})
|
:elixir_config.put(:argv, args)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -224,7 +224,7 @@ defmodule System do
|
|||||||
The function must receive the exit status code as an argument.
|
The function must receive the exit status code as an argument.
|
||||||
"""
|
"""
|
||||||
def at_exit(fun) when is_function(fun, 1) do
|
def at_exit(fun) when is_function(fun, 1) do
|
||||||
:elixir_code_server.cast {:at_exit, fun}
|
:elixir_config.update :at_exit, &[fun|&1]
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -431,18 +431,18 @@ defmodule System do
|
|||||||
@spec cmd(binary, [binary], Keyword.t) ::
|
@spec cmd(binary, [binary], Keyword.t) ::
|
||||||
{Collectable.t, exit_status :: non_neg_integer}
|
{Collectable.t, exit_status :: non_neg_integer}
|
||||||
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
|
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
|
||||||
command = String.to_char_list(command)
|
cmd = String.to_char_list(command)
|
||||||
|
|
||||||
command =
|
cmd =
|
||||||
if Path.type(command) == :absolute do
|
if Path.type(cmd) == :absolute do
|
||||||
command
|
cmd
|
||||||
else
|
else
|
||||||
:os.find_executable(command) || command
|
:os.find_executable(cmd) || :erlang.error(:enoent, [command, args, opts])
|
||||||
end
|
end
|
||||||
|
|
||||||
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
|
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
|
||||||
{initial, fun} = Collectable.into(into)
|
{initial, fun} = Collectable.into(into)
|
||||||
do_cmd Port.open({:spawn_executable, command}, opts), initial, fun
|
do_cmd Port.open({:spawn_executable, cmd}, opts), initial, fun
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_cmd(port, acc, fun) do
|
defp do_cmd(port, acc, fun) do
|
||||||
@@ -463,9 +463,12 @@ defmodule System do
|
|||||||
defp cmd_opts([{:arg0, bin}|t], opts, into) when is_binary(bin),
|
defp cmd_opts([{:arg0, bin}|t], opts, into) when is_binary(bin),
|
||||||
do: cmd_opts(t, [{:arg0, bin}|opts], into)
|
do: cmd_opts(t, [{:arg0, bin}|opts], into)
|
||||||
|
|
||||||
defp cmd_opts([{:stderr_to_stdout, bool}|t], opts, into) when is_boolean(bool),
|
defp cmd_opts([{:stderr_to_stdout, true}|t], opts, into),
|
||||||
do: cmd_opts(t, [:stderr_to_stdout|opts], into)
|
do: cmd_opts(t, [:stderr_to_stdout|opts], into)
|
||||||
|
|
||||||
|
defp cmd_opts([{:stderr_to_stdout, false}|t], opts, into),
|
||||||
|
do: cmd_opts(t, opts, into)
|
||||||
|
|
||||||
defp cmd_opts([{:parallelism, bool}|t], opts, into) when is_boolean(bool),
|
defp cmd_opts([{:parallelism, bool}|t], opts, into) when is_boolean(bool),
|
||||||
do: cmd_opts(t, [{:parallelism, bool}|opts], into)
|
do: cmd_opts(t, [{:parallelism, bool}|opts], into)
|
||||||
|
|
||||||
|
|||||||
+55
-36
@@ -11,18 +11,17 @@ defmodule URI do
|
|||||||
|
|
||||||
import Bitwise
|
import Bitwise
|
||||||
|
|
||||||
@ports %{
|
@schemes [
|
||||||
"ftp" => 21,
|
"ftp",
|
||||||
"http" => 80,
|
"http",
|
||||||
"https" => 443,
|
"https",
|
||||||
"ldap" => 389,
|
"ldap",
|
||||||
"sftp" => 22,
|
"sftp",
|
||||||
"tftp" => 69,
|
"tftp",
|
||||||
}
|
]
|
||||||
|
|
||||||
Enum.each @ports, fn {scheme, port} ->
|
Enum.each @schemes, fn scheme ->
|
||||||
def normalize_scheme(unquote(scheme)), do: unquote(scheme)
|
def normalize_scheme(unquote(scheme)), do: unquote(scheme)
|
||||||
def default_port(unquote(scheme)), do: unquote(port)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -47,8 +46,7 @@ defmodule URI do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def default_port(scheme) when is_binary(scheme) do
|
def default_port(scheme) when is_binary(scheme) do
|
||||||
{:ok, dict} = Application.fetch_env(:elixir, :uri)
|
:elixir_config.get({:uri, scheme})
|
||||||
Map.get(dict, scheme)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -59,8 +57,7 @@ defmodule URI do
|
|||||||
new URIs.
|
new URIs.
|
||||||
"""
|
"""
|
||||||
def default_port(scheme, port) when is_binary(scheme) and port > 0 do
|
def default_port(scheme, port) when is_binary(scheme) and port > 0 do
|
||||||
{:ok, dict} = Application.fetch_env(:elixir, :uri)
|
:elixir_config.put({:uri, scheme}, port)
|
||||||
Application.put_env(:elixir, :uri, Map.put(dict, scheme, port), persistent: true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -98,7 +95,10 @@ defmodule URI do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def decode_query(q, dict \\ %{}) when is_binary(q) do
|
def decode_query(q, dict \\ %{}) when is_binary(q) do
|
||||||
Enum.reduce query_decoder(q), dict, fn({k, v}, acc) -> Dict.put(acc, k, v) end
|
case do_decode_query(q) do
|
||||||
|
nil -> dict
|
||||||
|
{{k, v}, q} -> decode_query(q, Dict.put(dict, k, v))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -107,19 +107,19 @@ defmodule URI do
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map &(&1)
|
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map(&(&1))
|
||||||
[{"foo", "1"}, {"bar", "2"}]
|
[{"foo", "1"}, {"bar", "2"}]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def query_decoder(q) when is_binary(q) do
|
def query_decoder(q) when is_binary(q) do
|
||||||
Stream.unfold(q, &do_decoder/1)
|
Stream.unfold(q, &do_decode_query/1)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_decoder("") do
|
defp do_decode_query("") do
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_decoder(q) do
|
defp do_decode_query(q) do
|
||||||
{first, next} =
|
{first, next} =
|
||||||
case :binary.split(q, "&") do
|
case :binary.split(q, "&") do
|
||||||
[first, rest] -> {first, rest}
|
[first, rest] -> {first, rest}
|
||||||
@@ -234,7 +234,7 @@ defmodule URI do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def decode(uri) do
|
def decode(uri) do
|
||||||
unpercent(uri)
|
unpercent(uri, "", false)
|
||||||
catch
|
catch
|
||||||
:malformed_uri ->
|
:malformed_uri ->
|
||||||
raise ArgumentError, "malformed URI #{inspect uri}"
|
raise ArgumentError, "malformed URI #{inspect uri}"
|
||||||
@@ -250,23 +250,25 @@ defmodule URI do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def decode_www_form(str) do
|
def decode_www_form(str) do
|
||||||
String.split(str, "+") |> Enum.map_join(" ", &unpercent/1)
|
unpercent(str, "", true)
|
||||||
catch
|
catch
|
||||||
:malformed_uri ->
|
:malformed_uri ->
|
||||||
raise ArgumentError, "malformed URI #{inspect str}"
|
raise ArgumentError, "malformed URI #{inspect str}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp unpercent(<<?%, hex_1, hex_2, tail :: binary>>) do
|
defp unpercent(<<?+, tail::binary>>, acc, spaces = true) do
|
||||||
<<bsl(hex_to_dec(hex_1), 4) + hex_to_dec(hex_2)>> <> unpercent(tail)
|
unpercent(tail, <<acc::binary, ?\s>>, spaces)
|
||||||
end
|
|
||||||
defp unpercent(<<?%, _>>), do: throw(:malformed_uri)
|
|
||||||
defp unpercent(<<?%>>), do: throw(:malformed_uri)
|
|
||||||
|
|
||||||
defp unpercent(<<head, tail :: binary>>) do
|
|
||||||
<<head>> <> unpercent(tail)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp unpercent(<<>>), do: <<>>
|
defp unpercent(<<?%, hex_1, hex_2, tail::binary>>, acc, spaces) do
|
||||||
|
unpercent(tail, <<acc::binary, bsl(hex_to_dec(hex_1), 4) + hex_to_dec(hex_2)>>, spaces)
|
||||||
|
end
|
||||||
|
defp unpercent(<<?%, _::binary>>, _acc, _spaces), do: throw(:malformed_uri)
|
||||||
|
|
||||||
|
defp unpercent(<<head, tail::binary>>, acc, spaces) do
|
||||||
|
unpercent(tail, <<acc::binary, head>>, spaces)
|
||||||
|
end
|
||||||
|
defp unpercent(<<>>, acc, _spaces), do: acc
|
||||||
|
|
||||||
defp hex_to_dec(n) when n in ?A..?F, do: n - ?A + 10
|
defp hex_to_dec(n) when n in ?A..?F, do: n - ?A + 10
|
||||||
defp hex_to_dec(n) when n in ?a..?f, do: n - ?a + 10
|
defp hex_to_dec(n) when n in ?a..?f, do: n - ?a + 10
|
||||||
@@ -274,12 +276,17 @@ defmodule URI do
|
|||||||
defp hex_to_dec(_n), do: throw(:malformed_uri)
|
defp hex_to_dec(_n), do: throw(:malformed_uri)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Parses a URI into components.
|
Parses a well-formed URI reference into its components.
|
||||||
|
|
||||||
URIs have portions that are handled specially for the particular
|
Note this function expects a well-formed URI and does not perform
|
||||||
scheme of the URI. For example, http and https have different
|
any validation. See the examples section below of how `URI.parse/1`
|
||||||
default ports. Such values can be accessed and registered via
|
can be used to parse a wide range of relative URIs.
|
||||||
`URI.default_port/1` and `URI.default_port/2`.
|
|
||||||
|
This function uses the parsing regular expression as defined
|
||||||
|
in the Appendix B of RFC3986.
|
||||||
|
|
||||||
|
When a URI is given without a port, the values registered via
|
||||||
|
`URI.default_port/1` and `URI.default_port/2` are used.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@@ -288,12 +295,24 @@ defmodule URI do
|
|||||||
authority: "elixir-lang.org", userinfo: nil,
|
authority: "elixir-lang.org", userinfo: nil,
|
||||||
host: "elixir-lang.org", port: 80}
|
host: "elixir-lang.org", port: 80}
|
||||||
|
|
||||||
|
iex> URI.parse("//elixir-lang.org/")
|
||||||
|
%URI{authority: "elixir-lang.org", fragment: nil, host: "elixir-lang.org",
|
||||||
|
path: "/", port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||||
|
|
||||||
|
iex> URI.parse("/foo/bar")
|
||||||
|
%URI{authority: nil, fragment: nil, host: nil, path: "/foo/bar",
|
||||||
|
port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||||
|
|
||||||
|
iex> URI.parse("foo/bar")
|
||||||
|
%URI{authority: nil, fragment: nil, host: nil, path: "foo/bar",
|
||||||
|
port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def parse(%URI{} = uri), do: uri
|
def parse(%URI{} = uri), do: uri
|
||||||
|
|
||||||
def parse(s) when is_binary(s) do
|
def parse(s) when is_binary(s) do
|
||||||
# From http://tools.ietf.org/html/rfc3986#appendix-B
|
# From http://tools.ietf.org/html/rfc3986#appendix-B
|
||||||
regex = ~r/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/
|
regex = ~r/^(([a-z][a-z0-9\+\-\.]*):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i
|
||||||
parts = nillify(Regex.run(regex, s))
|
parts = nillify(Regex.run(regex, s))
|
||||||
|
|
||||||
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
|
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
|
||||||
|
|||||||
@@ -51,6 +51,19 @@ defmodule Version do
|
|||||||
|
|
||||||
"~> 2.0.0"
|
"~> 2.0.0"
|
||||||
|
|
||||||
|
`~>` will never include pre-release versions of its upper bound.
|
||||||
|
It can also be used to set an upper bound on only the major
|
||||||
|
version part. See the table below for `~>` requirements and
|
||||||
|
their corresponding translation.
|
||||||
|
|
||||||
|
`~>` | Translation
|
||||||
|
:------------- | :---------------------
|
||||||
|
`~> 2.0.0` | `>= 2.0.0 and < 2.1.0`
|
||||||
|
`~> 2.1.2` | `>= 2.1.2 and < 2.2.0`
|
||||||
|
`~> 2.1.3-dev` | `>= 2.1.3-dev and < 2.2.0`
|
||||||
|
`~> 2.0` | `>= 2.0.0 and < 3.0.0`
|
||||||
|
`~> 2.1` | `>= 2.1.0 and < 3.0.0`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Kernel, except: [match?: 2]
|
import Kernel, except: [match?: 2]
|
||||||
@@ -61,7 +74,7 @@ defmodule Version do
|
|||||||
@type major :: String.t | non_neg_integer
|
@type major :: String.t | non_neg_integer
|
||||||
@type minor :: non_neg_integer | nil
|
@type minor :: non_neg_integer | nil
|
||||||
@type patch :: non_neg_integer | nil
|
@type patch :: non_neg_integer | nil
|
||||||
@type pre :: [String.t]
|
@type pre :: [String.t | non_neg_integer]
|
||||||
@type build :: String.t | nil
|
@type build :: String.t | nil
|
||||||
@type matchable :: {major :: major,
|
@type matchable :: {major :: major,
|
||||||
minor :: minor,
|
minor :: minor,
|
||||||
@@ -506,10 +519,22 @@ end
|
|||||||
|
|
||||||
defimpl String.Chars, for: Version do
|
defimpl String.Chars, for: Version do
|
||||||
def to_string(version) do
|
def to_string(version) do
|
||||||
pre = unless Enum.empty?(pre = version.pre), do: "-#{pre}"
|
pre = pre(version.pre)
|
||||||
build = if build = version.build, do: "+#{build}"
|
build = if build = version.build, do: "+#{build}"
|
||||||
"#{version.major}.#{version.minor}.#{version.patch}#{pre}#{build}"
|
"#{version.major}.#{version.minor}.#{version.patch}#{pre}#{build}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp pre([]) do
|
||||||
|
""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp pre(pre) do
|
||||||
|
"-" <>
|
||||||
|
Enum.map_join(pre, ".", fn
|
||||||
|
int when is_integer(int) -> Integer.to_string(int)
|
||||||
|
string when is_binary(string) -> string
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defimpl Inspect, for: Version do
|
defimpl Inspect, for: Version do
|
||||||
|
|||||||
@@ -29,9 +29,12 @@ start(_Type, _Args) ->
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
ok = io:setopts(standard_io, Opts),
|
ok = io:setopts(standard_io, Opts),
|
||||||
%% Must use undocument {unicode, true} to set unicode on standard_error, more
|
|
||||||
%% info: http://erlang.org/pipermail/erlang-bugs/2014-April/004310.html
|
ok = case io:setopts(standard_error, [{encoding,utf8}]) of
|
||||||
ok = io:setopts(standard_error, [{unicode,true}]),
|
ok -> ok;
|
||||||
|
{error, _} -> io:setopts(standard_error, [{unicode,true}]) %% R17.3 and earlier
|
||||||
|
end,
|
||||||
|
|
||||||
case file:native_name_encoding() of
|
case file:native_name_encoding() of
|
||||||
latin1 ->
|
latin1 ->
|
||||||
io:format(standard_error,
|
io:format(standard_error,
|
||||||
@@ -42,10 +45,29 @@ start(_Type, _Args) ->
|
|||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
|
|
||||||
elixir_sup:start_link().
|
URIs = [{<<"ftp">>, 21},
|
||||||
|
{<<"sftp">>, 22},
|
||||||
|
{<<"tftp">>, 69},
|
||||||
|
{<<"http">>, 80},
|
||||||
|
{<<"https">>, 443},
|
||||||
|
{<<"ldap">>, 389}],
|
||||||
|
URIConfig = [{{uri, Scheme}, Port} || {Scheme, Port} <- URIs],
|
||||||
|
CompilerOpts = [{docs,true},{debug_info,true},{warnings_as_errors,false}],
|
||||||
|
Config = [{at_exit, []},
|
||||||
|
{compiler_options, orddict:from_list(CompilerOpts)}
|
||||||
|
| URIConfig],
|
||||||
|
Tab = elixir_config:new(Config),
|
||||||
|
case elixir_sup:start_link() of
|
||||||
|
{ok, Sup} ->
|
||||||
|
{ok, Sup, Tab};
|
||||||
|
{error, _Reason} = Error ->
|
||||||
|
elixir_config:delete(Tab),
|
||||||
|
Error
|
||||||
|
end.
|
||||||
|
|
||||||
|
stop(Tab) ->
|
||||||
|
elixir_config:delete(Tab).
|
||||||
|
|
||||||
stop(_S) ->
|
|
||||||
ok.
|
|
||||||
|
|
||||||
config_change(_Changed, _New, _Remove) ->
|
config_change(_Changed, _New, _Remove) ->
|
||||||
ok.
|
ok.
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ define({Line,E}, Kind, Call, Expr) ->
|
|||||||
unless_loaded(Fun, Args, Callback) ->
|
unless_loaded(Fun, Args, Callback) ->
|
||||||
case code:is_loaded(?kernel) of
|
case code:is_loaded(?kernel) of
|
||||||
{_, _} -> apply(?kernel, Fun, Args);
|
{_, _} -> apply(?kernel, Fun, Args);
|
||||||
false -> Callback()
|
false -> Callback()
|
||||||
end.
|
end.
|
||||||
|
|
||||||
env() ->
|
env() ->
|
||||||
|
|||||||
@@ -6,14 +6,11 @@
|
|||||||
|
|
||||||
-define(timeout, 30000).
|
-define(timeout, 30000).
|
||||||
-record(elixir_code_server, {
|
-record(elixir_code_server, {
|
||||||
compilation_status=[],
|
|
||||||
argv=[],
|
|
||||||
loaded=[],
|
loaded=[],
|
||||||
at_exit=[],
|
|
||||||
pool={[],0},
|
|
||||||
paths={[],[]},
|
paths={[],[]},
|
||||||
compiler_options=[{docs,true},{debug_info,true},{warnings_as_errors,false}],
|
mod_pool={[],0},
|
||||||
erl_compiler_options=nil
|
mod_ets=dict:new(),
|
||||||
|
compilation_status=[]
|
||||||
}).
|
}).
|
||||||
|
|
||||||
call(Args) ->
|
call(Args) ->
|
||||||
@@ -29,14 +26,21 @@ start_link() ->
|
|||||||
|
|
||||||
init(ok) ->
|
init(ok) ->
|
||||||
%% We attempt to load those modules here so throughout
|
%% We attempt to load those modules here so throughout
|
||||||
%% the codebase we can use a code:is_loaded/1 check on
|
%% the codebase we can avoid code:is_loaded/1 checks.
|
||||||
%% bootstrap instead of using the more expensive
|
|
||||||
%% code:ensure_loaded/1 multiple times.
|
|
||||||
_ = code:ensure_loaded('Elixir.Macro.Env'),
|
_ = code:ensure_loaded('Elixir.Macro.Env'),
|
||||||
_ = code:ensure_loaded('Elixir.Module.LocalsTracker'),
|
|
||||||
_ = code:ensure_loaded('Elixir.Kernel.LexicalTracker'),
|
%% The table where we store module definitions
|
||||||
|
_ = ets:new(elixir_modules, [set, protected, named_table, {read_concurrency, true}]),
|
||||||
|
|
||||||
{ok, #elixir_code_server{}}.
|
{ok, #elixir_code_server{}}.
|
||||||
|
|
||||||
|
handle_call({defmodule, Pid, Tuple}, _From, Config) ->
|
||||||
|
{Ref, New} = defmodule(Pid, Tuple, Config),
|
||||||
|
{reply, Ref, New};
|
||||||
|
|
||||||
|
handle_call({undefmodule, Ref}, _From, Config) ->
|
||||||
|
{reply, ok, undefmodule(Ref, Config)};
|
||||||
|
|
||||||
handle_call({acquire, Path}, From, Config) ->
|
handle_call({acquire, Path}, From, Config) ->
|
||||||
Current = Config#elixir_code_server.loaded,
|
Current = Config#elixir_code_server.loaded,
|
||||||
case orddict:find(Path, Current) of
|
case orddict:find(Path, Current) of
|
||||||
@@ -53,18 +57,6 @@ handle_call({acquire, Path}, From, Config) ->
|
|||||||
handle_call(loaded, _From, Config) ->
|
handle_call(loaded, _From, Config) ->
|
||||||
{reply, [F || {F, true} <- Config#elixir_code_server.loaded], Config};
|
{reply, [F || {F, true} <- Config#elixir_code_server.loaded], Config};
|
||||||
|
|
||||||
handle_call(at_exit, _From, Config) ->
|
|
||||||
{reply, Config#elixir_code_server.at_exit, Config};
|
|
||||||
|
|
||||||
handle_call(flush_at_exit, _From, Config) ->
|
|
||||||
{reply, Config#elixir_code_server.at_exit, Config#elixir_code_server{at_exit=[]}};
|
|
||||||
|
|
||||||
handle_call(argv, _From, Config) ->
|
|
||||||
{reply, Config#elixir_code_server.argv, Config};
|
|
||||||
|
|
||||||
handle_call(compiler_options, _From, Config) ->
|
|
||||||
{reply, Config#elixir_code_server.compiler_options, Config};
|
|
||||||
|
|
||||||
handle_call({compilation_status, CompilerPid}, _From, Config) ->
|
handle_call({compilation_status, CompilerPid}, _From, Config) ->
|
||||||
CompilationStatusList = Config#elixir_code_server.compilation_status,
|
CompilationStatusList = Config#elixir_code_server.compilation_status,
|
||||||
CompilationStatusListNew = orddict:erase(CompilerPid, CompilationStatusList),
|
CompilationStatusListNew = orddict:erase(CompilerPid, CompilationStatusList),
|
||||||
@@ -73,20 +65,11 @@ handle_call({compilation_status, CompilerPid}, _From, Config) ->
|
|||||||
Config#elixir_code_server{compilation_status=CompilationStatusListNew}};
|
Config#elixir_code_server{compilation_status=CompilationStatusListNew}};
|
||||||
|
|
||||||
handle_call(retrieve_module_name, _From, Config) ->
|
handle_call(retrieve_module_name, _From, Config) ->
|
||||||
case Config#elixir_code_server.pool of
|
case Config#elixir_code_server.mod_pool of
|
||||||
{[H|T], Counter} ->
|
{[H|T], Counter} ->
|
||||||
{reply, module_tuple(H), Config#elixir_code_server{pool={T,Counter}}};
|
{reply, module_tuple(H), Config#elixir_code_server{mod_pool={T,Counter}}};
|
||||||
{[], Counter} ->
|
{[], Counter} ->
|
||||||
{reply, module_tuple(Counter), Config#elixir_code_server{pool={[],Counter+1}}}
|
{reply, module_tuple(Counter), Config#elixir_code_server{mod_pool={[],Counter+1}}}
|
||||||
end;
|
|
||||||
|
|
||||||
handle_call(erl_compiler_options, _From, Config) ->
|
|
||||||
case Config#elixir_code_server.erl_compiler_options of
|
|
||||||
nil ->
|
|
||||||
Opts = erl_compiler_options(),
|
|
||||||
{reply, Opts, Config#elixir_code_server{erl_compiler_options=Opts}};
|
|
||||||
Opts ->
|
|
||||||
{reply, Opts, Config}
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call(paths, _From, Config) ->
|
handle_call(paths, _From, Config) ->
|
||||||
@@ -95,20 +78,11 @@ handle_call(paths, _From, Config) ->
|
|||||||
handle_call(Request, _From, Config) ->
|
handle_call(Request, _From, Config) ->
|
||||||
{stop, {badcall, Request}, Config}.
|
{stop, {badcall, Request}, Config}.
|
||||||
|
|
||||||
handle_cast({at_exit, AtExit}, Config) ->
|
|
||||||
{noreply, Config#elixir_code_server{at_exit=[AtExit|Config#elixir_code_server.at_exit]}};
|
|
||||||
|
|
||||||
handle_cast({argv, Argv}, Config) ->
|
|
||||||
{noreply, Config#elixir_code_server{argv=Argv}};
|
|
||||||
|
|
||||||
handle_cast({compiler_options, Options}, Config) ->
|
|
||||||
Final = orddict:merge(fun(_,_,V) -> V end, Config#elixir_code_server.compiler_options, Options),
|
|
||||||
{noreply, Config#elixir_code_server{compiler_options=Final}};
|
|
||||||
|
|
||||||
handle_cast({register_warning, CompilerPid}, Config) ->
|
handle_cast({register_warning, CompilerPid}, Config) ->
|
||||||
CompilationStatusCurrent = Config#elixir_code_server.compilation_status,
|
CompilationStatusCurrent = Config#elixir_code_server.compilation_status,
|
||||||
CompilationStatusNew = orddict:store(CompilerPid, error, CompilationStatusCurrent),
|
CompilationStatusNew = orddict:store(CompilerPid, error, CompilationStatusCurrent),
|
||||||
case orddict:find(warnings_as_errors, Config#elixir_code_server.compiler_options) of
|
CompilerOptions = elixir_config:get(compiler_options),
|
||||||
|
case orddict:find(warnings_as_errors, CompilerOptions) of
|
||||||
{ok, true} -> {noreply, Config#elixir_code_server{compilation_status=CompilationStatusNew}};
|
{ok, true} -> {noreply, Config#elixir_code_server{compilation_status=CompilationStatusNew}};
|
||||||
_ -> {noreply, Config}
|
_ -> {noreply, Config}
|
||||||
end;
|
end;
|
||||||
@@ -137,8 +111,8 @@ handle_cast({unload_files, Files}, Config) ->
|
|||||||
Unloaded = lists:foldl(fun(File, Acc) -> orddict:erase(File, Acc) end, Current, Files),
|
Unloaded = lists:foldl(fun(File, Acc) -> orddict:erase(File, Acc) end, Current, Files),
|
||||||
{noreply, Config#elixir_code_server{loaded=Unloaded}};
|
{noreply, Config#elixir_code_server{loaded=Unloaded}};
|
||||||
|
|
||||||
handle_cast({return_module_name, H}, #elixir_code_server{pool={T,Counter}} = Config) ->
|
handle_cast({return_module_name, H}, #elixir_code_server{mod_pool={T,Counter}} = Config) ->
|
||||||
{noreply, Config#elixir_code_server{pool={[H|T],Counter}}};
|
{noreply, Config#elixir_code_server{mod_pool={[H|T],Counter}}};
|
||||||
|
|
||||||
handle_cast({paths, PA, PZ}, #elixir_code_server{} = Config) ->
|
handle_cast({paths, PA, PZ}, #elixir_code_server{} = Config) ->
|
||||||
{noreply, Config#elixir_code_server{paths={PA,PZ}}};
|
{noreply, Config#elixir_code_server{paths={PA,PZ}}};
|
||||||
@@ -146,7 +120,10 @@ handle_cast({paths, PA, PZ}, #elixir_code_server{} = Config) ->
|
|||||||
handle_cast(Request, Config) ->
|
handle_cast(Request, Config) ->
|
||||||
{stop, {badcast, Request}, Config}.
|
{stop, {badcast, Request}, Config}.
|
||||||
|
|
||||||
handle_info(_Request, Config) ->
|
handle_info({'DOWN', Ref, process, _Pid, _Reason}, Config) ->
|
||||||
|
{noreply, undefmodule(Ref, Config)};
|
||||||
|
|
||||||
|
handle_info(_Msg, Config) ->
|
||||||
{noreply, Config}.
|
{noreply, Config}.
|
||||||
|
|
||||||
terminate(_Reason, _Config) ->
|
terminate(_Reason, _Config) ->
|
||||||
@@ -158,22 +135,17 @@ code_change(_Old, Config, _Extra) ->
|
|||||||
module_tuple(I) ->
|
module_tuple(I) ->
|
||||||
{list_to_atom("elixir_compiler_" ++ integer_to_list(I)), I}.
|
{list_to_atom("elixir_compiler_" ++ integer_to_list(I)), I}.
|
||||||
|
|
||||||
erl_compiler_options() ->
|
defmodule(Pid, Tuple, #elixir_code_server{mod_ets=ModEts} = Config) ->
|
||||||
Key = "ERL_COMPILER_OPTIONS",
|
ets:insert(elixir_modules, Tuple),
|
||||||
case os:getenv(Key) of
|
Ref = erlang:monitor(process, Pid),
|
||||||
false -> [];
|
Mod = erlang:element(1, Tuple),
|
||||||
Str when is_list(Str) ->
|
{Ref, Config#elixir_code_server{mod_ets=dict:store(Ref, Mod, ModEts)}}.
|
||||||
case erl_scan:string(Str) of
|
|
||||||
{ok,Tokens,_} ->
|
undefmodule(Ref, #elixir_code_server{mod_ets=ModEts} = Config) ->
|
||||||
case erl_parse:parse_term(Tokens ++ [{dot, 1}]) of
|
case dict:find(Ref, ModEts) of
|
||||||
{ok,List} when is_list(List) -> List;
|
{ok, Mod} ->
|
||||||
{ok,Term} -> [Term];
|
ets:delete(elixir_modules, Mod),
|
||||||
{error,_Reason} ->
|
Config#elixir_code_server{mod_ets=dict:erase(Ref, ModEts)};
|
||||||
io:format("Ignoring bad term in ~ts\n", [Key]),
|
error ->
|
||||||
[]
|
Config
|
||||||
end;
|
|
||||||
{error, {_,_,_Reason}, _} ->
|
|
||||||
io:format("Ignoring bad term in ~ts\n", [Key]),
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
%% Public API
|
%% Public API
|
||||||
|
|
||||||
get_opt(Key) ->
|
get_opt(Key) ->
|
||||||
Dict = elixir_code_server:call(compiler_options),
|
Dict = elixir_config:get(compiler_options),
|
||||||
case lists:keyfind(Key, 1, Dict) of
|
case lists:keyfind(Key, 1, Dict) of
|
||||||
false -> false;
|
false -> false;
|
||||||
{Key, Value} -> Value
|
{Key, Value} -> Value
|
||||||
@@ -78,7 +78,7 @@ code_loading_compilation(Forms, Vars, #{line := Line} = E) ->
|
|||||||
|
|
||||||
%% Pass {native, false} to speed up bootstrap
|
%% Pass {native, false} to speed up bootstrap
|
||||||
%% process when native is set to true
|
%% process when native is set to true
|
||||||
AllOpts = elixir_code_server:call(erl_compiler_options),
|
AllOpts = options(),
|
||||||
FinalOpts = AllOpts -- [native, warn_missing_spec],
|
FinalOpts = AllOpts -- [native, warn_missing_spec],
|
||||||
inner_module(Form, FinalOpts, true, E, fun(_, Binary) ->
|
inner_module(Form, FinalOpts, true, E, fun(_, Binary) ->
|
||||||
%% If we have labeled locals, anonymous functions
|
%% If we have labeled locals, anonymous functions
|
||||||
@@ -92,6 +92,36 @@ code_loading_compilation(Forms, Vars, #{line := Line} = E) ->
|
|||||||
dispatch_loaded(Module, Fun, Args, Purgeable, I, EE)
|
dispatch_loaded(Module, Fun, Args, Purgeable, I, EE)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
options() ->
|
||||||
|
case elixir_config:get(erl_compiler_options) of
|
||||||
|
nil ->
|
||||||
|
elixir_config:update(erl_compiler_options, fun options/1);
|
||||||
|
Opts ->
|
||||||
|
Opts
|
||||||
|
end.
|
||||||
|
|
||||||
|
options(nil) ->
|
||||||
|
Key = "ERL_COMPILER_OPTIONS",
|
||||||
|
case os:getenv(Key) of
|
||||||
|
false -> [];
|
||||||
|
Str when is_list(Str) ->
|
||||||
|
case erl_scan:string(Str) of
|
||||||
|
{ok,Tokens,_} ->
|
||||||
|
case erl_parse:parse_term(Tokens ++ [{dot, 1}]) of
|
||||||
|
{ok,List} when is_list(List) -> List;
|
||||||
|
{ok,Term} -> [Term];
|
||||||
|
{error,_Reason} ->
|
||||||
|
io:format("Ignoring bad term in ~ts\n", [Key]),
|
||||||
|
[]
|
||||||
|
end;
|
||||||
|
{error, {_,_,_Reason}, _} ->
|
||||||
|
io:format("Ignoring bad term in ~ts\n", [Key]),
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
options(Opts) ->
|
||||||
|
Opts.
|
||||||
|
|
||||||
dispatch_loaded(Module, Fun, Args, Purgeable, I, E) ->
|
dispatch_loaded(Module, Fun, Args, Purgeable, I, E) ->
|
||||||
Res = Module:Fun(Args),
|
Res = Module:Fun(Args),
|
||||||
code:delete(Module),
|
code:delete(Module),
|
||||||
@@ -142,8 +172,8 @@ module(Forms, Opts, E, Callback) ->
|
|||||||
Final =
|
Final =
|
||||||
case (get_opt(debug_info) == true) orelse
|
case (get_opt(debug_info) == true) orelse
|
||||||
lists:member(debug_info, Opts) of
|
lists:member(debug_info, Opts) of
|
||||||
true -> [debug_info] ++ elixir_code_server:call(erl_compiler_options);
|
true -> [debug_info] ++ options();
|
||||||
false -> elixir_code_server:call(erl_compiler_options)
|
false -> options()
|
||||||
end,
|
end,
|
||||||
inner_module(Forms, Final, false, E, Callback).
|
inner_module(Forms, Final, false, E, Callback).
|
||||||
|
|
||||||
@@ -177,7 +207,10 @@ no_auto_import() ->
|
|||||||
|
|
||||||
core() ->
|
core() ->
|
||||||
{ok, _} = application:ensure_all_started(elixir),
|
{ok, _} = application:ensure_all_started(elixir),
|
||||||
elixir_code_server:cast({compiler_options, [{docs,false},{internal,true}]}),
|
New = orddict:from_list([{docs,false},{internal,true}]),
|
||||||
|
Merge = fun(_, _, Value) -> Value end,
|
||||||
|
Update = fun(Old) -> orddict:merge(Merge, Old, New) end,
|
||||||
|
_ = elixir_config:update(compiler_options, Update),
|
||||||
[core_file(File) || File <- core_main()].
|
[core_file(File) || File <- core_main()].
|
||||||
|
|
||||||
core_file(File) ->
|
core_file(File) ->
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
-module(elixir_config).
|
||||||
|
-compile({no_auto_import, [get/1]}).
|
||||||
|
-export([new/1, delete/1, put/2, get/1, update/2, get_and_put/2]).
|
||||||
|
-export([start_link/0, init/1, handle_call/3, handle_cast/2,
|
||||||
|
handle_info/2, code_change/3, terminate/2]).
|
||||||
|
-behaviour(gen_server).
|
||||||
|
|
||||||
|
%% public api
|
||||||
|
|
||||||
|
new(Opts) ->
|
||||||
|
Tab = ets:new(?MODULE, [named_table, public, {read_concurrency, true}]),
|
||||||
|
true = ets:insert_new(?MODULE, Opts),
|
||||||
|
Tab.
|
||||||
|
|
||||||
|
delete(?MODULE) ->
|
||||||
|
ets:delete(?MODULE).
|
||||||
|
|
||||||
|
put(Key, Value) ->
|
||||||
|
gen_server:call(?MODULE, {put, Key, Value}).
|
||||||
|
|
||||||
|
get(Key) ->
|
||||||
|
case ets:lookup(?MODULE, Key) of
|
||||||
|
[{_, Value}] -> Value;
|
||||||
|
[] -> nil
|
||||||
|
end.
|
||||||
|
|
||||||
|
update(Key, Fun) ->
|
||||||
|
gen_server:call(?MODULE, {update, Key, Fun}).
|
||||||
|
|
||||||
|
get_and_put(Key, Value) ->
|
||||||
|
gen_server:call(?MODULE, {get_and_put, Key, Value}).
|
||||||
|
|
||||||
|
start_link() ->
|
||||||
|
gen_server:start_link({local, ?MODULE}, ?MODULE, ?MODULE, []).
|
||||||
|
|
||||||
|
%% gen_server api
|
||||||
|
|
||||||
|
init(Tab) ->
|
||||||
|
%% Ets table must be writable
|
||||||
|
public = ets:info(Tab, protection),
|
||||||
|
{ok, Tab}.
|
||||||
|
|
||||||
|
handle_call({put, Key, Value}, _From, Tab) ->
|
||||||
|
ets:insert(Tab, {Key, Value}),
|
||||||
|
{reply, ok, Tab};
|
||||||
|
handle_call({update, Key, Fun}, _From, Tab) ->
|
||||||
|
Value = Fun(get(Key)),
|
||||||
|
ets:insert(Tab, {Key, Value}),
|
||||||
|
{reply, Value, Tab};
|
||||||
|
handle_call({get_and_put, Key, Value}, _From, Tab) ->
|
||||||
|
OldValue = get(Key),
|
||||||
|
ets:insert(Tab, {Key, Value}),
|
||||||
|
{reply, OldValue, Tab}.
|
||||||
|
|
||||||
|
handle_cast(Cast, Tab) ->
|
||||||
|
{stop, {bad_cast, Cast}, Tab}.
|
||||||
|
|
||||||
|
handle_info(_Msg, Tab) ->
|
||||||
|
{noreply, Tab}.
|
||||||
|
|
||||||
|
code_change(_OldVsn, Tab, _Extra) ->
|
||||||
|
{ok, Tab}.
|
||||||
|
|
||||||
|
terminate(_Reason, _Tab) ->
|
||||||
|
ok.
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
handle_info/2, terminate/2, code_change/3, next/0]).
|
handle_info/2, terminate/2, code_change/3, next/0]).
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
-define(timeout, 1000). %% 1 second
|
-define(timeout, 30000). %% 30 seconds
|
||||||
-define(limit, 4294967296). %% 2^32
|
-define(limit, 4294967295). %% 2^32 - 1
|
||||||
|
|
||||||
next() ->
|
next() ->
|
||||||
gen_server:call(?MODULE, next, ?timeout).
|
gen_server:call(?MODULE, next, ?timeout).
|
||||||
|
|||||||
@@ -1,49 +1,35 @@
|
|||||||
% Holds the logic responsible for function definitions (def(p) and defmacro(p)).
|
% Holds the logic responsible for function definitions (def(p) and defmacro(p)).
|
||||||
-module(elixir_def).
|
-module(elixir_def).
|
||||||
-export([table/1, clauses_table/1, setup/1,
|
-export([setup/1, reset_last/1, lookup_definition/2,
|
||||||
cleanup/1, reset_last/1, lookup_definition/2,
|
|
||||||
delete_definition/2, store_definition/6, unwrap_definitions/2,
|
delete_definition/2, store_definition/6, unwrap_definitions/2,
|
||||||
store_each/8, format_error/1]).
|
store_each/7, format_error/1]).
|
||||||
-include("elixir.hrl").
|
-include("elixir.hrl").
|
||||||
|
|
||||||
-define(attr, '__def_table').
|
-define(last_def, {elixir, last_def}).
|
||||||
-define(clauses_attr, '__clauses_table').
|
-define(attr, {elixir, def_table}).
|
||||||
|
-define(clauses_attr, {elixir, clauses_table}).
|
||||||
|
|
||||||
%% Table management functions. Called internally.
|
%% Table management functions. Called internally.
|
||||||
|
|
||||||
table(Module) ->
|
|
||||||
ets:lookup_element(Module, ?attr, 2).
|
|
||||||
|
|
||||||
clauses_table(Module) ->
|
|
||||||
ets:lookup_element(Module, ?clauses_attr, 2).
|
|
||||||
|
|
||||||
setup(Module) ->
|
setup(Module) ->
|
||||||
ets:insert(Module, {?attr, ets:new(Module, [set, public])}),
|
|
||||||
ets:insert(Module, {?clauses_attr, ets:new(Module, [bag, public])}),
|
|
||||||
reset_last(Module),
|
reset_last(Module),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
cleanup(Module) ->
|
|
||||||
ets:delete(table(Module)),
|
|
||||||
ets:delete(clauses_table(Module)).
|
|
||||||
|
|
||||||
%% Reset the last item. Useful when evaling code.
|
|
||||||
reset_last(Module) ->
|
reset_last(Module) ->
|
||||||
ets:insert(table(Module), {last, []}).
|
ets:insert(elixir_module:data_table(Module), {?last_def, []}).
|
||||||
|
|
||||||
%% Looks up a definition from the database.
|
|
||||||
lookup_definition(Module, Tuple) ->
|
lookup_definition(Module, Tuple) ->
|
||||||
case ets:lookup(table(Module), Tuple) of
|
case ets:lookup(elixir_module:defs_table(Module), Tuple) of
|
||||||
[Result] ->
|
[Result] ->
|
||||||
CTable = clauses_table(Module),
|
CTable = elixir_module:clas_table(Module),
|
||||||
{Result, [Clause || {_, Clause} <- ets:lookup(CTable, Tuple)]};
|
{Result, [Clause || {_, Clause} <- ets:lookup(CTable, Tuple)]};
|
||||||
_ ->
|
_ ->
|
||||||
false
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete_definition(Module, Tuple) ->
|
delete_definition(Module, Tuple) ->
|
||||||
ets:delete(table(Module), Tuple),
|
ets:delete(elixir_module:defs_table(Module), Tuple),
|
||||||
ets:delete(clauses_table(Module), Tuple).
|
ets:delete(elixir_module:clas_table(Module), Tuple).
|
||||||
|
|
||||||
% Invoked by the wrap definition with the function abstract tree.
|
% Invoked by the wrap definition with the function abstract tree.
|
||||||
% Each function is then added to the function table.
|
% Each function is then added to the function table.
|
||||||
@@ -98,16 +84,12 @@ store_definition(Line, Kind, CheckClauses, Name, Args, Guards, Body, KeepLocatio
|
|||||||
DefaultsLength = length(Defaults),
|
DefaultsLength = length(Defaults),
|
||||||
elixir_locals:record_defaults(Tuple, Kind, Module, DefaultsLength),
|
elixir_locals:record_defaults(Tuple, Kind, Module, DefaultsLength),
|
||||||
|
|
||||||
File = ?m(E, file),
|
File = ?m(E, file),
|
||||||
Table = table(Module),
|
|
||||||
CTable = clauses_table(Module),
|
|
||||||
|
|
||||||
compile_super(Module, Super, E),
|
compile_super(Module, Super, E),
|
||||||
check_previous_defaults(Table, Line, Name, Arity, Kind, DefaultsLength, E),
|
check_previous_defaults(Line, Module, Name, Arity, Kind, DefaultsLength, E),
|
||||||
|
|
||||||
store_each(CheckClauses, Kind, File, Location,
|
store_each(CheckClauses, Kind, File, Location, Module, DefaultsLength, Function),
|
||||||
Table, CTable, DefaultsLength, Function),
|
[store_each(false, Kind, File, Location, Module, 0,
|
||||||
[store_each(false, Kind, File, Location, Table, CTable, 0,
|
|
||||||
default_function_for(Kind, Name, Default)) || Default <- Defaults],
|
default_function_for(Kind, Name, Default)) || Default <- Defaults],
|
||||||
|
|
||||||
make_struct_available(Kind, Module, Name, Args),
|
make_struct_available(Kind, Module, Name, Args),
|
||||||
@@ -166,7 +148,7 @@ normalize_location(File) ->
|
|||||||
%% Compile super
|
%% Compile super
|
||||||
|
|
||||||
compile_super(Module, true, #{function := Function}) ->
|
compile_super(Module, true, #{function := Function}) ->
|
||||||
elixir_def_overridable:store(Module, Function, true);
|
elixir_def_overridable:super(Module, Function);
|
||||||
compile_super(_Module, _, _E) -> ok.
|
compile_super(_Module, _, _E) -> ok.
|
||||||
|
|
||||||
%% Translate the given call and expression given
|
%% Translate the given call and expression given
|
||||||
@@ -229,9 +211,8 @@ is_macro(_) -> false.
|
|||||||
% It returns a list of all functions to be exported, plus the macros,
|
% It returns a list of all functions to be exported, plus the macros,
|
||||||
% and the body of all functions.
|
% and the body of all functions.
|
||||||
unwrap_definitions(File, Module) ->
|
unwrap_definitions(File, Module) ->
|
||||||
Table = table(Module),
|
Table = elixir_module:defs_table(Module),
|
||||||
CTable = clauses_table(Module),
|
CTable = elixir_module:clas_table(Module),
|
||||||
ets:delete(Table, last),
|
|
||||||
|
|
||||||
{All, Private} = unwrap_definition(ets:tab2list(Table), File, Module, CTable, [], []),
|
{All, Private} = unwrap_definition(ets:tab2list(Table), File, Module, CTable, [], []),
|
||||||
Unreachable = elixir_locals:warn_unused_local(File, Module, Private),
|
Unreachable = elixir_locals:warn_unused_local(File, Module, Private),
|
||||||
@@ -327,26 +308,33 @@ warn_bodyless_function(Line, File, _Module, Kind, Tuple) ->
|
|||||||
%% This function also checks and emit warnings in case
|
%% This function also checks and emit warnings in case
|
||||||
%% the kind, of the visibility of the function changes.
|
%% the kind, of the visibility of the function changes.
|
||||||
|
|
||||||
store_each(Check, Kind, File, Location, Table, CTable, Defaults, {function, Line, Name, Arity, Clauses}) ->
|
store_each(Check, Kind, File, Location, Module, Defaults, {function, Line, Name, Arity, Clauses}) ->
|
||||||
Tuple = {Name, Arity},
|
Data = elixir_module:data_table(Module),
|
||||||
|
Defs = elixir_module:defs_table(Module),
|
||||||
|
Clas = elixir_module:clas_table(Module),
|
||||||
|
|
||||||
|
Tuple = {Name, Arity},
|
||||||
HasBody = Clauses =/= [],
|
HasBody = Clauses =/= [],
|
||||||
case ets:lookup(Table, Tuple) of
|
|
||||||
[{Tuple, StoredKind, StoredLine, StoredFile, StoredCheck, StoredLocation, {StoredDefaults, LastHasBody, LastDefaults}}] ->
|
case ets:lookup(Defs, Tuple) of
|
||||||
|
[{Tuple, StoredKind, StoredLine, StoredFile, StoredCheck,
|
||||||
|
StoredLocation, {StoredDefaults, LastHasBody, LastDefaults}}] ->
|
||||||
FinalLine = StoredLine,
|
FinalLine = StoredLine,
|
||||||
FinalLocation = StoredLocation,
|
FinalLocation = StoredLocation,
|
||||||
FinalDefaults = {max(Defaults, StoredDefaults), HasBody, Defaults},
|
FinalDefaults = {max(Defaults, StoredDefaults), HasBody, Defaults},
|
||||||
check_valid_kind(Line, File, Name, Arity, Kind, StoredKind),
|
check_valid_kind(Line, File, Name, Arity, Kind, StoredKind),
|
||||||
(Check and StoredCheck) andalso
|
(Check and StoredCheck) andalso
|
||||||
check_valid_clause(Line, File, Name, Arity, Kind, Table, StoredLine, StoredFile),
|
check_valid_clause(Line, File, Name, Arity, Kind, Data, StoredLine, StoredFile),
|
||||||
check_valid_defaults(Line, File, Name, Arity, Kind, Defaults, LastDefaults, LastHasBody);
|
check_valid_defaults(Line, File, Name, Arity, Kind, Defaults, LastDefaults, LastHasBody);
|
||||||
[] ->
|
[] ->
|
||||||
FinalLine = Line,
|
FinalLine = Line,
|
||||||
FinalLocation = Location,
|
FinalLocation = Location,
|
||||||
FinalDefaults = {Defaults, HasBody, Defaults}
|
FinalDefaults = {Defaults, HasBody, Defaults}
|
||||||
end,
|
end,
|
||||||
Check andalso ets:insert(Table, {last, {Name, Arity}}),
|
|
||||||
ets:insert(CTable, [{Tuple, Clause} || Clause <- Clauses ]),
|
Check andalso ets:insert(Data, {?last_def, {Name, Arity}}),
|
||||||
ets:insert(Table, {Tuple, Kind, FinalLine, File, Check, FinalLocation, FinalDefaults}).
|
ets:insert(Clas, [{Tuple, Clause} || Clause <- Clauses]),
|
||||||
|
ets:insert(Defs, {Tuple, Kind, FinalLine, File, Check, FinalLocation, FinalDefaults}).
|
||||||
|
|
||||||
%% Validations
|
%% Validations
|
||||||
|
|
||||||
@@ -355,8 +343,8 @@ check_valid_kind(Line, File, Name, Arity, Kind, StoredKind) ->
|
|||||||
elixir_errors:form_error([{line, Line}], File, ?MODULE,
|
elixir_errors:form_error([{line, Line}], File, ?MODULE,
|
||||||
{changed_kind, {Name, Arity, StoredKind, Kind}}).
|
{changed_kind, {Name, Arity, StoredKind, Kind}}).
|
||||||
|
|
||||||
check_valid_clause(Line, File, Name, Arity, Kind, Table, StoredLine, StoredFile) ->
|
check_valid_clause(Line, File, Name, Arity, Kind, Data, StoredLine, StoredFile) ->
|
||||||
case ets:lookup_element(Table, last, 2) of
|
case ets:lookup_element(Data, ?last_def, 2) of
|
||||||
{Name,Arity} -> [];
|
{Name,Arity} -> [];
|
||||||
[] -> [];
|
[] -> [];
|
||||||
_ ->
|
_ ->
|
||||||
@@ -379,8 +367,8 @@ check_valid_defaults(Line, File, Name, Arity, Kind, _, _, _) ->
|
|||||||
elixir_errors:form_error([{line, Line}], File, ?MODULE,
|
elixir_errors:form_error([{line, Line}], File, ?MODULE,
|
||||||
{clauses_with_defaults, {Kind, Name, Arity}}).
|
{clauses_with_defaults, {Kind, Name, Arity}}).
|
||||||
|
|
||||||
check_previous_defaults(Table, Line, Name, Arity, Kind, Defaults, E) ->
|
check_previous_defaults(Line, Module, Name, Arity, Kind, Defaults, E) ->
|
||||||
Matches = ets:match(Table, {{Name, '$2'}, '$1', '_', '_', '_', '_', {'$3', '_', '_'}}),
|
Matches = ets:match(elixir_module:defs_table(Module), {{Name, '$2'}, '$1', '_', '_', '_', '_', {'$3', '_', '_'}}),
|
||||||
[ begin
|
[ begin
|
||||||
elixir_errors:form_error([{line, Line}], ?m(E, file), ?MODULE,
|
elixir_errors:form_error([{line, Line}], ?m(E, file), ?MODULE,
|
||||||
{defs_with_defaults, Name, {Kind, Arity}, {K, A}})
|
{defs_with_defaults, Name, {Kind, Arity}, {K, A}})
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
% Holds the logic responsible for defining overridable functions and handling super.
|
% Holds the logic responsible for defining overridable functions and handling super.
|
||||||
-module(elixir_def_overridable).
|
-module(elixir_def_overridable).
|
||||||
-export([store_pending/1, ensure_defined/4,
|
-export([setup/1, overridable/1, overridable/2, name/2, super/2, store_pending/1,
|
||||||
name/2, store/3, format_error/1]).
|
ensure_defined/4, format_error/1]).
|
||||||
-include("elixir.hrl").
|
-include("elixir.hrl").
|
||||||
|
-define(attr, {elixir, overridable}).
|
||||||
|
|
||||||
|
setup(Module) ->
|
||||||
|
overridable(Module, []).
|
||||||
|
|
||||||
overridable(Module) ->
|
overridable(Module) ->
|
||||||
ets:lookup_element(elixir_module:data_table(Module), '__overridable', 2).
|
ets:lookup_element(elixir_module:data_table(Module), ?attr, 2).
|
||||||
|
|
||||||
overridable(Module, Value) ->
|
overridable(Module, Value) ->
|
||||||
ets:insert(elixir_module:data_table(Module), {'__overridable', Value}).
|
ets:insert(elixir_module:data_table(Module), {?attr, Value}).
|
||||||
|
|
||||||
%% Check if an overridable function is defined.
|
%% Check if an overridable function is defined.
|
||||||
|
|
||||||
@@ -43,19 +47,19 @@ store(Module, Function, GenerateName) ->
|
|||||||
false -> {Kind, Name}
|
false -> {Kind, Name}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
case code:is_loaded('Elixir.Module.LocalsTracker') of
|
case elixir_compiler:get_opt(internal) of
|
||||||
{_, _} ->
|
false ->
|
||||||
'Elixir.Module.LocalsTracker':reattach(Module, Kind, {Name, Arity}, Neighbours);
|
'Elixir.Module.LocalsTracker':reattach(Module, Kind, {Name, Arity}, Neighbours);
|
||||||
_ ->
|
true ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Def = {function, Line, FinalName, Arity, Clauses},
|
Def = {function, Line, FinalName, Arity, Clauses},
|
||||||
elixir_def:store_each(false, FinalKind, File, Location,
|
elixir_def:store_each(false, FinalKind, File, Location, Module, Defaults, Def)
|
||||||
elixir_def:table(Module), elixir_def:clauses_table(Module), Defaults, Def)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Store pending declarations that were not manually made concrete.
|
super(Module, Function) ->
|
||||||
|
store(Module, Function, true).
|
||||||
|
|
||||||
store_pending(Module) ->
|
store_pending(Module) ->
|
||||||
_ = [store(Module, X, false) || {X, {_, _, _, false}} <- overridable(Module),
|
_ = [store(Module, X, false) || {X, {_, _, _, false}} <- overridable(Module),
|
||||||
@@ -73,4 +77,4 @@ format_error({no_super, Module, {Name, Arity}}) ->
|
|||||||
format_fa({Name, Arity}) ->
|
format_fa({Name, Arity}) ->
|
||||||
A = atom_to_binary(Name, utf8),
|
A = atom_to_binary(Name, utf8),
|
||||||
B = integer_to_binary(Arity),
|
B = integer_to_binary(Arity),
|
||||||
<< A/binary, $/, B/binary >>.
|
<<A/binary, $/, B/binary>>.
|
||||||
|
|||||||
@@ -9,20 +9,7 @@
|
|||||||
find_import/4, format_error/1]).
|
find_import/4, format_error/1]).
|
||||||
-include("elixir.hrl").
|
-include("elixir.hrl").
|
||||||
-import(ordsets, [is_element/2]).
|
-import(ordsets, [is_element/2]).
|
||||||
|
|
||||||
-define(atom, 'Elixir.Atom').
|
|
||||||
-define(float, 'Elixir.Float').
|
|
||||||
-define(io, 'Elixir.IO').
|
|
||||||
-define(integer, 'Elixir.Integer').
|
|
||||||
-define(kernel, 'Elixir.Kernel').
|
-define(kernel, 'Elixir.Kernel').
|
||||||
-define(list, 'Elixir.List').
|
|
||||||
-define(map, 'Elixir.Map').
|
|
||||||
-define(node, 'Elixir.Node').
|
|
||||||
-define(port, 'Elixir.Port').
|
|
||||||
-define(process, 'Elixir.Process').
|
|
||||||
-define(string, 'Elixir.String').
|
|
||||||
-define(system, 'Elixir.System').
|
|
||||||
-define(tuple, 'Elixir.Tuple').
|
|
||||||
|
|
||||||
default_functions() ->
|
default_functions() ->
|
||||||
[{?kernel, elixir_imported_functions()}].
|
[{?kernel, elixir_imported_functions()}].
|
||||||
@@ -82,7 +69,7 @@ remote_function(Meta, Receiver, Name, Arity, E) ->
|
|||||||
check_deprecation(Meta, Receiver, Name, Arity, E),
|
check_deprecation(Meta, Receiver, Name, Arity, E),
|
||||||
|
|
||||||
elixir_lexical:record_remote(Receiver, ?m(E, lexical_tracker)),
|
elixir_lexical:record_remote(Receiver, ?m(E, lexical_tracker)),
|
||||||
case inline(Receiver, Name, Arity) of
|
case elixir_rewrite:inline(Receiver, Name, Arity) of
|
||||||
{AR, AN} -> {remote, AR, AN, Arity};
|
{AR, AN} -> {remote, AR, AN, Arity};
|
||||||
false -> {remote, Receiver, Name, Arity}
|
false -> {remote, Receiver, Name, Arity}
|
||||||
end.
|
end.
|
||||||
@@ -103,9 +90,9 @@ dispatch_import(Meta, Name, Args, E, Callback) ->
|
|||||||
dispatch_require(Meta, Receiver, Name, Args, E, Callback) when is_atom(Receiver) ->
|
dispatch_require(Meta, Receiver, Name, Args, E, Callback) when is_atom(Receiver) ->
|
||||||
Arity = length(Args),
|
Arity = length(Args),
|
||||||
|
|
||||||
case rewrite(Receiver, Name, Args, Arity) of
|
case elixir_rewrite:inline(Receiver, Name, Arity) of
|
||||||
{ok, AR, AN, AA} ->
|
{AR, AN} ->
|
||||||
Callback(AR, AN, AA);
|
Callback(AR, AN, Args);
|
||||||
false ->
|
false ->
|
||||||
case expand_require(Meta, Receiver, {Name, Arity}, Args, E) of
|
case expand_require(Meta, Receiver, {Name, Arity}, Args, E) of
|
||||||
{ok, Receiver, Quoted} -> expand_quoted(Meta, Receiver, Name, Arity, Quoted, E);
|
{ok, Receiver, Quoted} -> expand_quoted(Meta, Receiver, Name, Arity, Quoted, E);
|
||||||
@@ -151,11 +138,7 @@ do_expand_import(Meta, {Name, Arity} = Tuple, Args, Module, E, Result) ->
|
|||||||
{function, Receiver} ->
|
{function, Receiver} ->
|
||||||
elixir_lexical:record_import(Receiver, ?m(E, lexical_tracker)),
|
elixir_lexical:record_import(Receiver, ?m(E, lexical_tracker)),
|
||||||
elixir_locals:record_import(Tuple, Receiver, Module, ?m(E, function)),
|
elixir_locals:record_import(Tuple, Receiver, Module, ?m(E, function)),
|
||||||
|
{ok, Receiver, Name, Args};
|
||||||
case rewrite(Receiver, Name, Args, Arity) of
|
|
||||||
{ok, _, _, _} = Res -> Res;
|
|
||||||
false -> {ok, Receiver, Name, Args}
|
|
||||||
end;
|
|
||||||
{macro, Receiver} ->
|
{macro, Receiver} ->
|
||||||
check_deprecation(Meta, Receiver, Name, Arity, E),
|
check_deprecation(Meta, Receiver, Name, Arity, E),
|
||||||
elixir_lexical:record_import(Receiver, ?m(E, lexical_tracker)),
|
elixir_lexical:record_import(Receiver, ?m(E, lexical_tracker)),
|
||||||
@@ -167,8 +150,8 @@ do_expand_import(Meta, {Name, Arity} = Tuple, Args, Module, E, Result) ->
|
|||||||
error -> {ok, Receiver, Name, Args}
|
error -> {ok, Receiver, Name, Args}
|
||||||
end;
|
end;
|
||||||
false when Module == ?kernel ->
|
false when Module == ?kernel ->
|
||||||
case rewrite(Module, Name, Args, Arity) of
|
case elixir_rewrite:inline(Module, Name, Arity) of
|
||||||
{ok, _, _, _} = Res -> Res;
|
{AR, AN} -> {ok, AR, AN, Args};
|
||||||
false -> error
|
false -> error
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
@@ -232,7 +215,7 @@ expand_quoted(Meta, Receiver, Name, Arity, Quoted, E) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
caller(Line, #{module := nil} = E) ->
|
caller(Line, #{module := nil} = E) ->
|
||||||
{elixir_compiler, '__FILE__', 2, location(Line, E)};
|
{elixir_compiler_0, '__FILE__', 1, location(Line, E)};
|
||||||
caller(Line, #{module := Module, function := nil} = E) ->
|
caller(Line, #{module := Module, function := nil} = E) ->
|
||||||
{Module, '__MODULE__', 0, location(Line, E)};
|
{Module, '__MODULE__', 0, location(Line, E)};
|
||||||
caller(Line, #{module := Module, function := {Name, Arity}} = E) ->
|
caller(Line, #{module := Module, function := {Name, Arity}} = E) ->
|
||||||
@@ -342,163 +325,6 @@ elixir_imported_macros() ->
|
|||||||
error:undef -> []
|
error:undef -> []
|
||||||
end.
|
end.
|
||||||
|
|
||||||
rewrite(?atom, to_string, [Arg], _) ->
|
|
||||||
{ok, erlang, atom_to_binary, [Arg, utf8]};
|
|
||||||
rewrite(?kernel, elem, [Tuple, Index], _) ->
|
|
||||||
{ok, erlang, element, [increment(Index), Tuple]};
|
|
||||||
rewrite(?kernel, put_elem, [Tuple, Index, Value], _) ->
|
|
||||||
{ok, erlang, setelement, [increment(Index), Tuple, Value]};
|
|
||||||
rewrite(?map, 'has_key?', [Map, Key], _) ->
|
|
||||||
{ok, maps, is_key, [Key, Map]};
|
|
||||||
rewrite(?map, fetch, [Map, Key], _) ->
|
|
||||||
{ok, maps, find, [Key, Map]};
|
|
||||||
rewrite(?map, put, [Map, Key, Value], _) ->
|
|
||||||
{ok, maps, put, [Key, Value, Map]};
|
|
||||||
rewrite(?map, delete, [Map, Key], _) ->
|
|
||||||
{ok, maps, remove, [Key, Map]};
|
|
||||||
rewrite(?process, monitor, [Arg], _) ->
|
|
||||||
{ok, erlang, monitor, [process, Arg]};
|
|
||||||
rewrite(?string, to_atom, [Arg], _) ->
|
|
||||||
{ok, erlang, binary_to_atom, [Arg, utf8]};
|
|
||||||
rewrite(?string, to_existing_atom, [Arg], _) ->
|
|
||||||
{ok, erlang, binary_to_existing_atom, [Arg, utf8]};
|
|
||||||
rewrite(?tuple, insert_at, [Tuple, Index, Term], _) ->
|
|
||||||
{ok, erlang, insert_element, [increment(Index), Tuple, Term]};
|
|
||||||
rewrite(?tuple, delete_at, [Tuple, Index], _) ->
|
|
||||||
{ok, erlang, delete_element, [increment(Index), Tuple]};
|
|
||||||
rewrite(?tuple, duplicate, [Data, Size], _) ->
|
|
||||||
{ok, erlang, make_tuple, [Size, Data]};
|
|
||||||
|
|
||||||
rewrite(Receiver, Name, Args, Arity) ->
|
|
||||||
case inline(Receiver, Name, Arity) of
|
|
||||||
{AR, AN} -> {ok, AR, AN, Args};
|
|
||||||
false -> false
|
|
||||||
end.
|
|
||||||
|
|
||||||
increment(Number) when is_number(Number) ->
|
|
||||||
Number + 1;
|
|
||||||
increment(Other) ->
|
|
||||||
{{'.', [], [erlang, '+']}, [], [Other, 1]}.
|
|
||||||
|
|
||||||
inline(?atom, to_char_list, 1) -> {erlang, atom_to_list};
|
|
||||||
inline(?io, iodata_length, 1) -> {erlang, iolist_size};
|
|
||||||
inline(?io, iodata_to_binary, 1) -> {erlang, iolist_to_binary};
|
|
||||||
inline(?integer, to_string, 1) -> {erlang, integer_to_binary};
|
|
||||||
inline(?integer, to_string, 2) -> {erlang, integer_to_binary};
|
|
||||||
inline(?integer, to_char_list, 1) -> {erlang, integer_to_list};
|
|
||||||
inline(?integer, to_char_list, 2) -> {erlang, integer_to_list};
|
|
||||||
inline(?float, to_string, 1) -> {erlang, float_to_binary};
|
|
||||||
inline(?float, to_char_list, 1) -> {erlang, float_to_list};
|
|
||||||
inline(?list, to_atom, 1) -> {erlang, list_to_atom};
|
|
||||||
inline(?list, to_existing_atom, 1) -> {erlang, list_to_existing_atom};
|
|
||||||
inline(?list, to_float, 1) -> {erlang, list_to_float};
|
|
||||||
inline(?list, to_integer, 1) -> {erlang, list_to_integer};
|
|
||||||
inline(?list, to_integer, 2) -> {erlang, list_to_integer};
|
|
||||||
inline(?list, to_tuple, 1) -> {erlang, list_to_tuple};
|
|
||||||
|
|
||||||
inline(?kernel, '+', 2) -> {erlang, '+'};
|
|
||||||
inline(?kernel, '-', 2) -> {erlang, '-'};
|
|
||||||
inline(?kernel, '+', 1) -> {erlang, '+'};
|
|
||||||
inline(?kernel, '-', 1) -> {erlang, '-'};
|
|
||||||
inline(?kernel, '*', 2) -> {erlang, '*'};
|
|
||||||
inline(?kernel, '/', 2) -> {erlang, '/'};
|
|
||||||
inline(?kernel, '++', 2) -> {erlang, '++'};
|
|
||||||
inline(?kernel, '--', 2) -> {erlang, '--'};
|
|
||||||
inline(?kernel, 'not', 1) -> {erlang, 'not'};
|
|
||||||
inline(?kernel, '<', 2) -> {erlang, '<'};
|
|
||||||
inline(?kernel, '>', 2) -> {erlang, '>'};
|
|
||||||
inline(?kernel, '<=', 2) -> {erlang, '=<'};
|
|
||||||
inline(?kernel, '>=', 2) -> {erlang, '>='};
|
|
||||||
inline(?kernel, '==', 2) -> {erlang, '=='};
|
|
||||||
inline(?kernel, '!=', 2) -> {erlang, '/='};
|
|
||||||
inline(?kernel, '===', 2) -> {erlang, '=:='};
|
|
||||||
inline(?kernel, '!==', 2) -> {erlang, '=/='};
|
|
||||||
inline(?kernel, abs, 1) -> {erlang, abs};
|
|
||||||
inline(?kernel, apply, 2) -> {erlang, apply};
|
|
||||||
inline(?kernel, apply, 3) -> {erlang, apply};
|
|
||||||
inline(?kernel, binary_part, 3) -> {erlang, binary_part};
|
|
||||||
inline(?kernel, bit_size, 1) -> {erlang, bit_size};
|
|
||||||
inline(?kernel, byte_size, 1) -> {erlang, byte_size};
|
|
||||||
inline(?kernel, 'div', 2) -> {erlang, 'div'};
|
|
||||||
inline(?kernel, exit, 1) -> {erlang, exit};
|
|
||||||
inline(?kernel, hd, 1) -> {erlang, hd};
|
|
||||||
inline(?kernel, is_atom, 1) -> {erlang, is_atom};
|
|
||||||
inline(?kernel, is_binary, 1) -> {erlang, is_binary};
|
|
||||||
inline(?kernel, is_bitstring, 1) -> {erlang, is_bitstring};
|
|
||||||
inline(?kernel, is_boolean, 1) -> {erlang, is_boolean};
|
|
||||||
inline(?kernel, is_float, 1) -> {erlang, is_float};
|
|
||||||
inline(?kernel, is_function, 1) -> {erlang, is_function};
|
|
||||||
inline(?kernel, is_function, 2) -> {erlang, is_function};
|
|
||||||
inline(?kernel, is_integer, 1) -> {erlang, is_integer};
|
|
||||||
inline(?kernel, is_list, 1) -> {erlang, is_list};
|
|
||||||
inline(?kernel, is_map, 1) -> {erlang, is_map};
|
|
||||||
inline(?kernel, is_number, 1) -> {erlang, is_number};
|
|
||||||
inline(?kernel, is_pid, 1) -> {erlang, is_pid};
|
|
||||||
inline(?kernel, is_port, 1) -> {erlang, is_port};
|
|
||||||
inline(?kernel, is_reference, 1) -> {erlang, is_reference};
|
|
||||||
inline(?kernel, is_tuple, 1) -> {erlang, is_tuple};
|
|
||||||
inline(?kernel, length, 1) -> {erlang, length};
|
|
||||||
inline(?kernel, make_ref, 0) -> {erlang, make_ref};
|
|
||||||
inline(?kernel, map_size, 1) -> {erlang, map_size};
|
|
||||||
inline(?kernel, max, 2) -> {erlang, max};
|
|
||||||
inline(?kernel, min, 2) -> {erlang, min};
|
|
||||||
inline(?kernel, node, 0) -> {erlang, node};
|
|
||||||
inline(?kernel, node, 1) -> {erlang, node};
|
|
||||||
inline(?kernel, 'rem', 2) -> {erlang, 'rem'};
|
|
||||||
inline(?kernel, round, 1) -> {erlang, round};
|
|
||||||
inline(?kernel, self, 0) -> {erlang, self};
|
|
||||||
inline(?kernel, send, 2) -> {erlang, send};
|
|
||||||
inline(?kernel, spawn, 1) -> {erlang, spawn};
|
|
||||||
inline(?kernel, spawn, 3) -> {erlang, spawn};
|
|
||||||
inline(?kernel, spawn_link, 1) -> {erlang, spawn_link};
|
|
||||||
inline(?kernel, spawn_link, 3) -> {erlang, spawn_link};
|
|
||||||
inline(?kernel, spawn_monitor, 1) -> {erlang, spawn_monitor};
|
|
||||||
inline(?kernel, spawn_monitor, 3) -> {erlang, spawn_monitor};
|
|
||||||
inline(?kernel, throw, 1) -> {erlang, throw};
|
|
||||||
inline(?kernel, tl, 1) -> {erlang, tl};
|
|
||||||
inline(?kernel, trunc, 1) -> {erlang, trunc};
|
|
||||||
inline(?kernel, tuple_size, 1) -> {erlang, tuple_size};
|
|
||||||
|
|
||||||
inline(?map, keys, 1) -> {maps, keys};
|
|
||||||
inline(?map, merge, 2) -> {maps, merge};
|
|
||||||
inline(?map, size, 1) -> {maps, size};
|
|
||||||
inline(?map, values, 1) -> {maps, values};
|
|
||||||
inline(?map, to_list, 1) -> {maps, to_list};
|
|
||||||
|
|
||||||
inline(?node, spawn, 2) -> {erlang, spawn};
|
|
||||||
inline(?node, spawn, 3) -> {erlang, spawn_opt};
|
|
||||||
inline(?node, spawn, 4) -> {erlang, spawn};
|
|
||||||
inline(?node, spawn, 5) -> {erlang, spawn_opt};
|
|
||||||
inline(?node, spawn_link, 2) -> {erlang, spawn_link};
|
|
||||||
inline(?node, spawn_link, 4) -> {erlang, spawn_link};
|
|
||||||
|
|
||||||
inline(?process, exit, 2) -> {erlang, exit};
|
|
||||||
inline(?process, spawn, 2) -> {erlang, spawn_opt};
|
|
||||||
inline(?process, spawn, 4) -> {erlang, spawn_opt};
|
|
||||||
inline(?process, demonitor, 1) -> {erlang, demonitor};
|
|
||||||
inline(?process, demonitor, 2) -> {erlang, demonitor};
|
|
||||||
inline(?process, link, 1) -> {erlang, link};
|
|
||||||
inline(?process, unlink, 1) -> {erlang, unlink};
|
|
||||||
|
|
||||||
inline(?port, open, 2) -> {erlang, open_port};
|
|
||||||
inline(?port, call, 3) -> {erlang, port_call};
|
|
||||||
inline(?port, close, 1) -> {erlang, port_close};
|
|
||||||
inline(?port, command, 2) -> {erlang, port_command};
|
|
||||||
inline(?port, command, 3) -> {erlang, port_command};
|
|
||||||
inline(?port, connect, 2) -> {erlang, port_connect};
|
|
||||||
inline(?port, control, 3) -> {erlang, port_control};
|
|
||||||
inline(?port, info, 1) -> {erlang, port_info};
|
|
||||||
inline(?port, info, 2) -> {erlang, port_info};
|
|
||||||
inline(?port, list, 0) -> {erlang, ports};
|
|
||||||
|
|
||||||
inline(?string, to_float, 1) -> {erlang, binary_to_float};
|
|
||||||
inline(?string, to_integer, 1) -> {erlang, binary_to_integer};
|
|
||||||
inline(?string, to_integer, 2) -> {erlang, binary_to_integer};
|
|
||||||
inline(?system, stacktrace, 0) -> {erlang, get_stacktrace};
|
|
||||||
inline(?tuple, to_list, 1) -> {erlang, tuple_to_list};
|
|
||||||
|
|
||||||
inline(_, _, _) -> false.
|
|
||||||
|
|
||||||
check_deprecation(Meta, Receiver, Name, Arity, #{file := File}) ->
|
check_deprecation(Meta, Receiver, Name, Arity, #{file := File}) ->
|
||||||
case deprecation(Receiver, Name, Arity) of
|
case deprecation(Receiver, Name, Arity) of
|
||||||
false -> ok;
|
false -> ok;
|
||||||
|
|||||||
@@ -55,7 +55,23 @@ parse_error(Line, File, Error, <<>>) ->
|
|||||||
parse_error(Line, File, <<"syntax error before: ">>, <<"'end'">>) ->
|
parse_error(Line, File, <<"syntax error before: ">>, <<"'end'">>) ->
|
||||||
do_raise(Line, File, 'Elixir.SyntaxError', <<"unexpected token: end">>);
|
do_raise(Line, File, 'Elixir.SyntaxError', <<"unexpected token: end">>);
|
||||||
|
|
||||||
%% Binaries are wrapped in [<<...>>], so we need to unwrap them
|
%% Produce a human-readable message for errors before a sigil
|
||||||
|
parse_error(Line, File, <<"syntax error before: ">>, <<"{sigil,", _Rest/binary>> = Full) ->
|
||||||
|
{ok, Tokens, _} = erl_scan:string(binary_to_list(Full)),
|
||||||
|
{ok, {sigil, _, Sigil, [Content], _}} = erl_parse:parse_term(Tokens ++ [{dot, 1}]),
|
||||||
|
Message = <<"syntax error before: sigil ~", Sigil," with content '", Content/binary, "'">>,
|
||||||
|
do_raise(Line, File, 'Elixir.SyntaxError', Message);
|
||||||
|
|
||||||
|
%% Aliases are wrapped in ['']
|
||||||
|
parse_error(Line, File, Error, <<"['", Token/binary>>) when is_binary(Error) ->
|
||||||
|
Rest =
|
||||||
|
case binary:split(Token, <<"'">>) of
|
||||||
|
[Part, _] -> Part;
|
||||||
|
_ -> <<>>
|
||||||
|
end,
|
||||||
|
do_raise(Line, File, 'Elixir.SyntaxError', <<Error/binary, Rest/binary>>);
|
||||||
|
|
||||||
|
%% Binaries (and interpolation) are wrapped in [<<...>>]
|
||||||
parse_error(Line, File, Error, <<"[", _/binary>> = Full) when is_binary(Error) ->
|
parse_error(Line, File, Error, <<"[", _/binary>> = Full) when is_binary(Error) ->
|
||||||
Rest =
|
Rest =
|
||||||
case binary:split(Full, <<"<<">>) of
|
case binary:split(Full, <<"<<">>) of
|
||||||
@@ -127,10 +143,13 @@ handle_file_warning(_, File, {Line,erl_lint,{undefined_behaviour,Module}}) ->
|
|||||||
handle_file_warning(_, _File, {Line,erl_lint,{unused_var,_Var}}) when Line =< 0 ->
|
handle_file_warning(_, _File, {Line,erl_lint,{unused_var,_Var}}) when Line =< 0 ->
|
||||||
ok;
|
ok;
|
||||||
|
|
||||||
%% Ignore shadowed vars as we guarantee no conflicts ourselves
|
%% Ignore shadowed and exported vars as we guarantee no conflicts ourselves
|
||||||
handle_file_warning(_, _File, {_Line,erl_lint,{shadowed_var,_Var,_Where}}) ->
|
handle_file_warning(_, _File, {_Line,erl_lint,{shadowed_var,_Var,_Where}}) ->
|
||||||
ok;
|
ok;
|
||||||
|
|
||||||
|
handle_file_warning(_, _File, {_Line,erl_lint,{exported_var,_Var,_Where}}) ->
|
||||||
|
ok;
|
||||||
|
|
||||||
%% Rewrite nomatch_guard to be more generic it can happen inside if, unless, etc
|
%% Rewrite nomatch_guard to be more generic it can happen inside if, unless, etc
|
||||||
handle_file_warning(_, File, {Line, sys_core_fold, nomatch_guard}) ->
|
handle_file_warning(_, File, {Line, sys_core_fold, nomatch_guard}) ->
|
||||||
warn(Line, File, "this check/guard will always yield the same result");
|
warn(Line, File, "this check/guard will always yield the same result");
|
||||||
@@ -140,6 +159,11 @@ handle_file_warning(_, File, {Line,erl_lint,{unused_var,Var}}) ->
|
|||||||
Message = format_error(erl_lint, {unused_var, format_var(Var)}),
|
Message = format_error(erl_lint, {unused_var, format_var(Var)}),
|
||||||
warn(Line, File, Message);
|
warn(Line, File, Message);
|
||||||
|
|
||||||
|
%% Handle literal eval failures
|
||||||
|
handle_file_warning(_, File, {Line,sys_core_fold,{eval_failure, Error}}) ->
|
||||||
|
#{'__struct__' := Struct} = 'Elixir.Exception':normalize(error, Error),
|
||||||
|
warn(Line, File, ["this expression will fail with ", elixir_aliases:inspect(Struct)]);
|
||||||
|
|
||||||
%% Default behaviour
|
%% Default behaviour
|
||||||
handle_file_warning(_, File, {Line,Module,Desc}) ->
|
handle_file_warning(_, File, {Line,Module,Desc}) ->
|
||||||
Message = format_error(Module, Desc),
|
Message = format_error(Module, Desc),
|
||||||
|
|||||||
@@ -474,7 +474,8 @@ expand_remote(Receiver, DotMeta, Right, Meta, Args, E, EL) ->
|
|||||||
true -> ok
|
true -> ok
|
||||||
end,
|
end,
|
||||||
{EArgs, EA} = expand_args(Args, E),
|
{EArgs, EA} = expand_args(Args, E),
|
||||||
{{{'.', DotMeta, [Receiver, Right]}, Meta, EArgs}, elixir_env:mergev(EL, EA)}.
|
{elixir_rewrite:rewrite(Receiver, DotMeta, Right, Meta, EArgs),
|
||||||
|
elixir_env:mergev(EL, EA)}.
|
||||||
|
|
||||||
%% Lexical helpers
|
%% Lexical helpers
|
||||||
|
|
||||||
@@ -489,8 +490,8 @@ validate_opts(Meta, Kind, Allowed, Opts, E) when is_list(Opts) ->
|
|||||||
"unsupported option ~ts given to ~s", ['Elixir.Kernel':inspect(Key), Kind])
|
"unsupported option ~ts given to ~s", ['Elixir.Kernel':inspect(Key), Kind])
|
||||||
end || {Key, _} <- Opts, not lists:member(Key, Allowed)];
|
end || {Key, _} <- Opts, not lists:member(Key, Allowed)];
|
||||||
|
|
||||||
validate_opts(Meta, Kind, _Allowed, _Opts, S) ->
|
validate_opts(Meta, Kind, _Allowed, _Opts, E) ->
|
||||||
compile_error(Meta, S#elixir_scope.file, "invalid options for ~s, expected a keyword list", [Kind]).
|
compile_error(Meta, ?m(E, file), "invalid options for ~s, expected a keyword list", [Kind]).
|
||||||
|
|
||||||
no_alias_opts(KV) when is_list(KV) ->
|
no_alias_opts(KV) when is_list(KV) ->
|
||||||
case lists:keyfind(as, 1, KV) of
|
case lists:keyfind(as, 1, KV) of
|
||||||
|
|||||||
@@ -101,7 +101,9 @@ do_receive(Meta, {Key, _}, _Acc, E) ->
|
|||||||
%% Try
|
%% Try
|
||||||
|
|
||||||
'try'(Meta, [], E) ->
|
'try'(Meta, [], E) ->
|
||||||
compile_error(Meta, ?m(E, file), "missing do keywords in try");
|
compile_error(Meta, ?m(E, file), "missing do keyword in try");
|
||||||
|
'try'(Meta, [{do, _}], E) ->
|
||||||
|
compile_error(Meta, ?m(E, file), "missing catch/rescue/after/else keyword in try");
|
||||||
'try'(Meta, KV, E) when not is_list(KV) ->
|
'try'(Meta, KV, E) when not is_list(KV) ->
|
||||||
elixir_errors:compile_error(Meta, ?m(E, file), "invalid arguments for try");
|
elixir_errors:compile_error(Meta, ?m(E, file), "invalid arguments for try");
|
||||||
'try'(Meta, KV, E) ->
|
'try'(Meta, KV, E) ->
|
||||||
|
|||||||
@@ -126,29 +126,6 @@ collect_filters([H|T], Acc) ->
|
|||||||
collect_filters([], Acc) ->
|
collect_filters([], Acc) ->
|
||||||
{Acc, []}.
|
{Acc, []}.
|
||||||
|
|
||||||
%% If all we have is one enum generator, we check if it is a list
|
|
||||||
%% for optimization otherwise fallback to the reduce generator.
|
|
||||||
build_inline(Line, [{enum, Meta, Left, Right, Filters}] = Orig, Expr, Into, Var, Acc, S) ->
|
|
||||||
case Right of
|
|
||||||
{cons, _, _, _} ->
|
|
||||||
build_comprehension(Line, Orig, Expr, Into);
|
|
||||||
{Other, _, _} when Other == tuple; Other == map ->
|
|
||||||
build_reduce(Orig, Expr, Into, Acc, S);
|
|
||||||
_ ->
|
|
||||||
Clauses = [{enum, Meta, Left, Var, Filters}],
|
|
||||||
|
|
||||||
{'case', -1, Right, [
|
|
||||||
{clause, -1,
|
|
||||||
[Var],
|
|
||||||
[[elixir_utils:erl_call(Line, erlang, is_list, [Var])]],
|
|
||||||
[build_comprehension(Line, Clauses, Expr, Into)]},
|
|
||||||
{clause, -1,
|
|
||||||
[Var],
|
|
||||||
[],
|
|
||||||
[build_reduce(Clauses, Expr, Into, Acc, S)]}
|
|
||||||
]}
|
|
||||||
end;
|
|
||||||
|
|
||||||
build_inline(Line, Clauses, Expr, Into, _Var, Acc, S) ->
|
build_inline(Line, Clauses, Expr, Into, _Var, Acc, S) ->
|
||||||
case lists:all(fun(Clause) -> element(1, Clause) == bin end, Clauses) of
|
case lists:all(fun(Clause) -> element(1, Clause) == bin end, Clauses) of
|
||||||
true -> build_comprehension(Line, Clauses, Expr, Into);
|
true -> build_comprehension(Line, Clauses, Expr, Into);
|
||||||
@@ -165,7 +142,7 @@ build_into(Line, Clauses, Expr, Into, Fun, Acc, S) ->
|
|||||||
MatchExpr = {match, Line,
|
MatchExpr = {match, Line,
|
||||||
{tuple, Line, [Acc, Fun]},
|
{tuple, Line, [Acc, Fun]},
|
||||||
elixir_utils:erl_call(Line, 'Elixir.Collectable', into, [Into])
|
elixir_utils:erl_call(Line, 'Elixir.Collectable', into, [Into])
|
||||||
},
|
},
|
||||||
|
|
||||||
TryExpr =
|
TryExpr =
|
||||||
{'try', Line,
|
{'try', Line,
|
||||||
@@ -199,10 +176,8 @@ build_reduce(Clauses, Expr, {bin, _, _} = Into, Acc, S) ->
|
|||||||
|
|
||||||
build_reduce_clause([{enum, Meta, Left, Right, Filters}|T], Expr, Arg, Acc, S) ->
|
build_reduce_clause([{enum, Meta, Left, Right, Filters}|T], Expr, Arg, Acc, S) ->
|
||||||
Line = ?line(Meta),
|
Line = ?line(Meta),
|
||||||
Inner = build_reduce_clause(T, Expr, Acc, Acc, S),
|
True = build_reduce_clause(T, Expr, Acc, Acc, S),
|
||||||
|
False = Acc,
|
||||||
True = pair(Line, cont, Inner),
|
|
||||||
False = pair(Line, cont, Acc),
|
|
||||||
|
|
||||||
Clauses0 =
|
Clauses0 =
|
||||||
case is_var(Left) of
|
case is_var(Left) of
|
||||||
@@ -218,11 +193,8 @@ build_reduce_clause([{enum, Meta, Left, Right, Filters}|T], Expr, Arg, Acc, S) -
|
|||||||
[Left, Acc], [],
|
[Left, Acc], [],
|
||||||
[join_filters(Line, Filters, True, False)]}|Clauses0],
|
[join_filters(Line, Filters, True, False)]}|Clauses0],
|
||||||
|
|
||||||
Args = [Right, pair(Line, cont, Arg), {'fun', Line, {clauses, Clauses1}}],
|
Args = [Right, Arg, {'fun', Line, {clauses, Clauses1}}],
|
||||||
Tuple = elixir_utils:erl_call(Line, 'Elixir.Enumerable', reduce, Args),
|
elixir_utils:erl_call(Line, 'Elixir.Enum', reduce, Args);
|
||||||
|
|
||||||
%% Use -1 because in case of no returns we don't care about the result
|
|
||||||
elixir_utils:erl_call(-1, erlang, element, [{integer, Line, 2}, Tuple]);
|
|
||||||
|
|
||||||
build_reduce_clause([{bin, Meta, Left, Right, Filters}|T], Expr, Arg, Acc, S) ->
|
build_reduce_clause([{bin, Meta, Left, Right, Filters}|T], Expr, Arg, Acc, S) ->
|
||||||
Line = ?line(Meta),
|
Line = ?line(Meta),
|
||||||
|
|||||||
@@ -10,17 +10,19 @@
|
|||||||
-define(tracker, 'Elixir.Kernel.LexicalTracker').
|
-define(tracker, 'Elixir.Kernel.LexicalTracker').
|
||||||
|
|
||||||
run(File, Dest, Callback) ->
|
run(File, Dest, Callback) ->
|
||||||
case code:is_loaded(?tracker) of
|
case elixir_compiler:get_opt(internal) of
|
||||||
{file, _} ->
|
|
||||||
{ok, Pid} = ?tracker:start_link(Dest),
|
|
||||||
try
|
|
||||||
Callback(Pid)
|
|
||||||
after
|
|
||||||
warn_unused_aliases(File, Pid),
|
|
||||||
warn_unused_imports(File, Pid),
|
|
||||||
unlink(Pid), ?tracker:stop(Pid)
|
|
||||||
end;
|
|
||||||
false ->
|
false ->
|
||||||
|
{ok, Pid} = ?tracker:start_link(Dest),
|
||||||
|
try Callback(Pid) of
|
||||||
|
Res ->
|
||||||
|
warn_unused_aliases(File, Pid),
|
||||||
|
warn_unused_imports(File, Pid),
|
||||||
|
Res
|
||||||
|
after
|
||||||
|
unlink(Pid),
|
||||||
|
?tracker:stop(Pid)
|
||||||
|
end;
|
||||||
|
true ->
|
||||||
Callback(nil)
|
Callback(nil)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
-export([macro_for/3, local_for/3, local_for/4]).
|
-export([macro_for/3, local_for/3, local_for/4]).
|
||||||
|
|
||||||
-include("elixir.hrl").
|
-include("elixir.hrl").
|
||||||
-define(attr, '__locals_tracker').
|
-define(attr, {elixir, locals_tracker}).
|
||||||
-define(tracker, 'Elixir.Module.LocalsTracker').
|
-define(tracker, 'Elixir.Module.LocalsTracker').
|
||||||
|
|
||||||
macro_for(Module, Name, Arity) ->
|
macro_for(Module, Name, Arity) ->
|
||||||
@@ -72,12 +72,12 @@ rewrite_clause(Else, _) -> Else.
|
|||||||
%% TRACKING
|
%% TRACKING
|
||||||
|
|
||||||
setup(Module) ->
|
setup(Module) ->
|
||||||
case code:is_loaded(?tracker) of
|
case elixir_compiler:get_opt(internal) of
|
||||||
{file, _} ->
|
|
||||||
{ok, Pid} = ?tracker:start_link(),
|
|
||||||
ets:insert(Module, {?attr, Pid}),
|
|
||||||
ok;
|
|
||||||
false ->
|
false ->
|
||||||
|
{ok, Pid} = ?tracker:start_link(),
|
||||||
|
ets:insert(elixir_module:data_table(Module), {?attr, Pid}),
|
||||||
|
ok;
|
||||||
|
true ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@@ -108,7 +108,7 @@ if_tracker(Module, Callback) ->
|
|||||||
if_tracker(Module, ok, Callback).
|
if_tracker(Module, ok, Callback).
|
||||||
|
|
||||||
if_tracker(Module, Default, Callback) ->
|
if_tracker(Module, Default, Callback) ->
|
||||||
try ets:lookup_element(Module, ?attr, 2) of
|
try ets:lookup_element(elixir_module:data_table(Module), ?attr, 2) of
|
||||||
Pid -> Callback(Pid)
|
Pid -> Callback(Pid)
|
||||||
catch
|
catch
|
||||||
error:badarg -> Default
|
error:badarg -> Default
|
||||||
@@ -118,7 +118,7 @@ if_tracker(Module, Default, Callback) ->
|
|||||||
|
|
||||||
cache_env(#{module := Module} = RE) ->
|
cache_env(#{module := Module} = RE) ->
|
||||||
E = RE#{line := nil,vars := []},
|
E = RE#{line := nil,vars := []},
|
||||||
try ets:lookup_element(Module, ?attr, 2) of
|
try ets:lookup_element(elixir_module:data_table(Module), ?attr, 2) of
|
||||||
Pid ->
|
Pid ->
|
||||||
{Pid, ?tracker:cache_env(Pid, E)}
|
{Pid, ?tracker:cache_env(Pid, E)}
|
||||||
catch
|
catch
|
||||||
|
|||||||
@@ -5,9 +5,11 @@
|
|||||||
|
|
||||||
expand_map(Meta, [{'|', UpdateMeta, [Left, Right]}], E) ->
|
expand_map(Meta, [{'|', UpdateMeta, [Left, Right]}], E) ->
|
||||||
{[ELeft|ERight], EA} = elixir_exp:expand_args([Left|Right], E),
|
{[ELeft|ERight], EA} = elixir_exp:expand_args([Left|Right], E),
|
||||||
|
validate_kv(Meta, ERight, Right, E),
|
||||||
{{'%{}', Meta, [{'|', UpdateMeta, [ELeft, ERight]}]}, EA};
|
{{'%{}', Meta, [{'|', UpdateMeta, [ELeft, ERight]}]}, EA};
|
||||||
expand_map(Meta, Args, E) ->
|
expand_map(Meta, Args, E) ->
|
||||||
{EArgs, EA} = elixir_exp:expand_args(Args, E),
|
{EArgs, EA} = elixir_exp:expand_args(Args, E),
|
||||||
|
validate_kv(Meta, EArgs, Args, E),
|
||||||
{{'%{}', Meta, EArgs}, EA}.
|
{{'%{}', Meta, EArgs}, EA}.
|
||||||
|
|
||||||
expand_struct(Meta, Left, Right, E) ->
|
expand_struct(Meta, Left, Right, E) ->
|
||||||
@@ -35,6 +37,15 @@ expand_struct(Meta, Left, Right, E) ->
|
|||||||
|
|
||||||
{{'%', EMeta, [ELeft, ERight]}, EE}.
|
{{'%', EMeta, [ELeft, ERight]}, EE}.
|
||||||
|
|
||||||
|
validate_kv(Meta, KV, Original, E) ->
|
||||||
|
lists:foldl(fun
|
||||||
|
({_K, _V}, Acc) -> Acc + 1;
|
||||||
|
(_, Acc) ->
|
||||||
|
compile_error(Meta, ?m(E, file),
|
||||||
|
"expected key-value pairs in a map, got: ~ts",
|
||||||
|
['Elixir.Macro':to_string(lists:nth(Acc, Original))])
|
||||||
|
end, 1, KV).
|
||||||
|
|
||||||
translate_map(Meta, Args, S) ->
|
translate_map(Meta, Args, S) ->
|
||||||
{Assocs, TUpdate, US} = extract_assoc_update(Args, S),
|
{Assocs, TUpdate, US} = extract_assoc_update(Args, S),
|
||||||
translate_map(Meta, Assocs, TUpdate, US).
|
translate_map(Meta, Assocs, TUpdate, US).
|
||||||
@@ -139,14 +150,10 @@ translate_map(Meta, Assocs, TUpdate, #elixir_scope{extra=Extra} = S) ->
|
|||||||
|
|
||||||
Line = ?line(Meta),
|
Line = ?line(Meta),
|
||||||
|
|
||||||
{TArgs, SA} = lists:mapfoldl(fun
|
{TArgs, SA} = lists:mapfoldl(fun({Key, Value}, Acc) ->
|
||||||
({Key, Value}, Acc) ->
|
{TKey, Acc1} = KeyFun(Key, Acc),
|
||||||
{TKey, Acc1} = KeyFun(Key, Acc),
|
{TValue, Acc2} = ValFun(Value, Acc1#elixir_scope{extra=Extra}),
|
||||||
{TValue, Acc2} = ValFun(Value, Acc1#elixir_scope{extra=Extra}),
|
{{Op, ?line(Meta), TKey, TValue}, Acc2}
|
||||||
{{Op, ?line(Meta), TKey, TValue}, Acc2};
|
|
||||||
(Other, _Acc) ->
|
|
||||||
compile_error(Meta, S#elixir_scope.file, "expected key-value pairs in map, got: ~ts",
|
|
||||||
['Elixir.Macro':to_string(Other)])
|
|
||||||
end, S, Assocs),
|
end, S, Assocs),
|
||||||
|
|
||||||
build_map(Line, TUpdate, TArgs, SA).
|
build_map(Line, TUpdate, TArgs, SA).
|
||||||
|
|||||||
@@ -1,25 +1,27 @@
|
|||||||
-module(elixir_module).
|
-module(elixir_module).
|
||||||
-export([compile/4, data_table/1, docs_table/1, is_open/1,
|
-export([data_table/1, defs_table/1, clas_table/1, is_open/1,
|
||||||
expand_callback/6, add_beam_chunk/3, format_error/1]).
|
compile/4, expand_callback/6, add_beam_chunk/3, format_error/1]).
|
||||||
-include("elixir.hrl").
|
-include("elixir.hrl").
|
||||||
|
|
||||||
-define(acc_attr, '__acc_attributes').
|
-define(acc_attr, {elixir, acc_attributes}).
|
||||||
-define(docs_attr, '__docs_table').
|
-define(lexical_attr, {elixir, lexical_tracker}).
|
||||||
-define(lexical_attr, '__lexical_tracker').
|
-define(persisted_attr, {elixir, persisted_attributes}).
|
||||||
-define(persisted_attr, '__persisted_attributes').
|
-define(overridable_attr, {elixir, overridable}).
|
||||||
-define(overridable_attr, '__overridable').
|
-define(location_attr, {elixir, location}).
|
||||||
-define(location_attr, '__location').
|
|
||||||
|
|
||||||
%% TABLE METHODS
|
%% TABLE METHODS
|
||||||
|
|
||||||
data_table(Module) ->
|
data_table(Module) ->
|
||||||
Module.
|
ets:lookup_element(elixir_modules, Module, 2).
|
||||||
|
|
||||||
docs_table(Module) ->
|
defs_table(Module) ->
|
||||||
ets:lookup_element(Module, ?docs_attr, 2).
|
ets:lookup_element(elixir_modules, Module, 3).
|
||||||
|
|
||||||
|
clas_table(Module) ->
|
||||||
|
ets:lookup_element(elixir_modules, Module, 4).
|
||||||
|
|
||||||
is_open(Module) ->
|
is_open(Module) ->
|
||||||
Module == ets:info(Module, name).
|
ets:lookup(elixir_modules, Module) /= [].
|
||||||
|
|
||||||
%% Compilation hook
|
%% Compilation hook
|
||||||
|
|
||||||
@@ -49,12 +51,12 @@ do_compile(Line, Module, Block, Vars, E) ->
|
|||||||
check_module_availability(Line, File, Module),
|
check_module_availability(Line, File, Module),
|
||||||
|
|
||||||
Docs = elixir_compiler:get_opt(docs),
|
Docs = elixir_compiler:get_opt(docs),
|
||||||
build(Line, File, Module, Docs, ?m(E, lexical_tracker)),
|
{Data, Defs, Clas, Ref} = build(Line, File, Module, Docs, ?m(E, lexical_tracker)),
|
||||||
|
|
||||||
try
|
try
|
||||||
{Result, NE} = eval_form(Line, Module, Block, Vars, E),
|
{Result, NE} = eval_form(Line, Module, Data, Block, Vars, E),
|
||||||
|
|
||||||
_ = case ets:lookup(data_table(Module), 'on_load') of
|
_ = case ets:lookup(Data, 'on_load') of
|
||||||
[] -> ok;
|
[] -> ok;
|
||||||
[{on_load,OnLoad}] ->
|
[{on_load,OnLoad}] ->
|
||||||
[elixir_locals:record_local(Tuple, Module) || Tuple <- OnLoad]
|
[elixir_locals:record_local(Tuple, Module) || Tuple <- OnLoad]
|
||||||
@@ -65,14 +67,14 @@ do_compile(Line, Module, Block, Vars, E) ->
|
|||||||
|
|
||||||
{All, Forms0} = functions_form(Line, File, Module, Def, Defp,
|
{All, Forms0} = functions_form(Line, File, Module, Def, Defp,
|
||||||
Defmacro, Defmacrop, Exports, Functions),
|
Defmacro, Defmacrop, Exports, Functions),
|
||||||
Forms1 = specs_form(Module, Defmacro, Defmacrop, Forms0),
|
Forms1 = specs_form(Data, Defmacro, Defmacrop, Forms0),
|
||||||
Forms2 = types_form(Line, File, Module, Forms1),
|
Forms2 = types_form(Line, File, Data, Forms1),
|
||||||
Forms3 = attributes_form(Line, File, Module, Forms2),
|
Forms3 = attributes_form(Line, File, Data, Forms2),
|
||||||
|
|
||||||
elixir_locals:ensure_no_import_conflict(Line, File, Module, All),
|
elixir_locals:ensure_no_import_conflict(Line, File, Module, All),
|
||||||
|
|
||||||
case Docs of
|
case Docs of
|
||||||
true -> warn_unused_docs(Line, File, Module, doc);
|
true -> warn_unused_docs(Line, File, Data, doc);
|
||||||
false -> false
|
false -> false
|
||||||
end,
|
end,
|
||||||
|
|
||||||
@@ -83,65 +85,68 @@ do_compile(Line, Module, Block, Vars, E) ->
|
|||||||
{attribute, Line, module, Module} | Forms3
|
{attribute, Line, module, Module} | Forms3
|
||||||
],
|
],
|
||||||
|
|
||||||
Binary = load_form(Line, Final, compile_opts(Module), NE),
|
Binary = load_form(Line, Data, Final, compile_opts(Module), NE),
|
||||||
{module, Module, Binary, Result}
|
{module, Module, Binary, Result}
|
||||||
after
|
after
|
||||||
elixir_locals:cleanup(Module),
|
elixir_locals:cleanup(Module),
|
||||||
elixir_def:cleanup(Module),
|
ets:delete(Data),
|
||||||
ets:delete(docs_table(Module)),
|
ets:delete(Defs),
|
||||||
ets:delete(data_table(Module))
|
ets:delete(Clas),
|
||||||
|
elixir_code_server:call({undefmodule, Ref})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Hook that builds both attribute and functions and set up common hooks.
|
%% Hook that builds both attribute and functions and set up common hooks.
|
||||||
|
|
||||||
build(Line, File, Module, Docs, Lexical) ->
|
build(Line, File, Module, Docs, Lexical) ->
|
||||||
%% Table with meta information about the module.
|
case ets:lookup(elixir_modules, Module) of
|
||||||
DataTable = data_table(Module),
|
[{Module, _, _, _, OldLine, OldFile}] ->
|
||||||
|
|
||||||
OldTable = ets:info(DataTable, name),
|
|
||||||
case OldTable == DataTable of
|
|
||||||
true ->
|
|
||||||
[{OldFile, OldLine}] = ets:lookup_element(OldTable, ?location_attr, 2),
|
|
||||||
Error = {module_in_definition, Module, OldFile, OldLine},
|
Error = {module_in_definition, Module, OldFile, OldLine},
|
||||||
elixir_errors:form_error([{line, Line}], File, ?MODULE, Error);
|
elixir_errors:form_error([{line, Line}], File, ?MODULE, Error);
|
||||||
false ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
end,
|
end,
|
||||||
|
|
||||||
DataTable = ets:new(DataTable, [set, named_table, public]),
|
Data = ets:new(Module, [set, public]),
|
||||||
ets:insert(DataTable, {before_compile, []}),
|
Defs = ets:new(Module, [set, public]),
|
||||||
ets:insert(DataTable, {after_compile, []}),
|
Clas = ets:new(Module, [bag, public]),
|
||||||
|
|
||||||
|
Ref = elixir_code_server:call({defmodule, self(),
|
||||||
|
{Module, Data, Defs, Clas, Line, File}}),
|
||||||
|
|
||||||
|
ets:insert(Data, {before_compile, []}),
|
||||||
|
ets:insert(Data, {after_compile, []}),
|
||||||
|
ets:insert(Data, {moduledoc, nil}),
|
||||||
|
|
||||||
case Docs of
|
case Docs of
|
||||||
true -> ets:insert(DataTable, {on_definition, [{'Elixir.Module', compile_doc}]});
|
true -> ets:insert(Data, {on_definition, [{'Elixir.Module', compile_doc}]});
|
||||||
_ -> ets:insert(DataTable, {on_definition, []})
|
_ -> ets:insert(Data, {on_definition, []})
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Attributes = [behaviour, on_load, spec, type, typep, opaque, callback, compile, external_resource],
|
Attributes = [behaviour, on_load, compile, external_resource],
|
||||||
ets:insert(DataTable, {?acc_attr, [before_compile, after_compile, on_definition, derive|Attributes]}),
|
ets:insert(Data, {?acc_attr, [before_compile, after_compile, on_definition, derive,
|
||||||
ets:insert(DataTable, {?persisted_attr, [vsn|Attributes]}),
|
spec, type, typep, opaque, callback|Attributes]}),
|
||||||
ets:insert(DataTable, {?docs_attr, ets:new(DataTable, [ordered_set, public])}),
|
ets:insert(Data, {?persisted_attr, [vsn|Attributes]}),
|
||||||
ets:insert(DataTable, {?lexical_attr, Lexical}),
|
ets:insert(Data, {?lexical_attr, Lexical}),
|
||||||
ets:insert(DataTable, {?overridable_attr, []}),
|
|
||||||
ets:insert(DataTable, {?location_attr, [{File, Line}]}),
|
|
||||||
|
|
||||||
%% Setup other modules
|
%% Setup definition related modules
|
||||||
elixir_def:setup(Module),
|
elixir_def:setup(Module),
|
||||||
elixir_locals:setup(Module),
|
elixir_locals:setup(Module),
|
||||||
ok.
|
elixir_def_overridable:setup(Module),
|
||||||
|
|
||||||
|
{Data, Defs, Clas, Ref}.
|
||||||
|
|
||||||
%% Receives the module representation and evaluates it.
|
%% Receives the module representation and evaluates it.
|
||||||
|
|
||||||
eval_form(Line, Module, Block, Vars, E) ->
|
eval_form(Line, Module, Data, Block, Vars, E) ->
|
||||||
{Value, EE} = elixir_compiler:eval_forms(Block, Vars, E),
|
{Value, EE} = elixir_compiler:eval_forms(Block, Vars, E),
|
||||||
elixir_def_overridable:store_pending(Module),
|
elixir_def_overridable:store_pending(Module),
|
||||||
EV = elixir_env:linify({Line, EE#{vars := [], export_vars := nil}}),
|
EV = elixir_env:linify({Line, EE#{vars := [], export_vars := nil}}),
|
||||||
EC = eval_callbacks(Line, Module, before_compile, [EV], EV),
|
EC = eval_callbacks(Line, Data, before_compile, [EV], EV),
|
||||||
elixir_def_overridable:store_pending(Module),
|
elixir_def_overridable:store_pending(Module),
|
||||||
{Value, EC}.
|
{Value, EC}.
|
||||||
|
|
||||||
eval_callbacks(Line, Module, Name, Args, E) ->
|
eval_callbacks(Line, Data, Name, Args, E) ->
|
||||||
Callbacks = lists:reverse(ets:lookup_element(data_table(Module), Name, 2)),
|
Callbacks = lists:reverse(ets:lookup_element(Data, Name, 2)),
|
||||||
|
|
||||||
lists:foldl(fun({M,F}, Acc) ->
|
lists:foldl(fun({M,F}, Acc) ->
|
||||||
expand_callback(Line, M, F, Args, Acc#{vars := [], export_vars := nil},
|
expand_callback(Line, M, F, Args, Acc#{vars := [], export_vars := nil},
|
||||||
@@ -160,13 +165,11 @@ functions_form(Line, File, Module, Def, Defp, Defmacro, Defmacrop, Exports, Body
|
|||||||
|
|
||||||
%% Add attributes handling to the form
|
%% Add attributes handling to the form
|
||||||
|
|
||||||
attributes_form(Line, File, Module, Current) ->
|
attributes_form(Line, File, Data, Current) ->
|
||||||
Table = data_table(Module),
|
AccAttrs = ets:lookup_element(Data, ?acc_attr, 2),
|
||||||
|
PersistedAttrs = ets:lookup_element(Data, ?persisted_attr, 2),
|
||||||
|
|
||||||
AccAttrs = ets:lookup_element(Table, '__acc_attributes', 2),
|
Transform = fun({Key, Value}, Acc) when is_atom(Key) ->
|
||||||
PersistedAttrs = ets:lookup_element(Table, '__persisted_attributes', 2),
|
|
||||||
|
|
||||||
Transform = fun({Key, Value}, Acc) ->
|
|
||||||
case lists:member(Key, PersistedAttrs) of
|
case lists:member(Key, PersistedAttrs) of
|
||||||
false -> Acc;
|
false -> Acc;
|
||||||
true ->
|
true ->
|
||||||
@@ -182,7 +185,8 @@ attributes_form(Line, File, Module, Current) ->
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
ets:foldl(Transform, Current, Table).
|
Results = ets:select(Data, [{{'$1', '_'}, [{is_atom, '$1'}], ['$_']}]),
|
||||||
|
lists:foldl(Transform, Current, Results).
|
||||||
|
|
||||||
process_attribute(Line, File, external_resource, Values) ->
|
process_attribute(Line, File, external_resource, Values) ->
|
||||||
lists:usort([process_external_resource(Line, File, Value) || Value <- Values]);
|
lists:usort([process_external_resource(Line, File, Value) || Value <- Values]);
|
||||||
@@ -197,27 +201,21 @@ process_external_resource(Line, File, Value) ->
|
|||||||
|
|
||||||
%% Types
|
%% Types
|
||||||
|
|
||||||
types_form(Line, File, Module, Forms0) ->
|
types_form(Line, File, Data, Forms0) ->
|
||||||
case code:ensure_loaded('Elixir.Kernel.Typespec') of
|
case elixir_compiler:get_opt(internal) of
|
||||||
{module, 'Elixir.Kernel.Typespec'} ->
|
false ->
|
||||||
Types0 = 'Elixir.Module':get_attribute(Module, type) ++
|
Types0 = get_typespec(Data, type) ++ get_typespec(Data, typep)
|
||||||
'Elixir.Module':get_attribute(Module, typep) ++
|
++ get_typespec(Data, opaque),
|
||||||
'Elixir.Module':get_attribute(Module, opaque),
|
|
||||||
|
|
||||||
Types1 = ['Elixir.Kernel.Typespec':translate_type(Kind, Expr, Doc, Caller) ||
|
Types1 = ['Elixir.Kernel.Typespec':translate_type(Kind, Expr, Doc, Caller) ||
|
||||||
{Kind, Expr, Doc, Caller} <- Types0],
|
{Kind, Expr, Doc, Caller} <- Types0],
|
||||||
|
|
||||||
'Elixir.Module':delete_attribute(Module, type),
|
warn_unused_docs(Line, File, Data, typedoc),
|
||||||
'Elixir.Module':delete_attribute(Module, typep),
|
|
||||||
'Elixir.Module':delete_attribute(Module, opaque),
|
|
||||||
|
|
||||||
warn_unused_docs(Line, File, Module, typedoc),
|
|
||||||
|
|
||||||
Forms1 = types_attributes(Types1, Forms0),
|
Forms1 = types_attributes(Types1, Forms0),
|
||||||
Forms2 = export_types_attributes(Types1, Forms1),
|
Forms2 = export_types_attributes(Types1, Forms1),
|
||||||
typedocs_attributes(Types1, Forms2);
|
typedocs_attributes(Types1, Forms2);
|
||||||
|
|
||||||
{error, _} ->
|
true ->
|
||||||
Forms0
|
Forms0
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@@ -247,23 +245,17 @@ typedocs_attributes(Types, Forms) ->
|
|||||||
|
|
||||||
%% Specs
|
%% Specs
|
||||||
|
|
||||||
specs_form(Module, Defmacro, Defmacrop, Forms) ->
|
specs_form(Data, Defmacro, Defmacrop, Forms) ->
|
||||||
case code:ensure_loaded('Elixir.Kernel.Typespec') of
|
case elixir_compiler:get_opt(internal) of
|
||||||
{module, 'Elixir.Kernel.Typespec'} ->
|
false ->
|
||||||
Specs0 = 'Elixir.Module':get_attribute(Module, spec) ++
|
Specs0 = get_typespec(Data, spec) ++ get_typespec(Data, callback),
|
||||||
'Elixir.Module':get_attribute(Module, callback),
|
|
||||||
|
|
||||||
Specs1 = ['Elixir.Kernel.Typespec':translate_spec(Kind, Expr, Caller) ||
|
Specs1 = ['Elixir.Kernel.Typespec':translate_spec(Kind, Expr, Caller) ||
|
||||||
{Kind, Expr, Caller} <- Specs0],
|
{Kind, Expr, Caller} <- Specs0],
|
||||||
Specs2 = lists:flatmap(fun(Spec) ->
|
Specs2 = lists:flatmap(fun(Spec) ->
|
||||||
translate_macro_spec(Spec, Defmacro, Defmacrop)
|
translate_macro_spec(Spec, Defmacro, Defmacrop)
|
||||||
end, Specs1),
|
end, Specs1),
|
||||||
|
|
||||||
'Elixir.Module':delete_attribute(Module, spec),
|
|
||||||
'Elixir.Module':delete_attribute(Module, callback),
|
|
||||||
specs_attributes(Forms, Specs2);
|
specs_attributes(Forms, Specs2);
|
||||||
|
true ->
|
||||||
{error, _} ->
|
|
||||||
Forms
|
Forms
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@@ -307,11 +299,11 @@ compile_opts(Module) ->
|
|||||||
[] -> []
|
[] -> []
|
||||||
end.
|
end.
|
||||||
|
|
||||||
load_form(Line, Forms, Opts, E) ->
|
load_form(Line, Data, Forms, Opts, E) ->
|
||||||
elixir_compiler:module(Forms, Opts, E, fun(Module, Binary0) ->
|
elixir_compiler:module(Forms, Opts, E, fun(Module, Binary0) ->
|
||||||
Docs = elixir_compiler:get_opt(docs),
|
Docs = elixir_compiler:get_opt(docs),
|
||||||
Binary = add_docs_chunk(Binary0, Module, Line, Docs),
|
Binary = add_docs_chunk(Binary0, Data, Line, Docs),
|
||||||
eval_callbacks(Line, Module, after_compile, [E, Binary], E),
|
eval_callbacks(Line, Data, after_compile, [E, Binary], E),
|
||||||
|
|
||||||
case get(elixir_compiled) of
|
case get(elixir_compiled) of
|
||||||
Current when is_list(Current) ->
|
Current when is_list(Current) ->
|
||||||
@@ -331,23 +323,30 @@ load_form(Line, Forms, Opts, E) ->
|
|||||||
Binary
|
Binary
|
||||||
end).
|
end).
|
||||||
|
|
||||||
add_docs_chunk(Bin, Module, Line, true) ->
|
add_docs_chunk(Bin, Data, Line, true) ->
|
||||||
ChunkData = term_to_binary({elixir_docs_v1, [
|
ChunkData = term_to_binary({elixir_docs_v1, [
|
||||||
{docs, get_docs(Module)},
|
{docs, get_docs(Data)},
|
||||||
{moduledoc, get_moduledoc(Line, Module)}
|
{moduledoc, get_moduledoc(Line, Data)}
|
||||||
]}),
|
]}),
|
||||||
add_beam_chunk(Bin, "ExDc", ChunkData);
|
add_beam_chunk(Bin, "ExDc", ChunkData);
|
||||||
|
|
||||||
add_docs_chunk(Bin, _, _, _) -> Bin.
|
add_docs_chunk(Bin, _, _, _) -> Bin.
|
||||||
|
|
||||||
get_docs(Module) ->
|
get_docs(Data) ->
|
||||||
ordsets:from_list(
|
Match = ets:match(Data, {{doc, '$1'}, '$2', '$3', '$4', '$5'}),
|
||||||
|
lists:usort(
|
||||||
[{Tuple, Line, Kind, Sig, Doc} ||
|
[{Tuple, Line, Kind, Sig, Doc} ||
|
||||||
{Tuple, Line, Kind, Sig, Doc} <- ets:tab2list(docs_table(Module)),
|
[Tuple, Line, Kind, Sig, Doc] <- Match,
|
||||||
Kind =/= type, Kind =/= opaque]).
|
Kind =/= type, Kind =/= opaque]).
|
||||||
|
|
||||||
get_moduledoc(Line, Module) ->
|
get_moduledoc(Line, Data) ->
|
||||||
{Line, 'Elixir.Module':get_attribute(Module, moduledoc)}.
|
{Line, ets:lookup_element(Data, moduledoc, 2)}.
|
||||||
|
|
||||||
|
get_typespec(Data, Key) ->
|
||||||
|
case ets:lookup(Data, Key) of
|
||||||
|
[{Key, Value}] -> Value;
|
||||||
|
[] -> []
|
||||||
|
end.
|
||||||
|
|
||||||
check_module_availability(Line, File, Module) ->
|
check_module_availability(Line, File, Module) ->
|
||||||
Reserved = ['Elixir.Any', 'Elixir.BitString', 'Elixir.Function', 'Elixir.PID',
|
Reserved = ['Elixir.Any', 'Elixir.BitString', 'Elixir.Function', 'Elixir.PID',
|
||||||
@@ -370,8 +369,8 @@ check_module_availability(Line, File, Module) ->
|
|||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
warn_unused_docs(Line, File, Module, Attribute) ->
|
warn_unused_docs(Line, File, Data, Attribute) ->
|
||||||
case ets:member(data_table(Module), Attribute) of
|
case ets:member(Data, Attribute) of
|
||||||
true ->
|
true ->
|
||||||
elixir_errors:form_warn([{line, Line}], File, ?MODULE, {unused_doc, Attribute});
|
elixir_errors:form_warn([{line, Line}], File, ?MODULE, {unused_doc, Attribute});
|
||||||
_ ->
|
_ ->
|
||||||
|
|||||||
@@ -0,0 +1,224 @@
|
|||||||
|
-module(elixir_rewrite).
|
||||||
|
-export([rewrite/5, inline/3]).
|
||||||
|
|
||||||
|
%% Convenience variables
|
||||||
|
|
||||||
|
-define(hidden, [{line,-1}]).
|
||||||
|
|
||||||
|
-define(atom, 'Elixir.Atom').
|
||||||
|
-define(enum, 'Elixir.Enum').
|
||||||
|
-define(float, 'Elixir.Float').
|
||||||
|
-define(io, 'Elixir.IO').
|
||||||
|
-define(integer, 'Elixir.Integer').
|
||||||
|
-define(kernel, 'Elixir.Kernel').
|
||||||
|
-define(list, 'Elixir.List').
|
||||||
|
-define(map, 'Elixir.Map').
|
||||||
|
-define(node, 'Elixir.Node').
|
||||||
|
-define(port, 'Elixir.Port').
|
||||||
|
-define(process, 'Elixir.Process').
|
||||||
|
-define(string, 'Elixir.String').
|
||||||
|
-define(string_chars, 'Elixir.String.Chars').
|
||||||
|
-define(system, 'Elixir.System').
|
||||||
|
-define(tuple, 'Elixir.Tuple').
|
||||||
|
|
||||||
|
%% Inline
|
||||||
|
|
||||||
|
%% Inline rules are straight-forward, they keep the same
|
||||||
|
%% number and order of arguments and show up on captures.
|
||||||
|
|
||||||
|
inline(?atom, to_char_list, 1) -> {erlang, atom_to_list};
|
||||||
|
inline(?io, iodata_length, 1) -> {erlang, iolist_size};
|
||||||
|
inline(?io, iodata_to_binary, 1) -> {erlang, iolist_to_binary};
|
||||||
|
inline(?integer, to_string, 1) -> {erlang, integer_to_binary};
|
||||||
|
inline(?integer, to_string, 2) -> {erlang, integer_to_binary};
|
||||||
|
inline(?integer, to_char_list, 1) -> {erlang, integer_to_list};
|
||||||
|
inline(?integer, to_char_list, 2) -> {erlang, integer_to_list};
|
||||||
|
inline(?float, to_string, 1) -> {erlang, float_to_binary};
|
||||||
|
inline(?float, to_char_list, 1) -> {erlang, float_to_list};
|
||||||
|
inline(?list, to_atom, 1) -> {erlang, list_to_atom};
|
||||||
|
inline(?list, to_existing_atom, 1) -> {erlang, list_to_existing_atom};
|
||||||
|
inline(?list, to_float, 1) -> {erlang, list_to_float};
|
||||||
|
inline(?list, to_integer, 1) -> {erlang, list_to_integer};
|
||||||
|
inline(?list, to_integer, 2) -> {erlang, list_to_integer};
|
||||||
|
inline(?list, to_tuple, 1) -> {erlang, list_to_tuple};
|
||||||
|
|
||||||
|
inline(?kernel, '+', 2) -> {erlang, '+'};
|
||||||
|
inline(?kernel, '-', 2) -> {erlang, '-'};
|
||||||
|
inline(?kernel, '+', 1) -> {erlang, '+'};
|
||||||
|
inline(?kernel, '-', 1) -> {erlang, '-'};
|
||||||
|
inline(?kernel, '*', 2) -> {erlang, '*'};
|
||||||
|
inline(?kernel, '/', 2) -> {erlang, '/'};
|
||||||
|
inline(?kernel, '++', 2) -> {erlang, '++'};
|
||||||
|
inline(?kernel, '--', 2) -> {erlang, '--'};
|
||||||
|
inline(?kernel, 'not', 1) -> {erlang, 'not'};
|
||||||
|
inline(?kernel, '<', 2) -> {erlang, '<'};
|
||||||
|
inline(?kernel, '>', 2) -> {erlang, '>'};
|
||||||
|
inline(?kernel, '<=', 2) -> {erlang, '=<'};
|
||||||
|
inline(?kernel, '>=', 2) -> {erlang, '>='};
|
||||||
|
inline(?kernel, '==', 2) -> {erlang, '=='};
|
||||||
|
inline(?kernel, '!=', 2) -> {erlang, '/='};
|
||||||
|
inline(?kernel, '===', 2) -> {erlang, '=:='};
|
||||||
|
inline(?kernel, '!==', 2) -> {erlang, '=/='};
|
||||||
|
inline(?kernel, abs, 1) -> {erlang, abs};
|
||||||
|
inline(?kernel, apply, 2) -> {erlang, apply};
|
||||||
|
inline(?kernel, apply, 3) -> {erlang, apply};
|
||||||
|
inline(?kernel, binary_part, 3) -> {erlang, binary_part};
|
||||||
|
inline(?kernel, bit_size, 1) -> {erlang, bit_size};
|
||||||
|
inline(?kernel, byte_size, 1) -> {erlang, byte_size};
|
||||||
|
inline(?kernel, 'div', 2) -> {erlang, 'div'};
|
||||||
|
inline(?kernel, exit, 1) -> {erlang, exit};
|
||||||
|
inline(?kernel, hd, 1) -> {erlang, hd};
|
||||||
|
inline(?kernel, is_atom, 1) -> {erlang, is_atom};
|
||||||
|
inline(?kernel, is_binary, 1) -> {erlang, is_binary};
|
||||||
|
inline(?kernel, is_bitstring, 1) -> {erlang, is_bitstring};
|
||||||
|
inline(?kernel, is_boolean, 1) -> {erlang, is_boolean};
|
||||||
|
inline(?kernel, is_float, 1) -> {erlang, is_float};
|
||||||
|
inline(?kernel, is_function, 1) -> {erlang, is_function};
|
||||||
|
inline(?kernel, is_function, 2) -> {erlang, is_function};
|
||||||
|
inline(?kernel, is_integer, 1) -> {erlang, is_integer};
|
||||||
|
inline(?kernel, is_list, 1) -> {erlang, is_list};
|
||||||
|
inline(?kernel, is_map, 1) -> {erlang, is_map};
|
||||||
|
inline(?kernel, is_number, 1) -> {erlang, is_number};
|
||||||
|
inline(?kernel, is_pid, 1) -> {erlang, is_pid};
|
||||||
|
inline(?kernel, is_port, 1) -> {erlang, is_port};
|
||||||
|
inline(?kernel, is_reference, 1) -> {erlang, is_reference};
|
||||||
|
inline(?kernel, is_tuple, 1) -> {erlang, is_tuple};
|
||||||
|
inline(?kernel, length, 1) -> {erlang, length};
|
||||||
|
inline(?kernel, make_ref, 0) -> {erlang, make_ref};
|
||||||
|
inline(?kernel, map_size, 1) -> {erlang, map_size};
|
||||||
|
inline(?kernel, max, 2) -> {erlang, max};
|
||||||
|
inline(?kernel, min, 2) -> {erlang, min};
|
||||||
|
inline(?kernel, node, 0) -> {erlang, node};
|
||||||
|
inline(?kernel, node, 1) -> {erlang, node};
|
||||||
|
inline(?kernel, 'rem', 2) -> {erlang, 'rem'};
|
||||||
|
inline(?kernel, round, 1) -> {erlang, round};
|
||||||
|
inline(?kernel, self, 0) -> {erlang, self};
|
||||||
|
inline(?kernel, send, 2) -> {erlang, send};
|
||||||
|
inline(?kernel, spawn, 1) -> {erlang, spawn};
|
||||||
|
inline(?kernel, spawn, 3) -> {erlang, spawn};
|
||||||
|
inline(?kernel, spawn_link, 1) -> {erlang, spawn_link};
|
||||||
|
inline(?kernel, spawn_link, 3) -> {erlang, spawn_link};
|
||||||
|
inline(?kernel, spawn_monitor, 1) -> {erlang, spawn_monitor};
|
||||||
|
inline(?kernel, spawn_monitor, 3) -> {erlang, spawn_monitor};
|
||||||
|
inline(?kernel, throw, 1) -> {erlang, throw};
|
||||||
|
inline(?kernel, tl, 1) -> {erlang, tl};
|
||||||
|
inline(?kernel, trunc, 1) -> {erlang, trunc};
|
||||||
|
inline(?kernel, tuple_size, 1) -> {erlang, tuple_size};
|
||||||
|
|
||||||
|
inline(?map, keys, 1) -> {maps, keys};
|
||||||
|
inline(?map, merge, 2) -> {maps, merge};
|
||||||
|
inline(?map, size, 1) -> {maps, size};
|
||||||
|
inline(?map, values, 1) -> {maps, values};
|
||||||
|
inline(?map, to_list, 1) -> {maps, to_list};
|
||||||
|
|
||||||
|
inline(?node, spawn, 2) -> {erlang, spawn};
|
||||||
|
inline(?node, spawn, 3) -> {erlang, spawn_opt};
|
||||||
|
inline(?node, spawn, 4) -> {erlang, spawn};
|
||||||
|
inline(?node, spawn, 5) -> {erlang, spawn_opt};
|
||||||
|
inline(?node, spawn_link, 2) -> {erlang, spawn_link};
|
||||||
|
inline(?node, spawn_link, 4) -> {erlang, spawn_link};
|
||||||
|
|
||||||
|
inline(?process, exit, 2) -> {erlang, exit};
|
||||||
|
inline(?process, spawn, 2) -> {erlang, spawn_opt};
|
||||||
|
inline(?process, spawn, 4) -> {erlang, spawn_opt};
|
||||||
|
inline(?process, demonitor, 1) -> {erlang, demonitor};
|
||||||
|
inline(?process, demonitor, 2) -> {erlang, demonitor};
|
||||||
|
inline(?process, link, 1) -> {erlang, link};
|
||||||
|
inline(?process, unlink, 1) -> {erlang, unlink};
|
||||||
|
|
||||||
|
inline(?port, open, 2) -> {erlang, open_port};
|
||||||
|
inline(?port, call, 3) -> {erlang, port_call};
|
||||||
|
inline(?port, close, 1) -> {erlang, port_close};
|
||||||
|
inline(?port, command, 2) -> {erlang, port_command};
|
||||||
|
inline(?port, command, 3) -> {erlang, port_command};
|
||||||
|
inline(?port, connect, 2) -> {erlang, port_connect};
|
||||||
|
inline(?port, control, 3) -> {erlang, port_control};
|
||||||
|
inline(?port, info, 1) -> {erlang, port_info};
|
||||||
|
inline(?port, info, 2) -> {erlang, port_info};
|
||||||
|
inline(?port, list, 0) -> {erlang, ports};
|
||||||
|
|
||||||
|
inline(?string, to_float, 1) -> {erlang, binary_to_float};
|
||||||
|
inline(?string, to_integer, 1) -> {erlang, binary_to_integer};
|
||||||
|
inline(?string, to_integer, 2) -> {erlang, binary_to_integer};
|
||||||
|
inline(?system, stacktrace, 0) -> {erlang, get_stacktrace};
|
||||||
|
inline(?tuple, to_list, 1) -> {erlang, tuple_to_list};
|
||||||
|
|
||||||
|
inline(_, _, _) -> false.
|
||||||
|
|
||||||
|
%% Rewrite rules
|
||||||
|
%% Rewrite rules are more complex than regular inlining code.
|
||||||
|
%% It receives all remote call arguments and return quoted
|
||||||
|
%% expressions with the new environment.
|
||||||
|
|
||||||
|
%% Complex rewrite rules
|
||||||
|
|
||||||
|
rewrite(?string_chars, _DotMeta, 'to_string', _Meta, [String]) when is_binary(String) ->
|
||||||
|
String;
|
||||||
|
rewrite(?string_chars, DotMeta, 'to_string', Meta, [String]) ->
|
||||||
|
Var = {'_rewrite', ?hidden, nil},
|
||||||
|
Guard = {{'.', ?hidden, [erlang, is_binary]}, ?hidden, [Var]},
|
||||||
|
Slow = remote(?string_chars, DotMeta, 'to_string', Meta, [Var]),
|
||||||
|
Fast = Var,
|
||||||
|
|
||||||
|
{'case', ?hidden, [String, [{do,
|
||||||
|
[{'->', ?hidden, [[{'when', ?hidden, [Var, Guard]}], Fast]},
|
||||||
|
{'->', ?hidden, [[Var], Slow]}]
|
||||||
|
}]]};
|
||||||
|
|
||||||
|
rewrite(?enum, DotMeta, 'reverse', Meta, [List]) when is_list(List) ->
|
||||||
|
remote(lists, DotMeta, 'reverse', Meta, [List]);
|
||||||
|
rewrite(?enum, DotMeta, 'reverse', Meta, [List]) ->
|
||||||
|
Var = {'_rewrite', ?hidden, nil},
|
||||||
|
Guard = {{'.', ?hidden, [erlang, is_list]}, ?hidden, [Var]},
|
||||||
|
Slow = remote(?enum, DotMeta, 'reverse', Meta, [Var]),
|
||||||
|
Fast = remote(lists, DotMeta, 'reverse', Meta, [Var]),
|
||||||
|
|
||||||
|
{'case', ?hidden, [List, [{do,
|
||||||
|
[{'->', ?hidden, [[{'when', ?hidden, [Var, Guard]}], Fast]},
|
||||||
|
{'->', ?hidden, [[Var], Slow]}]
|
||||||
|
}]]};
|
||||||
|
|
||||||
|
rewrite(Receiver, DotMeta, Right, Meta, Args) ->
|
||||||
|
{EReceiver, ERight, EArgs} = rewrite(Receiver, Right, Args),
|
||||||
|
remote(EReceiver, DotMeta, ERight, Meta, EArgs).
|
||||||
|
|
||||||
|
%% Simple rewrite rules
|
||||||
|
|
||||||
|
rewrite(?atom, to_string, [Arg]) ->
|
||||||
|
{erlang, atom_to_binary, [Arg, utf8]};
|
||||||
|
rewrite(?kernel, elem, [Tuple, Index]) ->
|
||||||
|
{erlang, element, [increment(Index), Tuple]};
|
||||||
|
rewrite(?kernel, put_elem, [Tuple, Index, Value]) ->
|
||||||
|
{erlang, setelement, [increment(Index), Tuple, Value]};
|
||||||
|
rewrite(?map, 'has_key?', [Map, Key]) ->
|
||||||
|
{maps, is_key, [Key, Map]};
|
||||||
|
rewrite(?map, fetch, [Map, Key]) ->
|
||||||
|
{maps, find, [Key, Map]};
|
||||||
|
rewrite(?map, put, [Map, Key, Value]) ->
|
||||||
|
{maps, put, [Key, Value, Map]};
|
||||||
|
rewrite(?map, delete, [Map, Key]) ->
|
||||||
|
{maps, remove, [Key, Map]};
|
||||||
|
rewrite(?process, monitor, [Arg]) ->
|
||||||
|
{erlang, monitor, [process, Arg]};
|
||||||
|
rewrite(?string, to_atom, [Arg]) ->
|
||||||
|
{erlang, binary_to_atom, [Arg, utf8]};
|
||||||
|
rewrite(?string, to_existing_atom, [Arg]) ->
|
||||||
|
{erlang, binary_to_existing_atom, [Arg, utf8]};
|
||||||
|
rewrite(?tuple, insert_at, [Tuple, Index, Term]) ->
|
||||||
|
{erlang, insert_element, [increment(Index), Tuple, Term]};
|
||||||
|
rewrite(?tuple, delete_at, [Tuple, Index]) ->
|
||||||
|
{erlang, delete_element, [increment(Index), Tuple]};
|
||||||
|
rewrite(?tuple, duplicate, [Data, Size]) ->
|
||||||
|
{erlang, make_tuple, [Size, Data]};
|
||||||
|
rewrite(Receiver, Fun, Args) ->
|
||||||
|
{Receiver, Fun, Args}.
|
||||||
|
|
||||||
|
%% Rewrite helpers
|
||||||
|
|
||||||
|
remote(Receiver, DotMeta, Right, Meta, Args) ->
|
||||||
|
{{'.', DotMeta, [Receiver, Right]}, Meta, Args}.
|
||||||
|
|
||||||
|
increment(Number) when is_number(Number) ->
|
||||||
|
Number + 1;
|
||||||
|
increment(Other) ->
|
||||||
|
{{'.', [], [erlang, '+']}, [], [Other, 1]}.
|
||||||
@@ -7,6 +7,16 @@ start_link() ->
|
|||||||
|
|
||||||
init(ok) ->
|
init(ok) ->
|
||||||
Workers = [
|
Workers = [
|
||||||
|
{
|
||||||
|
elixir_config,
|
||||||
|
{elixir_config, start_link, []},
|
||||||
|
|
||||||
|
permanent, % Restart = permanent | transient | temporary
|
||||||
|
2000, % Shutdown = brutal_kill | int() >= 0 | infinity
|
||||||
|
worker, % Type = worker | supervisor
|
||||||
|
[elixir_config] % Modules = [Module] | dynamic
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
elixir_code_server,
|
elixir_code_server,
|
||||||
{elixir_code_server, start_link, []},
|
{elixir_code_server, start_link, []},
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
translate({'=', Meta, [Left, Right]}, S) ->
|
translate({'=', Meta, [Left, Right]}, S) ->
|
||||||
Return = case Left of
|
Return = case Left of
|
||||||
{'_', _, Atom} when is_atom(Atom) -> false;
|
{'_', _, Atom} when is_atom(Atom) -> S#elixir_scope.return;
|
||||||
_ -> true
|
_ -> true
|
||||||
end,
|
end,
|
||||||
|
|
||||||
@@ -257,6 +257,9 @@ translate({{'.', _, [Left, Right]}, Meta, Args}, S)
|
|||||||
%% the is_map/1 guard instead of matching on map. Hopefully
|
%% the is_map/1 guard instead of matching on map. Hopefully
|
||||||
%% we can use a match on 17.1.
|
%% we can use a match on 17.1.
|
||||||
%%
|
%%
|
||||||
|
%% In the future, we could also use maps:get/2 instead
|
||||||
|
%% of pattern match, reducing the AST footprint.
|
||||||
|
%%
|
||||||
%% http://erlang.org/pipermail/erlang-bugs/2014-April/004338.html
|
%% http://erlang.org/pipermail/erlang-bugs/2014-April/004338.html
|
||||||
{{'case', -1, TLeft, [
|
{{'case', -1, TLeft, [
|
||||||
{clause, -1,
|
{clause, -1,
|
||||||
|
|||||||
@@ -156,6 +156,16 @@ erl_rescue_guard_for(Meta, Var, 'Elixir.BadStructError') ->
|
|||||||
erl_tuple_size(Meta, Var, 3),
|
erl_tuple_size(Meta, Var, 3),
|
||||||
erl_record_compare(Meta, Var, badstruct));
|
erl_record_compare(Meta, Var, badstruct));
|
||||||
|
|
||||||
|
erl_rescue_guard_for(Meta, Var, 'Elixir.BadMapError') ->
|
||||||
|
erl_and(Meta,
|
||||||
|
erl_tuple_size(Meta, Var, 2),
|
||||||
|
erl_record_compare(Meta, Var, badmap));
|
||||||
|
|
||||||
|
erl_rescue_guard_for(Meta, Var, 'Elixir.KeyError') ->
|
||||||
|
erl_and(Meta,
|
||||||
|
erl_tuple_size(Meta, Var, 2),
|
||||||
|
erl_record_compare(Meta, Var, badkey));
|
||||||
|
|
||||||
erl_rescue_guard_for(Meta, Var, 'Elixir.ArgumentError') ->
|
erl_rescue_guard_for(Meta, Var, 'Elixir.ArgumentError') ->
|
||||||
erl_or(Meta,
|
erl_or(Meta,
|
||||||
{erl(Meta, '=='), Meta, [Var, badarg]},
|
{erl(Meta, '=='), Meta, [Var, badarg]},
|
||||||
|
|||||||
@@ -291,7 +291,7 @@ defmodule Kernel.ExceptionTest do
|
|||||||
|
|
||||||
test "format_fa" do
|
test "format_fa" do
|
||||||
assert Exception.format_fa(fn -> end, 1) =~
|
assert Exception.format_fa(fn -> end, 1) =~
|
||||||
~r"#Function<\d\.\d+/0 in Kernel\.ExceptionTest\.test format_fa/1>/1"
|
~r"#Function<\d+\.\d+/0 in Kernel\.ExceptionTest\.test format_fa/1>/1"
|
||||||
end
|
end
|
||||||
|
|
||||||
import Exception, only: [message: 1]
|
import Exception, only: [message: 1]
|
||||||
@@ -327,4 +327,20 @@ defmodule Kernel.ExceptionTest do
|
|||||||
assert %ErlangError{original: :sample} |> message ==
|
assert %ErlangError{original: :sample} |> message ==
|
||||||
"erlang error: :sample"
|
"erlang error: :sample"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "format_stacktrace from file" do
|
||||||
|
assert_raise ArgumentError, fn ->
|
||||||
|
Code.eval_string("def foo do end", [], file: "myfile")
|
||||||
|
end
|
||||||
|
|
||||||
|
assert Exception.format_stacktrace(System.stacktrace) =~ "myfile:1: (file)"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "format_stacktrace from module" do
|
||||||
|
assert_raise ArgumentError, fn ->
|
||||||
|
Code.eval_string("defmodule Foo do raise ArgumentError, ~s(oops) end", [], file: "myfile")
|
||||||
|
end
|
||||||
|
|
||||||
|
assert Exception.format_stacktrace(System.stacktrace) =~ "myfile:1: (module)"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
LF
|
||||||
|
CR
CRLF
|
||||||
|
LFCR
|
||||||
|
|
||||||
@@ -432,6 +432,23 @@ defmodule GenEventTest do
|
|||||||
Logger.add_backend(:console, flush: true)
|
Logger.add_backend(:console, flush: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "add_process_handler/2 with GenEvent" do
|
||||||
|
{:ok, snd} = GenEvent.start_link()
|
||||||
|
GenEvent.add_handler(snd, ReplyHandler, {self(), false})
|
||||||
|
|
||||||
|
{:ok, fst} = GenEvent.start_link()
|
||||||
|
:gen.call(fst, self(), {:add_process_handler, snd, snd})
|
||||||
|
|
||||||
|
assert GenEvent.notify(fst, :hello) == :ok
|
||||||
|
assert_receive {:event, :hello}
|
||||||
|
|
||||||
|
assert GenEvent.ack_notify(fst, :hello) == :ok
|
||||||
|
assert_receive {:event, :hello}
|
||||||
|
|
||||||
|
assert GenEvent.sync_notify(fst, :hello) == :ok
|
||||||
|
assert_received {:event, :hello}
|
||||||
|
end
|
||||||
|
|
||||||
test ":sys.get_status/2" do
|
test ":sys.get_status/2" do
|
||||||
{:ok, pid} = GenEvent.start(name: :my_gen_event_name)
|
{:ok, pid} = GenEvent.start(name: :my_gen_event_name)
|
||||||
:ok = GenEvent.add_handler(pid, {ReplyHandler, :ok}, {self(), true})
|
:ok = GenEvent.add_handler(pid, {ReplyHandler, :ok}, {self(), true})
|
||||||
|
|||||||
@@ -299,6 +299,8 @@ defmodule Inspect.MapTest do
|
|||||||
assert_raise ArgumentError, msg, fn ->
|
assert_raise ArgumentError, msg, fn ->
|
||||||
inspect(%Failing{})
|
inspect(%Failing{})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assert [{Inspect.Inspect.MapTest.Failing, :inspect, 2, _}|_] = System.stacktrace
|
||||||
end
|
end
|
||||||
|
|
||||||
test :exception do
|
test :exception do
|
||||||
|
|||||||
@@ -14,24 +14,24 @@ defmodule IO.ANSI.DocsTest do
|
|||||||
|
|
||||||
test "heading is formatted" do
|
test "heading is formatted" do
|
||||||
result = format_heading("wibble")
|
result = format_heading("wibble")
|
||||||
assert String.starts_with?(result, "\e[0m\n\e[7m\e[33m\e[1m")
|
assert String.starts_with?(result, "\e[0m\n\e[7m\e[33m")
|
||||||
assert String.ends_with?(result, "\e[0m\n\e[0m")
|
assert String.ends_with?(result, "\e[0m\n\e[0m")
|
||||||
assert String.contains?(result, " wibble ")
|
assert String.contains?(result, " wibble ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "first level heading is converted" do
|
test "first level heading is converted" do
|
||||||
result = format("# wibble\n\ntext\n")
|
result = format("# wibble\n\ntext\n")
|
||||||
assert result == "\e[33m\e[1mWIBBLE\e[0m\n\e[0m\ntext\n\e[0m"
|
assert result == "\e[33mWIBBLE\e[0m\n\e[0m\ntext\n\e[0m"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "second level heading is converted" do
|
test "second level heading is converted" do
|
||||||
result = format("## wibble\n\ntext\n")
|
result = format("## wibble\n\ntext\n")
|
||||||
assert result == "\e[33m\e[1mwibble\e[0m\n\e[0m\ntext\n\e[0m"
|
assert result == "\e[33mwibble\e[0m\n\e[0m\ntext\n\e[0m"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "third level heading is converted" do
|
test "third level heading is converted" do
|
||||||
result = format("## wibble\n\ntext\n")
|
result = format("## wibble\n\ntext\n")
|
||||||
assert result == "\e[33m\e[1mwibble\e[0m\n\e[0m\ntext\n\e[0m"
|
assert result == "\e[33mwibble\e[0m\n\e[0m\ntext\n\e[0m"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "code block is converted" do
|
test "code block is converted" do
|
||||||
@@ -103,6 +103,9 @@ defmodule IO.ANSI.DocsTest do
|
|||||||
result = format("*world*")
|
result = format("*world*")
|
||||||
assert result == "\e[1mworld\e[0m\n\e[0m"
|
assert result == "\e[1mworld\e[0m\n\e[0m"
|
||||||
|
|
||||||
|
result = format("*world*.")
|
||||||
|
assert result == "\e[1mworld\e[0m.\n\e[0m"
|
||||||
|
|
||||||
result = format("**world**")
|
result = format("**world**")
|
||||||
assert result == "\e[1mworld\e[0m\n\e[0m"
|
assert result == "\e[1mworld\e[0m\n\e[0m"
|
||||||
|
|
||||||
@@ -127,6 +130,28 @@ defmodule IO.ANSI.DocsTest do
|
|||||||
assert result == "\e[36mhello world\e[0m\n\e[0m"
|
assert result == "\e[36mhello world\e[0m\n\e[0m"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "multiple stars/underscores/backticks work" do
|
||||||
|
result = format("*hello world* *hello world*")
|
||||||
|
assert result == "\e[1mhello world\e[0m \e[1mhello world\e[0m\n\e[0m"
|
||||||
|
|
||||||
|
result = format("_hello world_ _hello world_")
|
||||||
|
assert result == "\e[4mhello world\e[0m \e[4mhello world\e[0m\n\e[0m"
|
||||||
|
|
||||||
|
result = format("`hello world` `hello world`")
|
||||||
|
assert result == "\e[36mhello world\e[0m \e[36mhello world\e[0m\n\e[0m"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "multiple stars/underscores/backticks work when separated by other words" do
|
||||||
|
result = format("*hello world* unit test *hello world*")
|
||||||
|
assert result == "\e[1mhello world\e[0m unit test \e[1mhello world\e[0m\n\e[0m"
|
||||||
|
|
||||||
|
result = format("_hello world_ unit test _hello world_")
|
||||||
|
assert result == "\e[4mhello world\e[0m unit test \e[4mhello world\e[0m\n\e[0m"
|
||||||
|
|
||||||
|
result = format("`hello world` unit test `hello world`")
|
||||||
|
assert result == "\e[36mhello world\e[0m unit test \e[36mhello world\e[0m\n\e[0m"
|
||||||
|
end
|
||||||
|
|
||||||
test "star/underscore preceeded by space doesn't get interpreted" do
|
test "star/underscore preceeded by space doesn't get interpreted" do
|
||||||
result = format("_unit _size")
|
result = format("_unit _size")
|
||||||
assert result == "_unit _size\n\e[0m"
|
assert result == "_unit _size\n\e[0m"
|
||||||
@@ -138,6 +163,43 @@ defmodule IO.ANSI.DocsTest do
|
|||||||
assert result == "*unit *size\n\e[0m"
|
assert result == "*unit *size\n\e[0m"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "star/underscore/backtick preceeded by non-space delimiters gets interpreted" do
|
||||||
|
result = format("(`hello world`)")
|
||||||
|
assert result == "(\e[36mhello world\e[0m)\n\e[0m"
|
||||||
|
result = format("<`hello world`>")
|
||||||
|
assert result == "<\e[36mhello world\e[0m>\n\e[0m"
|
||||||
|
|
||||||
|
result = format("(*hello world*)")
|
||||||
|
assert result == "(\e[1mhello world\e[0m)\n\e[0m"
|
||||||
|
result = format("@*hello world*@")
|
||||||
|
assert result == "@\e[1mhello world\e[0m@\n\e[0m"
|
||||||
|
|
||||||
|
result = format("(_hello world_)")
|
||||||
|
assert result == "(\e[4mhello world\e[0m)\n\e[0m"
|
||||||
|
result = format("'_hello world_'")
|
||||||
|
assert result == "'\e[4mhello world\e[0m'\n\e[0m"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "star/underscore/backtick starts/ends within a word doesn't get interpreted" do
|
||||||
|
result = format("foo_bar, foo_bar_baz!")
|
||||||
|
assert result == "foo_bar, foo_bar_baz!\n\e[0m"
|
||||||
|
|
||||||
|
result = format("_foo_bar")
|
||||||
|
assert result == "_foo_bar\n\e[0m"
|
||||||
|
|
||||||
|
result = format("foo_bar_")
|
||||||
|
assert result == "foo_bar_\n\e[0m"
|
||||||
|
|
||||||
|
result = format("foo*bar, foo*bar*baz!")
|
||||||
|
assert result == "foo*bar, foo*bar*baz!\n\e[0m"
|
||||||
|
|
||||||
|
result = format("*foo*bar")
|
||||||
|
assert result == "*foo*bar\n\e[0m"
|
||||||
|
|
||||||
|
result = format("foo*bar*")
|
||||||
|
assert result == "foo*bar*\n\e[0m"
|
||||||
|
end
|
||||||
|
|
||||||
test "backtick preceeded by space gets interpreted" do
|
test "backtick preceeded by space gets interpreted" do
|
||||||
result = format("`unit `size")
|
result = format("`unit `size")
|
||||||
assert result == "\e[36munit \e[0msize\n\e[0m"
|
assert result == "\e[36munit \e[0msize\n\e[0m"
|
||||||
@@ -200,11 +262,6 @@ defmodule IO.ANSI.DocsTest do
|
|||||||
assert result == "\e[36m__world__\e[0m\n\e[0m"
|
assert result == "\e[36m__world__\e[0m\n\e[0m"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "backtick works inside parenthesis" do
|
|
||||||
result = format("(`hello world`)")
|
|
||||||
assert result == "(\e[36mhello world\e[0m)\n\e[0m"
|
|
||||||
end
|
|
||||||
|
|
||||||
test "escaping of underlines within links" do
|
test "escaping of underlines within links" do
|
||||||
result = format("(http://en.wikipedia.org/wiki/ANSI_escape_code)")
|
result = format("(http://en.wikipedia.org/wiki/ANSI_escape_code)")
|
||||||
assert result == "(http://en.wikipedia.org/wiki/ANSI_escape_code)\n\e[0m"
|
assert result == "(http://en.wikipedia.org/wiki/ANSI_escape_code)\n\e[0m"
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ defmodule IOTest do
|
|||||||
assert File.close(file) == :ok
|
assert File.close(file) == :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test :binread_all do
|
||||||
|
{:ok, file} = File.open(Path.expand('fixtures/file.bin', __DIR__))
|
||||||
|
assert "LF\nCR\rCRLF\r\nLFCR\n\r" == IO.binread(file, :all)
|
||||||
|
assert File.close(file) == :ok
|
||||||
|
end
|
||||||
|
|
||||||
test :getn do
|
test :getn do
|
||||||
{:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__))
|
{:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__))
|
||||||
assert "F" == IO.getn(file, "")
|
assert "F" == IO.getn(file, "")
|
||||||
|
|||||||
@@ -151,6 +151,12 @@ defmodule Kernel.ComprehensionTest do
|
|||||||
assert for({_,x} <- [1, 2, a: 3, b: 4, c: 5], do: x * 2) == [6, 8, 10]
|
assert for({_,x} <- [1, 2, a: 3, b: 4, c: 5], do: x * 2) == [6, 8, 10]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "list for comprehension matched to '_' on last line of block" do
|
||||||
|
assert (if true do
|
||||||
|
_ = for x <- [1, 2, 3], do: x * 2
|
||||||
|
end) == [2, 4, 6]
|
||||||
|
end
|
||||||
|
|
||||||
test "list for comprehensions with filters" do
|
test "list for comprehensions with filters" do
|
||||||
assert for(x <- [1, 2, 3], x > 1, x < 3, do: x * 2) == [4]
|
assert for(x <- [1, 2, 3], x > 1, x < 3, do: x * 2) == [4]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ defmodule Kernel.ErrorsTest do
|
|||||||
"nofile:1: syntax error before: \"world\"",
|
"nofile:1: syntax error before: \"world\"",
|
||||||
'"hello" "world"'
|
'"hello" "world"'
|
||||||
|
|
||||||
|
assert_compile_fail SyntaxError,
|
||||||
|
"nofile:1: syntax error before: Foobar",
|
||||||
|
'1 Foobar'
|
||||||
|
|
||||||
assert_compile_fail SyntaxError,
|
assert_compile_fail SyntaxError,
|
||||||
"nofile:1: syntax error before: foo",
|
"nofile:1: syntax error before: foo",
|
||||||
'Foo.:foo'
|
'Foo.:foo'
|
||||||
@@ -85,6 +89,12 @@ defmodule Kernel.ErrorsTest do
|
|||||||
'+.foo'
|
'+.foo'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test :syntax_error_before_sigil do
|
||||||
|
assert_compile_fail SyntaxError,
|
||||||
|
"nofile:1: syntax error before: sigil ~s with content 'bar baz'",
|
||||||
|
'~s(foo) ~s(bar baz)'
|
||||||
|
end
|
||||||
|
|
||||||
test :compile_error_on_op_ambiguity do
|
test :compile_error_on_op_ambiguity do
|
||||||
msg = "nofile:1: \"a -1\" looks like a function call but there is a variable named \"a\", " <>
|
msg = "nofile:1: \"a -1\" looks like a function call but there is a variable named \"a\", " <>
|
||||||
"please use explicit parenthesis or even spaces"
|
"please use explicit parenthesis or even spaces"
|
||||||
@@ -227,6 +237,10 @@ defmodule Kernel.ErrorsTest do
|
|||||||
assert_compile_fail SyntaxError,
|
assert_compile_fail SyntaxError,
|
||||||
"nofile:1: syntax error before: '{'",
|
"nofile:1: syntax error before: '{'",
|
||||||
'%{:a, :b}{a: :b}'
|
'%{:a, :b}{a: :b}'
|
||||||
|
|
||||||
|
assert_compile_fail CompileError,
|
||||||
|
"nofile:1: expected key-value pairs in a map, got: put_in(foo.bar().baz(), nil)",
|
||||||
|
'foo = 1; %{put_in(foo.bar.baz, nil), :bar}'
|
||||||
end
|
end
|
||||||
|
|
||||||
test :struct_fields_on_defstruct do
|
test :struct_fields_on_defstruct do
|
||||||
@@ -320,6 +334,13 @@ defmodule Kernel.ErrorsTest do
|
|||||||
assert_compile_fail CompileError,
|
assert_compile_fail CompileError,
|
||||||
"nofile:1: invalid arguments for quote",
|
"nofile:1: invalid arguments for quote",
|
||||||
'quote 1'
|
'quote 1'
|
||||||
|
assert_compile_fail CompileError,
|
||||||
|
"nofile:1: invalid options for quote, expected a keyword list",
|
||||||
|
'''
|
||||||
|
quote :foo do
|
||||||
|
foo
|
||||||
|
end
|
||||||
|
'''
|
||||||
end
|
end
|
||||||
|
|
||||||
test :invalid_calls do
|
test :invalid_calls do
|
||||||
@@ -735,8 +756,13 @@ defmodule Kernel.ErrorsTest do
|
|||||||
end
|
end
|
||||||
'''
|
'''
|
||||||
|
|
||||||
assert_compile_fail CompileError,
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
"nofile:2: spec for undefined function ErrorsTest.omg/0",
|
message = "nofile:2: spec for undefined function omg/0"
|
||||||
|
else
|
||||||
|
message = "nofile:2: spec for undefined function ErrorsTest.omg/0"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_compile_fail CompileError, message,
|
||||||
'''
|
'''
|
||||||
defmodule ErrorsTest do
|
defmodule ErrorsTest do
|
||||||
@spec omg :: atom
|
@spec omg :: atom
|
||||||
|
|||||||
@@ -392,11 +392,6 @@ defmodule Kernel.ExpansionTest do
|
|||||||
|
|
||||||
## Try
|
## Try
|
||||||
|
|
||||||
test "try: expands do" do
|
|
||||||
assert expand(quote do: (try do x = y end; x)) ==
|
|
||||||
quote do: (try do x = y() end; x())
|
|
||||||
end
|
|
||||||
|
|
||||||
test "try: expands catch" do
|
test "try: expands catch" do
|
||||||
assert expand(quote do: (try do x catch x, y -> z = :erlang.+(x, y) end; z)) ==
|
assert expand(quote do: (try do x catch x, y -> z = :erlang.+(x, y) end; z)) ==
|
||||||
quote do: (try do x() catch x, y -> z = :erlang.+(x, y) end; z())
|
quote do: (try do x() catch x, y -> z = :erlang.+(x, y) end; z())
|
||||||
@@ -417,6 +412,12 @@ defmodule Kernel.ExpansionTest do
|
|||||||
quote do: (try do x() rescue unquote(:in)(x, _) -> x; unquote(:in)(_, [:"Elixir.Error"]) -> x() end; x())
|
quote do: (try do x() rescue unquote(:in)(x, _) -> x; unquote(:in)(_, [:"Elixir.Error"]) -> x() end; x())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "try: expects more than do" do
|
||||||
|
assert_raise CompileError, ~r"missing catch/rescue/after/else keyword in try", fn ->
|
||||||
|
expand(quote do: (try do x = y end; x))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
## Binaries
|
## Binaries
|
||||||
|
|
||||||
test "bitstrings: size * unit" do
|
test "bitstrings: size * unit" do
|
||||||
|
|||||||
@@ -304,6 +304,36 @@ defmodule Kernel.RaiseTest do
|
|||||||
assert result == "no match of right hand side value: 0"
|
assert result == "no match of right hand side value: 0"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
|
test :bad_key_error do
|
||||||
|
result = try do
|
||||||
|
%{%{} | foo: :bar}
|
||||||
|
rescue
|
||||||
|
x in [KeyError] -> Exception.message(x)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert result == "key :foo not found"
|
||||||
|
|
||||||
|
result = try do
|
||||||
|
%{}.foo
|
||||||
|
rescue
|
||||||
|
x in [KeyError] -> Exception.message(x)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert result == "key :foo not found in: %{}"
|
||||||
|
end
|
||||||
|
|
||||||
|
test :bad_map_error do
|
||||||
|
result = try do
|
||||||
|
%{zero(0) | foo: :bar}
|
||||||
|
rescue
|
||||||
|
x in [BadMapError] -> Exception.message(x)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert result == "expected a map, got: 0"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test :case_clause_error do
|
test :case_clause_error do
|
||||||
x = :example
|
x = :example
|
||||||
result = try do
|
result = try do
|
||||||
|
|||||||
@@ -154,15 +154,6 @@ defmodule Kernel.TypespecTest do
|
|||||||
types(module)
|
types(module)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "@type with a range" do
|
|
||||||
module = test_module do
|
|
||||||
@type mytype :: range(1, 10)
|
|
||||||
end
|
|
||||||
|
|
||||||
assert [type: {:mytype, {:type, _, :range, [{:integer, _, 1}, {:integer, _, 10}]}, []}] =
|
|
||||||
types(module)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "@type with a range op" do
|
test "@type with a range op" do
|
||||||
module = test_module do
|
module = test_module do
|
||||||
@type mytype :: 1..10
|
@type mytype :: 1..10
|
||||||
@@ -177,11 +168,19 @@ defmodule Kernel.TypespecTest do
|
|||||||
@type mytype :: %{hello: :world}
|
@type mytype :: %{hello: :world}
|
||||||
end
|
end
|
||||||
|
|
||||||
assert [type: {:mytype,
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
{:type, _, :map, [
|
assert [type: {:mytype,
|
||||||
{:type, _, :map_field_assoc, {:atom, _, :hello}, {:atom, _, :world}}
|
{:type, _, :map, [
|
||||||
]},
|
{:type, _, :map_field_assoc, [{:atom, _, :hello}, {:atom, _, :world}]}
|
||||||
[]}] = types(module)
|
]},
|
||||||
|
[]}] = types(module)
|
||||||
|
else
|
||||||
|
assert [type: {:mytype,
|
||||||
|
{:type, _, :map, [
|
||||||
|
{:type, _, :map_field_assoc, {:atom, _, :hello}, {:atom, _, :world}}
|
||||||
|
]},
|
||||||
|
[]}] = types(module)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "@type with a struct" do
|
test "@type with a struct" do
|
||||||
@@ -190,13 +189,23 @@ defmodule Kernel.TypespecTest do
|
|||||||
@type mytype :: %TestTypespec{hello: :world}
|
@type mytype :: %TestTypespec{hello: :world}
|
||||||
end
|
end
|
||||||
|
|
||||||
assert [type: {:mytype,
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
{:type, _, :map, [
|
assert [type: {:mytype,
|
||||||
{:type, _, :map_field_assoc, {:atom, _, :__struct__}, {:atom, _, TestTypespec}},
|
{:type, _, :map, [
|
||||||
{:type, _, :map_field_assoc, {:atom, _, :hello}, {:atom, _, :world}},
|
{:type, _, :map_field_assoc, [{:atom, _, :__struct__}, {:atom, _, TestTypespec}]},
|
||||||
{:type, _, :map_field_assoc, {:atom, _, :other}, {:type, _, :term, []}}
|
{:type, _, :map_field_assoc, [{:atom, _, :hello}, {:atom, _, :world}]},
|
||||||
]},
|
{:type, _, :map_field_assoc, [{:atom, _, :other}, {:type, _, :term, []}]}
|
||||||
[]}] = types(module)
|
]},
|
||||||
|
[]}] = types(module)
|
||||||
|
else
|
||||||
|
assert [type: {:mytype,
|
||||||
|
{:type, _, :map, [
|
||||||
|
{:type, _, :map_field_assoc, {:atom, _, :__struct__}, {:atom, _, TestTypespec}},
|
||||||
|
{:type, _, :map_field_assoc, {:atom, _, :hello}, {:atom, _, :world}},
|
||||||
|
{:type, _, :map_field_assoc, {:atom, _, :other}, {:type, _, :term, []}}
|
||||||
|
]},
|
||||||
|
[]}] = types(module)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "@type with undefined struct" do
|
test "@type with undefined struct" do
|
||||||
|
|||||||
@@ -375,6 +375,20 @@ defmodule Kernel.WarningTest do
|
|||||||
purge Sample
|
purge Sample
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test :badarg_warning do
|
||||||
|
assert capture_err(fn ->
|
||||||
|
assert_raise ArgumentError, fn ->
|
||||||
|
Code.eval_string """
|
||||||
|
defmodule Sample do
|
||||||
|
Atom.to_string "abc"
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end) =~ "warning: this expression will fail with ArgumentError"
|
||||||
|
after
|
||||||
|
purge [Sample]
|
||||||
|
end
|
||||||
|
|
||||||
test :undefined_function_for_behaviour do
|
test :undefined_function_for_behaviour do
|
||||||
assert capture_err(fn ->
|
assert capture_err(fn ->
|
||||||
Code.eval_string """
|
Code.eval_string """
|
||||||
|
|||||||
@@ -6,11 +6,25 @@ defmodule KernelTest do
|
|||||||
test "=~/2" do
|
test "=~/2" do
|
||||||
assert ("abcd" =~ ~r/c(d)/) == true
|
assert ("abcd" =~ ~r/c(d)/) == true
|
||||||
assert ("abcd" =~ ~r/e/) == false
|
assert ("abcd" =~ ~r/e/) == false
|
||||||
|
assert ("abcd" =~ ~R/c(d)/) == true
|
||||||
|
assert ("abcd" =~ ~R/e/) == false
|
||||||
|
|
||||||
string = "^ab+cd*$"
|
string = "^ab+cd*$"
|
||||||
assert (string =~ "ab+") == true
|
assert (string =~ "ab+") == true
|
||||||
assert (string =~ "bb") == false
|
assert (string =~ "bb") == false
|
||||||
|
|
||||||
|
assert ("abcd" =~ ~r//) == true
|
||||||
|
assert ("abcd" =~ ~R//) == true
|
||||||
|
assert ("abcd" =~ "") == true
|
||||||
|
|
||||||
|
assert ("" =~ ~r//) == true
|
||||||
|
assert ("" =~ ~R//) == true
|
||||||
|
assert ("" =~ "") == true
|
||||||
|
|
||||||
|
assert ("" =~ "abcd") == false
|
||||||
|
assert ("" =~ ~r/abcd/) == false
|
||||||
|
assert ("" =~ ~R/abcd/) == false
|
||||||
|
|
||||||
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
1234 =~ "hello"
|
1234 =~ "hello"
|
||||||
end
|
end
|
||||||
@@ -18,6 +32,34 @@ defmodule KernelTest do
|
|||||||
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
1234 =~ ~r"hello"
|
1234 =~ ~r"hello"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
|
1234 =~ ~R"hello"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
|
~r"hello" =~ "hello"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
|
~r"hello" =~ ~r"hello"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
|
:abcd =~ ~r//
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
|
||||||
|
:abcd =~ ""
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Regex.match?/2", fn ->
|
||||||
|
"abcd" =~ nil
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise FunctionClauseError, "no function clause matching in Regex.match?/2", fn ->
|
||||||
|
"abcd" =~ :abcd
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "^" do
|
test "^" do
|
||||||
|
|||||||
@@ -85,8 +85,16 @@ defmodule MapTest do
|
|||||||
test "update maps" do
|
test "update maps" do
|
||||||
assert %{two_items_map | a: 3} == %{a: 3, b: 2}
|
assert %{two_items_map | a: 3} == %{a: 3, b: 2}
|
||||||
|
|
||||||
assert_raise ArgumentError, fn ->
|
# TODO: proper handling of API changes in different Erlang/OTP releases
|
||||||
%{two_items_map | c: 3}
|
case :erlang.system_info(:otp_release) do
|
||||||
|
'17' ->
|
||||||
|
assert_raise ArgumentError, fn ->
|
||||||
|
%{two_items_map | c: 3}
|
||||||
|
end
|
||||||
|
_ ->
|
||||||
|
assert_raise KeyError, fn ->
|
||||||
|
%{two_items_map | c: 3}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -121,11 +121,19 @@ defmodule ProtocolTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "protocol defines callbacks" do
|
test "protocol defines callbacks" do
|
||||||
assert get_callbacks(Sample, :ok, 1) ==
|
if :erlang.system_info(:otp_release) >= '18' do
|
||||||
[{:type, 9, :fun, [{:type, 9, :product, [{:type, 9, :t, []}]}, {:type, 9, :boolean, []}]}]
|
assert get_callbacks(Sample, :ok, 1) ==
|
||||||
|
[{:type, [9], :fun, [{:type, [9], :product, [{:user_type, [9], :t, []}]}, {:type, [9], :boolean, []}]}]
|
||||||
|
|
||||||
assert get_callbacks(WithAny, :ok, 1) ==
|
assert get_callbacks(WithAny, :ok, 1) ==
|
||||||
[{:type, 16, :fun, [{:type, 16, :product, [{:type, 16, :t, []}]}, {:type, 16, :term, []}]}]
|
[{:type, [16], :fun, [{:type, [16], :product, [{:user_type, [16], :t, []}]}, {:type, [16], :term, []}]}]
|
||||||
|
else
|
||||||
|
assert get_callbacks(Sample, :ok, 1) ==
|
||||||
|
[{:type, 9, :fun, [{:type, 9, :product, [{:type, 9, :t, []}]}, {:type, 9, :boolean, []}]}]
|
||||||
|
|
||||||
|
assert get_callbacks(WithAny, :ok, 1) ==
|
||||||
|
[{:type, 16, :fun, [{:type, 16, :product, [{:type, 16, :t, []}]}, {:type, 16, :term, []}]}]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "protocol defines attributes" do
|
test "protocol defines attributes" do
|
||||||
|
|||||||
@@ -52,7 +52,10 @@ defmodule RecordTest do
|
|||||||
|
|
||||||
Record.defrecord :timestamp, [:date, :time]
|
Record.defrecord :timestamp, [:date, :time]
|
||||||
Record.defrecord :user, __MODULE__, name: "john", age: 25
|
Record.defrecord :user, __MODULE__, name: "john", age: 25
|
||||||
Record.defrecordp :file_info, Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
Record.defrecordp :file_info,
|
||||||
|
Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
||||||
|
Record.defrecordp :certificate, :OTPCertificate,
|
||||||
|
Record.extract(:OTPCertificate, from_lib: "public_key/include/public_key.hrl")
|
||||||
|
|
||||||
test "records generates macros that generates tuples" do
|
test "records generates macros that generates tuples" do
|
||||||
record = user()
|
record = user()
|
||||||
|
|||||||
@@ -189,6 +189,10 @@ defmodule RegexTest do
|
|||||||
assert Regex.replace(~r(b), "abc", "d") == "adc"
|
assert Regex.replace(~r(b), "abc", "d") == "adc"
|
||||||
assert Regex.replace(~r(b), "abc", "[\\0]") == "a[b]c"
|
assert Regex.replace(~r(b), "abc", "[\\0]") == "a[b]c"
|
||||||
assert Regex.replace(~r[(b)], "abc", "[\\1]") == "a[b]c"
|
assert Regex.replace(~r[(b)], "abc", "[\\1]") == "a[b]c"
|
||||||
|
assert Regex.replace(~r[(b)], "abc", "[\\2]") == "a[]c"
|
||||||
|
assert Regex.replace(~r[(b)], "abc", "[\\3]") == "a[]c"
|
||||||
|
assert Regex.replace(~r(b), "abc", "[\\g{0}]") == "a[b]c"
|
||||||
|
assert Regex.replace(~r[(b)], "abc", "[\\g{1}]") == "a[b]c"
|
||||||
|
|
||||||
assert Regex.replace(~r(b), "abcbe", "d") == "adcde"
|
assert Regex.replace(~r(b), "abcbe", "d") == "adcde"
|
||||||
assert Regex.replace(~r(b), "abcbe", "d", global: false) == "adcbe"
|
assert Regex.replace(~r(b), "abcbe", "d", global: false) == "adcbe"
|
||||||
|
|||||||
@@ -67,6 +67,19 @@ defmodule StreamTest do
|
|||||||
assert Enum.zip(list, list) == Enum.zip(stream, stream)
|
assert Enum.zip(list, list) == Enum.zip(stream, stream)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "chunk/4 is haltable" do
|
||||||
|
assert 1..10 |> Stream.take(6) |> Stream.chunk(4, 4, [7, 8]) |> Enum.to_list ==
|
||||||
|
[[1, 2, 3, 4], [5, 6, 7, 8]]
|
||||||
|
assert 1..10 |> Stream.take(6) |> Stream.chunk(4, 4, [7, 8]) |> Stream.take(3) |> Enum.to_list ==
|
||||||
|
[[1, 2, 3, 4], [5, 6, 7, 8]]
|
||||||
|
assert 1..10 |> Stream.take(6) |> Stream.chunk(4, 4, [7, 8]) |> Stream.take(2) |> Enum.to_list ==
|
||||||
|
[[1, 2, 3, 4], [5, 6, 7, 8]]
|
||||||
|
assert 1..10 |> Stream.take(6) |> Stream.chunk(4, 4, [7, 8]) |> Stream.take(1) |> Enum.to_list ==
|
||||||
|
[[1, 2, 3, 4]]
|
||||||
|
assert 1..6 |> Stream.take(6) |> Stream.chunk(4, 4, [7, 8]) |> Enum.to_list ==
|
||||||
|
[[1, 2, 3, 4], [5, 6, 7, 8]]
|
||||||
|
end
|
||||||
|
|
||||||
test "chunk_by/2" do
|
test "chunk_by/2" do
|
||||||
stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
|
stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
|
||||||
|
|
||||||
@@ -75,6 +88,8 @@ defmodule StreamTest do
|
|||||||
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
|
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
|
||||||
assert stream |> Stream.take(3) |> Enum.to_list ==
|
assert stream |> Stream.take(3) |> Enum.to_list ==
|
||||||
[[1], [2, 2], [3]]
|
[[1], [2, 2], [3]]
|
||||||
|
assert 1..10 |> Stream.chunk(2) |> Enum.take(2) ==
|
||||||
|
[[1, 2], [3, 4]]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "chunk_by/2 is zippable" do
|
test "chunk_by/2 is zippable" do
|
||||||
@@ -311,11 +326,23 @@ defmodule StreamTest do
|
|||||||
assert Enum.take(stream, 5) == [1, 2, 3, 4, 3]
|
assert Enum.take(stream, 5) == [1, 2, 3, 4, 3]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "flat_map/2 properly halts both inner and outer stream when inner stream is halted" do
|
||||||
|
# Fixes a bug that, when the inner stream was done,
|
||||||
|
# sending it a halt would cause it to return the
|
||||||
|
# inner stream was halted, forcing flat_map to get
|
||||||
|
# the next value from the outer stream, evaluate it,
|
||||||
|
# get annother inner stream, just to halt it.
|
||||||
|
assert [1, 2] # 2 should never be used
|
||||||
|
|> Stream.flat_map(fn 1 -> Stream.repeatedly(fn -> 1 end) end)
|
||||||
|
|> Stream.flat_map(fn 1 -> Stream.repeatedly(fn -> 1 end) end)
|
||||||
|
|> Enum.take(1) == [1]
|
||||||
|
end
|
||||||
|
|
||||||
test "interval/1" do
|
test "interval/1" do
|
||||||
stream = Stream.interval(10)
|
stream = Stream.interval(10)
|
||||||
now = :erlang.now
|
now = :os.timestamp
|
||||||
assert Enum.take(stream, 5) == [0, 1, 2, 3, 4]
|
assert Enum.take(stream, 5) == [0, 1, 2, 3, 4]
|
||||||
assert :timer.now_diff(:erlang.now, now) > 50000
|
assert :timer.now_diff(:os.timestamp, now) > 50000
|
||||||
end
|
end
|
||||||
|
|
||||||
test "into/2 and run/1" do
|
test "into/2 and run/1" do
|
||||||
@@ -595,9 +622,9 @@ defmodule StreamTest do
|
|||||||
|
|
||||||
test "timer/1" do
|
test "timer/1" do
|
||||||
stream = Stream.timer(10)
|
stream = Stream.timer(10)
|
||||||
now = :erlang.now
|
now = :os.timestamp
|
||||||
assert Enum.to_list(stream) == [0]
|
assert Enum.to_list(stream) == [0]
|
||||||
assert :timer.now_diff(:erlang.now, now) > 10000
|
assert :timer.now_diff(:os.timestamp, now) > 10000
|
||||||
end
|
end
|
||||||
|
|
||||||
test "unfold/2" do
|
test "unfold/2" do
|
||||||
|
|||||||
@@ -3,17 +3,12 @@ Code.require_file "test_helper.exs", __DIR__
|
|||||||
defmodule StringIOTest do
|
defmodule StringIOTest do
|
||||||
use ExUnit.Case, async: true
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
test "start and stop" do
|
test "open and close" do
|
||||||
{:ok, pid} = StringIO.open("")
|
{:ok, pid} = StringIO.open("")
|
||||||
assert StringIO.close(pid) == {:ok, {"", ""}}
|
assert StringIO.close(pid) == {:ok, {"", ""}}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "start_link and stop" do
|
test "contents" do
|
||||||
{:ok, pid} = StringIO.open("")
|
|
||||||
assert StringIO.close(pid) == {:ok, {"", ""}}
|
|
||||||
end
|
|
||||||
|
|
||||||
test "peek" do
|
|
||||||
{:ok, pid} = StringIO.open("abc")
|
{:ok, pid} = StringIO.open("abc")
|
||||||
IO.write(pid, "edf")
|
IO.write(pid, "edf")
|
||||||
assert StringIO.contents(pid) == {"abc", "edf"}
|
assert StringIO.contents(pid) == {"abc", "edf"}
|
||||||
|
|||||||
@@ -135,6 +135,8 @@ defmodule StringTest do
|
|||||||
|
|
||||||
test :rstrip do
|
test :rstrip do
|
||||||
assert String.rstrip("") == ""
|
assert String.rstrip("") == ""
|
||||||
|
assert String.rstrip("1\n") == "1"
|
||||||
|
assert String.rstrip("\r\n") == ""
|
||||||
assert String.rstrip(" abc ") == " abc"
|
assert String.rstrip(" abc ") == " abc"
|
||||||
assert String.rstrip(" abc a") == " abc a"
|
assert String.rstrip(" abc a") == " abc a"
|
||||||
assert String.rstrip("a abc a\n\n") == "a abc a"
|
assert String.rstrip("a abc a\n\n") == "a abc a"
|
||||||
@@ -192,6 +194,7 @@ defmodule StringTest do
|
|||||||
assert String.reverse("Hello ∂og") == "go∂ olleH"
|
assert String.reverse("Hello ∂og") == "go∂ olleH"
|
||||||
assert String.reverse("Ā̀stute") == "etutsĀ̀"
|
assert String.reverse("Ā̀stute") == "etutsĀ̀"
|
||||||
assert String.reverse(String.reverse("Hello World")) == "Hello World"
|
assert String.reverse(String.reverse("Hello World")) == "Hello World"
|
||||||
|
assert String.reverse(String.reverse("Hello \r\n World")) == "Hello \r\n World"
|
||||||
end
|
end
|
||||||
|
|
||||||
test :replace do
|
test :replace do
|
||||||
@@ -238,7 +241,7 @@ defmodule StringTest do
|
|||||||
# Extended
|
# Extended
|
||||||
assert String.graphemes("Ā̀stute") == ["Ā̀", "s", "t", "u", "t", "e"]
|
assert String.graphemes("Ā̀stute") == ["Ā̀", "s", "t", "u", "t", "e"]
|
||||||
# CLRF
|
# CLRF
|
||||||
assert String.graphemes("\n\r\f") == ["\n\r", "\f"]
|
assert String.graphemes("\r\n\f") == ["\r\n", "\f"]
|
||||||
# Regional indicator
|
# Regional indicator
|
||||||
assert String.graphemes("\x{1F1E6}\x{1F1E7}\x{1F1E8}") == ["\x{1F1E6}\x{1F1E7}\x{1F1E8}"]
|
assert String.graphemes("\x{1F1E6}\x{1F1E7}\x{1F1E8}") == ["\x{1F1E6}\x{1F1E7}\x{1F1E8}"]
|
||||||
# Hangul
|
# Hangul
|
||||||
|
|||||||
@@ -65,6 +65,16 @@ defmodule SystemTest do
|
|||||||
opts = [into: [], cd: System.cwd!, env: %{"foo" => "bar"},
|
opts = [into: [], cd: System.cwd!, env: %{"foo" => "bar"},
|
||||||
arg0: "hecho", stderr_to_stdout: true, parallelism: true]
|
arg0: "hecho", stderr_to_stdout: true, parallelism: true]
|
||||||
assert {["hello\n"], 0} = System.cmd "echo", ["hello"], opts
|
assert {["hello\n"], 0} = System.cmd "echo", ["hello"], opts
|
||||||
|
|
||||||
|
with_tmp_dir(fn dir ->
|
||||||
|
new_path = Path.join([dir, "echo2"])
|
||||||
|
File.cp!(System.find_executable("echo"), new_path)
|
||||||
|
assert :enoent = catch_error(System.cmd(new_path, ["hello"]))
|
||||||
|
|
||||||
|
File.cd!(dir)
|
||||||
|
assert :enoent = catch_error(System.cmd("echo2", ["hello"]))
|
||||||
|
assert {"hello\n", 0} = System.cmd(Path.join([System.cwd, "echo2"]), ["hello"])
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "find_executable/1" do
|
test "find_executable/1" do
|
||||||
@@ -72,4 +82,17 @@ defmodule SystemTest do
|
|||||||
assert is_binary System.find_executable("erl")
|
assert is_binary System.find_executable("erl")
|
||||||
assert !System.find_executable("does-not-really-exist-from-elixir")
|
assert !System.find_executable("does-not-really-exist-from-elixir")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp with_tmp_dir(function) do
|
||||||
|
dir = tmp_dir_name()
|
||||||
|
tmp_dir = System.tmp_dir!
|
||||||
|
tmp_path = Path.join [tmp_dir, dir]
|
||||||
|
File.rm_rf! tmp_path
|
||||||
|
File.mkdir_p! tmp_path
|
||||||
|
File.cd! tmp_dir, fn -> function.(dir) end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp tmp_dir_name do
|
||||||
|
:crypto.rand_bytes(4) |> Base.encode16
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -142,17 +142,25 @@ defmodule URITest do
|
|||||||
|
|
||||||
test :default_port do
|
test :default_port do
|
||||||
assert URI.default_port("http") == 80
|
assert URI.default_port("http") == 80
|
||||||
assert URI.default_port("unknown") == nil
|
try do
|
||||||
|
URI.default_port("http", 8000)
|
||||||
|
assert URI.default_port("http") == 8000
|
||||||
|
after
|
||||||
|
URI.default_port("http", 80)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert URI.default_port("unknown") == nil
|
||||||
URI.default_port("unknown", 13)
|
URI.default_port("unknown", 13)
|
||||||
assert URI.default_port("unknown") == 13
|
assert URI.default_port("unknown") == 13
|
||||||
end
|
end
|
||||||
|
|
||||||
test :parse_bad_uris do
|
test :parse_bad_uris do
|
||||||
assert URI.parse("https:??@?F?@#>F//23/")
|
|
||||||
assert URI.parse("")
|
assert URI.parse("")
|
||||||
assert URI.parse(":https")
|
assert URI.parse("https:??@?F?@#>F//23/")
|
||||||
assert URI.parse("https")
|
|
||||||
|
assert URI.parse(":https").path == ":https"
|
||||||
|
assert URI.parse("https").path == "https"
|
||||||
|
assert URI.parse("ht\0tps://foo.com").path == "ht\0tps://foo.com"
|
||||||
end
|
end
|
||||||
|
|
||||||
test :ipv6_addresses do
|
test :ipv6_addresses do
|
||||||
|
|||||||
@@ -72,6 +72,8 @@ defmodule VersionTest do
|
|||||||
assert V.parse("1.0.0-dev") |> elem(1) |> to_string == "1.0.0-dev"
|
assert V.parse("1.0.0-dev") |> elem(1) |> to_string == "1.0.0-dev"
|
||||||
assert V.parse("1.0.0+lol") |> elem(1) |> to_string == "1.0.0+lol"
|
assert V.parse("1.0.0+lol") |> elem(1) |> to_string == "1.0.0+lol"
|
||||||
assert V.parse("1.0.0-dev+lol") |> elem(1) |> to_string == "1.0.0-dev+lol"
|
assert V.parse("1.0.0-dev+lol") |> elem(1) |> to_string == "1.0.0-dev+lol"
|
||||||
|
assert V.parse("1.0.0-0") |> elem(1) |> to_string == "1.0.0-0"
|
||||||
|
assert V.parse("1.0.0-rc.0") |> elem(1) |> to_string == "1.0.0-rc.0"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "invalid match" do
|
test "invalid match" do
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ defmodule String.Unicode do
|
|||||||
data_path = Path.join(__DIR__, "UnicodeData.txt")
|
data_path = Path.join(__DIR__, "UnicodeData.txt")
|
||||||
|
|
||||||
{codes, whitespace} = Enum.reduce File.stream!(data_path), {[], []}, fn(line, {cacc, wacc}) ->
|
{codes, whitespace} = Enum.reduce File.stream!(data_path), {[], []}, fn(line, {cacc, wacc}) ->
|
||||||
[ codepoint, _name, _category,
|
[codepoint, _name, _category,
|
||||||
_class, bidi, _decomposition,
|
_class, bidi, _decomposition,
|
||||||
_numeric_1, _numeric_2, _numeric_3,
|
_numeric_1, _numeric_2, _numeric_3,
|
||||||
_bidi_mirror, _unicode_1, _iso,
|
_bidi_mirror, _unicode_1, _iso,
|
||||||
upper, lower, title ] = :binary.split(line, ";", [:global])
|
upper, lower, title] = :binary.split(line, ";", [:global])
|
||||||
|
|
||||||
title = :binary.part(title, 0, byte_size(title) - 1)
|
title = :binary.part(title, 0, byte_size(title) - 1)
|
||||||
|
|
||||||
@@ -36,42 +36,42 @@ defmodule String.Unicode do
|
|||||||
special_path = Path.join(__DIR__, "SpecialCasing.txt")
|
special_path = Path.join(__DIR__, "SpecialCasing.txt")
|
||||||
|
|
||||||
codes = Enum.reduce File.stream!(special_path), codes, fn(line, acc) ->
|
codes = Enum.reduce File.stream!(special_path), codes, fn(line, acc) ->
|
||||||
[ codepoint, lower, title, upper, _comment ] = :binary.split(line, "; ", [:global])
|
[codepoint, lower, title, upper, _comment] = :binary.split(line, "; ", [:global])
|
||||||
key = to_binary.(codepoint)
|
key = to_binary.(codepoint)
|
||||||
:lists.keystore(key, 1, acc, {key, to_binary.(upper), to_binary.(lower), to_binary.(title)})
|
:lists.keystore(key, 1, acc, {key, to_binary.(upper), to_binary.(lower), to_binary.(title)})
|
||||||
end
|
end
|
||||||
|
|
||||||
# Downcase
|
# Downcase
|
||||||
|
|
||||||
def downcase(string), do: do_downcase(string) |> IO.iodata_to_binary
|
def downcase(string), do: downcase(string, "")
|
||||||
|
|
||||||
for {codepoint, _upper, lower, _title} <- codes, lower && lower != codepoint do
|
for {codepoint, _upper, lower, _title} <- codes, lower && lower != codepoint do
|
||||||
defp do_downcase(unquote(codepoint) <> rest) do
|
defp downcase(unquote(codepoint) <> rest, acc) do
|
||||||
unquote(:binary.bin_to_list(lower)) ++ downcase(rest)
|
downcase(rest, acc <> unquote(lower))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_downcase(<< char, rest :: binary >>) do
|
defp downcase(<<char, rest :: binary>>, acc) do
|
||||||
[char|do_downcase(rest)]
|
downcase(rest, <<acc::binary, char>>)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_downcase(""), do: []
|
defp downcase("", acc), do: acc
|
||||||
|
|
||||||
# Upcase
|
# Upcase
|
||||||
|
|
||||||
def upcase(string), do: do_upcase(string) |> IO.iodata_to_binary
|
def upcase(string), do: upcase(string, "")
|
||||||
|
|
||||||
for {codepoint, upper, _lower, _title} <- codes, upper && upper != codepoint do
|
for {codepoint, upper, _lower, _title} <- codes, upper && upper != codepoint do
|
||||||
defp do_upcase(unquote(codepoint) <> rest) do
|
defp upcase(unquote(codepoint) <> rest, acc) do
|
||||||
unquote(:binary.bin_to_list(upper)) ++ do_upcase(rest)
|
upcase(rest, acc <> unquote(upper))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_upcase(<< char, rest :: binary >>) do
|
defp upcase(<<char, rest :: binary>>, acc) do
|
||||||
[char|do_upcase(rest)]
|
upcase(rest, <<acc::binary, char>>)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_upcase(""), do: []
|
defp upcase("", acc), do: acc
|
||||||
|
|
||||||
# Titlecase once
|
# Titlecase once
|
||||||
|
|
||||||
@@ -88,7 +88,6 @@ defmodule String.Unicode do
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Strip
|
# Strip
|
||||||
|
|
||||||
def lstrip(""), do: ""
|
def lstrip(""), do: ""
|
||||||
|
|
||||||
for codepoint <- whitespace do
|
for codepoint <- whitespace do
|
||||||
@@ -99,27 +98,45 @@ defmodule String.Unicode do
|
|||||||
|
|
||||||
def lstrip(other) when is_binary(other), do: other
|
def lstrip(other) when is_binary(other), do: other
|
||||||
|
|
||||||
def rstrip(string) when is_binary(string) do
|
@whitespace_max_size 3
|
||||||
do_rstrip(string, [], [])
|
|
||||||
end
|
|
||||||
|
|
||||||
for codepoint <- whitespace do
|
for codepoint <- whitespace do
|
||||||
c = :binary.bin_to_list(codepoint) |> :lists.reverse
|
# We need to increment @whitespace_max_size as well
|
||||||
|
# as the small table (_s) if we add a new entry here.
|
||||||
|
case byte_size(codepoint) do
|
||||||
|
3 ->
|
||||||
|
defp do_rstrip_l(unquote(codepoint)), do: -3
|
||||||
|
2 ->
|
||||||
|
defp do_rstrip_l(<<_, unquote(codepoint)>>), do: -2
|
||||||
|
|
||||||
defp do_rstrip(unquote(codepoint) <> rest, acc1, acc2) do
|
defp do_rstrip_s(unquote(codepoint)), do: <<>>
|
||||||
do_rstrip(rest, unquote(c) ++ (acc1 || acc2), acc2)
|
1 ->
|
||||||
|
defp do_rstrip_l(<<unquote(codepoint), unquote(codepoint), unquote(codepoint)>>), do: -3
|
||||||
|
defp do_rstrip_l(<<_, unquote(codepoint), unquote(codepoint)>>), do: -2
|
||||||
|
defp do_rstrip_l(<<_, _, unquote(codepoint)>>), do: -1
|
||||||
|
|
||||||
|
defp do_rstrip_s(<<x, unquote(codepoint)>>), do: do_rstrip_s(<<x>>)
|
||||||
|
defp do_rstrip_s(unquote(codepoint)), do: <<>>
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_rstrip(<< char, rest :: binary >>, nil, acc2) do
|
defp do_rstrip_l(_), do: 0
|
||||||
do_rstrip(rest, nil, [char|acc2])
|
defp do_rstrip_s(o), do: o
|
||||||
|
|
||||||
|
def rstrip(string) when is_binary(string) do
|
||||||
|
rstrip(string, byte_size(string))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_rstrip(<< char, rest :: binary >>, acc1, _acc2) do
|
defp rstrip(string, size) when size < @whitespace_max_size do
|
||||||
do_rstrip(rest, nil, [char|acc1])
|
do_rstrip_s(string)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_rstrip(<<>>, _acc1, acc2), do: acc2 |> :lists.reverse |> IO.iodata_to_binary
|
defp rstrip(string, size) do
|
||||||
|
trail = binary_part(string, size, -@whitespace_max_size)
|
||||||
|
case do_rstrip_l(trail) do
|
||||||
|
0 -> string
|
||||||
|
x -> rstrip(binary_part(string, 0, size + x), size + x)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Split
|
# Split
|
||||||
|
|
||||||
@@ -190,7 +207,7 @@ defmodule String.Graphemes do
|
|||||||
end
|
end
|
||||||
|
|
||||||
cluster = Enum.reduce File.stream!(cluster_path), HashDict.new, fn(line, dict) ->
|
cluster = Enum.reduce File.stream!(cluster_path), HashDict.new, fn(line, dict) ->
|
||||||
[ _full, first, last, class ] = Regex.run(regex, line)
|
[_full, first, last, class] = Regex.run(regex, line)
|
||||||
|
|
||||||
# Skip surrogates
|
# Skip surrogates
|
||||||
if first == "D800" and last == "DFFF" do
|
if first == "D800" and last == "DFFF" do
|
||||||
@@ -208,8 +225,8 @@ defmodule String.Graphemes do
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Don't break CRLF
|
# Don't break CRLF
|
||||||
def next_grapheme(<< ?\n, ?\r, rest :: binary >>) do
|
def next_grapheme(<< ?\r, ?\n, rest :: binary >>) do
|
||||||
{"\n\r", rest}
|
{"\r\n", rest}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Break on control
|
# Break on control
|
||||||
|
|||||||
@@ -207,26 +207,16 @@ defmodule ExUnit.Assertions do
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc false
|
||||||
Asserts `value` is true.
|
|
||||||
If it fails, it raises an expectation error
|
|
||||||
using the given `left` and `right` values.
|
|
||||||
|
|
||||||
You probably don't need to use this—the regular `assert` function
|
|
||||||
handles this for you.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
assert this > that, this, that, "more than"
|
|
||||||
|
|
||||||
"""
|
|
||||||
def assert(value, left, right, message) when is_binary(message) do
|
def assert(value, left, right, message) when is_binary(message) do
|
||||||
assert(value, left: left, right: right, message: message)
|
assert(value, left: left, right: right, message: message)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Asserts a message was or is going to be received. Unlike
|
Asserts a message was or is going to be received.
|
||||||
`assert_received`, it has a default timeout of 100 milliseconds.
|
|
||||||
|
Unlike `assert_received`, it has a default timeout
|
||||||
|
of 100 milliseconds.
|
||||||
|
|
||||||
The `expected` argument is a pattern.
|
The `expected` argument is a pattern.
|
||||||
|
|
||||||
@@ -460,8 +450,6 @@ defmodule ExUnit.Assertions do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
refute_receive message, timeout \\ 100, message \\ nil
|
|
||||||
|
|
||||||
Asserts `message` was not received (and won't be received) within
|
Asserts `message` was not received (and won't be received) within
|
||||||
the `timeout` period.
|
the `timeout` period.
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,17 @@ defmodule ExUnit.CaptureIO do
|
|||||||
|
|
||||||
import ExUnit.CaptureIO
|
import ExUnit.CaptureIO
|
||||||
|
|
||||||
test :example do
|
test "checking the return value and the IO output" do
|
||||||
assert capture_io(fn ->
|
fun = fn ->
|
||||||
IO.puts "a"
|
assert Enum.each(["some", "example"], &(IO.puts &1)) == :ok
|
||||||
end) == "a\n"
|
end
|
||||||
|
|
||||||
|
assert capture_io(fun) == "some\nexample\n"
|
||||||
|
|
||||||
|
# Or use only: `capture_io(fun)` to silence the
|
||||||
|
# IO output (so only assert the return value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -91,15 +95,13 @@ defmodule ExUnit.CaptureIO do
|
|||||||
prompt_config = Keyword.get(options, :capture_prompt, true)
|
prompt_config = Keyword.get(options, :capture_prompt, true)
|
||||||
input = Keyword.get(options, :input, "")
|
input = Keyword.get(options, :input, "")
|
||||||
|
|
||||||
original_gl = :erlang.group_leader
|
original_gl = Process.group_leader()
|
||||||
{:ok, capture_gl} = StringIO.open(input, capture_prompt: prompt_config)
|
{:ok, capture_gl} = StringIO.open(input, capture_prompt: prompt_config)
|
||||||
:erlang.group_leader(capture_gl, self)
|
|
||||||
|
|
||||||
try do
|
try do
|
||||||
fun.()
|
Process.group_leader(self(), capture_gl)
|
||||||
StringIO.close(capture_gl) |> elem(1) |> elem(1)
|
do_capture_io(capture_gl, fun)
|
||||||
after
|
after
|
||||||
:erlang.group_leader(original_gl, self)
|
Process.group_leader(self(), original_gl)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -119,8 +121,7 @@ defmodule ExUnit.CaptureIO do
|
|||||||
Process.register(capture_io, device)
|
Process.register(capture_io, device)
|
||||||
|
|
||||||
try do
|
try do
|
||||||
fun.()
|
do_capture_io(capture_io, fun)
|
||||||
StringIO.close(capture_io) |> elem(1) |> elem(1)
|
|
||||||
after
|
after
|
||||||
try do
|
try do
|
||||||
Process.unregister(device)
|
Process.unregister(device)
|
||||||
@@ -131,4 +132,20 @@ defmodule ExUnit.CaptureIO do
|
|||||||
ExUnit.Server.remove_device(device)
|
ExUnit.Server.remove_device(device)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_capture_io(string_io, fun) do
|
||||||
|
try do
|
||||||
|
_ = fun.()
|
||||||
|
:ok
|
||||||
|
catch
|
||||||
|
kind, reason ->
|
||||||
|
stack = System.stacktrace()
|
||||||
|
_ = StringIO.close(string_io)
|
||||||
|
:erlang.raise(kind, reason, stack)
|
||||||
|
else
|
||||||
|
:ok ->
|
||||||
|
{:ok, output} = StringIO.close(string_io)
|
||||||
|
elem(output, 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -162,15 +162,17 @@ defmodule ExUnit.Case do
|
|||||||
|
|
||||||
quote do
|
quote do
|
||||||
unless Module.get_attribute(__MODULE__, :ex_unit_tests) do
|
unless Module.get_attribute(__MODULE__, :ex_unit_tests) do
|
||||||
if unquote(async) do
|
|
||||||
ExUnit.Server.add_async_case(__MODULE__)
|
|
||||||
else
|
|
||||||
ExUnit.Server.add_sync_case(__MODULE__)
|
|
||||||
end
|
|
||||||
|
|
||||||
Enum.each [:ex_unit_tests, :tag, :moduletag],
|
Enum.each [:ex_unit_tests, :tag, :moduletag],
|
||||||
&Module.register_attribute(__MODULE__, &1, accumulate: true)
|
&Module.register_attribute(__MODULE__, &1, accumulate: true)
|
||||||
|
|
||||||
|
if unquote(async) do
|
||||||
|
@moduletag async: true
|
||||||
|
ExUnit.Server.add_async_case(__MODULE__)
|
||||||
|
else
|
||||||
|
@moduletag async: false
|
||||||
|
ExUnit.Server.add_sync_case(__MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
@before_compile ExUnit.Case
|
@before_compile ExUnit.Case
|
||||||
use ExUnit.Callbacks
|
use ExUnit.Callbacks
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,23 +9,23 @@ defmodule ExUnit.Formatter do
|
|||||||
The following events are possible:
|
The following events are possible:
|
||||||
|
|
||||||
* `{:suite_started, opts}` -
|
* `{:suite_started, opts}` -
|
||||||
the suite has started with the specified options to the runner.
|
the suite has started with the specified options to the runner.
|
||||||
|
|
||||||
* `{:suite_finished, run_us, load_us}` -
|
* `{:suite_finished, run_us, load_us}` -
|
||||||
the suite has finished. `run_us` and `load_us` are the run and load
|
the suite has finished. `run_us` and `load_us` are the run and load
|
||||||
times in microseconds respectively.
|
times in microseconds respectively.
|
||||||
|
|
||||||
* `{:case_started, test_case}` -
|
* `{:case_started, test_case}` -
|
||||||
a test case has started. See `ExUnit.TestCase` for details.
|
a test case has started. See `ExUnit.TestCase` for details.
|
||||||
|
|
||||||
* `{:case_finished, test_case}` -
|
* `{:case_finished, test_case}` -
|
||||||
a test case has finished. See `ExUnit.TestCase` for details.
|
a test case has finished. See `ExUnit.TestCase` for details.
|
||||||
|
|
||||||
* `{:test_started, test_case}` -
|
* `{:test_started, test_case}` -
|
||||||
a test case has started. See `ExUnit.Test` for details.
|
a test case has started. See `ExUnit.Test` for details.
|
||||||
|
|
||||||
* `{:test_finished, test_case}` -
|
* `{:test_finished, test_case}` -
|
||||||
a test case has finished. See `ExUnit.Test` for details.
|
a test case has finished. See `ExUnit.Test` for details.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ defmodule ExUnit.Runner do
|
|||||||
|> Keyword.put(:exclude, exclude)
|
|> Keyword.put(:exclude, exclude)
|
||||||
|> Keyword.put(:include, include)
|
|> Keyword.put(:include, include)
|
||||||
|> Keyword.put_new(:max_cases, :erlang.system_info(:schedulers_online))
|
|> Keyword.put_new(:max_cases, :erlang.system_info(:schedulers_online))
|
||||||
|> Keyword.put_new(:seed, :erlang.now |> elem(2))
|
|> Keyword.put_new(:seed, :os.timestamp |> elem(2))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp loop(config) do
|
defp loop(config) do
|
||||||
@@ -159,7 +159,9 @@ defmodule ExUnit.Runner do
|
|||||||
{test_case, pending} =
|
{test_case, pending} =
|
||||||
receive do
|
receive do
|
||||||
{^case_pid, :case_finished, test_case, tests} ->
|
{^case_pid, :case_finished, test_case, tests} ->
|
||||||
Process.demonitor(case_ref, [:flush])
|
receive do
|
||||||
|
{:DOWN, ^case_ref, :process, ^case_pid, _} -> :ok
|
||||||
|
end
|
||||||
{test_case, tests}
|
{test_case, tests}
|
||||||
{:DOWN, ^case_ref, :process, ^case_pid, error} ->
|
{:DOWN, ^case_ref, :process, ^case_pid, error} ->
|
||||||
test_case = %{test_case | state: {:failed, {{:EXIT, case_pid}, error, []}}}
|
test_case = %{test_case | state: {:failed, {{:EXIT, case_pid}, error, []}}}
|
||||||
@@ -214,15 +216,25 @@ defmodule ExUnit.Runner do
|
|||||||
test =
|
test =
|
||||||
receive do
|
receive do
|
||||||
{^test_pid, :test_finished, test} ->
|
{^test_pid, :test_finished, test} ->
|
||||||
Process.demonitor(test_ref, [:flush])
|
receive do
|
||||||
|
{:DOWN, ^test_ref, :process, ^test_pid, _} -> :ok
|
||||||
|
end
|
||||||
test
|
test
|
||||||
{:DOWN, ^test_ref, :process, ^test_pid, error} ->
|
{:DOWN, ^test_ref, :process, ^test_pid, error} ->
|
||||||
%{test | state: {:failed, {{:EXIT, test_pid}, error, []}}}
|
%{test | state: {:failed, {{:EXIT, test_pid}, error, []}}}
|
||||||
after
|
after
|
||||||
timeout ->
|
timeout ->
|
||||||
|
stacktrace =
|
||||||
|
try do
|
||||||
|
Process.info(test_pid, :current_stacktrace)
|
||||||
|
catch
|
||||||
|
_, _ -> []
|
||||||
|
else
|
||||||
|
{:current_stacktrace, stacktrace} -> stacktrace
|
||||||
|
end
|
||||||
Process.exit(test_pid, :kill)
|
Process.exit(test_pid, :kill)
|
||||||
Process.demonitor(test_ref, [:flush])
|
Process.demonitor(test_ref, [:flush])
|
||||||
%{test | state: {:failed, {:error, %ExUnit.TimeoutError{timeout: timeout}, []}}}
|
%{test | state: {:failed, {:error, %ExUnit.TimeoutError{timeout: timeout}, stacktrace}}}
|
||||||
end
|
end
|
||||||
|
|
||||||
exec_on_exit(test, test_pid)
|
exec_on_exit(test, test_pid)
|
||||||
|
|||||||
@@ -28,6 +28,25 @@ defmodule ExUnit.CaptureIOTest do
|
|||||||
import ExUnit.CaptureIO
|
import ExUnit.CaptureIO
|
||||||
doctest ExUnit.CaptureIO, import: true
|
doctest ExUnit.CaptureIO, import: true
|
||||||
|
|
||||||
|
test "no leakage on failures" do
|
||||||
|
group_leader = Process.group_leader()
|
||||||
|
|
||||||
|
test = self()
|
||||||
|
assert_raise ArgumentError, fn ->
|
||||||
|
capture_io(fn ->
|
||||||
|
send(test, {:string_io, Process.group_leader()})
|
||||||
|
raise ArgumentError
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
receive do
|
||||||
|
{:string_io, pid} ->
|
||||||
|
ref = Process.monitor(pid)
|
||||||
|
assert_receive {:DOWN, ^ref, _, _, _}
|
||||||
|
end
|
||||||
|
assert Process.group_leader() == group_leader
|
||||||
|
end
|
||||||
|
|
||||||
test "with no output" do
|
test "with no output" do
|
||||||
assert capture_io(fn ->
|
assert capture_io(fn ->
|
||||||
end) == ""
|
end) == ""
|
||||||
@@ -290,19 +309,14 @@ defmodule ExUnit.CaptureIOTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "with assert inside" do
|
test "with assert inside" do
|
||||||
group_leader = :erlang.group_leader
|
|
||||||
|
|
||||||
try do
|
try do
|
||||||
capture_io(fn ->
|
capture_io(fn ->
|
||||||
assert false
|
assert false
|
||||||
end)
|
end)
|
||||||
rescue
|
rescue
|
||||||
error in [ExUnit.AssertionError] ->
|
error in [ExUnit.AssertionError] ->
|
||||||
"Expected truthy, got false" = error.message
|
assert error.message == "Expected truthy, got false"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ensure no leakage on failures
|
|
||||||
assert group_leader == :erlang.group_leader
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "capture :stderr by two processes" do
|
test "capture :stderr by two processes" do
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ defmodule ExUnit.CaseTest do
|
|||||||
assert context[:case] == __MODULE__
|
assert context[:case] == __MODULE__
|
||||||
assert context[:test] == __ENV__.function |> elem(0)
|
assert context[:test] == __ENV__.function |> elem(0)
|
||||||
assert context[:line] == line
|
assert context[:line] == line
|
||||||
|
assert context[:async] == true
|
||||||
assert context[:hello] == true
|
assert context[:hello] == true
|
||||||
assert context[:world] == :good
|
assert context[:world] == :good
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -50,8 +50,9 @@ defmodule ExUnitTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert capture_io(fn -> ExUnit.run end) =~
|
output = capture_io(fn -> ExUnit.run end)
|
||||||
"** (ExUnit.TimeoutError) test timed out after 10ms"
|
assert output =~ "** (ExUnit.TimeoutError) test timed out after 10ms"
|
||||||
|
assert output =~ ~r"\(stdlib\) timer\.erl:\d+: :timer\.sleep/1"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "filtering cases with tags" do
|
test "filtering cases with tags" do
|
||||||
|
|||||||
+45
-78
@@ -273,54 +273,35 @@ defmodule IEx do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def configure(options) do
|
def configure(options) do
|
||||||
Enum.each options, fn {k, v} ->
|
IEx.Config.configure(options)
|
||||||
Application.put_env(:iex, k, configure(k, v))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp configure(k, v) when k in [:colors, :inspect] and is_list(v) do
|
|
||||||
Keyword.merge(Application.get_env(:iex, k), v)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp configure(:history_size, v) when is_integer(v) do
|
|
||||||
v
|
|
||||||
end
|
|
||||||
|
|
||||||
defp configure(k, v) when k in [:default_prompt, :alive_prompt] and is_binary(v) do
|
|
||||||
v
|
|
||||||
end
|
|
||||||
|
|
||||||
defp configure(k, v) do
|
|
||||||
raise ArgumentError, "invalid configuration or value for pair #{inspect k} - #{inspect v}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns IEx configuration.
|
Returns IEx configuration.
|
||||||
"""
|
"""
|
||||||
def configuration do
|
def configuration do
|
||||||
Application.get_all_env(:iex)
|
IEx.Config.configuration()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Registers a function to be invoked after the IEx process is spawned.
|
Registers a function to be invoked after the IEx process is spawned.
|
||||||
"""
|
"""
|
||||||
def after_spawn(fun) when is_function(fun) do
|
def after_spawn(fun) when is_function(fun) do
|
||||||
Application.put_env(:iex, :after_spawn, [fun|after_spawn])
|
IEx.Config.after_spawn(fun)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns registered `after_spawn` callbacks.
|
Returns registered `after_spawn` callbacks.
|
||||||
"""
|
"""
|
||||||
def after_spawn do
|
def after_spawn do
|
||||||
{:ok, list} = Application.fetch_env(:iex, :after_spawn)
|
IEx.Config.after_spawn()
|
||||||
list
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns `true` if IEx was started.
|
Returns `true` if IEx was started.
|
||||||
"""
|
"""
|
||||||
def started? do
|
def started? do
|
||||||
Application.get_env(:iex, :started, false)
|
IEx.Config.started?()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -329,37 +310,28 @@ defmodule IEx do
|
|||||||
ANSI escapes in `string` are not processed in any way.
|
ANSI escapes in `string` are not processed in any way.
|
||||||
"""
|
"""
|
||||||
def color(color, string) do
|
def color(color, string) do
|
||||||
colors = Application.get_env(:iex, :colors)
|
case IEx.Config.color(color) do
|
||||||
|
nil ->
|
||||||
if color_enabled?(colors[:enabled]) do
|
string
|
||||||
ansi = Keyword.get(colors, color, default_color(color))
|
ansi ->
|
||||||
IO.iodata_to_binary(IO.ANSI.format_fragment(ansi, true)) <> string <> IO.ANSI.reset
|
IO.iodata_to_binary([IO.ANSI.format_fragment(ansi, true), string | IO.ANSI.reset])
|
||||||
else
|
|
||||||
string
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp color_enabled?(nil), do: IO.ANSI.enabled?
|
|
||||||
defp color_enabled?(bool) when is_boolean(bool), do: bool
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets the IEx width for printing.
|
Gets the IEx width for printing.
|
||||||
|
|
||||||
Used by helpers and it has a maximum cap of 80 chars.
|
Used by helpers and it has a maximum cap of 80 chars.
|
||||||
"""
|
"""
|
||||||
def width do
|
def width do
|
||||||
case :io.columns() do
|
IEx.Config.width()
|
||||||
{:ok, width} -> min(width, 80)
|
|
||||||
{:error, _} -> 80
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets the options used for inspecting.
|
Gets the options used for inspecting.
|
||||||
"""
|
"""
|
||||||
def inspect_opts do
|
def inspect_opts do
|
||||||
Application.get_env(:iex, :inspect) ++
|
IEx.Config.inspect_opts()
|
||||||
[width: width(), pretty: true]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@@ -419,27 +391,43 @@ defmodule IEx do
|
|||||||
Setting variables or importing modules in IEx does not
|
Setting variables or importing modules in IEx does not
|
||||||
affect the caller the environment (hence it is called `pry`).
|
affect the caller the environment (hence it is called `pry`).
|
||||||
"""
|
"""
|
||||||
defmacro pry(timeout \\ 1000) do
|
defmacro pry(timeout \\ 5000) do
|
||||||
quote do
|
quote do
|
||||||
env = __ENV__
|
IEx.pry(binding, __ENV__, unquote(timeout))
|
||||||
meta = "#{inspect self} at #{Path.relative_to_cwd(env.file)}:#{env.line}"
|
|
||||||
opts = [binding: binding, dot_iex_path: "", env: env, prefix: "pry"]
|
|
||||||
res = IEx.Server.take_over("Request to pry #{meta}", opts, unquote(timeout))
|
|
||||||
|
|
||||||
# We cannot use colors because IEx may be off.
|
|
||||||
case res do
|
|
||||||
{:error, :self} = err ->
|
|
||||||
IO.puts :stdio, "IEx cannot pry itself."
|
|
||||||
{:error, :no_iex} = err ->
|
|
||||||
IO.puts :stdio, "Cannot pry #{meta}. Is an IEx shell running?"
|
|
||||||
_ ->
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
|
|
||||||
res
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Callback for `IEx.pry/1`.
|
||||||
|
|
||||||
|
You can invoke this function directly when you are not able to invoke
|
||||||
|
`IEx.pry/1` as a macro. This function expects the binding (from
|
||||||
|
`Kernel.binding/0`), the environment (from `__ENV__`) and the timeout
|
||||||
|
(a sensible default is 5000).
|
||||||
|
"""
|
||||||
|
def pry(binding, env, timeout) do
|
||||||
|
meta = "#{inspect self} at #{Path.relative_to_cwd(env.file)}:#{env.line}"
|
||||||
|
opts = [binding: binding, dot_iex_path: "", env: env, prefix: "pry"]
|
||||||
|
res = IEx.Server.take_over("Request to pry #{meta}", opts, timeout)
|
||||||
|
|
||||||
|
# We cannot use colors because IEx may be off.
|
||||||
|
case res do
|
||||||
|
{:error, :self} ->
|
||||||
|
IO.puts :stdio, "IEx cannot pry the shell itself."
|
||||||
|
{:error, :no_iex} ->
|
||||||
|
extra =
|
||||||
|
case :os.type do
|
||||||
|
{:win32, _} -> " If you are Windows, you may need to start IEx with the --werl flag."
|
||||||
|
_ -> ""
|
||||||
|
end
|
||||||
|
IO.puts :stdio, "Cannot pry #{meta}. Is an IEx shell running?" <> extra
|
||||||
|
_ ->
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
res
|
||||||
|
end
|
||||||
|
|
||||||
## Callbacks
|
## Callbacks
|
||||||
|
|
||||||
# This is a callback invoked by Erlang shell utilities
|
# This is a callback invoked by Erlang shell utilities
|
||||||
@@ -467,7 +455,6 @@ defmodule IEx do
|
|||||||
defp start_iex() do
|
defp start_iex() do
|
||||||
unless started? do
|
unless started? do
|
||||||
{:ok, _} = Application.ensure_all_started(:iex)
|
{:ok, _} = Application.ensure_all_started(:iex)
|
||||||
Application.put_env(:iex, :started, true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
:ok
|
:ok
|
||||||
@@ -503,24 +490,4 @@ defmodule IEx do
|
|||||||
_ = for fun <- Enum.reverse(after_spawn), do: fun.()
|
_ = for fun <- Enum.reverse(after_spawn), do: fun.()
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
# Used by default on evaluation cycle
|
|
||||||
defp default_color(:eval_interrupt), do: [:yellow]
|
|
||||||
defp default_color(:eval_result), do: [:yellow]
|
|
||||||
defp default_color(:eval_error), do: [:red]
|
|
||||||
defp default_color(:eval_info), do: [:normal]
|
|
||||||
defp default_color(:stack_app), do: [:red, :bright]
|
|
||||||
defp default_color(:stack_info), do: [:red]
|
|
||||||
|
|
||||||
# Used by ls
|
|
||||||
defp default_color(:ls_directory), do: [:blue]
|
|
||||||
defp default_color(:ls_device), do: [:green]
|
|
||||||
|
|
||||||
# Used by ansi docs
|
|
||||||
defp default_color(:doc_bold), do: [:bright]
|
|
||||||
defp default_color(:doc_code), do: [:cyan, :bright]
|
|
||||||
defp default_color(:doc_headings), do: [:yellow, :bright]
|
|
||||||
defp default_color(:doc_inline_code), do: [:cyan]
|
|
||||||
defp default_color(:doc_underline), do: [:underline]
|
|
||||||
defp default_color(:doc_title), do: [:reverse, :yellow, :bright]
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
defmodule IEx.App do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Application
|
||||||
|
|
||||||
|
def start(_type, _args) do
|
||||||
|
import Supervisor.Spec
|
||||||
|
|
||||||
|
children = [worker(IEx.Config, [])]
|
||||||
|
options = [strategy: :one_for_one, name: IEx.Supervisor]
|
||||||
|
|
||||||
|
tab = IEx.Config.new()
|
||||||
|
case Supervisor.start_link(children, options) do
|
||||||
|
{:ok, pid} ->
|
||||||
|
{:ok, pid, tab}
|
||||||
|
{:error, _} = error ->
|
||||||
|
IEx.Config.delete(tab)
|
||||||
|
error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop(tab) do
|
||||||
|
IEx.Config.delete(tab)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,12 +10,12 @@ defmodule IEx.Autocomplete do
|
|||||||
h === ?. and t != []->
|
h === ?. and t != []->
|
||||||
expand_dot(reduce(t))
|
expand_dot(reduce(t))
|
||||||
h === ?: ->
|
h === ?: ->
|
||||||
expand_erlang_modules
|
expand_erlang_modules()
|
||||||
identifier?(h) ->
|
identifier?(h) ->
|
||||||
expand_expr(reduce(expr))
|
expand_expr(reduce(expr))
|
||||||
(h == ?/) and t != [] and identifier?(hd(t)) ->
|
(h == ?/) and t != [] and identifier?(hd(t)) ->
|
||||||
expand_expr(reduce(t))
|
expand_expr(reduce(t))
|
||||||
h in '(+[' ->
|
h in '([{' ->
|
||||||
expand('')
|
expand('')
|
||||||
true ->
|
true ->
|
||||||
no()
|
no()
|
||||||
@@ -40,32 +40,26 @@ defmodule IEx.Autocomplete do
|
|||||||
defp expand_expr(expr) do
|
defp expand_expr(expr) do
|
||||||
case Code.string_to_quoted expr do
|
case Code.string_to_quoted expr do
|
||||||
{:ok, atom} when is_atom(atom) ->
|
{:ok, atom} when is_atom(atom) ->
|
||||||
expand_erlang_modules Atom.to_string(atom)
|
expand_erlang_modules(Atom.to_string(atom))
|
||||||
{:ok, {atom, _, nil}} when is_atom(atom) ->
|
{:ok, {atom, _, nil}} when is_atom(atom) ->
|
||||||
expand_import Atom.to_string(atom)
|
expand_import(Atom.to_string(atom))
|
||||||
{:ok, {:__aliases__, _, [root]}} ->
|
{:ok, {:__aliases__, _, [root]}} ->
|
||||||
expand_elixir_modules [], Atom.to_string(root)
|
expand_elixir_modules([], Atom.to_string(root))
|
||||||
{:ok, {:__aliases__, _, [h|_] = list}} when is_atom(h) ->
|
{:ok, {:__aliases__, _, [h|_] = list}} when is_atom(h) ->
|
||||||
hint = Atom.to_string(List.last(list))
|
hint = Atom.to_string(List.last(list))
|
||||||
list = Enum.take(list, length(list) - 1)
|
list = Enum.take(list, length(list) - 1)
|
||||||
expand_elixir_modules list, hint
|
expand_elixir_modules(list, hint)
|
||||||
{:ok, {{:., _, [mod, fun]}, _, []}} when is_atom(fun) ->
|
{:ok, {{:., _, [mod, fun]}, _, []}} when is_atom(fun) ->
|
||||||
expand_call mod, Atom.to_string(fun)
|
expand_call(mod, Atom.to_string(fun))
|
||||||
_ ->
|
_ ->
|
||||||
no()
|
no()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp reduce(expr) do
|
defp reduce(expr) do
|
||||||
last_token(Enum.reverse(expr), [' ', '(', '[', '+', '-'])
|
Enum.reverse Enum.reduce [' ', '(', '[', '{'], expr, fn token, acc ->
|
||||||
end
|
hd(:string.tokens(acc, token))
|
||||||
|
end
|
||||||
defp last_token(s, []) do
|
|
||||||
s
|
|
||||||
end
|
|
||||||
|
|
||||||
defp last_token(s, [h|t]) do
|
|
||||||
last_token(List.last(:string.tokens(s, h)), t)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp yes(hint, entries) do
|
defp yes(hint, entries) do
|
||||||
@@ -93,42 +87,22 @@ defmodule IEx.Autocomplete do
|
|||||||
length = byte_size(hint)
|
length = byte_size(hint)
|
||||||
prefix = :binary.longest_common_prefix(binary)
|
prefix = :binary.longest_common_prefix(binary)
|
||||||
if prefix in [0, length] do
|
if prefix in [0, length] do
|
||||||
entries = Enum.reduce(entries, [], fn e, acc -> to_entries(e) ++ acc end)
|
yes("", Enum.flat_map(entries, &to_entries/1))
|
||||||
yes("", entries)
|
|
||||||
else
|
else
|
||||||
yes(:binary.part(first.name, prefix, length-prefix), [])
|
yes(:binary.part(first.name, prefix, length-prefix), [])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
## Root Modules
|
|
||||||
|
|
||||||
defp root_modules do
|
|
||||||
Enum.reduce :code.all_loaded, [], fn {m, _}, acc ->
|
|
||||||
mod = Atom.to_string(m)
|
|
||||||
case mod do
|
|
||||||
"Elixir" <> _ ->
|
|
||||||
tokens = String.split(mod, ".")
|
|
||||||
if length(tokens) == 2 do
|
|
||||||
[%{kind: :module, name: List.last(tokens), type: :elixir}|acc]
|
|
||||||
else
|
|
||||||
acc
|
|
||||||
end
|
|
||||||
_ ->
|
|
||||||
[%{kind: :module, name: mod, type: :erlang}|acc]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
## Expand calls
|
## Expand calls
|
||||||
|
|
||||||
# :atom.fun
|
# :atom.fun
|
||||||
defp expand_call(mod, hint) when is_atom(mod) do
|
defp expand_call(mod, hint) when is_atom(mod) do
|
||||||
expand_require mod, hint
|
expand_require(mod, hint)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Elixir.fun
|
# Elixir.fun
|
||||||
defp expand_call({:__aliases__, _, list}, hint) do
|
defp expand_call({:__aliases__, _, list}, hint) do
|
||||||
expand_require Module.concat(list), hint
|
expand_require(Module.concat(list), hint)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp expand_call(_, _) do
|
defp expand_call(_, _) do
|
||||||
@@ -136,13 +110,13 @@ defmodule IEx.Autocomplete do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp expand_require(mod, hint) do
|
defp expand_require(mod, hint) do
|
||||||
format_expansion module_funs(mod, hint), hint
|
format_expansion match_module_funs(mod, hint), hint
|
||||||
end
|
end
|
||||||
|
|
||||||
defp expand_import(hint) do
|
defp expand_import(hint) do
|
||||||
funs = module_funs(IEx.Helpers, hint) ++
|
funs = match_module_funs(IEx.Helpers, hint) ++
|
||||||
module_funs(Kernel, hint) ++
|
match_module_funs(Kernel, hint) ++
|
||||||
module_funs(Kernel.SpecialForms, hint)
|
match_module_funs(Kernel.SpecialForms, hint)
|
||||||
format_expansion funs, hint
|
format_expansion funs, hint
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -152,55 +126,72 @@ defmodule IEx.Autocomplete do
|
|||||||
format_expansion match_erlang_modules(hint), hint
|
format_expansion match_erlang_modules(hint), hint
|
||||||
end
|
end
|
||||||
|
|
||||||
defp match_erlang_modules("") do
|
|
||||||
Enum.filter root_modules, fn m -> m.type === :erlang end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp match_erlang_modules(hint) do
|
defp match_erlang_modules(hint) do
|
||||||
Enum.filter root_modules, fn m -> String.starts_with?(m.name, hint) end
|
for mod <- match_modules(hint, true) do
|
||||||
|
%{kind: :module, name: mod, type: :erlang}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
## Elixir modules
|
## Elixir modules
|
||||||
|
|
||||||
defp expand_elixir_modules(list, hint) do
|
defp expand_elixir_modules(list, hint) do
|
||||||
mod = Module.concat(list)
|
mod = Module.concat(list)
|
||||||
format_expansion elixir_submodules(mod, hint, list == []) ++ module_funs(mod, hint), hint
|
format_expansion match_elixir_modules(mod, hint, list == []) ++
|
||||||
|
match_module_funs(mod, hint), hint
|
||||||
end
|
end
|
||||||
|
|
||||||
defp elixir_submodules(mod, hint, root) do
|
defp match_elixir_modules(module, hint, root) do
|
||||||
modname = Atom.to_string(mod)
|
module = Atom.to_string(module)
|
||||||
depth = length(String.split(modname, ".")) + 1
|
depth = length(String.split(module, ".")) + 1
|
||||||
base = modname <> "." <> hint
|
base = module <> "." <> hint
|
||||||
|
|
||||||
Enum.reduce modules_as_lists(root), [], fn(m, acc) ->
|
for mod <- match_modules(base, root),
|
||||||
if String.starts_with?(m, base) do
|
tokens = String.split(mod, "."),
|
||||||
tokens = String.split(m, ".")
|
length(tokens) == depth do
|
||||||
if length(tokens) == depth do
|
name = List.last(tokens)
|
||||||
name = List.last(tokens)
|
%{kind: :module, type: :elixir, name: name}
|
||||||
[%{kind: :module, type: :elixir, name: name}|acc]
|
|
||||||
else
|
|
||||||
acc
|
|
||||||
end
|
|
||||||
else
|
|
||||||
acc
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp modules_as_lists(true) do
|
|
||||||
["Elixir.Elixir"] ++ modules_as_lists(false)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp modules_as_lists(false) do
|
|
||||||
Enum.map(:code.all_loaded, fn({m, _}) -> Atom.to_string(m) end)
|
|
||||||
end
|
|
||||||
|
|
||||||
## Helpers
|
## Helpers
|
||||||
|
|
||||||
defp module_funs(mod, hint) do
|
defp match_modules(hint, root) do
|
||||||
|
get_modules(root)
|
||||||
|
|> :lists.usort()
|
||||||
|
|> Enum.drop_while(& not String.starts_with?(&1, hint))
|
||||||
|
|> Enum.take_while(& String.starts_with?(&1, hint))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_modules(true) do
|
||||||
|
["Elixir.Elixir"] ++ get_modules(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_modules(false) do
|
||||||
|
modules = Enum.map(:code.all_loaded, fn({m, _}) -> Atom.to_string(m) end)
|
||||||
|
|
||||||
|
if :code.get_mode() === :interactive do
|
||||||
|
modules ++ get_modules_from_applications()
|
||||||
|
else
|
||||||
|
modules
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_modules_from_applications do
|
||||||
|
for [app] <- loaded_applications(),
|
||||||
|
{_, modules} = :application.get_key(app, :modules),
|
||||||
|
module <- modules do
|
||||||
|
Atom.to_string(module)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp loaded_applications do
|
||||||
|
:ets.match(:ac_tab, {{:loaded, :"$1"}, :_})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp match_module_funs(mod, hint) do
|
||||||
case ensure_loaded(mod) do
|
case ensure_loaded(mod) do
|
||||||
{:module, _} ->
|
{:module, _} ->
|
||||||
falist = get_funs(mod)
|
falist = get_module_funs(mod)
|
||||||
|
|
||||||
list = Enum.reduce falist, [], fn {f, a}, acc ->
|
list = Enum.reduce falist, [], fn {f, a}, acc ->
|
||||||
case :lists.keyfind(f, 1, acc) do
|
case :lists.keyfind(f, 1, acc) do
|
||||||
@@ -211,15 +202,15 @@ defmodule IEx.Autocomplete do
|
|||||||
|
|
||||||
for {fun, arities} <- list,
|
for {fun, arities} <- list,
|
||||||
name = Atom.to_string(fun),
|
name = Atom.to_string(fun),
|
||||||
hint == "" or String.starts_with?(name, hint) do
|
String.starts_with?(name, hint) do
|
||||||
%{kind: :function, name: name, arities: arities}
|
%{kind: :function, name: name, arities: arities}
|
||||||
end
|
end |> :lists.sort()
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_funs(mod) do
|
defp get_module_funs(mod) do
|
||||||
if function_exported?(mod, :__info__, 1) do
|
if function_exported?(mod, :__info__, 1) do
|
||||||
if docs = Code.get_docs(mod, :docs) do
|
if docs = Code.get_docs(mod, :docs) do
|
||||||
for {tuple, _line, _kind, _sign, doc} <- docs, doc != false, do: tuple
|
for {tuple, _line, _kind, _sign, doc} <- docs, doc != false, do: tuple
|
||||||
@@ -241,7 +232,7 @@ defmodule IEx.Autocomplete do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp to_entries(%{kind: :function, name: name, arities: arities}) do
|
defp to_entries(%{kind: :function, name: name, arities: arities}) do
|
||||||
for a <- arities, do: "#{name}/#{a}"
|
for a <- :lists.sort(arities), do: "#{name}/#{a}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp to_uniq_entries(%{kind: :module}) do
|
defp to_uniq_entries(%{kind: :module}) do
|
||||||
|
|||||||
+10
-8
@@ -53,13 +53,15 @@ defmodule IEx.CLI do
|
|||||||
if tty_works? do
|
if tty_works? do
|
||||||
:user_drv.start([:"tty_sl -c -e", tty_args])
|
:user_drv.start([:"tty_sl -c -e", tty_args])
|
||||||
else
|
else
|
||||||
:user.start
|
:application.set_env(:stdlib, :shell_prompt_func,
|
||||||
unless match? {:win32, _}, :os.type do
|
{__MODULE__, :prompt})
|
||||||
IO.puts "Warning: could not run smart terminal, falling back to dumb one"
|
:user.start()
|
||||||
end
|
|
||||||
local_start()
|
local_start()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
def prompt(_n) do
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
# Check if tty works. If it does not, we fall back to the
|
# Check if tty works. If it does not, we fall back to the
|
||||||
# simple/dumb terminal. This is starting the linked in
|
# simple/dumb terminal. This is starting the linked in
|
||||||
@@ -94,7 +96,7 @@ defmodule IEx.CLI do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def local_start do
|
def local_start do
|
||||||
IEx.start(config(), {:elixir, :start_cli, []})
|
IEx.start(options(), {:elixir, :start_cli, []})
|
||||||
end
|
end
|
||||||
|
|
||||||
def remote_start(parent, ref) do
|
def remote_start(parent, ref) do
|
||||||
@@ -108,7 +110,7 @@ defmodule IEx.CLI do
|
|||||||
|
|
||||||
defp remote_start_function do
|
defp remote_start_function do
|
||||||
ref = make_ref
|
ref = make_ref
|
||||||
config = config()
|
opts = options()
|
||||||
|
|
||||||
parent = spawn_link fn ->
|
parent = spawn_link fn ->
|
||||||
receive do
|
receive do
|
||||||
@@ -119,11 +121,11 @@ defmodule IEx.CLI do
|
|||||||
end
|
end
|
||||||
|
|
||||||
fn ->
|
fn ->
|
||||||
IEx.start(config, {__MODULE__, :remote_start, [parent, ref]})
|
IEx.start(opts, {__MODULE__, :remote_start, [parent, ref]})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp config do
|
defp options do
|
||||||
[dot_iex_path: find_dot_iex(:init.get_plain_arguments)]
|
[dot_iex_path: find_dot_iex(:init.get_plain_arguments)]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
defmodule IEx.Config do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
@table __MODULE__
|
||||||
|
@agent __MODULE__
|
||||||
|
@keys [:colors, :inspect, :history_size, :default_prompt, :alive_prompt]
|
||||||
|
@colors [:eval_interrupt, :eval_result, :eval_error, :eval_info, :stack_app,
|
||||||
|
:stack_info, :ls_directory, :ls_device]
|
||||||
|
|
||||||
|
def new() do
|
||||||
|
tab = :ets.new(@table, [:named_table, :public])
|
||||||
|
true = :ets.insert_new(tab, [after_spawn: []])
|
||||||
|
tab
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete(__MODULE__) do
|
||||||
|
:ets.delete(__MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def configure(options) do
|
||||||
|
Agent.update(@agent, __MODULE__, :handle_configure, [options])
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_configure(tab, options) do
|
||||||
|
options = :lists.ukeysort(1, options)
|
||||||
|
get_config()
|
||||||
|
|> Keyword.merge(options, &merge_option/3)
|
||||||
|
|> put_config()
|
||||||
|
tab
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_config() do
|
||||||
|
Application.get_all_env(:iex)
|
||||||
|
|> Keyword.take(@keys)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp put_config(config) do
|
||||||
|
put = fn({key, value}) when key in @keys ->
|
||||||
|
Application.put_env(:iex, key, value)
|
||||||
|
end
|
||||||
|
Enum.each(config, put)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp merge_option(:colors, old, new) when is_list(new), do: Keyword.merge(old, new)
|
||||||
|
defp merge_option(:inspect, old, new) when is_list(new), do: Keyword.merge(old, new)
|
||||||
|
defp merge_option(:history_size, _old, new) when is_integer(new), do: new
|
||||||
|
defp merge_option(:default_prompt, _old, new) when is_binary(new), do: new
|
||||||
|
defp merge_option(:alive_prompt, _old, new) when is_binary(new), do: new
|
||||||
|
|
||||||
|
defp merge_option(k, _old, new) do
|
||||||
|
raise ArgumentError, "invalid configuration or value for pair #{inspect k} - #{inspect new}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def configuration() do
|
||||||
|
Keyword.merge(default_config(), get_config(), &merge_option/3)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp default_config() do
|
||||||
|
Enum.map(@keys, &{&1, default_option(&1)})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp default_option(:colors), do: [{:enabled, IO.ANSI.enabled?} | default_colors()]
|
||||||
|
defp default_option(:inspect), do: []
|
||||||
|
defp default_option(:history_size), do: 20
|
||||||
|
|
||||||
|
defp default_option(prompt) when prompt in [:default_prompt, :alive_prompt] do
|
||||||
|
"%prefix(%counter)>"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp default_colors() do
|
||||||
|
Enum.map(@colors, &{&1, default_color(&1)}) ++ default_doc_colors()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp default_doc_colors() do
|
||||||
|
Keyword.drop(IO.ANSI.Docs.default_options(), [:enabled, :width])
|
||||||
|
end
|
||||||
|
|
||||||
|
# Used by default on evaluation cycle
|
||||||
|
defp default_color(:eval_interrupt), do: [:yellow]
|
||||||
|
defp default_color(:eval_result), do: [:yellow]
|
||||||
|
defp default_color(:eval_error), do: [:red]
|
||||||
|
defp default_color(:eval_info), do: [:normal]
|
||||||
|
defp default_color(:stack_app), do: [:red, :bright]
|
||||||
|
defp default_color(:stack_info), do: [:red]
|
||||||
|
|
||||||
|
# Used by ls
|
||||||
|
defp default_color(:ls_directory), do: [:blue]
|
||||||
|
defp default_color(:ls_device), do: [:green]
|
||||||
|
|
||||||
|
# Used by ansi docs
|
||||||
|
defp default_color(doc_color) do
|
||||||
|
IO.ANSI.Docs.default_options()
|
||||||
|
|> Keyword.fetch!(doc_color)
|
||||||
|
end
|
||||||
|
|
||||||
|
def color(color) do
|
||||||
|
colors = Application.get_env(:iex, :colors, [])
|
||||||
|
if colors_enabled?(colors) do
|
||||||
|
case Keyword.fetch(colors, color) do
|
||||||
|
{:ok, value} ->
|
||||||
|
value
|
||||||
|
:error ->
|
||||||
|
default_color(color)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp colors_enabled?(colors \\ Application.get_env(:iex, :colors, [])) do
|
||||||
|
case Keyword.fetch(colors, :enabled) do
|
||||||
|
{:ok, enabled} ->
|
||||||
|
enabled
|
||||||
|
:error ->
|
||||||
|
IO.ANSI.enabled?()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def ansi_docs() do
|
||||||
|
colors = Application.get_env(:iex, :colors, [])
|
||||||
|
if enabled = colors_enabled?(colors) do
|
||||||
|
[width: width(), enabled: enabled] ++ colors
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect_opts() do
|
||||||
|
inspect_options = Application.get_env(:iex, :inspect, []) ++ [pretty: true]
|
||||||
|
cond do
|
||||||
|
Keyword.has_key?(inspect_options, :width) ->
|
||||||
|
inspect_options
|
||||||
|
colors_enabled? ->
|
||||||
|
[width: width()] ++ inspect_options
|
||||||
|
true ->
|
||||||
|
inspect_options
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def width() do
|
||||||
|
case :io.columns() do
|
||||||
|
{:ok, width} -> min(width, 80)
|
||||||
|
{:error, _} -> 80
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def after_spawn(fun) do
|
||||||
|
Agent.update(@agent, __MODULE__, :handle_after_spawn, [fun])
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_after_spawn(tab, fun) do
|
||||||
|
:ets.update_element(tab, :after_spawn, {2, [fun | after_spawn()]})
|
||||||
|
end
|
||||||
|
|
||||||
|
def after_spawn() do
|
||||||
|
:ets.lookup_element(@table, :after_spawn, 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
def started?() do
|
||||||
|
Process.whereis(@agent) !== nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def history_size(), do: get(:history_size)
|
||||||
|
|
||||||
|
def default_prompt(), do: get(:default_prompt)
|
||||||
|
|
||||||
|
def alive_prompt(), do: get(:alive_prompt)
|
||||||
|
|
||||||
|
defp get(key) do
|
||||||
|
case Application.fetch_env(:iex, key) do
|
||||||
|
{:ok, value} ->
|
||||||
|
value
|
||||||
|
:error ->
|
||||||
|
default_option(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_link() do
|
||||||
|
Agent.start_link(__MODULE__, :init, [@table], [name: @agent])
|
||||||
|
end
|
||||||
|
|
||||||
|
def init(tab) do
|
||||||
|
:public = :ets.info(tab, :protection)
|
||||||
|
tab
|
||||||
|
end
|
||||||
|
end
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user