Created
January 5, 2026 07:43
-
-
Save hugefiver/0651f65707869ef6553590e01a9d6f4f to your computer and use it in GitHub Desktop.
antigravity-code-executor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Antigravity Code Executor - VS Code Extension | |
| * | |
| * This is a *reference rewrite* for comprehension and further refactoring. | |
| * The original runtime depends on VS Code extension APIs (vscode) and a custom | |
| * module bundler structure (webpack-style modules table). | |
| * | |
| * Key behaviors: | |
| * - Registers command `antigravity-code-executor.executeCode` | |
| * - Executes user-provided JS code in a Node.js vm sandbox | |
| * - Exposes whitelisted modules (fs, path, child_process, etc.) to sandbox | |
| * - Returns execution logs as a newline-joined string | |
| * | |
| * Security note: | |
| * The sandbox spreads `globalThis`, exposing `process` and other globals. | |
| * Combined with `child_process` and `fs`, this is NOT a secure sandbox. | |
| */ | |
| import * as vscode from "vscode"; | |
| import * as vm from "vm"; | |
| import * as fs from "fs"; | |
| import * as path from "path"; | |
| import * as util from "util"; | |
| import * as child_process from "child_process"; | |
| import * as os from "os"; | |
| import * as http from "http"; | |
| import * as https from "https"; | |
| import * as dns from "dns"; | |
| import * as querystring from "querystring"; | |
| import * as events from "events"; | |
| type AllowedModules = { | |
| fs: typeof fs; | |
| path: typeof path; | |
| util: typeof util; | |
| child_process: typeof child_process; | |
| os: typeof os; | |
| http: typeof http; | |
| https: typeof https; | |
| dns: typeof dns; | |
| querystring: typeof querystring; | |
| events: typeof events; | |
| vscode: typeof vscode; | |
| }; | |
| const allowedModules: AllowedModules = { | |
| fs, | |
| path, | |
| util, | |
| child_process, | |
| os, | |
| http, | |
| https, | |
| dns, | |
| querystring, | |
| events, | |
| vscode, | |
| }; | |
| const toolkit: Record<string, unknown> = {}; | |
| class CodeExecutionOutputChannel { | |
| private static _instance: vscode.LogOutputChannel | null = null; | |
| static getInstance(): vscode.LogOutputChannel { | |
| if (!CodeExecutionOutputChannel._instance) { | |
| CodeExecutionOutputChannel._instance = vscode.window.createOutputChannel( | |
| "Agent JS Code Execution", | |
| { log: true } | |
| ); | |
| } | |
| return CodeExecutionOutputChannel._instance; | |
| } | |
| } | |
| function createSandboxRequire(moduleName: string): unknown { | |
| if (Object.prototype.hasOwnProperty.call(allowedModules, moduleName)) { | |
| return allowedModules[moduleName as keyof AllowedModules]; | |
| } | |
| throw new Error(`Module "${moduleName}" is not allowed in sandbox`); | |
| } | |
| function createOutputFunction( | |
| logsArray: string[], | |
| outputChannel: vscode.LogOutputChannel | |
| ): (logMessage: string) => void { | |
| return function output(logMessage: string): void { | |
| logsArray.push(logMessage); | |
| outputChannel.appendLine(logMessage); | |
| }; | |
| } | |
| function createSandboxContext( | |
| logsArray: string[], | |
| outputChannel: vscode.LogOutputChannel | |
| ): vm.Context { | |
| return vm.createContext({ | |
| ...globalThis, | |
| toolkit, | |
| ...allowedModules, | |
| require: createSandboxRequire, | |
| output: createOutputFunction(logsArray, outputChannel), | |
| }); | |
| } | |
| async function executeCodeInSandbox(sourceCode: string): Promise<string> { | |
| const outputChannel = CodeExecutionOutputChannel.getInstance(); | |
| const executionLogs: string[] = []; | |
| const sandboxContext = createSandboxContext(executionLogs, outputChannel); | |
| const wrappedCode = `(async () => { ${sourceCode} })();`; | |
| try { | |
| const script = new vm.Script(wrappedCode); | |
| await script.runInContext(sandboxContext); | |
| } catch (error: unknown) { | |
| const errorMessage = | |
| error instanceof Error ? error.message : String(error); | |
| executionLogs.push(`CODE ERROR: ${errorMessage}`); | |
| } | |
| return executionLogs.join("\n"); | |
| } | |
| export async function activate( | |
| context: vscode.ExtensionContext | |
| ): Promise<void> { | |
| const executeCodeCommand = vscode.commands.registerCommand( | |
| "antigravity-code-executor.executeCode", | |
| executeCodeInSandbox | |
| ); | |
| context.subscriptions.push(executeCodeCommand); | |
| } | |
| export function deactivate(): void {} | |
| export { CodeExecutionOutputChannel }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment