Files
plainleaf/common/syscalls/index.ts
T
Zef Hemel b62cef6d92 Add tag definition and registration to ScriptEnvironment
- Introduced a new `TagDef` type to define tags with optional schema and metatable.
- Added `registerTag` method in `ScriptEnvironment` to register tags.
- Updated `indexSyscalls` to handle tag registration and enrich query results with tag metadata.
- Modified `queryLua` and `DataStore` to support an optional enricher function for enhanced data processing.
2025-04-04 16:56:27 +02:00

50 lines
1.3 KiB
TypeScript

import type { SysCallMapping } from "$lib/plugos/system.ts";
import type {
LuaCollectionQuery,
LuaQueryCollection,
} from "$common/space_lua/query_collection.ts";
import {
jsToLuaValue,
type LuaEnv,
type LuaStackFrame,
} from "$common/space_lua/runtime.ts";
import type { Client } from "../../web/client.ts";
import type { TagDef } from "$common/space_script.ts";
export function indexSyscalls(client: Client): SysCallMapping {
return {
"index.tag": (_ctx, tagName: string): LuaQueryCollection => {
return {
query: (
query: LuaCollectionQuery,
env: LuaEnv,
sf: LuaStackFrame,
): Promise<any[]> => {
return client.ds.luaQuery(
["idx", tagName],
query,
env,
sf,
(key, value: any) => {
const tag = key[1];
const tagDef = client.clientSystem.scriptEnv.tagDefs[tag];
if (!tagDef) {
// Return as is
return value;
}
// Convert to LuaTable
value = jsToLuaValue(value);
value.metatable = tagDef.metatable;
return value;
},
);
},
};
},
"index.defineTag": (_ctx, tagDef: TagDef) => {
client.clientSystem.scriptEnv.registerTag(tagDef);
},
};
}