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

Use js.BigInt #145

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/current/src/main/scala/io/scalajs/nodejs/buffer/Buffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -784,33 +784,30 @@ class Buffer private[this] () extends Uint8Array( /* dummy to trick constructor
* @see https://nodejs.org/api/buffer.html#buffer_buf_readbiguint64be_offset
*/
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
def readBigInt64BE(offset: Int = js.native): Buffer.UnsafeBigInt = js.native
def readBigInt64BE(offset: Int = js.native): scalajs.js.BigInt = js.native

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

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

/**
* @see https://nodejs.org/api/buffer.html#buffer_buf_writebigint64le_value_offset
*/
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
def writeBigInt64LE(value: Buffer.UnsafeBigInt, offset: Int = js.native): Int = js.native
def writeBigInt64LE(value: scalajs.js.BigInt, offset: Int = js.native): Int = js.native
}

@js.native
@JSGlobal
object Buffer extends js.Object {
// TODO: Node.js added BigInt-related things (e.g. readBigInt64BE). Should support when Scala.js support it
type UnsafeBigInt = js.Dynamic

/////////////////////////////////////////////////////////////////////////////////
// Properties
/////////////////////////////////////////////////////////////////////////////////
Expand Down
18 changes: 9 additions & 9 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/Fs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ trait Fs extends IEventEmitter with FSConstants {
*/
def fstat(fd: FileDescriptor, callback: FsCallback1[Stats]): Unit = js.native

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

/**
* Synchronous fstat(2).
* @param fd the file descriptor
* @return an instance of [[fs.Stats]].
*/
def fstatSync(fd: FileDescriptor, options: StatOptions = js.native): Stats = js.native
def fstatSync(fd: FileDescriptor, options: StatOptions = js.native): StatsVariant = js.native

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

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

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

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

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

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

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

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

/**
* Asynchronous symlink(2). No arguments other than a possible exception are given to the completion callback.
Expand Down Expand Up @@ -1131,7 +1131,7 @@ object Fs extends Fs {
def lchmod(path: Path, mode: FileMode): js.Promise[Unit] = js.native
def lchown(path: Path, uid: UID, gid: GID): js.Promise[Unit] = js.native
def link(existingPath: Path, newPath: Path): js.Promise[Unit] = js.native
def lstat(path: Path, options: StatOptions): js.Promise[Stats] = js.native
def lstat(path: Path, options: StatOptions): js.Promise[StatsVariant] = js.native
def mkdir(path: Path, options: MkdirOptions = js.native): js.Promise[Unit] = js.native
def mkdir(path: Path, mode: FileMode): js.Promise[Unit] = js.native
def mkdtemp(prefix: String, options: FileEncodingOptions): js.Promise[String] = js.native
Expand All @@ -1147,7 +1147,7 @@ object Fs extends Fs {
def rmdir(path: Path): js.Promise[Unit] = js.native
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
def rmdir(path: Path, options: RmdirOptions): js.Promise[Unit] = js.native
def stat(path: Path, options: StatOptions = js.native): js.Promise[Stats] = js.native
def stat(path: Path, options: StatOptions = js.native): js.Promise[StatsVariant] = js.native
def symlink(target: Path, path: Path, `type`: String = js.native): js.Promise[Unit] = js.native
def truncate(path: Path, length: Int = js.native): js.Promise[Unit] = js.native
def unlink(path: Path): js.Promise[Unit] = js.native
Expand All @@ -1173,7 +1173,7 @@ object Fs extends Fs {
def readFile(encoding: String): js.Promise[String] = js.native
def readFile(options: ReadFileOptions): js.Promise[Output] = js.native
def stat(): js.Promise[Stats] = js.native
def stat(options: StatOptions): js.Promise[Stats] = js.native
def stat(options: StatOptions): js.Promise[StatsVariant] = js.native
def sync(): js.Promise[Unit] = js.native
def truncate(): js.Promise[Unit] = js.native
def truncate(length: Int): js.Promise[Unit] = js.native
Expand Down
132 changes: 88 additions & 44 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/Stats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,54 @@ package fs

import scala.scalajs.js

/**
* fs.Stats (Stat Time Values) - Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous
* counterparts are of this type.
* @example
* {{{
* Stats {
* dev: 2114,
* ino: 48064969,
* mode: 33188,
* nlink: 1,
* uid: 85,
* gid: 100,
* rdev: 0,
* size: 527,
* blksize: 4096,
* blocks: 8,
* atimeMs: 1318289051000.1,
* mtimeMs: 1318289051000.1,
* ctimeMs: 1318289051000.1,
* birthtimeMs: 1318289051000.1,
* atime: Mon, 10 Oct 2011 23:24:11 GMT,
* mtime: Mon, 10 Oct 2011 23:24:11 GMT,
* ctime: Mon, 10 Oct 2011 23:24:11 GMT,
* birthtime: Mon, 10 Oct 2011 23:24:11 GMT
* }
* }}}
* @since 0.1.21
*/
// TODO: BigIntStats if Scala.js added BigInt support
@js.native
trait Stats extends js.Object {
trait IStats[TInt, TFloat] extends js.Object {

/////////////////////////////////////////////////////////////////////////////////
// Properties
/////////////////////////////////////////////////////////////////////////////////

def dev: Int = js.native
def dev: TInt = js.native

def ino: Double = js.native
def ino: TFloat = js.native

def mode: FileMode = js.native
def mode: TInt = js.native

def nlink: Int = js.native
def nlink: TInt = js.native

def uid: UID = js.native
def uid: TInt = js.native

def gid: GID = js.native
def gid: TInt = js.native

def rdev: Int = js.native
def rdev: TInt = js.native

def size: Double = js.native
def size: TFloat = js.native

def blksize: Int = js.native
def blksize: TInt = js.native

def blocks: Int = js.native
def blocks: TInt = js.native

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

def atimeMs: Double = js.native
def atimeMs: TFloat = js.native

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

def mtimeMs: Double = js.native
def mtimeMs: TFloat = js.native

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

def ctimeMs: Double = js.native
def ctimeMs: TFloat = js.native

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

def birthtimeMs: Double = js.native
def birthtimeMs: TFloat = js.native

/////////////////////////////////////////////////////////////////////////////////
// Methods
Expand All @@ -111,3 +83,75 @@ trait Stats extends js.Object {

def isSocket(): Boolean = js.native
}

/**
* fs.Stats (Stat Time Values) - Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous
* counterparts are of this type.
* @example
* {{{
* Stats {
* dev: 2114,
* ino: 48064969,
* mode: 33188,
* nlink: 1,
* uid: 85,
* gid: 100,
* rdev: 0,
* size: 527,
* blksize: 4096,
* blocks: 8,
* atimeMs: 1318289051000.1,
* mtimeMs: 1318289051000.1,
* ctimeMs: 1318289051000.1,
* birthtimeMs: 1318289051000.1,
* atime: Mon, 10 Oct 2011 23:24:11 GMT,
* mtime: Mon, 10 Oct 2011 23:24:11 GMT,
* ctime: Mon, 10 Oct 2011 23:24:11 GMT,
* birthtime: Mon, 10 Oct 2011 23:24:11 GMT
* }
* }}}
* @since 0.1.21
*/
@js.native
trait Stats extends IStats[Int, Double]

/**
* fs.Stats (Stat Time Values) - Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous
* counterparts are of this type.
*
* @example
* {{{
* BigIntStats {
* dev: 2114n,
* ino: 48064969n,
* mode: 33188n,
* nlink: 1n,
* uid: 85n,
* gid: 100n,
* rdev: 0n,
* size: 527n,
* blksize: 4096n,
* blocks: 8n,
* atimeMs: 1318289051000n,
* mtimeMs: 1318289051000n,
* ctimeMs: 1318289051000n,
* birthtimeMs: 1318289051000n,
* atimeNs: 1318289051000000000n,
* mtimeNs: 1318289051000000000n,
* ctimeNs: 1318289051000000000n,
* birthtimeNs: 1318289051000000000n,
* atime: Mon, 10 Oct 2011 23:24:11 GMT,
* mtime: Mon, 10 Oct 2011 23:24:11 GMT,
* ctime: Mon, 10 Oct 2011 23:24:11 GMT,
* birthtime: Mon, 10 Oct 2011 23:24:11 GMT
* }
* }}}
* @since 0.1.21
*/
@js.native
trait BigIntStats extends IStats[BigInt, BigInt] {
def atimeNs: BigInt = js.native
def mtimeNs: BigInt = js.native
def ctimeNs: BigInt = js.native
def birthtimeNs: BigInt = js.native
}
14 changes: 8 additions & 6 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package object fs {

type Dirent = Fs.Dirent

type StatsVariant = Stats | BigIntStats

/////////////////////////////////////////////////////////////////////////////////
// Implicit conversions and classes
/////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -89,8 +91,8 @@ package object fs {
def fdatasyncFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](instance.fdatasync(fd, _))

@inline
def fstatFuture(fd: FileDescriptor, options: StatOptions): Future[Stats] = {
promiseWithError1[FileIOError, Stats](instance.fstat(fd, options, _))
def fstatFuture(fd: FileDescriptor, options: StatOptions): Future[StatsVariant] = {
promiseWithError1[FileIOError, StatsVariant](instance.fstat(fd, options, _))
}

@inline
Expand Down Expand Up @@ -129,8 +131,8 @@ package object fs {
}

@inline
def lstatFuture(path: Path, options: StatOptions): Future[Stats] = {
promiseWithError1[FileIOError, Stats](instance.lstat(path, options, _))
def lstatFuture(path: Path, options: StatOptions): Future[StatsVariant] = {
promiseWithError1[FileIOError, StatsVariant](instance.lstat(path, options, _))
}

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

@inline
def statFuture(path: Path, options: StatOptions): Future[Stats] = {
promiseWithError1[FileIOError, Stats](instance.stat(path, options, _))
def statFuture(path: Path, options: StatOptions): Future[StatsVariant] = {
promiseWithError1[FileIOError, StatsVariant](instance.stat(path, options, _))
}

@inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,7 @@ class WarningOptions(

@js.native
trait HrTime extends js.Function1[js.Array[Int], js.Array[Int]] with js.Function0[js.Array[Int]] {
// TODO: js.BigInt
def bigint(): js.Any = js.native
def bigint(): js.BigInt = js.native
}

@enableMembersIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.scalajs.nodejs.buffer

import io.scalajs.nodejs.buffer.Buffer
import org.scalatest.FunSpec

import scala.scalajs.js
Expand All @@ -11,7 +10,7 @@ import scala.scalajs.js
class BufferTest extends FunSpec {
it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") {
val buf = Buffer.allocUnsafe(8)
val v = js.Dynamic.global.BigInt("0x0102030405060708")
val v = js.BigInt("0x0102030405060708")
buf.writeBigInt64BE(v, 0);
assert(Buffer.compare(buf, Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8))) === 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ProcessTest extends FunSpec {
}

it("hrtime.bigint") {
// TODO: js.BigInt
val value = Process.hrtime.bigint()
assert(value.toString().matches("^\\d+$"))
}
Expand Down