Skip to content

Commit ec0daed

Browse files
refactor(lang/js): Revise JavaScript language support section (#304)
* Revise JavaScript language support section As with other sections, added more details and factored out code examples into other files. * Fixed two errors in `jco componentize` command The world name was wrong in one of the commands, and "--disable all" should be "--disable=all" (at least with a recent jco version) * Added omitted steps of creating package.json files (without which we get "Cannot use import statement outside a module" error) * Fixed missing semicolon in component.wit file for string-reverse example * Corrected string-reverse example to use split() and join() (the example didn't run before) * Corrected string-reverse-uppercase example to declare reverseAndUppercase(s) rather than reverseAndUppercase() (this example also didn't run before). * Update component-model/src/language-support/javascript.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/javascript.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/javascript.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/javascript.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/javascript.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Update component-model/src/language-support/javascript.md Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> * Re-add line breaks in jco commands --------- Co-authored-by: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com>
1 parent d058d0c commit ec0daed

File tree

11 files changed

+354
-229
lines changed

11 files changed

+354
-229
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const add = {
2+
add(x, y) {
3+
return x + y;
4+
}
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "adder-wasm",
3+
"description": "Simple codebase for compiling an add interface to WebAssembly with jco",
4+
"type": "module"
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { add } from "./dist/transpiled/adder.js";
2+
3+
console.log("1 + 2 = " + add.add(1, 2));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package example:string-reverse-upper@0.1.0;
2+
3+
@since(version = 0.1.0)
4+
interface reversed-upper {
5+
reverse-and-uppercase: func(s: string) -> string;
6+
}
7+
8+
world revup {
9+
//
10+
// NOTE, the import below translates to:
11+
// <namespace>:<package>/<interface>@<package version>
12+
//
13+
import example:string-reverse/reverse@0.1.0;
14+
15+
export reversed-upper;
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* If this import listed below is missing, please run
3+
*
4+
* ```
5+
* npm run build && npm run compose && npm run transpile`
6+
* ```
7+
*/
8+
import { reversedUpper } from "./dist/transpiled/string-reverse-upper.js";
9+
10+
const result = reversedUpper.reverseAndUppercase("!dlroW olleH");
11+
12+
console.log(`reverseAndUppercase('!dlroW olleH') = ${result}`);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* This module is the JS implementation of the `revup` WIT world
3+
*/
4+
5+
/**
6+
* The import here is *virtual*. It refers to the `import`ed `reverse` interface in component.wit.
7+
*
8+
* These types *do not resolve* when the first `string-reverse-upper` component is built,
9+
* but the types are relevant for the resulting *composed* component.
10+
*/
11+
import { reverseString } from 'example:string-reverse/reverse@0.1.0';
12+
13+
/**
14+
* The JavaScript export below represents the export of the `reversed-upper` interface,
15+
* which which contains `revup` as it's primary exported function.
16+
*/
17+
export const reversedUpper = {
18+
/**
19+
* Represents the implementation of the `reverse-and-uppercase` function in the `reversed-upper` interface
20+
*
21+
* This function makes use of `reverse-string` which is *imported* from another WebAssembly binary.
22+
*/
23+
reverseAndUppercase(s) {
24+
return reverseString(s).toLocaleUpperCase();
25+
},
26+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package example:string-reverse@0.1.0;
2+
3+
@since(version = 0.1.0)
4+
interface reverse {
5+
reverse-string: func(s: string) -> string;
6+
}
7+
8+
world string-reverse {
9+
export reverse;
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "string-reverse",
3+
"description": "Simple codebase for compiling a string reversing interface to WebAssembly with jco",
4+
"type": "module"
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// If this import listed below is missing, please run `npm run transpile`
2+
import { reverse } from "./dist/transpiled/string-reverse.js";
3+
4+
const reversed = reverse.reverseString("!dlroW olleH");
5+
6+
console.log(`reverseString('!dlroW olleH') = ${reversed}`);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* This module is the JS implementation of the `string-reverse` WIT world
3+
*/
4+
5+
/**
6+
* The JavaScript export below represents the export of the `reverse` interface,
7+
* which which contains `reverse-string` as its primary exported function.
8+
*/
9+
export const reverse = {
10+
/**
11+
* This JavaScript will be interpreted by `jco` and turned into a
12+
* WebAssembly binary with a single export (this `reverse` function).
13+
*/
14+
reverseString(s) {
15+
return s.split("")
16+
.reverse()
17+
.join("");
18+
}
19+
};

0 commit comments

Comments
 (0)