Comments (12)
It seems unnecessary to me that Function.prototype.toString() returns a useless placeholder token [...] there is at least some code which relies on Function.prototype.toString()
You are right and we want toString() in BinAST to work the same way as it does for plain JS but it's just a matter of implementation priorities. Right now we're working on a new file format for BinAST and decoder performance so we've shelved toString() for the timebeing.
from proposal-binary-ast.
You could always display the arguments, eg.: example toString()
result:
function example(foo, bar) {
[sourceless code]
}
That would avoid breaking the old Angular dependency injection.
from proposal-binary-ast.
from proposal-binary-ast.
@ExE-Boss That's an interesting middle ground, though I'm not sure it's quite right either, makes it necessary to use a regex to tell that the code isn't real, though I guess not all that many people are going to be accidentally eval
ing some garbage they got from Function.prototype.toString()
.
from proposal-binary-ast.
One of the things that make JavaScript great is the fact you can eval() most functions' string representations (plus a pair of parentheses) and get a function back. This is not recommended, but is still great for a lot of reasons:
- You can inline a function into a
data:
URL (which is useful, for say, creating a Worker on-demand) - You can read arguments and comments as said above and perform actions based upon the results
And probably more things I can't remember right now.
IMO the VM should generate a string representation from its internal AST, on-demand, when the toString
method is called.
from proposal-binary-ast.
- You can inline a function into a data: URL
That's a bug, not a feature. Unless you want to help attackers.
from proposal-binary-ast.
Also, I can't overstate how many times I use Function.prototype.toString
to read the source code of some unknown callback in the browser console or the node console.
from proposal-binary-ast.
Yes I agree with @xorgy. There was usage of Function.prototype.toString()
at least in the first version of Angular for implementation of dependency injection.
from proposal-binary-ast.
makes it necessary to use a regex to tell that the code isn't real
You already need to do that with native functions, where the result is slightly different between browsers: tc39/Function-prototype-toString-revision#21.
Also, the ESNext native function toString()
method will optionally be returning function parameters.
from proposal-binary-ast.
@vdjeric Fair enough, though I suspect regardless of what BinAST looks like, it is not likely to be difficult for implementers to write some sort of toString, especially the sort that @ExE-Boss is describing (since there's not much that can be done to collapse arguments, especially with object destructuring, so presumably names will be intact).
As long as a slightly more useful Function.prototype.toString
is not off the table, I'm happy. :- )
from proposal-binary-ast.
@rossberg not like a security issue, more like this:
const worker = new Worker('data:text/javascript,(' + () => {
/* worker code goes here... */
} + ')()')
In this example, it helps you define a worker next to the place it's going to be used and not in a new file. There's probably more interesting things you can do with data-urls and toString. I can't see how this is a bug.
from proposal-binary-ast.
If there is need to have Function.prototype.toString()
compatible with eval()
, it would be best for comments to not be saved in the AST, because they are not executed.
Python has AST-relevant public APIs built into the standard library as the ast
module, and it's evident that the Python AST parser doesn't save comments (Python uses #
to denote them):
import ast
print(ast.unparse(ast.parse("""def a():
# does stuff
return 0""")))
which outputs:
def a():
return 0
The comment isn't there because it's not necessary for the function to work. Docstrings are preserved, though.
from proposal-binary-ast.
Related Issues (20)
- .length property should be available for lazy function HOT 3
- Can we have `AssertedPositionalParameterName` interface which contains index? HOT 5
- Can we put more information into AssertedParameterScope ? HOT 2
- Why do some interfaces not inherit from Node? HOT 8
- (Optional) type information HOT 2
- Add comments to length field of functions. HOT 1
- Transfer repo to TC39 org HOT 5
- Add verification of property names vs identifiers
- What should be put into AssertedParameterScope for duplicate parameters? HOT 6
- Consider merging LiteralInfinityExpression into LiteralNumericExpression HOT 3
- Could the binary AST be...a bit more binary? HOT 1
- Consider splitting `LiteralNumericExpression` to `LiteralInt32Expression` + `LiteralFloatExpression`
- Could the AST be based on a reduced subset of productions? HOT 1
- Consider a compressed file format with pre-shared dictionary.
- Modify IDL order if all JS engines benefit
- [question] I have a question that Binary AST file can be translated directly to bytecode or it should be translated to standard AST and then translated to bytecode in browser? HOT 2
- What is the current status of this proposal? HOT 12
- Solved by W3C EXI? HOT 3
- Request for opinion on alternative HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-binary-ast.