mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-21 19:25:35 -05:00
synclink integration (#1258)
synclink integration + fixes for `py-repl` related tests and `display` tests
This commit is contained in:
@@ -3,30 +3,32 @@ import { getLogger } from './logger';
|
||||
import { Stdio } from './stdio';
|
||||
import { InstallError, ErrorCode } from './exceptions';
|
||||
import { robustFetch } from './fetch';
|
||||
import type { loadPyodide as loadPyodideDeclaration, PyodideInterface, PyProxy } from 'pyodide';
|
||||
import type { loadPyodide as loadPyodideDeclaration, PyodideInterface, PyProxy, PyProxyDict } from 'pyodide';
|
||||
import type { ProxyMarked } from 'synclink';
|
||||
import * as Synclink from 'synclink';
|
||||
|
||||
declare const loadPyodide: typeof loadPyodideDeclaration;
|
||||
const logger = getLogger('pyscript/pyodide');
|
||||
|
||||
export type InterpreterInterface = PyodideInterface | null;
|
||||
export type InterpreterInterface = (PyodideInterface & ProxyMarked) | null;
|
||||
|
||||
interface Micropip extends PyProxy {
|
||||
install(packageName: string | string[]): Promise<void>;
|
||||
}
|
||||
|
||||
type FSInterface = {
|
||||
writeFile(path: string, data: Uint8Array | string, options?: { canOwn: boolean }): void;
|
||||
writeFile(path: string, data: Uint8Array | string, options?: { canOwn?: boolean; encoding?: string }): void;
|
||||
mkdirTree(path: string): void;
|
||||
mkdir(path: string): void;
|
||||
};
|
||||
} & ProxyMarked;
|
||||
|
||||
type PATHFSInterface = {
|
||||
resolve(path: string): string;
|
||||
};
|
||||
} & ProxyMarked;
|
||||
|
||||
type PATHInterface = {
|
||||
dirname(path: string): string;
|
||||
};
|
||||
} & ProxyMarked;
|
||||
|
||||
/*
|
||||
RemoteInterpreter class is responsible to process requests from the
|
||||
@@ -50,9 +52,9 @@ export class RemoteInterpreter extends Object {
|
||||
PATH: PATHInterface;
|
||||
PATH_FS: PATHFSInterface;
|
||||
|
||||
globals: PyProxy;
|
||||
globals: PyProxyDict & ProxyMarked;
|
||||
// TODO: Remove this once `runtimes` is removed!
|
||||
interpreter: InterpreterInterface;
|
||||
interpreter: InterpreterInterface & ProxyMarked;
|
||||
|
||||
constructor(src = 'https://cdn.jsdelivr.net/pyodide/v0.22.1/full/pyodide.js') {
|
||||
super();
|
||||
@@ -82,15 +84,18 @@ export class RemoteInterpreter extends Object {
|
||||
* path.
|
||||
*/
|
||||
async loadInterpreter(config: AppConfig, stdio: Stdio): Promise<void> {
|
||||
this.interface = await loadPyodide({
|
||||
stdout: (msg: string) => {
|
||||
stdio.stdout_writeline(msg);
|
||||
},
|
||||
stderr: (msg: string) => {
|
||||
stdio.stderr_writeline(msg);
|
||||
},
|
||||
fullStdLib: false,
|
||||
});
|
||||
this.interface = Synclink.proxy(
|
||||
await loadPyodide({
|
||||
stdout: (msg: string) => {
|
||||
// TODO: add syncify when moved to worker
|
||||
stdio.stdout_writeline(msg);
|
||||
},
|
||||
stderr: (msg: string) => {
|
||||
stdio.stderr_writeline(msg);
|
||||
},
|
||||
fullStdLib: false,
|
||||
}),
|
||||
);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
this.FS = this.interface.FS;
|
||||
// eslint-disable-next-line
|
||||
@@ -101,7 +106,7 @@ export class RemoteInterpreter extends Object {
|
||||
// TODO: Remove this once `runtimes` is removed!
|
||||
this.interpreter = this.interface;
|
||||
|
||||
this.globals = this.interface.globals;
|
||||
this.globals = Synclink.proxy(this.interface.globals as PyProxyDict);
|
||||
|
||||
if (config.packages) {
|
||||
logger.info('Found packages in configuration to install. Loading micropip...');
|
||||
@@ -262,4 +267,23 @@ export class RemoteInterpreter extends Object {
|
||||
const importlib = this.interface.pyimport('importlib') as PyProxy & { invalidate_caches(): void };
|
||||
importlib.invalidate_caches();
|
||||
}
|
||||
|
||||
pyimport(mod_name: string): PyProxy & Synclink.ProxyMarked {
|
||||
return Synclink.proxy(this.interface.pyimport(mod_name));
|
||||
}
|
||||
|
||||
mkdirTree(path: string) {
|
||||
this.FS.mkdirTree(path);
|
||||
}
|
||||
|
||||
writeFile(path: string, content: string) {
|
||||
this.FS.writeFile(path, content, { encoding: 'utf8' });
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
setHandler(func_name: string, handler: any): void {
|
||||
const pyscript_module = this.interface.pyimport('pyscript');
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
pyscript_module[func_name] = handler;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user