Skip to content

Commit fab030c

Browse files
authoredMar 6, 2017
Playground: refactor instrumentation to allow specs (#4061)
ensure instrumented code with prelude is able to compile.
1 parent f218af6 commit fab030c

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed
 

‎spec/compiler/crystal/tools/playground_spec.cr

+11
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,14 @@ describe Playground::AgentInstrumentorTransformer do
571571
CR
572572
end
573573
end
574+
575+
private def assert_compile(source)
576+
sources = Playground::Session.instrument_and_prelude("", "", 0, source, Logger.new(nil))
577+
compiler = Compiler.new
578+
compiler.no_codegen = true
579+
result = compiler.compile sources, "fake-no-build"
580+
end
581+
582+
describe Playground::Session do
583+
it { assert_compile %(puts "1") }
584+
end

‎src/compiler/crystal/tools/playground/server.cr

+18-13
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,17 @@ module Crystal::Playground
1313
@tag = 0
1414
end
1515

16-
def run(source, tag)
17-
@logger.info "Request to run code (session=#{@session_key}, tag=#{tag}).\n#{source}"
18-
19-
@tag = tag
20-
begin
21-
ast = Parser.new(source).parse
22-
rescue ex : Crystal::Exception
23-
send_exception ex, tag
24-
return
25-
end
16+
def self.instrument_and_prelude(session_key, port, tag, source, logger)
17+
ast = Parser.new(source).parse
2618

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

3022
prelude = %(
3123
require "compiler/crystal/tools/playground/agent"
3224
3325
class Crystal::Playground::Agent
34-
@@instance = Crystal::Playground::Agent.new("ws://localhost:#{@port}/agent/#{@session_key}/#{tag}", #{tag})
26+
@@instance = Crystal::Playground::Agent.new("ws://localhost:#{port}/agent/#{session_key}/#{tag}", #{tag})
3527
3628
def self.instance
3729
@@instance
@@ -43,10 +35,23 @@ module Crystal::Playground
4335
end
4436
)
4537

46-
sources = [
38+
[
4739
Compiler::Source.new("playground_prelude", prelude),
4840
Compiler::Source.new("play", instrumented),
4941
]
42+
end
43+
44+
def run(source, tag)
45+
@logger.info "Request to run code (session=#{@session_key}, tag=#{tag}).\n#{source}"
46+
47+
@tag = tag
48+
begin
49+
sources = self.class.instrument_and_prelude(@session_key, @port, tag, source, @logger)
50+
rescue ex : Crystal::Exception
51+
send_exception ex, tag
52+
return
53+
end
54+
5055
output_filename = tempfile "play-#{@session_key}-#{tag}"
5156
compiler = Compiler.new
5257
compiler.color = false

0 commit comments

Comments
 (0)
Please sign in to comment.