Skip to content

Commit d24aa65

Browse files
committed
feat(platform): wasm
1 parent 4d38cef commit d24aa65

6 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎Python/cpython.nim‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import lifecycle
88
import ./getversion
99
import ../Parser/[lexer, parser, apis,]
1010
import ../Objects/bundle
11-
import ../Utils/[utils, compat, fileio,]
11+
import ../Utils/[utils, compat, fileio, compat_io_os]
1212
import ./pythonrun
1313
import ./pythonrun/utils
1414
import ./main as pymain
@@ -111,12 +111,12 @@ proc main*(cmdline: string|seq[string] = ""){.mayAsync.} =
111111
template noLongOption(p: OptParser) =
112112
if p.kind == cmdLongOption:
113113
p.unknownOption()
114-
when defined(js) and cmdline is seq[string]:
114+
when defined(js) or defined(wasm):
115115
# fix: initOptParser will call paramCount.
116116
# which is only defined when -d:nodejs
117117
var cmdline =
118118
if cmdline.len == 0: @["-"]
119-
else: cmdline
119+
else: commandLineParamsCompat()
120120
var
121121
args: seq[string]
122122
versionVerbosity = 0
@@ -157,7 +157,7 @@ proc main*(cmdline: string|seq[string] = ""){.mayAsync.} =
157157
of 1: echoVersion()
158158
else: echoVersion(verbose=true)
159159

160-
when isMainModule and appType != "lib":
160+
when isMainModule and appType != "lib" and not defined(npy_noMain):
161161
when defined(js):
162162
{.error: "python.nim is for c target. Compile jspython.nim as js target" .}
163163

‎Python/lifecycle.nim‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@ proc pycore_init_types =
4646
chk PyExc_InitTypes(): "failed to initialize an exception type"
4747
Py_Int_Float_InitTypes handle_PyStatus_ERR
4848

49+
const
50+
wasm = defined(wasm)
51+
needCallMain = defined(npy_noMain)
52+
when needCallMain:
53+
when wasm:
54+
#XXX:NIM-BUG: bypass a new static INLINE was generated
55+
{.emit: """void NimMain();""".}
56+
proc NimMain() {.importc, nodecl.}
57+
else:
58+
proc NimMain() {.importc, cdecl.}
4959
proc pycore_interp_init =
60+
when needCallMain:
61+
NimMain()
5062
pycore_init_types()
5163

5264
chk PySys_Create(sys): "can't initialize sys module"
@@ -88,7 +100,7 @@ proc pyInit*(args: seq[string]) =
88100
]#
89101
pyConfig.program_name = getAppFilenameCompat()
90102
pyConfig.executable = pyConfig.program_name
91-
when compiles(expandFilename""):
103+
when not wasm and compiles(expandFilename""):
92104
pyConfig.executable = pyConfig.executable.expandFilename()
93105
pyConfig.argv = args
94106
#TODO:argv shall be filtered to remove `-X`,etc

‎README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Python programming VM implemented in Nim.
2323
- Fun and practice. Learn both Python and Nim.
2424
- Serve as a altertive small version of CPython
2525
(as of 0.1.1, less than 2MB on release build mode)
26-
26+
- Used as plugin for those using WASM
2727

2828
### How to use
2929

@@ -61,6 +61,12 @@ see help message
6161
nimble build
6262
bin/npython
6363
```
64+
##### For a WASM executable (wasi-sdk backend)
65+
66+
```
67+
nimble buildWasm
68+
wasmtime --invoke NimMain bin/npython.wasm
69+
```
6470

6571
##### For JS backend
6672

‎Utils/destroyPatch.nim‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
template sinceNim(ma, mi, pa): bool =
33
(NimMajor, NimMinor, NimPatch) >= (ma, mi, pa)
4-
const Js = defined(js)
4+
const Js = defined(js) or defined(wasm)
55
when not Js and sinceNim(2,1,1) or
66
Js and sinceNim(2,3,2):
77
template defdestroy*(self; Self; body){.dirty.} =

‎Utils/nexportc.nim‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import ./nexportc_header
33
export nexportc_header
4-
when appType == "lib":
4+
when appType == "lib" or defined(wasm):
55
import ./nexportcImpl
66
export nexportcImpl
77
else:

‎npython.nimble‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ template pylib(x, ver) =
3636
else: pylibPre & x
3737

3838
pylib "pyrepr", " ^= 0.1.1"
39-
pylib "jscompat", " ^= 0.1.3"
39+
pylib "jscompat", " ^= 0.1.4"
4040
pylib "translateEscape", " ^= 0.1.0"
4141

4242
# copied from nimpylib.nimble
@@ -101,6 +101,16 @@ taskWithArgs buildDbg, "debug build, output product will be appended with a suff
101101
taskWithArgs buildLib, "build shared library":
102102
selfExecBuildWithSrcAdd "c --app:lib", (binDir / namedBin[srcName].toDll), args
103103

104+
#taskRequires "buildWasm", "wasm_backend ^= 0.1.2"
105+
taskWithArgs buildWasm, "build .wasm(wasi) executable":
106+
pylib "wasm_backend", " ^= 0.1.2"
107+
let res = gorgeEx("nim-wasm-build-flags " & NimVersion, cache=NimVersion)
108+
if res.exitCode != 0:
109+
quit res.output
110+
let cmd = "c " & res.output
111+
selfExecBuildWithSrcAdd cmd,
112+
binPathWithoutExt & ".wasm", args
113+
104114
taskWithArgs buildJs, "build JS. supported backends: " &
105115
"-d:nodejs|-d:deno|-d:jsAlert":
106116
selfExecBuildWithSrcAdd "js", binPathWithoutExt & ".js", args

0 commit comments

Comments
 (0)