Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: Add some Runtime demos
  • Loading branch information
cdata committed May 29, 2024
commit e8ec56cc5f0a8df4d1195221d272202c05c7dfea
2 changes: 2 additions & 0 deletions rust/usuba/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ impl From<HexError> for UsubaError {

impl From<JoinError> for UsubaError {
fn from(value: JoinError) -> Self {
error!("{}", value);
UsubaError::Internal(format!("{}", value))
}
}

impl From<anyhow::Error> for UsubaError {
fn from(value: anyhow::Error) -> Self {
error!("{}", value);
UsubaError::Internal(format!("{}", value))
}
}
Expand Down
13 changes: 13 additions & 0 deletions typescript/packages/runtime-demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@
<link rel="stylesheet" href="./shoelace/dist/themes/light.css" />
<link rel="stylesheet" href="./shoelace/dist/themes/dark.css" />
<link rel="stylesheet" href="./styles.css" />
<style>
#demos {
display: flex;
flex-direction: row;
gap: 1em;
}
</style>
</head>

<body>
<section id="demos">
<button onclick="demoOne()">Demo 1</button>
<button onclick="demoTwo()">Demo 2</button>
<button onclick="demoThree()">Demo 3</button>
<button onclick="demoFour()">Demo 4</button>
</section>

<script type="module" src="./src/index.ts"></script>
</body>
Expand Down
80 changes: 80 additions & 0 deletions typescript/packages/runtime-demo/src/demo1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Runtime } from '@commontools/usuba-rt';

export const COMMON_DIRECTORY_WIT = `
package common:directory;

interface lookup {
entry: func(index: u32) -> string;
}

world directory {
export lookup;
}`;

export const EXAMPLE_HELLO_WIT = `
package example:hello;

world hello {
import common:directory/lookup;

export hello: func() -> string;
}`;

export const EXAMPLE_HELLO_JS = `
import { entry } from 'common:directory/lookup';

export function hello() {
let value = entry(Math.floor(Math.random() * 10));
return 'Hello, Agent ' + value;
}`;

export const demo = async () => {
console.log('Initializing Runtime');

const rt = new Runtime([COMMON_DIRECTORY_WIT]);

console.log('Defining Module');

type ExpectedExports = {
hello: () => string;
};

const module = await rt.defineModule<ExpectedExports>({
contentType: 'text/javascript',
wit: EXAMPLE_HELLO_WIT,
sourceCode: EXAMPLE_HELLO_JS,
});

console.log('Instantiating Module');

const agents = [
'Marnie',
'Orange',
'Frank',
'Moon',
'Sparrow',
'River',
'Archer',
'Bard',
'Helman',
'Poisson',
];

const { hello } = await module.instantiate({
'common:directory/lookup': {
entry(index: number) {
return agents[Math.floor(index) % 10];
},
},
});

console.log('Invoking Module API:');

console.log(`%c${hello()}`, 'font-size: 1.5em; font-weight: bold;');

(self as any).demos ||= {};
(self as any).demos.one = {
hello,
};
console.log('fin');
};
131 changes: 131 additions & 0 deletions typescript/packages/runtime-demo/src/demo2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Runtime } from '@commontools/usuba-rt';

export const COMMON_ITERATOR_WIT = `
package common:iterator;

interface iterator {
variant value {
%string(string),
number(f64),
boolean(bool),
buffer(list<u8>)
}

resource iterable {
next: func() -> option<iterable>;
value: func() -> option<value>;
}

create: func() -> iterable;
}

world common {
export iterator;
}`;

export const USER_MODULE_JS = `
class Iterable {
#iter;
#value;

constructor(iter) {
this.#iter = iter;
this.#value = iter.next();
}

next() {
this.#value = this.#iter.next();
if (this.#value.done) {
return;
}
return this;
}

value() {
return {
tag: 'number',
val: this.#value.value
};
}
}

function *doWork() {
for (let i = 0; i < 10; ++i) {
yield i;
}
}

export const iterator = {
Iterable,
create() {
const i = new Iterable(doWork());
console.log('Doing work', i);
return new Iterable(doWork());
}
};`;

export const demo = async () => {
console.log('Initializing Runtime');

const rt = new Runtime([]);

console.log('Defining Module');

type Value =
| {
tag: 'number';
val: number;
}
| {
tag: 'string';
val: string;
}
| {
tag: 'boolean';
val: boolean;
}
| {
tag: 'buffer';
val: Uint8Array;
};
interface Iterator {
next(): Iterator | void;
value(): Value | void;
}
type ExpectedExports = {
iterator: {
create: () => Iterator;
};
};

const module = await rt.defineModule<ExpectedExports>({
contentType: 'text/javascript',
wit: COMMON_ITERATOR_WIT,
sourceCode: USER_MODULE_JS,
});

console.log('Instantiating Module');

const {
iterator: { create: createIterator },
} = await module.instantiate({});

console.log('Invoking Module API:');

for (
let iterator: Iterator | void = createIterator();
iterator;
iterator = iterator.next()
) {
console.log(
`%cValue: ${iterator?.value()?.val}`,
'font-size: 1.5em; font-weight: bold;'
);
}

(self as any).demos ||= {};
(self as any).demos.four = {
createIterator,
};
console.log('fin');
};
76 changes: 76 additions & 0 deletions typescript/packages/runtime-demo/src/demo3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Runtime } from '@commontools/usuba-rt';
import {
COMMON_DIRECTORY_WIT,
EXAMPLE_HELLO_WIT,
EXAMPLE_HELLO_JS,
} from './demo1.js';

export const COMMON_DIRECTORY_JS = `
const agents = [
'Marnie',
'Orange',
'Frank',
'Moon',
'Sparrow',
'River',
'Archer',
'Bard',
'Helman',
'Poisson',
];

export const lookup = {
entry: (index) => agents[Math.floor(Math.random() * agents.length)]
};
`;

export const demo = async () => {
console.log('Initializing first Runtime');

const rtOne = new Runtime([]);

console.log('Defining first Module');

type ExpectedExportsOne = {
lookup: {
entry(index: number): string;
};
};

const moduleOne = await rtOne.defineModule<ExpectedExportsOne>({
contentType: 'text/javascript',
wit: COMMON_DIRECTORY_WIT,
sourceCode: COMMON_DIRECTORY_JS,
});

const rtTwo = new Runtime([COMMON_DIRECTORY_WIT]);

console.log('Defining second Module');

type ExpectedExportsTwo = {
hello: () => string;
};

const moduleTwo = await rtTwo.defineModule<ExpectedExportsTwo>({
contentType: 'text/javascript',
wit: EXAMPLE_HELLO_WIT,
sourceCode: EXAMPLE_HELLO_JS,
});

console.log('Instantiating both Modules');

const { hello } = await moduleTwo.instantiate({
'common:directory/lookup': (await moduleOne.instantiate({})).lookup,
});

console.log('Invoking final Module API:');

console.log(`%c${hello()}`, 'font-size: 1.5em; font-weight: bold;');

(self as any).demos ||= {};
(self as any).demos.two = {
hello,
};

console.log('fin');
};
62 changes: 62 additions & 0 deletions typescript/packages/runtime-demo/src/demo4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Runtime } from '@commontools/usuba-rt';
import { COMMON_DIRECTORY_WIT, EXAMPLE_HELLO_WIT } from './demo1.js';
import { COMMON_DIRECTORY_JS } from './demo3.js';

export const EXAMPLE_HELLO_PY = `
import random
import hello
from hello.imports import lookup

class Hello(hello.Hello):
def hello(self) -> str:
return "Hello, Agent %s!" % lookup.entry(random.randint(0, 9))
`;

export const demo = async () => {
console.log('Initializing first Runtime');

const rtOne = new Runtime([]);

console.log('Defining first Module');

type ExpectedExportsOne = {
lookup: {
entry(index: number): string;
};
};

const moduleOne = await rtOne.defineModule<ExpectedExportsOne>({
contentType: 'text/javascript',
wit: COMMON_DIRECTORY_WIT,
sourceCode: COMMON_DIRECTORY_JS,
});

const rtTwo = new Runtime([COMMON_DIRECTORY_WIT]);

console.log('Defining second Module');

type ExpectedExportsTwo = {
hello: () => string;
};

const moduleTwo = await rtTwo.defineModule<ExpectedExportsTwo>({
contentType: 'text/x-python',
wit: EXAMPLE_HELLO_WIT,
sourceCode: EXAMPLE_HELLO_PY,
});

console.log('Instantiating both Modules');

const { hello } = await moduleTwo.instantiate({
'common:directory/lookup': (await moduleOne.instantiate({})).lookup,
});

console.log('Invoking final Module API:');

console.log(`%c${hello()}`, 'font-size: 1.5em; font-weight: bold;');
(self as any).demos ||= {};
(self as any).demos.three = {
hello,
};
console.log('fin');
};
Loading