Files
mixly3/boards/default/python_pyodide/211.bundle.js
2024-10-24 23:09:38 +08:00

1 line
6.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";(self.webpackChunk_mixly_python_pyodide=self.webpackChunk_mixly_python_pyodide||[]).push([[211],{1838:(t,e,n)=>{n.d(e,{E:()=>o});class o extends Error{constructor(t){super(`Function ${t} not implemented!`),this.name="NotImplementedError"}}},3560:(t,e,n)=>{n.d(e,{z:()=>o.z});var o=n(6358)},6358:(t,e,n)=>{n.d(e,{z:()=>r});var o=n(9610);var s=n(1838);class i extends Event{constructor(t,e){super(t),this.detail=e}}class r{constructor(t){this._isReady=!1,this._ready=new o.D,this._stopped=Promise.resolve(),this._execution_count=0,this._pendingInput=void 0,this._listeners=new Map,this._files=new Map,this._modules=new Map,this._options=this.clone(t),this._rootPath=t.rootPath,this.safeKernel()||console.warn(`%c⚠ DANGER ⚠: kernel ${this.language()} not safe! Please consider using a safe kernel.`,"color: red; font-size: 20px;")}safeKernel(){return!1}version(){return"0.62.22"}language(){throw new s.E("language")}languageName(){throw new s.E("languageName")}moduleExts(){throw new s.E("moduleExts")}get execution_count(){return this._execution_count}evalAsync(t,e,n,o=null){throw new s.E("evalAsync")}async putFile(t,e){this._files.set(t,e)}async putModule(t,e){this._modules.set(t,e)}userModules(){return[]}getFile(t){throw new s.E("getFile")}getUserModuleFile(t){throw new s.E("getUserModuleFile")}async more(t){throw new s.E("more")}async complete(t){return[]}banner(){return`Welcome to the ${this.languageName()} REPL!`}ps1(){return">>> "}ps2(){return"... "}async _init(){}async init(){try{await this._init()}catch(t){return void this._ready.reject(t)}this.addEventListener("eval.request",this.evalFromEvent.bind(this)),await this.start()}async _start(){}async start(){await this._stopped,this._execution_count=0;try{await this._start()}catch(t){return void this._ready.reject(t)}this._files.forEach(((t,e)=>this.putFile(e,t))),this._modules.forEach(((t,e)=>this.putModule(e,t))),this._isReady=!0,this._ready.resolve()}async _stop(){}stop(){this._isReady=!1,this._ready=new o.D,this._stopped=this._stop()}restart(){this.stop(),this.start()}get isReady(){return this._isReady}async ready(){return this._ready.promise}basthonRoot(t=!1){let e=this._rootPath+"/"+this.version()+"/"+this.language();if(t&&!e.startsWith("http")){const t=window.location.origin+window.location.pathname;e=t.substring(0,t.lastIndexOf("/"))+"/"+e}return e}download(t,e){if(!("string"==typeof t||t instanceof String)){const e=new Blob([t],{type:"application/octet-stream"});t=window.URL.createObjectURL(e)}const n=document.createElement("a");n.download=e,n.href=t,n.target="_blank",n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n)}static loadScript(t){return new Promise((function(e,n){let o=document.createElement("script");o.onload=e,o.onerror=n,o.src=t,document.head.appendChild(o)}))}dispatchEvent(t,e){document.dispatchEvent(new i(`basthon.${t}`,e))}addEventListener(t,e){const n=t=>e(t.detail);document.addEventListener(`basthon.${t}`,n);let o=this._listeners.get(t);null==o&&(o=new Map,this._listeners.set(t,o)),o.set(e,n)}removeEventListener(t,e){const n=this._listeners.get(t);document.removeEventListener(`basthon.${t}`,n?.get(e))}async inputAsync(t,e=!1,n=void 0){(n=this.clone(n)).content={prompt:t,password:e};const s=new o.D;n.resolve=s.resolve.bind(s),n.reject=s.reject.bind(s),this._pendingInput=s,this.dispatchEvent("eval.input",n);const i=await s.promise;return this._pendingInput=void 0,i}resolvePendingInput(){this._pendingInput?.resolve("")}clone(t){return JSON.parse(JSON.stringify(t))}async putRessource(t,e){const n=t.split(".").pop()??"";return this.moduleExts().includes(n)?await this.putModule(t,e):await this.putFile(t,e)}pendingInput(){return null!=this._pendingInput}async evalFromEvent(t){const e=e=>n=>{let o=this.clone(t);o.stream=e,o.content=n,this.dispatchEvent("eval.output",o)},n=e("stdout"),o=e("stderr");let s;try{s=await this.evalAsync(t.code,n,o,t)}catch(e){o(e.toString());const n=this.clone(t);return n.error=e,n.execution_count=this.execution_count,void this.dispatchEvent("eval.error",n)}if(null==s)return;const i=s[0],r=s[1];let a=this.clone(t);a.execution_count=r,null!=i&&(a.result=i),this.dispatchEvent("eval.finished",a)}}},211:(t,e,n)=>{n.d(e,{Python3Kernel:()=>s});var o=n(3560);class s extends o.z{constructor(t){super(t),this._pyodideURLs=["https://cdn.jsdelivr.net/pyodide/v{PYODIDE_VERSION}/full/pyodide.js"],this.__kernel__=null,this.pythonVersion="",this._pyodideURLs.unshift(`${this.basthonRoot()}/pyodide/pyodide.js`),this._pyodideURLs=t?.pyodideURLs??this._pyodideURLs}basthonModulesRoot(t=!1){return this.basthonRoot(t)+"/modules"}language(){return"python3"}languageName(){return"Python 3"}moduleExts(){return["py"]}async _onload(){const t=window.pyodide,e=t._api.repodata_packages;for(let t of Object.keys(e)){const n=e[t];n.file_name=n.file_name.replace("{basthonRoot}",this.basthonRoot(!0))}this.pythonVersion=t.runPython("import platform ; platform.python_version()"),await t.loadPackage("basthon"),this.__kernel__=t.pyimport("basthon").__kernel__}async _init(){await super._init();let t=this._pyodideURLs[0];for(let e of this._pyodideURLs){e=e.replace("{PYODIDE_VERSION}","0.23.2");try{if((await fetch(e,{method:"HEAD"})).ok){t=e;break}}catch(t){}}try{await s.loadScript(t)}catch(t){throw console.log(t),console.error("Can't load pyodide.js"),t}if(null==window.loadPyodide)throw console.log("window.loadPyodide is null!"),new Error("Can't load pyodide.js");try{window.pyodide=await window.loadPyodide({lockFileURL:this.basthonRoot()+"/repodata.json"})}catch(t){throw console.log(t),console.error("Can't load Python from Pyodide"),t}await this._onload()}get execution_count(){return this.__kernel__.execution_count()}async evalAsync(t,e,n,o=null){void 0===e&&(e=function(t){console.log(t)}),void 0===n&&(n=function(t){console.error(t)});const s=await this.__kernel__.eval(t,e,n,o);if(!(s instanceof window.pyodide.ffi.PyProxy))throw s;const i=s.toJs({create_proxies:!1,dict_converter:Object.fromEntries});return s.destroy(),i}async _start(){await super._start(),this.__kernel__.start()}async _stop(){this.__kernel__.stop(),await super._stop()}async putFile(t,e){this.__kernel__.put_file(t,e)}async putModule(t,e){return await this.__kernel__.put_module(t,e)}userModules(){const t=this.__kernel__.user_modules();if(!(t instanceof window.pyodide.ffi.PyProxy))return t;const e=t.toJs();return t.destroy(),e}getFile(t){return this.__kernel__.get_file(t).toJs()}getUserModuleFile(t){return this.__kernel__.get_user_module_file(t).toJs()}async more(t){return this.__kernel__.more(t)}banner(){return'Python 3.11.2 (main, May 3 2023 04:00:05) on WebAssembly/Emscripten\nType "help", "copyright", "credits" or "license" for more information.'}async complete(t){const e=this.__kernel__.complete(t);if(!(e instanceof window.pyodide.ffi.PyProxy))return e;const n=e.toJs();return e.destroy(),n}chdir(t){window.pyodide._module.FS.chdir(t)}mkdir(t){window.pyodide._module.FS.mkdir(t)}}}}]);