Function.toString()

71 views
Skip to first unread message

johnj...@chromium.org

unread,
Apr 26, 2013, 8:48:37 PM4/26/13
to traceur-comp...@googlegroups.com
Any thoughts on how to handle Function.toString() in a transcoded system? If one does not overwrite the default runtime behavior, the result will be transcoded source. This will either be a surprise or a bug, depending upon the use case.

I wonder if it is practical to overwrite each function object's |toString| method to return the un-transcoded source as a source transformation?

jjb

Erik Arvidsson

unread,
Apr 26, 2013, 8:56:34 PM4/26/13
to traceur-comp...@googlegroups.com
I would not bother. What is the use case?
> --
> You received this message because you are subscribed to the Google Groups
> "traceur-compiler-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to traceur-compiler-d...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
erik

John J Barton

unread,
Apr 29, 2013, 2:46:42 PM4/29/13
to traceur-comp...@googlegroups.com
Function.toString() is used in meta-programming because it allows one to use conventional dev tools and syntax checking for at least the static parts of your program.

I use it to create code to run in the Web page I am debugging. Devtools uses it for the same purpose and to generate stub-support for its remote protocol.  I've seen it used in error message handling as well.

jjb

johnj...@chromium.org

unread,
Apr 30, 2013, 1:12:35 PM4/30/13
to traceur-comp...@googlegroups.com
Turns out this was not too difficult to implement. 

I had one other constraint: transcoding a single function declaration must result in a single function declaration, not eg a store into a secret global table and a function. So while trancoding I grab the source of the function:
    _encodedSource: function(location) {
      return escape(location.start.source.contents.substring(location.start.offset, location.end.offset));
    },

and add a line to the function block that looks like: 

var __qp_functionToString = "function%20testMe%28%29%20%7B%0A%20%20%20%20return%20true%3B%0A%20%20%7D";

In my overriding Function.prototype.toString() I call the built in function to get the transcoded version, clip out the string above and return the unescaped version.

Not pretty but that's how shim runtimes are...

jjb

Erik Arvidsson

unread,
Apr 30, 2013, 1:47:13 PM4/30/13
to traceur-comp...@googlegroups.com
Could you use the source map to do this somehow?

John J Barton

unread,
Apr 30, 2013, 2:07:06 PM4/30/13
to traceur-comp...@googlegroups.com
Yes I think so. Rather than encoding the function source, I could encode just the start/end line/col. Then I could ship all of the source maps to the runtime and consult them to extract the original source.

Depending on how expensive it is to tokenize my var __qp_functionToString = long string, this could be somewhat faster than my version. 

A missing bit of technology that would make the sourcemap approach a winner is some way to synchronously call back from the runtime so that only the sourcemaps that actually are used need to be transferred.

Sourcemaps are more about communicating information from an external transcoder to the debugger. In my case the transcoder is sorta built-in to the debugger so I can do things that would normally require sourcemaps more directly.

jjb
Reply all
Reply to author
Forward
0 new messages