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

VERIFIED FIXED in 1.5R5

Status

Rhino
Core
--
enhancement
VERIFIED FIXED
15 years ago
15 years ago

People

(Reporter: Igor Bukanov, Assigned: Igor Bukanov)

Tracking

Details

Attachments

(3 attachments, 2 obsolete attachments)

(Assignee)

Description

15 years ago
It would be nice if Rhino would support Object.prototype.toSource() and uneval()
as defined in SpiderMonkey.
(Assignee)

Comment 1

15 years ago
Created attachment 135329 [details] [diff] [review]
Implementation

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.
(Assignee)

Comment 2

15 years ago
Created attachment 135396 [details] [diff] [review]
uneval(-0) => -0 and someFunction.toSource(0) == someFunction.toSource(0)

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;
	  }
(Assignee)

Updated

15 years ago
Attachment #135329 - Attachment is obsolete: true
(Assignee)

Comment 3

15 years ago
Created attachment 135407 [details] [diff] [review]
ScriptableObject.toSource defaults to ({}) form

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
(Assignee)

Comment 4

15 years ago
I committed the latest patch
Status: NEW → RESOLVED
Last Resolved: 15 years ago
Resolution: --- → FIXED
(Assignee)

Comment 5

15 years ago
Created attachment 135413 [details] [diff] [review]
No need to skip toSource/uneval tests in rhino-n.tests

Now Rhino does not need to skip toSource/uneval tests from the test suite

Comment 6

15 years ago
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
(Assignee)

Comment 7

15 years ago
Created attachment 135729 [details] [diff] [review]
Fix to exactly match SM during nested toSource

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.
(Assignee)

Comment 8

15 years ago
I committed the last fix as well.
(Assignee)

Comment 9

15 years ago
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.