Fix aqua-to-js, start to split responsibility

This commit is contained in:
Akim Mamedov 2023-11-12 21:54:43 +07:00
parent f5e9923974
commit 2f316cc8fb
12 changed files with 1071 additions and 6886 deletions

View File

@ -0,0 +1,48 @@
/**
* 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 { writeFile } from "fs/promises";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { compileFromPath } from "@fluencelabs/aqua-api";
import aquaToJs from "@fluencelabs/aqua-to-js";
const cr = await compileFromPath({
filePath: join(
dirname(fileURLToPath(import.meta.url)),
"_aqua",
"smoke_test.aqua",
),
targetType: "air",
imports: [join(dirname(fileURLToPath(import.meta.url)), "node_modules")],
});
const res = await aquaToJs(cr, "ts");
if (res == null) {
throw new Error(cr.errors.join("\n"));
}
await writeFile(
join(
dirname(fileURLToPath(import.meta.url)),
"src",
"_aqua",
"smoke_test.ts",
),
res.sources,
);

View File

@ -11,7 +11,7 @@
"type": "module",
"scripts": {
"build": "tsc",
"compile-aqua": "fluence aqua -i ./_aqua -o ./src/_aqua"
"compile-aqua": "node --loader ts-node/esm compile-aqua.ts"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
@ -20,10 +20,12 @@
"base64-js": "1.5.1"
},
"devDependencies": {
"@fluencelabs/aqua-api": "0.12.2",
"@fluencelabs/aqua-lib": "0.6.0",
"@fluencelabs/cli": "0.7.2",
"@fluencelabs/js-client": "workspace:^",
"@fluencelabs/registry": "0.8.2",
"@fluencelabs/trust-graph": "3.1.2"
"@fluencelabs/aqua-to-js": "workspace:*",
"@fluencelabs/js-client": "workspace:*",
"@fluencelabs/registry": "0.8.7",
"@fluencelabs/trust-graph": "3.1.2",
"ts-node": "10.9.1"
}
}

View File

@ -2,23 +2,24 @@
// @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
* This file is generated using:
* @fluencelabs/aqua-api version: 0.12.0
* @fluencelabs/aqua-to-js version: 0.2.0
* If you find any bugs in generated AIR, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
* If you find any bugs in generated JS/TS, please write an issue on GitHub: https://github.com/fluencelabs/js-client/issues
*
*/
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,
@ -46,131 +47,130 @@ export function registerSrv(
serviceId: string,
service: SrvDef,
): void;
export function registerSrv(...args: any) {
export function registerSrv(...args: any[]) {
registerService$$(args, {
defaultServiceId: "single_module_srv",
functions: {
tag: "labeledProduct",
fields: {
create: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
wasm_b64_content: {
tag: "scalar",
name: "string",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "struct",
name: "ServiceCreationResult",
fields: {
error: {
tag: "option",
type: {
tag: "scalar",
name: "string",
tag: "scalar",
},
tag: "option",
},
service_id: {
tag: "option",
type: {
tag: "scalar",
name: "string",
tag: "scalar",
},
tag: "option",
},
success: {
tag: "scalar",
name: "bool",
tag: "scalar",
},
},
tag: "struct",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
list: {
tag: "arrow",
domain: {
tag: "nil",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "array",
type: {
tag: "scalar",
name: "string",
tag: "scalar",
},
tag: "array",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
remove: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
service_id: {
tag: "scalar",
name: "string",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "struct",
name: "RemoveResult",
fields: {
error: {
tag: "option",
type: {
tag: "scalar",
name: "string",
tag: "scalar",
},
tag: "option",
},
success: {
tag: "scalar",
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>;
clear_state: (callParams: CallParams$$<null>) => void | Promise<void>;
test_logs: (callParams: CallParams$$<null>) => void | Promise<void>;
multiply: (
num: number,
callParams: CallParams$$<"num">,
) => number | Promise<number>;
add: (
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(
serviceId: string,
@ -181,130 +181,129 @@ export function registerCalcService(
serviceId: string,
service: CalcServiceDef,
): void;
export function registerCalcService(...args: any) {
export function registerCalcService(...args: any[]) {
registerService$$(args, {
functions: {
tag: "labeledProduct",
fields: {
add: {
tag: "arrow",
divide: {
domain: {
tag: "labeledProduct",
fields: {
num: {
tag: "scalar",
name: "f64",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
clear_state: {
tag: "arrow",
domain: {
tag: "nil",
},
codomain: {
tag: "nil",
},
},
divide: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
num: {
tag: "scalar",
name: "f64",
},
},
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
},
],
},
},
multiply: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
num: {
tag: "scalar",
name: "f64",
},
},
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
},
],
},
},
state: {
tag: "arrow",
domain: {
tag: "nil",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
},
],
},
},
subtract: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
num: {
tag: "scalar",
name: "f64",
},
},
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
},
],
},
},
test_logs: {
tag: "arrow",
domain: {
tag: "nil",
},
codomain: {
tag: "nil",
},
tag: "arrow",
},
multiply: {
domain: {
fields: {
num: {
name: "f64",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
items: [
{
name: "f64",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
add: {
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",
},
},
tag: "labeledProduct",
},
});
}
@ -329,66 +328,75 @@ export function registerHelloWorld(
serviceId: string,
service: HelloWorldDef,
): void;
export function registerHelloWorld(...args: any) {
type registerHelloWorldArgs =
| [HelloWorldDef]
| [string, HelloWorldDef]
| [IFluenceClient$$, string, HelloWorldDef]
| [IFluenceClient$$, HelloWorldDef];
export function registerHelloWorld(...args: registerHelloWorldArgs) {
if (typeof args[0] === "string") {
args[0].toString();
args[1].toString();
args[2].hello;
}
registerService$$(args, {
defaultServiceId: "hello-world",
functions: {
tag: "labeledProduct",
fields: {
hello: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
str: {
tag: "scalar",
name: "string",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "string",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
},
},
tag: "labeledProduct",
},
});
}
// Functions
export const resourceTest_script = `
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "label") [] label)
(call %init_peer_id% ("getDataSrv" "label") [] -label-arg-)
)
(xor
(new $resource_id
(seq
(seq
(seq
(call %init_peer_id% ("peer" "timestamp_sec") [] t)
(call %init_peer_id% ("peer" "timestamp_sec") [] ret)
(xor
(seq
(seq
(call -relay- ("registry" "get_key_bytes") [label [] t [] ""] bytes)
(call -relay- ("registry" "get_key_bytes") [-label-arg- [] ret [] ""] ret-0)
(xor
(call %init_peer_id% ("sig" "sign") [bytes] result)
(call %init_peer_id% ("sig" "sign") [ret-0] ret-1)
(fail %last_error%)
)
)
(xor
(match result.$.success false
(ap result.$.error.[0] $error)
(match ret-1.$.success false
(ap ret-1.$.error.[0] $error)
)
(new $successful
(seq
@ -397,29 +405,29 @@ export const resourceTest_script = `
(seq
(seq
(seq
(ap result.$.signature result_flat)
(call -relay- ("registry" "get_key_id") [label %init_peer_id%] id)
(ap ret-1.$.signature ret-1_flat)
(call -relay- ("registry" "get_key_id") [-label-arg- %init_peer_id%] ret-2)
)
(call -relay- ("op" "string_to_b58") [id] k)
(call -relay- ("op" "string_to_b58") [ret-2] ret-3)
)
(call -relay- ("kad" "neighborhood") [k [] []] nodes)
(call -relay- ("kad" "neighborhood") [ret-3 [] []] ret-4)
)
(par
(fold nodes n-0
(fold ret-4 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 ("peer" "timestamp_sec") [] ret-5)
(call n-0 ("trust-graph" "get_weight") [%init_peer_id% ret-5] ret-6)
)
(call n-0 ("registry" "register_key") [label [] t [] "" result_flat.$.[0] weight t-0] result-0)
(call n-0 ("registry" "register_key") [-label-arg- [] ret [] "" ret-1_flat.$.[0] ret-6 ret-5] ret-7)
)
(xor
(seq
(match result-0.$.success true
(match ret-7.$.success true
(ap true $successful)
)
(new $-ephemeral-stream-
@ -429,7 +437,7 @@ export const resourceTest_script = `
)
)
(seq
(ap result-0.$.error $error)
(ap ret-7.$.error $error)
(new $-ephemeral-stream-
(new #-ephemeral-canon-
(canon -relay- $-ephemeral-stream- #-ephemeral-canon-)
@ -490,7 +498,10 @@ export const resourceTest_script = `
)
(ap "ok" $status)
)
(call -relay- ("peer" "timeout") [6000 "timeout"] $status)
(seq
(call -relay- ("peer" "timeout") [6000 "timeout"] ret-8)
(ap ret-8 $status)
)
)
(new $status_test
(seq
@ -560,7 +571,7 @@ export const resourceTest_script = `
(match result-1_gate.$.[0] false
(ap "resource wasn't created: timeout exceeded" $error)
)
(ap id $resource_id)
(ap ret-2 $resource_id)
)
)
)
@ -580,10 +591,11 @@ export const resourceTest_script = `
(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 },
@ -595,41 +607,41 @@ export function resourceTest(
config?: { ttl?: number },
): Promise<ResourceTestResult>;
export function resourceTest(...args: any) {
export function resourceTest(...args: any[]) {
return callFunction$$(
args,
{
functionName: "resourceTest",
arrow: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
label: {
tag: "scalar",
name: "string",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "option",
type: {
tag: "scalar",
name: "string",
tag: "scalar",
},
tag: "option",
},
{
tag: "array",
type: {
tag: "scalar",
name: "string",
tag: "scalar",
},
tag: "array",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
names: {
relay: "-relay-",
@ -646,17 +658,17 @@ export function resourceTest(...args: any) {
}
export const helloTest_script = `
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(xor
(call %init_peer_id% ("hello-world" "hello") ["Fluence user"] hello)
(call %init_peer_id% ("hello-world" "hello") ["Fluence user"] ret)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
)
)
(call %init_peer_id% ("callbackSrv" "response") [hello])
)
`;
(call %init_peer_id% ("callbackSrv" "response") [ret])
)
`;
export function helloTest(config?: { ttl?: number }): Promise<string>;
@ -665,26 +677,26 @@ export function helloTest(
config?: { ttl?: number },
): Promise<string>;
export function helloTest(...args: any) {
export function helloTest(...args: any[]) {
return callFunction$$(
args,
{
functionName: "helloTest",
arrow: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "string",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
names: {
relay: "-relay-",
@ -701,17 +713,17 @@ export function helloTest(...args: any) {
}
export const callHappy_script = `
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "a") [] a)
(call %init_peer_id% ("getDataSrv" "a") [] -a-arg-)
)
(call %init_peer_id% ("getDataSrv" "b") [] b)
(call %init_peer_id% ("getDataSrv" "b") [] -b-arg-)
)
(call %init_peer_id% ("getDataSrv" "c") [] c)
(call %init_peer_id% ("getDataSrv" "c") [] -c-arg-)
)
(xor
(xor
@ -722,17 +734,14 @@ export const callHappy_script = `
)
)
(call %init_peer_id% ("callbackSrv" "response") [init_call_res0])
)
`;
)
`;
export function callHappy(
a: string,
b: number,
c: number,
d: (
arg0: string,
callParams: CallParams$$<"arg0">,
) => number | Promise<number>,
d: (arg0: string) => number | Promise<number>,
config?: { ttl?: number },
): Promise<number>;
@ -741,67 +750,64 @@ export function callHappy(
a: string,
b: number,
c: number,
d: (
arg0: string,
callParams: CallParams$$<"arg0">,
) => number | Promise<number>,
d: (arg0: string) => number | Promise<number>,
config?: { ttl?: number },
): Promise<number>;
export function callHappy(...args: any) {
export function callHappy(...args: any[]) {
return callFunction$$(
args,
{
functionName: "callHappy",
arrow: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
a: {
tag: "scalar",
name: "string",
tag: "scalar",
},
b: {
tag: "scalar",
name: "f64",
tag: "scalar",
},
c: {
tag: "scalar",
name: "f64",
tag: "scalar",
},
d: {
tag: "arrow",
domain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "string",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
names: {
relay: "-relay-",
@ -818,11 +824,11 @@ export function callHappy(...args: any) {
}
export const demo_calculation_script = `
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
(call %init_peer_id% ("getDataSrv" "service_id") [] -service_id-arg-)
)
(xor
(seq
@ -830,23 +836,23 @@ export const demo_calculation_script = `
(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-arg- "test_logs") [])
(call %init_peer_id% (-service_id-arg- "add") [10] ret)
)
(call %init_peer_id% (service_id "multiply") [5])
(call %init_peer_id% (-service_id-arg- "multiply") [5] ret-0)
)
(call %init_peer_id% (service_id "subtract") [8])
(call %init_peer_id% (-service_id-arg- "subtract") [8] ret-1)
)
(call %init_peer_id% (service_id "divide") [6])
(call %init_peer_id% (-service_id-arg- "divide") [6] ret-2)
)
(call %init_peer_id% (service_id "state") [] res)
(call %init_peer_id% (-service_id-arg- "state") [] ret-3)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
)
)
(call %init_peer_id% ("callbackSrv" "response") [res])
)
`;
(call %init_peer_id% ("callbackSrv" "response") [ret-3])
)
`;
export function demo_calculation(
service_id: string,
@ -859,31 +865,31 @@ export function demo_calculation(
config?: { ttl?: number },
): Promise<number>;
export function demo_calculation(...args: any) {
export function demo_calculation(...args: any[]) {
return callFunction$$(
args,
{
functionName: "demo_calculation",
arrow: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
service_id: {
tag: "scalar",
name: "string",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
names: {
relay: "-relay-",
@ -900,11 +906,11 @@ export function demo_calculation(...args: any) {
}
export const marineTest_script = `
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "wasm64") [] wasm64)
(call %init_peer_id% ("getDataSrv" "wasm64") [] -wasm64-arg-)
)
(xor
(seq
@ -913,25 +919,25 @@ export const marineTest_script = `
(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% ("single_module_srv" "create") [-wasm64-arg-] ret)
(call %init_peer_id% (ret.$.service_id.[0] "test_logs") [])
)
(call %init_peer_id% (serviceResult.$.service_id.[0] "add") [10])
(call %init_peer_id% (ret.$.service_id.[0] "add") [10] ret-0)
)
(call %init_peer_id% (serviceResult.$.service_id.[0] "multiply") [5])
(call %init_peer_id% (ret.$.service_id.[0] "multiply") [5] ret-1)
)
(call %init_peer_id% (serviceResult.$.service_id.[0] "subtract") [8])
(call %init_peer_id% (ret.$.service_id.[0] "subtract") [8] ret-2)
)
(call %init_peer_id% (serviceResult.$.service_id.[0] "divide") [6])
(call %init_peer_id% (ret.$.service_id.[0] "divide") [6] ret-3)
)
(call %init_peer_id% (serviceResult.$.service_id.[0] "state") [] res)
(call %init_peer_id% (ret.$.service_id.[0] "state") [] ret-4)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 0])
)
)
(call %init_peer_id% ("callbackSrv" "response") [res])
)
`;
(call %init_peer_id% ("callbackSrv" "response") [ret-4])
)
`;
export function marineTest(
wasm64: string,
@ -944,31 +950,31 @@ export function marineTest(
config?: { ttl?: number },
): Promise<number>;
export function marineTest(...args: any) {
export function marineTest(...args: any[]) {
return callFunction$$(
args,
{
functionName: "marineTest",
arrow: {
tag: "arrow",
domain: {
tag: "labeledProduct",
fields: {
wasm64: {
tag: "scalar",
name: "string",
tag: "scalar",
},
},
tag: "labeledProduct",
},
codomain: {
tag: "unlabeledProduct",
items: [
{
tag: "scalar",
name: "f64",
tag: "scalar",
},
],
tag: "unlabeledProduct",
},
tag: "arrow",
},
names: {
relay: "-relay-",
@ -983,5 +989,3 @@ export function marineTest(...args: any) {
marineTest_script,
);
}
/* eslint-enable */

View File

@ -14,13 +14,13 @@
* limitations under the License.
*/
import { ArrowWithoutCallbacks, NonArrowType } from "@fluencelabs/interfaces";
import { ArrowType, NonArrowType } from "@fluencelabs/interfaces";
import { match, P } from "ts-pattern";
import { getFuncArgs } from "./utils.js";
export function genTypeName(
t: NonArrowType | ArrowWithoutCallbacks,
t: NonArrowType | ArrowType,
name: string,
): readonly [string | undefined, string] {
const genType = typeToTs(t);
@ -46,7 +46,7 @@ export function genTypeName(
});
}
export function typeToTs(t: NonArrowType | ArrowWithoutCallbacks): string {
export function typeToTs(t: NonArrowType | ArrowType): string {
return match(t)
.with({ tag: "nil" }, () => {
return "null";
@ -120,6 +120,8 @@ export function typeToTs(t: NonArrowType | ArrowWithoutCallbacks): string {
return [name, typeToTs(type)];
});
// JS-client argument
if (domain.tag !== "unlabeledProduct") {
const generic =
args.length === 0
? "null"
@ -130,6 +132,7 @@ export function typeToTs(t: NonArrowType | ArrowWithoutCallbacks): string {
.join(" | ");
args.push(["callParams", `CallParams$$<${generic}>`]);
}
const funcArgs = args
.map(([name, type]) => {

View File

@ -20,6 +20,8 @@ import { genTypeName, typeToTs } from "../common.js";
import { CLIENT } from "../constants.js";
import { capitalize, getFuncArgs } from "../utils.js";
import { DefaultServiceId } from "./service.js";
export interface TypeGenerator {
type(field: string, type: string): string;
generic(field: string, type: string): string;
@ -117,11 +119,18 @@ export class TSTypeGenerator implements TypeGenerator {
const serviceDecl = `service: ${srvName}Def`;
const serviceIdDecl = `serviceId: string`;
const registerServiceArgs = [
const registerServiceArgs =
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
(srvDef.defaultServiceId as DefaultServiceId).s_Some__f_value != null
? [
[serviceDecl],
[serviceIdDecl, serviceDecl],
[peerDecl, serviceDecl],
[peerDecl, serviceIdDecl, serviceDecl],
]
: [
[serviceIdDecl, serviceDecl],
[peerDecl, serviceIdDecl, serviceDecl],
];
return [

View File

@ -20,7 +20,7 @@ import { recursiveRenameLaquaProps } from "../utils.js";
import { TypeGenerator } from "./interfaces.js";
interface DefaultServiceId {
export interface DefaultServiceId {
s_Some__f_value?: string;
}

View File

@ -0,0 +1,120 @@
/**
* 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 {
ArrowWithoutCallbacks,
FunctionCallDef,
JSONValue,
SimpleTypes,
UnlabeledProductType,
} from "@fluencelabs/interfaces";
import { typeToTs } from "../common.js";
export function validateFunctionCall(
schema: FunctionCallDef,
...args: JSONValue[]
) {
const schemaArgs =
schema.arrow.domain.tag === "nil"
? []
: Object.values(schema.arrow.domain.fields);
if (args.length !== schemaArgs.length) {
throw new Error(
`Expected ${schemaArgs.length} arguments but provided ${args.length}`,
);
}
for (let i = 0; i < args.length; i++) {
validateFunctionCallArg(schemaArgs[i], args[i], i + 1);
}
}
export function validateFunctionCallArg(
schema: SimpleTypes | UnlabeledProductType | ArrowWithoutCallbacks,
arg: JSONValue,
argIndex: number,
) {
if (!isTypeMatchesSchema(schema, arg)) {
const expectedType = typeToTs(schema);
throw new Error(
`Argument ${argIndex} doesn't match schema. Expected type: ${expectedType}`,
);
}
}
export function isTypeMatchesSchema(
schema: SimpleTypes | UnlabeledProductType | ArrowWithoutCallbacks,
arg: JSONValue,
): boolean {
if (schema.tag === "nil") {
return arg === null;
} else if (schema.tag === "option") {
return arg === null || isTypeMatchesSchema(schema.type, arg);
} else if (schema.tag === "scalar") {
if (
[
"u8",
"u16",
"u32",
"u64",
"i8",
"i16",
"i32",
"i64",
"f32",
"f64",
].includes(schema.name)
) {
return typeof arg === "number";
} else if (schema.name === "bool") {
return typeof arg === "boolean";
} else if (schema.name === "string") {
return typeof arg === "string";
} else {
// Should not be possible
return false;
}
} else if (schema.tag === "array") {
return (
Array.isArray(arg) &&
arg.every((item) => {
return isTypeMatchesSchema(schema.type, item);
})
);
} else if (schema.tag === "struct") {
return (
!Array.isArray(arg) &&
typeof arg === "object" &&
arg !== null &&
Object.entries(schema.fields).every(([field, type]) => {
return isTypeMatchesSchema(type, arg[field]);
})
);
} else if (schema.tag === "unlabeledProduct") {
return (
Array.isArray(arg) &&
arg.every((item, index) => {
return isTypeMatchesSchema(schema.items[index], item);
})
);
} else if (schema.tag === "arrow") {
return typeof arg === "function";
} else {
return schema.tag === "topType";
}
}

View File

@ -154,7 +154,13 @@ export type ProductType = UnlabeledProductType | LabeledProductType;
* ArrowType is a profunctor pointing its domain to codomain.
* Profunctor means variance: Arrow is contravariant on domain, and variant on codomain.
*/
export type ArrowType<T extends LabeledProductType | UnlabeledProductType> = {
export type ArrowType<
T extends
| LabeledProductType<SimpleTypes | ArrowType<UnlabeledProductType>>
| UnlabeledProductType =
| LabeledProductType<SimpleTypes | ArrowType<UnlabeledProductType>>
| UnlabeledProductType,
> = {
/**
* Type descriptor. Used for pattern-matching
*/
@ -174,14 +180,14 @@ export type ArrowType<T extends LabeledProductType | UnlabeledProductType> = {
/**
* Arrow which domain contains only non-arrow types
*/
export type ArrowWithoutCallbacks = ArrowType<
UnlabeledProductType | LabeledProductType<SimpleTypes>
>;
export type ArrowWithoutCallbacks = ArrowType<UnlabeledProductType>;
/**
* Arrow which domain does can contain both non-arrow types and arrows (which themselves cannot contain arrows)
*/
export type ArrowWithCallbacks = ArrowType<LabeledProductType>;
export type ArrowWithCallbacks = ArrowType<
LabeledProductType<SimpleTypes | ArrowWithoutCallbacks>
>;
export interface FunctionCallConstants {
/**
@ -232,9 +238,7 @@ export interface FunctionCallDef {
/**
* Underlying arrow which represents function in aqua
*/
arrow: ArrowType<
LabeledProductType<SimpleTypes | ArrowType<UnlabeledProductType>>
>;
arrow: ArrowWithCallbacks;
/**
* Names of the different entities used in generated air script

View File

@ -25,7 +25,9 @@ import type {
ArrowWithoutCallbacks,
JSONArray,
JSONValue,
LabeledProductType,
NonArrowType,
SimpleTypes,
} from "@fluencelabs/interfaces";
import { match } from "ts-pattern";
@ -95,7 +97,7 @@ export const aqua2ts = (value: JSONValue, type: NonArrowType): JSONValue => {
*/
export const aquaArgs2Ts = (
req: CallServiceData,
arrow: ArrowWithoutCallbacks,
arrow: ArrowType<LabeledProductType<SimpleTypes>>,
): JSONArray => {
const argTypes = match(arrow.domain)
.with({ tag: "labeledProduct" }, (x) => {
@ -187,6 +189,7 @@ export const returnType2Aqua = (
returnValue: any,
arrowType: ArrowType<NonArrowType>,
) => {
// TODO: cover with tests
if (arrowType.codomain.tag === "nil") {
return {};
}

View File

@ -0,0 +1,28 @@
/**
* 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 { JSONArray, JSONValue } from "@fluencelabs/interfaces";
import { ParticleContext } from "../jsServiceHost/interfaces.js";
export type MaybePromise<T> = T | Promise<T>;
export type ServiceImpl = Record<
string,
(
...args: [...JSONArray, ParticleContext]
) => MaybePromise<JSONValue | undefined>
>;

6697
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["**/src/**/*"]
"include": ["packages"],
"exclude": ["node_modules", "dist", "build"]
}