( options: QuickJsExecutorOptions, code: string, toolInvoker: SandboxToolInvoker, runPromise: RunPromise, )
| 368 | }; |
| 369 | |
| 370 | const evaluateInQuickJs = async ( |
| 371 | options: QuickJsExecutorOptions, |
| 372 | code: string, |
| 373 | toolInvoker: SandboxToolInvoker, |
| 374 | runPromise: RunPromise, |
| 375 | ): Promise<ExecuteResult> => { |
| 376 | const timeoutMs = Math.max(100, options.timeoutMs ?? DEFAULT_TIMEOUT_MS); |
| 377 | const deadlineMs = Date.now() + timeoutMs; |
| 378 | const logs: string[] = []; |
| 379 | const pendingDeferreds = new Set<QuickJSDeferredPromise>(); |
| 380 | const QuickJS = await resolveQuickJS(); |
| 381 | const runtime = QuickJS.newRuntime(); |
| 382 | |
| 383 | try { |
| 384 | runtime.setMemoryLimit(options.memoryLimitBytes ?? DEFAULT_MEMORY_LIMIT_BYTES); |
| 385 | runtime.setMaxStackSize(options.maxStackSizeBytes ?? DEFAULT_MAX_STACK_SIZE_BYTES); |
| 386 | |
| 387 | runtime.setInterruptHandler(shouldInterruptAfterDeadline(deadlineMs)); |
| 388 | |
| 389 | const context = runtime.newContext(); |
| 390 | try { |
| 391 | const logBridge = createLogBridge(context, logs); |
| 392 | context.setProp(context.global, "__executor_log", logBridge); |
| 393 | logBridge.dispose(); |
| 394 | |
| 395 | const toolBridge = createToolBridge(context, toolInvoker, pendingDeferreds, runPromise); |
| 396 | context.setProp(context.global, "__executor_invokeTool", toolBridge); |
| 397 | toolBridge.dispose(); |
| 398 | |
| 399 | const evaluated = context.evalCode(buildExecutionSource(code), EXECUTION_FILENAME); |
| 400 | if (evaluated.error) { |
| 401 | const error = context.dump(evaluated.error); |
| 402 | evaluated.error.dispose(); |
| 403 | return { |
| 404 | result: null, |
| 405 | error: normalizeExecutionError(error, deadlineMs, timeoutMs), |
| 406 | logs, |
| 407 | } satisfies ExecuteResult; |
| 408 | } |
| 409 | |
| 410 | context.setProp(context.global, "__executor_result", evaluated.value); |
| 411 | evaluated.value.dispose(); |
| 412 | |
| 413 | const stateResult = context.evalCode( |
| 414 | "(function(p){ var s = { v: void 0, e: void 0, settled: false }; var formatError = function(e){ if (e && typeof e === 'object') { var message = typeof e.message === 'string' ? e.message : ''; var stack = typeof e.stack === 'string' ? e.stack : ''; if (message && stack) { return stack.indexOf(message) === -1 ? message + '\\n' + stack : stack; } if (message) return message; if (stack) return stack; } return String(e); }; p.then(function(v){ s.v = v; s.settled = true; }, function(e){ s.e = formatError(e); s.settled = true; }); return s; })(__executor_result)", |
| 415 | ); |
| 416 | if (stateResult.error) { |
| 417 | const error = context.dump(stateResult.error); |
| 418 | stateResult.error.dispose(); |
| 419 | return { |
| 420 | result: null, |
| 421 | error: normalizeExecutionError(error, deadlineMs, timeoutMs), |
| 422 | logs, |
| 423 | } satisfies ExecuteResult; |
| 424 | } |
| 425 | |
| 426 | const stateHandle = stateResult.value; |
| 427 | try { |
no test coverage detected