Giter VIP home page Giter VIP logo

Comments (12)

vdjeric avatar vdjeric commented on May 27, 2024 4

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.

ExE-Boss avatar ExE-Boss commented on May 27, 2024 2

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.

guybedford avatar guybedford commented on May 27, 2024 2

from proposal-binary-ast.

xorgy avatar xorgy commented on May 27, 2024 1

@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 evaling some garbage they got from Function.prototype.toString().

from proposal-binary-ast.

fabiosantoscode avatar fabiosantoscode commented on May 27, 2024 1

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.

rossberg avatar rossberg commented on May 27, 2024 1

@fabiosantoscode:

  • 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.

fabiosantoscode avatar fabiosantoscode commented on May 27, 2024 1

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.

myshov avatar myshov commented on May 27, 2024

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.

ExE-Boss avatar ExE-Boss commented on May 27, 2024

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.

xorgy avatar xorgy commented on May 27, 2024

@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.

fabiosantoscode avatar fabiosantoscode commented on May 27, 2024

@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.

Keyacom avatar Keyacom commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.