1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: MIT
4
5import { JSON } from "json-as";
6import { __server } from "../server";
7import {
8 Call,
9 CallRes,
10 Commit,
11 ConfPlugin,
12 File,
13 PluginSdkType,
14} from "./index";
15
16export function init(
17 repoNamePtr: i32,
18 repoNameSize: i32,
19 initKindPtr: i32,
20 initKindSize: i32,
21 confPtr: i32,
22 confSize: i32,
23): void {
24 const repoName = String.UTF8.decodeUnsafe(repoNamePtr, repoNameSize);
25 const serializedConf = String.UTF8.decodeUnsafe(confPtr, confSize);
26 const initKind = String.UTF8.decodeUnsafe(initKindPtr, initKindSize);
27 if (__server && __server!.plugin) {
28 __server!.plugin!.init(repoName, initKind, serializedConf);
29 }
30}
31
32export function startCommit(commitPtr: i32, commitSize: i32): void {
33 if (__server && __server!.plugin) {
34 const commit = JSON.parse<Commit>(
35 String.UTF8.decodeUnsafe(commitPtr, commitSize),
36 );
37 __server!.plugin!.startCommit(commit);
38 }
39}
40
41export function addFile(ptr: i32, size: i32): void {
42 if (__server && __server!.plugin) {
43 const file = JSON.parse<File>(String.UTF8.decodeUnsafe(ptr, size));
44 __server!.plugin!.addFile(file);
45 }
46}
47
48export function modFile(ptr: i32, size: i32): void {
49 if (__server && __server!.plugin) {
50 const file = JSON.parse<File>(String.UTF8.decodeUnsafe(ptr, size));
51 __server!.plugin!.modFile(file);
52 }
53}
54
55export function delFile(ptr: i32, size: i32): void {
56 if (__server && __server!.plugin) {
57 const file = JSON.parse<File>(String.UTF8.decodeUnsafe(ptr, size));
58 __server!.plugin!.delFile(file);
59 }
60}
61
62export function endCommit(commitPtr: i32, commitSize: i32): void {
63 if (__server && __server!.plugin) {
64 const commit = JSON.parse<Commit>(
65 String.UTF8.decodeUnsafe(commitPtr, commitSize),
66 );
67 __server!.plugin!.endCommit(commit);
68 }
69}
70
71export function finish(): void {
72 if (__server && __server!.plugin) {
73 __server!.plugin!.finish();
74 }
75}
76
77export function report(iteratorNamePtr: i32, iteratorNameSize: i32): void {
78 if (__server && __server!.plugin) {
79 __server!.reportToPlugin(
80 String.UTF8.decodeUnsafe(iteratorNamePtr, iteratorNameSize),
81 );
82 }
83}
84
85export function call(jsonPtr: i32, jsonSize: i32): string {
86 if (__server) {
87 const call = JSON.parse<Call>(String.UTF8.decodeUnsafe(jsonPtr, jsonSize));
88 const callRes = new CallRes(new Map(), "method not found");
89 const callback = __server!.getExportedFuncs(call.name);
90 if (callback != null) {
91 const res = callback(call.args);
92 callRes.res = res;
93 }
94 return JSON.stringify(callRes);
95 }
96 return "";
97}
98
99export function pluginConf(): string {
100 if (__server) {
101 const conf = new ConfPlugin(
102 __server!.run,
103 "0.5.0",
104 PluginSdkType.Assemblyscript,
105 );
106 return JSON.stringify<ConfPlugin>(conf);
107 }
108 return "{}";
109}
110
111export function malloc(size: usize): usize {
112 const ptr = __new(size, 1);
113 // @ts-ignore
114 __pin(ptr);
115 return ptr;
116}
117
118export function free(ptr: usize): void {
119 // @ts-ignore
120 __unpin(ptr);
121}