<commit_before>// Copyright 2010-2013 RethinkDB, all rights reserved.
#ifndef RDB_PROTOCOL_FUNC_HPP_
#define RDB_PROTOCOL_FUNC_HPP_

#include <map>
#include <string>
#include <utility>
#include <vector>

#include "errors.hpp"
#include <boost/variant/static_visitor.hpp>

#include "containers/counted.hpp"
#include "containers/uuid.hpp"
#include "rdb_protocol/datum.hpp"
#include "rdb_protocol/env.hpp"
#include "rdb_protocol/sym.hpp"
#include "rdb_protocol/term.hpp"
#include "rpc/serialize_macros.hpp"

class js_runner_t;

namespace ql {

class func_visitor_t;

class func_t : public single_threaded_countable_t<func_t>, public pb_rcheckable_t {
public:
    explicit func_t(const protob_t<const Term> &term);
    explicit func_t(const protob_t<const Backtrace> &bt_source);

    virtual ~func_t();

    virtual counted_t<val_t> call(env_t *env, const std::vector<counted_t<const datum_t> > &args) const = 0;

    virtual bool is_deterministic() const = 0;

    // Used by info_term_t.
    virtual std::string print_source() const = 0;

    virtual void visit(func_visitor_t *visitor) const = 0;

    void assert_deterministic(const char *extra_msg) const;

    bool filter_call(env_t *env, counted_t<const datum_t> arg, counted_t<func_t> default_filter_val) const;

    // These are simple, they call the vector version of call.
    counted_t<val_t> call(env_t *env) const;
    counted_t<val_t> call(env_t *env, counted_t<const datum_t> arg) const;
    counted_t<val_t> call(env_t *env, counted_t<const datum_t> arg1, counted_t<const datum_t> arg2) const;

private:
    virtual bool filter_helper(env_t *env, counted_t<const datum_t> arg) const = 0;

    DISABLE_COPYING(func_t);
};

class reql_func_t : public func_t {
public:
    reql_func_t(const protob_t<const Backtrace> backtrace,  // for pb_rcheckable_t
                const var_scope_t &captured_scope,
                std::vector<sym_t> arg_names,
                counted_t<term_t> body);
    ~reql_func_t();

    counted_t<val_t> call(env_t *env, const std::vector<counted_t<const datum_t> > &args) const;
    bool is_deterministic() const;

    std::string print_source() const;

    void visit(func_visitor_t *visitor) const;

private:
    friend class wire_func_construction_visitor_t;
    bool filter_helper(env_t *env, counted_t<const datum_t> arg) const;

    // RSI: Capture just the part of the scope this function uses, not the _whole_ scope.
    var_scope_t captured_scope;

    std::vector<sym_t> arg_names;
    counted_t<term_t> body;
<commit_msg>Removed outdated RSI comment about only capturing the scope we use.<commit_after>// Copyright 2010-2013 RethinkDB, all rights reserved.
#ifndef RDB_PROTOCOL_FUNC_HPP_
#define RDB_PROTOCOL_FUNC_HPP_

#include <map>
#include <string>
#include <utility>
#include <vector>

#include "errors.hpp"
#include <boost/variant/static_visitor.hpp>

#include "containers/counted.hpp"
#include "containers/uuid.hpp"
#include "rdb_protocol/datum.hpp"
#include "rdb_protocol/env.hpp"
#include "rdb_protocol/sym.hpp"
#include "rdb_protocol/term.hpp"
#include "rpc/serialize_macros.hpp"

class js_runner_t;

namespace ql {

class func_visitor_t;

class func_t : public single_threaded_countable_t<func_t>, public pb_rcheckable_t {
public:
    explicit func_t(const protob_t<const Term> &term);
    explicit func_t(const protob_t<const Backtrace> &bt_source);

    virtual ~func_t();

    virtual counted_t<val_t> call(env_t *env, const std::vector<counted_t<const datum_t> > &args) const = 0;

    virtual bool is_deterministic() const = 0;

    // Used by info_term_t.
    virtual std::string print_source() const = 0;

    virtual void visit(func_visitor_t *visitor) const = 0;

    void assert_deterministic(const char *extra_msg) const;

    bool filter_call(env_t *env, counted_t<const datum_t> arg, counted_t<func_t> default_filter_val) const;

    // These are simple, they call the vector version of call.
    counted_t<val_t> call(env_t *env) const;
    counted_t<val_t> call(env_t *env, counted_t<const datum_t> arg) const;
    counted_t<val_t> call(env_t *env, counted_t<const datum_t> arg1, counted_t<const datum_t> arg2) const;

private:
    virtual bool filter_helper(env_t *env, counted_t<const datum_t> arg) const = 0;

    DISABLE_COPYING(func_t);
};

class reql_func_t : public func_t {
public:
    reql_func_t(const protob_t<const Backtrace> backtrace,  // for pb_rcheckable_t
                const var_scope_t &captured_scope,
                std::vector<sym_t> arg_names,
                counted_t<term_t> body);
    ~reql_func_t();

    counted_t<val_t> call(env_t *env, const std::vector<counted_t<const datum_t> > &args) const;
    bool is_deterministic() const;

    std::string print_source() const;

    void visit(func_visitor_t *visitor) const;

private:
    friend class wire_func_construction_visitor_t;
    bool filter_helper(env_t *env, counted_t<const datum_t> arg) const;

    var_scope_t captured_scope;

    std::vector<sym_t> arg_names;
    counted_t<term_t> body;

{'fraction_non_alphanumeric': 0.10187774670395526, 'fraction_numerical': 0.0059928086296444265, 'mean_word_length': 4.619528619528619, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1328707', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1823, 'n_words': 427}