-
Notifications
You must be signed in to change notification settings - Fork 31
WebAssembly Module #204
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
Merged
Merged
WebAssembly Module #204
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
706b4aa
minimal wasm MVP
Yongbeom-Kim 980ba22
Add comments for module methods
Yongbeom-Kim a3c96dc
Add documentation
Yongbeom-Kim 344abb9
Change line endings to windows
Yongbeom-Kim d8ddc16
Fix eslint
Yongbeom-Kim 83128d2
Update dependency
Yongbeom-Kim 37d1f54
Merge branch 'master' into master
Yongbeom-Kim acddad3
replace line ending
Yongbeom-Kim 128a08b
Merge branch 'master' of https://github.com/Yongbeom-Kim/source-acade…
Yongbeom-Kim e92f640
Merge branch 'master' into master
Yongbeom-Kim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,9 @@ | |
"remote_execution": { | ||
"tabs": [] | ||
}, | ||
"wasm": { | ||
"tabs": [] | ||
}, | ||
"arcade_2d": { | ||
"tabs": [ | ||
"ArcadeTwod" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* WebAssembly Module for Source Academy, for developing with WebAssembly Text and Binaries. | ||
* | ||
* Examples: | ||
* Simple 'add' library: | ||
* ```wat | ||
* import {wcompile, wrun} from "wasm"; | ||
* | ||
* const program = ` | ||
* (module | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.add) | ||
* (export "add" (func 0)) | ||
* ) | ||
* `; | ||
* | ||
* | ||
* const binary = wcompile(program); | ||
* const exports = wrun(binary); | ||
* | ||
* display(binary); | ||
* display_list(exports); | ||
* | ||
* const add_fn = head(tail(exports)); | ||
* | ||
* display(add_fn(10, 35)); | ||
* ``` | ||
* | ||
* 'Calculator Language': | ||
* ```wat | ||
* // Type your program in here! | ||
* import {wcompile, wrun} from "wasm"; | ||
* | ||
* const program = ` | ||
* (module | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.add) | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.sub) | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.mul) | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.div) | ||
* (export "add" (func 0)) | ||
* (export "sub" (func 1)) | ||
* (export "mul" (func 2)) | ||
* (export "div" (func 3)) | ||
* )`; | ||
* | ||
* | ||
* const encoding = wcompile(program); | ||
* let exports = wrun(encoding); | ||
* | ||
* display_list(exports); | ||
* | ||
* const div = head(tail(exports)); | ||
* exports = tail(tail(exports)); | ||
* const mul = head(tail(exports)); | ||
* exports = tail(tail(exports)); | ||
* const sub = head(tail(exports)); | ||
* exports = tail(tail(exports)); | ||
* const add = head(tail(exports)); | ||
* | ||
* display(div(10, -35)); | ||
* display(mul(10, 12347)); | ||
* display(sub(12347, 12347)); | ||
* display(add(10, 0)); | ||
* ``` | ||
* | ||
* | ||
* @module wasm | ||
* @author Kim Yongbeom | ||
*/ | ||
export { | ||
wcompile, | ||
wrun, | ||
} from './wabt'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { compile } from 'source-academy-wabt'; | ||
import { objectToLinkedList } from 'source-academy-utils'; | ||
|
||
/** | ||
* Compile a (hopefully valid) WebAssembly Text module to binary. | ||
* @param program program to compile | ||
* @returns an array of 8-bit unsigned integers. | ||
*/ | ||
export const wcompile = (program: string) => Array.from(compile(program)); | ||
|
||
/** | ||
* Run a compiled WebAssembly Binary Buffer. | ||
* @param buffer an array of 8-bit unsigned integers to run | ||
* @returns a linked list of exports that the relevant WebAssembly Module exports | ||
*/ | ||
export const wrun = (buffer: number[] | Uint8Array) => { | ||
if (buffer instanceof Array) { | ||
buffer = new Uint8Array(buffer); | ||
} | ||
|
||
const exps = new WebAssembly.Instance(new WebAssembly.Module(buffer)).exports; | ||
return objectToLinkedList(exps); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you change the line-encoding of this file to CRLF instead of LF, in order to match the rest of the codebase?
It's a bit weird, but this repository uses CRLF encoding for files, even on Unix systems.
To change the line encoding, open the file on VSCode, the on the bottom right toolbar:
Click "Select End of Line Sequence" and change it to CRLF, then commit the changes.
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.
Thanks for the review!
I've changed the relevant settings on VS Code and fixed all eslint issues, which should have fixed the problem? I'm not entirely sure if the problem is gone, but eslint isn't giving me any warnings so I take it that the issue is fixed.
Please let me know if there's anything else (or if the issue persists)!
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 don't think the line-encoding is updated in the GitHub repository, it is only updated on your local files. You can check if there are warnings by refering to the GitHub Checks.

I think your
git config core.autocrlf
is set to true, so git will replace CRLF with the line encoding in the GitHub repository automatically, which is LF. Which is why it is not updated on the GitHub repository, even if you change it locally.I faced a similar problem with the line-encodings, but I was able to fix it by referring to this: https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf
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.
At some point someone configured the repository to use CRLF instead of LF (I believe it was @Cloud7050) within the eslint configuration, I've since left that as is. If we want to move back to LF it would be some minor changes to the build system
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.
Using
git ls-files --eol
, this repo appears to sometimes use LF and sometimes use CRLF in the index (and some files are even mixed). For comparison, the main js-slang repo appears to mainly use LF.This repo's base eslint config is based on a config I've been carrying with me for a while. As I'm on Windows, that rule's setting is less of a concious choice for this repo. If I'm understanding this correctly, maybe it'll be better to:
core.autocrlf
config to handle line endings across platforms (true on Windows, input on Linux/macOS?)linebreak-style
rule in the base eslint config. Users are free to locally checkout LF or CRLFFrom there I think the repo may sometimes get CRLF introduced, but they would sometimes get corrected to LF down the line. Any thoughts?
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.
Thanks for the help with the line-encoding issue - I've fixed the issue with this PR, let me move this discussion to a fresh issue. #209
Uh oh!
There was an error while loading. Please reload this page.
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.
Is this PR ready to be merged? @Yongbeom-Kim