Skip to content

Commit

Permalink
runtime: add exit() builtin function
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Apr 24, 2021
1 parent 45a8319 commit fce6f4e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions SPEC.md
Expand Up @@ -168,6 +168,7 @@ These are the right primitives, but we can build much more sophisticated systems
- `urand(length) => string`: a string of given length containing random bits, safe for cryptography work
- `time() => number`: number of seconds in floating point in UNIX epoch.
- `exec(string, [list], string, callback) => callback`: Exec the command at a given path with given arguments, with a given stdin, call given callback with stdout when exited.
- `exit(number)`: Exit the current process with the given exit code.

### Math

Expand Down
24 changes: 24 additions & 0 deletions pkg/ink/runtime.go
Expand Up @@ -66,6 +66,7 @@ func (ctx *Context) LoadEnvironment() {
ctx.LoadFunc("time", inkTime)
ctx.LoadFunc("wait", inkWait)
ctx.LoadFunc("exec", inkExec)
ctx.LoadFunc("exit", inkExit)

// math
ctx.LoadFunc("sin", inkSin)
Expand Down Expand Up @@ -1362,6 +1363,29 @@ func inkExec(ctx *Context, in []Value) (Value, error) {
}, nil
}

func inkExit(ctx *Context, in []Value) (Value, error) {
if len(in) < 1 {
return nil, Err{
ErrRuntime,
"exit() takes one argument",
}
}

code, isNum := in[0].(NumberValue)

if !isNum {
return nil, Err{
ErrRuntime,
"argument to exit() must be an exit code number",
}
}

os.Exit(int(float64(code)))

// unreachable
return Null, nil
}

func inkSin(ctx *Context, in []Value) (Value, error) {
if len(in) < 1 {
return nil, Err{
Expand Down

0 comments on commit fce6f4e

Please sign in to comment.