From 43a7243c43c752891e0bc78db29489ea9a0933f3 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Tue, 7 Jan 2025 15:37:44 +0100 Subject: [PATCH] Add MiniRacer::Platform.set_flags! for the truffleruby backend --- lib/mini_racer/truffleruby.rb | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/mini_racer/truffleruby.rb b/lib/mini_racer/truffleruby.rb index f4e7bba..ff31b45 100644 --- a/lib/mini_racer/truffleruby.rb +++ b/lib/mini_racer/truffleruby.rb @@ -331,10 +331,37 @@ def idle_notification(idle_time) end class Platform - def self.set_flag_as_str!(flag) - raise TypeError, "wrong type argument #{flag.class} (should be a string)" unless flag.is_a?(String) - raise MiniRacer::PlatformAlreadyInitialized, "The platform is already initialized." if Context.instance_variable_get(:@context_initialized) - Context.instance_variable_set(:@use_strict, true) if "--use_strict" == flag + class << self + def set_flags!(*args, **kwargs) + flags_to_strings([args, kwargs]).each do |flag| + set_flag_as_str!(flag) + end + end + + private + + def flags_to_strings(flags) + flags.flatten.map { |flag| flag_to_string(flag) }.flatten + end + + # normalize flags to strings, and adds leading dashes if needed + def flag_to_string(flag) + if flag.is_a?(Hash) + flag.map do |key, value| + "#{flag_to_string(key)} #{value}" + end + else + str = flag.to_s + str = "--#{str}" unless str.start_with?('--') + str + end + end + + def set_flag_as_str!(flag) + raise TypeError, "wrong type argument #{flag.class} (should be a string)" unless flag.is_a?(String) + raise MiniRacer::PlatformAlreadyInitialized, "The platform is already initialized." if Context.instance_variable_get(:@context_initialized) + Context.instance_variable_set(:@use_strict, true) if "--use_strict" == flag + end end end