Skip to content
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
5 changes: 5 additions & 0 deletions component-model/examples/tutorial/js/adder/adder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const add = {
add(x, y) {
return x + y;
}
};
5 changes: 5 additions & 0 deletions component-model/examples/tutorial/js/adder/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "adder-wasm",
"description": "Simple codebase for compiling an add interface to WebAssembly with jco",
"type": "module"
}
3 changes: 3 additions & 0 deletions component-model/examples/tutorial/js/adder/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { add } from "./dist/transpiled/adder.js";

console.log("1 + 2 = " + add.add(1, 2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example:string-reverse-upper@0.1.0;

@since(version = 0.1.0)
interface reversed-upper {
reverse-and-uppercase: func(s: string) -> string;
}

world revup {
//
// NOTE, the import below translates to:
// <namespace>:<package>/<interface>@<package version>
//
import example:string-reverse/reverse@0.1.0;

export reversed-upper;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* If this import listed below is missing, please run
*
* ```
* npm run build && npm run compose && npm run transpile`
* ```
*/
import { reversedUpper } from "./dist/transpiled/string-reverse-upper.js";

const result = reversedUpper.reverseAndUppercase("!dlroW olleH");

console.log(`reverseAndUppercase('!dlroW olleH') = ${result}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This module is the JS implementation of the `revup` WIT world
*/

/**
* The import here is *virtual*. It refers to the `import`ed `reverse` interface in component.wit.
*
* These types *do not resolve* when the first `string-reverse-upper` component is built,
* but the types are relevant for the resulting *composed* component.
*/
import { reverseString } from 'example:string-reverse/reverse@0.1.0';

/**
* The JavaScript export below represents the export of the `reversed-upper` interface,
* which which contains `revup` as it's primary exported function.
*/
export const reversedUpper = {
/**
* Represents the implementation of the `reverse-and-uppercase` function in the `reversed-upper` interface
*
* This function makes use of `reverse-string` which is *imported* from another WebAssembly binary.
*/
reverseAndUppercase(s) {
return reverseString(s).toLocaleUpperCase();
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package example:string-reverse@0.1.0;

@since(version = 0.1.0)
interface reverse {
reverse-string: func(s: string) -> string;
}

world string-reverse {
export reverse;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "string-reverse",
"description": "Simple codebase for compiling a string reversing interface to WebAssembly with jco",
"type": "module"
}
6 changes: 6 additions & 0 deletions component-model/examples/tutorial/js/string-reverse/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// If this import listed below is missing, please run `npm run transpile`
import { reverse } from "./dist/transpiled/string-reverse.js";

const reversed = reverse.reverseString("!dlroW olleH");

console.log(`reverseString('!dlroW olleH') = ${reversed}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* This module is the JS implementation of the `string-reverse` WIT world
*/

/**
* The JavaScript export below represents the export of the `reverse` interface,
* which which contains `reverse-string` as its primary exported function.
*/
export const reverse = {
/**
* This JavaScript will be interpreted by `jco` and turned into a
* WebAssembly binary with a single export (this `reverse` function).
*/
reverseString(s) {
return s.split("")
.reverse()
.join("");
}
};
Loading