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

Function tutorial

tutorials/integration/cpp/16_sandbox.cpp:143–321  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141#define SCRIPT_NAME "/tutorials/integration/cpp/16_sandbox.das"
142
143void tutorial() {
144 TextPrinter tout;
145 string scriptPath = getDasRoot() + SCRIPT_NAME;
146
147 // ================================================================
148 // Demo 1: No restrictions (normal compilation)
149 // ================================================================
150 tout << "=== Demo 1: No restrictions ===\n";
151 {
152 auto fAccess = make_smart<FsFileAccess>();
153 CodeOfPolicies policies;
154 run_script("Normal mode", scriptPath, fAccess, policies, tout);
155 }
156
157 // ================================================================
158 // Demo 2: CodeOfPolicies restrictions
159 // ================================================================
160 tout << "\n=== Demo 2: Policies — no_unsafe ===\n";
161 {
162 auto fAccess = make_smart<FsFileAccess>();
163
164 // Policy: forbid unsafe blocks
165 CodeOfPolicies policies;
166 policies.no_unsafe = true;
167
168 // The safe script should compile fine
169 run_script("Safe script with no_unsafe", scriptPath,
170 fAccess, policies, tout);
171
172 // An unsafe script should fail to compile
173 tout << "\n";
174 auto fAccessUnsafe = make_smart<FsFileAccess>();
175 fAccessUnsafe->setFileInfo("unsafe_test.das",
176 make_unique<TextFileInfo>(UNSAFE_SCRIPT,
177 uint32_t(strlen(UNSAFE_SCRIPT)),
178 false));
179 run_script("Unsafe script with no_unsafe",
180 "unsafe_test.das", fAccessUnsafe, policies, tout);
181 }
182
183 // ================================================================
184 // Demo 3: Memory limits
185 // ================================================================
186 tout << "\n=== Demo 3: Memory limits ===\n";
187 {
188 auto fAccess = make_smart<FsFileAccess>();
189
190 CodeOfPolicies policies;
191 policies.stack = 8 * 1024; // 8 KB stack
192 policies.max_heap_allocated = 1024 * 1024; // 1 MB heap
193 policies.max_string_heap_allocated = 256*1024; // 256 KB strings
194
195 tout << " Stack: " << policies.stack << " bytes\n";
196 tout << " Max heap: " << policies.max_heap_allocated
197 << " bytes\n";
198 tout << " Max strings: " << policies.max_string_heap_allocated
199 << " bytes\n";
200

Callers 1

mainFunction · 0.70

Calls 3

getDasRootFunction · 0.85
run_scriptFunction · 0.85
setFileInfoMethod · 0.80

Tested by

no test coverage detected