Skip to content

Commit

Permalink
Add QuickJS as a Javascript engine option
Browse files Browse the repository at this point in the history
https://bellard.org/quickjs

Some benefits over SM:

 * Small. We're using 6 or so C files vs 700+ SM91 C++ files.

 * Built with Apache CouchDB as opposed having to maintain a separate SM
   package, like for RHEL9, for instance, where they dropped support for SM
   already. (see #4154).

 * Embedding friendly. Designed from ground-up for embedding. SM has been
   updating the C++ API such that we have to keep copy-pasting new versions of
   our C++ code every year or so. (see
   #4305).

 * Easy to modify to accept Spidermonkey 1.8.5 top level functions for
   map/reduce code so we don't have have to parse the JS, AST transform it, and
   then re-compile it.

 * Configurable runtime feature set - can disable workers, promises and other
   API and features which may not work well in a backend JS environment. Some
   users may want more, some may want to disable even Date(time) features to
   hedge again Spectre-style attacks (spectreattack.com).

 * Allows granular time (reduction) tracking if we wanted to provide a runtime
   allowance for each function.

 * Better sandboxing. Creating a whole JSRuntime takes only 300 microseconds, so
   we can afford to do that on reset. JSRuntimes cannot share JS data or object
   between them.

 * Seems to be faster in preliminary benchmarking with small
   concurrent VDU and view builds:
     https://gist.github.com/nickva/ed239651114794ebb138b1f16c5f6758
   Results seem promising:
     - 4x faster than SM 1.8.5
     - 5x faster than SM 91
     - 6x reduced memory usage per couchjs process (5MB vs 30MB)

 * Allows compiling JS bytecode ahead of time a C array of bytes.

QuickJS can be built alongside Spidermonkey and toggled on/off at runtime:

```
./configure --dev --js-engine=quickjs
```

This makes it the default engine. But Spidermonkey can still be set in the
config option.

```
[couchdb]
js_engine = spidermonkey | quickjs
```

To test individual views, without switching the default use the
`javascript_quickjs` language in the design docs. To keep using Spidermonkey
engine after switching the default, can use `javascript_spidermonkey` language
in design docs. However, language selection will reset the view and the view
will have to be rebuilt.

It's also possible to build without Spidermonkey support completely by using:
```
./configure --disable-spidermonkey
```

Issue: #4448
  • Loading branch information
nickva committed May 1, 2024
1 parent f5292d8 commit 0c339bf
Show file tree
Hide file tree
Showing 61 changed files with 86,983 additions and 30 deletions.
53 changes: 53 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2308,3 +2308,56 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For the QuickJS component couch_js/quickjs/quickjs:

QuickJS Javascript Engine

Copyright (c) 2017-2021 Fabrice Bellard
Copyright (c) 2017-2021 Charlie Gordon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

getline/getdelim functions in couchjs.c

Copyright (c) 2011 The NetBSD Foundation, Inc.
All rights reserved.

This code is derived from software contributed to The NetBSD Foundation
by Christos Zoulas.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# *******************************************************

include version.mk

-include install.mk
REBAR?=$(CURDIR)/bin/rebar
REBAR3?=$(CURDIR)/bin/rebar3
ERLFMT?=$(CURDIR)/bin/erlfmt
Expand Down Expand Up @@ -122,8 +122,9 @@ help:
# target: couch - Build CouchDB core, use ERL_COMPILER_OPTIONS to provide custom compiler's options
couch: config.erl
@COUCHDB_VERSION=$(COUCHDB_VERSION) COUCHDB_GIT_SHA=$(COUCHDB_GIT_SHA) $(REBAR) compile $(COMPILE_OPTS)
ifeq ($(with_spidermonkey), true)
@cp src/couch/priv/couchjs bin/

endif

.PHONY: docs
# target: docs - Build documentation
Expand Down Expand Up @@ -227,7 +228,7 @@ python-black-update: .venv/bin/black
--exclude="build/|buck-out/|dist/|_build/|\.git/|\.hg/|\.mypy_cache/|\.nox/|\.tox/|\.venv/|src/rebar/pr2relnotes.py|src/fauxton" \
build-aux/*.py dev/run src/mango/test/*.py src/docs/src/conf.py src/docs/ext/*.py .

-include install.mk

ifeq ($(with_nouveau), false)
exclude_nouveau=--exclude nouveau
endif
Expand Down Expand Up @@ -392,6 +393,9 @@ dist: all derived
@mkdir -p apache-couchdb-$(COUCHDB_VERSION)/share/docs/man
@cp src/docs/build/man/apachecouchdb.1 apache-couchdb-$(COUCHDB_VERSION)/share/docs/man/

ifeq ($(with_spidermonkey), false)
@rm -rf apache-couchdb-$(COUCHDB_VERSION)/src/couch/priv/couch_js
endif
@tar czf apache-couchdb-$(COUCHDB_VERSION)$(IN_RC).tar.gz apache-couchdb-$(COUCHDB_VERSION)
@echo "Done: apache-couchdb-$(COUCHDB_VERSION)$(IN_RC).tar.gz"

Expand All @@ -404,6 +408,13 @@ release: all
@$(REBAR) generate # make full erlang release
@cp bin/weatherreport rel/couchdb/bin/weatherreport

ifeq ($(with_spidermonkey), true)
@cp src/couch/priv/couchjs rel/couchdb/bin/couchjs
@cp share/server/main.js rel/couchdb/server/main.js
@cp share/server/main-ast-bypass.js rel/couchdb/server/main-ast-bypass.js
@cp share/server/main-coffee.js rel/couchdb/server/main-coffee.js
endif

ifeq ($(with_fauxton), 1)
@mkdir -p rel/couchdb/share/
@cp -R share/www rel/couchdb/share/
Expand Down
12 changes: 10 additions & 2 deletions Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# ***************************************************

include version.mk
-include install.mk

SHELL=cmd.exe
REBAR?=$(CURDIR)/bin/rebar.cmd
Expand Down Expand Up @@ -116,7 +117,9 @@ help:
# target: couch - Build CouchDB core, use ERL_COMPILER_OPTIONS to provide custom compiler's options
couch: config.erl
@set COUCHDB_VERSION=$(COUCHDB_VERSION) && set COUCHDB_GIT_SHA=$(COUCHDB_GIT_SHA) && $(REBAR) compile $(COMPILE_OPTS)
ifeq ($(with_spidermonkey), true)
@copy src\couch\priv\couchjs.exe bin
endif


.PHONY: docs
Expand Down Expand Up @@ -204,7 +207,7 @@ python-black-update: .venv/bin/black
--exclude="build/|buck-out/|dist/|_build/|\.git/|\.hg/|\.mypy_cache/|\.nox/|\.tox/|\.venv/|src/erlfmt|src/rebar/pr2relnotes.py|src/fauxton" \
build-aux dev\run dev\format_*.py src\mango\test src\docs\src\conf.py src\docs\ext .

-include install.mk


ifeq ($(with_nouveau), false)
exclude_nouveau=--exclude nouveau
Expand Down Expand Up @@ -364,7 +367,12 @@ release: all
@echo 'Installing CouchDB into rel\couchdb\ ...'
-@rmdir /s/q rel\couchdb >NUL 2>&1 || true
@$(REBAR) generate
@copy src\couch\priv\couchjs.exe rel\couchdb\bin
ifeq ($(with_spidermonkey), true)
@copy src\couch\priv\couchjs.exe rel\couchdb\bin\couchjs.exe
@copy share\server\main.js rel\couchdb\server\main.js
@copy share\server\main-ast-bypass.js rel\couchdb\server\main-ast-bypass.js
@copy share\server\main-coffee.js rel\couchdb\server\main-coffee.js
endif

ifeq ($(with_fauxton), 1)
-@mkdir rel\couchdb\share
Expand Down
6 changes: 5 additions & 1 deletion build-aux/Jenkinsfile.pr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mkdir build
cd build
tar -xf ${WORKSPACE}/apache-couchdb-*.tar.gz
cd apache-couchdb-*
./configure --enable-nouveau --enable-clouseau
./configure --enable-nouveau --enable-clouseau --js-engine=${JS_ENGINE}
make check || (make build-report && false)
'''

Expand Down Expand Up @@ -253,6 +253,10 @@ pipeline {
name 'SM_VSN'
values '78'
}
axis {
name 'JS_ENGINE'
values 'quickjs', 'spidermonkey'
}
}

stages {
Expand Down
49 changes: 47 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ WITH_NOUVEAU="false"
WITH_CLOUSEAU=0
ERLANG_MD5="false"
SKIP_DEPS=0
WITH_SPIDERMONKEY="true"

run_erlang() {
erl -noshell -eval "$1" -eval "halt()."
}

COUCHDB_USER="$(whoami 2>/dev/null || echo couchdb)"
JS_ENGINE=${JS_ENGINE:-"spidermonkey"}
SM_VSN=${SM_VSN:-"91"}
CLOUSEAU_MTH=${CLOUSEAU_MTH:-"dist"}
CLOUSEAU_URI=${CLOUSEAU_URI:-"https://github.com/cloudant-labs/clouseau/releases/download/%s/clouseau-%s-dist.zip"}
Expand Down Expand Up @@ -75,6 +77,8 @@ Options:
--rebar=PATH use rebar by specified path (version >=2.6.0 && <3.0 required)
--rebar3=PATH use rebar3 by specified path
--erlfmt=PATH use erlfmt by specified path
--js-engine=ENGINE default js engine: spidermonkey or quickjs
--disable-spidermonkey disable spidermonkey, don't try to build it
EOF
}

Expand Down Expand Up @@ -221,6 +225,28 @@ parse_opts() {
printf 'ERROR: "--spidermonkey-version" requires a non-empty argument.\n' >&2
exit 1
;;
--js-engine)
if [ -n "$2" ]; then
eval JS_ENGINE=$2
shift 2
continue
else
printf 'ERROR: "--js-engine" requires a non-empty argument.\n' >&2
exit 1
fi
;;
--js-engine=?*)
eval JS_ENGINE=${1#*=}
;;
--js-engine=)
printf 'ERROR: "--js-engine" requires a non-empty argument.\n' >&2
exit 1
;;
--disable-spidermonkey)
WITH_SPIDERMONKEY="false"
shift
continue
;;

--clouseau-version)
if [ -n "$2" ]; then
Expand Down Expand Up @@ -293,12 +319,12 @@ parse_opts() {

parse_opts $@

if [ "${ARCH}" = "aarch64" ] && [ "${SM_VSN}" = "60" ]; then
if [ "${WITH_SPIDERMONKEY}" = "true" ] && [ "${ARCH}" = "aarch64" ] && [ "${SM_VSN}" = "60" ]; then
echo "ERROR: SpiderMonkey 60 is known broken on ARM 64 (aarch64). Use another version instead."
exit 1
fi

if [ "${ERLANG_OS}" = "unix" ]; then
if [ "${WITH_SPIDERMONKEY}" = "true" ] && [ "${ERLANG_OS}" = "unix" ]; then
case "${SM_VSN}" in
1.8.5)
SM_HEADERS="js"
Expand All @@ -316,11 +342,24 @@ if [ "${ERLANG_OS}" = "unix" ]; then
fi
fi

# If spidermonkey was disabled but JS_ENGINE set to "spidermonkey", reset it to "quickjs"
if [ "${WITH_SPIDERMONKEY}" = "false" ] && [ "${JS_ENGINE}" = "spidermonkey" ]; then
echo "NOTICE: Spidermonkey was disabled, but JS_ENGINE=spidermonkey. Setting JS_ENGINE=quickjs"
JS_ENGINE="quickjs"
fi

# If we're in a release tarball and we don't have proper, then mark it as skipped
if [ ! -d .git ] && [ "$WITH_PROPER" = "true" ] && [ ! -d src/proper ]; then
WITH_PROPER="false"
fi

# If we're in a release tarball and we don't have spidermonkey, then mark it as skipped and enable quickjs
if [ ! -d .git ] && [ "$WITH_SPIDERMONKEY" = "true" ] && [ ! -d src/couch/priv/couch_js ]; then
echo "NOTICE: Spidermonkey was disabled in release tarball. Setting JS_ENGINE=quickjs"
WITH_SPIDERMONKEY="false"
JS_ENGINE="quickjs"
fi

echo "==> configuring couchdb in rel/couchdb.config"
cat > rel/couchdb.config << EOF
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down Expand Up @@ -350,7 +389,9 @@ cat > rel/couchdb.config << EOF
{log_file, "$LOG_FILE"}.
{fauxton_root, "./share/www"}.
{user, "$COUCHDB_USER"}.
{js_engine, "$JS_ENGINE"}.
{spidermonkey_version, "$SM_VSN"}.
{with_spidermonkey, "$WITH_SPIDERMONKEY"}.
{node_name, "-name couchdb@127.0.0.1"}.
{cluster_port, 5984}.
{backend_port, 5986}.
Expand Down Expand Up @@ -380,13 +421,17 @@ with_nouveau = $WITH_NOUVEAU
with_clouseau = $WITH_CLOUSEAU
user = $COUCHDB_USER
js_engine = $JS_ENGINE
spidermonkey_version = $SM_VSN
with_spidermonkey = $WITH_SPIDERMONKEY
EOF

cat > $rootdir/config.erl << EOF
{with_proper, $WITH_PROPER}.
{erlang_md5, $ERLANG_MD5}.
{js_engine, "$JS_ENGINE"}.
{spidermonkey_version, "$SM_VSN"}.
{with_spidermonkey, "$WITH_SPIDERMONKEY"}.
EOF

install_local_rebar() {
Expand Down
23 changes: 23 additions & 0 deletions configure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
-DisableFauxton request build process skip building Fauxton (default false)
-DisableDocs request build process skip building documentation (default false)
-DisableSpiderMonkey do not use SpiderMonkey as JS engine (default false)
-EnableNouveau enable the new experiemtal search module (default false)
-EnableClouseau enable the Clouseau search module (default false)
-SkipDeps do not update Erlang dependencies (default false)
-CouchDBUser USER set the username to run as (defaults to current user)
-SpiderMonkeyVersion VSN select the version of SpiderMonkey to use (default 91)
-JSEngine ENGINE select JS engine to use (spidermonkey or quickjs) (default spidermonkey)
-ClouseauVersion VSN select the version of Clouseau to use (default 2.22.0)
-ClouseauMethod MTH method for Clouseau to deploy: git or dist (default dist)
-ClouseauUri URI location for retrieving Clouseau (default https://github.com/cloudant-labs/clouseau/releases/download/2.22.0/clouseau-2.22.0-dist.zip)
Expand Down Expand Up @@ -52,13 +54,16 @@ Param(
[switch]$EnableClouseau = $false, # do not use Clouseau by default
[switch]$SkipDeps = $false, # do not update erlang dependencies
[switch]$DisableProper = $false, # a compilation pragma. proper is a kind of automated test suite
[switch]$DisableSpiderMonkey = $false, # do not use SpiderMonkey as JS engine
[switch]$EnableErlangMD5 = $false, # don't use Erlang for md5 hash operations by default

[ValidateNotNullOrEmpty()]
[string]$CouchDBUser = [Environment]::UserName, # set the username to run as (defaults to current user)
[ValidateNotNullOrEmpty()]
[string]$SpiderMonkeyVersion = "91", # select the version of SpiderMonkey to use (default 91)
[ValidateNotNullOrEmpty()]
[string]$JSEngine = "spidermonkey", # select the JS engine (spidermonkey | quickjs) to use (default spidermonkey)
[ValidateNotNullOrEmpty()]
[string]$ClouseauMethod = "dist", # method for Clouseau to deploy: git or dist (default dist)
[ValidateNotNullOrEmpty()]
[string]$ClouseauVersion = "2.22.0", # select the version of Clouseau to use (default 2.22.0)
Expand Down Expand Up @@ -145,6 +150,17 @@ $WithClouseau = $(If ($EnableClouseau) {1} else {0})
$Hostname = [System.Net.Dns]::GetHostEntry([string]"localhost").HostName
$WithProper = (-not $DisableProper).ToString().ToLower()
$ErlangMD5 = ($EnableErlangMD5).ToString().ToLower()
$WithSpiderMonkey = (-not $DisableSpiderMonkey).ToString().ToLower()

if ($JSEngine -eq "quickjs") {
$WithSpiderMonkey = "false"
}

# If spidermonkey was disabled but JS_ENGINE set to "spidermonkey", reset it to "quickjs"
if ( ($WithSpiderMonkey -eq "false" ) -and ($JS_ENGINE -eq "spidermonkey" ) ) {
Write-Verbose "NOTICE: Spidermonkey was disabled, but JS_ENGINE=spidermonkey. Setting JS_ENGINE=quickjs"
$JS_ENGINE = "quickjs"
}

Write-Verbose "==> configuring couchdb in rel\couchdb.config"
$CouchDBConfig = @"
Expand Down Expand Up @@ -175,7 +191,9 @@ $CouchDBConfig = @"
{log_file, ""}.
{fauxton_root, "./share/www"}.
{user, "$CouchDBUser"}.
{js_engine, "$JSEngine"}.
{spidermonkey_version, "$SpiderMonkeyVersion"}.
{with_spidermonkey, "$WithSpiderMonkey"}.
{node_name, "-name couchdb@127.0.0.1"}.
{cluster_port, 5984}.
{backend_port, 5986}.
Expand Down Expand Up @@ -221,14 +239,19 @@ with_nouveau = $WithNouveau
with_clouseau = $WithClouseau
user = $CouchDBUser
js_engine = $JSEngine
spidermonkey_version = $SpiderMonkeyVersion
with_spidermonkey = $WithSpiderMonkey
"@
$InstallMk | Out-File "$rootdir\install.mk" -encoding ascii

$ConfigERL = @"
{with_proper, $WithProper}.
{erlang_md5, $ErlangMD5}.
{js_engine, "$JSEngine"}.
{spidermonkey_version, "$SpiderMonkeyVersion"}.
{with_spidermonkey, "$WithSpiderMonkey"}.
"@
$ConfigERL | Out-File "$rootdir\config.erl" -encoding ascii

Expand Down
1 change: 1 addition & 0 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ SubDirs = [
"src/b64url",
"src/exxhash",
"src/ets_lru",
"src/couch_quickjs",
"src/chttpd",
"src/couch",
"src/couch_event",
Expand Down

0 comments on commit 0c339bf

Please sign in to comment.