-
|
Hello! I couldn't find anything in the docs for this, if I wanted to serialize a user's function and make it available in other places they are using mathjs I think I'd have to store the line as a string and recompile it each time I load the calculation. in one place: let source1 = `
z=1
foo(x,y)=x+y
bar=foo(1,2)
`;
let scope = {};
math.evaluate(source1, scope);
serializeScope(scope)then somewhere else (eg different web request) I would love this to work: let otherSource = 'aval=foo(1,2)'
let scope = loadScope(..);
math.evaluate(otherSource, scope)Do i have the right understanding here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
That is an interesting case. When serializing (see docs), only data types can be serialized, and not functions. JavaScript functions can have bindings from outside, which would break when serializing. So it's not trivial to restore such a function. Also functions created inside the expression parser itself do have bindings. A pragmatic solution for now could be to create a wrapper around the function Fo the long term: a solution could be to extend functions created in the exprssion parser with a method |
Beta Was this translation helpful? Give feedback.

That is an interesting case. When serializing (see docs), only data types can be serialized, and not functions. JavaScript functions can have bindings from outside, which would break when serializing. So it's not trivial to restore such a function. Also functions created inside the expression parser itself do have bindings.
A pragmatic solution for now could be to create a wrapper around the function
evaluate, remember all expressions passed toevaluate, and simply store the orginal expressions. To restore this, all expressions can be executed again.Fo the long term: a solution could be to extend functions created in the exprssion parser with a method
.toJSONinto something like{"mathjs…