Skip to content

Commit

Permalink
Add MiniRacer::Platform.set_flags! for the truffleruby backend
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Jan 7, 2025
1 parent 8d1e66c commit 43a7243
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions lib/mini_racer/truffleruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 43a7243

Please sign in to comment.