( options: QuickJsExecutorOptions, code: string, toolInvoker: SandboxToolInvoker, runPromise: RunPromise, )
| 358 | }; |
| 359 | |
| 360 | const evaluateInQuickJs = async ( |
| 361 | options: QuickJsExecutorOptions, |
| 362 | code: string, |
| 363 | toolInvoker: SandboxToolInvoker, |
| 364 | runPromise: RunPromise, |
| 365 | ): Promise<ExecuteResult> => { |
| 366 | const timeoutMs = Math.max(100, options.timeoutMs ?? DEFAULT_TIMEOUT_MS); |
| 367 | const deadlineMs = Date.now() + timeoutMs; |
| 368 | const logs: string[] = []; |
| 369 | const pendingDeferreds = new Set<QuickJSDeferredPromise>(); |
| 370 | const QuickJS = await resolveQuickJS(); |
| 371 | const runtime = QuickJS.newRuntime(); |
| 372 | |
| 373 | try { |
| 374 | runtime.setMemoryLimit(options.memoryLimitBytes ?? DEFAULT_MEMORY_LIMIT_BYTES); |
| 375 | runtime.setMaxStackSize(options.maxStackSizeBytes ?? DEFAULT_MAX_STACK_SIZE_BYTES); |
| 376 | |
| 377 | runtime.setInterruptHandler(shouldInterruptAfterDeadline(deadlineMs)); |
| 378 | |
| 379 | const context = runtime.newContext(); |
| 380 | try { |
| 381 | const logBridge = createLogBridge(context, logs); |
| 382 | context.setProp(context.global, "__executor_log", logBridge); |
| 383 | logBridge.dispose(); |
| 384 | |
| 385 | const toolBridge = createToolBridge(context, toolInvoker, pendingDeferreds, runPromise); |
| 386 | context.setProp(context.global, "__executor_invokeTool", toolBridge); |
| 387 | toolBridge.dispose(); |
| 388 | |
| 389 | const evaluated = context.evalCode(buildExecutionSource(code), EXECUTION_FILENAME); |
| 390 | if (evaluated.error) { |
| 391 | const error = context.dump(evaluated.error); |
| 392 | evaluated.error.dispose(); |
| 393 | return { |
| 394 | result: null, |
| 395 | error: normalizeExecutionError(error, deadlineMs, timeoutMs), |
| 396 | logs, |
| 397 | } satisfies ExecuteResult; |
| 398 | } |
| 399 | |
| 400 | context.setProp(context.global, "__executor_result", evaluated.value); |
| 401 | evaluated.value.dispose(); |
| 402 | |
| 403 | const stateResult = context.evalCode( |
| 404 | "(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)", |
| 405 | ); |
| 406 | if (stateResult.error) { |
| 407 | const error = context.dump(stateResult.error); |
| 408 | stateResult.error.dispose(); |
| 409 | return { |
| 410 | result: null, |
| 411 | error: normalizeExecutionError(error, deadlineMs, timeoutMs), |
| 412 | logs, |
| 413 | } satisfies ExecuteResult; |
| 414 | } |
| 415 | |
| 416 | const stateHandle = stateResult.value; |
| 417 | try { |
no test coverage detected