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

Commit 35810a5

Browse files
authored
Merge pull request #145 from exoego/BigInt
Use js.BigInt
2 parents b8d584c + 20d3667 commit 35810a5

File tree

7 files changed

+111
-71
lines changed

7 files changed

+111
-71
lines changed

app/current/src/main/scala/io/scalajs/nodejs/buffer/Buffer.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -784,33 +784,30 @@ class Buffer private[this] () extends Uint8Array( /* dummy to trick constructor
784784
* @see https://nodejs.org/api/buffer.html#buffer_buf_readbiguint64be_offset
785785
*/
786786
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
787-
def readBigInt64BE(offset: Int = js.native): Buffer.UnsafeBigInt = js.native
787+
def readBigInt64BE(offset: Int = js.native): scalajs.js.BigInt = js.native
788788

789789
/**
790790
* @see https://nodejs.org/api/buffer.html#buffer_buf_readbiguint64le_offset
791791
*/
792792
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
793-
def readBigUInt64LE(offset: Int = js.native): Buffer.UnsafeBigInt = js.native
793+
def readBigUInt64LE(offset: Int = js.native): scalajs.js.BigInt = js.native
794794

795795
/**
796796
* @see https://nodejs.org/api/buffer.html#buffer_buf_writebigint64be_value_offset
797797
*/
798798
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
799-
def writeBigInt64BE(value: Buffer.UnsafeBigInt, offset: Int = js.native): Int = js.native
799+
def writeBigInt64BE(value: scalajs.js.BigInt, offset: Int = js.native): Int = js.native
800800

801801
/**
802802
* @see https://nodejs.org/api/buffer.html#buffer_buf_writebigint64le_value_offset
803803
*/
804804
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
805-
def writeBigInt64LE(value: Buffer.UnsafeBigInt, offset: Int = js.native): Int = js.native
805+
def writeBigInt64LE(value: scalajs.js.BigInt, offset: Int = js.native): Int = js.native
806806
}
807807

808808
@js.native
809809
@JSGlobal
810810
object Buffer extends js.Object {
811-
// TODO: Node.js added BigInt-related things (e.g. readBigInt64BE). Should support when Scala.js support it
812-
type UnsafeBigInt = js.Dynamic
813-
814811
/////////////////////////////////////////////////////////////////////////////////
815812
// Properties
816813
/////////////////////////////////////////////////////////////////////////////////

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,14 @@ trait Fs extends IEventEmitter with FSConstants {
285285
*/
286286
def fstat(fd: FileDescriptor, callback: FsCallback1[Stats]): Unit = js.native
287287

288-
def fstat(fd: FileDescriptor, options: StatOptions, callback: FsCallback1[Stats]): Unit = js.native
288+
def fstat(fd: FileDescriptor, options: StatOptions, callback: FsCallback1[StatsVariant]): Unit = js.native
289289

290290
/**
291291
* Synchronous fstat(2).
292292
* @param fd the file descriptor
293293
* @return an instance of [[fs.Stats]].
294294
*/
295-
def fstatSync(fd: FileDescriptor, options: StatOptions = js.native): Stats = js.native
295+
def fstatSync(fd: FileDescriptor, options: StatOptions = js.native): StatsVariant = js.native
296296

297297
/**
298298
* Asynchronous fsync(2). No arguments other than a possible exception are given to the completion callback.
@@ -399,7 +399,7 @@ trait Fs extends IEventEmitter with FSConstants {
399399
*/
400400
def linkSync(existingPath: Path, newPath: Path): Unit = js.native
401401

402-
def lstat(path: Path, options: StatOptions, callback: FsCallback1[Stats]): Unit = js.native
402+
def lstat(path: Path, options: StatOptions, callback: FsCallback1[StatsVariant]): Unit = js.native
403403

404404
/**
405405
* Asynchronous lstat(2).
@@ -417,7 +417,7 @@ trait Fs extends IEventEmitter with FSConstants {
417417
*/
418418
def lstatSync(path: Path): Stats = js.native
419419

420-
def lstatSync(path: Path, options: StatOptions): Stats = js.native
420+
def lstatSync(path: Path, options: StatOptions): StatsVariant = js.native
421421

422422
/**
423423
* Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback.
@@ -798,15 +798,15 @@ trait Fs extends IEventEmitter with FSConstants {
798798
*/
799799
def stat(path: Path, callback: FsCallback1[Stats]): Stats = js.native
800800

801-
def stat(path: Path, options: StatOptions, callback: FsCallback1[Stats]): Stats = js.native
801+
def stat(path: Path, options: StatOptions, callback: FsCallback1[StatsVariant]): StatsVariant = js.native
802802

803803
/**
804804
* Synchronous stat(2). Returns an instance of [[fs.Stats]].
805805
* @example fs.statSync(path)
806806
*/
807807
def statSync(path: Path): Stats = js.native
808808

809-
def statSync(path: Path, options: StatOptions): Stats = js.native
809+
def statSync(path: Path, options: StatOptions): StatsVariant = js.native
810810

811811
/**
812812
* Asynchronous symlink(2). No arguments other than a possible exception are given to the completion callback.
@@ -1131,7 +1131,7 @@ object Fs extends Fs {
11311131
def lchmod(path: Path, mode: FileMode): js.Promise[Unit] = js.native
11321132
def lchown(path: Path, uid: UID, gid: GID): js.Promise[Unit] = js.native
11331133
def link(existingPath: Path, newPath: Path): js.Promise[Unit] = js.native
1134-
def lstat(path: Path, options: StatOptions): js.Promise[Stats] = js.native
1134+
def lstat(path: Path, options: StatOptions): js.Promise[StatsVariant] = js.native
11351135
def mkdir(path: Path, options: MkdirOptions = js.native): js.Promise[Unit] = js.native
11361136
def mkdir(path: Path, mode: FileMode): js.Promise[Unit] = js.native
11371137
def mkdtemp(prefix: String, options: FileEncodingOptions): js.Promise[String] = js.native
@@ -1147,7 +1147,7 @@ object Fs extends Fs {
11471147
def rmdir(path: Path): js.Promise[Unit] = js.native
11481148
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
11491149
def rmdir(path: Path, options: RmdirOptions): js.Promise[Unit] = js.native
1150-
def stat(path: Path, options: StatOptions = js.native): js.Promise[Stats] = js.native
1150+
def stat(path: Path, options: StatOptions = js.native): js.Promise[StatsVariant] = js.native
11511151
def symlink(target: Path, path: Path, `type`: String = js.native): js.Promise[Unit] = js.native
11521152
def truncate(path: Path, length: Int = js.native): js.Promise[Unit] = js.native
11531153
def unlink(path: Path): js.Promise[Unit] = js.native
@@ -1173,7 +1173,7 @@ object Fs extends Fs {
11731173
def readFile(encoding: String): js.Promise[String] = js.native
11741174
def readFile(options: ReadFileOptions): js.Promise[Output] = js.native
11751175
def stat(): js.Promise[Stats] = js.native
1176-
def stat(options: StatOptions): js.Promise[Stats] = js.native
1176+
def stat(options: StatOptions): js.Promise[StatsVariant] = js.native
11771177
def sync(): js.Promise[Unit] = js.native
11781178
def truncate(): js.Promise[Unit] = js.native
11791179
def truncate(length: Int): js.Promise[Unit] = js.native

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

Lines changed: 88 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,54 @@ package fs
33

44
import scala.scalajs.js
55

6-
/**
7-
* fs.Stats (Stat Time Values) - Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous
8-
* counterparts are of this type.
9-
* @example
10-
* {{{
11-
* Stats {
12-
* dev: 2114,
13-
* ino: 48064969,
14-
* mode: 33188,
15-
* nlink: 1,
16-
* uid: 85,
17-
* gid: 100,
18-
* rdev: 0,
19-
* size: 527,
20-
* blksize: 4096,
21-
* blocks: 8,
22-
* atimeMs: 1318289051000.1,
23-
* mtimeMs: 1318289051000.1,
24-
* ctimeMs: 1318289051000.1,
25-
* birthtimeMs: 1318289051000.1,
26-
* atime: Mon, 10 Oct 2011 23:24:11 GMT,
27-
* mtime: Mon, 10 Oct 2011 23:24:11 GMT,
28-
* ctime: Mon, 10 Oct 2011 23:24:11 GMT,
29-
* birthtime: Mon, 10 Oct 2011 23:24:11 GMT
30-
* }
31-
* }}}
32-
* @since 0.1.21
33-
*/
34-
// TODO: BigIntStats if Scala.js added BigInt support
356
@js.native
36-
trait Stats extends js.Object {
7+
trait IStats[TInt, TFloat] extends js.Object {
8+
379
/////////////////////////////////////////////////////////////////////////////////
3810
// Properties
3911
/////////////////////////////////////////////////////////////////////////////////
4012

41-
def dev: Int = js.native
13+
def dev: TInt = js.native
4214

43-
def ino: Double = js.native
15+
def ino: TFloat = js.native
4416

45-
def mode: FileMode = js.native
17+
def mode: TInt = js.native
4618

47-
def nlink: Int = js.native
19+
def nlink: TInt = js.native
4820

49-
def uid: UID = js.native
21+
def uid: TInt = js.native
5022

51-
def gid: GID = js.native
23+
def gid: TInt = js.native
5224

53-
def rdev: Int = js.native
25+
def rdev: TInt = js.native
5426

55-
def size: Double = js.native
27+
def size: TFloat = js.native
5628

57-
def blksize: Int = js.native
29+
def blksize: TInt = js.native
5830

59-
def blocks: Int = js.native
31+
def blocks: TInt = js.native
6032

6133
/**
6234
* "Access Time" - Time when file data last accessed. Changed by the mknod(2), utimes(2), and read(2) system calls.
6335
*/
6436
def atime: js.Date = js.native
6537

66-
def atimeMs: Double = js.native
38+
def atimeMs: TFloat = js.native
6739

6840
/**
6941
* "Modified Time" - Time when file data last modified. Changed by the mknod(2), utimes(2), and write(2) system calls.
7042
*/
7143
def mtime: js.Date = js.native
7244

73-
def mtimeMs: Double = js.native
45+
def mtimeMs: TFloat = js.native
7446

7547
/**
7648
* "Change Time" - Time when file status was last changed (inode data modification). Changed by the chmod(2),
7749
* chown(2), link(2), mknod(2), rename(2), unlink(2), utimes(2), read(2), and write(2) system calls.
7850
*/
7951
def ctime: js.Date = js.native
8052

81-
def ctimeMs: Double = js.native
53+
def ctimeMs: TFloat = js.native
8254

8355
/**
8456
* "Birth Time" - Time of file creation. Set once when the file is created. On filesystems where birthtime is not
@@ -88,7 +60,7 @@ trait Stats extends js.Object {
8860
*/
8961
def birthtime: js.Date = js.native
9062

91-
def birthtimeMs: Double = js.native
63+
def birthtimeMs: TFloat = js.native
9264

9365
/////////////////////////////////////////////////////////////////////////////////
9466
// Methods
@@ -111,3 +83,75 @@ trait Stats extends js.Object {
11183

11284
def isSocket(): Boolean = js.native
11385
}
86+
87+
/**
88+
* fs.Stats (Stat Time Values) - Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous
89+
* counterparts are of this type.
90+
* @example
91+
* {{{
92+
* Stats {
93+
* dev: 2114,
94+
* ino: 48064969,
95+
* mode: 33188,
96+
* nlink: 1,
97+
* uid: 85,
98+
* gid: 100,
99+
* rdev: 0,
100+
* size: 527,
101+
* blksize: 4096,
102+
* blocks: 8,
103+
* atimeMs: 1318289051000.1,
104+
* mtimeMs: 1318289051000.1,
105+
* ctimeMs: 1318289051000.1,
106+
* birthtimeMs: 1318289051000.1,
107+
* atime: Mon, 10 Oct 2011 23:24:11 GMT,
108+
* mtime: Mon, 10 Oct 2011 23:24:11 GMT,
109+
* ctime: Mon, 10 Oct 2011 23:24:11 GMT,
110+
* birthtime: Mon, 10 Oct 2011 23:24:11 GMT
111+
* }
112+
* }}}
113+
* @since 0.1.21
114+
*/
115+
@js.native
116+
trait Stats extends IStats[Int, Double]
117+
118+
/**
119+
* fs.Stats (Stat Time Values) - Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous
120+
* counterparts are of this type.
121+
*
122+
* @example
123+
* {{{
124+
* BigIntStats {
125+
* dev: 2114n,
126+
* ino: 48064969n,
127+
* mode: 33188n,
128+
* nlink: 1n,
129+
* uid: 85n,
130+
* gid: 100n,
131+
* rdev: 0n,
132+
* size: 527n,
133+
* blksize: 4096n,
134+
* blocks: 8n,
135+
* atimeMs: 1318289051000n,
136+
* mtimeMs: 1318289051000n,
137+
* ctimeMs: 1318289051000n,
138+
* birthtimeMs: 1318289051000n,
139+
* atimeNs: 1318289051000000000n,
140+
* mtimeNs: 1318289051000000000n,
141+
* ctimeNs: 1318289051000000000n,
142+
* birthtimeNs: 1318289051000000000n,
143+
* atime: Mon, 10 Oct 2011 23:24:11 GMT,
144+
* mtime: Mon, 10 Oct 2011 23:24:11 GMT,
145+
* ctime: Mon, 10 Oct 2011 23:24:11 GMT,
146+
* birthtime: Mon, 10 Oct 2011 23:24:11 GMT
147+
* }
148+
* }}}
149+
* @since 0.1.21
150+
*/
151+
@js.native
152+
trait BigIntStats extends IStats[BigInt, BigInt] {
153+
def atimeNs: BigInt = js.native
154+
def mtimeNs: BigInt = js.native
155+
def ctimeNs: BigInt = js.native
156+
def birthtimeNs: BigInt = js.native
157+
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ package object fs {
2424

2525
type Dirent = Fs.Dirent
2626

27+
type StatsVariant = Stats | BigIntStats
28+
2729
/////////////////////////////////////////////////////////////////////////////////
2830
// Implicit conversions and classes
2931
/////////////////////////////////////////////////////////////////////////////////
@@ -89,8 +91,8 @@ package object fs {
8991
def fdatasyncFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](instance.fdatasync(fd, _))
9092

9193
@inline
92-
def fstatFuture(fd: FileDescriptor, options: StatOptions): Future[Stats] = {
93-
promiseWithError1[FileIOError, Stats](instance.fstat(fd, options, _))
94+
def fstatFuture(fd: FileDescriptor, options: StatOptions): Future[StatsVariant] = {
95+
promiseWithError1[FileIOError, StatsVariant](instance.fstat(fd, options, _))
9496
}
9597

9698
@inline
@@ -129,8 +131,8 @@ package object fs {
129131
}
130132

131133
@inline
132-
def lstatFuture(path: Path, options: StatOptions): Future[Stats] = {
133-
promiseWithError1[FileIOError, Stats](instance.lstat(path, options, _))
134+
def lstatFuture(path: Path, options: StatOptions): Future[StatsVariant] = {
135+
promiseWithError1[FileIOError, StatsVariant](instance.lstat(path, options, _))
134136
}
135137

136138
@inline
@@ -324,8 +326,8 @@ package object fs {
324326
def statFuture(path: Path): Future[Stats] = promiseWithError1[FileIOError, Stats](instance.stat(path, _))
325327

326328
@inline
327-
def statFuture(path: Path, options: StatOptions): Future[Stats] = {
328-
promiseWithError1[FileIOError, Stats](instance.stat(path, options, _))
329+
def statFuture(path: Path, options: StatOptions): Future[StatsVariant] = {
330+
promiseWithError1[FileIOError, StatsVariant](instance.stat(path, options, _))
329331
}
330332

331333
@inline

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,7 @@ class WarningOptions(
480480

481481
@js.native
482482
trait HrTime extends js.Function1[js.Array[Int], js.Array[Int]] with js.Function0[js.Array[Int]] {
483-
// TODO: js.BigInt
484-
def bigint(): js.Any = js.native
483+
def bigint(): js.BigInt = js.native
485484
}
486485

487486
@enableMembersIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)

app/current/src/test/scala/io/scalajs/nodejs/buffer/BufferTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.scalajs.nodejs.buffer
22

3-
import io.scalajs.nodejs.buffer.Buffer
43
import org.scalatest.FunSpec
54

65
import scala.scalajs.js
@@ -11,7 +10,7 @@ import scala.scalajs.js
1110
class BufferTest extends FunSpec {
1211
it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") {
1312
val buf = Buffer.allocUnsafe(8)
14-
val v = js.Dynamic.global.BigInt("0x0102030405060708")
13+
val v = js.BigInt("0x0102030405060708")
1514
buf.writeBigInt64BE(v, 0);
1615
assert(Buffer.compare(buf, Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8))) === 0)
1716
}

app/nodejs-v10/src/test/scala/io/scalajs/nodejs/process/ProcessTest.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class ProcessTest extends FunSpec {
4343
}
4444

4545
it("hrtime.bigint") {
46-
// TODO: js.BigInt
4746
val value = Process.hrtime.bigint()
4847
assert(value.toString().matches("^\\d+$"))
4948
}

0 commit comments

Comments
 (0)