-
-
Notifications
You must be signed in to change notification settings - Fork 197
Feat/add zig wasm #853
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
base: develop
Are you sure you want to change the base?
Feat/add zig wasm #853
Conversation
β Deploy Preview for livecodes ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Great work (as usual!), @Seth0x41 Re: const isSharedArrayBufferSupported = typeof SharedArrayBuffer !== 'undefined';When I enable it, I get an error which seems to be related to this code Re: cache Re: formatting I have some relatively minor comments. I will add these on code. Please run Thank you :) |
| export const cppWasmBaseUrl = /* @__PURE__ */ getUrl('@chriskoch/[email protected]/'); | ||
|
|
||
| export const csharpWasmBaseUrl = /* @__PURE__ */ getUrl('@seth0x41/[email protected]/'); | ||
| export const zigWasmBaseUrl = /* @__PURE__ */ getUrl('@seth0x41/[email protected]/'); |
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.
This file is sorted alphabetically to easily find the variables.
Please move this to the end of the file.
| const JS_UNTAR_URL = 'https://unpkg.com/[email protected]/build/dist/untar.js'; | ||
| const WASI_SHIM_URL = 'https://unpkg.com/@bjorn3/[email protected]/dist/index.js'; | ||
|
|
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.
Please move these to vendors.ts.
Also use the getUrl function to allow dynamically using the selected CDN.
e.g.
const jsUntarUrl = /* @__PURE__ */ getUrl('[email protected]/build/dist/untar.js');
| livecodes.zig ??= {}; | ||
|
|
||
| // check if SharedArrayBuffer supported by the browser else it will use ArrayBuffer | ||
| const isSharedArrayBufferSupported = typeof SharedArrayBuffer !== 'undefined' && window.crossOriginIsolated; |
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.
We can remove checking for corssOriginIsolated.
| const isSharedArrayBufferSupported = typeof SharedArrayBuffer !== 'undefined' && window.crossOriginIsolated; | |
| const isSharedArrayBufferSupported = typeof SharedArrayBuffer !== 'undefined'; |
| const loadScript = (url: string): Promise<void> => | ||
| new Promise<void>((resolve, reject) => { | ||
| const script = document.createElement('script'); | ||
| script.src = url; | ||
| script.async = true; | ||
| script.onload = () => resolve(); | ||
| script.onerror = () => reject(new Error(`Failed to load ${url}`)); | ||
| document.head.appendChild(script); | ||
| }); | ||
|
|
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.
We already have a util function for that here: https://github.com/Seth0x41/livecodes/blob/dbfc085b293b2b6594f7625ddd5e0ab5719e665d/src/livecodes/utils/utils.ts#L175
await loadScript(url, 'untar');| if (!(window as any).WASI) { | ||
| const wasiModule = await import(WASI_SHIM_URL); | ||
| ['WASI', 'File', 'Directory', 'OpenFile', 'PreopenDirectory', 'ConsoleStdout'].forEach( | ||
| (key) => wasiModule[key] && ((window as any)[key] = wasiModule[key]) |
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.
I prefer not adding many global variables so that we do not pollute the user environment.
Please keep this in a local variable scoped inside the script.
If you really need to add global variables, please add them to window.livecodes.
| const fileData = isSharedArrayBufferSupported | ||
| ? createFile(entry.buffer) | ||
| : new File(entry.buffer); | ||
|
|
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.
createFile already checks for isSharedArrayBufferSupported and uses the same logic.
| const fileData = isSharedArrayBufferSupported | |
| ? createFile(entry.buffer) | |
| : new File(entry.buffer); | |
| const fileData = createFile(entry.buffer); |
| extensions: ['zig'], | ||
| editor: 'script', | ||
| largeDownload: true, | ||
| }; |
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.
We are not using prettier for formatting.
We should use formatter key instead and use ZLS there.
| extensions: ['zig'], | ||
| editor: 'script', | ||
| largeDownload: true, | ||
| }; |
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.
This is used as a label for the language in the UI for compiler code viewer.
Let's keep it zig.
| }; | |
| compiledCodeLanguage: 'zig', |
| extensions: ['zig'], | ||
| editor: 'script', | ||
| largeDownload: true, | ||
| }; |
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.
ZLS will provide highlighting for codemirror editor only.
We can tell the code editor to use syntax highlighting for Rust which has a relatively similar syntax.
We can then override that in codemirror.
We have to add rust to the Language type.
| }; | |
| editor: 'script', | |
| editorLanguage: 'rust', |
| prologStarter, | ||
| blocklyStarter, | ||
| diagramsStarter, | ||
| zigWasmStarter, |
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.
let's put it after C++
I try to keep related languages together.
|
I found this: https://github.com/PlasmoHQ/prettier-plugin-zig |
| content: ` | ||
| <div class="container"> | ||
| <h1>Hello, <span id="name">World</span>!</h1> | ||
| <img class="logo" alt="logo" src="http://127.0.0.1:8080/livecodes/assets/templates/zig.svg" /> |
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.
We need to use baseUrl instead of absolute path for images in templates
| <img class="logo" alt="logo" src="http://127.0.0.1:8080/livecodes/assets/templates/zig.svg" /> | |
| <img class="logo" alt="logo" src="{{ __livecodes_baseUrl__ }}assets/templates/zig.svg" /> |
| name: 'zig-wasm', | ||
| title: 'Zig (Wasm) Starter', |
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.
I think we can just call the starter template zig.
| name: 'zig-wasm', | |
| title: 'Zig (Wasm) Starter', | |
| name: 'zig', | |
| title: 'Zig Starter', |
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.
We would then need to change the TS type and other mentions of the template name (e.g. in docs, language-info and command menu) to use the new template name.
|


What type of PR is this? (check all applicable)
Description
Notes:
The current code doesn't support ZLS yet, but we're working on adding support for it together. Right now, the code uses SharedArrayBuffer if the browser supports it, and falls back to ArrayBuffer if it doesn't.
This means ZLS can't be used yet, but the server can still run normally.
The second point is that I used a simple caching mechanism for lazy loading, to reduce lookup time to O(1).
The source code is converted to a 32-bit integer and used as a cache key.
I used 31 as a prime number because it provides good hash distribution, helps avoid collisions, and is fast .. Java uses it in hashCode.
0x7fffffff is a 31-bit positive integer that keeps hash values positive and avoids overflow issues.
Third point, I havenβt added a formatter yet. Do you have any suggestions for one?
Looking forward to hearing your feedback. :)
Related Tickets & Documents
Mobile & Desktop Screenshots/Recordings
Added tests?
Added to documentations?
[optional] Are there any post-deployment tasks we need to perform?
[optional] What gif best describes this PR or how it makes you feel?