@innovatorved/code-lab 0.1.13 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/workbench.ts CHANGED
@@ -57,7 +57,7 @@
57
57
 
58
58
  // The bundler rewrites this literal from package.json at build time. Keeping
59
59
  // the fallback in sync lets tests require src/workbench.js directly.
60
- const CODELAB_VERSION = '0.1.13';
60
+ const CODELAB_VERSION = '0.2.0';
61
61
 
62
62
  // Ambient TypeScript declarations for Node.js built-ins inside Monaco
63
63
  const NODE_AMBIENT_DTS = `
@@ -1096,11 +1096,69 @@ declare module 'bun:sqlite' {
1096
1096
  return monacoPromise;
1097
1097
  }
1098
1098
 
1099
- // Helper to convert JS file set to TS file set if explicit TS files aren't provided
1100
- function mapFilesToLanguage(filesObj, lang, explicitTsFiles) {
1099
+ const DEFAULT_STANDALONE_CPP_FILES = {
1100
+ 'solution.hpp': `#pragma once
1101
+ #include <bits/stdc++.h>
1102
+ #include <codelab/dsa.hpp>
1103
+ using namespace std;
1104
+
1105
+ vector<int> twoSum(const vector<int>& nums, int target) {
1106
+ unordered_map<int, int> seen;
1107
+ for (int i = 0; i < (int)nums.size(); ++i) {
1108
+ int complement = target - nums[i];
1109
+ if (seen.count(complement)) return {seen[complement], i};
1110
+ seen[nums[i]] = i;
1111
+ }
1112
+ return {};
1113
+ }
1114
+ `,
1115
+ 'main.cpp': `#include "solution.hpp"
1116
+
1117
+ int main() {
1118
+ vector<int> nums = {2, 7, 11, 15};
1119
+ codelab::dbg(twoSum(nums, 9));
1120
+ return 0;
1121
+ }
1122
+ `,
1123
+ 'test.cpp': `#include <codelab/test.hpp>
1124
+ #include "solution.hpp"
1125
+
1126
+ int main() {
1127
+ DESCRIBE("Standalone C++ Sandbox", [] {
1128
+ TEST("twoSum finds target pair indices", [] {
1129
+ ASSERT_EQ(twoSum({2, 7, 11, 15}, 9), (vector<int>{0, 1}));
1130
+ ASSERT_EQ(twoSum({3, 2, 4}, 6), (vector<int>{1, 2}));
1131
+ });
1132
+ });
1133
+ return codelab::test::exit_code();
1134
+ }
1135
+ `
1136
+ };
1137
+
1138
+ // Helper to convert JS file set to TS or C++ file set if explicit language files aren't provided
1139
+ function mapFilesToLanguage(filesObj, lang, explicitTsFiles, explicitCppFiles) {
1101
1140
  if (lang === 'ts' && explicitTsFiles && Object.keys(explicitTsFiles).length > 0) {
1102
1141
  return { ...explicitTsFiles };
1103
1142
  }
1143
+ if (lang === 'cpp') {
1144
+ if (explicitCppFiles && Object.keys(explicitCppFiles).length > 0) {
1145
+ return { ...explicitCppFiles };
1146
+ }
1147
+ const existingCpp = {};
1148
+ for (const [name, content] of Object.entries(filesObj || {})) {
1149
+ if (/\.(cpp|cc|cxx|h|hpp|md)$/i.test(name)) {
1150
+ existingCpp[name] = content;
1151
+ }
1152
+ }
1153
+ if (Object.keys(existingCpp).some(f => /\.(cpp|cc|cxx)$/i.test(f))) {
1154
+ return existingCpp;
1155
+ }
1156
+ const outCpp = { ...DEFAULT_STANDALONE_CPP_FILES };
1157
+ if (filesObj && filesObj['README.md']) {
1158
+ outCpp['README.md'] = filesObj['README.md'];
1159
+ }
1160
+ return outCpp;
1161
+ }
1104
1162
  const out = {};
1105
1163
  for (const [name, content] of Object.entries(filesObj || {})) {
1106
1164
  if (lang === 'ts' && name.endsWith('.js')) {
@@ -1332,7 +1390,9 @@ declare module 'bun:sqlite' {
1332
1390
  ...(options.guided || {}),
1333
1391
  ...(options.starterFiles ? { starterFiles: options.starterFiles } : {}),
1334
1392
  ...(options.tsStarterFiles ? { tsStarterFiles: options.tsStarterFiles } : {}),
1335
- ...((options.tsSolutionFiles || options.tsFiles) ? { tsSolutionFiles: options.tsSolutionFiles || options.tsFiles } : {})
1393
+ ...((options.tsSolutionFiles || options.tsFiles) ? { tsSolutionFiles: options.tsSolutionFiles || options.tsFiles } : {}),
1394
+ ...(options.cppStarterFiles ? { cppStarterFiles: options.cppStarterFiles } : {}),
1395
+ ...((options.cppSolutionFiles || options.cppFiles) ? { cppSolutionFiles: options.cppSolutionFiles || options.cppFiles } : {})
1336
1396
  };
1337
1397
  this.steps = options.steps || this.guidedConfig?.steps || [];
1338
1398
  this.hasGuidedLab = Boolean(
@@ -1340,12 +1400,41 @@ declare module 'bun:sqlite' {
1340
1400
  options.guidedLab ||
1341
1401
  (this.steps && this.steps.length > 0)
1342
1402
  );
1403
+ this.enabledLanguages = new Set(['js']);
1404
+ if (Array.isArray(options.languages)) {
1405
+ for (const l of options.languages) {
1406
+ if (l === 'js' || l === 'ts' || l === 'cpp') this.enabledLanguages.add(l);
1407
+ }
1408
+ }
1409
+ if (
1410
+ options.hasMultipleLanguages ||
1411
+ this.guidedConfig.tsStarterFiles ||
1412
+ this.guidedConfig.tsSolutionFiles ||
1413
+ this.guidedConfig.tsFiles ||
1414
+ options.lang === 'ts' ||
1415
+ options.language === 'ts'
1416
+ ) {
1417
+ this.enabledLanguages.add('ts');
1418
+ }
1419
+ if (
1420
+ this.guidedConfig.cppStarterFiles ||
1421
+ this.guidedConfig.cppSolutionFiles ||
1422
+ this.guidedConfig.cppFiles ||
1423
+ options.lang === 'cpp' ||
1424
+ options.language === 'cpp'
1425
+ ) {
1426
+ this.enabledLanguages.add('cpp');
1427
+ }
1343
1428
  this.hasMultipleLanguages = Boolean(
1344
1429
  options.hasMultipleLanguages ||
1345
1430
  options.languages ||
1431
+ this.enabledLanguages.size > 1 ||
1346
1432
  this.guidedConfig.tsStarterFiles ||
1347
1433
  this.guidedConfig.tsSolutionFiles ||
1348
1434
  this.guidedConfig.tsFiles ||
1435
+ this.guidedConfig.cppStarterFiles ||
1436
+ this.guidedConfig.cppSolutionFiles ||
1437
+ this.guidedConfig.cppFiles ||
1349
1438
  (this.hasGuidedLab && (options.lang || options.language))
1350
1439
  );
1351
1440
 
@@ -1513,10 +1602,12 @@ declare module 'bun:sqlite' {
1513
1602
  this._bindEvents();
1514
1603
  this._initEditor();
1515
1604
 
1516
- if (Array.isArray(options.plugins)) {
1517
- for (const plugin of options.plugins) {
1518
- this.use(plugin);
1519
- }
1605
+ const initialPlugins = [
1606
+ ...(Array.isArray(options.extensions) ? options.extensions : []),
1607
+ ...(Array.isArray(options.plugins) ? options.plugins : [])
1608
+ ];
1609
+ for (const plugin of initialPlugins) {
1610
+ this.use(plugin);
1520
1611
  }
1521
1612
 
1522
1613
  if (options.repo && typeof options.repo === 'string') {
@@ -1952,7 +2043,11 @@ declare module 'bun:sqlite' {
1952
2043
  curEl.textContent = `Ln ${this.cursorPos.lineNumber || 1}, Col ${this.cursorPos.column || 1}`;
1953
2044
  }
1954
2045
  if (langEl) {
1955
- langEl.textContent = this.lang === 'ts' ? 'TypeScript 5.x' : 'JavaScript (CJS/ESM)';
2046
+ langEl.textContent = this.lang === 'cpp'
2047
+ ? 'C++20 (Clang/WASI)'
2048
+ : this.lang === 'ts'
2049
+ ? 'TypeScript 5.x'
2050
+ : 'JavaScript (CJS/ESM)';
1956
2051
  }
1957
2052
  }
1958
2053
 
@@ -1962,10 +2057,20 @@ declare module 'bun:sqlite' {
1962
2057
  ...this.referenceFiles,
1963
2058
  ...(this.guidedConfig?.starterFiles || {})
1964
2059
  };
1965
- if (mode === 'guided' && this.guidedConfig?.starterFiles) {
1966
- return mapFilesToLanguage(baseJsStarter, lang, this.guidedConfig?.tsStarterFiles);
2060
+ if (mode === 'guided' && (this.guidedConfig?.starterFiles || this.guidedConfig?.tsStarterFiles || this.guidedConfig?.cppStarterFiles)) {
2061
+ return mapFilesToLanguage(
2062
+ baseJsStarter,
2063
+ lang,
2064
+ this.guidedConfig?.tsStarterFiles,
2065
+ this.guidedConfig?.cppStarterFiles
2066
+ );
1967
2067
  }
1968
- return mapFilesToLanguage(baseJsSolution, lang, this.guidedConfig?.tsSolutionFiles);
2068
+ return mapFilesToLanguage(
2069
+ baseJsSolution,
2070
+ lang,
2071
+ this.guidedConfig?.tsSolutionFiles,
2072
+ this.guidedConfig?.cppSolutionFiles
2073
+ );
1969
2074
  }
1970
2075
 
1971
2076
  _isUserCreatedFile(filename) {
@@ -1988,6 +2093,21 @@ declare module 'bun:sqlite' {
1988
2093
 
1989
2094
  _pickDefaultFile() {
1990
2095
  const names = Object.keys(this.files);
2096
+ if (this.lang === 'cpp') {
2097
+ if (this.guidedConfig?.mainFile) {
2098
+ const cppMain = this.guidedConfig.mainFile.replace(/\.(js|ts)$/, '.hpp');
2099
+ if (this.files[cppMain]) return cppMain;
2100
+ const cppMainSrc = this.guidedConfig.mainFile.replace(/\.(js|ts)$/, '.cpp');
2101
+ if (this.files[cppMainSrc]) return cppMainSrc;
2102
+ }
2103
+ if (this.files['solution.hpp']) return 'solution.hpp';
2104
+ if (this.files['main.cpp']) return 'main.cpp';
2105
+ const cppImpl = names.find(f =>
2106
+ /\.(cpp|cc|cxx|hpp|h)$/i.test(f) &&
2107
+ !/(^|\/)(demo|test)\.(cpp|cc|cxx)$/i.test(f)
2108
+ );
2109
+ return cppImpl || names.find(f => !f.endsWith('.md')) || names[0] || '';
2110
+ }
1991
2111
  const ext = this.lang === 'ts' ? '.ts' : '.js';
1992
2112
  if (this.guidedConfig?.mainFile) {
1993
2113
  const mainCandidate = this.lang === 'ts'
@@ -2004,6 +2124,27 @@ declare module 'bun:sqlite' {
2004
2124
 
2005
2125
  _findEntryFile(kind) {
2006
2126
  const names = Object.keys(this.files);
2127
+ if (this.lang === 'cpp') {
2128
+ if (kind === 'test') {
2129
+ if (this.explicitTestEntry) {
2130
+ const cppTest = this.explicitTestEntry.replace(/\.(js|ts)$/, '.cpp');
2131
+ if (this.files[cppTest]) return cppTest;
2132
+ if (this.files[this.explicitTestEntry]) return this.explicitTestEntry;
2133
+ }
2134
+ if (this.files['test.cpp']) return 'test.cpp';
2135
+ return names.find(f => /(^|\/)test\.(cpp|cc|cxx)$/i.test(f)) || null;
2136
+ }
2137
+ if (this.explicitEntry) {
2138
+ const cppEntry = this.explicitEntry.replace(/\.(js|ts)$/, '.cpp');
2139
+ if (this.files[cppEntry]) return cppEntry;
2140
+ if (this.files[this.explicitEntry]) return this.explicitEntry;
2141
+ }
2142
+ if (this.files['main.cpp']) return 'main.cpp';
2143
+ if (this.files['demo.cpp']) return 'demo.cpp';
2144
+ return names.find(f => /(^|\/)(main|demo)\.(cpp|cc|cxx)$/i.test(f)) ||
2145
+ names.find(f => /\.(cpp|cc|cxx)$/i.test(f) && !/(^|\/)test\.(cpp|cc|cxx)$/i.test(f)) ||
2146
+ null;
2147
+ }
2007
2148
  if (kind === 'test' && this.explicitTestEntry) {
2008
2149
  const candidate = this.lang === 'ts' ? this.explicitTestEntry.replace(/\.js$/, '.ts') : this.explicitTestEntry;
2009
2150
  if (this.files[candidate]) return candidate;
@@ -2014,16 +2155,16 @@ declare module 'bun:sqlite' {
2014
2155
  if (this.files[candidate]) return candidate;
2015
2156
  if (this.files[this.explicitEntry]) return this.explicitEntry;
2016
2157
  }
2017
- const pattern = kind === 'test' ? /(^|\/)test\.(js|ts)$/i : /(^|\/)demo\.(js|ts)$/i;
2158
+ const pattern = kind === 'test' ? /(^|\/)test\.(js|ts|cpp|cc|cxx)$/i : /(^|\/)(demo|main)\.(js|ts|cpp|cc|cxx)$/i;
2018
2159
  const found = names.find(f => pattern.test(f));
2019
2160
  if (found) return found;
2020
2161
  if (kind === 'demo') {
2021
- const isConfigOrTest = f => /(^|\/)(test|.*\.(?:test|spec|config|d))\.(js|ts|mjs|cjs)$/i.test(f) || /(^|\/)\.[^/]+$/i.test(f);
2162
+ const isConfigOrTest = f => /(^|\/)(test|.*\.(?:test|spec|config|d))\.(js|ts|mjs|cjs|cpp|cc|cxx)$/i.test(f) || /(^|\/)\.[^/]+$/i.test(f);
2022
2163
  const hasHtmlEntry = Boolean(this.files['index.html'] || this.files['public/index.html']);
2023
2164
  if (hasHtmlEntry) {
2024
2165
  return names.find(f => /(^|\/)(server|app|demo)\.(js|ts)$/i.test(f) && !/^src\//i.test(f)) || null;
2025
2166
  }
2026
- return names.find(f => /\.(js|ts)$/i.test(f) && !isConfigOrTest(f)) || null;
2167
+ return names.find(f => /\.(js|ts|cpp|cc|cxx)$/i.test(f) && !isConfigOrTest(f)) || null;
2027
2168
  }
2028
2169
  return null;
2029
2170
  }
@@ -2050,6 +2191,44 @@ declare module 'bun:sqlite' {
2050
2191
  });
2051
2192
  }
2052
2193
 
2194
+ _renderLanguageSwitcherHtml() {
2195
+ const hasMulti = Boolean(
2196
+ this.hasMultipleLanguages ||
2197
+ (this.enabledLanguages && this.enabledLanguages.size > 1)
2198
+ );
2199
+ if (!hasMulti) return '';
2200
+ const showTs = Boolean(
2201
+ !this.enabledLanguages ||
2202
+ this.enabledLanguages.has('ts') ||
2203
+ !this.enabledLanguages.has('cpp')
2204
+ );
2205
+ const showCpp = Boolean(this.enabledLanguages && this.enabledLanguages.has('cpp'));
2206
+ return `
2207
+ <div class="codelab-lang-switch" role="group" aria-label="Language Selector">
2208
+ <button type="button" class="codelab-lang-btn ${this.lang === 'js' ? 'active' : ''}" data-cl-lang="js" data-lang="js" aria-pressed="${this.lang === 'js'}">JS</button>
2209
+ ${showTs ? `<button type="button" class="codelab-lang-btn ${this.lang === 'ts' ? 'active' : ''}" data-cl-lang="ts" data-lang="ts" aria-pressed="${this.lang === 'ts'}">TS</button>` : ''}
2210
+ ${showCpp ? `<button type="button" class="codelab-lang-btn ${this.lang === 'cpp' ? 'active' : ''}" data-cl-lang="cpp" data-lang="cpp" aria-pressed="${this.lang === 'cpp'}">C++</button>` : ''}
2211
+ </div>
2212
+ `;
2213
+ }
2214
+
2215
+ _refreshLanguageSwitcher() {
2216
+ if (!this.container) return;
2217
+ const topLeft = this.container.querySelector('.codelab-topbar__left');
2218
+ if (!topLeft) return;
2219
+ const existing = topLeft.querySelector('.codelab-lang-switch');
2220
+ const htmlStr = this._renderLanguageSwitcherHtml().trim();
2221
+ if (existing) {
2222
+ if (htmlStr) {
2223
+ existing.outerHTML = htmlStr;
2224
+ } else {
2225
+ existing.remove();
2226
+ }
2227
+ } else if (htmlStr) {
2228
+ topLeft.insertAdjacentHTML('beforeend', htmlStr);
2229
+ }
2230
+ }
2231
+
2053
2232
  // ── Render Workbench DOM ───────────────────────────────────────────────
2054
2233
  _renderShell() {
2055
2234
  if (this.themeOption !== 'auto') {
@@ -2090,12 +2269,7 @@ declare module 'bun:sqlite' {
2090
2269
  </button>
2091
2270
  </div>
2092
2271
  ` : ''}
2093
- ${this.hasMultipleLanguages ? `
2094
- <div class="codelab-lang-switch" role="group" aria-label="Language Selector">
2095
- <button type="button" class="codelab-lang-btn ${this.lang === 'js' ? 'active' : ''}" data-cl-lang="js" data-lang="js" aria-pressed="${this.lang === 'js'}">JS</button>
2096
- <button type="button" class="codelab-lang-btn ${this.lang === 'ts' ? 'active' : ''}" data-cl-lang="ts" data-lang="ts" aria-pressed="${this.lang === 'ts'}">TS</button>
2097
- </div>
2098
- ` : ''}
2272
+ ${this._renderLanguageSwitcherHtml()}
2099
2273
  </div>
2100
2274
 
2101
2275
  <div class="codelab-topbar__right">
@@ -2269,7 +2443,7 @@ declare module 'bun:sqlite' {
2269
2443
  <span class="codelab-statusbar__sep">·</span>
2270
2444
  <span class="codelab-statusbar__item">UTF-8</span>
2271
2445
  <span class="codelab-statusbar__sep">·</span>
2272
- <span class="codelab-statusbar__item" id="cl-status-lang">${this.lang === 'ts' ? 'TypeScript 5.x' : 'JavaScript (CJS/ESM)'}</span>
2446
+ <span class="codelab-statusbar__item" id="cl-status-lang">${this.lang === 'cpp' ? 'C++20 (Clang/WASI)' : this.lang === 'ts' ? 'TypeScript 5.x' : 'JavaScript (CJS/ESM)'}</span>
2273
2447
  </div>
2274
2448
  </footer>
2275
2449
 
@@ -2290,13 +2464,30 @@ declare module 'bun:sqlite' {
2290
2464
  this._updateRunStateUI();
2291
2465
  }
2292
2466
 
2467
+ _resolveStepTargetFile(rawTarget) {
2468
+ if (!rawTarget) return '';
2469
+ if (this.lang === 'cpp') {
2470
+ const asHpp = rawTarget.replace(/\.(js|ts)$/, '.hpp');
2471
+ if (this.files && this.files[asHpp] !== undefined) return asHpp;
2472
+ const asCpp = rawTarget.replace(/\.(js|ts)$/, '.cpp');
2473
+ if (this.files && this.files[asCpp] !== undefined) return asCpp;
2474
+ if (this.files && this.files['solution.hpp'] !== undefined) return 'solution.hpp';
2475
+ if (this.files && this.files['main.cpp'] !== undefined) return 'main.cpp';
2476
+ return asCpp;
2477
+ }
2478
+ if (this.lang === 'ts') {
2479
+ return rawTarget.replace(/\.js$/, '.ts');
2480
+ }
2481
+ return rawTarget;
2482
+ }
2483
+
2293
2484
  _detectActiveSymbol(filePath) {
2294
2485
  if (!filePath || !this.files || typeof this.files[filePath] !== 'string') return '';
2295
2486
  if (this.mode === 'guided' && this.steps && this.steps.length > 0) {
2296
2487
  const step = this.steps[this.activeStepIndex];
2297
2488
  if (step) {
2298
2489
  const rawTarget = step.targetFile || '';
2299
- const stepTarget = this.lang === 'ts' && rawTarget ? rawTarget.replace(/\.js$/, '.ts') : rawTarget;
2490
+ const stepTarget = rawTarget ? this._resolveStepTargetFile(rawTarget) : '';
2300
2491
  if ((!stepTarget || stepTarget === filePath) && (step.targetSymbol || step.symbol)) {
2301
2492
  return String(step.targetSymbol || step.symbol);
2302
2493
  }
@@ -2499,7 +2690,7 @@ declare module 'bun:sqlite' {
2499
2690
 
2500
2691
  const step = this.steps[this.activeStepIndex] || this.steps[0];
2501
2692
  const isStepPassed = this.completedSteps.has(step.id);
2502
- const targetFile = this.lang === 'ts' && step.targetFile ? step.targetFile.replace(/\.js$/, '.ts') : (step.targetFile || this._pickDefaultFile());
2693
+ const targetFile = step.targetFile ? this._resolveStepTargetFile(step.targetFile) : this._pickDefaultFile();
2503
2694
  const progressPct = Math.round((this.completedSteps.size / Math.max(1, this.steps.length)) * 100);
2504
2695
 
2505
2696
  // Match step test results
@@ -2726,8 +2917,8 @@ declare module 'bun:sqlite' {
2726
2917
  const sortedFiles = Array.from(node.files.entries()).sort((a, b) => a[0].localeCompare(b[0]));
2727
2918
  for (const [baseName, fullPath] of sortedFiles) {
2728
2919
  const isActive = fullPath === this.activeFile;
2729
- const badge = fullPath.endsWith('.md') ? 'DOC' : fullPath.endsWith('.json') ? 'PKG' : /(^|\/)test\./i.test(fullPath) ? 'TEST' : /(^|\/)demo\./i.test(fullPath) ? 'DEMO' : 'SRC';
2730
- const ext = fullPath.endsWith('.ts') ? 'ts' : fullPath.endsWith('.js') ? 'js' : fullPath.endsWith('.json') ? 'json' : fullPath.endsWith('.md') ? 'md' : 'txt';
2920
+ const badge = fullPath.endsWith('.md') ? 'DOC' : fullPath.endsWith('.json') ? 'PKG' : /(^|\/)test\./i.test(fullPath) ? 'TEST' : /(^|\/)(demo|main)\./i.test(fullPath) ? 'DEMO' : /\.(hpp|h)$/i.test(fullPath) ? 'HDR' : 'SRC';
2921
+ const ext = /\.(cpp|cc|cxx|hpp|h)$/i.test(fullPath) ? 'cpp' : fullPath.endsWith('.ts') ? 'ts' : fullPath.endsWith('.js') ? 'js' : fullPath.endsWith('.json') ? 'json' : fullPath.endsWith('.md') ? 'md' : 'txt';
2731
2922
  const fileIcon = ext === 'json' ? CL_ICONS.database : ext === 'md' ? CL_ICONS.book : CL_ICONS.file;
2732
2923
  const indent = depth * 14 + 10;
2733
2924
  treeHtml += `
@@ -2851,10 +3042,10 @@ declare module 'bun:sqlite' {
2851
3042
  const openTabs = Array.isArray(this.openTabs) ? this.openTabs : [];
2852
3043
  const tabsHtml = openTabs.map(f => {
2853
3044
  const isActive = f === this.activeFile;
2854
- const ext = f.endsWith('.ts') ? 'ts' : f.endsWith('.js') ? 'js' : f.endsWith('.json') ? 'json' : f.endsWith('.md') ? 'md' : 'txt';
3045
+ const ext = /\.(cpp|cc|cxx|hpp|h)$/i.test(f) ? 'cpp' : f.endsWith('.ts') ? 'ts' : f.endsWith('.js') ? 'js' : f.endsWith('.json') ? 'json' : f.endsWith('.md') ? 'md' : 'txt';
2855
3046
  return `
2856
3047
  <div role="tab" tabindex="0" aria-selected="${isActive}" class="codelab-tab ${isActive ? 'active' : ''}" data-cl-file="${escapeHtml(f)}" data-tab-file="${escapeHtml(f)}">
2857
- <span class="codelab-tab__ext codelab-tab__ext--${ext}">${ext.toUpperCase()}</span>
3048
+ <span class="codelab-tab__ext codelab-tab__ext--${ext}">${ext === 'cpp' ? 'C++' : ext.toUpperCase()}</span>
2858
3049
  <span class="codelab-tab__label">${escapeHtml(f)}</span>
2859
3050
  <button type="button" class="codelab-tab__close" data-action="close-tab" data-cl-file="${escapeHtml(f)}" title="Close ${escapeHtml(f)}" aria-label="Close ${escapeHtml(f)}">×</button>
2860
3051
  </div>
@@ -3109,7 +3300,7 @@ declare module 'bun:sqlite' {
3109
3300
  createFile(rawName, initialContent = null) {
3110
3301
  let clean = String(rawName).trim().replace(/^\/+/, '');
3111
3302
  if (!clean) return;
3112
- if (!/\.(js|ts|json|md|txt)$/i.test(clean) && !clean.includes('.')) {
3303
+ if (!/\.(js|ts|cpp|cc|cxx|hpp|h|json|md|txt)$/i.test(clean) && !clean.includes('.')) {
3113
3304
  clean += `.${this.lang}`;
3114
3305
  }
3115
3306
  if (this.files[clean] !== undefined) {
@@ -3127,7 +3318,11 @@ declare module 'bun:sqlite' {
3127
3318
  } else {
3128
3319
  this.files[clean] = clean.endsWith('.json')
3129
3320
  ? '{\n "name": "' + this.labId + '",\n "dependencies": {}\n}\n'
3130
- : `'use strict';\n\n// ${clean}\n`;
3321
+ : /\.(hpp|h)$/i.test(clean)
3322
+ ? `#pragma once\n#include <bits/stdc++.h>\n\n// ${clean}\n`
3323
+ : /\.(cpp|cc|cxx)$/i.test(clean)
3324
+ ? `#include <bits/stdc++.h>\nusing namespace std;\n\n// ${clean}\nint main() {\n return 0;\n}\n`
3325
+ : `'use strict';\n\n// ${clean}\n`;
3131
3326
  }
3132
3327
  this._saveState();
3133
3328
  this._getOrCreateModel(clean);
@@ -5735,7 +5930,15 @@ declare module 'bun:sqlite' {
5735
5930
  const rawUri = `file:///${this.labId}/${filename}`;
5736
5931
  const uri = uriParser ? uriParser.parse(rawUri) : { toString: () => rawUri };
5737
5932
  let model = typeof this.monaco.editor.getModel === 'function' ? this.monaco.editor.getModel(uri) : null;
5738
- const lang = filename.endsWith('.ts') ? 'typescript' : filename.endsWith('.json') ? 'json' : filename.endsWith('.md') ? 'markdown' : 'javascript';
5933
+ const lang = /\.(cpp|cc|cxx|hpp|h)$/i.test(filename)
5934
+ ? 'cpp'
5935
+ : filename.endsWith('.ts')
5936
+ ? 'typescript'
5937
+ : filename.endsWith('.json')
5938
+ ? 'json'
5939
+ : filename.endsWith('.md')
5940
+ ? 'markdown'
5941
+ : 'javascript';
5739
5942
  if (!model) {
5740
5943
  model = this.monaco.editor.createModel(this.files[filename] || '', lang, uri);
5741
5944
  if (model && typeof model.onDidChangeContent === 'function') {
@@ -6521,7 +6724,12 @@ declare module 'bun:sqlite' {
6521
6724
  { id: 'export-workspace', label: 'Workspace: Export Workspace as JSON', action: () => this.exportWorkspace({ download: true }) },
6522
6725
  { id: 'export-workspace-zip', label: 'Workspace: Export Project as ZIP Archive', action: () => this.exportProjectZip({ download: true }) },
6523
6726
  { id: 'reset-workspace', label: 'Workspace: Reset Workspace to Clean State', action: () => this.resetLab() },
6524
- { id: 'switch-lang', label: 'Language: Switch JavaScript / TypeScript', action: () => this.setLanguage(this.lang === 'js' ? 'ts' : 'js') },
6727
+ { id: 'switch-lang', label: 'Language: Switch JavaScript / TypeScript / C++', action: () => {
6728
+ const order = ['js', 'ts'];
6729
+ if (this.enabledLanguages && this.enabledLanguages.has('cpp')) order.push('cpp');
6730
+ const next = order[(order.indexOf(this.lang) + 1) % order.length] || 'js';
6731
+ this.setLanguage(next);
6732
+ } },
6525
6733
  { id: 'switch-mode', label: 'Mode: Switch Guided / Sandbox Mode', action: () => this.setMode(this.mode === 'guided' ? 'sandbox' : 'guided') },
6526
6734
  { id: 'toggle-guide', label: 'View: Toggle Guide Sidebar', action: () => this.toggleGuide() },
6527
6735
  { id: 'toggle-drawer', label: 'View: Toggle Bottom Drawer (Tests / Terminal / VFS)', action: () => {
@@ -6986,7 +7194,7 @@ declare module 'bun:sqlite' {
6986
7194
  const step = this.steps[idx];
6987
7195
  if (step) {
6988
7196
  const rawTarget = step.targetFile || this._pickDefaultFile();
6989
- const targetFile = this.lang === 'ts' && rawTarget ? rawTarget.replace(/\.js$/, '.ts') : rawTarget;
7197
+ const targetFile = rawTarget ? this._resolveStepTargetFile(rawTarget) : this._pickDefaultFile();
6990
7198
  this.openFile(targetFile, step.targetSymbol || `TODO (Step ${idx + 1})`);
6991
7199
  }
6992
7200
  }
@@ -7090,15 +7298,27 @@ declare module 'bun:sqlite' {
7090
7298
  }
7091
7299
  }
7092
7300
 
7301
+ get language() {
7302
+ return this.lang;
7303
+ }
7304
+
7305
+ set language(newLang) {
7306
+ this.setLanguage(newLang);
7307
+ }
7308
+
7093
7309
  setLanguage(newLang) {
7094
- if (newLang !== 'js' && newLang !== 'ts') return;
7310
+ if (newLang !== 'js' && newLang !== 'ts' && newLang !== 'cpp') return;
7095
7311
  if (this.lang === newLang) return;
7096
7312
  this.lang = newLang;
7313
+ if (this.enabledLanguages instanceof Set) {
7314
+ this.enabledLanguages.add(newLang);
7315
+ }
7097
7316
  this.files = this._computeInitialFiles();
7098
7317
  this.openTabs = this._orderedFileNames();
7099
7318
  this.activeFile = this._pickDefaultFile();
7100
7319
  this._saveState();
7101
7320
 
7321
+ this._refreshLanguageSwitcher();
7102
7322
  this.container.querySelectorAll('[data-cl-lang]').forEach(btn => {
7103
7323
  const isActive = btn.dataset.clLang === this.lang;
7104
7324
  btn.classList.toggle('active', isActive);
@@ -7110,6 +7330,14 @@ declare module 'bun:sqlite' {
7110
7330
  this._renderGuidePane();
7111
7331
  }
7112
7332
 
7333
+ switchLang(newLang) {
7334
+ return this.setLanguage(newLang);
7335
+ }
7336
+
7337
+ switchMode(newMode) {
7338
+ return this.setMode(newMode);
7339
+ }
7340
+
7113
7341
  configure({ mode, lang, stepIndex, onClose } = {}) {
7114
7342
  if (typeof onClose === 'function') {
7115
7343
  this.onClose = onClose;
@@ -7129,7 +7357,7 @@ declare module 'bun:sqlite' {
7129
7357
  if (mode === 'guided' || mode === 'sandbox') {
7130
7358
  this.setMode(mode);
7131
7359
  }
7132
- if (lang === 'js' || lang === 'ts') {
7360
+ if (lang === 'js' || lang === 'ts' || lang === 'cpp') {
7133
7361
  this.setLanguage(lang);
7134
7362
  }
7135
7363
  if (typeof stepIndex === 'number' && !Number.isNaN(stepIndex)) {
@@ -7384,12 +7612,14 @@ declare module 'bun:sqlite' {
7384
7612
  const payload = {
7385
7613
  ...execOptions,
7386
7614
  instanceId: this.instanceId,
7615
+ cppCompiler: execOptions.cppCompiler || this.cppCompiler,
7387
7616
  onStdout: execOptions.onStdout || ((chunk) => this._appendTerminalStream('stdout', chunk)),
7388
7617
  onStderr: execOptions.onStderr || ((chunk) => this._appendTerminalStream('stderr', chunk)),
7389
7618
  signal: execOptions.signal || this._activeCommandAbortController?.signal
7390
7619
  };
7391
7620
  try {
7392
- if (typeof root.CodeLabRuntime?.preloadNpmPackages === 'function' && payload && payload.files) {
7621
+ const isCppEntry = Boolean(payload && typeof payload.entry === 'string' && /\.(cpp|cc|cxx|wasm)$/i.test(payload.entry));
7622
+ if (!isCppEntry && typeof root.CodeLabRuntime?.preloadNpmPackages === 'function' && payload && payload.files) {
7393
7623
  const rawDetected = this._detectPackages();
7394
7624
  const detectedList = Array.isArray(rawDetected)
7395
7625
  ? rawDetected
@@ -7406,9 +7636,11 @@ declare module 'bun:sqlite' {
7406
7636
  payload.packages = Object.assign({}, preloaded, payload.packages || {});
7407
7637
  }
7408
7638
  }
7409
- this._setRuntimeStatus('running', `Running ${payload.entry || payload.mode || 'script'}...`);
7639
+ this._setRuntimeStatus('running', isCppEntry ? `Compiling & Running ${payload.entry}...` : `Running ${payload.entry || payload.mode || 'script'}...`);
7410
7640
  let res;
7411
- if (typeof root.CodeLabRuntime?.executeInWorker === 'function') {
7641
+ if (isCppEntry && typeof root.CodeLabRuntime?.executeInVirtualRuntime === 'function') {
7642
+ res = await root.CodeLabRuntime.executeInVirtualRuntime(payload);
7643
+ } else if (typeof root.CodeLabRuntime?.executeInWorker === 'function') {
7412
7644
  res = await root.CodeLabRuntime.executeInWorker(payload);
7413
7645
  } else if (typeof root.CodeLabRuntime?.executeInVirtualRuntime === 'function') {
7414
7646
  res = await root.CodeLabRuntime.executeInVirtualRuntime(payload);
@@ -7445,13 +7677,18 @@ declare module 'bun:sqlite' {
7445
7677
  this.isRunning = true;
7446
7678
  this._updateRunStateUI();
7447
7679
  this.drawerTab = 'terminal';
7448
- this.terminalLogs.push({ type: 'system', text: `\n$ node ${entry}` });
7680
+ const isCppDemo = /\.(cpp|cc|cxx)$/i.test(entry);
7681
+ this.terminalLogs.push({
7682
+ type: 'system',
7683
+ text: isCppDemo ? `\n$ g++ -std=c++20 ${entry} -o a.out && ./a.out` : `\n$ node ${entry}`
7684
+ });
7449
7685
  this._renderDrawerBody();
7450
7686
 
7451
- const abortCtrl = new AbortController();
7687
+ let abortCtrl = new AbortController();
7452
7688
  this._activeCommandAbortController = abortCtrl;
7453
7689
  let streamedStdout = false;
7454
7690
  let streamedStderr = false;
7691
+ let lastRes = null;
7455
7692
 
7456
7693
  try {
7457
7694
  const res = await this._executeRuntime({
@@ -7468,6 +7705,7 @@ declare module 'bun:sqlite' {
7468
7705
  this._appendTerminalStream('stderr', c);
7469
7706
  }
7470
7707
  });
7708
+ lastRes = res;
7471
7709
  this._flushTerminalStream();
7472
7710
  const aborted = this._isAbortResult(res, abortCtrl.signal);
7473
7711
 
@@ -7503,6 +7741,7 @@ declare module 'bun:sqlite' {
7503
7741
  }
7504
7742
 
7505
7743
  this._renderDrawerBody();
7744
+ return lastRes;
7506
7745
  }
7507
7746
 
7508
7747
  async runTests(filterTestNames = null) {
@@ -7518,12 +7757,17 @@ declare module 'bun:sqlite' {
7518
7757
  this.isRunning = true;
7519
7758
  this._updateRunStateUI();
7520
7759
  this.drawerTab = 'tests';
7521
- this.terminalLogs.push({ type: 'system', text: `\n$ node ${entry}` });
7760
+ const isCppTest = /\.(cpp|cc|cxx)$/i.test(entry);
7761
+ this.terminalLogs.push({
7762
+ type: 'system',
7763
+ text: isCppTest ? `\n$ g++ -std=c++20 ${entry} -o a.out && ./a.out` : `\n$ node ${entry}`
7764
+ });
7522
7765
 
7523
- const abortCtrl = new AbortController();
7766
+ let abortCtrl = new AbortController();
7524
7767
  this._activeCommandAbortController = abortCtrl;
7525
7768
  let streamedStdout = false;
7526
7769
  let streamedStderr = false;
7770
+ let lastRes = null;
7527
7771
 
7528
7772
  try {
7529
7773
  const res = await this._executeRuntime({
@@ -7541,6 +7785,7 @@ declare module 'bun:sqlite' {
7541
7785
  this._appendTerminalStream('stderr', c);
7542
7786
  }
7543
7787
  });
7788
+ lastRes = res;
7544
7789
  this._flushTerminalStream();
7545
7790
  const aborted = this._isAbortResult(res, abortCtrl.signal);
7546
7791
 
@@ -7621,6 +7866,7 @@ declare module 'bun:sqlite' {
7621
7866
  this._renderStepPills();
7622
7867
  this._renderGuidePane();
7623
7868
  this._renderDrawerBody();
7869
+ return lastRes;
7624
7870
  }
7625
7871
 
7626
7872
  async verifyCurrentStep() {
@@ -7951,7 +8197,7 @@ declare module 'bun:sqlite' {
7951
8197
  if (data.mode === 'guided' || data.mode === 'sandbox') {
7952
8198
  this.mode = data.mode;
7953
8199
  }
7954
- if (data.language === 'js' || data.language === 'ts') {
8200
+ if (data.language === 'js' || data.language === 'ts' || data.language === 'cpp') {
7955
8201
  this.lang = data.language;
7956
8202
  }
7957
8203
  if (data.files && typeof data.files === 'object') {
@@ -8501,6 +8747,10 @@ declare module 'bun:sqlite' {
8501
8747
  } else if (typeof plugin === 'object' && typeof plugin.install === 'function') {
8502
8748
  plugin.install(this, pluginOptions);
8503
8749
  }
8750
+ if (this.enabledLanguages instanceof Set && this.enabledLanguages.size > 1) {
8751
+ this.hasMultipleLanguages = true;
8752
+ }
8753
+ this._refreshLanguageSwitcher();
8504
8754
  return this;
8505
8755
  }
8506
8756
 
@@ -8563,7 +8813,7 @@ declare module 'bun:sqlite' {
8563
8813
  if (rootEl) rootEl.setAttribute('data-cl-theme', this._instance._resolvedTheme());
8564
8814
  } else if (name === 'mode' && (newVal === 'guided' || newVal === 'sandbox')) {
8565
8815
  this._instance.switchMode(newVal);
8566
- } else if ((name === 'language' || name === 'lang') && (newVal === 'js' || newVal === 'ts')) {
8816
+ } else if ((name === 'language' || name === 'lang') && (newVal === 'js' || newVal === 'ts' || newVal === 'cpp')) {
8567
8817
  this._instance.switchLang(newVal);
8568
8818
  } else if (name === 'title' && newVal) {
8569
8819
  this._instance.title = newVal;
@@ -8728,7 +8978,10 @@ declare module 'bun:sqlite' {
8728
8978
  renderMarkdown: renderSimpleMarkdown,
8729
8979
  renderSimpleMarkdown: renderSimpleMarkdown,
8730
8980
  extensions: {
8731
- guidedLab: null
8981
+ guidedLab: null,
8982
+ javascript: null,
8983
+ typescript: null,
8984
+ cpp: null
8732
8985
  },
8733
8986
  parseGitHubRepoUrl,
8734
8987
  cloneRepo: cloneGitHubRepository,