On Mar 15, 2009, at 10:13 AM, Tobie Langel wrote:
Added Ruby and Python debug string rendering of methods.
Ruby: http://gist.github.com/78355
Python: http://gist.github.com/78424
Please let me know if you would like those resources included in the
mailing list of if external references are fine.
Links work for me. But here is the uneval/toSource equivalent, inline,
using the same notation (single quotes around string results, although
all results are string results, in a one-line comment starting //->
after the repr call):
function repr(v) {
print(uneval(v))
}
repr(false)
//-> 'false'
repr(true)
//-> 'true'
repr(null)
//-> 'null'
repr(undefined)
//-> '(void 0)'
repr('foo')
//-> '"foo"'
repr(123)
//-> '123'
repr(123.4)
//-> '123.4'
r = new RegExp('foo')
repr(r)
//-> '/foo/'
repr(new Date)
//-> '(new Date(1237141453430))'
repr([0, 1, [2, 3]])
//-> '[0, 1, [2, 3]]'
repr({foo: 123})
//-> '({foo:123})'
a = [1, 2, 3]
a.push(a)
repr(a)
//-> '#1=[1, 2, 3, #1#]'
h = {foo: 123}
h.bar = h
repr(h)
//-> '#1={foo:123, bar:#1#}'
function Foo() {
var self = {}
self.__init__ = function () {
self.foo = 123
self.bar = 456
}
return self
}
repr(Foo())
//-> '({__init__:(function () {self.foo = 123;self.bar = 456;})})'
repr(Foo)
//-> 'function Foo() {var self = {};self.__init__ = function ()
{self.foo = 123;self.bar = 456;};return self;}'
repr([].reverse)
//-> 'function reverse() {[native code]}'
Since undefined was added as a global property after ES1, when there
was too much risk of a web page defining a same-named global, it's
read/write and deletable, so uneval(undefined) produces '(void 0)'.
Note also that unlike toString, the function toSource results squish
code onto one line. This can get ugly, but it was considered
convenient for short functions. Perhaps it was a mistake to do this,
though.
Sharp variables handle cycles and join points but they are overkill
for an inspect-like utility, and they require aggressive memoization
to avoid divergence calling non-idempotent getters or otherwise
dealing with object graphs that modify themselves (via SpiderMonkey's
internal metaprogramming hooks) as they are crawled to find the join
points.
Comments welcome.
/be
_______________________________________________
Es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss