Class: Superscript::Runner
- Inherits:
-
Object
- Object
- Superscript::Runner
- Defined in:
- lib/superscript/runner.rb
Instance Method Summary collapse
-
#initialize(path = nil) ⇒ Runner
constructor
A new instance of Runner.
- #run!(ctx, contents: nil) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Runner
Returns a new instance of Runner.
28 29 30 31 32 33 34 |
# File 'lib/superscript/runner.rb', line 28 def initialize path=nil @path = if path path else "<interactive>" end end |
Instance Method Details
#run!(ctx, contents: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/superscript/runner.rb', line 36 def run!(ctx, contents:nil) contents = File.read(@path) unless contents @armed = false trace = TracePoint.new do |tp| if ENV["SUPERSCRIPT_DEBUG"] p [@armed, tp.path, tp.lineno, tp.method_id, tp.event, tp.defined_class] end if tp.defined_class&.name == "BasicObject" && tp.method_id == :instance_eval if tp.event == :script_compiled @armed = true elsif tp.event == :c_return @armed = false end end if tp.event == :return && tp.defined_class.ancestors.include?(Superscript::Ctx) @armed = true end next unless @armed case tp.event when :class ::Superscript.error :tp_module_define, "Defining modules is not allowed" when :line lines = if tp.path == "<interactive>" contents.split("\n") else File.read(tp.path).split("\n") end line = lines[tp.lineno-1].lstrip puts "< #{tp.path}:#{tp.lineno-1}" puts line when :c_call # allow calls to these classes next if ["Array", "String","Float", "Integer"].include? tp.defined_class.name case tp.method_id when :singleton_method_added trace.disable ::Superscript.error :tp_singleton_method_added, "Deffining methods is not allowed" else trace.disable case tp.defined_class.name when "Class" ::Superscript.error :tp_class_define, "Defining classes is not allowed" else class_name = case tp.defined_class.inspect when "Kernel" "Kernel" when "Module" "Module" when "Exception" "Exception" else class_name_matches = tp.defined_class.inspect.match(/^#<.*:(.*)>$/) class_name_matches[1] end command_name = case class_name when "Kernel", "Module", "Exception" tp.method_id else "#{class_name}.#{tp.method_id}" end ::Superscript.error :tp_command_not_found, "Command not found '#{command_name}'" end end when :call # disable if calling some other file unless tp.path == @path @armed = false end if tp.defined_class.ancestors.include? Superscript::Ctx @armed = false end end end value = begin trace.enable v = ctx.instance_eval contents, @path trace.disable v rescue Exception => ex case ex.class.to_s when "SystemExit" exit ex.status when "NameError" print "#{@path}:#{ex.backtrace_locations.first.lineno} " puts ex..split(" for ").first else ::Superscript.error :exception, ex end ensure trace.disable end value end |