Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 08fba0e

Browse files
author
exoego
committed
Overhaul global module and top level objects
1 parent 0108341 commit 08fba0e

File tree

9 files changed

+106
-146
lines changed

9 files changed

+106
-146
lines changed
Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.scalajs.nodejs
22

3-
import io.scalajs.nodejs.timers.{SetInterval, SetTimeout, UnRef, _}
3+
import io.scalajs.nodejs.timers._
44

55
import scala.scalajs.js
66

@@ -10,78 +10,42 @@ import scala.scalajs.js
1010
* var something inside an Node.js module will be local to that module.
1111
*/
1212
@js.native
13+
@deprecated("Use io.scalajs.nodejs package object", "0.9.0")
1314
trait Global extends js.Object {
14-
15-
/////////////////////////////////////////////////////////////////////////////////
16-
// Global Classes
17-
/////////////////////////////////////////////////////////////////////////////////
18-
19-
def DTRACE_NET_SERVER_CONNECTION: js.Function = js.native
20-
21-
def DTRACE_NET_STREAM_END: js.Function = js.native
22-
23-
def DTRACE_HTTP_SERVER_REQUEST: js.Function = js.native
24-
25-
def DTRACE_HTTP_SERVER_RESPONSE: js.Function = js.native
26-
27-
def DTRACE_HTTP_CLIENT_REQUEST: js.Function = js.native
28-
29-
def DTRACE_HTTP_CLIENT_RESPONSE: js.Function = js.native
30-
31-
/////////////////////////////////////////////////////////////////////////////////
32-
// Global Constants
33-
/////////////////////////////////////////////////////////////////////////////////
34-
35-
/**
36-
* The name of the directory that the currently executing script resides in.
37-
*/
38-
def __dirname: String = js.native
39-
40-
/**
41-
* The filename of the code being executed. This is the resolved absolute path of this code file. For a main program
42-
* this is not necessarily the same filename used in the command line. The value inside a module is the path to that
43-
* module file.
44-
*/
45-
def __filename: String = js.native
46-
47-
/**
48-
* A reference to the module.exports that is shorter to type. See module system documentation for details on when
49-
* to use exports and when to use module.exports.
50-
*
51-
* exports isn't actually a global but rather local to each module.
52-
*/
53-
val exports: js.Object = js.native
15+
@deprecated("Use io.scalajs.nodejs.exports", "0.9.0")
16+
def exports: js.UndefOr[js.Object] = js.native
5417

5518
/**
5619
* A reference to the current module. In particular module.exports is used for defining what a module exports and
5720
* makes available through require().
5821
*
5922
* module isn't actually a global but rather local to each module.
6023
*/
61-
val module: Module = js.native
62-
63-
/////////////////////////////////////////////////////////////////////////////////
64-
// Global Objects
65-
/////////////////////////////////////////////////////////////////////////////////
24+
@deprecated("Use io.scalajs.nodejs.module", "0.9.0")
25+
def module: Module = js.native
6626

27+
@deprecated("Use io.scalajs.nodejs.clearImmediate", "0.9.0")
6728
def clearImmediate: ClearImmediate = js.native
6829

30+
@deprecated("Use io.scalajs.nodejs.clearInterval", "0.9.0")
6931
def clearInterval: ClearInterval = js.native
7032

33+
@deprecated("Use io.scalajs.nodejs.clearTimeout", "0.9.0")
7134
def clearTimeout: ClearTimeout = js.native
7235

36+
@deprecated("Use io.scalajs.nodejs.console", "0.9.0")
7337
def console: console_module.Console = js.native
7438

39+
@deprecated("Use io.scalajs.nodejs.process", "0.9.0")
7540
def process: io.scalajs.nodejs.process.Process = js.native
7641

77-
def ref: Ref = js.native
78-
42+
@deprecated("Use io.scalajs.nodejs.setImmediate", "0.9.0")
7943
def setImmediate: SetImmediate = js.native
8044

45+
@deprecated("Use io.scalajs.nodejs.setInterval", "0.9.0")
8146
def setInterval: SetInterval = js.native
8247

48+
@deprecated("Use io.scalajs.nodejs.setTimeout", "0.9.0")
8349
def setTimeout: SetTimeout = js.native
8450

85-
def unref: UnRef = js.native
86-
8751
}

app/current/src/main/scala/io/scalajs/nodejs/Module.scala

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.scalajs.nodejs
22

3+
import com.thoughtworks.enableIf
4+
35
import scala.scalajs.js
4-
import scala.scalajs.js.annotation.JSGlobal
6+
import scala.scalajs.js.annotation.JSImport
57

68
/**
79
* In each module, the module free variable is a reference to the object representing the current module.
@@ -51,23 +53,31 @@ trait Module extends js.Object {
5153
*/
5254
var parent: js.Any = js.native
5355

56+
var paths: js.Array[String] = js.native
57+
5458
/**
5559
* The module.require method provides a way to load a module as if require() was called from the original module.
5660
* <p/><b>Note</b> that in order to do this, you must get a reference to the module object. Since require() returns the
5761
* module.exports, and the module is typically only available within a specific module's code, it must be
5862
* explicitly exported in order to be used.
5963
*/
60-
def require[T](id: String): T = js.native
64+
def require[T <: js.Any](id: String): T = js.native
6165

6266
}
6367

6468
/**
6569
* Module Companion
6670
*/
67-
object Module {
71+
@js.native
72+
@JSImport("module", JSImport.Namespace)
73+
object Module extends Module {
74+
75+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
76+
var builtinModules: js.Array[String] = js.native
6877

69-
@js.native
70-
@JSGlobal("module")
71-
implicit object module extends Module
78+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
79+
def createRequire(filename: String): Require = js.native
7280

81+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
82+
def createRequire(filename: io.scalajs.nodejs.url.URL): Require = js.native
7383
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.scalajs.nodejs
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSGlobal
5+
6+
@js.native
7+
sealed trait Require extends js.Object {
8+
def apply(id: String): js.Any = js.native
9+
10+
val cache: js.Dictionary[js.Any] = js.native
11+
val main: js.UndefOr[Module] = js.native
12+
val resolve: RequireResolver = js.native
13+
}
14+
15+
@js.native
16+
@JSGlobal("require")
17+
object Require extends Require
18+
19+
@js.native
20+
trait RequireResolver extends js.Object {
21+
def apply(request: String, options: ResolveOptions = js.native): js.Any = js.native
22+
23+
def paths(requiest: String): js.Array[String] = js.native
24+
}
25+
26+
class ResolveOptions(
27+
var paths: js.UndefOr[js.Array[String]] = js.undefined
28+
) extends js.Object

app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ package object fs {
2121
type Output = String | Buffer
2222
type FileWriteOptions = FileAppendOptions
2323

24+
type ReaddirArrays = js.Array[String] | js.Array[Buffer]
25+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
26+
type ReaddirArrays2 = ReaddirArrays | js.Array[fs.Dirent]
27+
2428
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
2529
type Dirent = Fs.Dirent
2630

Lines changed: 13 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.scalajs
22

33
import com.thoughtworks.enableIf
4-
import io.scalajs.nodejs.buffer.Buffer
54
import io.scalajs.nodejs.timers._
65

76
import scala.concurrent.duration.FiniteDuration
@@ -54,10 +53,6 @@ package object nodejs {
5453
// The handle object can be either a server, a socket (anything with an underlying _handle member), or an object with an fd member that is a valid file descriptor.
5554
type Handle = js.Function | HasHandle | HasFileDescriptor
5655

57-
type ReaddirArrays = js.Array[String] | js.Array[Buffer]
58-
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
59-
type ReaddirArrays2 = ReaddirArrays | js.Array[fs.Dirent]
60-
6156
/////////////////////////////////////////////////////////////////////////////////
6257
// Built-in Properties
6358
/////////////////////////////////////////////////////////////////////////////////
@@ -82,15 +77,18 @@ package object nodejs {
8277
*/
8378
def __filename: String = js.Dynamic.global.__filename.asInstanceOf[String]
8479

85-
/**
86-
* In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope
87-
* var something will define a global variable. In Node.js this is different. The top-level scope is not the global
88-
* scope; var something inside a Node.js module will be local to that module.
89-
*/
9080
@js.native
9181
@JSGlobal("global")
82+
@deprecated("Use objects in io.scalajs.nodejs", "0.9.0")
9283
object global extends Global
9384

85+
@js.native
86+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
87+
@JSGlobal("queueMicrotask")
88+
object queueMicrotask extends js.Function1[js.Function, Unit] {
89+
override def apply(arg1: js.Function): Unit = js.native
90+
}
91+
9492
/////////////////////////////////////////////////////////////////////////////////
9593
// Timers
9694
/////////////////////////////////////////////////////////////////////////////////
@@ -107,10 +105,6 @@ package object nodejs {
107105
@JSGlobal("clearTimeout")
108106
object clearTimeout extends ClearTimeout
109107

110-
@js.native
111-
@JSGlobal("ref")
112-
object ref extends Ref
113-
114108
@js.native
115109
@JSGlobal("setImmediate")
116110
object setImmediate extends SetImmediate
@@ -123,10 +117,6 @@ package object nodejs {
123117
@JSGlobal("setTimeout")
124118
object setTimeout extends SetTimeout
125119

126-
@js.native
127-
@JSGlobal("unref")
128-
object unref extends UnRef
129-
130120
/////////////////////////////////////////////////////////////////////////////////
131121
// Implicit Conversions
132122
/////////////////////////////////////////////////////////////////////////////////
@@ -136,86 +126,23 @@ package object nodejs {
136126
* @param duration the given [[FiniteDuration duration]]
137127
* @return the time in milliseconds as an integer
138128
*/
129+
@deprecated("Use io.scalajs.util.DurationHelper", "0.9.0")
139130
implicit def duration2Int(duration: FiniteDuration): Int = duration.toMillis.toInt
140131

141132
/**
142133
* Implicit conversion to translate durations into a double
143134
* @param duration the given [[FiniteDuration duration]]
144135
* @return the time in milliseconds as a double
145136
*/
137+
@deprecated("Use io.scalajs.util.DurationHelper", "0.9.0")
146138
implicit def duration2Double(duration: FiniteDuration): Double = duration.toMillis.toDouble
147139

148140
/**
149141
* Implicit conversion to transform Node [[Error]]s to [[Exception]]s
150142
* @param error the given [[Error]]
151143
* @return the resulting [[Exception]]
152144
*/
153-
implicit def error2Exception(error: Error): Exception = js.JavaScriptException(error.message)
154-
155-
/////////////////////////////////////////////////////////////////////////////////
156-
// Exit Codes - Node.js will normally exit with a 0 status code when no more
157-
// async operations are pending. The following status codes are
158-
// used in other cases:
159-
/////////////////////////////////////////////////////////////////////////////////
160-
161-
type ExitCode = Int
162-
163-
/**
164-
* There was an uncaught exception, and it was not handled by a domain or an 'uncaughtException' event handler.
165-
*/
166-
val UncaughtFatalException: ExitCode = 1
167-
168-
/**
169-
* The JavaScript source code internal in Node.js's bootstrapping process caused a parse error. This is extremely
170-
* rare, and generally can only happen during development of Node.js itself.
171-
*/
172-
val InternalJavaScriptParseError: ExitCode = 3
173-
174-
/**
175-
* The JavaScript source code internal in Node.js's bootstrapping process failed to return a function value when
176-
* evaluated. This is extremely rare, and generally can only happen during development of Node.js itself.
177-
*/
178-
val InternalJavaScriptEvaluationFailure: ExitCode = 4
179-
180-
/**
181-
* There was a fatal unrecoverable error in V8. Typically a message will be printed to stderr with the prefix FATAL ERROR.
182-
*/
183-
val FatalError: ExitCode = 5
184-
185-
/**
186-
* There was an uncaught exception, but the internal fatal exception handler function was somehow set to a non-function,
187-
* and could not be called.
188-
*/
189-
val NonFunctionInternalExceptionHandler: ExitCode = 6
190-
191-
/**
192-
* There was an uncaught exception, and the internal fatal exception handler function itself threw an error while
193-
* attempting to handle it. This can happen, for example, if a 'uncaughtException' or domain.on('error') handler
194-
* throws an error.
195-
*/
196-
val InternalExceptionHandlerRunTimeFailure: ExitCode = 7
197-
198-
/**
199-
* Either an unknown option was specified, or an option requiring a value was provided without a value.
200-
*/
201-
val InvalidArgument: ExitCode = 8
202-
203-
/**
204-
* The JavaScript source code internal in Node.js's bootstrapping process threw an error when the bootstrapping
205-
* function was called. This is extremely rare, and generally can only happen during development of Node.js itself.
206-
*/
207-
val InternalJavaScriptRunTimeFailure: ExitCode = 10
208-
209-
/**
210-
* The --debug and/or --debug-brk options were set, but an invalid port number was chosen.
211-
*/
212-
val InvalidDebugArgument: ExitCode = 12
213-
214-
/**
215-
* If Node.js receives a fatal signal such as SIGKILL or SIGHUP, then its exit code will be 128 plus the value of
216-
* the signal code. This is a standard Unix practice, since exit codes are defined to be 7-bit integers, and signal
217-
* exits set the high-order bit, and then contain the value of the signal code.
218-
*/
219-
val SignalExits: ExitCode = 128
220-
145+
@deprecated("Use toException extension method from io.scalajs.util.NodeJSConverters._", "0.9.0")
146+
implicit def error2Exception(error: Error): Exception =
147+
io.scalajs.util.NodeJSConverters.ErrorExtension(error).toException()
221148
}

app/current/src/main/scala/io/scalajs/nodejs/process/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ package object process {
1313
type Environment = js.Dictionary[String]
1414
// TODO: js.Set
1515
type EnvironmentFlags = js.Any
16-
17-
type SendHandle = net.Socket | net.Server
16+
type ExitCode = Int
17+
type SendHandle = net.Socket | net.Server
1818

1919
@deprecated("use Process object instead", "0.9.0")
2020
def allowedNodeEnvironmentFlags: EnvironmentFlags = Process.allowedNodeEnvironmentFlags
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.scalajs.util
2+
3+
import io.scalajs.nodejs.Error
4+
5+
import scala.scalajs.js
6+
7+
object NodeJSConverters {
8+
implicit final class ErrorExtension(val error: Error) extends AnyVal {
9+
@inline
10+
def toException(): Exception = js.JavaScriptException(error.message)
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.scalajs.nodejs
2+
3+
import org.scalatest.FunSuite
4+
5+
import scala.scalajs.js
6+
7+
class TopLevelTest extends FunSuite {
8+
9+
test("queueMicrotask") {
10+
assert(queueMicrotask.isInstanceOf[js.Function])
11+
queueMicrotask(() => println("printed from queueMicrotask"))
12+
}
13+
14+
}

0 commit comments

Comments
 (0)