Skip to content

Commit

Permalink
Merge pull request #76 from d5/is_function
Browse files Browse the repository at this point in the history
'is_function' and 'is_callable' builtin functions
  • Loading branch information
d5 authored Feb 3, 2019
2 parents d90f286 + 90aafc8 commit edfb765
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
24 changes: 16 additions & 8 deletions docs/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,32 +185,40 @@ v := bytes(100)

## is_string

Returns `true` if the object is string. Or it returns `false`.
Returns `true` if the object's type is string. Or it returns `false`.

## is_int

Returns `true` if the object is int. Or it returns `false`.
Returns `true` if the object's type is int. Or it returns `false`.

## is_bool

Returns `true` if the object is bool. Or it returns `false`.
Returns `true` if the object's type is bool. Or it returns `false`.

## is_float

Returns `true` if the object is float. Or it returns `false`.
Returns `true` if the object's type is float. Or it returns `false`.

## is_char

Returns `true` if the object is char. Or it returns `false`.
Returns `true` if the object's type is char. Or it returns `false`.

## is_bytes

Returns `true` if the object is bytes. Or it returns `false`.
Returns `true` if the object's type is bytes. Or it returns `false`.

## is_error

Returns `true` if the object is error. Or it returns `false`.
Returns `true` if the object's type is error. Or it returns `false`.

## is_undefined

Returns `true` if the object is undefined. Or it returns `false`.
Returns `true` if the object's type is undefined. Or it returns `false`.

## is_function

Returns `true` if the object's type is function or closure. Or it returns `false`. Note that `is_function` returns `false` for builtin functions and user-provided callable objects.

## is_callable

Returns `true` if the object is callable (e.g. function, closure, builtin function, or user-provided callable objects). Or it returns `false`.
26 changes: 26 additions & 0 deletions objects/builtin_type_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,29 @@ func builtinIsUndefined(args ...Object) (Object, error) {

return FalseValue, nil
}

func builtinIsFunction(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}

switch args[0].(type) {
case *CompiledFunction, *Closure:
return TrueValue, nil
}

return FalseValue, nil
}

func builtinIsCallable(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}

switch args[0].(type) {
case *CompiledFunction, *Closure, Callable: // BuiltinFunction is Callable
return TrueValue, nil
}

return FalseValue, nil
}
8 changes: 8 additions & 0 deletions objects/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ var Builtins = []NamedBuiltinFunc{
Name: "is_undefined",
Func: builtinIsUndefined,
},
{
Name: "is_function",
Func: builtinIsFunction,
},
{
Name: "is_callable",
Func: builtinIsCallable,
},
{
Name: "to_json",
Func: builtinToJSON,
Expand Down
21 changes: 21 additions & 0 deletions runtime/vm_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,25 @@ func TestBuiltinFunction(t *testing.T) {
expect(t, `out = type_name(bytes( 1))`, "bytes")
expect(t, `out = type_name(undefined)`, "undefined")
expect(t, `out = type_name(error("err"))`, "error")
expect(t, `out = type_name(func() {})`, "compiled-function")
expect(t, `a := func(x) { return func() { return x } }; out = type_name(a(5))`, "closure") // closure

// is_function
expect(t, `out = is_function(1)`, false)
expect(t, `out = is_function(func() {})`, true)
expect(t, `out = is_function(func(x) { return x })`, true)
expect(t, `out = is_function(len)`, false) // builtin function
expect(t, `a := func(x) { return func() { return x } }; out = is_function(a)`, true) // function
expect(t, `a := func(x) { return func() { return x } }; out = is_function(a(5))`, true) // closure
expectWithSymbols(t, `out = is_function(x)`, false, SYM{"x": &StringArray{Value: []string{"foo", "bar"}}}) // user object

// is_callable
expect(t, `out = is_callable(1)`, false)
expect(t, `out = is_callable(func() {})`, true)
expect(t, `out = is_callable(func(x) { return x })`, true)
expect(t, `out = is_callable(len)`, true) // builtin function
expect(t, `a := func(x) { return func() { return x } }; out = is_callable(a)`, true) // function
expect(t, `a := func(x) { return func() { return x } }; out = is_callable(a(5))`, true) // closure
expectWithSymbols(t, `out = is_callable(x)`, true, SYM{"x": &StringArray{Value: []string{"foo", "bar"}}}) // user object

}

0 comments on commit edfb765

Please sign in to comment.