Skip to content

Commit

Permalink
Inject execution options in web worker code. Related to issue brython…
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreQuentel committed Apr 30, 2024
1 parent 3092dfb commit db1e1d5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
12 changes: 7 additions & 5 deletions www/src/brython.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ $B.getPythonModule=function(name){return $B.imported[name]}
$B.python_to_js=function(src,script_id){
return "(function() {\n"+from_py(src,script_id)+"\nreturn locals}())"}
$B.pythonToJS=$B.python_to_js
function fakeScript(filename){this.options={}}
var fakeScript=$B.fakeScript=function(filename){this.options={}}
fakeScript.prototype.getAttribute=function(key){return this.options[key]?? null}
fakeScript.prototype.dispatchEvent=function(){}
$B.runPythonSource=function(src,options){var script_id
Expand Down Expand Up @@ -180,8 +180,8 @@ $B.unicode_bidi_whitespace=[9,10,11,12,13,28,29,30,31,32,133,5760,8192,8193,8194
;
__BRYTHON__.implementation=[3,12,3,'dev',0]
__BRYTHON__.version_info=[3,12,0,'final',0]
__BRYTHON__.compiled_date="2024-04-27 08:04:32.357791"
__BRYTHON__.timestamp=1714197872357
__BRYTHON__.compiled_date="2024-04-30 14:36:49.337002"
__BRYTHON__.timestamp=1714480609336
__BRYTHON__.builtin_module_names=["_ajax","_ast","_base64","_binascii","_io_classes","_json","_jsre","_locale","_multiprocessing","_posixsubprocess","_profile","_random","_sre","_sre_utils","_string","_strptime","_svg","_symtable","_tokenize","_webcomponent","_webworker","_zlib_utils","_zlib_utils1","_zlib_utils_kozh","array","builtins","dis","encoding_cp932","encoding_cp932_v2","hashlib","html_parser","marshal","math","modulefinder","posix","pyexpat","python_re","python_re_new","unicodedata"]
;

Expand Down Expand Up @@ -838,7 +838,7 @@ $B.options_parsed=true
return options}
if(!($B.isWebWorker ||$B.isNode)){var startup_observer=new MutationObserver(function(mutations){for(var mutation of mutations){for(var addedNode of mutation.addedNodes){addPythonScript(addedNode);}}});
startup_observer.observe(document.documentElement,{childList:true,subtree:true});}
var brython_options={}
var brython_options=$B.brython_options={}
var python_scripts=[]
if(!$B.isWebWorker){
python_scripts=python_scripts.concat(Array.from(
Expand Down Expand Up @@ -5724,7 +5724,9 @@ path.shift()}}
var pythonpath=$B.get_option_from_filename('pythonpath',filename)
if(pythonpath){
var ix=path.indexOf(script_dir)
if(ix===-1){console.log('bizarre, script_dir',script_dir,'not in path',path)}else{path.splice(ix,1,...pythonpath)}}
if(ix===-1){console.log('bizarre, script_dir',script_dir,'not in path',path)}else{var fullpaths=[]
for(var p of pythonpath){if(p=='.'){fullpaths.push(script_dir)}else if(! p.startsWith($B.script_domain)){fullpaths.push(script_dir+'/'+p)}else{fullpaths.push(p)}}
path.splice(ix,1,...fullpaths)}}
if($B.protocol !=="file"){meta_path.push($B.finders.path)
path_hooks.push($B.url_hook)}
$B.import_info[filename]={meta_path,path_hooks,path}}
Expand Down
2 changes: 1 addition & 1 deletion www/src/brython_builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ $B.python_to_js = function(src, script_id){

$B.pythonToJS = $B.python_to_js

function fakeScript(filename){
var fakeScript = $B.fakeScript = function(filename){
this.options = {}
}

Expand Down
2 changes: 1 addition & 1 deletion www/src/brython_stdlib.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions www/src/libs/_webworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function create_worker(){
$B.url2name[filename] = script_id

var brython_scripts = scripts_to_load(
$B.get_option_from_filename('debug', filename))
$B.get_option_from_filename('debug', filename))

var js = $B.py2js({src, filename}, script_id).to_js(),
header = '';
Expand Down Expand Up @@ -204,11 +204,27 @@ function create_worker(){
header += '$B.file_cache[module.__file__] = `' + src + '`\n'
// restore brython_path
header += `$B.brython_path = "${$B.brython_path}"\n`
// inject script attributes to get options
header += `var script = $B.scripts["${filename}"] = new $B.fakeScript()\n`
for(var key in $B.brython_options){
var value = $B.brython_options[key]
if(Array.isArray(value)){
value = `[${value.map(x => '"' + x + '"')}]`
}else{
value = `"${value}"`
}
header += `script.options["${key}"] = ${value}\n`
}

for(var attr of worker_script.attributes){
header += `script.options["${attr.name}"] = "${attr.value}"\n`
}

// restore path for imports (cf. issue #1305)
header += `$B.make_import_paths("${filename}")\n`

// Call brython() to initialize internal Brython values
var save_option = JSON.stringify($B.save_options)
var save_option = JSON.stringify($B.brython_options)
header += `brython(${save_option})\n`

// send dummy message to trigger resolution of Promise
Expand Down
2 changes: 1 addition & 1 deletion www/src/py2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ if(!($B.isWebWorker || $B.isNode)){
});
}

var brython_options = {}
var brython_options = $B.brython_options = {}

var python_scripts = []

Expand Down
12 changes: 11 additions & 1 deletion www/src/py_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,17 @@ $B.make_import_paths = function(filename){
console.log('bizarre, script_dir', script_dir,
'not in path', path)
}else{
path.splice(ix, 1, ...pythonpath)
var fullpaths = []
for(var p of pythonpath){
if(p == '.'){
fullpaths.push(script_dir)
}else if(! p.startsWith($B.script_domain)){
fullpaths.push(script_dir + '/' + p)
}else{
fullpaths.push(p)
}
}
path.splice(ix, 1, ...fullpaths)
}
}
// Use the defaut finder using sys.path if protocol is not file://
Expand Down
4 changes: 2 additions & 2 deletions www/src/version_info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__BRYTHON__.implementation = [3, 12, 3, 'dev', 0]
__BRYTHON__.version_info = [3, 12, 0, 'final', 0]
__BRYTHON__.compiled_date = "2024-04-27 08:04:32.357791"
__BRYTHON__.timestamp = 1714197872357
__BRYTHON__.compiled_date = "2024-04-30 14:36:49.337002"
__BRYTHON__.timestamp = 1714480609336
__BRYTHON__.builtin_module_names = ["_ajax",
"_ast",
"_base64",
Expand Down

0 comments on commit db1e1d5

Please sign in to comment.