Closed freddi301 closed 6 years ago
Dupe of #5.
I modified the issue. I think this proposal could have a broader scope. Please consider re-reading @domenic
Thanks. I'm not interested in working on the problem of user-accessible string or AST serializations, partially because it defeats the performance benefits making it unusable for the use cases I'm hoping to solve. Sounds like a separate proposal to me. (Or you can just use .name and .toString() instead of .reify().)
It's more about making the whole language serializable (clojures, builtin instances like Date...).
The engine has anyway access to all the needed information (it's enough to see a js debugger in action) at runtime to serialize let's say a clojure, so the cost will be paid only at serialization time.
So the thing you want is the guarantee is that block of code doesn't contain free variables.
Here a jslint/eslint rule can do the job:
function add(a){ noclojure: { return b => a + b } }
// ---------------------------------------^--------
// non local variable "a" used in a serializable code block
Then it could be serialized as
const task = async () => {
noclojure: {
const res = await fetch("people.json");
const json = await res.json();
return json[2].firstName;
}
};
const stringSerializedTask = task.toString()
// or
task |> serialize |> sendOverWireOrToWorker
where serialize can return something like this for performance reasons.
Instead of introducing new syntax for variable capture a technique can be
const task = () => {
noclojure: {
return async file => {
const res = await fetch("people.json");
const json = await res.json();
return json[2].firstName;
};
}
};
worker.postMessage({ task: task.toSource(), parameters: ["people.json"] })
worker file
onmessage = ({ data: { task, parameters } }) => eval(task)()(...parameters)
where eval could be a more performant version of eval
which doesn't need access to outer scope (JS engines parses functions lazily anyway).
Yep, I understand. You're proposing a different proposal, and I encourage you to do that in your own repository, not this one.
I think this proposal could be less intrusive. Just let's suppose every object has a function to serialize itself into a JSON like
Calling this method on a function
We can then transfer the JSON over the wire or between workers.
Then on the other end
If there are some closures, they get resolved when we call [
Symbol.reify
]()Basically implementing reify method everywhere, we could have javascript fully serializable.