Skip to content

Commit 9768400

Browse files
committed
wip
1 parent 8ba5530 commit 9768400

File tree

7 files changed

+48
-32
lines changed

7 files changed

+48
-32
lines changed

include/pyjs/pre_js/init.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,18 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
5757

5858

5959

60-
default_scope = Module["globals"]()
61-
// Module["default_scope"] = default_scope;
62-
// Module['_py_objects'].push(Module["default_scope"]);
63-
60+
var default_scope = Module["main_scope"]()
61+
Module["default_scope"] = default_scope;
6462

6563
Module['_py_objects'].push(Module["_interpreter"]);
6664

6765

6866

69-
Module['exec'] = function(code, globals=default_scope) {
67+
Module['exec'] = function(code, globals=default_scope, locals=default_scope) {
7068
if(globals === undefined){
7169
globals = Module["globals"]()
7270
}
73-
let ret = Module._exec(code, globals)
71+
let ret = Module._exec(code, globals, locals)
7472
// console.error("exec done");
7573
if (ret.has_err) {
7674
throw ret
@@ -79,23 +77,23 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
7977

8078

8179

82-
Module['eval'] = function(code, globals=default_scope) {
80+
Module['eval'] = function(code, globals=default_scope, locals=default_scope) {
8381
if(globals === undefined){
8482
globals = Module["globals"]()
8583
}
86-
let ret = Module._eval(code, globals)
84+
let ret = Module._eval(code, globals, locals)
8785
if (ret.has_err) {
8886
throw ret
8987
} else {
9088
return ret['ret']
9189
}
9290
};
9391

94-
Module['eval_file'] = function(file, globals=default_scope) {
92+
Module['eval_file'] = function(file, globals=default_scope, locals=default_scope) {
9593
if(globals === undefined){
9694
globals = Module["globals"]()
9795
}
98-
let ret = Module._eval_file(file, globals)
96+
let ret = Module._eval_file(file, globals, locals)
9997
if (ret.has_err) {
10098
throw ret
10199
}
@@ -160,7 +158,8 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
160158

161159

162160
let types = keys.map(Module['_get_type_string'])
163-
let ret = this._raw_getitem(keys, types, BigInt(keys.length))
161+
console.log("getitem keys", keys, types);
162+
let ret = this._raw_getitem(keys, types, keys.length)
164163
if (ret.has_err) {
165164
throw ret
166165
} else {

src/convert.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ namespace pyjs
1515
std::pair<em::val,bool> implicit_py_to_js(py::object& py_ret)
1616
{
1717
// py::module_ pyjs = py::module_::import("pyjs_utils");
18-
// const std::string info = pyjs.attr("implicit_convert_info")(py_ret).cast<std::string>();
18+
// const std::string info = pyjs.attr("implicit_convert_info")(py_ret).cast<std::string>();
1919

2020

2121
const std::string info = py_ret.get_type().attr("__name__").str();
2222

2323
if (info == "int")
2424
{
25-
return std::make_pair(em::val(py_ret.cast<int>()),false);
25+
auto emval = em::val(py_ret.cast<int>());
26+
return std::make_pair(std::move(emval),false);
2627
}
2728
else if (info == "str")
2829
{

src/export_js_module.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ namespace pyjs
2222
namespace py = pybind11;
2323

2424

25-
em::val eval(const std::string & code, py::object & globals)
25+
em::val eval(const std::string & code, py::object & globals , py::object & locals)
2626
{
2727
em::val ret = em::val::object();;
2828
ret.set("has_err", false);
2929
try
3030
{
31-
py::object py_ret = py::eval(code, globals);
31+
py::object py_ret = py::eval(code, globals, locals);
3232
auto [jsval, is_proxy] = implicit_py_to_js(py_ret);
3333
ret.set("ret",jsval);
3434
ret.set("is_proxy",is_proxy);
@@ -45,13 +45,13 @@ namespace pyjs
4545

4646

4747

48-
em::val exec(const std::string & code, py::object & globals)
48+
em::val exec(const std::string & code, py::object & globals, py::object & locals)
4949
{
5050
em::val ret = em::val::object();
5151
ret.set("has_err", false);
5252
try
5353
{
54-
py::exec(code, globals);
54+
py::exec(code, globals, locals);
5555
return ret;
5656
}
5757
catch (py::error_already_set& e)
@@ -65,12 +65,12 @@ namespace pyjs
6565
}
6666

6767

68-
em::val eval_file(const std::string & filename, py::object & globals)
68+
em::val eval_file(const std::string & filename, py::object & globals, py::object & locals)
6969
{
7070
em::val ret = em::val::object();
7171
try
7272
{
73-
py::eval_file(filename, globals);
73+
py::eval_file(filename, globals, locals);
7474
ret.set("has_err",em::val(false));
7575
return ret;
7676
}
@@ -138,15 +138,15 @@ namespace pyjs
138138
export_py_object();
139139

140140
// // main scope
141-
// em::function("main_scope",em::select_overload<py::object()>(
142-
// []()->py::object{
143-
// std::cout<<"get scope"<<std::endl;
144-
// auto scope = py::module_::import("__main__").attr("__dict__");
145-
// //py::exec("import pyjs;import asyncio", scope);
146-
// std::cout<<"get scope DONE"<<std::endl;
147-
// return scope;
148-
// }
149-
// ));
141+
em::function("main_scope",em::select_overload<py::object()>(
142+
[]()->py::object{
143+
std::cout<<"get scope"<<std::endl;
144+
auto scope = py::module_::import("__main__").attr("__dict__");
145+
//py::exec("import pyjs;import asyncio", scope);
146+
std::cout<<"get scope DONE"<<std::endl;
147+
return scope;
148+
}
149+
));
150150

151151

152152
em::function("globals", +[]()->py::object{

src/export_py_object.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ namespace pyjs
8989

9090
// implicit to py
9191

92+
std::cout<<"getitem n_keys "<<n_keys<<std::endl;
93+
9294
py::list py_args;
9395
for (std::size_t i = 0; i < n_keys; ++i)
9496
{
@@ -141,6 +143,7 @@ namespace pyjs
141143
em::select_overload<em::val(py::object&)>(
142144
[](py::object& pyobject) -> em::val
143145
{
146+
std::cout<<"call 0-ary"<<std::endl;
144147
try
145148
{
146149
py::gil_scoped_acquire acquire;
@@ -158,6 +161,7 @@ namespace pyjs
158161
em::select_overload<em::val(py::object&, em::val)>(
159162
[](py::object& pyobject, em::val arg1) -> em::val
160163
{
164+
std::cout<<"call 1-ary"<<std::endl;
161165
try
162166
{
163167
py::gil_scoped_acquire acquire;

src/js_timestamp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define PYJS_JS_UTC_TIMESTAMP "2025-10-07 07:20:42.296449"
1+
#define PYJS_JS_UTC_TIMESTAMP "2025-10-07 08:54:42.780758"

tests/atests/async_tests.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async def test_stuff_2():
1212
assert 2 == 2
1313

1414

15-
async def test_callbacks_in_async():
15+
async def test_callbacks_in_async_1():
1616
def py_func(arg1, arg2):
1717
return arg1 * 2 + arg2
1818

@@ -32,12 +32,15 @@ def py_func(arg1, arg2):
3232
cleanup.delete()
3333

3434

35-
async def test_callbacks_in_async():
35+
async def test_callbacks_in_async_2():
3636

3737
async_js_function = pyjs.js.Function(
3838
"""
3939
return async function(py_dict){
40-
return py_dict.get('value')
40+
console.log('call py: ')
41+
const val = py_dict.get('value')
42+
console.log('got value:', val)
43+
return val
4144
}
4245
"""
4346
)()

tests/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import pyjs
22
import time
3+
4+
import sys
5+
sys.path.append("/tests")
6+
7+
# print current cwd
8+
import os
9+
print("CWD:", os.getcwd())
10+
311
from atests import *
412
from js_tests import main as js_main
513

@@ -42,6 +50,7 @@ async def main():
4250
return 1
4351

4452
if run_sync_pytest_tests:
53+
4554
import pyjs
4655
import pytest
4756

0 commit comments

Comments
 (0)