@@ -85,6 +85,7 @@ void Engine::clear()
8585 m_broadcastMap.clear ();
8686 m_broadcastSenders.clear ();
8787 m_sortedDrawables.clear ();
88+ m_unsafeConstants.clear ();
8889 m_threads.clear ();
8990 m_threadsToStop.clear ();
9091 m_scripts.clear ();
@@ -245,10 +246,41 @@ void Engine::compile()
245246 // Resolve entities by ID
246247 resolveIds ();
247248
249+ // Find unsafe constants (string numerals) in costume and sound names
250+ m_unsafeConstants.clear ();
251+
252+ for (auto target : m_targets) {
253+ IEngine *engine = target->engine ();
254+
255+ if (engine) {
256+ auto checkConstant = [this ](const std::string &str) {
257+ Value stringValue (str);
258+ Value numberValue (stringValue.toDouble ());
259+
260+ if (stringValue.isValidNumber () && str == numberValue.toString ())
261+ m_unsafeConstants.insert (str);
262+ };
263+
264+ const auto &targets = engine->targets ();
265+ bool found = false ;
266+
267+ for (const auto &target : targets) {
268+ const auto &costumes = target->costumes ();
269+ const auto &sounds = target->sounds ();
270+
271+ for (const auto &costume : costumes)
272+ checkConstant (costume->name ());
273+
274+ for (const auto &sound : sounds)
275+ checkConstant (sound->name ());
276+ }
277+ }
278+ }
279+
248280 // Compile scripts
249281 for (auto target : m_targets) {
250282 std::cout << " Compiling scripts in target " << target->name () << " ..." << std::endl;
251- auto ctx = Compiler::createContext (this , target.get ());
283+ auto ctx = Compiler::createContext (this , target.get (), m_unsafeConstants );
252284 m_compilerContexts[target.get ()] = ctx;
253285 Compiler compiler (ctx.get ());
254286 const auto &blocks = target->blocks ();
@@ -1789,7 +1821,7 @@ void Engine::compileMonitor(std::shared_ptr<Monitor> monitor)
17891821 auto ext = blockExtension (block->opcode ());
17901822
17911823 if (ext) {
1792- auto ctx = Compiler::createContext (this , target);
1824+ auto ctx = Compiler::createContext (this , target, m_unsafeConstants );
17931825 Compiler compiler (ctx.get ());
17941826 MonitorNameFunc nameFunc = resolveMonitorNameFunc (ext, block->opcode ());
17951827
0 commit comments