mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-27 06:41:32 +00:00
Package rename
This commit is contained in:
27
packages/core/aqua-to-js-compiler/package.json
Normal file
27
packages/core/aqua-to-js-compiler/package.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "@fluencelabs/aqua-to-js-compiler",
|
||||
"type": "module",
|
||||
"version": "0.0.0",
|
||||
"description": "Tool for generating aqua wrapper",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "vitest run"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@fluencelabs/aqua-api": "0.12.0",
|
||||
"@fluencelabs/aqua-lib": "0.7.3",
|
||||
"@fluencelabs/interfaces": "workspace:*",
|
||||
"@fluencelabs/js-client": "*",
|
||||
"@fluencelabs/registry": "0.8.7",
|
||||
"@fluencelabs/spell": "0.5.20",
|
||||
"@fluencelabs/trust-graph": "0.4.7",
|
||||
"ts-pattern": "5.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "5.1.6",
|
||||
"vitest": "0.29.7"
|
||||
}
|
||||
}
|
98
packages/core/aqua-to-js-compiler/src/common.ts
Normal file
98
packages/core/aqua-to-js-compiler/src/common.ts
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ArrowType, NonArrowType, ProductType } from '@fluencelabs/interfaces';
|
||||
import { match, P } from 'ts-pattern';
|
||||
import { getFuncArgs } from './utils.js';
|
||||
|
||||
export function genTypeName(t: NonArrowType, name: string): readonly [string | undefined, string] {
|
||||
const genType = typeToTs(t);
|
||||
return match(t)
|
||||
.with(
|
||||
{ tag: 'nil' },
|
||||
() => [undefined, 'void'] as const
|
||||
).with(
|
||||
{ tag: 'struct' },
|
||||
() => [`export type ${name} = ${genType}`, name] as const
|
||||
).with(
|
||||
{ tag: P.union('labeledProduct', 'unlabeledProduct') },
|
||||
(item) => {
|
||||
const args = item.tag === 'labeledProduct'
|
||||
? Object.values(item.fields)
|
||||
: item.items;
|
||||
|
||||
if (args.length === 1) {
|
||||
return genTypeName(args[0], name);
|
||||
}
|
||||
|
||||
return [`export type ${name} = ${genType}`, name] as const;
|
||||
},
|
||||
).otherwise(() => [undefined, genType] as const);
|
||||
}
|
||||
|
||||
export function typeToTs(t: NonArrowType | ArrowType<NonArrowType> | ProductType<NonArrowType>): string {
|
||||
return match(t)
|
||||
.with(
|
||||
{ tag: 'nil' },
|
||||
() => 'null'
|
||||
).with(
|
||||
{ tag: 'option' },
|
||||
({ type }) => typeToTs(type) + ' | null'
|
||||
).with(
|
||||
{ tag: 'scalar' },
|
||||
({ name }) => match(name)
|
||||
.with(P.union('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', 'f32', 'f64'), () => 'number')
|
||||
.with('bool', () => 'boolean')
|
||||
.with('string', () => 'string')
|
||||
.with(P._, () => 'any').exhaustive()
|
||||
).with(
|
||||
{ tag: 'array' },
|
||||
({ type }) => typeToTs(type) + '[]'
|
||||
).with(
|
||||
{ tag: 'struct' },
|
||||
({ fields }) => `{ ${Object.entries(fields).map(([field, type]) => `${field}: ${typeToTs(type)};`).join(' ')} }`
|
||||
).with(
|
||||
{ tag: 'labeledProduct' },
|
||||
({ fields }) => `{ ${Object.entries(fields).map(([field, type]) => `${field}: ${typeToTs(type)};`).join(' ')} }`
|
||||
).with(
|
||||
{ tag: 'unlabeledProduct' },
|
||||
({ items }) => `[${items.map(item => typeToTs(item)).join(', ')}]`
|
||||
).with(
|
||||
{ tag: 'arrow' },
|
||||
({ tag, domain, codomain }) => {
|
||||
const retType = codomain.tag === 'nil'
|
||||
? 'void'
|
||||
: codomain.items.length === 1
|
||||
? typeToTs(codomain.items[0])
|
||||
: typeToTs(codomain);
|
||||
|
||||
const args = getFuncArgs(domain).map(([name, type]) => ([name, typeToTs(type)]));
|
||||
|
||||
const generic = args.length === 0 ? 'null' : args.map(([name]) => `'${name}'`).join(' | ');
|
||||
args.push(['callParams', `CallParams$$<${generic}>`]);
|
||||
|
||||
const funcArgs = args.map(([name, type]) => `${name}: ${type}`).join(', ');
|
||||
|
||||
return `(${funcArgs}) => ${retType} | Promise<${retType}>`;
|
||||
}
|
||||
).with(
|
||||
{ tag: 'topType' },
|
||||
() => 'unknown'
|
||||
).with(
|
||||
{ tag: 'bottomType' },
|
||||
() => 'never'
|
||||
).exhaustive();
|
||||
}
|
17
packages/core/aqua-to-js-compiler/src/constants.ts
Normal file
17
packages/core/aqua-to-js-compiler/src/constants.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export const CLIENT = 'IFluenceClient$$';
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import * as fs from 'fs';
|
||||
import generate from '../index.js';
|
||||
import { compileFromPath } from '@fluencelabs/aqua-api';
|
||||
import * as url from 'url';
|
||||
|
||||
describe('Aqua to js/ts compiler', () => {
|
||||
it('compiles smoke tests successfully', async () => {
|
||||
const res = await compileFromPath({
|
||||
filePath: url.fileURLToPath(new URL('./sources/smoke_test.aqua', import.meta.url)),
|
||||
imports: ['./node_modules'],
|
||||
targetType: 'air'
|
||||
});
|
||||
|
||||
const jsResult = generate(res, 'js');
|
||||
const jsSnapshot = fs.readFileSync(new URL('./snapshots/smoke_test.js', import.meta.url))
|
||||
|
||||
expect(jsResult).toEqual(jsSnapshot.toString());
|
||||
|
||||
const tsResult = generate(res, 'ts');
|
||||
const tsSnapshot = fs.readFileSync(new URL('./snapshots/smoke_test.ts', import.meta.url))
|
||||
|
||||
expect(tsResult).toEqual(tsSnapshot.toString());
|
||||
});
|
||||
});
|
@ -0,0 +1,763 @@
|
||||
/* eslint-disable */
|
||||
// @ts-nocheck
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: 0.12.0
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
import {
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
} from '@fluencelabs/js-client';
|
||||
|
||||
// Services
|
||||
|
||||
export function registerSrv(...args) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"defaultServiceId": "single_module_srv",
|
||||
"functions": {
|
||||
"fields": {
|
||||
"create": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"wasm_b64_content": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "ServiceCreationResult",
|
||||
"fields": {
|
||||
"error": {
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
"service_id": {
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
"success": {
|
||||
"name": "bool",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "struct"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"list": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "array"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"remove": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"service_id": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "RemoveResult",
|
||||
"fields": {
|
||||
"error": {
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
"success": {
|
||||
"name": "bool",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "struct"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function registerCalcService(...args) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"functions": {
|
||||
"fields": {
|
||||
"add": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"clear_state": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"divide": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"multiply": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"state": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"subtract": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"test_logs": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"tag": "arrow"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function registerHelloWorld(...args) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"defaultServiceId": "hello-world",
|
||||
"functions": {
|
||||
"fields": {
|
||||
"hello": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"str": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Functions
|
||||
export const resourceTest_script = `
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "label") [] label)
|
||||
)
|
||||
(xor
|
||||
(new $resource_id
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("peer" "timestamp_sec") [] t)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("registry" "get_key_bytes") [label [] t [] ""] bytes)
|
||||
(xor
|
||||
(call %init_peer_id% ("sig" "sign") [bytes] result)
|
||||
(fail %last_error%)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(match result.$.success false
|
||||
(ap result.$.error.[0] $error)
|
||||
)
|
||||
(new $successful
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(ap result.$.signature result_flat)
|
||||
(call -relay- ("registry" "get_key_id") [label %init_peer_id%] id)
|
||||
)
|
||||
(call -relay- ("op" "string_to_b58") [id] k)
|
||||
)
|
||||
(call -relay- ("kad" "neighborhood") [k [] []] nodes)
|
||||
)
|
||||
(par
|
||||
(fold nodes n-0
|
||||
(par
|
||||
(xor
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call n-0 ("peer" "timestamp_sec") [] t-0)
|
||||
(call n-0 ("trust-graph" "get_weight") [%init_peer_id% t-0] weight)
|
||||
)
|
||||
(call n-0 ("registry" "register_key") [label [] t [] "" result_flat.$.[0] weight t-0] result-0)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(match result-0.$.success true
|
||||
(ap true $successful)
|
||||
)
|
||||
(new $-ephemeral-stream-
|
||||
(new #-ephemeral-canon-
|
||||
(canon -relay- $-ephemeral-stream- #-ephemeral-canon-)
|
||||
)
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(ap result-0.$.error $error)
|
||||
(new $-ephemeral-stream-
|
||||
(new #-ephemeral-canon-
|
||||
(canon -relay- $-ephemeral-stream- #-ephemeral-canon-)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
(fail %last_error%)
|
||||
)
|
||||
(next n-0)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
)
|
||||
(new $status
|
||||
(new $result-1
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(par
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "sub") [1 1] sub)
|
||||
(new $successful_test
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "add") [sub 1] successful_incr)
|
||||
(fold $successful successful_fold_var
|
||||
(seq
|
||||
(seq
|
||||
(ap successful_fold_var $successful_test)
|
||||
(canon -relay- $successful_test #successful_iter_canon)
|
||||
)
|
||||
(xor
|
||||
(match #successful_iter_canon.length successful_incr
|
||||
(null)
|
||||
)
|
||||
(next successful_fold_var)
|
||||
)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
)
|
||||
(canon -relay- $successful_test #successful_result_canon)
|
||||
)
|
||||
(ap #successful_result_canon successful_gate)
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("math" "sub") [1 1] sub-0)
|
||||
)
|
||||
(ap "ok" $status)
|
||||
)
|
||||
(call -relay- ("peer" "timeout") [6000 "timeout"] $status)
|
||||
)
|
||||
(new $status_test
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "add") [0 1] status_incr)
|
||||
(fold $status status_fold_var
|
||||
(seq
|
||||
(seq
|
||||
(ap status_fold_var $status_test)
|
||||
(canon -relay- $status_test #status_iter_canon)
|
||||
)
|
||||
(xor
|
||||
(match #status_iter_canon.length status_incr
|
||||
(null)
|
||||
)
|
||||
(next status_fold_var)
|
||||
)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
)
|
||||
(canon -relay- $status_test #status_result_canon)
|
||||
)
|
||||
(ap #status_result_canon status_gate)
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(match status_gate.$.[0] "ok"
|
||||
(ap true $result-1)
|
||||
)
|
||||
(ap false $result-1)
|
||||
)
|
||||
)
|
||||
(new $result-1_test
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "add") [0 1] result-1_incr)
|
||||
(fold $result-1 result-1_fold_var
|
||||
(seq
|
||||
(seq
|
||||
(ap result-1_fold_var $result-1_test)
|
||||
(canon -relay- $result-1_test #result-1_iter_canon)
|
||||
)
|
||||
(xor
|
||||
(match #result-1_iter_canon.length result-1_incr
|
||||
(null)
|
||||
)
|
||||
(next result-1_fold_var)
|
||||
)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
)
|
||||
(canon -relay- $result-1_test #result-1_result_canon)
|
||||
)
|
||||
(ap #result-1_result_canon result-1_gate)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(match result-1_gate.$.[0] false
|
||||
(ap "resource wasn't created: timeout exceeded" $error)
|
||||
)
|
||||
(ap id $resource_id)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(fail %last_error%)
|
||||
)
|
||||
)
|
||||
(canon %init_peer_id% $resource_id #-resource_id-fix-0)
|
||||
)
|
||||
(ap #-resource_id-fix-0 -resource_id-flat-0)
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(canon %init_peer_id% $error #error_canon)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [-resource_id-flat-0 #error_canon])
|
||||
)
|
||||
`;
|
||||
|
||||
|
||||
export function resourceTest(...args) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "resourceTest",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"label": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "array"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
resourceTest_script
|
||||
);
|
||||
}
|
||||
|
||||
export const helloTest_script = `
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(xor
|
||||
(call %init_peer_id% ("hello-world" "hello") ["Fluence user"] hello)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [hello])
|
||||
)
|
||||
`;
|
||||
|
||||
|
||||
export function helloTest(...args) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "helloTest",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
helloTest_script
|
||||
);
|
||||
}
|
||||
|
||||
export const demo_calculation_script = `
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% (service_id "test_logs") [])
|
||||
(call %init_peer_id% (service_id "add") [10])
|
||||
)
|
||||
(call %init_peer_id% (service_id "multiply") [5])
|
||||
)
|
||||
(call %init_peer_id% (service_id "subtract") [8])
|
||||
)
|
||||
(call %init_peer_id% (service_id "divide") [6])
|
||||
)
|
||||
(call %init_peer_id% (service_id "state") [] res)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
)
|
||||
`;
|
||||
|
||||
|
||||
export function demo_calculation(...args) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "demo_calculation",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"service_id": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
demo_calculation_script
|
||||
);
|
||||
}
|
||||
|
||||
export const marineTest_script = `
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "wasm64") [] wasm64)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("single_module_srv" "create") [wasm64] serviceResult)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "test_logs") [])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "add") [10])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "multiply") [5])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "subtract") [8])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "divide") [6])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "state") [] res)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
)
|
||||
`;
|
||||
|
||||
|
||||
export function marineTest(...args) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "marineTest",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"wasm64": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
marineTest_script
|
||||
);
|
||||
}
|
||||
|
||||
/* eslint-enable */
|
@ -0,0 +1,829 @@
|
||||
/* eslint-disable */
|
||||
// @ts-nocheck
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: 0.12.0
|
||||
*
|
||||
*/
|
||||
import type { IFluenceClient as IFluenceClient$$, CallParams as CallParams$$ } from '@fluencelabs/js-client';
|
||||
|
||||
import {
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
} from '@fluencelabs/js-client';
|
||||
|
||||
// Services
|
||||
export interface SrvDef {
|
||||
create: (wasm_b64_content: string, callParams: CallParams$$<'wasm_b64_content'>) => { error: string | null; service_id: string | null; success: boolean; } | Promise<{ error: string | null; service_id: string | null; success: boolean; }>;
|
||||
list: (callParams: CallParams$$<null>) => string[] | Promise<string[]>;
|
||||
remove: (service_id: string, callParams: CallParams$$<'service_id'>) => { error: string | null; success: boolean; } | Promise<{ error: string | null; success: boolean; }>;
|
||||
}
|
||||
export function registerSrv(service: SrvDef): void;
|
||||
export function registerSrv(serviceId: string, service: SrvDef): void;
|
||||
export function registerSrv(peer: IFluenceClient$$, service: SrvDef): void;
|
||||
export function registerSrv(peer: IFluenceClient$$, serviceId: string, service: SrvDef): void;
|
||||
export function registerSrv(...args: any[]) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"defaultServiceId": "single_module_srv",
|
||||
"functions": {
|
||||
"fields": {
|
||||
"create": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"wasm_b64_content": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "ServiceCreationResult",
|
||||
"fields": {
|
||||
"error": {
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
"service_id": {
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
"success": {
|
||||
"name": "bool",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "struct"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"list": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "array"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"remove": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"service_id": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "RemoveResult",
|
||||
"fields": {
|
||||
"error": {
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
"success": {
|
||||
"name": "bool",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "struct"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export interface CalcServiceDef {
|
||||
add: (num: number, callParams: CallParams$$<'num'>) => number | Promise<number>;
|
||||
clear_state: (callParams: CallParams$$<null>) => void | Promise<void>;
|
||||
divide: (num: number, callParams: CallParams$$<'num'>) => number | Promise<number>;
|
||||
multiply: (num: number, callParams: CallParams$$<'num'>) => number | Promise<number>;
|
||||
state: (callParams: CallParams$$<null>) => number | Promise<number>;
|
||||
subtract: (num: number, callParams: CallParams$$<'num'>) => number | Promise<number>;
|
||||
test_logs: (callParams: CallParams$$<null>) => void | Promise<void>;
|
||||
}
|
||||
export function registerCalcService(service: CalcServiceDef): void;
|
||||
export function registerCalcService(serviceId: string, service: CalcServiceDef): void;
|
||||
export function registerCalcService(peer: IFluenceClient$$, service: CalcServiceDef): void;
|
||||
export function registerCalcService(peer: IFluenceClient$$, serviceId: string, service: CalcServiceDef): void;
|
||||
export function registerCalcService(...args: any[]) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"functions": {
|
||||
"fields": {
|
||||
"add": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"clear_state": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"divide": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"multiply": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"state": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"subtract": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"num": {
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"test_logs": {
|
||||
"domain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"codomain": {
|
||||
"tag": "nil"
|
||||
},
|
||||
"tag": "arrow"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export interface HelloWorldDef {
|
||||
hello: (str: string, callParams: CallParams$$<'str'>) => string | Promise<string>;
|
||||
}
|
||||
export function registerHelloWorld(service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(serviceId: string, service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(peer: IFluenceClient$$, service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(peer: IFluenceClient$$, serviceId: string, service: HelloWorldDef): void;
|
||||
export function registerHelloWorld(...args: any[]) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"defaultServiceId": "hello-world",
|
||||
"functions": {
|
||||
"fields": {
|
||||
"hello": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"str": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Functions
|
||||
export const resourceTest_script = `
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "label") [] label)
|
||||
)
|
||||
(xor
|
||||
(new $resource_id
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("peer" "timestamp_sec") [] t)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("registry" "get_key_bytes") [label [] t [] ""] bytes)
|
||||
(xor
|
||||
(call %init_peer_id% ("sig" "sign") [bytes] result)
|
||||
(fail %last_error%)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(match result.$.success false
|
||||
(ap result.$.error.[0] $error)
|
||||
)
|
||||
(new $successful
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(ap result.$.signature result_flat)
|
||||
(call -relay- ("registry" "get_key_id") [label %init_peer_id%] id)
|
||||
)
|
||||
(call -relay- ("op" "string_to_b58") [id] k)
|
||||
)
|
||||
(call -relay- ("kad" "neighborhood") [k [] []] nodes)
|
||||
)
|
||||
(par
|
||||
(fold nodes n-0
|
||||
(par
|
||||
(xor
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call n-0 ("peer" "timestamp_sec") [] t-0)
|
||||
(call n-0 ("trust-graph" "get_weight") [%init_peer_id% t-0] weight)
|
||||
)
|
||||
(call n-0 ("registry" "register_key") [label [] t [] "" result_flat.$.[0] weight t-0] result-0)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(match result-0.$.success true
|
||||
(ap true $successful)
|
||||
)
|
||||
(new $-ephemeral-stream-
|
||||
(new #-ephemeral-canon-
|
||||
(canon -relay- $-ephemeral-stream- #-ephemeral-canon-)
|
||||
)
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(ap result-0.$.error $error)
|
||||
(new $-ephemeral-stream-
|
||||
(new #-ephemeral-canon-
|
||||
(canon -relay- $-ephemeral-stream- #-ephemeral-canon-)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
(fail %last_error%)
|
||||
)
|
||||
(next n-0)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
)
|
||||
(new $status
|
||||
(new $result-1
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(par
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "sub") [1 1] sub)
|
||||
(new $successful_test
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "add") [sub 1] successful_incr)
|
||||
(fold $successful successful_fold_var
|
||||
(seq
|
||||
(seq
|
||||
(ap successful_fold_var $successful_test)
|
||||
(canon -relay- $successful_test #successful_iter_canon)
|
||||
)
|
||||
(xor
|
||||
(match #successful_iter_canon.length successful_incr
|
||||
(null)
|
||||
)
|
||||
(next successful_fold_var)
|
||||
)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
)
|
||||
(canon -relay- $successful_test #successful_result_canon)
|
||||
)
|
||||
(ap #successful_result_canon successful_gate)
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("math" "sub") [1 1] sub-0)
|
||||
)
|
||||
(ap "ok" $status)
|
||||
)
|
||||
(call -relay- ("peer" "timeout") [6000 "timeout"] $status)
|
||||
)
|
||||
(new $status_test
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "add") [0 1] status_incr)
|
||||
(fold $status status_fold_var
|
||||
(seq
|
||||
(seq
|
||||
(ap status_fold_var $status_test)
|
||||
(canon -relay- $status_test #status_iter_canon)
|
||||
)
|
||||
(xor
|
||||
(match #status_iter_canon.length status_incr
|
||||
(null)
|
||||
)
|
||||
(next status_fold_var)
|
||||
)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
)
|
||||
(canon -relay- $status_test #status_result_canon)
|
||||
)
|
||||
(ap #status_result_canon status_gate)
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(match status_gate.$.[0] "ok"
|
||||
(ap true $result-1)
|
||||
)
|
||||
(ap false $result-1)
|
||||
)
|
||||
)
|
||||
(new $result-1_test
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("math" "add") [0 1] result-1_incr)
|
||||
(fold $result-1 result-1_fold_var
|
||||
(seq
|
||||
(seq
|
||||
(ap result-1_fold_var $result-1_test)
|
||||
(canon -relay- $result-1_test #result-1_iter_canon)
|
||||
)
|
||||
(xor
|
||||
(match #result-1_iter_canon.length result-1_incr
|
||||
(null)
|
||||
)
|
||||
(next result-1_fold_var)
|
||||
)
|
||||
)
|
||||
(never)
|
||||
)
|
||||
)
|
||||
(canon -relay- $result-1_test #result-1_result_canon)
|
||||
)
|
||||
(ap #result-1_result_canon result-1_gate)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(match result-1_gate.$.[0] false
|
||||
(ap "resource wasn't created: timeout exceeded" $error)
|
||||
)
|
||||
(ap id $resource_id)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(fail %last_error%)
|
||||
)
|
||||
)
|
||||
(canon %init_peer_id% $resource_id #-resource_id-fix-0)
|
||||
)
|
||||
(ap #-resource_id-fix-0 -resource_id-flat-0)
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(canon %init_peer_id% $error #error_canon)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [-resource_id-flat-0 #error_canon])
|
||||
)
|
||||
`;
|
||||
|
||||
export type ResourceTestResult = [string | null, string[]]
|
||||
|
||||
export function resourceTest(
|
||||
label: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<ResourceTestResult>;
|
||||
|
||||
export function resourceTest(
|
||||
peer: IFluenceClient$$,
|
||||
label: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<ResourceTestResult>;
|
||||
|
||||
export function resourceTest(...args: any[]) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "resourceTest",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"label": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "option"
|
||||
},
|
||||
{
|
||||
"type": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
},
|
||||
"tag": "array"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
resourceTest_script
|
||||
);
|
||||
}
|
||||
|
||||
export const helloTest_script = `
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(xor
|
||||
(call %init_peer_id% ("hello-world" "hello") ["Fluence user"] hello)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [hello])
|
||||
)
|
||||
`;
|
||||
|
||||
export function helloTest(
|
||||
config?: {ttl?: number}
|
||||
): Promise<string>;
|
||||
|
||||
export function helloTest(
|
||||
peer: IFluenceClient$$,
|
||||
config?: {ttl?: number}
|
||||
): Promise<string>;
|
||||
|
||||
export function helloTest(...args: any[]) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "helloTest",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
helloTest_script
|
||||
);
|
||||
}
|
||||
|
||||
export const demo_calculation_script = `
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% (service_id "test_logs") [])
|
||||
(call %init_peer_id% (service_id "add") [10])
|
||||
)
|
||||
(call %init_peer_id% (service_id "multiply") [5])
|
||||
)
|
||||
(call %init_peer_id% (service_id "subtract") [8])
|
||||
)
|
||||
(call %init_peer_id% (service_id "divide") [6])
|
||||
)
|
||||
(call %init_peer_id% (service_id "state") [] res)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
)
|
||||
`;
|
||||
|
||||
export function demo_calculation(
|
||||
service_id: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<number>;
|
||||
|
||||
export function demo_calculation(
|
||||
peer: IFluenceClient$$,
|
||||
service_id: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<number>;
|
||||
|
||||
export function demo_calculation(...args: any[]) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "demo_calculation",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"service_id": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
demo_calculation_script
|
||||
);
|
||||
}
|
||||
|
||||
export const marineTest_script = `
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "wasm64") [] wasm64)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("single_module_srv" "create") [wasm64] serviceResult)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "test_logs") [])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "add") [10])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "multiply") [5])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "subtract") [8])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "divide") [6])
|
||||
)
|
||||
(call %init_peer_id% (serviceResult.$.service_id.[0] "state") [] res)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
)
|
||||
`;
|
||||
|
||||
export function marineTest(
|
||||
wasm64: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<number>;
|
||||
|
||||
export function marineTest(
|
||||
peer: IFluenceClient$$,
|
||||
wasm64: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<number>;
|
||||
|
||||
export function marineTest(...args: any[]) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName": "marineTest",
|
||||
"arrow": {
|
||||
"domain": {
|
||||
"fields": {
|
||||
"wasm64": {
|
||||
"name": "string",
|
||||
"tag": "scalar"
|
||||
}
|
||||
},
|
||||
"tag": "labeledProduct"
|
||||
},
|
||||
"codomain": {
|
||||
"items": [
|
||||
{
|
||||
"name": "f64",
|
||||
"tag": "scalar"
|
||||
}
|
||||
],
|
||||
"tag": "unlabeledProduct"
|
||||
},
|
||||
"tag": "arrow"
|
||||
},
|
||||
"names": {
|
||||
"relay": "-relay-",
|
||||
"getDataSrv": "getDataSrv",
|
||||
"callbackSrv": "callbackSrv",
|
||||
"responseSrv": "callbackSrv",
|
||||
"responseFnName": "response",
|
||||
"errorHandlingSrv": "errorHandlingSrv",
|
||||
"errorFnName": "error"
|
||||
}
|
||||
},
|
||||
marineTest_script
|
||||
);
|
||||
}
|
||||
|
||||
/* eslint-enable */
|
@ -0,0 +1,55 @@
|
||||
import "@fluencelabs/registry/resources-api.aqua"
|
||||
|
||||
service HelloWorld("hello-world"):
|
||||
hello(str: string) -> string
|
||||
|
||||
func resourceTest(label: string) -> ?string, *string:
|
||||
res, errors <- createResource(label)
|
||||
<- res, errors
|
||||
|
||||
func helloTest() -> string:
|
||||
hello <- HelloWorld.hello("Fluence user")
|
||||
<- hello
|
||||
|
||||
service CalcService:
|
||||
add(num: f64) -> f64
|
||||
clear_state()
|
||||
divide(num: f64) -> f64
|
||||
multiply(num: f64) -> f64
|
||||
state() -> f64
|
||||
subtract(num: f64) -> f64
|
||||
test_logs()
|
||||
|
||||
data ServiceCreationResult:
|
||||
success: bool
|
||||
service_id: ?string
|
||||
error: ?string
|
||||
|
||||
data RemoveResult:
|
||||
success: bool
|
||||
error: ?string
|
||||
|
||||
alias ListServiceResult: []string
|
||||
|
||||
service Srv("single_module_srv"):
|
||||
create(wasm_b64_content: string) -> ServiceCreationResult
|
||||
remove(service_id: string) -> RemoveResult
|
||||
list() -> ListServiceResult
|
||||
|
||||
|
||||
func demo_calculation(service_id: string) -> f64:
|
||||
CalcService service_id
|
||||
CalcService.test_logs()
|
||||
CalcService.add(10)
|
||||
CalcService.multiply(5)
|
||||
CalcService.subtract(8)
|
||||
CalcService.divide(6)
|
||||
res <- CalcService.state()
|
||||
<- res
|
||||
|
||||
func marineTest(wasm64: string) -> f64:
|
||||
serviceResult <- Srv.create(wasm64)
|
||||
res <- demo_calculation(serviceResult.service_id!)
|
||||
<- res
|
||||
|
||||
|
44
packages/core/aqua-to-js-compiler/src/generate/function.ts
Normal file
44
packages/core/aqua-to-js-compiler/src/generate/function.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { AquaFunction } from '@fluencelabs/aqua-api/aqua-api.js';
|
||||
import { recursiveRenameLaquaProps } from '../utils.js';
|
||||
import { TypeGenerator } from './interfaces.js';
|
||||
|
||||
export class FunctionGenerator {
|
||||
constructor(
|
||||
private typeGenerator: TypeGenerator
|
||||
) {}
|
||||
|
||||
generate(functions: Record<string, AquaFunction>) {
|
||||
return Object.values(functions).map(func => this.generateFunction(func)).join('\n\n');
|
||||
}
|
||||
|
||||
private generateFunction(func: AquaFunction) {
|
||||
const scriptConstName = func.funcDef.functionName + '_script';
|
||||
return `export const ${scriptConstName} = \`
|
||||
${func.script}\`;
|
||||
|
||||
${this.typeGenerator.funcType(func)}
|
||||
export function ${func.funcDef.functionName}(${this.typeGenerator.type('...args', 'any[]')}) {
|
||||
return callFunction$$(
|
||||
args,
|
||||
${JSON.stringify(recursiveRenameLaquaProps(func.funcDef), null, 4)},
|
||||
${scriptConstName}
|
||||
);
|
||||
}`
|
||||
}
|
||||
}
|
32
packages/core/aqua-to-js-compiler/src/generate/header.ts
Normal file
32
packages/core/aqua-to-js-compiler/src/generate/header.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export default function(isJs: boolean, aquaVersion: string) {
|
||||
return `/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: ${aquaVersion}
|
||||
*
|
||||
*/
|
||||
${!isJs ? 'import type { IFluenceClient as IFluenceClient$$, CallParams as CallParams$$ } from \'@fluencelabs/js-client\';' : ''}
|
||||
|
||||
import {
|
||||
v5_callFunction as callFunction$$,
|
||||
v5_registerService as registerService$$,
|
||||
} from '@fluencelabs/js-client';`;
|
||||
}
|
40
packages/core/aqua-to-js-compiler/src/generate/index.ts
Normal file
40
packages/core/aqua-to-js-compiler/src/generate/index.ts
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import header from './header.js';
|
||||
import { getAquaApiVersion } from '../utils.js';
|
||||
import { FunctionGenerator } from './function.js';
|
||||
import { CompilationResult } from '@fluencelabs/aqua-api/aqua-api.js';
|
||||
import { JSTypeGenerator, TSTypeGenerator } from './interfaces.js';
|
||||
import { ServiceGenerator } from './service.js';
|
||||
|
||||
type OutputType = 'js' | 'ts';
|
||||
|
||||
export default function ({ services, functions }: CompilationResult, outputType: OutputType) {
|
||||
const typeGenerator = outputType === 'js' ? new JSTypeGenerator() : new TSTypeGenerator();
|
||||
return `/* eslint-disable */
|
||||
// @ts-nocheck
|
||||
${header(outputType === 'js', getAquaApiVersion())}
|
||||
|
||||
// Services
|
||||
${new ServiceGenerator(typeGenerator).generate(services)}
|
||||
|
||||
// Functions
|
||||
${new FunctionGenerator(typeGenerator).generate(functions)}
|
||||
|
||||
/* eslint-enable */
|
||||
`
|
||||
};
|
125
packages/core/aqua-to-js-compiler/src/generate/interfaces.ts
Normal file
125
packages/core/aqua-to-js-compiler/src/generate/interfaces.ts
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { AquaFunction, CompilationResult } from '@fluencelabs/aqua-api/aqua-api.js';
|
||||
import { CLIENT } from '../constants.js';
|
||||
import { FunctionCallDef, ServiceDef } from '@fluencelabs/interfaces';
|
||||
import { genTypeName, typeToTs } from '../common.js';
|
||||
import { capitalize, getFuncArgs } from '../utils.js';
|
||||
|
||||
export interface TypeGenerator {
|
||||
type(field: string, type: string): string;
|
||||
generic(field: string, type: string): string;
|
||||
bang(field: string): string;
|
||||
funcType(funcDef: AquaFunction): string;
|
||||
serviceType(srvName: string, srvDef: ServiceDef): string;
|
||||
}
|
||||
|
||||
export class TSTypeGenerator implements TypeGenerator {
|
||||
bang(field: string): string {
|
||||
return `${field}!`;
|
||||
}
|
||||
|
||||
generic(field: string, type: string): string {
|
||||
return `${field}<${type}>`;
|
||||
}
|
||||
|
||||
type(field: string, type: string): string {
|
||||
return `${field}: ${type}`;
|
||||
}
|
||||
|
||||
funcType({ funcDef }: AquaFunction): string {
|
||||
const args = getFuncArgs(funcDef.arrow.domain).map(([name, type]) => {
|
||||
const [typeDesc, t] = genTypeName(type, capitalize(funcDef.functionName) + 'Arg' + capitalize(name));
|
||||
return [typeDesc, `${name}: ${t}`] as const;
|
||||
});
|
||||
args.push([undefined, `config?: {ttl?: number}`]);
|
||||
|
||||
const argsDefs = args.map(([, def]) => " " + def);
|
||||
const argsDesc = args.filter(([desc]) => desc !== undefined).map(([desc]) => desc);
|
||||
|
||||
const functionOverloads = [
|
||||
argsDefs.join(',\n'),
|
||||
[` peer: ${CLIENT}`, ...argsDefs].join(',\n')
|
||||
];
|
||||
|
||||
const [resTypeDesc, resType] = genTypeName(funcDef.arrow.codomain, capitalize(funcDef.functionName) + "Result");
|
||||
|
||||
return [
|
||||
argsDesc.join('\n'),
|
||||
resTypeDesc || "",
|
||||
functionOverloads.flatMap(fo => [
|
||||
`export function ${funcDef.functionName}(`,
|
||||
fo,
|
||||
`): Promise<${resType}>;`,
|
||||
''
|
||||
]).join('\n')
|
||||
].filter(s => s !== '').join('\n\n');
|
||||
}
|
||||
|
||||
serviceType(srvName: string, srvDef: ServiceDef): string {
|
||||
const members = srvDef.functions.tag === 'nil' ? [] : Object.entries(srvDef.functions.fields);
|
||||
|
||||
const interfaceDefs = members
|
||||
.map(([name, arrow]) => {
|
||||
return ` ${name}: ${typeToTs(arrow)};`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
const interfaces = [`export interface ${srvName}Def {`, interfaceDefs, '}'].join('\n');
|
||||
|
||||
const peerDecl = `peer: ${CLIENT}`;
|
||||
const serviceDecl = `service: ${srvName}Def`;
|
||||
const serviceIdDecl = `serviceId: string`;
|
||||
const registerServiceArgs = [
|
||||
[serviceDecl],
|
||||
[serviceIdDecl, serviceDecl],
|
||||
[peerDecl, serviceDecl],
|
||||
[peerDecl, serviceIdDecl, serviceDecl]
|
||||
];
|
||||
|
||||
return [interfaces, ...registerServiceArgs.map(registerServiceArg => {
|
||||
const args = registerServiceArg.join(', ');
|
||||
return `export function register${srvName}(${args}): void;`
|
||||
})].join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
export class JSTypeGenerator implements TypeGenerator {
|
||||
bang(field: string): string {
|
||||
return field;
|
||||
}
|
||||
|
||||
generic(field: string, type: string): string {
|
||||
return field;
|
||||
}
|
||||
|
||||
type(field: string, type: string): string {
|
||||
return field;
|
||||
}
|
||||
|
||||
funcType(): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
serviceType(): string {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export interface EntityGenerator {
|
||||
generate(compilationResult: CompilationResult): string;
|
||||
}
|
64
packages/core/aqua-to-js-compiler/src/generate/service.ts
Normal file
64
packages/core/aqua-to-js-compiler/src/generate/service.ts
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ServiceDef } from '@fluencelabs/interfaces';
|
||||
import { recursiveRenameLaquaProps } from '../utils.js';
|
||||
import { TypeGenerator } from './interfaces.js';
|
||||
|
||||
interface DefaultServiceId {
|
||||
s_Some__f_value?: string
|
||||
}
|
||||
|
||||
export class ServiceGenerator {
|
||||
constructor(
|
||||
private typeGenerator: TypeGenerator
|
||||
) {}
|
||||
|
||||
generate(services: Record<string, ServiceDef>): string {
|
||||
const generated = Object.entries(services).map(([srvName, srvDef]) => this.generateService(srvName, srvDef)).join('\n\n');
|
||||
|
||||
return generated + '\n';
|
||||
}
|
||||
|
||||
private generateService(srvName: string, srvDef: ServiceDef) {
|
||||
return [
|
||||
this.typeGenerator.serviceType(srvName, srvDef),
|
||||
this.generateRegisterServiceOverload(srvName, srvDef)
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
private generateRegisterServiceOverload(srvName: string, srvDef: ServiceDef) {
|
||||
return [
|
||||
`export function register${srvName}(${this.typeGenerator.type('...args', 'any[]')}) {`,
|
||||
' registerService$$(',
|
||||
' args,',
|
||||
` ${this.serviceToJson(srvDef)}`,
|
||||
' );',
|
||||
'}'
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
private serviceToJson(service: ServiceDef): string {
|
||||
return JSON.stringify({
|
||||
...(
|
||||
(service.defaultServiceId as DefaultServiceId)?.s_Some__f_value
|
||||
? { defaultServiceId: (service.defaultServiceId as DefaultServiceId).s_Some__f_value }
|
||||
: {}
|
||||
),
|
||||
functions: recursiveRenameLaquaProps(service.functions)
|
||||
}, null, 4);
|
||||
}
|
||||
}
|
123
packages/core/aqua-to-js-compiler/src/index.ts
Normal file
123
packages/core/aqua-to-js-compiler/src/index.ts
Normal file
@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// @ts-nocheck
|
||||
|
||||
import { compileFromPath } from '@fluencelabs/aqua-api';
|
||||
import type { ArrowType, LabeledProductType, ProductType, ServiceDef } from '@fluencelabs/interfaces';
|
||||
import assert from 'assert';
|
||||
import { match, P } from 'ts-pattern';
|
||||
import {
|
||||
ArrayType,
|
||||
BottomType,
|
||||
NilType,
|
||||
NonArrowType,
|
||||
OptionType,
|
||||
ScalarType,
|
||||
StructType,
|
||||
TopType,
|
||||
UnlabeledProductType
|
||||
} from '@fluencelabs/interfaces';
|
||||
import * as fs from 'fs';
|
||||
import generate from './generate/index.js';
|
||||
|
||||
const res = await compileFromPath({
|
||||
filePath: './src/generate/__test__/sources/smoke_test.aqua',
|
||||
imports: ['./node_modules'],
|
||||
targetType: 'air'
|
||||
});
|
||||
|
||||
const data = generate(res, 'ts');
|
||||
|
||||
fs.writeFileSync('./src/generate/__test__/snapshots/smoke_test.ts', data);
|
||||
|
||||
|
||||
process.exit();
|
||||
|
||||
|
||||
type GetTsTypeFromScalar<T extends ScalarType> = T['name'] extends 'u8' | 'u16' | 'u32' | 'u64' | 'i8' | 'i16' | 'i32' | 'i64' | 'f32' | 'f64'
|
||||
? number
|
||||
: T['name'] extends 'bool'
|
||||
? boolean
|
||||
: T['name'] extends 'string'
|
||||
? string
|
||||
: never;
|
||||
|
||||
type MapTuple<T> = { [K in keyof T]: T[K] extends NonArrowType ? GetTsType<T[K]> : never }
|
||||
|
||||
type GetTsType<T extends NonArrowType> = T extends NilType
|
||||
? null
|
||||
: T extends ArrayType
|
||||
? GetTsType<T['type']>[]
|
||||
: T extends StructType
|
||||
? { [K in keyof T]: GetTsType<T> }
|
||||
: T extends OptionType
|
||||
? GetTsType<T['type']> | null
|
||||
: T extends ScalarType
|
||||
? GetTsTypeFromScalar<T>
|
||||
: T extends TopType
|
||||
? unknown
|
||||
: T extends BottomType
|
||||
? never
|
||||
: T extends Exclude<UnlabeledProductType<infer H>, NilType>
|
||||
? MapTuple<H>
|
||||
: T extends Exclude<LabeledProductType<infer H>, NilType>
|
||||
? H extends NonArrowType
|
||||
? { [K in keyof T['fields']]: GetTsType<H> }
|
||||
: never
|
||||
: never;
|
||||
|
||||
|
||||
|
||||
const struct = {
|
||||
"tag" : "unlabeledProduct",
|
||||
"items" : [
|
||||
{
|
||||
"tag" : "struct",
|
||||
"name" : "RemoveResult",
|
||||
"fields" : {
|
||||
"error" : {
|
||||
"tag" : "option",
|
||||
"type" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
},
|
||||
"success" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "bool"
|
||||
}
|
||||
}
|
||||
}
|
||||
] as Array<SomeNonArrowTypes>
|
||||
} as const;
|
||||
|
||||
type tt = GetTsType<typeof struct>;
|
||||
|
||||
const services = res.services as Record<string, ServiceDef>
|
||||
|
||||
const service = services['Srv'];
|
||||
|
||||
if (service.functions.tag === 'nil') {
|
||||
throw new Error('nil');
|
||||
}
|
||||
|
||||
const codomain = service.functions.fields['reload'].domain;
|
||||
console.log(service.functions);
|
||||
console.log(service);
|
||||
// console.log(codomain);
|
||||
// assert(codomain.tag === 'labeledProduct');
|
||||
console.log(JSON.stringify(codomain));
|
60
packages/core/aqua-to-js-compiler/src/utils.ts
Normal file
60
packages/core/aqua-to-js-compiler/src/utils.ts
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import pkg from '../package.json' assert { type: 'json' };
|
||||
import { ArrowType, NonArrowType, ProductType } from '@fluencelabs/interfaces';
|
||||
|
||||
export function getAquaApiVersion() {
|
||||
return pkg.dependencies['@fluencelabs/aqua-api'];
|
||||
}
|
||||
|
||||
export function getFuncArgs(domain: ProductType<NonArrowType>): [string, NonArrowType][] {
|
||||
if (domain.tag === 'labeledProduct') {
|
||||
return Object.entries(domain.fields).map(([label, type]) => [label, type]);
|
||||
} else if (domain.tag === 'unlabeledProduct') {
|
||||
return domain.items.map((type, index) => ['arg' + index, type]);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function recursiveRenameLaquaProps(obj: unknown): unknown {
|
||||
if (typeof obj !== 'object' || obj === null) return obj;
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(item => recursiveRenameLaquaProps(item));
|
||||
}
|
||||
|
||||
return Object.getOwnPropertyNames(obj).reduce((acc, prop) => {
|
||||
let accessProp = prop;
|
||||
if (prop.includes('Laqua_js')) {
|
||||
// Last part of the property separated by "_" is a correct name
|
||||
const refinedProperty = prop.split('_').pop()!;
|
||||
if (refinedProperty in obj) {
|
||||
accessProp = refinedProperty;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[accessProp]: recursiveRenameLaquaProps(obj[accessProp as keyof typeof obj])
|
||||
};
|
||||
}, {});
|
||||
}
|
||||
|
||||
export function capitalize(str: string) {
|
||||
return str.slice(0, 1).toUpperCase() + str.slice(1);
|
||||
}
|
9
packages/core/aqua-to-js-compiler/tsconfig.json
Normal file
9
packages/core/aqua-to-js-compiler/tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"],
|
||||
}
|
Reference in New Issue
Block a user