summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-02-06 10:40:29 +0800
committerGitHub <[email protected]>2026-02-06 10:40:29 +0800
commit095d5034b4cba62e7476d2b09e7a9a6a19d27255 (patch)
tree9490b6117dcd057408e501e842f6521fc0bd7ce8
parentb366835683953d6be0600e2b9c7ee4dea7968c61 (diff)
Refactor WASM target execution to use helper function
-rw-r--r--xmake/actions/run/main.lua37
1 files changed, 21 insertions, 16 deletions
diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua
index 161e8ccae..db1ccef3b 100644
--- a/xmake/actions/run/main.lua
+++ b/xmake/actions/run/main.lua
@@ -33,6 +33,26 @@ import("private.detect.check_targetname")
import("lib.detect.find_tool")
import("private.action.utils", {alias = "action_utils"})
+function _run_wasm_target_in_browser(targetfile, opt)
+ opt = opt or {}
+ local rundir = opt.rundir
+ local addenvs = opt.addenvs
+ local setenvs = opt.setenvs
+ local emrun = find_tool("emrun")
+ if emrun then
+ os.execv(emrun.program, {targetfile}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs})
+ else
+ local python = find_tool("python3")
+ if not python then
+ raise("emrun or python not found, which is required for running wasm target in browser!")
+ end
+ local url = "http://localhost:8000/" .. path.relative(targetfile, rundir):gsub("\\", "/")
+ print("please open the url in browser")
+ cprint("${color.success}%s${clear}", url)
+ os.execv(python.program, {"-m", "http.server", "--bind", "127.0.0.1", "8000"}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs})
+ end
+end
+
-- run target
function _do_run_target(target)
@@ -61,21 +81,7 @@ function _do_run_target(target)
-- run wasm target in browser
if target:is_plat("wasm") then
- -- try to run with emrun
- local emrun = find_tool("emrun")
- if emrun then
- os.execv(emrun.program, {targetfile}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs})
- else
- -- try to run with python
- local python = find_tool("python3")
- if not python then
- raise("emrun or python not found, which is required for running wasm target in browser!")
- end
- local url = "http://localhost:8000/" .. path.relative(targetfile, rundir):gsub("\\", "/")
- print("please open the url in browser")
- cprint("${color.success}%s${clear}", url)
- os.execv(python.program, {"-m", "http.server", "--bind", "127.0.0.1", "8000"}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs})
- end
+ _run_wasm_target_in_browser(targetfile, {rundir = rundir, addenvs = addenvs, setenvs = setenvs})
return
end
@@ -269,4 +275,3 @@ function main()
-- leave project directory
os.cd(oldir)
end
-