MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / tutorial

Function tutorial

tutorials/integration/cpp/02_calling_functions.cpp:35–291  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33};
34
35void tutorial() {
36 TextPrinter tout;
37 ModuleGroup dummyLibGroup;
38 auto fAccess = make_smart<FsFileAccess>();
39
40 auto program = compileDaScript(getDasRoot() + SCRIPT_NAME,
41 fAccess, tout, dummyLibGroup);
42 if (program->failed()) {
43 tout << "Compilation failed:\n";
44 for (auto & err : program->errors) {
45 tout << reportError(err.at, err.what, err.extra,
46 err.fixme, err.cerr);
47 }
48 return;
49 }
50
51 Context ctx(program->getContextStackSize());
52 if (!program->simulate(ctx, tout)) {
53 tout << "Simulation failed:\n";
54 for (auto & err : program->errors) {
55 tout << reportError(err.at, err.what, err.extra,
56 err.fixme, err.cerr);
57 }
58 return;
59 }
60
61 // ===================================================================
62 // PART A — Low-level: cast<T> + evalWithCatch
63 //
64 // This is the manual approach. You pack arguments into a vec4f
65 // array using cast<T>::from, call evalWithCatch, and unpack the
66 // return value with cast<T>::to.
67 // ===================================================================
68 printf("=== Part A: Low-level (cast + evalWithCatch) ===\n");
69
70 // -------------------------------------------------------------------
71 // A1. int arguments, int return value
72 // -------------------------------------------------------------------
73 {
74 auto fnAdd = ctx.findFunction("add");
75 if (!fnAdd) { tout << "'add' not found\n"; return; }
76
77 // Optional: verify the signature at runtime.
78 // verifyCall<ReturnType, ArgType1, ArgType2, ...> checks that the
79 // function's debug info matches the expected C++ types.
80 // This is slow (walks debug info), so do it once — not in a loop.
81 if (!verifyCall<int32_t, int32_t, int32_t>(fnAdd->debugInfo, dummyLibGroup)) {
82 tout << "'add' has wrong signature\n";
83 return;
84 }
85
86 vec4f args[2];
87 args[0] = cast<int32_t>::from(17);
88 args[1] = cast<int32_t>::from(25);
89
90 vec4f ret = ctx.evalWithCatch(fnAdd, args);
91 if (auto ex = ctx.getException()) {
92 tout << "exception in add: " << ex << "\n";

Callers 1

mainFunction · 0.70

Calls 10

compileDaScriptFunction · 0.85
getDasRootFunction · 0.85
reportErrorFunction · 0.85
printfFunction · 0.85
toFunction · 0.85
getContextStackSizeMethod · 0.80
evalWithCatchMethod · 0.80
invokeFunction · 0.50
simulateMethod · 0.45
findFunctionMethod · 0.45

Tested by

no test coverage detected