Skip to content

Commit e6db0ff

Browse files
committed
Fix scala-js#3685: Rename js.special.globalThis to fileLevelThis.
As ECMAScript is about to receive a new global variable called `globalThis`, that is not equivalent to our `js.special.globalThis`, we need to rename the latter.
1 parent d9fb84c commit e6db0ff

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

library/src/main/scala/scala/scalajs/js/special/package.scala

+50-7
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,56 @@ package object special {
4141
def delete(obj: scala.Any, key: scala.Any): Unit =
4242
throw new java.lang.Error("stub")
4343

44-
/** The value of the global JavaScript `this`.
44+
/** The value of the JavaScript `this` at the top-level of the generated
45+
* file.
4546
*
4647
* This returns the value that would be obtained by writing `this` at the
47-
* top-level of the JavaScript file generated by Scala.js. In most
48-
* JavaScript environments, this is equivalent to the *global object*, but
49-
* it is not necessarily the case. For example, on Node.js, `this` is the
50-
* object representing the `exports` of the current module.
48+
* top-level of the JavaScript file generated by Scala.js. In scripts, this
49+
* is equivalent to the *global object*, but in other environments, it is
50+
* not necessarily the case. For example, in CommonJS modules on Node.js,
51+
* `this` is the object representing the `exports` of the current module,
52+
* and it is `undefined` in ECMAScript modules.
5153
*
5254
* Using this value should be rare, and mostly limited to writing code
5355
* detecting what the global object is. For example, a typical detection
54-
* code looks like:
56+
* code--in case we do not need to worry of ES modules--looks like:
57+
* {{{
58+
* val globalObject = {
59+
* import js.Dynamic.{global => g}
60+
* if (js.typeOf(g.global) != "undefined" && (g.global.Object eq g.Object)) {
61+
* // Node.js environment detected
62+
* g.global
63+
* } else {
64+
* // In all other well-known environment, we can use the global `this`
65+
* js.special.fileLevelThis
66+
* }
67+
* }
68+
* }}}
69+
* Note that the above code is not comprehensive, as there can be JavaScript
70+
* environments where the global object cannot be fetched neither through
71+
* `global` nor `this`. If your code needs to run in such an environment, it
72+
* is up to you to use an appropriate detection procedure.
73+
*/
74+
@inline
75+
def fileLevelThis: scala.Any =
76+
scala.scalajs.runtime.linkingInfo.globalThis
77+
78+
/** The value of the JavaScript `this` at the top-level of the generated
79+
* file.
80+
*
81+
* *Deprecated*: this value does not correspond to JavaScript's
82+
* `globalThis` value. Use `fileLevelThis` instead.
83+
*
84+
* This returns the value that would be obtained by writing `this` at the
85+
* top-level of the JavaScript file generated by Scala.js. In scripts, this
86+
* is equivalent to the *global object*, but in other environments, it is
87+
* not necessarily the case. For example, in CommonJS modules on Node.js,
88+
* `this` is the object representing the `exports` of the current module,
89+
* and it is `undefined` in ECMAScript modules.
90+
*
91+
* Using this value should be rare, and mostly limited to writing code
92+
* detecting what the global object is. For example, a typical detection
93+
* code--in case we do not need to worry of ES modules--looks like:
5594
* {{{
5695
* val globalObject = {
5796
* import js.Dynamic.{global => g}
@@ -69,9 +108,13 @@ package object special {
69108
* `global` nor `this`. If your code needs to run in such an environment, it
70109
* is up to you to use an appropriate detection procedure.
71110
*/
111+
@deprecated(
112+
"Does not correspond to JavaScript's globalThis. " +
113+
"Use fileLevelThis instead.",
114+
"0.6.29")
72115
@inline
73116
def globalThis: scala.Any =
74-
scala.scalajs.runtime.linkingInfo.globalThis
117+
fileLevelThis
75118

76119
/** Exact equivalent of the `debugger` keyword of JavaScript.
77120
*

test-suite/js/src/test/scala/org/scalajs/testsuite/jsinterop/SpecialTest.scala

+9-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class SpecialTest {
6060
js.special.delete(a[js.Object]("foo"), kh.key)
6161
}
6262

63-
// js.special.globalThis
63+
// js.special.fileLevelThis
6464

65-
@Test def globalThis_can_be_used_to_detect_the_global_object(): Unit = {
65+
@Test def fileLevelThis_can_be_used_to_detect_the_global_object(): Unit = {
6666
val globalObject = {
6767
import js.Dynamic.{global => g}
6868
// We've got to use selectDynamic explicitly not to crash Scala 2.10
@@ -72,13 +72,19 @@ class SpecialTest {
7272
g.selectDynamic("global")
7373
} else {
7474
// In all other well-known environment, we can use the global `this`
75-
js.special.globalThis
75+
js.special.fileLevelThis
7676
}
7777
}
7878

7979
assertSame(js.Dynamic.global, globalObject)
8080
}
8181

82+
// js.special.globalThis (deprecated)
83+
84+
@Test def globalThis_is_fileLevelThis(): Unit = {
85+
assertSame(js.special.fileLevelThis, js.special.globalThis)
86+
}
87+
8288
// js.special.debugger
8389

8490
@Test def should_support_debugger_statements_through_the_whole_pipeline_issue_1402(): Unit = {

0 commit comments

Comments
 (0)