Skip to content

Commit

Permalink
Playground: refactor instrumentation to allow specs (#4061)
Browse files Browse the repository at this point in the history
ensure instrumented code with prelude is able to compile.
  • Loading branch information
bcardiff committed Mar 6, 2017
1 parent f218af6 commit fab030c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
11 changes: 11 additions & 0 deletions spec/compiler/crystal/tools/playground_spec.cr
Expand Up @@ -571,3 +571,14 @@ describe Playground::AgentInstrumentorTransformer do
CR
end
end

private def assert_compile(source)
sources = Playground::Session.instrument_and_prelude("", "", 0, source, Logger.new(nil))
compiler = Compiler.new
compiler.no_codegen = true
result = compiler.compile sources, "fake-no-build"
end

describe Playground::Session do
it { assert_compile %(puts "1") }
end
31 changes: 18 additions & 13 deletions src/compiler/crystal/tools/playground/server.cr
Expand Up @@ -13,25 +13,17 @@ module Crystal::Playground
@tag = 0
end

def run(source, tag)
@logger.info "Request to run code (session=#{@session_key}, tag=#{tag}).\n#{source}"

@tag = tag
begin
ast = Parser.new(source).parse
rescue ex : Crystal::Exception
send_exception ex, tag
return
end
def self.instrument_and_prelude(session_key, port, tag, source, logger)
ast = Parser.new(source).parse

instrumented = Playground::AgentInstrumentorTransformer.transform(ast).to_s
@logger.info "Code instrumentation (session=#{@session_key}, tag=#{tag}).\n#{instrumented}"
logger.info "Code instrumentation (session=#{session_key}, tag=#{tag}).\n#{instrumented}"

prelude = %(
require "compiler/crystal/tools/playground/agent"
class Crystal::Playground::Agent
@@instance = Crystal::Playground::Agent.new("ws://localhost:#{@port}/agent/#{@session_key}/#{tag}", #{tag})
@@instance = Crystal::Playground::Agent.new("ws://localhost:#{port}/agent/#{session_key}/#{tag}", #{tag})
def self.instance
@@instance
Expand All @@ -43,10 +35,23 @@ module Crystal::Playground
end
)

sources = [
[
Compiler::Source.new("playground_prelude", prelude),
Compiler::Source.new("play", instrumented),
]
end

def run(source, tag)
@logger.info "Request to run code (session=#{@session_key}, tag=#{tag}).\n#{source}"

@tag = tag
begin
sources = self.class.instrument_and_prelude(@session_key, @port, tag, source, @logger)
rescue ex : Crystal::Exception
send_exception ex, tag
return
end

output_filename = tempfile "play-#{@session_key}-#{tag}"
compiler = Compiler.new
compiler.color = false
Expand Down

0 comments on commit fab030c

Please sign in to comment.