Skip to content

"cannot coerce undefined to object" - Spread into array syntax issues #140

Description

@lll000111

EDIT Very small reproducible example added in a comment below.


I have a weird problem that running in isolation works just fine.

Code

(Excerpt)

  if (thing.referenceTo !== undefined) {
    trace(thing.referenceTo + '\n');
    for (let entry of thing.referenceTo) {
      trace(entry + '\n');
    }

    [...thing.referenceTo];  // LOCATION OF ERROR, line #178

thing.referenceTo is a Set with a single string value "*" inside as shown by the trace output below.

Output

I wish I could copy & paste from the console in xsbug....

[object Set]
'*'
the-file.js (178) # Break: the-function: cannot coerce undefined to object!

The same thing happens in another place (the case is resched because Object.prototype.toString.call returned string "Set", which was extracted, so obj is sure to be a Set):

case 'Set':
      return actualStringify([...obj], seenObjects);

Just for the record, all of that is well-tested, the exact same code, just a mini-example of our stuff, runs on node.js browser, react-native - and even low.js (on this board).

Works when run in isolation

I tried to run this as an isolated example:

function isString(thing) {
  return typeof thing === 'string';
}

const thing = {
  referenceTo: new Set(['*'])
};

trace(thing.referenceTo + '\n');

for (let entry of thing.referenceTo) {
  trace(entry + '\n');
}

if (
  thing.referenceTo !== undefined &&
  (
    !(thing.referenceTo instanceof Set) ||
    ![...thing.referenceTo].every(type => isString(type))
  )
) {
  throw new Error('Error');
}

but this worked fine.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed - please verifyIssue has been fixed. Please verify and close.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions