Last active
March 12, 2018 13:23
-
-
Save sasa1977/c03f3b86382d19ef4ec3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmd = ~S""" | |
ruby -e ' | |
require "bundler" | |
require "erlang/etf" | |
require "stringio" | |
STDOUT.sync = true | |
def receive_input | |
encoded_length = STDIN.read(4) | |
return nil unless encoded_length | |
length = encoded_length.unpack("N").first | |
Erlang.binary_to_term(STDIN.read(length)) | |
end | |
def send_response(value) | |
response = Erlang.term_to_binary(Erlang::Tuple[:response, value]) | |
STDOUT.write([response.bytesize].pack("N")) | |
STDOUT.write(response) | |
true | |
end | |
context = binding | |
while (cmd = receive_input) do | |
if cmd.is_a?(Erlang::Tuple) && cmd[0] == :eval | |
eval(cmd[1], context) | |
end | |
end | |
' | |
""" | |
send_eval = fn(port, command) -> | |
Port.command(port, :erlang.term_to_binary({:eval, command})) | |
end | |
port = Port.open({:spawn, cmd}, [:binary, {:packet, 4}]) | |
send_eval.(port, "a = 1") | |
send_eval.(port, ~S""" | |
while a < 10 do | |
a *= 3 | |
end | |
""") | |
send_eval.(port, "send_response(a)") | |
receive do | |
{^port, {:data, result}} -> | |
IO.puts("Elixir got: #{inspect :erlang.binary_to_term(result)}") | |
end | |
Port.close(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment