|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +JSVM2 is a JavaScript interpreter implemented in JavaScript, designed to execute JavaScript code in a sandboxed environment. It implements ECMAScript 5 features with partial ES2015+ support and follows the ECMAScript specification (https://www.ecma-international.org/wp-content/uploads/ECMA-262_5th_edition_december_2009.pdf). |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Building |
| 12 | +```bash |
| 13 | +npm run build # Build the project using Rollup |
| 14 | +``` |
| 15 | + |
| 16 | +### Testing |
| 17 | +```bash |
| 18 | +npm test # Run all tests with Jest |
| 19 | +npm run test:coverage # Run tests with coverage report |
| 20 | +npm run test:debug # Run tests in debug mode with inspector |
| 21 | +``` |
| 22 | + |
| 23 | +### Code Quality |
| 24 | +```bash |
| 25 | +npm run typecheck # Type check with TypeScript (use this to verify changes) |
| 26 | +``` |
| 27 | + |
| 28 | +Note: There are no lint commands defined in package.json. Always run `npm run typecheck` after making changes to ensure TypeScript compliance. |
| 29 | + |
| 30 | +## Architecture Overview |
| 31 | + |
| 32 | +### Core Components |
| 33 | + |
| 34 | +- **VM (`src/vm.ts`)**: Main entry point providing `run()`, `runInContext()`, and `createContext()` functions |
| 35 | +- **Visitor Pattern (`src/visitor.ts`)**: Central dispatcher that routes AST nodes to appropriate handlers |
| 36 | +- **Context (`src/context.ts`)**: Sandboxed execution environment with global objects (ECMAScript standard globals) |
| 37 | +- **Scope (`src/scope.ts`)**: Variable scope management system |
| 38 | +- **Path (`src/path.ts`)**: AST node wrapper providing traversal context |
| 39 | +- **Stack (`src/stack.ts`)**: Call stack management for debugging and error handling |
| 40 | + |
| 41 | +### Language Support Structure |
| 42 | + |
| 43 | +- **`src/standard/es5/`**: Implementation of ECMAScript 5 features (expressions, statements, declarations) |
| 44 | +- **`src/standard/es2015/`**: Implementation of ES2015+ features (arrow functions, let/const, destructuring) |
| 45 | +- **`src/standard/index.ts`**: Combines all language feature implementations into a single visitor map |
| 46 | + |
| 47 | +### Key Execution Flow |
| 48 | + |
| 49 | +1. Code is parsed into AST using Babel parser |
| 50 | +2. AST is wrapped in Path objects |
| 51 | +3. Visitor pattern dispatches each node type to its implementation |
| 52 | +4. Scope chain manages variable bindings |
| 53 | +5. Context provides sandboxed global environment |
| 54 | + |
| 55 | +### Test Structure |
| 56 | + |
| 57 | +- **`__tests__/helper.ts`**: Core testing utilities with `run()` and `runExp()` functions |
| 58 | +- Tests use Babel to transform modern JavaScript to ES5 when needed |
| 59 | +- Supports optional code hoisting and minification for testing edge cases |
| 60 | + |
| 61 | +## Implementation Guidelines |
| 62 | + |
| 63 | +- The engine operates in strict mode |
| 64 | +- All AST node types must have corresponding implementations in `src/standard/` |
| 65 | +- New language features should be added to appropriate ES version directories |
| 66 | +- Context isolation is critical - avoid exposing host environment globals |
| 67 | +- Follow the existing visitor pattern for consistency |
| 68 | + |
| 69 | +## 语言规范 |
| 70 | + |
| 71 | +- 所有对话和文档都使用中文,代码中注释也使用中文 |
| 72 | +- 文档使用 markdown 格式 |
0 commit comments