4 ms·
Fun project. From examining the source code, they are using the technique that can be considered a kind of higher order abstract syntax (HOAS). In particular,
by lambdapie 11y ago
Fun project. From examining the source code, they are using the technique that can be considered a kind of higher order abstract syntax (HOAS). In particular, LJSON can be fooled by so called exotic terms. E.g.
LJSON.stringify(function(v) { return v; })
evaluates to the string
(function(v0){return v0})
While
LJSON.stringify(function(v) { return v(null) } )
Evaluates to the string
(function(v0){return "v0"})
In the latter case, v(null) was able to examine the variable that stringify injects into the function. In this case, v(null) is the name of the injected variable.
- LightMachine 11y agoVery good catch. Any suggestion how I could improve the usage of null for determining the end of a call list?
- lambdapie 11y agoActually there are other problems that are caused by the fact that you're not really doing HOAS, but something even more difficult, namely trying to infer the function by passing in values. For example LJSON.stringify(function(v) { if (v == 0) { return "zero"; } else { return "one"; } }) Evaluates to the string (function(v0){return "one"}) The only way to avoid both these issues, is to inspect the code of a function f using f.toString(). However you would need to consider the scope where f was defined.
- LightMachine 11y agoAh, fair enough. The user should avoid using native JS constructs on the code to be stringified, though - it is an interesting/useful side effect of the method that you are able to, but ideally you would define your functions using only the pure λ-calculus grammar. Thanks for figuring that out!