forked from apache/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QuickJS is a Javascript engine option
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 apache#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 apache#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 trasform 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). * Allow granular time (reduction) trackign if we wanted to provide a runtime allowance for each function. * Better sandboxing. Creating a whole JSRuntime takes only 300 microsecond, so we can afford to do that on reset. JSRuntimes cannot share JS data or object between them. For now to make it easy to experiment added it in as a JS "version" enabled at compile time. ``` ./configure --dev --spidermonkey-version quickjs && make ``` Only tested on MacOS and Linux. All `make check` tests pass there. QuickJS Patches: * Disable workers. (Brings in pthread, etc) * Enable spidermonkey 1.8.5 function expression mode
- Loading branch information
Showing
77 changed files
with
98,585 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// 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. | ||
|
||
function create_sandbox() { | ||
var sandbox = {}; | ||
sandbox.emit = Views.emit; | ||
sandbox.sum = Views.sum; | ||
sandbox.log = log; | ||
sandbox.toJSON = JSON.stringify; | ||
sandbox.JSON = JSON; | ||
sandbox.provides = Mime.provides; | ||
sandbox.registerType = Mime.registerType; | ||
sandbox.start = Render.start; | ||
sandbox.send = Render.send; | ||
sandbox.getRow = Render.getRow; | ||
sandbox.isArray = isArray; | ||
sandbox.index = Dreyfus.index; | ||
return sandbox; | ||
}; | ||
|
||
function create_filter_sandbox() { | ||
var sandbox = create_sandbox(); | ||
sandbox.emit = Filter.emit; | ||
return sandbox; | ||
}; | ||
|
||
function seal(obj, flag) { | ||
Object.freeze(obj); | ||
}; | ||
|
||
var DDoc = (function() { | ||
var ddoc_dispatch = { | ||
"lists" : Render.list, | ||
"shows" : Render.show, | ||
"filters" : Filter.filter, | ||
"views" : Filter.filter_view, | ||
"updates" : Render.update, | ||
"validate_doc_update" : Validate.validate, | ||
"rewrites" : Render.rewrite | ||
}; | ||
var ddocs = {}; | ||
return { | ||
ddoc : function() { | ||
var args = []; | ||
for (var i=0; i < arguments.length; i++) { | ||
args.push(arguments[i]); | ||
}; | ||
var ddocId = args.shift(); | ||
if (ddocId == "new") { | ||
// get the real ddocId. | ||
ddocId = args.shift(); | ||
// store the ddoc, functions are lazily compiled. | ||
ddocs[ddocId] = args.shift(); | ||
print("true"); | ||
} else { | ||
// Couch makes sure we know this ddoc already. | ||
var ddoc = ddocs[ddocId]; | ||
if (!ddoc) throw(["fatal", "query_protocol_error", "uncached design doc: "+ddocId]); | ||
var funPath = args.shift(); | ||
var cmd = funPath[0]; | ||
// the first member of the fun path determines the type of operation | ||
var funArgs = args.shift(); | ||
if (ddoc_dispatch[cmd]) { | ||
// get the function, call the command with it | ||
var point = ddoc; | ||
for (var i=0; i < funPath.length; i++) { | ||
if (i+1 == funPath.length) { | ||
var fun = point[funPath[i]]; | ||
if (!fun) { | ||
throw(["error","not_found", | ||
"missing " + funPath[0] + " function " + funPath[i] + | ||
" on design doc " + ddocId]); | ||
} | ||
if (typeof fun != "function") { | ||
// For filter_view we want the emit() function | ||
// to be overridden and just toggle a flag instead of | ||
// accumulating rows | ||
var sandbox = (cmd === "views") ? create_filter_sandbox() : create_sandbox(); | ||
fun = Couch.compileFunction(fun, ddoc, funPath.join('.'), sandbox); | ||
// cache the compiled fun on the ddoc | ||
point[funPath[i]] = fun; | ||
}; | ||
} else { | ||
point = point[funPath[i]]; | ||
} | ||
}; | ||
|
||
// run the correct responder with the cmd body | ||
ddoc_dispatch[cmd].apply(null, [fun, ddoc, funArgs]); | ||
} else { | ||
// unknown command, quit and hope the restarted version is better | ||
throw(["fatal", "unknown_command", "unknown ddoc command '" + cmd + "'"]); | ||
} | ||
} | ||
} | ||
}; | ||
})(); | ||
|
||
function handleError(e) { | ||
if (e === null) { | ||
// internal error, another possibility when out of memory | ||
// nothing to do except rethrow and let main.c catch it and exit(1) | ||
throw(null); | ||
} | ||
const type = e[0]; | ||
if (type == "fatal") { | ||
e[0] = "error"; // we tell the client it was a fatal error by dying | ||
respond(e); | ||
return false; | ||
} else if (type == "error") { | ||
respond(e); | ||
return true; | ||
} else if (e.error && e.reason) { | ||
// compatibility with old error format | ||
respond(["error", e.error, e.reason]); | ||
return true; | ||
} else if (e.name == "InternalError" && e.message == "out of memory") { | ||
// hard crash when out of memory | ||
respond(["error", e.name, e.message]); | ||
return false; | ||
} else if (e.name) { | ||
respond(["error", e.name, e]); | ||
return true; | ||
} else { | ||
respond(["error","unnamed_error", e.stack]); | ||
return true; | ||
} | ||
}; | ||
|
||
globalThis.cmd_dispatch = function(line) { | ||
const cmd = JSON.parse(line); | ||
State.line_length = line.length; | ||
try { | ||
switch (cmd.shift()) { | ||
case "ddoc": | ||
DDoc.ddoc.apply(null, cmd); | ||
break; | ||
case "reset": | ||
State.reset.apply(null, cmd); | ||
break; | ||
case "add_fun": | ||
State.addFun.apply(null, cmd); | ||
break; | ||
case "add_lib": | ||
State.addLib.apply(null, cmd); | ||
break; | ||
case "map_doc": | ||
Views.mapDoc.apply(null, cmd); | ||
break; | ||
case "index_doc": | ||
Dreyfus.indexDoc.apply(null, cmd); | ||
break; | ||
case "reduce": | ||
Views.reduce.apply(null, cmd); | ||
break; | ||
case "rereduce": | ||
Views.rereduce.apply(null, cmd); | ||
break; | ||
default: | ||
// unknown command, quit and hope the restarted version is better | ||
throw(["fatal", "unknown_command", "unknown command '" + cmdkey + "'"]); | ||
} | ||
} catch(e) { | ||
return handleError(e); | ||
}; | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/quickjs/examples/hello | ||
/quickjs/examples/hello_module | ||
/quickjs/examples/test_fib | ||
/quickjs/hello.c | ||
/quickjs/libquickjs.a | ||
/quickjs/libquickjs.lto.a | ||
/quickjs/qjs | ||
/quickjs/qjsc | ||
/quickjs/qjscalc | ||
/quickjs/qjscalc.c | ||
/quickjs/repl.c | ||
/quickjs/run-test262 | ||
/quickjs/test_fib.c | ||
*.dSYM/ | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
# Test compile for development | ||
# -Wno-format-truncation \ | ||
# | ||
gcc -fsanitize=address -fno-omit-frame-pointer -g3 -ggdb3 -gdwarf-3 -fstandalone-debug \ | ||
-flto -O0 -Wall -MMD -Wno-array-bounds -Wno-deprecated-declarations\ | ||
-Iquickjs \ | ||
-D_GNU_SOURCE -DCONFIG_BIGNUM=0 -DCONFIG_VERSION=\"quickjs\" \ | ||
quickjs/cutils.c quickjs/libbf.c quickjs/libregexp.c quickjs/libunicode.c quickjs/quickjs-libc.c quickjs/quickjs.c main.c \ | ||
-o main \ | ||
-lm | ||
|
||
#cp ./main ../../couchjs | ||
|
||
# -fno-string-normalize -fno-map -fno-promise -fno-typedarray -fno-typedarray -fno-regexp -fno-json -fno-eval -fno-proxy -fno-date -m -o examples/hello_module examples/hello_module.js | ||
|
||
# "-fsanitize=address -fno-omit-frame-pointer -g3 -ggdb3 -gdwarf-3 -fstandalone-debug -flto -Wall -MMD -Wno-array-bounds -Wno-format-truncation -D_GNU_SOURCE -DCONFIG_BIGNUM=0 -O0 -DCONFIG_VERSION=\\\"quickjs\\\" -Ipriv/couch_js/quickjs/quickjs", |
Oops, something went wrong.