Files
mixly3/boards/default/python_pyodide/211.bundle.js
2024-09-08 13:32:13 +08:00

1 line
7.2 KiB
JavaScript

"use strict";(self.webpackChunk_mixly_python_pyodide=self.webpackChunk_mixly_python_pyodide||[]).push([[211],{6998:(e,t,n)=>{n.d(t,{z:()=>a});var o=n(9610);const s="filesystem:/";!function(){var e;const t=async e=>await new Promise(((t,n)=>{const o=new FileReader;o.onloadend=()=>{t(o.result)},o.onerror=n,o.readAsDataURL(new Blob([e]))}));window._fetch=null!==(e=window._fetch)&&void 0!==e?e:window.fetch,window.fetch=async function(e,n){var o;const i=null!==(o=e.url)&&void 0!==o?o:e.toString(),r=s;if(i.startsWith(r)){const e=i.slice(12);let n=null;const o={status:200,statusText:"OK",headers:new Headers};try{n=window.Basthon.getFile(e);const s=await t(n),i=s.substring(s.indexOf(":")+1,s.indexOf(";"));n=n.buffer,o.headers.append("Content-Type",i),o.headers.append("Content-Length",n.byteLength)}catch(e){o.status=404,o.statusText="Not Found"}return new Response(n,o)}return await window._fetch(e,n)};const n=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,"src");Object.defineProperty(HTMLImageElement.prototype,"src",{...n,get:function(){var e;return null===(e=null==n?void 0:n.get)||void 0===e?void 0:e.call(this)},set:function(e){var o;const i=s;if(e.startsWith(i)){const o=e.slice(12),s=window.Basthon.getFile(o).buffer;(async()=>{var e;null===(e=null==n?void 0:n.set)||void 0===e||e.call(this,await t(s))})()}else null===(o=null==n?void 0:n.set)||void 0===o||o.call(this,e)}})}();class i extends Error{constructor(e){super(`Function ${e} not implemented!`),this.name="NotImplementedError"}}class r extends Event{constructor(e,t){super(e),this.detail=t}}class a{constructor(e){this._ready=!1,this._loaded=new o.D,this._execution_count=0,this._pendingInput=!1,this._listeners=new Map,this._rootPath=e.rootPath}version(){return"0.62.21"}language(){throw new i("language")}languageName(){throw new i("languageName")}moduleExts(){throw new i("moduleExts")}async launch(){throw new i("launch")}get execution_count(){return this._execution_count}evalAsync(e,t,n,o=null){throw new i("evalAsync")}restart(){throw new i("restart")}async putFile(e,t){throw new i("putFile")}async putModule(e,t){throw new i("putModule")}userModules(){return[]}getFile(e){throw new i("getFile")}getUserModuleFile(e){throw new i("getUserModuleFile")}more(e){throw new i("more")}complete(e){return[]}banner(){return`Welcome to the ${this.languageName()} REPL!`}ps1(){return">>> "}ps2(){return"... "}async init(){try{await this.launch()}catch(e){return void this._loaded.reject(e)}this.addEventListener("eval.request",this.evalFromEvent.bind(this)),this._ready=!0,this._loaded.resolve()}get ready(){return this._ready}async loaded(){await this._loaded.promise}basthonRoot(e=!1){let t=this._rootPath+"/"+this.version()+"/"+this.language();if(e&&!t.startsWith("http")){const e=window.location.origin+window.location.pathname;t=e.substring(0,e.lastIndexOf("/"))+"/"+t}return t}download(e,t){if(!("string"==typeof e||e instanceof String)){const t=new Blob([e],{type:"application/octet-stream"});e=window.URL.createObjectURL(t)}const n=document.createElement("a");n.download=t,n.href=e,n.target="_blank",n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n)}static loadScript(e){return new Promise((function(t,n){let o=document.createElement("script");o.onload=t,o.onerror=n,o.src=e,document.head.appendChild(o)}))}dispatchEvent(e,t){document.dispatchEvent(new r(`basthon.${e}`,t))}addEventListener(e,t){const n=e=>t(e.detail);document.addEventListener(`basthon.${e}`,n);let o=this._listeners.get(e);null==o&&(o=new Map,this._listeners.set(e,o)),o.set(t,n)}removeEventListener(e,t){const n=this._listeners.get(e);document.removeEventListener(`basthon.${e}`,null==n?void 0:n.get(t))}async inputAsync(e,t=!1,n=void 0){(n=this.clone(n)).content={prompt:e,password:t};const o=new Promise((function(e,t){n.resolve=e,n.reject=t}));this._pendingInput=!0,this.dispatchEvent("eval.input",n);const s=await o;return this._pendingInput=!1,s}clone(e){return JSON.parse(JSON.stringify(e))}async putRessource(e,t){var n;const o=null!==(n=e.split(".").pop())&&void 0!==n?n:"";return this.moduleExts().includes(o)?await this.putModule(e,t):await this.putFile(e,t)}pendingInput(){return this._pendingInput}async evalFromEvent(e){const t=t=>n=>{let o=this.clone(e);o.stream=t,o.content=n,this.dispatchEvent("eval.output",o)},n=t("stdout"),o=t("stderr");let s;try{s=await this.evalAsync(e.code,n,o,e)}catch(t){o(t.toString());const n=this.clone(e);return n.error=t,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(e);a.execution_count=r,null!=i&&(a.result=i),this.dispatchEvent("eval.finished",a)}}},211:(e,t,n)=>{n.d(t,{KernelPython3:()=>s});var o=n(6998);class s extends o.z{constructor(e){var t;super(e),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=null!==(t=null==e?void 0:e.pyodideURLs)&&void 0!==t?t:this._pyodideURLs}basthonModulesRoot(e=!1){return this.basthonRoot(e)+"/modules"}language(){return"python3"}languageName(){return"Python 3"}moduleExts(){return["py"]}async _onload(){const e=window.pyodide,t=e._api.repodata_packages;for(let e of Object.keys(t)){const n=t[e];n.file_name=n.file_name.replace("{basthonRoot}",this.basthonRoot(!0))}this.pythonVersion=e.runPython("import platform ; platform.python_version()"),await e.loadPackage("basthon"),this.__kernel__=e.pyimport("basthon").__kernel__}async launch(){let e=this._pyodideURLs[0];for(let t of this._pyodideURLs){t=t.replace("{PYODIDE_VERSION}","0.23.2");try{if((await fetch(t,{method:"HEAD"})).ok){e=t;break}}catch(e){}}try{await s.loadScript(e)}catch(e){throw console.log(e),console.error("Can't load pyodide.js"),e}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(e){throw console.log(e),console.error("Can't load Python from Pyodide"),e}await this._onload()}get execution_count(){return this.__kernel__.execution_count()}async evalAsync(e,t,n,o=null){void 0===t&&(t=function(e){console.log(e)}),void 0===n&&(n=function(e){console.error(e)});const s=await this.__kernel__.eval(e,t,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}restart(){return this.__kernel__.restart()}async putFile(e,t){this.__kernel__.put_file(e,t)}async putModule(e,t){return await this.__kernel__.put_module(e,t)}userModules(){const e=this.__kernel__.user_modules();if(!(e instanceof window.pyodide.ffi.PyProxy))return e;const t=e.toJs();return e.destroy(),t}getFile(e){return this.__kernel__.get_file(e).toJs()}getUserModuleFile(e){return this.__kernel__.get_user_module_file(e).toJs()}more(e){return this.__kernel__.more(e)}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.'}complete(e){const t=this.__kernel__.complete(e);if(!(t instanceof window.pyodide.ffi.PyProxy))return t;const n=t.toJs();return t.destroy(),n}chdir(e){window.pyodide._module.FS.chdir(e)}mkdir(e){window.pyodide._module.FS.mkdir(e)}}}}]);