13using Array = Poco::JSON::Array;
25 std::map<std::string, std::vector<Function>>
functions;
29 std::map<std::string, const Contract*>
methods;
31 std::string localKey(
const std::string&
file,
const std::string&
scope,
const std::string&
name)
const {
37 Type variable(
const std::string&
file,
const std::string&
scope,
const std::string&
name)
const {
43 bool merge(Type& into,
const Type&
from) {
44 if (
from.name.empty() ||
from.name ==
"null")
return false;
46 if (into.name.empty() || into.name ==
"null")
47 into.name =
from.name;
48 else if (into.name !=
from.name)
49 into.name =
"dynamic";
50 into.values.insert(
from.values.begin(),
from.values.end());
51 into.identities.insert(
from.identities.begin(),
from.identities.end());
52 into.hosts.insert(
from.hosts.begin(),
from.hosts.end());
53 if (into.values.size() > 8 || into.identities.size() > 8 || into.hosts.size() > 8) {
54 into.name =
"dynamic";
56 into.identities.clear();
59 return before.name != into.name || before.values != into.values || before.identities != into.identities ||
60 before.hosts != into.hosts;
62 const Contract*
contract(
const Expression& expression,
const std::string&
file,
const std::string&
scope,
63 unsigned depth = 0)
const {
64 if (
depth >= 64 || expression.kind !=
"call" || expression.operands.empty())
return nullptr;
65 const auto& callee = expression.operands[0];
66 if (callee.kind !=
"member" || callee.operands.empty())
return nullptr;
72 Type
type(
const Expression& expression,
const std::string&
file,
const std::string&
scope,
73 unsigned depth = 0)
const {
74 if (
depth >= 64)
return {};
75 if (expression.kind ==
"string")
return {
"string", {expression.value}, {}, {}};
76 if (expression.kind ==
"number")
return {
"number", {}, {}, {}};
77 if (expression.kind ==
"identifier") {
78 if (expression.value ==
"null")
return {
"null", {}, {}, {}};
79 return variable(
file,
scope, expression.value);
81 if (expression.kind !=
"call" || expression.operands.empty())
return {};
82 const auto& callee = expression.operands[0];
83 if (callee.kind ==
"member" && !callee.operands.empty() && callee.operands[0].kind ==
"identifier" &&
84 callee.operands[0].value ==
"eve" && !
variables.contains(localKey(
file,
scope,
"eve")) &&
86 auto factory =
factories.find(callee.value);
87 if (factory !=
factories.end() && expression.operands.size() == 1)
88 return {
"module:" + factory->second, {}, {}, {}};
94 api->semantics && !api->semantics->resultClass.empty() ? api->semantics->resultClass : api->returnType;
95 if (
value.name ==
"dynamic")
value.name.clear();
96 if (api->semantics && api->semantics->role ==
"scene_ref") {
97 value.identities = argument(expression, api->semantics->identityArgument,
file,
scope).values;
98 value.hosts = argument(expression, api->semantics->relatedArgument,
file,
scope).values;
102 Type argument(
const Expression& call,
int index,
const std::string&
file,
const std::string&
scope)
const {
103 if (
index < 0 ||
static_cast<size_t>(
index + 1) >= call.operands.size())
return {};
114 Object::Ptr
node(
const std::string&
id,
const std::string&
kind, Object::Ptr
origin = {}) {
117 if (
snapshot.nodes.size() >= 16384)
throw std::runtime_error(
"Automatic graph node budget exceeded");
119 value->set(
"id",
id);
121 value->set(
"label",
id);
122 value->set(
"status",
"resolved");
123 value->set(
"evidence",
"compiler-derived");
124 value->set(
"semantic",
true);
131 snapshot.nodes.at(
from)->getValue<std::string>(
"status") !=
"resolved" ||
132 snapshot.nodes.at(
to)->getValue<std::string>(
"status") !=
"resolved")
136 found->second->set(
"occurrences",
found->second->getValue<
unsigned>(
"occurrences") + 1);
139 if (
snapshot.edges.size() >= 32768)
throw std::runtime_error(
"Automatic graph edge budget exceeded");
145 value->set(
"evidence",
"typed-source-candidate");
146 value->set(
"sourceCoverage",
"representative-source");
147 value->set(
"occurrences", 1u);
152 Object::Ptr sceneEntity(
const std::string&
host,
const std::string&
nodeId, Object::Ptr
origin,
153 const std::string& displayName = {},
const std::string&
coverage =
"script-ref-only") {
156 item->set(
"sceneHost",
host);
157 item->set(
"nodeId",
nodeId);
158 if (!displayName.empty())
159 item->set(
"label", displayName);
160 else if (!item->has(
"label") || item->getValue<std::string>(
"label") ==
id)
161 item->set(
"label",
nodeId);
162 const auto existing = item->optValue<std::string>(
"definitionCoverage",
"");
163 if (existing.empty() || existing ==
"script-ref-only" ||
coverage !=
"script-ref-only")
164 item->set(
"definitionCoverage",
coverage);
167 std::string functionId(
const std::string&
name)
const {
170 return (
found->second[0].file.starts_with(
"tests/") ?
"test:" :
"script:") +
name;
174 if (api.semantics)
factories[api.semantics->factory] = api.module;
175 if (!api.scriptClass.empty()) {
176 const auto key = api.scriptClass +
'\0' + api.method;
187 slot.name =
"module:" + entry.second;
191 if (!
file->error.empty())
continue;
192 for (
const auto&
value :
file->structure.classes) {
194 if (item->has(
"fields") || item->getValue<std::string>(
"status") ==
"ambiguous") {
195 item->set(
"status",
"ambiguous");
196 item->remove(
"fields");
199 field->set(
"status",
"ambiguous");
200 field->remove(
"type");
201 field->remove(
"defaultValue");
213 auto fieldId =
"field:" +
value.name +
"." +
binding.name;
215 indexed->set(
"type",
field->
get(
"type"));
216 indexed->set(
"initializerKind",
binding.expression.kind);
217 if (
binding.expression.kind ==
"string" ||
binding.expression.kind ==
"number")
218 indexed->set(
"defaultValue",
binding.expression.value);
221 item->set(
"fields",
fields);
233 for (
int pass = 0; pass < 16; ++pass) {
236 if (!
file->error.empty())
continue;
240 for (
const auto& call :
file->structure.
calls) {
241 const auto& expression = call.expression;
242 if (expression.operands.empty() || expression.operands[0].kind !=
"identifier")
continue;
246 for (
size_t p = 0;
p <
target.syntax->parameters.size() &&
p + 1 < expression.operands.size(); ++
p)
249 type(expression.operands[
p + 1],
path, call.scope));
260 for (
int pass = 0; pass < 16; ++pass)
265 if (locations.size() != 1) {
266 Object::Ptr diagnostic =
new Object();
267 diagnostic->set(
"code",
"ambiguous_function");
268 diagnostic->set(
"id",
name);
269 snapshot.diagnostics.push_back(diagnostic);
272 const auto& location = locations[0];
273 auto item =
node(functionId(
name), location.file.starts_with(
"tests/") ?
"test" :
"script",
279 auto inferred = variable(location.file,
name,
parameter.name);
280 value->set(
"type", inferred.name.empty() ?
"dynamic" : inferred.
name);
284 const auto dot =
name.rfind(
'.');
285 if (dot != std::string::npos)
286 edge(
"class:" +
name.substr(0, dot), functionId(
name),
"declares",
287 source(location.file, location.syntax->line));
290 std::string jsonType(
const Poco::Dynamic::Var&
value)
const {
291 if (
value.type() ==
typeid(Object::Ptr))
return "object";
292 if (
value.type() ==
typeid(Array::Ptr))
return "array";
293 if (
value.isString())
return "string";
294 if (
value.isBoolean())
return "boolean";
295 if (
value.isInteger())
return "integer";
296 return value.isNumeric() ?
"number" :
"unknown";
298 void schemaObjects(
const Poco::Dynamic::Var&
value,
const std::string&
path,
const std::string&
pointer,
299 Object::Ptr schema,
const std::string& apiKey) {
300 if (
value.type() ==
typeid(Object::Ptr)) {
301 auto object =
value.extract<Object::Ptr>();
302 const auto identity = schema->getValue<std::string>(
"identityField");
304 for (
const auto& field : *schema->getObject(
"fields"))
308 auto id = schema->getValue<std::string>(
"kind") +
":" +
object->getValue<std::string>(
identity);
310 if (item->has(
"definition")) {
311 item->set(
"status",
"ambiguous");
312 item->remove(
"definition");
313 Object::Ptr diagnostic =
new Object();
314 diagnostic->set(
"code",
"ambiguous_schema_identity");
315 diagnostic->set(
"id",
id);
316 snapshot.diagnostics.push_back(diagnostic);
317 }
else if (item->getValue<std::string>(
"status") !=
"ambiguous")
318 item->set(
"definition",
object);
319 item->set(
"schemaContract", apiKey);
320 item->set(
"evidence",
"engine-schema-shape");
322 for (
const auto& field : *
object) {
327 }
else if (
value.type() ==
typeid(Array::Ptr)) {
328 auto array =
value.extract<Array::Ptr>();
329 for (
size_t i = 0; i < array->size(); ++i)
330 schemaObjects(array->get(
static_cast<unsigned>(i)),
path,
pointer +
"/" + std::to_string(i), schema,
338 if (!api.semantics)
continue;
339 version += api.key() +
'\0' + api.toJson() +
'\0';
340 if (!api.semantics->schemaJson.empty()) {
341 Poco::JSON::Parser parser;
342 auto schema = parser.parse(api.semantics->schemaJson).extract<Object::Ptr>();
345 schemaObjects(
file->json,
path,
"", schema, api.
key());
348 std::map<std::string, std::set<std::string>> nativeByFunction, calledFunctions;
355 if (!
file->error.empty())
continue;
356 std::map<std::string, std::set<std::string>> filters, subjects, mounts;
357 std::map<std::string, std::vector<SceneDefine>> defines;
358 std::map<std::string, std::vector<std::string>> openStacks;
359 int unresolvedDefines = 0;
360 for (
const auto& call :
file->structure.
calls) {
361 const auto& expression = call.expression;
362 if (expression.operands.empty())
continue;
363 auto from = functionId(call.scope);
365 const auto& callee = expression.operands[0];
366 if (callee.kind ==
"identifier") {
367 auto target = functionId(callee.value);
373 const auto* api =
contract(expression,
path, call.scope);
374 if (!api && !
from.empty() && callee.kind ==
"member" &&
375 !
type(expression,
path, call.scope).name.starts_with(
"module:")) {
377 auto unknowns = item->getArray(
"unknownCalls");
379 unknowns =
new Array();
380 item->set(
"unknownCalls", unknowns);
382 if (unknowns->size() < 16) {
383 Object::Ptr unknown =
new Object();
384 unknown->set(
"method", callee.value);
385 unknown->set(
"source",
origin);
386 unknown->set(
"reason",
"receiver type unresolved or method ambiguous");
387 unknowns->add(unknown);
389 item->set(
"unknownCallsTruncated",
true);
392 auto apiId =
"api:" + api->key();
393 auto item =
node(apiId,
"engine_api");
394 Poco::JSON::Parser parser;
395 item->set(
"contract", parser.parse(api->toJson()));
396 item->set(
"evidence",
"engine-build-contract");
399 nativeByFunction[
from].insert(apiId);
401 if (!api->semantics || api->semantics->role.empty())
continue;
402 const auto& rule = *api->semantics;
403 auto identity = argument(expression, rule.identityArgument,
path, call.scope);
404 if (rule.role ==
"scene_ref" &&
identity.values.size() == 1) {
405 auto host = argument(expression, rule.relatedArgument,
path, call.scope);
407 }
else if ((rule.role ==
"scene_define" || rule.role ==
"scene_leaf") &&
identity.values.size() == 1) {
408 auto& stack = openStacks[call.scope];
409 auto related = argument(expression, rule.relatedArgument,
path, call.scope);
411 if (related.values.size() == 1) display = *related.values.begin();
412 const bool opens = rule.role ==
"scene_define";
413 if (!opens && stack.empty()) {
416 const auto parent = stack.empty() ?
"" : stack.back();
418 if (opens) stack.push_back(*
identity.values.begin());
420 }
else if (rule.role ==
"scene_pop") {
421 auto& stack = openStacks[call.scope];
426 }
else if (rule.role ==
"scene_mount" &&
identity.values.size() == 1) {
427 mounts[call.scope].insert(*
identity.values.begin());
428 }
else if (rule.role ==
"resource_load" &&
identity.values.size() == 1) {
429 const auto rel = *
identity.values.begin();
431 if (
sourcePath(rel).is_absolute() || norm.starts_with(
"../") ||
432 norm.find(
"/../") != std::string::npos) {
433 Object::Ptr diagnostic =
new Object();
434 diagnostic->set(
"code",
"resource_path_escaped");
435 diagnostic->set(
"path", rel);
436 diagnostic->set(
"loaderApi", api->key());
437 snapshot.diagnostics.push_back(diagnostic);
439 auto assetId =
"asset:" + rel;
440 auto asset =
node(assetId,
"project_asset",
origin);
441 asset->set(
"path", rel);
442 asset->set(
"loaderApi", api->key());
443 asset->set(
"label", rel);
444 std::string presence =
"unchecked";
446 std::error_code
error;
448 presence = std::filesystem::is_regular_file(full,
error) && !
error ?
"present" :
"absent";
450 asset->set(
"filePresence", presence);
453 }
else if (rule.role ==
"scene_write" && callee.kind ==
"member" && !callee.operands.empty()) {
455 if (
target.identities.size() == 1 &&
target.hosts.size() == 1) {
460 item->set(
"targetCoverage",
"dynamic-or-ambiguous");
461 }
else if (rule.role ==
"event_emit" &&
identity.values.size() == 1) {
462 auto eventId =
"event:" + *
identity.values.begin();
465 auto subject = argument(expression, rule.relatedArgument,
path, call.scope);
466 if (
subject.values.size() == 1)
468 }
else if ((rule.role ==
"event_type" || rule.role ==
"event_subject") && !call.comparison.empty() &&
469 call.comparedValue.kind ==
"string") {
470 if (rule.role ==
"event_type") {
471 auto id =
"event:" + call.comparedValue.value;
474 filters[call.scope].insert(
id);
476 subjects[call.scope].insert(call.comparedValue.value);
477 }
else if (rule.role ==
"quest_read" || rule.role ==
"quest_complete") {
480 rule.role ==
"quest_read" ?
"read_by" :
"completion_candidate",
origin);
486 for (
const auto& [
scope, items] : defines) {
487 const auto hostIt = mounts.find(
scope);
488 if (hostIt == mounts.end() || hostIt->second.size() != 1) {
489 unresolvedDefines +=
static_cast<int>(items.size());
492 if (!openStacks[
scope].empty()) unresolvedDefines +=
static_cast<int>(openStacks[
scope].
size());
493 const auto&
host = *hostIt->second.begin();
494 for (
const auto& define : items) {
496 sceneEntity(
host, define.nodeId, define.origin, define.displayName,
"script-beginNode+mount");
497 if (!define.parentId.empty()) {
498 entity->set(
"parentNodeId", define.parentId);
499 auto parent = sceneEntity(
host, define.parentId, define.origin);
500 edge(
parent->getValue<std::string>(
"id"),
entity->getValue<std::string>(
"id"),
"contains",
503 if (!define.from.empty())
504 edge(define.from,
entity->getValue<std::string>(
"id"),
"defines", define.origin);
507 if (unresolvedDefines &&
snapshot.stats) {
508 snapshot.stats->set(
"sceneDefinesUnresolved",
509 snapshot.stats->optValue<
int>(
"sceneDefinesUnresolved", 0) + unresolvedDefines);
512 for (const auto& event :
events)
519 if (locations.size() != 1 || !locations[0].file.starts_with(
"tests/"))
continue;
520 std::set<std::string> reached{functionId(
name)},
checked;
522 auto before = reached.size();
528 if (reached.size() == before)
break;
530 for (
const auto& api :
checked)