Skip to content

Commit

Permalink
pkg/emsdk embind/ CSI-ESC
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Nov 12, 2023
1 parent ae3d761 commit f55c995
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
16 changes: 12 additions & 4 deletions pygbag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@

# WaPy<=>CPython compat

import builtins

try:
# embed builtin module handles I/O on wasm
import embed

# aio function implemented only on stackless WaPy
sched_yield
except:
import builtins


builtins.sched_yield = lambda: None

Expand All @@ -50,13 +52,19 @@ def print_exception(e, out=sys.stderr, **kw):
sys.print_exception = print_exception


def ESC(*argv):
def ESC(*argv, flush=False):
for arg in argv:
sys.__stdout__.write(chr(0x1B))
sys.__stdout__.write(arg)
embed.flush()
if flush:
sys.stdout.flush()


def CSI(*argv):
for arg in argv:
ESC(f"[{arg}")
ESC(f"[{arg}", flush=False)
sys.stdout.flush()


builtins.CSI = CSI
builtins.ESC = ESC
2 changes: 2 additions & 0 deletions pygbag/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def truc(*argv, **kw):
print("truc", argv, kw)

fakehost.window = NoOp("platform.window")
fakehost.window.console = NoOp("platform.window.console")
fakehost.window.console.log = print
fakehost.window.get_terminal_console = truc

import aio.filelike
Expand Down
7 changes: 5 additions & 2 deletions pygbag/support/cross/aio/pep0723.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path

import re
print(sys.path)
import tomllib

import json
Expand Down Expand Up @@ -138,7 +139,7 @@ def install(pkg_file, sconf=None):
if pkg_file not in HISTORY:
HISTORY.append(pkg_file)
importlib.invalidate_caches()
print("142: {pkg_file} installed")
print(f"142: {pkg_file} installed")
except FileExistsError as ex:
print(f"38: {pkg_file} already installed (or partially)", ex)
except Exception as ex:
Expand Down Expand Up @@ -354,7 +355,9 @@ async def check_list(code=None, filename=None):

# we want host to load wasm packages too
# so make pure/bin folder first for imports
sys.path.insert(0, env.as_posix())

if env.as_posix() not in sys.path:
sys.path.insert(0, env.as_posix())

sconf = __import__("sysconfig").get_paths()
sconf["purelib"] = sconf["platlib"] = env.as_posix()
Expand Down
20 changes: 12 additions & 8 deletions pygbag/support/pythonrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,17 +1438,21 @@ def print_pg_bar(total, iteration):
# x10 mouse and xterm stuff
# https://github.com/muesli/termenv/pull/104
# https://xtermjs.org/docs/api/vtfeatures/
def ESC(*argv):
for arg in argv:
sys.__stdout__.write(chr(0x1B))
sys.__stdout__.write(arg)
embed.flush()

if not aio.cross.simulator:
def ESC(*argv):
for arg in argv:
sys.__stdout__.write(chr(0x1B))
sys.__stdout__.write(arg)
embed.flush()

def CSI(*argv):
for arg in argv:
ESC(f"[{arg}")

def CSI(*argv):
for arg in argv:
ESC(f"[{arg}")

builtins.ESC = ESC
builtins.CSI = CSI

try:
console
Expand Down
13 changes: 9 additions & 4 deletions scripts/build-loader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ fi

export PATCH_FS="--preload-file $(realpath platform_wasm/platform_wasm)@/data/data/org.python/assets/site-packages/platform_wasm"


LOPTS="-sMAIN_MODULE --bind -fno-rtti"
# =2 will break pyodide module reuses
LOPTS="-sMAIN_MODULE=1"

# O0/g3 is much faster to build and easier to debug

Expand Down Expand Up @@ -250,9 +250,11 @@ then
" 1>&2

# -std=gnu99 -std=c++23

cat > final_link.sh <<END
#!/bin/bash
emcc $FINAL_OPTS $LOPTS -std=gnu99 -D__PYDK__=1 -DNDEBUG \\
emcc $FINAL_OPTS $LOPTS -D__PYDK__=1 -DNDEBUG \\
-sTOTAL_MEMORY=256MB -sSTACK_SIZE=4MB -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH \\
$CF_SDL \\
--use-preload-plugins \\
Expand All @@ -263,7 +265,10 @@ emcc $FINAL_OPTS $LOPTS -std=gnu99 -D__PYDK__=1 -DNDEBUG \\
--preload-file ${DYNLOAD}@/usr/lib/python${PYBUILD}/lib-dynload \\
--preload-file ${REQUIREMENTS}@/data/data/org.python/assets/site-packages \\
-o ${DIST_DIR}/python${PYMAJOR}${PYMINOR}/${MODE}.js build/${MODE}.o \\
$LDFLAGS -sERROR_ON_UNDEFINED_SYMBOLS=0
$LDFLAGS -lembind -sERROR_ON_UNDEFINED_SYMBOLS=0
# --bind -fno-rtti
END
Expand Down
8 changes: 4 additions & 4 deletions static/pythons.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,14 @@ console.warn("TODO: user defined canvas")
if (vm.config.debug)
console.warn("too tall : have",max_height,"want",want_h)
want_h = max_height
want_w = want_h * ar
want_w = Math.trunc(want_h * ar)
}

if (want_w > max_width) {
if (vm.config.debug)
console.warn("too wide : have",max_width,"want",want_w)
want_w = max_width
want_h = want_h / ar
want_h = Math.trunc(want_h / ar)
}


Expand Down Expand Up @@ -805,13 +805,13 @@ console.warn("TODO: user defined canvas")
if (want_h > max_height) {
//console.warn ("Too much H")
want_h = max_height
want_w = want_h * ar
want_w = Math.trunc(want_h * ar)
}

if (want_w > max_width) {
//console.warn("Too much W")
want_w = max_width
want_h = want_h / ar
want_h = Math.trunc(want_h / ar)
}

// restore phy size
Expand Down

0 comments on commit f55c995

Please sign in to comment.