3

As it is said in the ECMAScript spec, all JavaScript code must be associated with a realm. This spec entity, in turn, is related with the fabled global object. I have not found any prohibition on sharing a single global object between several realms.

So I wonder is it then possible, for example, to get true from the expression arg instanceof Array when arg was created within the scope of a different realm (I mean something like this, it's a sample with <iframe>).

Does not the ECMAScript guarantee that this sort of situations are unachievable?

Ilya Loskutov
  • 1,967
  • 2
  • 20
  • 34
  • Are you asking about sharing "only" the global object, or also sharing all the intrinsics? – Bergi Jul 28 '21 at 19:44

1 Answers1

2

It's not explicitly forbidden, but imo it's pointless. You'd rather share the entire realm.

However, §9.6 InitializeHostDefinedRealm() seems to suggest that every realm should have its own global object:

  1. If the host requires use of an exotic object to serve as realm's global object, let global be such an object created in a host-defined manner.

(emphasis mine)

But I'm probably reading too much into that. Also, I'm not quite sure if InitializeHostDefinedRealm() is the only entry point for a host that could be used to create a realm.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • To recap, there are situations when `arg instanceof Array` might return true (providing that `arg` was created outside the scope of the current realm): a) several realms share the single global object; b) all ECMAScript code is associated with one single realm; c) appropriate fields of several global objects (global environments) is referenced to the single instance of `Array`. It seems none of these situations are explicitly prohibited by the spec. – Ilya Loskutov Jul 29 '21 at 10:49