Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions include/pyjs/pre_js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Module._is_initialized = false

Module['init_phase_1'] = async function(prefix, python_version, verbose) {

if(verbose){console.log("in init phase 1");}
if(verbose){console.log("in init phase 1");}
let version_str = `${python_version[0]}.${python_version[1]}`;

// list of python objects we need to delete when cleaning up
Expand All @@ -19,7 +19,7 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
var p = await Module['_wait_run_dependencies']();

if(prefix == "/"){
Module.setenv("LANG", "en_US.UTF-8");
Module.setenv("LANG", "en_US.UTF-8");

// LC_COLLATE="C"
// LC_CTYPE="UTF-8"
Expand Down Expand Up @@ -48,7 +48,7 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
}




// Module["_interpreter"] = new Module["_Interpreter"]()
console.log("initialize interpreter");
Expand Down Expand Up @@ -164,7 +164,7 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
return ret['ret']
}
};
if(verbose){console.log("in init phase 1 done!!");}
if(verbose){console.log("in init phase 1 done!!");}
}

Module['init_phase_2'] = function(prefix, python_version, verbose) {
Expand Down Expand Up @@ -216,7 +216,7 @@ def _add_resolve_done_callback(future, resolve, reject):

ensured_future.add_done_callback(done)
`)

Module._add_resolve_done_callback = Module.eval(`_add_resolve_done_callback`)
Module._py_objects.push(Module._add_resolve_done_callback);

Expand Down Expand Up @@ -262,17 +262,43 @@ _mock_termios()
del _mock_termios

def _mock_webbrowser():
webbrowser_mock = types.ModuleType("webbrowser")

def get():
webbrowser_mock

def open(url, new=0, autoraise=True):
pass
import pyjs
is_main_thread = pyjs.js.Function("""return typeof WorkerGlobalScope === "undefined" || !(self instanceof WorkerGlobalScope);""")()
if is_main_thread:
pyjs.js.window.open(url)
else:
# we're in a web worker
# This is sent to the main thread, which will do the window.open if implemented
obj = pyjs.js.Function("url","n",
"""
return {'OPEN_TAB':{'url': url, 'new': n}}
"""
)(url, new)
pyjs.js.postMessage(obj)

def open_new(url):
return open(url, 1)

def open_new_tab(url):
return open(url, 2)

webbrowser_mock = types.ModuleType("webbrowser")
webbrowser_mock.name = pyjs.js.Function("""
return /firefox/i.test(navigator.userAgent) ? "firefox"
: /edg/i.test(navigator.userAgent) ? "edge"
: /chrome|crios/i.test(navigator.userAgent) ? "chrome"
: /safari/i.test(navigator.userAgent) ? "safari"
: "Unknown";""")()
webbrowser_mock.get = get
webbrowser_mock.open = open
webbrowser_mock.open_new = open_new
webbrowser_mock.open_new_tab = open_new_tab
webbrowser_mock.Error = RuntimeError

sys.modules["webbrowser"] = webbrowser_mock
_mock_webbrowser()
Expand Down
26 changes: 11 additions & 15 deletions tests/tests/test_pyjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
if sys.version_info[0] == 3 and sys.version_info[1] >= 13:
def test_ssl_import():
import ssl

def test_import_lzma():
import lzma

def test_js_submodule():
from pyjs.js import Function

Expand Down Expand Up @@ -247,8 +247,8 @@ def test_to_js_dict():


def test_bytes_to_js():
pyjs.to_js(b"\x00").byteLength == 1
pyjs.to_js(b"\x00").byteLength == 1



def test_to_js_none():
Expand Down Expand Up @@ -393,7 +393,7 @@ def implicit_to_js(self):
obj = pyjs.js_object()
obj["the_value"] = self.value
return obj

class Bar(object):
def __init__(self, value):
self.value = value
Expand All @@ -403,7 +403,7 @@ def explicit_to_js(self):
obj = pyjs.js_object()
obj["the_value"] = self.value
return obj

class FooBarInstance(object):
def __init__(self, value):
self.value = value
Expand All @@ -419,8 +419,8 @@ def implicit_to_js(self):
obj = pyjs.js_object()
obj["the_implicit_value"] = self.value
return obj



foo_instance = Foo(42)
js_function = pyjs.js.Function("instance", """
Expand All @@ -439,7 +439,7 @@ def implicit_to_js(self):
""")
assert js_function(foo_bar_instance) is True
assert js_function(pyjs.to_js(foo_bar_instance)) is False

js_function = pyjs.js.Function("instance", """
return instance.the_explicit_value === 42;
""")
Expand All @@ -464,13 +464,13 @@ def test_literal_map():
if (converted.get("foo") !== 1 || converted.get("bar") !== "bar" || !converted.get("foobar").includes("foo") || !converted.get("foobar").includes("bar")) {
throw new Error("converted does not have the correct keys");
}
// ensure we can access the values via .
// ensure we can access the values via .
if (converted.foo !== 2 || converted.bar !== "bar" || !converted.foobar.includes("foo") || !converted.foobar.includes("bar")) {
throw new Error("converted does not have the correct values when accessed via .");
}
return true;
""")


def test_del_attr():
obj = eval_jsfunc(
Expand Down Expand Up @@ -696,7 +696,3 @@ def test_imports_sys():

def test_webbrowser():
from webbrowser import open, open_new, open_new_tab

open("google.com")
open_new("google.com")
open_new_tab("google.com")