Parallel Letter Frequency
This commit is contained in:
parent
3ca9867a11
commit
af89290bbc
45 changed files with 2862 additions and 0 deletions
14
wasm/hello-world/.eslintrc
Normal file
14
wasm/hello-world/.eslintrc
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"root": true,
|
||||
"extends": "@exercism/eslint-config-javascript",
|
||||
"env": {
|
||||
"jest": true
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
|
||||
"excludedFiles": ["custom.spec.js"],
|
||||
"extends": "@exercism/eslint-config-javascript/maintainers"
|
||||
}
|
||||
]
|
||||
}
|
||||
28
wasm/hello-world/.exercism/config.json
Normal file
28
wasm/hello-world/.exercism/config.json
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"authors": [
|
||||
"bushidocodes"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"hello-world.wat"
|
||||
],
|
||||
"test": [
|
||||
"hello-world.spec.js"
|
||||
],
|
||||
"example": [
|
||||
".meta/proof.ci.wat"
|
||||
],
|
||||
"invalidator": [
|
||||
"package.json"
|
||||
]
|
||||
},
|
||||
"blurb": "Exercism's classic introductory exercise. Just say \"Hello, World!\".",
|
||||
"source": "This is an exercise to introduce users to using Exercism",
|
||||
"source_url": "https://en.wikipedia.org/wiki/%22Hello,_world!%22_program",
|
||||
"custom": {
|
||||
"version.tests.compatibility": "jest-27",
|
||||
"flag.tests.task-per-describe": false,
|
||||
"flag.tests.may-run-long": false,
|
||||
"flag.tests.includes-optional": false
|
||||
}
|
||||
}
|
||||
1
wasm/hello-world/.npmrc
Normal file
1
wasm/hello-world/.npmrc
Normal file
|
|
@ -0,0 +1 @@
|
|||
audit=false
|
||||
343
wasm/hello-world/HELP.md
Normal file
343
wasm/hello-world/HELP.md
Normal file
|
|
@ -0,0 +1,343 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
## Setup
|
||||
|
||||
Go through the setup [instructions for WebAssembly][docs-exercism-wasm] to install the necessary dependencies.
|
||||
|
||||
## Requirements
|
||||
|
||||
Install assignment dependencies:
|
||||
|
||||
```shell
|
||||
# Using npm
|
||||
npm install
|
||||
|
||||
# Alternatively using yarn
|
||||
yarn
|
||||
```
|
||||
|
||||
## Making the test suite pass
|
||||
|
||||
All exercises come with a test suite to help you validate your solution before submitting.
|
||||
You can execute these tests by opening a command prompt in the exercise's directory, and then running:
|
||||
|
||||
```bash
|
||||
# Using npm
|
||||
npm test
|
||||
|
||||
# Alternatively using yarn
|
||||
yarn test
|
||||
```
|
||||
|
||||
In some test suites all tests but the first have been skipped.
|
||||
|
||||
Once you get a test passing, you can enable the next one by changing `xtest` to `test`.
|
||||
|
||||
## Writing custom tests
|
||||
|
||||
If you wish to write additional, custom, tests, create a new file `custom.spec.js`, and submit it with your solution together with the new file:
|
||||
|
||||
```shell
|
||||
exercism submit numbers.wat custom.spec.js
|
||||
```
|
||||
|
||||
[docs-exercism-wasm]: https://exercism.org/docs/tracks/wasm/installation
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit hello-world.wat` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [WebAssembly track's documentation](https://exercism.org/docs/tracks/wasm)
|
||||
- The [WebAssembly track's programming category on the forum](https://forum.exercism.org/c/programming/wasm)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
To get help if you're having trouble, you can use one of the following resources:
|
||||
|
||||
- [/r/WebAssembly](https://www.reddit.com/r/WebAssembly/) is the WebAssembly subreddit.
|
||||
- [Github issue tracker](https://github.com/exercism/wasm/issues) is where we track our development and maintenance of Javascript exercises in exercism. But if none of the above links help you, feel free to post an issue here.
|
||||
|
||||
## How to Debug
|
||||
|
||||
Unlike many languages, WebAssembly code does not automatically have access to global resources such as the console. Such functionality instead must be provided as imports.
|
||||
|
||||
In order to provide `console.log` like functionality and a few other niceties, the Exercism WebAssembly track exposes a standard library of functions across all exercises.
|
||||
|
||||
These functions must be imported at the top of your WebAssembly module and then can be called from within your WebAssembly code.
|
||||
|
||||
The `log_mem_*` functions expect to be able to access the linear memory of your WebAssembly module. By default, this is private state, so to make this accessible, you must export your linear memory under the export name `mem`. This is accomplished as follows:
|
||||
|
||||
```wasm
|
||||
(memory (export "mem") 1)
|
||||
```
|
||||
|
||||
## Logging Locals and Globals
|
||||
|
||||
We provide logging functions for each of the primitive WebAssembly types. This is useful for logging global and local variables.
|
||||
|
||||
### log_i32_s - Log a 32-bit signed integer to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_i32_s" (func $log_i32_s (param i32)))
|
||||
(func $main
|
||||
;; logs -1
|
||||
(call $log_i32_s (i32.const -1))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_i32_u - Log a 32-bit unsigned integer to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_i32_u" (func $log_i32_u (param i32)))
|
||||
(func $main
|
||||
;; Logs 42 to console
|
||||
(call $log_i32_u (i32.const 42))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_i64_s - Log a 64-bit signed integer to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_i64_s" (func $log_i64_s (param i64)))
|
||||
(func $main
|
||||
;; Logs -99 to console
|
||||
(call $log_i32_u (i64.const -99))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_i64_u - Log a 64-bit unsigned integer to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_i64_u" (func $log_i64_u (param i64)))
|
||||
(func $main
|
||||
;; Logs 42 to console
|
||||
(call $log_i64_u (i32.const 42))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_f32 - Log a 32-bit floating point number to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_f32" (func $log_f32 (param f32)))
|
||||
(func $main
|
||||
;; Logs 3.140000104904175 to console
|
||||
(call $log_f32 (f32.const 3.14))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_f64 - Log a 64-bit floating point number to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_f64" (func $log_f64 (param f64)))
|
||||
(func $main
|
||||
;; Logs 3.14 to console
|
||||
(call $log_f64 (f64.const 3.14))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
## Logging from Linear Memory
|
||||
|
||||
WebAssembly Linear Memory is a byte-addressable array of values. This serves as the equivalent of virtual memory for WebAssembly programs
|
||||
|
||||
We provide logging functions to interpret a range of addresses within linear memory as static arrays of certain types. This acts similar to the TypedArrays of JavaScript. The length parameters are not measured in bytes. They are measured in the number of consecutive elements of the type associated with the function.
|
||||
|
||||
**In order for these functions to work, your WebAssembly module must declare and export its linear memory using the named export "mem"**
|
||||
|
||||
```wasm
|
||||
(memory (export "mem") 1)
|
||||
```
|
||||
|
||||
### log_mem_as_utf8 - Log a sequence of UTF8 characters to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_utf8" (func $log_mem_as_utf8 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(data (i32.const 64) "Goodbye, Mars!")
|
||||
(func $main
|
||||
;; Logs "Goodbye, Mars!" to console
|
||||
(call $log_mem_as_utf8 (i32.const 64) (i32.const 14))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_i8 - Log a sequence of signed 8-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_i8" (func $log_mem_as_i8 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(memory.fill (i32.const 128) (i32.const -42) (i32.const 10))
|
||||
;; Logs an array of 10x -42 to console
|
||||
(call $log_mem_as_u8 (i32.const 128) (i32.const 10))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_u8 - Log a sequence of unsigned 8-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_u8" (func $log_mem_as_u8 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(memory.fill (i32.const 128) (i32.const 42) (i32.const 10))
|
||||
;; Logs an array of 10x 42 to console
|
||||
(call $log_mem_as_u8 (i32.const 128) (i32.const 10))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_i16 - Log a sequence of signed 16-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_i16" (func $log_mem_as_i16 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(i32.store16 (i32.const 128) (i32.const -10000))
|
||||
(i32.store16 (i32.const 130) (i32.const -10001))
|
||||
(i32.store16 (i32.const 132) (i32.const -10002))
|
||||
;; Logs [-10000, -10001, -10002] to console
|
||||
(call $log_mem_as_i16 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_u16 - Log a sequence of unsigned 16-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_u16" (func $log_mem_as_u16 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(i32.store16 (i32.const 128) (i32.const 10000))
|
||||
(i32.store16 (i32.const 130) (i32.const 10001))
|
||||
(i32.store16 (i32.const 132) (i32.const 10002))
|
||||
;; Logs [10000, 10001, 10002] to console
|
||||
(call $log_mem_as_u16 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_i32 - Log a sequence of signed 32-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_i32" (func $log_mem_as_i32 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(i32.store (i32.const 128) (i32.const -10000000))
|
||||
(i32.store (i32.const 132) (i32.const -10000001))
|
||||
(i32.store (i32.const 136) (i32.const -10000002))
|
||||
;; Logs [-10000000, -10000001, -10000002] to console
|
||||
(call $log_mem_as_i32 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_u32 - Log a sequence of unsigned 32-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_u32" (func $log_mem_as_u32 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(i32.store (i32.const 128) (i32.const 100000000))
|
||||
(i32.store (i32.const 132) (i32.const 100000001))
|
||||
(i32.store (i32.const 136) (i32.const 100000002))
|
||||
;; Logs [100000000, 100000001, 100000002] to console
|
||||
(call $log_mem_as_u32 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_i64 - Log a sequence of signed 64-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_i64" (func $log_mem_as_i64 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(i64.store (i32.const 128) (i64.const -10000000000))
|
||||
(i64.store (i32.const 136) (i64.const -10000000001))
|
||||
(i64.store (i32.const 144) (i64.const -10000000002))
|
||||
;; Logs [-10000000000, -10000000001, -10000000002] to console
|
||||
(call $log_mem_as_i64 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_u64 - Log a sequence of unsigned 64-bit integers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_u64" (func $log_mem_as_u64 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(i64.store (i32.const 128) (i64.const 10000000000))
|
||||
(i64.store (i32.const 136) (i64.const 10000000001))
|
||||
(i64.store (i32.const 144) (i64.const 10000000002))
|
||||
;; Logs [10000000000, 10000000001, 10000000002] to console
|
||||
(call $log_mem_as_u64 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_u64 - Log a sequence of 32-bit floating point numbers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_f32" (func $log_mem_as_f32 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(func $main
|
||||
(f32.store (i32.const 128) (f32.const 3.14))
|
||||
(f32.store (i32.const 132) (f32.const 3.14))
|
||||
(f32.store (i32.const 136) (f32.const 3.14))
|
||||
;; Logs [3.140000104904175, 3.140000104904175, 3.140000104904175] to console
|
||||
(call $log_mem_as_u64 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### log_mem_as_f64 - Log a sequence of 64-bit floating point numbers to console
|
||||
|
||||
```wasm
|
||||
(module
|
||||
(import "console" "log_mem_as_f64" (func $log_mem_as_f64 (param $byteOffset i32) (param $length i32)))
|
||||
(memory (export "mem") 1)
|
||||
(f64.store (i32.const 128) (f64.const 3.14))
|
||||
(f64.store (i32.const 136) (f64.const 3.14))
|
||||
(f64.store (i32.const 144) (f64.const 3.14))
|
||||
;; Logs [3.14, 3.14, 3.14] to console
|
||||
(call $log_mem_as_u64 (i32.const 128) (i32.const 3))
|
||||
)
|
||||
)
|
||||
```
|
||||
21
wasm/hello-world/LICENSE
Normal file
21
wasm/hello-world/LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2021 Exercism
|
||||
|
||||
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.
|
||||
31
wasm/hello-world/README.md
Normal file
31
wasm/hello-world/README.md
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# Hello World
|
||||
|
||||
Welcome to Hello World on Exercism's WebAssembly Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
The classical introductory exercise.
|
||||
Just say "Hello, World!".
|
||||
|
||||
["Hello, World!"][hello-world] is the traditional first program for beginning programming in a new language or environment.
|
||||
|
||||
The objectives are simple:
|
||||
|
||||
- Modify the provided code so that it produces the string "Hello, World!".
|
||||
- Run the test suite and make sure that it succeeds.
|
||||
- Submit your solution and check it at the website.
|
||||
|
||||
If everything goes well, you will be ready to fetch your first real exercise.
|
||||
|
||||
[hello-world]: https://en.wikipedia.org/wiki/%22Hello,_world!%22_program
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @bushidocodes
|
||||
|
||||
### Based on
|
||||
|
||||
This is an exercise to introduce users to using Exercism - https://en.wikipedia.org/wiki/%22Hello,_world!%22_program
|
||||
4
wasm/hello-world/babel.config.js
Normal file
4
wasm/hello-world/babel.config.js
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export default {
|
||||
presets: ["@exercism/babel-preset-javascript"],
|
||||
plugins: [],
|
||||
};
|
||||
40
wasm/hello-world/hello-world.spec.js
Normal file
40
wasm/hello-world/hello-world.spec.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { compileWat, WasmRunner } from "@exercism/wasm-lib";
|
||||
|
||||
let wasmModule;
|
||||
let currentInstance;
|
||||
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
const watPath = new URL("./hello-world.wat", import.meta.url);
|
||||
const { buffer } = await compileWat(watPath);
|
||||
wasmModule = await WebAssembly.compile(buffer);
|
||||
} catch (err) {
|
||||
console.log(`Error compiling *.wat: \n${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
describe("Hello World", () => {
|
||||
beforeEach(async () => {
|
||||
currentInstance = null;
|
||||
|
||||
if (!wasmModule) {
|
||||
return Promise.reject();
|
||||
}
|
||||
try {
|
||||
currentInstance = await new WasmRunner(wasmModule);
|
||||
return Promise.resolve();
|
||||
} catch (err) {
|
||||
console.log(`Error instantiating WebAssembly module: ${err}`);
|
||||
return Promise.reject();
|
||||
}
|
||||
});
|
||||
|
||||
test("Say Hi!", () => {
|
||||
expect(currentInstance).toBeTruthy();
|
||||
const [offset, length] = currentInstance.exports.hello();
|
||||
expect(length).toBe(13);
|
||||
const greeting = currentInstance.get_mem_as_utf8(offset, length);
|
||||
expect(greeting).toBe("Hello, World!");
|
||||
});
|
||||
});
|
||||
16
wasm/hello-world/hello-world.wat
Normal file
16
wasm/hello-world/hello-world.wat
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(module
|
||||
(memory (export "mem") 1)
|
||||
|
||||
;; Initializes the WebAssembly Linear Memory with a UTF-8 string of 14 characters starting at offset 64
|
||||
(data (i32.const 64) "Hello, World!")
|
||||
|
||||
;;
|
||||
;; Return a greeting
|
||||
;;
|
||||
;; Note: The final number (currently “14”) must match the length of the new string!
|
||||
;;
|
||||
;; @returns {(i32, i32)} The offset and length of the greeting
|
||||
(func (export "hello") (result i32 i32)
|
||||
(i32.const 64) (i32.const 13)
|
||||
)
|
||||
)
|
||||
35
wasm/hello-world/package.json
Normal file
35
wasm/hello-world/package.json
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"name": "@exercism/wasm-hello-world",
|
||||
"description": "Exercism exercises in WebAssembly.",
|
||||
"author": "Sean McBride",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/exercism/wasm",
|
||||
"directory": "exercises/practice/hello-world"
|
||||
},
|
||||
"jest": {
|
||||
"maxWorkers": 1
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.23.3",
|
||||
"@exercism/babel-preset-javascript": "^0.4.0",
|
||||
"@exercism/eslint-config-javascript": "^0.6.0",
|
||||
"@types/jest": "^29.5.8",
|
||||
"@types/node": "^20.9.1",
|
||||
"babel-jest": "^29.7.0",
|
||||
"core-js": "^3.33.2",
|
||||
"eslint": "^8.54.0",
|
||||
"jest": "^29.7.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*",
|
||||
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*",
|
||||
"lint": "eslint ."
|
||||
},
|
||||
"dependencies": {
|
||||
"@exercism/wasm-lib": "^0.2.0"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue