Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change type of Env.root to AmbientAuth #3962

Merged
merged 11 commits into from
Jan 16, 2022
21 changes: 21 additions & 0 deletions .release-notes/env-root-not-optional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Change type of Env.root to AmbientAuth

Previously, Env.root currently had the type (AmbientAuth | None) to allow for creation of artificial Env instances without AmbientAuth. Yet, there was no observable usage of this feature. It required extra work to make use of, as one always needs a pattern match or an as expression with a surrounding try. We've changed Env.root to only be AmbientAuth, no longer requiring a pattern match or a try.

To adjust your code to take this breaking change into account, you'd do the following. Where you previously had code like:

```pony
try
TCPListener(env.root as AmbientAuth, Listener(env.out))
else
env.out.print("unable to use the network")
end
```

You can now do:

```pony
TCPListener(env.root, Listener(env.out))
```

The same change can be made if you were pattern matching on the type of env.root.
6 changes: 1 addition & 5 deletions examples/echo/echo.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use "net"

actor Main
new create(env: Env) =>
try
TCPListener(env.root as AmbientAuth, Listener(env.out))
else
env.out.print("unable to use the network")
end
TCPListener(env.root, Listener(env.out))

class Listener is TCPListenNotify
let _out: OutStream
Expand Down
2 changes: 1 addition & 1 deletion examples/files/files.pony
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ actor Main

try
with file = OpenFile(
FilePath(env.root as AmbientAuth, env.args(1)?, caps)) as File
FilePath(env.root, env.args(1)?, caps)) as File
do
env.out.print(file.path.path)
for line in file.lines() do
Expand Down
7 changes: 1 addition & 6 deletions examples/mandelbrot/mandelbrot.pony
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ class val Config
limit = cmd.option("limit").f64().f32()
chunks = cmd.option("chunks").i64().usize()
width = cmd.option("width").i64().usize()
outpath =
try
FilePath(env.root as AmbientAuth, cmd.option("output").string())
else
None
end
outpath = FilePath(env.root, cmd.option("output").string())

new val none() =>
iterations = 0
Expand Down
23 changes: 7 additions & 16 deletions examples/net/listener.pony
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ class Listener is TCPListenNotify
listen.close()

fun ref connected(listen: TCPListener ref): TCPConnectionNotify iso^ =>
let env = _env

_env.out.print("Server starting")

let server = ServerSide(env)
let server = ServerSide(_env)

_spawn(listen)
server
Expand All @@ -45,16 +43,9 @@ class Listener is TCPListenNotify
_count = _count + 1
_env.out.print("spawn " + _count.string())

try
let env = _env

_env.out.print("Client starting")
TCPConnection(
_env.root as AmbientAuth,
ClientSide(env),
_host,
_service)
else
_env.out.print("couldn't create client side")
listen.close()
end
_env.out.print("Client starting")
TCPConnection(
_env.root,
ClientSide(_env),
_host,
_service)
9 changes: 2 additions & 7 deletions examples/net/net.pony
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@ actor Main
1
end

try
let auth = env.root as AmbientAuth
TCPListener(auth, recover Listener(env, limit) end)
UDPSocket(auth, recover Pong(env) end)
else
env.out.print("unable to use the network")
end
TCPListener(env.root, recover Listener(env, limit) end)
UDPSocket(env.root, recover Pong(env) end)
5 changes: 2 additions & 3 deletions examples/net/pong.pony
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ class Pong is UDPNotify
_env.out.print("Pong: listening on " + host + ":" + service)

let env = _env
let auth = env.root as AmbientAuth

if ip.ip4() then
UDPSocket.ip4(auth, recover Ping(env, ip) end)
UDPSocket.ip4(env.root, recover Ping(env, ip) end)
elseif ip.ip6() then
UDPSocket.ip6(auth, recover Ping(env, ip) end)
UDPSocket.ip6(env.root, recover Ping(env, ip) end)
else
error
end
Expand Down
15 changes: 6 additions & 9 deletions examples/under_pressure/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,9 @@ class Send is TimerNotify

actor Main
new create(env: Env) =>
try
let auth = env.root as AmbientAuth
let socket = TCPConnection(auth, recover SlowDown(auth, env.out) end,
"", "7669")

let timers = Timers
let t = Timer(Send(socket), 0, 5_000_000)
timers(consume t)
end
let socket = TCPConnection(env.root, recover SlowDown(env.root, env.out) end,
"", "7669")

let timers = Timers
let t = Timer(Send(socket), 0, 5_000_000)
timers(consume t)
18 changes: 7 additions & 11 deletions minimal-cases/issue-629/629.pony
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ actor Tester
connect()

be connect() =>
try
let ctx = SSLContext.set_client_verify(false)
let ssl = ctx.client()
let ctx = SSLContext.set_client_verify(false)
let ssl = ctx.client()

_conn = TCPConnection(_env.root as AmbientAuth,
SSLConnection(ResponseBuilder(_env, this), consume ssl),
"127.0.0.1", "8443".string())
_conn = TCPConnection(_env.root,
SSLConnection(ResponseBuilder(_env, this), consume ssl),
"127.0.0.1", "8443".string())

(_conn as TCPConnection).write(
"GET /index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
else
_env.out.print("failed to connect")
end
(_conn as TCPConnection).write(
"GET /index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")

be reconnect() =>
try
Expand Down
7 changes: 2 additions & 5 deletions packages/backpressure/backpressure.pony
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ class SlowDown is TCPConnectionNotify

actor Main
new create(env: Env) =>
try
let auth = env.root as AmbientAuth
let socket = TCPConnection(auth, recover SlowDown(auth, env.out) end,
"", "7669")
end
let socket = TCPConnection(env.root,
recover SlowDown(env.root, env.out) end, "", "7669")
```

## Caveat
Expand Down
8 changes: 3 additions & 5 deletions packages/builtin/env.pony
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ class val Env
An environment holds the command line and other values injected into the
program by default by the runtime.
"""
let root: (AmbientAuth | None)
let root: AmbientAuth
"""
The root capability.

Can be `None` for artificially constructed `Env` instances.
"""

let input: InputStream
Expand Down Expand Up @@ -58,14 +56,14 @@ class val Env
exitcode = {(code: I32) => @pony_exitcode(code) }

new val create(
root': (AmbientAuth | None),
root': AmbientAuth,
input': InputStream, out': OutStream,
err': OutStream, args': Array[String] val,
vars': Array[String] val,
exitcode': {(I32)} val)
=>
"""
Build an artificial environment. A root capability may be supplied.
Build an artificial environment. A root capability must be supplied.
"""
root = root'
input = input'
Expand Down
Loading