mirror of
https://github.com/encounter/jco.git
synced 2026-03-30 11:18:26 -07:00
fa0832ed76
* feat: Allow `--instantiation` to generate non-async code. The goal of this patch is to allow `--instantiation` to generate non- async code as discussed in https://github.com/bytecodealliance/jco/issues/245. First off, this patch updates the `--instantiation` option to receive a string value instead of a being a switch. The possible values are `async` (the default) or `sync`. The fact that `async` is the default value ensures the backward compatibility. Next, this patch updates the `TranspileOpts::instantiation` field to be `Option<InstantiationMode>` instead of `bool`. `InstantiationMode` is a enum with 2 variants: `Async` and `Sync`. The code is updated accordingly. Finally, the same patch adds non-async code during the bindgen pass. Everything is done in one patch in order to be atomic. Note: `function_bindgen.rs` is updated to improve an error message. It's helpful when one needs to debug its code. * test: Allow multiple test files per fixture. This patch allows one fixture to be tested by multiple test files. * test: Add the `strings.sync.ts` test. * chore: Rename and simplify the `instantiate` signature. `compileCore` becomes `getCoreModule`, and its function type definition is now `(path: string)` instead of `(path: string, imports: Record<string, any>)`.
70 lines
2.4 KiB
JavaScript
70 lines
2.4 KiB
JavaScript
import { readFile } from 'node:fs/promises';
|
||
import { readdirSync } from 'node:fs';
|
||
import { exec, jcoPath } from './helpers.js';
|
||
import { strictEqual } from 'node:assert';
|
||
import { componentNew, componentEmbed, transpile } from '@bytecodealliance/jco';
|
||
import { ok } from 'node:assert';
|
||
|
||
const eslintPath = `node_modules/eslint/bin/eslint.js`;
|
||
|
||
export async function readFlags (fixture) {
|
||
try {
|
||
var source = await readFile(fixture, 'utf8');
|
||
}
|
||
catch (e) {
|
||
if (e && e.code === 'ENOENT')
|
||
return [];
|
||
throw e;
|
||
}
|
||
|
||
const firstLine = source.split('\n')[0];
|
||
if (firstLine.startsWith('// Flags:'))
|
||
return firstLine.slice(9).trim().split(' ');
|
||
return [];
|
||
}
|
||
|
||
export async function codegenTest (fixtures) {
|
||
suite(`Transpiler codegen`, () => {
|
||
for (const fixture of fixtures) {
|
||
const name = fixture.replace(/(\.component)?\.(wasm|wat)$/, '');
|
||
|
||
for (const testFile of (readdirSync('test/runtime/')).filter(testFile => testFile.startsWith(`${name}.`))) {
|
||
const testName= testFile.replace(/\.ts$/, '');
|
||
|
||
test(`${testName} transpile`, async () => {
|
||
const flags = await readFlags(`test/runtime/${testFile}`);
|
||
var { stderr } = await exec(jcoPath, 'transpile', `test/fixtures/components/${fixture}`, '--name', testName, ...flags, '-o', `test/output/${testName}`);
|
||
strictEqual(stderr, '');
|
||
});
|
||
|
||
test(`${testName} lint`, async () => {
|
||
const flags = await readFlags(`test/runtime/${testFile}`);
|
||
|
||
if (flags.includes('--js'))
|
||
return;
|
||
|
||
var { stderr } = await exec(eslintPath, `test/output/${testName}/${testName}.js`, '-c', 'test/eslintrc.cjs');
|
||
strictEqual(stderr, '');
|
||
});
|
||
}
|
||
}
|
||
});
|
||
|
||
suite(`Naming`, () => {
|
||
test(`Resource deduping`, async () => {
|
||
const component = await componentNew(await componentEmbed({
|
||
witSource: await readFile(`test/fixtures/wits/resource-naming/resource-naming.wit`, 'utf8'),
|
||
dummy: true,
|
||
metadata: [['language', [['javascript', '']]], ['processed-by', [['dummy-gen', 'test']]]]
|
||
}));
|
||
|
||
const { files } = await transpile(component, { name: 'resource-naming' });
|
||
|
||
const bindingsSource = new TextDecoder().decode(files['resource-naming.js']);
|
||
|
||
ok(bindingsSource.includes('const Thing$1 = class Thing'));
|
||
ok(bindingsSource.includes('Thing: Thing$1'));
|
||
});
|
||
});
|
||
}
|