11/// <reference lib="webworker" />
22
3+ import { expose } from "comlink" ;
34import type { ReplOutput } from "../repl" ;
4- import type { MessageType , WorkerRequest , WorkerResponse } from "./runtime" ;
5+ import type { WorkerCapabilities } from "./runtime" ;
56import inspect from "object-inspect" ;
67
78function format ( ...args : unknown [ ] ) : string {
@@ -29,12 +30,12 @@ self.console = {
2930 } ,
3031} ;
3132
32- async function init ( { id } : WorkerRequest [ "init" ] ) {
33+ async function init ( /*_interruptBuffer?: Uint8Array*/ ) : Promise < {
34+ capabilities : WorkerCapabilities ;
35+ } > {
3336 // Initialize the worker and report capabilities
34- self . postMessage ( {
35- id,
36- payload : { capabilities : { interrupt : "restart" } } ,
37- } satisfies WorkerResponse [ "init" ] ) ;
37+ // interruptBuffer is not used for JavaScript (restart-based interruption)
38+ return { capabilities : { interrupt : "restart" } } ;
3839}
3940
4041async function replLikeEval ( code : string ) : Promise < unknown > {
@@ -72,8 +73,10 @@ async function replLikeEval(code: string): Promise<unknown> {
7273 }
7374}
7475
75- async function runCode ( { id, payload } : WorkerRequest [ "runCode" ] ) {
76- const { code } = payload ;
76+ async function runCode ( code : string ) : Promise < {
77+ output : ReplOutput [ ] ;
78+ updatedFiles : Record < string , string > ;
79+ } > {
7780 try {
7881 const result = await replLikeEval ( code ) ;
7982 jsOutput . push ( {
@@ -99,14 +102,13 @@ async function runCode({ id, payload }: WorkerRequest["runCode"]) {
99102 const output = [ ...jsOutput ] ;
100103 jsOutput = [ ] ; // Clear output
101104
102- self . postMessage ( {
103- id,
104- payload : { output, updatedFiles : [ ] } ,
105- } satisfies WorkerResponse [ "runCode" ] ) ;
105+ return { output, updatedFiles : { } as Record < string , string > } ;
106106}
107107
108- function runFile ( { id, payload } : WorkerRequest [ "runFile" ] ) {
109- const { name, files } = payload ;
108+ function runFile (
109+ name : string ,
110+ files : Record < string , string >
111+ ) : { output : ReplOutput [ ] ; updatedFiles : Record < string , string > } {
110112 // pyodide worker などと異なり、複数ファイルを読み込んでimportのようなことをするのには対応していません。
111113 try {
112114 self . eval ( files [ name ] ) ;
@@ -129,23 +131,17 @@ function runFile({ id, payload }: WorkerRequest["runFile"]) {
129131 const output = [ ...jsOutput ] ;
130132 jsOutput = [ ] ; // Clear output
131133
132- self . postMessage ( {
133- id,
134- payload : { output, updatedFiles : [ ] } ,
135- } satisfies WorkerResponse [ "runFile" ] ) ;
134+ return { output, updatedFiles : { } as Record < string , string > } ;
136135}
137136
138- async function checkSyntax ( { id , payload } : WorkerRequest [ "checkSyntax" ] ) {
139- const { code } = payload ;
140-
137+ async function checkSyntax (
138+ code : string
139+ ) : Promise < { status : "complete" | "incomplete" | "invalid" } > {
141140 try {
142141 // Try to create a Function to check syntax
143142 // new Function(code); // <- not working
144143 self . eval ( `() => {${ code } }` ) ;
145- self . postMessage ( {
146- id,
147- payload : { status : "complete" } ,
148- } satisfies WorkerResponse [ "checkSyntax" ] ) ;
144+ return { status : "complete" } ;
149145 } catch ( e ) {
150146 // Check if it's a syntax error or if more input is expected
151147 if ( e instanceof SyntaxError ) {
@@ -154,28 +150,18 @@ async function checkSyntax({ id, payload }: WorkerRequest["checkSyntax"]) {
154150 e . message . includes ( "Unexpected token '}'" ) ||
155151 e . message . includes ( "Unexpected end of input" )
156152 ) {
157- self . postMessage ( {
158- id,
159- payload : { status : "incomplete" } ,
160- } satisfies WorkerResponse [ "checkSyntax" ] ) ;
153+ return { status : "incomplete" } ;
161154 } else {
162- self . postMessage ( {
163- id,
164- payload : { status : "invalid" } ,
165- } satisfies WorkerResponse [ "checkSyntax" ] ) ;
155+ return { status : "invalid" } ;
166156 }
167157 } else {
168- self . postMessage ( {
169- id,
170- payload : { status : "invalid" } ,
171- } satisfies WorkerResponse [ "checkSyntax" ] ) ;
158+ return { status : "invalid" } ;
172159 }
173160 }
174161}
175162
176- async function restoreState ( { id , payload } : WorkerRequest [ "restoreState" ] ) {
163+ async function restoreState ( commands : string [ ] ) : Promise < object > {
177164 // Re-execute all previously successful commands to restore state
178- const { commands } = payload ;
179165 jsOutput = [ ] ; // Clear output for restoration
180166
181167 for ( const command of commands ) {
@@ -188,32 +174,15 @@ async function restoreState({ id, payload }: WorkerRequest["restoreState"]) {
188174 }
189175
190176 jsOutput = [ ] ; // Clear any output from restoration
191- self . postMessage ( {
192- id,
193- payload : { } ,
194- } satisfies WorkerResponse [ "restoreState" ] ) ;
177+ return { } ;
195178}
196179
197- self . onmessage = async ( event : MessageEvent < WorkerRequest [ MessageType ] > ) => {
198- switch ( event . data . type ) {
199- case "init" :
200- await init ( event . data ) ;
201- return ;
202- case "runCode" :
203- await runCode ( event . data ) ;
204- return ;
205- case "runFile" :
206- runFile ( event . data ) ;
207- return ;
208- case "checkSyntax" :
209- await checkSyntax ( event . data ) ;
210- return ;
211- case "restoreState" :
212- await restoreState ( event . data ) ;
213- return ;
214- default :
215- event . data satisfies never ;
216- originalConsole . error ( `Unknown message: ${ event . data } ` ) ;
217- return ;
218- }
180+ const api = {
181+ init,
182+ runCode,
183+ runFile,
184+ checkSyntax,
185+ restoreState,
219186} ;
187+
188+ expose ( api ) ;
0 commit comments