Repository navigation
Support Nim libraries #1074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Nim libraries #1074
Changes from all commits
26c3467
949cd4b
9963362
7f18915
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,36 +4,61 @@ import { getLanguageCustomSettings } from '../utils'; | |
|
|
||
| // Nim's JavaScript backend, for pages that want a document: the compiler emits one self-sufficient | ||
| // file, so it is compiled with `execute: false` and the result is run in the result page, the same | ||
| // shape as the TypeScript compiler's factory. | ||
| // shape as the TypeScript compiler's factory. External packages are fetched on first import. | ||
| export const nim: LanguageSpecs = { | ||
| name: 'nim', | ||
| title: 'Nim', | ||
| compiler: { | ||
| url: nimWasmBaseUrl + 'dist/nim-wasm.global.js', | ||
| factory: () => { | ||
| const compilers: Record<'js' | 'c' | 'cpp', Promise<any> | null> = { | ||
| js: null, | ||
| c: null, | ||
| cpp: null, | ||
| }; | ||
| const getCompiler = (target: 'js' | 'c' | 'cpp' = 'js') => { | ||
| if (!compilers[target]) { | ||
| const created: Promise<any> = (self as any).nimWasm.createCompiler({ | ||
| target, | ||
| emit: 'single', | ||
| baseUrl: nimWasmBaseUrl + 'assets/nim/', | ||
| }); | ||
| const compilers = new Map<string, Promise<any>>(); | ||
| const libraryCache = new Map<string, Promise<any>>(); | ||
|
Comment on lines
+14
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift Release compilers when library configurations change. Each edit to an inline library can produce a new cache key. 🤖 Prompt for AI Agents |
||
| const getCompiler = async ( | ||
| target: 'js' | 'c' | 'cpp' = 'js', | ||
| packages = true, | ||
| libraries?: unknown, | ||
| ) => { | ||
| const key = `${target}:${packages}:${JSON.stringify(libraries ?? null)}`; | ||
| let compiler = compilers.get(key); | ||
| if (!compiler) { | ||
| const created: Promise<any> = (async () => { | ||
| const entries = libraries | ||
| ? await (self as any).nimWasm.resolveLibraries(libraries, { cache: libraryCache }) | ||
| : {}; | ||
| return (self as any).nimWasm.createCompiler({ | ||
| target, | ||
| emit: 'single', | ||
| baseUrl: nimWasmBaseUrl + 'assets/nim/', | ||
| // An object is merged with the bundled registry; a user library therefore enables the | ||
| // package loader even if `packages` was false. | ||
| packages: Object.keys(entries).length ? entries : packages, | ||
| }); | ||
| })(); | ||
| // do not keep a failed boot (e.g. a transient network error) | ||
| created.catch(() => { | ||
| compilers[target] = null; | ||
| if (compilers.get(key) === created) compilers.delete(key); | ||
| }); | ||
| compilers[target] = created; | ||
| compilers.set(key, created); | ||
| compiler = created; | ||
| } | ||
| return compilers[target]; | ||
| return compiler; | ||
| }; | ||
| return async (code: string, { config }) => { | ||
| const target = getLanguageCustomSettings('nim', config)?.target || 'js'; | ||
| const result = await (await getCompiler(target)).run(code, '', { execute: false }); | ||
| const settings = getLanguageCustomSettings('nim', config) ?? {}; | ||
| const target = settings.target || 'js'; | ||
| const packages = settings.packages !== false; | ||
| let compiler: any; | ||
| try { | ||
| compiler = await getCompiler(target, packages, settings.libraries); | ||
| } catch (error) { | ||
| return { | ||
| code: '', | ||
| info: { | ||
| errors: [`Could not load Nim libraries: ${(error as Error)?.message ?? error}`], | ||
| }, | ||
| }; | ||
|
Comment on lines
+53
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Use an accurate error label when compiler initialization fails. If 🤖 Prompt for AI Agents |
||
| } | ||
| const result = await compiler.run(code, '', { execute: false }); | ||
| if (!result.compiledCode) { | ||
| return { code: '', info: { errors: result.errors || ['Nim produced no output.'] } }; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Document object entries in
librariesarrays.The type
string[] | objectexcludes arrays of objects, but Line 100 permits{ url, ref, … }entries in an array. The package also accepts that shape. Update this type and the matching table indocs/docs/languages/nim-wasm.mdxat Line 76 so users can identify the supported configuration. (github.com)🤖 Prompt for AI Agents