2023-04-03 21:52:40 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-02-13 17:41:35 +03:00
|
|
|
import type { CallResultsArray, InterpreterResult, RunParameters } from '@fluencelabs/avm';
|
|
|
|
import { deserializeAvmResult, serializeAvmArgs } from '@fluencelabs/avm';
|
2023-04-03 21:52:40 +04:00
|
|
|
import { IAvmRunner, IMarineHost, IWasmLoader } from '../marine/interfaces.js';
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
export class MarineBasedAvmRunner implements IAvmRunner {
|
2023-04-03 21:52:40 +04:00
|
|
|
constructor(private marine: IMarineHost, private avmWasmLoader: IWasmLoader) {}
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
async run(
|
|
|
|
runParams: RunParameters,
|
|
|
|
air: string,
|
|
|
|
prevData: Uint8Array,
|
|
|
|
data: Uint8Array,
|
|
|
|
callResults: CallResultsArray,
|
|
|
|
): Promise<InterpreterResult | Error> {
|
|
|
|
const args = serializeAvmArgs(runParams, air, prevData, data, callResults);
|
|
|
|
|
|
|
|
let avmCallResult: InterpreterResult | Error;
|
|
|
|
try {
|
|
|
|
const res = await this.marine.callService('avm', 'invoke', args, undefined);
|
|
|
|
avmCallResult = deserializeAvmResult(res);
|
|
|
|
} catch (e) {
|
|
|
|
avmCallResult = e instanceof Error ? e : new Error((e as any).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return avmCallResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
async start(): Promise<void> {
|
|
|
|
await this.marine.start();
|
|
|
|
await this.avmWasmLoader.start();
|
2023-03-11 00:03:34 +04:00
|
|
|
await this.marine.createService(this.avmWasmLoader.getValue(), 'avm');
|
2023-02-13 17:41:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop(): Promise<void> {}
|
|
|
|
}
|