Skip to content

Commit

Permalink
bump sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Feb 12, 2024
1 parent 3560789 commit d36ee5e
Show file tree
Hide file tree
Showing 15 changed files with 526 additions and 773 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
runs-on: ubuntu-22.04
env:
SDK_VERSION: 3.1.53.2bi
SDK_VERSION: 3.1.54.1bi
SYS_PYTHON: /usr/bin/python3
PACKAGES: emsdk hpy pygame
BUILD_STATIC: emsdk hpy
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Community Documentation : [https://pygame-web.github.io](https://pygame-web.gith

Runs python code directly in modern web browsers, including mobile versions.


Quick Start:

"your.app.folder" must contain a main.py and its loop must be async aware eg:

```py
Expand Down
157 changes: 88 additions & 69 deletions patchwork/pykpocket/pp_modules.cpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,112 @@
#define MODULE_NAME "embed"

PyObject *
PyInit_embed ()
{
PyObject *mod = vm->new_module (MODULE_NAME);
if (!mod)
{
puts ("PyInit_" MODULE_NAME " : module not allocated");
return NULL;
PyInit_embed() {
PyObject *mod = vm->new_module(MODULE_NAME);
if (!mod) {
puts("PyInit_" MODULE_NAME " : module not allocated");
return NULL;
}

vm->bind (mod, "readline() -> str", [] (VM *vm, ArgsView argv) {
return embed_readline (PyNULL, PyNULL);
});
vm->bind(mod, "readline() -> str", [](VM * vm, ArgsView argv) {
return embed_readline(PyNULL, PyNULL);}
);

vm->bind (mod, "stdin_select() -> int", [] (VM *vm, ArgsView argv) {
return embed_stdin_select (PyNULL, PyNULL);
});
vm->bind(mod, "stdin_select() -> int", [](VM * vm, ArgsView argv) {
return embed_stdin_select(PyNULL, PyNULL);
});

vm->bind (mod, "os_read() -> bytes", [] (VM *vm, ArgsView argv) {
return embed_os_read (PyNULL, PyNULL);
});
vm->bind(mod, "os_read() -> bytes", [](VM * vm, ArgsView argv) {
return embed_os_read(PyNULL, PyNULL);
});

vm->bind (mod, "emscripten_cancel_main_loop() -> None",
[] (VM *vm, ArgsView argv) {
emscripten_cancel_main_loop ();
Py_RETURN_NONE;
});
vm->bind(mod, "emscripten_cancel_main_loop() -> None", [](VM * vm, ArgsView argv) {
emscripten_cancel_main_loop(); Py_RETURN_NONE;
});

vm->bind (mod, "_new_module(name:str, code:str) -> None",
[] (VM *vm, ArgsView argv) {
Str &name = py_cast<Str &> (vm, argv[0]);
Str &code = py_cast<Str &> (vm, argv[1]);
vm->_lazy_modules[name] = code;
Py_RETURN_NONE;
});
vm->bind(mod, "_new_module(name:str, code:str) -> None", [](VM * vm, ArgsView argv) {
Str & name = py_cast < Str & >(vm, argv[0]);
Str & code = py_cast < Str & >(vm, argv[1]);
vm->_lazy_modules[name] = code;
Py_RETURN_NONE;
});

//_CAST(Str&, vm->py_str(argv[0])).c_str());
//_CAST(Str&, vm->py_str(argv[0])).c_str());

#if defined(__EMSCRIPTEN__)
vm->bind (mod, "jseval(js: str='') -> Any", [] (VM *vm, ArgsView argv) {
Str &js = py_cast<Str &> (vm, argv[0]);

char *json = (char *)EM_ASM_PTR ({
try {
const toeval = UTF8ToString ($0);
var result = null;
const evalue = eval (toeval);
if (evalue instanceof Function)
{
result = "[native code]";
vm->bind(mod, "jseval(js: str='') -> Any",[](VM * vm, ArgsView argv) {
Str & js = py_cast < Str & >(vm, argv[0]);
char *json = (char *)EM_ASM_PTR({
try {
const toeval = UTF8ToString($0);
var result = null;
const evalue = eval(toeval);
if (evalue instanceof Function) {
result = "[native code]";
} else {
if (evalue === undefined) {
result = null;
}
else {
result = evalue;
}
}
else {
if (evalue === undefined) {
result = null;
} else {
result = evalue;
}
}
} catch (x) {
result = `Exception (${ x })`;
}
return stringToNewUTF8 (JSON.stringify (result));
}, js.c_str ());

PyObject *json_loads = vm->_modules["json"]->attr ("loads");
PyObject *result
= vm->call (json_loads, py_var (vm, std::string_view (json)));
free (json);
return result;
});
} catch(x) {
result = (`Exception(${x})`);
}
return stringToNewUTF8(JSON.stringify(result));
}, js.c_str());

PyObject * json_loads = vm->_modules["json"]->attr("loads");
PyObject * result = vm->call(json_loads, py_var(vm, std::string_view(json)));
free(json);
return result;
});

#else
vm->bind (mod, "jseval(js: str='') -> str", [] (VM *vm, ArgsView argv) {
Str &js = py_cast<Str &> (vm, argv[0]);
emscripten_run_script (js.c_str ());
return py_var (vm, js);
});
vm->bind(mod, "jseval(js: str='') -> str",[](VM * vm, ArgsView argv) {
Str & js = py_cast < Str & >(vm, argv[0]);
emscripten_run_script(js.c_str());
return py_var(vm, js);
});
#endif

vm->bind (mod, "is_browser() -> int", [] (VM *vm, ArgsView argv) {
vm->bind(mod, "is_browser() -> int",[](VM * vm, ArgsView argv) {
#if defined(__EMSCRIPTEN__)
int test_result = EM_ASM_INT ({ return console.vm.is_browser; });
int test_result = EM_ASM_INT({
return console.vm.is_browser;
});
#else
int test_result = 0;
#endif
return py_var (vm, test_result);
});
return py_var(vm, test_result);
});
return mod;
}

#undef MODULE_NAME

return mod;

#if PK_ENABLE_OS
#else
#define MODULE_NAME "os"

PyObject *
PyInit_os() {
PyObject *mod = vm->new_module(MODULE_NAME);
if (!mod) {
puts("PyInit_" MODULE_NAME " : module not allocated");
return NULL;
}

vm->bind(mod, "read() -> bytes",[](VM * vm, ArgsView argv) {
return embed_os_read(PyNULL, PyNULL);
});

return mod;
}

#undef MODULE_NAME
#endif


Loading

0 comments on commit d36ee5e

Please sign in to comment.