Skip to content

Commit 2e76c43

Browse files
committed
fix(js): Lib/os not compiles for stat,unlink,chmods,scandir
1 parent fa0e500 commit 2e76c43

File tree

6 files changed

+56
-42
lines changed

6 files changed

+56
-42
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
import ../common
33
import ../../errno_impl/errnoUtils
44
import ./pyCfg
5-
when not InJS:
6-
importConfig [
7-
chmods,
8-
os
9-
]
10-
else:
5+
importConfig [
6+
chmods,
7+
os
8+
]
9+
when InJs:
1110
template decl(f, val) =
1211
const `HAVE f` = val
1312
decl chmod, true

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
when defined(js):
44
import ../common
55

6-
using mode: int
6+
type Mode* = int
7+
using mode: Mode
78
proc chmodImpl(path: cstring, mode){.importDenoOrNodeMod(fs, chmodSync).}
89
proc lchmodImpl(path: cstring, mode){.importNode(fs, lchmodSync).} ## XXX: nodejs: only works on macos
910
proc fchmodImpl(fd: int, mode){.importNode(fs, fchmodSync).}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ when defined(js):
33
import std/jsffi
44
import std/jscore
55
type Date* = DateTime
6-
from ../common import importNode
6+
from ../common import importNode, importDenoOrNodeMod
77
const InNode = defined(nodejs)
88
type Stat*#[{.importByNodeOrDeno("require('fs').Stats", "Deno.FileInfo").}]# = ref object of JsObject
99
## Stat in node, FileInfo in deno

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ when HAVE_FDOPENDIR:
198198
else:
199199
doAssert false, "unreachable"
200200
201-
template eScandirType =
202-
{.error: "TypeError:" &
203-
"scandir: path should be string, bytes, os.PathLike or None, not int".}
201+
type ScandirNotSupportFd = object of RootEffect
202+
proc eScandirType{.tags: [ScandirNotSupportFd].} =
203+
raise newException(TypeError,
204+
"scandir: path should be string, bytes, os.PathLike or None, not int")
204205
template scandirImpl(path){.dirty.} =
205206
sys.audit("os.scandir", path)
206207
when path is int:
@@ -227,9 +228,9 @@ template scandirImpl(path){.dirty.} =
227228
kind=nimPCKindFromDirent(path, name, direntp))
228229
yield de
229230
else:
230-
eScandirType
231+
eScandirType()
231232
else:
232-
eScandirType
233+
eScandirType()
233234
else:
234235
let spath = $path
235236
when defined(js):

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import ../common
55
import ./pyCfg
66
import ../util/handle_signal
77

8-
when not InJS:
9-
importConfig [
10-
stats,
11-
os
12-
]
13-
else:
8+
importConfig [
9+
stats,
10+
os
11+
]
12+
when InJS:
1413
template decl(f, val) =
1514
const `HAVE f` = val
1615
decl lstat, true
@@ -31,6 +30,7 @@ import std/macros
3130
TODO: impl our own stat... Get rid of `_wstat`
3231
]#
3332

33+
const DWin = defined(windows)
3434
when InJs:
3535
import std/jsffi
3636
import ./jsStat
@@ -75,7 +75,6 @@ when InJs:
7575
7676
7777
else:
78-
const DWin = defined(windows)
7978
when DWin:
8079
import ../util/[
8180
mywinlean, getFileInfo]
@@ -205,15 +204,15 @@ func to_result(s: sink Stat): stat_result =
205204
result = stat_result(data: s)
206205
207206
when InJs:
208-
proc statAux(st: var Stat, path: int|PathLike) =
207+
proc statAux(st: var Stat, path: int|string) =
209208
catchJsErrAndRaise:
210209
st =
211210
when path is int:
212211
fstatSync(path.cint)
213212
else:
214-
let cs = cstring($path)
213+
let cs = cstring(path)
215214
statSync(cs)
216-
proc lstatAux(st: var Stat, fd: int) =
215+
proc fstatAux(st: var Stat, fd: int) =
217216
catchJsErrAndRaise:
218217
st = fstatSync(fd.cint)
219218
proc lstatAux(st: var Stat, path: PathLike) =
@@ -494,7 +493,7 @@ when DWin:
494493
goto_cleanup
495494

496495

497-
proc win32_xstat_impl(path: LPCWSTR, status: var Stat, traverse: bool): int =
496+
proc win32_xstat_impl(path: LPCWSTR, status: var Stat, traverse: bool): cint =
498497
var statInfo: FILE_STAT_BASIC_INFORMATION
499498
if Py_GetFileInformationByName(path, FileStatBasicByNameInfo, addr statInfo, sizeof(statInfo).DWORD):
500499
if (statInfo.FileAttributes and DWORD FILE_ATTRIBUTE_REPARSE_POINT) == 0 or
@@ -513,7 +512,7 @@ when DWin:
513512

514513
return win32_xstat_slow_impl(path, status, traverse)
515514

516-
proc win32_xstat(path: WideCString, status: var Stat, traverse: bool): int =
515+
proc win32_xstat(path: WideCString, status: var Stat, traverse: bool): cint =
517516
#[Protocol violation: we explicitly clear errno, instead of
518517
setting it to a POSIX error. Callers should use GetLastError.]#
519518
result = win32_xstat_impl(path, status, traverse)
@@ -523,20 +522,35 @@ when DWin:
523522
status.st_ctime = status.st_birthtime
524523
status.st_ctime_nsec = status.st_birthtime_nsec
525524

525+
template def3STAT(S, L, F){.dirty.} =
526+
template STATf(p: string, s): cint= S
527+
template LSTAT(p: string, s): cint = L
528+
template FSTAT(p: int, s): cint = F
529+
526530

527-
template STATf(p, s): untyped = win32_xstat(newWideCString p, s, true)
528-
template LSTAT(p, s): untyped = win32_xstat(newWideCString p, s, false)
529-
template FSTAT(p, s): untyped = Py_fstat_noraise(p, s)
531+
when DWin:
532+
def3STAT(
533+
win32_xstat(newWideCString p, s, true),
534+
win32_xstat(newWideCString p, s, false),
535+
Py_fstat_noraise(p, s),
536+
)
530537
elif InJs:
531-
template STATf(p, s): untyped = statAux(p, s)
532-
template LSTAT(p, s): untyped = lstatAux(p, s)
533-
template FSTAT(p, s): untyped = fstatAux(p, s)
538+
template ret0(body): cint =
539+
body
540+
0
541+
def3STAT(
542+
ret0 statAux(s, p),
543+
ret0 lstatAux(s, p),
544+
ret0 fstatAux(s, p),
545+
)
534546
else:
535-
template STATf(p, s): untyped = posix.stat(cstring p, s)
536-
template LSTAT(p, s): untyped = posix.lstat(cstring p, s)
537-
template FSTAT(p, s): untyped = posix.fstat(cint p, s)
547+
def3STAT(
548+
posix.stat(cstring p, s),
549+
posix.lstat(cstring p, s),
550+
posix.fstat(cint p, s),
551+
)
538552

539-
proc Py_fstat_noraise*(fd: int, status: var Stat): int =
553+
proc Py_fstat_noraise*(fd: int, status: var Stat): cint =
540554
## EXT.
541555
##
542556
## fileutils.c `_Py_fstat_noraise`
@@ -585,8 +599,8 @@ proc Py_fstat_noraise*(fd: int, status: var Stat): int =
585599

586600
Py_attribute_data_to_stat(info, 0, addr basicInfo, pIdInfo, status)
587601
return 0
588-
else:
589-
int posix.fstat(cint fd, status)
602+
elif InJS:
603+
int FSTAT(fd, status)
590604

591605
proc Py_fstat*(fd: int, status: var Stat) =
592606
## EXT.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import ./pyCfg
44
import ./chkarg
55
import ../private/iph_utils
66

7-
when not InJS:
8-
importConfig [
9-
os
10-
]
11-
else:
7+
importConfig [
8+
os
9+
]
10+
when InJs:
1211
template decl(f, val) =
1312
const `HAVE f` = val
1413
decl unlinkat, false

0 commit comments

Comments
 (0)