Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Some tries :p #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Cache-control" content="no-cache">
</head>
<body>
<pre id="micropython-stdout"></pre>
hello:
<py-script worker="worker1">
</py-script>
<script src="pyscript.js" type="module"></script>
<script type="text/javascript">
</script>
</body>
</html>
46 changes: 42 additions & 4 deletions pyscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const main = function() {
const code = this.textContent;
this.textContent = "";
const script = {
code: code.trim() ? code : "",
code: code,
src: this.attributes.src ? this.attributes.src.value : "",
target: this
};
Expand Down Expand Up @@ -293,8 +293,42 @@ const main = function() {
static get url() {
return "mpbuild/micropython.js";
}
start(config){
if (1)
{
let mp_memory = 1024 * 1024; // 1Mb
let worker = 'worker1.mjs';
//needs to be a type = module rather than classic
const worker_thread = new Worker('worker1.mjs');

console.log('🔥 worker ', worker_thread)

var worker_code = {
code: ""
};
worker_thread.onmessage = (ev) => {
let data = ev.data;
console.log(typeof data, data);
worker_code.code = data;

let mp_js_startup = Module['onRuntimeInitialized'];
Module["onRuntimeInitialized"] = async function() {
mp_js_startup();
mp_js_init(mp_memory);

start(config) {
}
}

run = async function() {
await Interpreter.ready();
await eval(worker_code);
}

run()
}

//run current code if worker attr. is not used
else {
let mp_memory = 1024 * 1024; // 1Mb
if(config.mp_memory) {
mp_memory = config.mp_memory;
Expand All @@ -307,8 +341,9 @@ const main = function() {
mp_js_startup();
mp_js_init(mp_memory);
Interpreter.ready();
}
}
}
}

eval(script) {
mp_js_do_str(script.code);
Expand Down Expand Up @@ -608,7 +643,10 @@ const main = function() {
/*
Remove the splash screen, once MicroPyScript is finished starting.
*/
splashElement.parentNode.removeChild(splashElement);
if (splashElement.parentNode)
{
splashElement.parentNode.removeChild(splashElement);
}
}

function registerPlugin(plugin) {
Expand Down
2 changes: 2 additions & 0 deletions worker1.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('inside the worker 🔥');
postMessage('print(1)');