Skip to content

Method shorthand in object literal loses method name in toString() output #2318

@LoyDgIk

Description

@LoyDgIk

Description

When using method shorthand syntax (c(){}) inside an object literal in Rhino, calling toString() on the method returns "(){}" (missing the method name). In contrast, the traditional function expression (b: function(){}) correctly returns "function b(){}". This behavior differs from modern JavaScript engines (e.g., V8) which output the method name ("c(){}"). The issue likely stems from incomplete ES6 support in Rhino’s function representation.

Steps to Reproduce

  1. toString() output lacks the method name
    cff.c.toString() returns "(){}" instead of the expected "c(){}".
    In contrast, the traditional function expression (b: function(){}) correctly returns "function b(){}".
  2. uneval() generates syntactically invalid code
    uneval(cff) returns "({c:(){}, b:function(){}})". The fragment c:(){} is not valid JavaScript syntax – it should be either c: function(){} or c(){}. Consequently, attempting to eval() this string results in a SyntaxError.

These issues arise from incomplete ES6 support in Rhino’s handling of method definitions and their serialization.

Steps to Reproduce

Run the following script in Rhino:

let cff = {
    c(){},
    b: function(){}
};
console.log(cff.c.toString());
console.log(cff.b.toString());
console.log(uneval(cff));
eval(uneval(cff));  // This will throw a SyntaxError

Expected Behavior

· cff.c.toString() → "c(){}" (or at least include the method name).
· cff.b.toString() → "function (){}".
· uneval(cff) → a valid object literal representation, for example:
· "({c(){}, b: function(){}})" (preserving shorthand syntax) or
· "({c: function(){}, b: function(){}})" (using traditional syntax).
· eval(uneval(cff)) should succeed and produce an equivalent object.

Actual Behavior

· cff.c.toString() → "(){}" (method name omitted).
· cff.b.toString() → "function (){}" (correct).
· uneval(cff) → "({c:(){}, b:function(){}})" (invalid syntax c:(){}).
· eval(uneval(cff)) → SyntaxError .

Environment

· Rhino version: 1.9.0+ (tested with 1.9.0 and later releases)
· system: android

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions