Skip to content

Commit d7541b8

Browse files
committed
feat: Lib/os supports JS except inheritable,lseek
1 parent 41e9d57 commit d7541b8

30 files changed

+1141
-633
lines changed

src/pylib/Lib/os.nim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ export os
88

99
import ./os_impl/[
1010
consts, posix_like, subp, utils, path, walkImpl, listdirx,
11-
term, inheritable]
11+
]
12+
when not defined(js):
13+
import ./os_impl/[
14+
term, inheritable]
15+
export term, set_inheritable, get_inheritable
16+
1217
export
13-
consts, posix_like, subp, utils, path, walkImpl, listdirx,
14-
term, set_inheritable, get_inheritable
18+
consts, posix_like, subp, utils, path, walkImpl, listdirx
1519

1620

src/pylib/Lib/os_impl/common.nim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11

2-
import ../../pyerrors/oserr
2+
import ../../pyerrors/[oserr, simperr]
3+
when defined(js):
4+
import ../../pyerrors/jsoserr
5+
export jsoserr
6+
7+
import ../../jsutils/denoAttrs
8+
export denoAttrs
39
import ../../io_abc
410
import ../../noneType
511
import ../../pystring/[strimpl, strbltins]
612
import ../../pybytes/[bytesimpl, bytesbltins]
13+
import ../../version
14+
export version
715

8-
export io_abc, oserr, strimpl, strbltins.repr, bytesimpl, bytesbltins.repr
16+
export io_abc, oserr, simperr, strimpl, strbltins.repr, bytesimpl, bytesbltins.repr
917
export noneType
1018

src/pylib/Lib/os_impl/osJsPatch.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
when defined(js):
4+
from ./common import catchJsErrAsCode
5+
import ./posix_like/jsStat
6+
template existsWrap(existsX, isX) =
7+
proc existsX*(d: string): bool =
8+
var s: Stat
9+
let err = catchJsErrAsCode:
10+
s = statSync(cstring d)
11+
if err != 0: return false
12+
else: s.isX()
13+
existsWrap dirExists, isDirectory
14+
existsWrap fileExists, isFile
15+
existsWrap symlinkExists, isSymbolicLink
16+
17+
# easy to impl getCreationTime, etc.
18+
# but that'll introduce the conversions between `DateTime`, which is no need,
19+
# we just impl the used `get?time`
20+
template gen_getxtime(getxtime) =
21+
proc getxtime*(p: PathLike): float =
22+
var s: Stat
23+
let err = catchJsErrAndRaise :
24+
s = statSync(cstring d)
25+
s.getxtime
26+
27+

src/pylib/Lib/os_impl/path.nim

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ./posix_like/stat
55

66
import ./common
77
export common
8+
when InJs:
9+
import ./osJsPatch
810

911
import ./consts
1012
export consts
@@ -46,13 +48,16 @@ psExp normpath, normalizedPath
4648
func relpath*[T](p: PathLike[T], start=curdir): T =
4749
mapPathLike[T] relativePath($p, $start)
4850
49-
template expFRetTAsF(nam, nimProc){.dirty.} =
50-
## returns Time as float
51-
proc nam*[T](p: PathLike[T]): float =
52-
p.tryOsOp: result = nimProc($p).toUnixFloat
53-
expFRetTAsF getctime, getCreationTime
54-
expFRetTAsF getmtime, getLastModificationTime
55-
expFRetTAsF getatime, getLastAccessTime
51+
when defined(js):
52+
export getatime, getmtime, getctime
53+
else:
54+
template expFRetTAsF(nam, nimProc){.dirty.} =
55+
## returns Time as float
56+
proc nam*[T](p: PathLike[T]): float =
57+
p.tryOsOp: result = nimProc($p).toUnixFloat
58+
expFRetTAsF getctime, getCreationTime
59+
expFRetTAsF getmtime, getLastModificationTime
60+
expFRetTAsF getatime, getLastAccessTime
5661
5762
proc getsize*[T](filename: PathLike[T]): int =
5863
# std/os.`getFileSize` doesn't work for directory
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import ../../../pyerrors
3-
export pyerrors
3+
import ../../../pyerrors/errno as errnoMod
4+
export pyerrors, errnoMod
45

5-
var errno*{.importc, header: "<errno.h>".}: cint
66

src/pylib/Lib/os_impl/posix_like/fdopen.nim

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33

44
import std/macros
55

6-
import ../../../io
7-
export io # for open, write,...
6+
when defined(nimdoc) or defined(js) or defined(nimscript):
7+
template fdopen*(fd: Positive; x: varargs[untyped]): untyped =
8+
## not support JS/NimScript backend
9+
static: doAssert false, "unsupport JS/NimScript backend"
10+
else:
11+
import ../../../io
12+
export io # for open, write,...
813

9-
macro unpackVarargsWith1(callee, arg1: untyped; otherArgs: varargs[untyped]): untyped =
10-
result = newCall(callee, arg1)
11-
for i in 0 ..< otherArgs.len:
12-
result.add otherArgs[i]
14+
macro unpackVarargsWith1(callee, arg1: untyped; otherArgs: varargs[untyped]): untyped =
15+
result = newCall(callee, arg1)
16+
for i in 0 ..< otherArgs.len:
17+
result.add otherArgs[i]
1318

14-
template fdopen*(fd: Positive; x: varargs[untyped]): untyped =
15-
## Return an open file object connected to the file descriptor fd.
16-
##
17-
## This is an alias of the io.open() function and accepts the same arguments.
18-
## The only difference is that the first argument of fdopen() must always be an integer.
19-
bind io.open
20-
unpackVarargsWith1 io.open, fd, x
21-
# support kw (will be of kind: nnkExprEqExpr)
19+
template fdopen*(fd: Positive; x: varargs[untyped]): untyped =
20+
## Return an open file object connected to the file descriptor fd.
21+
##
22+
## This is an alias of the io.open() function and accepts the same arguments.
23+
## The only difference is that the first argument of fdopen() must always be an integer.
24+
bind io.open
25+
unpackVarargsWith1 io.open, fd, x
26+
# support kw (will be of kind: nnkExprEqExpr)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import std/os
3+
when defined(js):
4+
import ../common
5+
proc getpid*(): int{.importDenoOrProcess pid.}
6+
else:
7+
proc getpid*(): int =
8+
getCurrentProcessId()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
when defined(js):
3+
import std/jsffi
4+
from ../common import importNode
5+
const InNode = defined(nodejs)
6+
type Stat*#[{.importByNodeOrDeno("require('fs').Stats", "Deno.FileInfo").}]# = JsObject
7+
using self: Stat
8+
template impIsXNoNull(isX; isMeth = InNode){.dirty.} =
9+
## `is*` is method in NodeJs but attr in Deno.
10+
## this can only used for isDirectory, isFile, isSymbolicLink
11+
## as their result are non-null.
12+
proc isX*(self): bool{.importjs:"(#)." & astToStr(isX) & (
13+
when isMeth: "()" else: ""
14+
).}
15+
impIsXNoNull isDirectory
16+
impIsXNoNull isFile
17+
impIsXNoNull isSymbolicLink
18+
19+
proc statSync*(p: cstring): Stat{.importNode(fs, statSync).}
20+
proc fstatSync*(fd: cint): Stat{.importNode(fs, fstatSync).}
21+

0 commit comments

Comments
 (0)