Skip to content

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 10 commits into from
May 9, 2023
Merged
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
3 changes: 3 additions & 0 deletions modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
"remote_execution": {
"tabs": []
},
"wasm": {
"tabs": []
},
"arcade_2d": {
"tabs": [
"ArcadeTwod"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
"react-ace": "^10.1.0",
"react-dom": "^17.0.2",
"regl": "^2.1.0",
"source-academy-utils": "^1.0.0",
"source-academy-wabt": "^1.0.4",
"tslib": "^2.3.1"
},
"jest": {
Expand Down
87 changes: 87 additions & 0 deletions src/bundles/wasm/index.ts
Copy link
Member

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:

image

Click "Select End of Line Sequence" and change it to CRLF, then commit the changes.

Copy link
Contributor Author

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)!

Copy link
Contributor

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.
Screenshot 2023-04-14 141923
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

Copy link
Contributor

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

Copy link
Contributor

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:

  • Rely on each user's git core.autocrlf config to handle line endings across platforms (true on Windows, input on Linux/macOS?)
  • Turn off the linebreak-style rule in the base eslint config. Users are free to locally checkout LF or CRLF
  • Push a commit that converts all existing files to be indexed with LF (could use something like eol-converter-cli on npm)

From there I think the repo may sometimes get CRLF introduced, but they would sometimes get corrected to LF down the line. Any thoughts?

Copy link
Contributor Author

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

Copy link
Member

@martin-henz martin-henz Apr 26, 2023

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

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';
23 changes: 23 additions & 0 deletions src/bundles/wasm/wabt.ts
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);
};
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,11 @@ cjs-module-lexer@^1.0.0:
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz#9f84ba3244a512f3a54e5277e8eef4c489864e40"
integrity sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==

class-transformer@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.5.1.tgz#24147d5dffd2a6cea930a3250a677addf96ab336"
integrity sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==

classnames@^2.3.1:
version "2.3.2"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.2.tgz#351d813bf0137fcc6a76a16b88208d2560a0d924"
Expand Down Expand Up @@ -4938,6 +4943,11 @@ readable-stream@~2.3.6:
string_decoder "~1.1.1"
util-deprecate "~1.0.1"

reflect-metadata@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==

regenerator-runtime@^0.13.11:
version "0.13.11"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
Expand Down Expand Up @@ -5206,6 +5216,21 @@ socks@^2.6.2:
ip "^2.0.0"
smart-buffer "^4.2.0"

source-academy-utils@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/source-academy-utils/-/source-academy-utils-1.0.2.tgz#fc35a4e21e6e6a14743ed560978a9701af1a8bdc"
integrity sha512-cSx/Rxr0CEOr+KJKILKicOVSVknG82fMEozaituD5mjh92przLW8C4kafzXrfGMjPVb6p7lxFMk5S6QyiYI2/g==

source-academy-wabt@^1.0.4:
version "1.0.10"
resolved "https://registry.yarnpkg.com/source-academy-wabt/-/source-academy-wabt-1.0.10.tgz#4187804a10b8233dc0f3498b49d07523940b4789"
integrity sha512-eRm9Q+wm9rNKpaX3X+ykKjcLyrV2O6elAIG3qmkuOeOLk3f26QEFfroBvNxLtvVokkItWRHek9T/d5Gqrqo5tQ==
dependencies:
class-transformer "^0.5.1"
lodash "^4.17.21"
reflect-metadata "^0.1.13"
typescript "4"

source-map-support@0.5.13:
version "0.5.13"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
Expand Down Expand Up @@ -5584,6 +5609,11 @@ typedoc@^0.23.23:
minimatch "^6.1.6"
shiki "^0.14.1"

typescript@4:
version "4.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a"
integrity sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==

typescript@4.8:
version "4.8.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
Expand Down