#!/usr/bin/env node
import{a as _,b as O}from"./chunk-3KAVXQP4.js";import"./chunk-4OALHZXB.js";import"./chunk-D64U2VGQ.js";import"./chunk-LIYVQA3X.js";import{a as v,b as T}from"./chunk-OOJCYVYF.js";import{a as D}from"./chunk-4PRVMER6.js";import{c as y}from"./chunk-MO4RPBN2.js";import{b as C}from"./chunk-YU6OGPZR.js";import{a as M,b as c}from"./chunk-RLNOQILG.js";import{a as E}from"./chunk-4VDEBYW7.js";import{a as b,b as P}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";import{randomBytes as $}from"node:crypto";import{fileURLToPath as Q}from"node:url";import{Worker as R}from"node:worker_threads";var U=1e4,H=6e4,I={name:"python3",summary:"Execute Python code via CPython Emscripten",usage:"python3 [OPTIONS] [-c CODE | -m MODULE | FILE] [ARGS...]",description:["Execute Python code using CPython compiled to WebAssembly via Emscripten.","","This command runs Python in an isolated environment with access to","the virtual filesystem. Standard library modules are available."],options:["-c CODE     Execute CODE as Python script","-m MODULE   Run library module as a script","--version   Show Python version","--help      Show this help"],examples:['python3 -c "print(1 + 2)"','python3 -c "import sys; print(sys.version)"',"python3 script.py","python3 script.py arg1 arg2",`echo 'print("hello")' | python3`],notes:["CPython runs in WebAssembly, so execution may be slower than native Python.","Standard library modules are available (no pip install).","Maximum execution time is 30 seconds by default."]};function L(r){let e={code:null,module:null,scriptFile:null,showVersion:!1,scriptArgs:[]};if(r.length===0)return e;let t=r.findIndex(o=>!o.startsWith("-")||o==="-"||o==="--");for(let o=0;o<(t===-1?r.length:t);o++){let s=r[o];if(s==="-c")return o+1>=r.length?{stdout:"",stderr:`python3: option requires an argument -- 'c'
`,exitCode:2}:(e.code=r[o+1],e.scriptArgs=r.slice(o+2),e);if(s==="-m")return o+1>=r.length?{stdout:"",stderr:`python3: option requires an argument -- 'm'
`,exitCode:2}:(e.module=r[o+1],e.scriptArgs=r.slice(o+2),e);if(s==="--version"||s==="-V")return e.showVersion=!0,e;if(s.startsWith("-")&&s!=="-")return{stdout:"",stderr:`python3: unrecognized option '${s}'
`,exitCode:2}}if(t!==-1){let o=r[t];o==="--"?t+1<r.length&&(e.scriptFile=r[t+1],e.scriptArgs=r.slice(t+2)):(e.scriptFile=o,e.scriptArgs=r.slice(t+1))}return e}var k=new WeakMap;function z(r){let e=k.get(r);return e||(e={executionQueue:[],isExecuting:!1},k.set(r,e)),e}function ie(){k=new WeakMap}var N=Q(new URL("./worker.js",import.meta.url));function B(){return $(16).toString("hex")}function V(r,e){if(!r||typeof r!="object")return{success:!1,error:"Malformed worker response"};let t=r;return typeof t.protocolToken!="string"||t.protocolToken!==e?{success:!1,error:"Malformed worker response: invalid protocol token"}:t.type==="security-violation"?{success:!1,error:`Security violation: ${typeof t.violation?.type=="string"?t.violation.type:"unknown"}`}:typeof t.success!="boolean"?{success:!1,error:"Malformed worker response: missing success flag"}:t.success?{success:!0}:{success:!1,error:typeof t.error=="string"&&t.error.length>0?t.error:"Worker execution failed"}}function a(r){if(r.isExecuting||r.executionQueue.length===0)return;for(;r.executionQueue.length>0&&r.executionQueue[0].canceled;)r.executionQueue.shift();if(r.executionQueue.length===0)return;let e=r.executionQueue.shift();if(!e)return;r.isExecuting=!0;let t;try{t=C.runTrusted(()=>new R(N,{workerData:e.input}))}catch(n){let i=n instanceof Error?n.message:String(n);e.resolve({success:!1,error:c(i)}),r.isExecuting=!1,a(r);return}e.workerRef&&(e.workerRef.current=t);let o=y(e.requireDefenseContext,"python3","worker message callback",n=>{e.resolve(V(n,e.input.protocolToken)),r.isExecuting=!1,t.terminate(),a(r)}),s=y(e.requireDefenseContext,"python3","worker error callback",n=>{let i=c(E(n));e.resolve({success:!1,error:i}),r.isExecuting=!1,a(r)}),p=y(e.requireDefenseContext,"python3","worker exit callback",()=>{r.isExecuting&&(e.resolve({success:!1,error:"Worker exited unexpectedly"}),r.isExecuting=!1,a(r))}),f=n=>{try{o(n)}catch(i){let m=i instanceof Error?i.message:String(i);e.resolve({success:!1,error:c(m)}),r.isExecuting=!1,t.terminate(),a(r)}},l=n=>{try{s(n)}catch(i){let m=i instanceof Error?i.message:String(i);e.resolve({success:!1,error:c(m)}),r.isExecuting=!1,a(r)}},g=()=>{try{p()}catch(n){let i=n instanceof Error?n.message:String(n);e.resolve({success:!1,error:c(i)}),r.isExecuting=!1,a(r)}};t.on("message",f),t.on("error",l),t.on("exit",g)}async function j(r,e,t,o=[]){let s=_(),p=new O(s,e.fs,e.cwd,"python3",e.fetch,e.limits?.maxOutputSize??0),f=e.limits?.maxPythonTimeoutMs??U,l=e.fetch?Math.max(f,H):f,g=z(e.fs),n={protocolToken:B(),sharedBuffer:s,pythonCode:r,cwd:e.cwd,env:D(e.env),args:o,scriptPath:t,timeoutMs:l},i={current:null},m=new Promise(u=>{let d={input:n,resolve:()=>{},workerRef:i,requireDefenseContext:e.requireDefenseContext},A=y(e.requireDefenseContext,"python3","worker timeout callback",()=>{i.current?i.current.terminate():d.canceled=!0,u({success:!1,error:`Execution timeout: exceeded ${l}ms limit`})}),F=v(()=>{try{A()}catch(h){let W=h instanceof Error?h.message:String(h);u({success:!1,error:c(W)})}},l);d.resolve=h=>{T(F),u(h)},g.executionQueue.push(d),a(g)}),[x,w]=await Promise.all([p.run(l).catch(u=>({stdout:"",stderr:`python3: bridge error: ${c(E(u))}
`,exitCode:1})),m.catch(u=>({success:!1,error:c(E(u))}))]);if(!w.success&&w.error){let u=c(w.error);return{stdout:x.stdout,stderr:`${x.stderr}python3: ${u}
`,exitCode:x.exitCode||1}}return x}var Y={name:"python3",async execute(r,e){if(P(r))return b(I);let t=L(r);if("exitCode"in t)return t;if(t.showVersion)return{stdout:`Python 3.13.2 (Emscripten)
`,stderr:"",exitCode:0};let o,s;if(t.code!==null)o=t.code,s="-c";else if(t.module!==null){if(!/^[a-zA-Z_][a-zA-Z0-9_.]*$/.test(t.module))return{stdout:"",stderr:`python3: No module named '${t.module.slice(0,200)}'
`,exitCode:1};o=`import runpy; runpy.run_module('${t.module}', run_name='__main__')`,s=t.module}else if(t.scriptFile!==null){let p=e.fs.resolvePath(e.cwd,t.scriptFile);if(!await e.fs.exists(p))return{stdout:"",stderr:`python3: can't open file '${t.scriptFile}': [Errno 2] No such file or directory
`,exitCode:2};try{o=await e.fs.readFile(p),s=t.scriptFile}catch(f){let l=M(f.message);return{stdout:"",stderr:`python3: can't open file '${t.scriptFile}': ${l}
`,exitCode:2}}}else if(e.stdin.trim())o=e.stdin,s="<stdin>";else return{stdout:"",stderr:`python3: no input provided (use -c CODE, -m MODULE, or provide a script file)
`,exitCode:2};return j(o,e,s,t.scriptArgs)}},ce={name:"python",async execute(r,e){return Y.execute(r,e)}};export{ie as _resetExecutionQueue,Y as python3Command,ce as pythonCommand};
