Thanks for the response. Here is my use case:
Google is deprecating ScriptDb (
https://developers.google.com/apps-script/guides/script-db/). We have a complete replacement (actually more powerful and fully indexed called ReasonDB). We would like to be able to pass queries over the wire in "native format" to keep the syntactic dissonance between client and server side programming low.
What we don't want to support is the evaluation of arbitrary Javascript, just the evaluation of named functions that are passed in, including nested calls, e.g.
{test: eq(3,max(3,0))}.
In reality what we are usually doing is creating deferred execution functions by returning other functions that are then evaluated by the database, e.g. (but grossly over simplified):
given:
var functionMap = {eq: function(val) { return function(dbvalue) { return val===dbvalue; } }
var object = JSON5F.parse({age: eq(25),null,functionmap);
object will actually be {age: [function]} which when passed to the database will return all objects with age 25.
Obviously, someone could create an unsafe function and expose it through the functionMap. But, creating unsafe code can be done in many ways. Think of our approach as half way to a sandbox that can be exposed on the wire.
We have tried reviver functions and have found they require going completly declaritive, resulting in cumbersome JSON introducing a high level of dissonance as well as lots of quoted values and operators that have to be parsed and validated later anyway. There is actually another Javascript database that takes this approach,
TaffyDb.
Our approach can't be supported by Javascript sandboxes because it relies heavily on closure values that disappear when the sandbox is exited. Although we could theoretically put the entire database process inside a sandbox other limitations quickly come to the surface given the current state of Javascript sandboxes.
You can see a bit more about ReasonDB
here.