| 279 | try { |
| 280 | // Create a scoped environment for execution |
| 281 | const scopedExecution = () => { |
| 282 | const logMessages: string[] = []; |
| 283 | // Override console.log within this function's scope |
| 284 | const console = { |
| 285 | log: (...args: unknown[]) => { |
| 286 | logMessages.push(args.join(' ')); |
| 287 | }, |
| 288 | }; |
| 289 | |
| 290 | try { |
| 291 | // Execute the JavaScript code |
| 292 | const result = eval(javascriptCode) as unknown; |
| 293 | return { |
| 294 | result: result ? result : undefined, |
| 295 | 'console.log': logMessages, |
| 296 | }; |
| 297 | } catch (e) { |
| 298 | throw e; |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | // Execute the scoped function and capture the result |
| 303 | const executionResult = scopedExecution(); |