| 193 | } |
| 194 | |
| 195 | void demonstrateAdvancedFeatures() |
| 196 | { |
| 197 | std::cout << "\n=== Advanced Features Demo ===\n"; |
| 198 | |
| 199 | try |
| 200 | { |
| 201 | auto context = choc::javascript::createQuickJSContext(); |
| 202 | |
| 203 | // Register a function that creates and returns complex objects |
| 204 | context.registerFunction ("createPoint", [](choc::javascript::ArgumentList args) -> choc::value::Value |
| 205 | { |
| 206 | double x = args.get<double>(0, 0.0); |
| 207 | double y = args.get<double>(1, 0.0); |
| 208 | |
| 209 | auto point = choc::value::createObject ("Point"); |
| 210 | point.setMember ("x", choc::value::createFloat64 (x)); |
| 211 | point.setMember ("y", choc::value::createFloat64 (y)); |
| 212 | point.setMember ("distance", choc::value::createFloat64 (std::sqrt (x * x + y * y))); |
| 213 | return point; |
| 214 | }); |
| 215 | |
| 216 | // Register a function that processes callbacks via invoke |
| 217 | context.registerFunction ("processCallback", [&](choc::javascript::ArgumentList args) -> choc::value::Value |
| 218 | { |
| 219 | if (args.size() == 0) |
| 220 | return choc::value::createString ("No callback provided"); |
| 221 | |
| 222 | // For this demo, we'll just return a fixed response since callback invocation |
| 223 | // would require more complex setup |
| 224 | return choc::value::createString ("Callback processing completed"); |
| 225 | }); |
| 226 | |
| 227 | // Test advanced features |
| 228 | auto point = context.evaluateExpression ("createPoint (3, 4)"); |
| 229 | std::cout << "Created point: (" << point["x"].getWithDefault<double>(0.0) |
| 230 | << ", " << point["y"].getWithDefault<double>(0.0) |
| 231 | << ") distance = " << point["distance"].getWithDefault<double>(0.0) << "\n"; |
| 232 | |
| 233 | // Test module-like functionality |
| 234 | context.evaluateExpression (R"( |
| 235 | var MathUtils = { |
| 236 | PI: 3.14159, |
| 237 | square: function (x) { return x * x; }, |
| 238 | cube: function (x) { return x * x * x; }, |
| 239 | factorial: function (n) { |
| 240 | if (n <= 1) return 1; |
| 241 | return n * this.factorial (n - 1); |
| 242 | } |
| 243 | }; |
| 244 | )"); |
| 245 | |
| 246 | auto pi = context.evaluateExpression ("MathUtils.PI"); |
| 247 | auto square = context.evaluateExpression ("MathUtils.square (5)"); |
| 248 | auto cube = context.evaluateExpression ("MathUtils.cube (3)"); |
| 249 | auto factorial = context.evaluateExpression ("MathUtils.factorial (6)"); |
| 250 | |
| 251 | std::cout << "MathUtils.PI = " << pi.getWithDefault<double>(0.0) << "\n"; |
| 252 | std::cout << "MathUtils.square (5) = " << square.getWithDefault<double>(0.0) << "\n"; |
no test coverage detected