synclink integration (#1258)

synclink integration + fixes for `py-repl` related tests and `display` tests
This commit is contained in:
Madhur Tandon
2023-03-27 20:56:31 +05:30
committed by GitHub
parent 88f0738500
commit c8f9f16791
35 changed files with 555 additions and 230 deletions

View File

@@ -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;
}
}