Skip to content

Commit 7606722

Browse files
committed
fixup(js): fixup JS support:sys.executable,platform, timeit
1 parent d72dabd commit 7606722

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/pylib/Lib/sys.nim

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import std/os
99
import std/fenv
1010
when defined(js) or not (defined(Py_FORCE_UTF8_FS_ENCODING) or defined(windows)):
11-
from std/strutils import toLowerAscii
11+
import std/strutils # toLowerAscii startsWith
1212
when defined(nimPreviewSlimSystem):
1313
import std/assertions
1414

@@ -48,13 +48,13 @@ when not weirdTarget and not defined(windows):
4848

4949
proc getPlatform(): string =
5050
when defined(js):
51-
template inString(jo: JsObject): string =
52-
$(jo.to(cstring))
5351
when defined(nodejs):
54-
return require("os").platform().inString
52+
proc `os.platform`(): cstring{.importjs:
53+
"require('os').platform()".}
54+
return `os.platform`().`$`
5555
else:
56-
let navigator{.importcpp.}: JsObject
57-
result = navigator.platform.inString.toLowerAscii
56+
let `navigator.platform`{.importjs: "navigator.platform".}: cstring
57+
result = `navigator.platform`.`$`.toLowerAscii
5858
result =
5959
if result.startsWith "win32": "win32"
6060
elif result.startsWith "linux": "linux"
@@ -78,11 +78,14 @@ proc getPlatform(): string =
7878
# XXX: haiku, netbsd ok ?
7979
hostOS & uname_release_major()
8080

81-
const platform*: PyStr =
82-
str getPlatform()
83-
## .. note:: the value is standalone for bare system
84-
## and haiku/netbsd appended with major version instead of "unknown".
85-
## In short, this won't be "unknown" as Python does.
81+
when defined(js):
82+
let platform* = str getPlatform()
83+
else:
84+
const platform*: PyStr =
85+
str getPlatform()
86+
## .. note:: the value is standalone for bare system
87+
## and haiku/netbsd appended with major version instead of "unknown".
88+
## In short, this won't be "unknown" as Python does.
8689

8790
when not weirdTarget:
8891
when not defined(pylibSysNoStdio):
@@ -132,15 +135,15 @@ const
132135
#rounds: 1
133136
) ## float_info.rounds is defined as a `getter`, see `rounds`_
134137
135-
when not defined(nimscript):
138+
when not weirdTarget:
136139
let fiRound = fegetround().int
137140
template rounds*(fi: typeof(float_info)): int =
138141
## not available when nimscript
139142
bind fiRound
140143
fiRound
141144
else:
142145
template rounds*(fi: typeof(float_info)): int =
143-
{.error: "not available for nimscript/compile-time".}
146+
{.error: "not available for nimscript/JavaScript/compile-time".}
144147
145148
func int2hex(x: int): int =
146149
## 10 -> 0x10
@@ -216,6 +219,14 @@ else:
216219

217220
when defined(nimscript):
218221
template executable*: PyStr = str getCurrentCompilerExe()
222+
elif defined(js):
223+
const execPathJs =
224+
when defined(nodejs): "process.execPath"
225+
else: "typeof Deno === 'undefined' ? '' : Deno.execPath()"
226+
let execPath{.importjs: execPathJs.}: cstring
227+
template executable*: PyStr =
228+
bind execPath
229+
str execPath
219230
else:
220231
template executable*: PyStr =
221232
## .. note:: when nimscript, this is path of `Nim`;

src/pylib/Lib/timeit.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ template autorange*(self: Timer, callable=None): (int, float) =
1717
autorange(self, nil)
1818

1919
template sys_stderr: untyped = sys.stderr
20-
proc print_exc*(self: Timer, file: NoneType|File|typeof(sys.stderr) = None) =
20+
proc print_exc*(self: Timer, file: auto = None) =
2121
bind print_exc, sys_stderr
2222
print_exc(self,
2323
when file is NoneType: sys_stderr

0 commit comments

Comments
 (0)