import * as types from 'quickjs:types-in-exports/types';
async function as_result(f) {
try {
const result = await f();
return { tag: 'ok', val: result };
} catch (error) {
return { tag: 'err', val: error };
}
}
export const test = async () => {
console.log(`f1: ${await types.f1([0.1, 0.2, 0.3], ["a", "b"], ["c", "d"])}`);
console.log(`f2: ${await types.f2("hello world")}`);
console.log(`f3: ${await types.f3(true, -8, -16, -32, -64n, 8, 16, 32, 64n, 3.14, 2.718281828459045, 'c', "hello world")}`);
console.log(`f4: ${JSON.stringify(await as_result(() => types.f4({tag: 'ok', val: 42})))}`);
console.log(`f5: ${JSON.stringify(await as_result(() => types.f5({tag: 'err', val: 'error message'})))}`);
console.log(`f6: ${JSON.stringify(await as_result(() => types.f6({tag: 'ok', val: 'success'})))}`);
console.log(`f7: ${JSON.stringify(await as_result(() => types.f7({tag: 'ok'})))}`);
console.log(`f8: ${await types.f8(['example', 123, 4.56])}`);
const f9 = await types.f9({
a: "test",
b: 42,
c: 3.14,
d: {x: 1, y: 2},
e: undefined,
f: {tag: 'ok', val: 100},
g: [],
h: ['one', 'two'],
i: ['tuple', 1, 2.5]
});
console.log(`f9: ${JSON.stringify(f9)}`);
const f10 = await types.f10({
tag: 'specific',
val: 'example'
});
console.log(`f10: ${JSON.stringify(f10)}`);
const f11 = await types.f11('blue');
console.log(`f11: ${f11}`);
const f12 = await types.f12('green');
console.log(`f12: ${JSON.stringify(f12)}`);
const f13 = await types.f13({read: true, write: false, execute: true});
console.log(`f13: ${JSON.stringify(f13)}`);
const f14 = await types.f14(1234567890n);
console.log(`f14: ${f14}`);
const f15 = await types.f15(new Uint8Array([1, 2, 3, 4, 5]));
console.log(`f15: ${f15}`);
if (f15 instanceof Uint8Array) {
console.log(`f15 is a Uint8Array with length: ${f15.length}`);
}
};
export const nestedVariants = async (warmParent) => {
const { runJavaScript } = await import('wasm-rquickjs:execution');
if (warmParent) {
await types.f10({ tag: 'specific', val: 'parent' });
}
const results = [];
for (let i = 0; i < 3; i++) {
const result = await runJavaScript({
source: `
const types = await import('quickjs:types-in-exports/types');
const values = [];
for (const value of [
{ tag: 'specific', val: 'child-${i}' },
{ tag: 'none' },
{ tag: 'specific', val: 'again-${i}' },
]) {
values.push(await types.f10(value));
}
let error;
try {
await types.f18({ tag: 'any' });
} catch (caught) {
error = caught;
}
return { values, error };
`,
});
results.push(result.value);
}
return JSON.stringify(results);
};