Skip to content
Merged

Zig #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/ppp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"test": "npm run test:unit -- --run"
},
"dependencies": {
"@andrewbranch/untar.js": "^1.0.3",
"@xterm/addon-fit": "^0.11.0",
"@xterm/xterm": "^6.0.0",
"dotnet-runtime": "workspace:*",
Expand All @@ -37,7 +38,8 @@
"python-runtime": "workspace:*",
"ruby-runtime": "workspace:*",
"rust-runtime": "workspace:*",
"typescript-runtime": "workspace:*"
"typescript-runtime": "workspace:*",
"zig-runtime": "workspace:*"
},
"devDependencies": {
"@eslint/compat": "^2.0.5",
Expand Down
3 changes: 2 additions & 1 deletion apps/ppp/src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ type CachePrefix =
| 'php-cache@'
| 'python-cache@'
| 'ruby-cache@'
| 'rust-cache@';
| 'rust-cache@'
| 'zig-cache@';

export async function createCachedFetch(prefix: CachePrefix, key: string) {
return createFetch(await cache(prefix, await hashString(key)));
Expand Down
10 changes: 7 additions & 3 deletions apps/ppp/src/lib/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import VscodeIconsFileTypeGleam from '~icons/vscode-icons/file-type-gleam';
import VscodeIconsFileTypeCsharp from '~icons/vscode-icons/file-type-csharp';
import VscodeIconsFileTypeJava from '~icons/vscode-icons/file-type-java';
import VscodeIconsFileTypeRuby from '~icons/vscode-icons/file-type-ruby';
import VscodeIconsFileTypeZig from '~icons/vscode-icons/file-type-zig';

export enum Language {
JavaScript = 'javascript',
Expand All @@ -22,7 +23,8 @@ export enum Language {
Gleam = 'gleam',
CSharp = 'csharp',
Java = 'java',
Ruby = 'ruby'
Ruby = 'ruby',
Zig = 'zig'
}

export const LANGUAGES = Object.values(Language).sort();
Expand All @@ -37,7 +39,8 @@ export const LANGUAGE_TITLE: Record<Language, string> = {
[Language.Gleam]: 'Gleam',
[Language.CSharp]: 'CSharp',
[Language.Java]: 'Java',
[Language.Ruby]: 'Ruby'
[Language.Ruby]: 'Ruby',
[Language.Zig]: 'Zig'
};

export const LANGUAGE_ICONS: Record<Language, Component<SVGAttributes<SVGSVGElement>>> = {
Expand All @@ -50,7 +53,8 @@ export const LANGUAGE_ICONS: Record<Language, Component<SVGAttributes<SVGSVGElem
[Language.Gleam]: VscodeIconsFileTypeGleam,
[Language.CSharp]: VscodeIconsFileTypeCsharp,
[Language.Java]: VscodeIconsFileTypeJava,
[Language.Ruby]: VscodeIconsFileTypeRuby
[Language.Ruby]: VscodeIconsFileTypeRuby,
[Language.Zig]: VscodeIconsFileTypeZig
};

export function isLanguage(lang: string): lang is Language {
Expand Down
96 changes: 53 additions & 43 deletions apps/ppp/src/lib/monaco.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
import { loadWASM } from "onigasm";
import * as monaco from "monaco-editor";
import { Registry } from "monaco-textmate";
import { wireTmGrammars } from "monaco-editor-textmate";
import { loadWASM } from 'onigasm';
import * as monaco from 'monaco-editor';
import { Registry } from 'monaco-textmate';
import { wireTmGrammars } from 'monaco-editor-textmate';

import onigasmWasmUrl from "onigasm/lib/onigasm.wasm?url";
import onigasmWasmUrl from 'onigasm/lib/onigasm.wasm?url';

import { Language } from "$lib/language";
import gleamConfiguration from "$lib/gleam/language-configuration";
import gleamGrammarUrl from "$lib/gleam/gleam.tmLanguage.json?url";
import { Language } from '$lib/language';
import gleamConfiguration from '$lib/gleam/language-configuration';
import gleamGrammarUrl from '$lib/gleam/gleam.tmLanguage.json?url';
import zigConfiguration from '$lib/zig/language-configuration';
import zigGrammarUrl from '$lib/zig/zig.tmLanguage.json?url';

export const MONACO_LANGUAGE_ID: Record<Language, string> = {
[Language.PHP]: "php",
[Language.TypeScript]: "typescript",
[Language.JavaScript]: "javascript",
[Language.Python]: "python",
[Language.Go]: "go",
[Language.Rust]: "rust",
[Language.Gleam]: Language.Gleam,
[Language.CSharp]: "csharp",
[Language.Java]: "java",
[Language.Ruby]: "ruby",
[Language.PHP]: 'php',
[Language.TypeScript]: 'typescript',
[Language.JavaScript]: 'javascript',
[Language.Python]: 'python',
[Language.Go]: 'go',
[Language.Rust]: 'rust',
[Language.Gleam]: Language.Gleam,
[Language.CSharp]: 'csharp',
[Language.Java]: 'java',
[Language.Ruby]: 'ruby',
[Language.Zig]: Language.Zig
};

const LANGUAGE_ID_SCOPE_NAME = {
[Language.Gleam]: "source.gleam",
};

monaco.languages.register({ id: Language.Gleam });
monaco.languages.setLanguageConfiguration(Language.Gleam, gleamConfiguration);
const SCOPE_NAME_META = new Map<
string,
{ lang: Language; grammarUrl: string; config: monaco.languages.LanguageConfiguration }
>([
[
'source.gleam',
{ lang: Language.Gleam, grammarUrl: gleamGrammarUrl, config: gleamConfiguration }
],
['source.zig', { lang: Language.Zig, grammarUrl: zigGrammarUrl, config: zigConfiguration }]
]);

for (const [, meta] of SCOPE_NAME_META) {
monaco.languages.register({ id: meta.lang });
monaco.languages.setLanguageConfiguration(meta.lang, meta.config);
}

export async function loadTmGrammars() {
await loadWASM(onigasmWasmUrl);

const registry = new Registry({
getGrammarDefinition: async (scopeName) => {
switch (scopeName) {
case LANGUAGE_ID_SCOPE_NAME[Language.Gleam]:
return {
format: "json",
content: await (await fetch(gleamGrammarUrl)).json(),
};
default:
throw new Error(`Unknown scope name: ${scopeName}`);
}
},
});

const grammars = new Map(Object.entries(LANGUAGE_ID_SCOPE_NAME));

return wireTmGrammars(monaco, registry, grammars);
await loadWASM(onigasmWasmUrl);

const registry = new Registry({
getGrammarDefinition: async (scopeName) => {
const config = SCOPE_NAME_META.get(scopeName);
if (!config) {
throw new Error(`Unknown scope name: ${scopeName}`);
}
return {
format: 'json',
content: await fetch(config.grammarUrl).then((r) => r.json())
};
}
});

const grammars = new Map(SCOPE_NAME_META.entries().map(([scope, meta]) => [meta.lang, scope]));
return wireTmGrammars(monaco, registry, grammars);
}
46 changes: 24 additions & 22 deletions apps/ppp/src/lib/runtime/descriptions.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import type { Component } from "svelte";
import type { Component } from 'svelte';

import { Language } from "$lib/language";
import { Language } from '$lib/language';

import JsDescription from "./js/description.svelte";
import TsDescription from "./ts/description.svelte";
import PhpDescription from "./php/description.svelte";
import PyDescription from "./python/description.svelte";
import GoDescription from "./go/description.svelte";
import RustDescription from "./rust/description.svelte";
import GleamDescription from "./gleam/description.svelte";
import DotnetDescription from "./dotnet/description.svelte";
import JavaDescription from "./java/description.svelte";
import RubyDescription from "./ruby/description.svelte";
import JsDescription from './js/description.svelte';
import TsDescription from './ts/description.svelte';
import PhpDescription from './php/description.svelte';
import PyDescription from './python/description.svelte';
import GoDescription from './go/description.svelte';
import RustDescription from './rust/description.svelte';
import GleamDescription from './gleam/description.svelte';
import DotnetDescription from './dotnet/description.svelte';
import JavaDescription from './java/description.svelte';
import RubyDescription from './ruby/description.svelte';
import ZigDescription from './zig/description.svelte';

export const DESCRIPTIONS: Record<Language, Component> = {
[Language.JavaScript]: JsDescription,
[Language.TypeScript]: TsDescription,
[Language.PHP]: PhpDescription,
[Language.Python]: PyDescription,
[Language.Go]: GoDescription,
[Language.Rust]: RustDescription,
[Language.Gleam]: GleamDescription,
[Language.CSharp]: DotnetDescription,
[Language.Java]: JavaDescription,
[Language.Ruby]: RubyDescription,
[Language.JavaScript]: JsDescription,
[Language.TypeScript]: TsDescription,
[Language.PHP]: PhpDescription,
[Language.Python]: PyDescription,
[Language.Go]: GoDescription,
[Language.Rust]: RustDescription,
[Language.Gleam]: GleamDescription,
[Language.CSharp]: DotnetDescription,
[Language.Java]: JavaDescription,
[Language.Ruby]: RubyDescription,
[Language.Zig]: ZigDescription
};
46 changes: 24 additions & 22 deletions apps/ppp/src/lib/runtime/test-descriptions.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import type { Component } from "svelte";
import type { Component } from 'svelte';

import { Language } from "$lib/language";
import { Language } from '$lib/language';

import JsDescription from "./js/description.svelte";
import TsDescription from "./ts/description.svelte";
import PhpDescription from "./php/description.svelte";
import PyDescription from "./python/description.svelte";
import GoDescription from "./go/description.svelte";
import RustDescription from "./rust/description.svelte";
import GleamDescription from "./gleam/description.svelte";
import DotnetDescription from "./dotnet/test-description.svelte";
import JavaDescription from "./java/test-description.svelte";
import RubyDescription from "./ruby/description.svelte";
import JsDescription from './js/description.svelte';
import TsDescription from './ts/description.svelte';
import PhpDescription from './php/description.svelte';
import PyDescription from './python/description.svelte';
import GoDescription from './go/description.svelte';
import RustDescription from './rust/description.svelte';
import GleamDescription from './gleam/description.svelte';
import DotnetDescription from './dotnet/test-description.svelte';
import JavaDescription from './java/test-description.svelte';
import RubyDescription from './ruby/description.svelte';
import ZigDescription from './zig/description.svelte';

export const DESCRIPTIONS: Record<Language, Component> = {
[Language.JavaScript]: JsDescription,
[Language.TypeScript]: TsDescription,
[Language.PHP]: PhpDescription,
[Language.Python]: PyDescription,
[Language.Go]: GoDescription,
[Language.Rust]: RustDescription,
[Language.Gleam]: GleamDescription,
[Language.CSharp]: DotnetDescription,
[Language.Java]: JavaDescription,
[Language.Ruby]: RubyDescription,
[Language.JavaScript]: JsDescription,
[Language.TypeScript]: TsDescription,
[Language.PHP]: PhpDescription,
[Language.Python]: PyDescription,
[Language.Go]: GoDescription,
[Language.Rust]: RustDescription,
[Language.Gleam]: GleamDescription,
[Language.CSharp]: DotnetDescription,
[Language.Java]: JavaDescription,
[Language.Ruby]: RubyDescription,
[Language.Zig]: ZigDescription
};
58 changes: 58 additions & 0 deletions apps/ppp/src/lib/runtime/zig/compiler-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { untar } from '@andrewbranch/untar.js';
import type { CompilerFactory, Program } from 'libs/compiler';
import type { Streams } from 'libs/io';
import { createLogger } from 'libs/logger';
import { createCompilerWASI, createProgramWASI, ZigCompiler, ZigProgram } from 'zig-runtime';

import zigWasmUrl from 'zig-runtime/zig.wasm?url';
import compilerRtUrl from 'zig-runtime/lib/libcompiler_rt.a?url';
import stdLibUrl from 'zig-runtime/lib/zig.tar.gz?url';

import { createCachedFetch } from '$lib/fetch';

export const makeZigCompiler: CompilerFactory<Streams, Program> = async (ctx, streams) => {
const logger = createLogger(streams.out);
const fetcher = await createCachedFetch(
'zig-cache@',
`${zigWasmUrl}|${compilerRtUrl}|${stdLibUrl}`
);
async function fetch<R>(url: string, action: (r: Response) => R): Promise<R> {
const response = await fetcher(url, { signal: ctx.signal });
const result = await action(response);
logger.info(`Loaded ${url}`);
return result;
}
const [zigWasmModule, compilerRtArrayBuffer, stdLibFiles] = await Promise.all([
fetch(zigWasmUrl, (r) => WebAssembly.compileStreaming(r)),
fetch(compilerRtUrl, (r) => r.arrayBuffer()),
fetch(stdLibUrl, async (r) => {
let arrayBuffer = await r.arrayBuffer();
const magicNumber = new Uint8Array(arrayBuffer).slice(0, 2);
if (magicNumber[0] == 0x1f && magicNumber[1] == 0x8b) {
const ds = new DecompressionStream('gzip');
const response = new Response(new Response(arrayBuffer).body!.pipeThrough(ds));
arrayBuffer = await response.arrayBuffer();
} else {
// already decompressed
}
return untar(arrayBuffer);
})
]);
const compilerWasi = createCompilerWASI(streams, compilerRtArrayBuffer, stdLibFiles);
const programWasi = createProgramWASI(streams);
const compiler = new ZigCompiler(compilerWasi, zigWasmModule);
return {
async compile(ctx, files) {
if (files.length !== 1) {
throw new Error('Compilation of multiple files is not implemented');
}
const program = await compiler.compile(ctx, files[0].content);
return new ZigProgram(
programWasi,
program.buffer instanceof ArrayBuffer
? (program as Uint8Array<ArrayBuffer>)
: new Uint8Array(program)
);
}
};
};
9 changes: 9 additions & 0 deletions apps/ppp/src/lib/runtime/zig/description.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>Your code is compiled to wasm and executed in a web worker environment.</p>

<p>
The solution is based on an approach from <a
class="link"
target="_blank"
href="https://github.com/zigtools/playground">Zigtools Playground</a
>
</p>
6 changes: 6 additions & 0 deletions apps/ppp/src/lib/runtime/zig/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { startCompilerActor } from 'libs/compiler/actor';
import { createContext } from 'libs/context';

import { makeZigCompiler } from './compiler-factory';

startCompilerActor(createContext(), makeZigCompiler);
21 changes: 21 additions & 0 deletions apps/ppp/src/lib/zig/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Marc Tiehuis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions apps/ppp/src/lib/zig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Credits

This files are extracted from the [vscode-zig](https://codeberg.org/ziglang/vscode-zig) with MIT license.
Loading
Loading