This commit is contained in:
Akim Mamedov
2023-11-17 03:22:21 +07:00
parent 1669750e0a
commit 05bec9db21
5 changed files with 40 additions and 23 deletions

View File

@ -50,19 +50,19 @@ export function registerHelloWorld(peer: IFluenceClient$$, serviceId: string, se
// Functions // Functions
export type ResourceTestResultType = [string | null, string[]] export type ResourceTestResultType = [string | null, string[]]
export type resourceTestParams = [label: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, label: string, config?: {ttl?: number}]; export type ResourceTestParams = [label: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, label: string, config?: {ttl?: number}];
export type ResourceTestResult = Promise<ResourceTestResultType>; export type ResourceTestResult = Promise<ResourceTestResultType>;
export type helloTestParams = [config?: {ttl?: number}] | [peer: IFluenceClient$$, config?: {ttl?: number}]; export type HelloTestParams = [config?: {ttl?: number}] | [peer: IFluenceClient$$, config?: {ttl?: number}];
export type HelloTestResult = Promise<string>; export type HelloTestResult = Promise<string>;
export type demo_calculationParams = [service_id: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, service_id: string, config?: {ttl?: number}]; export type Demo_calculationParams = [service_id: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, service_id: string, config?: {ttl?: number}];
export type Demo_calculationResult = Promise<number>; export type Demo_calculationResult = Promise<number>;
export type marineTestParams = [wasm64: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, wasm64: string, config?: {ttl?: number}]; export type MarineTestParams = [wasm64: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, wasm64: string, config?: {ttl?: number}];
export type MarineTestResult = Promise<number>; export type MarineTestResult = Promise<number>;

View File

@ -542,7 +542,7 @@ export const resourceTest_script = `
export type ResourceTestResultType = [string | null, string[]] export type ResourceTestResultType = [string | null, string[]]
export type resourceTestParams = [label: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, label: string, config?: {ttl?: number}]; export type ResourceTestParams = [label: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, label: string, config?: {ttl?: number}];
export type ResourceTestResult = Promise<ResourceTestResultType>; export type ResourceTestResult = Promise<ResourceTestResultType>;
@ -609,7 +609,7 @@ export const helloTest_script = `
) )
`; `;
export type helloTestParams = [config?: {ttl?: number}] | [peer: IFluenceClient$$, config?: {ttl?: number}]; export type HelloTestParams = [config?: {ttl?: number}] | [peer: IFluenceClient$$, config?: {ttl?: number}];
export type HelloTestResult = Promise<string>; export type HelloTestResult = Promise<string>;
@ -679,7 +679,7 @@ export const demo_calculation_script = `
) )
`; `;
export type demo_calculationParams = [service_id: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, service_id: string, config?: {ttl?: number}]; export type Demo_calculationParams = [service_id: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, service_id: string, config?: {ttl?: number}];
export type Demo_calculationResult = Promise<number>; export type Demo_calculationResult = Promise<number>;
@ -757,7 +757,7 @@ export const marineTest_script = `
) )
`; `;
export type marineTestParams = [wasm64: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, wasm64: string, config?: {ttl?: number}]; export type MarineTestParams = [wasm64: string, config?: {ttl?: number}] | [peer: IFluenceClient$$, wasm64: string, config?: {ttl?: number}];
export type MarineTestResult = Promise<number>; export type MarineTestResult = Promise<number>;

View File

@ -86,7 +86,9 @@ export class TSTypeGenerator implements TypeGenerator {
return [ return [
argsDesc.join("\n"), argsDesc.join("\n"),
resTypeDesc ?? "", resTypeDesc ?? "",
`export type ${funcDef.functionName}Params = ${functionOverloadArgsType};`, `export type ${capitalize(
funcDef.functionName,
)}Params = ${functionOverloadArgsType};`,
`export type ${capitalize( `export type ${capitalize(
funcDef.functionName, funcDef.functionName,
)}Result = Promise<${resType}>;\n`, )}Result = Promise<${resType}>;\n`,

View File

@ -25,6 +25,7 @@ import type {
import { CallAquaFunctionConfig } from "./compilerSupport/callFunction.js"; import { CallAquaFunctionConfig } from "./compilerSupport/callFunction.js";
import { import {
aqua2ts, aqua2ts,
SchemaValidationError,
ts2aqua, ts2aqua,
wrapFunction, wrapFunction,
} from "./compilerSupport/conversions.js"; } from "./compilerSupport/conversions.js";
@ -37,10 +38,11 @@ const isAquaConfig = (
config: JSONValue | ServiceImpl[string] | undefined, config: JSONValue | ServiceImpl[string] | undefined,
): config is CallAquaFunctionConfig => { ): config is CallAquaFunctionConfig => {
return ( return (
typeof config === "object" && config === undefined ||
(typeof config === "object" &&
config !== null && config !== null &&
!Array.isArray(config) && !Array.isArray(config) &&
["undefined", "number"].includes(typeof config["ttl"]) ["undefined", "number"].includes(typeof config["ttl"]))
); );
}; };
@ -60,14 +62,17 @@ export const v5_callFunction = async (
def: FunctionCallDef, def: FunctionCallDef,
script: string, script: string,
): Promise<unknown> => { ): Promise<unknown> => {
const [peer, ...rest] = args; const [peerOrArg, ...rest] = args;
if (!(peer instanceof FluencePeer)) { if (!(peerOrArg instanceof FluencePeer)) {
await v5_callFunction([getDefaultPeer(), ...rest], def, script); await v5_callFunction([getDefaultPeer(), peerOrArg, ...rest], def, script);
return; return;
} }
const argNames = Object.keys(def.arrow); const argNames = Object.keys(
def.arrow.domain.tag === "nil" ? [] : def.arrow.domain.fields,
);
const schemaArgCount = argNames.length; const schemaArgCount = argNames.length;
type FunctionArg = SimpleTypes | ArrowWithoutCallbacks; type FunctionArg = SimpleTypes | ArrowWithoutCallbacks;
@ -88,7 +93,12 @@ export const v5_callFunction = async (
if (argSchema.tag === "arrow") { if (argSchema.tag === "arrow") {
if (typeof arg !== "function") { if (typeof arg !== "function") {
throw new Error("Argument and schema don't match"); throw new SchemaValidationError(
[argNames[i]],
argSchema,
"function",
arg,
);
} }
const wrappedFunction = wrapFunction(arg, argSchema); const wrappedFunction = wrapFunction(arg, argSchema);
@ -97,7 +107,12 @@ export const v5_callFunction = async (
} }
if (typeof arg === "function") { if (typeof arg === "function") {
throw new Error("Argument and schema don't match"); throw new SchemaValidationError(
[argNames[i]],
argSchema,
"non-function value",
arg,
);
} }
return [ return [
@ -111,7 +126,7 @@ export const v5_callFunction = async (
def.arrow.codomain.tag === "nil" || def.arrow.codomain.items.length === 0; def.arrow.codomain.tag === "nil" || def.arrow.codomain.items.length === 0;
const params = { const params = {
peer, peer: peerOrArg,
args: callArgs, args: callArgs,
config, config,
}; };

View File

@ -28,12 +28,12 @@ import { ParticleContext } from "../jsServiceHost/interfaces.js";
import { ServiceImpl } from "./types.js"; import { ServiceImpl } from "./types.js";
class SchemaValidationError extends Error { export class SchemaValidationError extends Error {
constructor( constructor(
public path: string[], public path: string[],
schema: NonArrowSimpleType, schema: NonArrowSimpleType | ArrowWithoutCallbacks,
expected: string, expected: string,
provided: JSONValue, provided: JSONValue | ServiceImpl[string],
) { ) {
const given = const given =
provided === null provided === null