Closed Bug 225465 Opened 21 years ago Closed 21 years ago

Support for Object.prototype.toSource() and uneval()

Categories

(Rhino Graveyard :: Core, enhancement)

enhancement
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: igor, Assigned: igor)

Details

Attachments

(3 files, 2 obsolete files)

It would be nice if Rhino would support Object.prototype.toSource() and uneval()
as defined in SpiderMonkey.
Attached patch Implementation (obsolete) β€” β€” Splinter Review
The patch follows SpiderMonkey semantics with regarding to toSource/uneval and
passes the following tests that currently excluded from the test suite for
Rhino:

#toSource tests
js1_5/Regress/regress-44009.js
js1_5/Object/regress-90596-001.js
js1_5/Object/regress-96284-001.js

#uneval tests
js1_5/Object/regress-90596-002.js
js1_5/Object/regress-96284-002.js


The implementation adds new method to ScriptableObject:

protected String toSourceImplementation(Context cx, Scriptable scope)

which is called from Object.prototype.toSource() if thisObj is instance of of
ScriptableObject. If not, then toString() is used. In this way it was not
necessary to define separated toSource JS method in each and every standard
object, but rather simple overriding of toSourceImplementation was enough.

In the patch toSourceImplementation by default calls toString() and only
NativeObject provides serialization in form of {name:value ...} so if the scope
is not a subclass of NativeObject (which is the case of Rhino shell), then
executing

var x = 1;
print(toSource())

would give [object global] rather then ({x:1}) as happens in SM.

Perhaps it would make sense to follow SM in this area as well.
In this version of the patch I changed toSourceImplementation(Context,
Scriptable scope) into toSource(Context, Scriptable scope, Object[] args) so
BaseFunction can override toSource and get access to the original JS arguments
and be fully on pair with SpiderMonkey:

Rhino 1.5 release 5 0000 00 00
js> function f() { return 1; }
js> print(f.toSource())
function f() {return 1;}
js> print(f.toSource(0))

function f() {
    return 1;
}

js> print(f.toSource(10))

	  function f() {
	      return 1;
	  }
Attachment #135329 - Attachment is obsolete: true
I changed ScriptableObject.toSource not to return toString() but rather produce
({}) form of enumeratable properties since in this way uneval would produce by
default syntactically correct JS.
Attachment #135396 - Attachment is obsolete: true
I committed the latest patch
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
Now Rhino does not need to skip toSource/uneval tests from the test suite
Igor: thanks, I have checked in your patch for rhino-n.tests:

[//d/JS_TRUNK/mozilla/js/tests] cvs ci rhino-n.tests
Checking in rhino-n.tests;
/cvsroot/mozilla/js/tests/rhino-n.tests,v  <--  rhino-n.tests
new revision: 1.56; previous revision: 1.55
done


The five tests involved now all pass in Rhino; marking Verified FIXED -
Status: RESOLVED → VERIFIED
The patch changes toSource applied for objects not to put () arround nested
objects so uneval({x:{y: 0}}) gives 
({x:{y:0}})
and not 
({x:({y:0})}).

In this way Rhino matches SpiderMonkey and the test case for bug 225831 that
relies on a particular output of toSource() to simplify test code runs fine in
Rhino optimized mode.
I committed the last fix as well.
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: