MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / evaluateInQuickJs

Function evaluateInQuickJs

packages/kernel/runtime-quickjs/src/index.ts:360–465  ·  view source on GitHub ↗
(
  options: QuickJsExecutorOptions,
  code: string,
  toolInvoker: SandboxToolInvoker,
  runPromise: RunPromise,
)

Source from the content-addressed store, hash-verified

358};
359
360const 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 {

Callers 1

runInQuickJsFunction · 0.85

Calls 11

resolveQuickJSFunction · 0.85
createLogBridgeFunction · 0.85
createToolBridgeFunction · 0.85
buildExecutionSourceFunction · 0.85
normalizeExecutionErrorFunction · 0.85
drainAsyncFunction · 0.85
readResultStateFunction · 0.85
timeoutMessageFunction · 0.85
readOutputItemsFunction · 0.85
disposeMethod · 0.80
clearMethod · 0.80

Tested by

no test coverage detected